1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "GCH_HwlBufferAllocator"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include "hwl_buffer_allocator.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 std::atomic<uint64_t> HwlBufferAllocator::global_instance_count_ = 0;
29 
Create(CameraBufferAllocatorHwl * camera_buffer_allocator_hwl)30 std::unique_ptr<IHalBufferAllocator> HwlBufferAllocator::Create(
31     CameraBufferAllocatorHwl* camera_buffer_allocator_hwl) {
32   ATRACE_CALL();
33   auto hwl_buffer_allocator =
34       std::unique_ptr<HwlBufferAllocator>(new HwlBufferAllocator());
35   if (hwl_buffer_allocator == nullptr) {
36     ALOGE("%s: Creating hwl_buffer_allocator failed.", __FUNCTION__);
37     return nullptr;
38   }
39 
40   status_t result =
41       hwl_buffer_allocator->Initialize(camera_buffer_allocator_hwl);
42   if (result != OK) {
43     ALOGE("%s: HwlBuffer Initialize failed.", __FUNCTION__);
44     return nullptr;
45   }
46 
47   std::unique_ptr<IHalBufferAllocator> base_allocator;
48   base_allocator.reset(hwl_buffer_allocator.release());
49 
50   return base_allocator;
51 }
52 
Initialize(CameraBufferAllocatorHwl * camera_buffer_allocator_hwl)53 status_t HwlBufferAllocator::Initialize(
54     CameraBufferAllocatorHwl* camera_buffer_allocator_hwl) {
55   ATRACE_CALL();
56   if (camera_buffer_allocator_hwl == nullptr) {
57     return BAD_VALUE;
58   }
59   camera_buffer_allocator_hwl_ = camera_buffer_allocator_hwl;
60   id_ = ++global_instance_count_;
61   return OK;
62 }
63 
AllocateBuffers(const HalBufferDescriptor & buffer_descriptor,std::vector<buffer_handle_t> * buffers)64 status_t HwlBufferAllocator::AllocateBuffers(
65     const HalBufferDescriptor& buffer_descriptor,
66     std::vector<buffer_handle_t>* buffers) {
67   ATRACE_CALL();
68   HalBufferDescriptor local_descriptor = buffer_descriptor;
69   // some vendor allocator need to be aware of allocator instance id to manage
70   // proper internal logic.
71   local_descriptor.allocator_id_ = id_;
72   status_t res =
73       camera_buffer_allocator_hwl_->AllocateBuffers(local_descriptor, buffers);
74   if (res != OK) {
75     ALOGE("%s: HWL buffer allocation failed: %s (%d).", __FUNCTION__,
76           strerror(-res), res);
77     return res;
78   }
79 
80   return OK;
81 }
82 
FreeBuffers(std::vector<buffer_handle_t> * buffers)83 void HwlBufferAllocator::FreeBuffers(std::vector<buffer_handle_t>* buffers) {
84   ATRACE_CALL();
85   camera_buffer_allocator_hwl_->FreeBuffers(buffers);
86   buffers->clear();
87 }
88 
89 }  // namespace google_camera_hal
90 }  // namespace android
91