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_GrallocBufferAllocator"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <grallocusage/GrallocUsageConversion.h>
24 #include <ui/GraphicBufferAllocator.h>
25 
26 #include "gralloc_buffer_allocator.h"
27 
28 namespace android {
29 namespace google_camera_hal {
30 
Create()31 std::unique_ptr<IHalBufferAllocator> GrallocBufferAllocator::Create() {
32   ATRACE_CALL();
33   auto gralloc_buffer_allocator =
34       std::unique_ptr<GrallocBufferAllocator>(new GrallocBufferAllocator());
35   if (gralloc_buffer_allocator == nullptr) {
36     ALOGE("%s: Creating gralloc_buffer_allocator failed.", __FUNCTION__);
37     return nullptr;
38   }
39 
40   std::unique_ptr<IHalBufferAllocator> base_allocator;
41   base_allocator.reset(gralloc_buffer_allocator.release());
42 
43   return base_allocator;
44 }
45 
ConvertHalBufferDescriptor(const HalBufferDescriptor & hal_buffer_descriptor,BufferDescriptor * gralloc_buffer_descriptor)46 void GrallocBufferAllocator::ConvertHalBufferDescriptor(
47     const HalBufferDescriptor& hal_buffer_descriptor,
48     BufferDescriptor* gralloc_buffer_descriptor) {
49   ATRACE_CALL();
50   // For BLOB format, the gralloc buffer width should be the actual size, and
51   // height should be 1.
52   if (hal_buffer_descriptor.format == HAL_PIXEL_FORMAT_BLOB) {
53     gralloc_buffer_descriptor->width =
54         hal_buffer_descriptor.width * hal_buffer_descriptor.height;
55     gralloc_buffer_descriptor->height = 1;
56   } else {
57     gralloc_buffer_descriptor->width = hal_buffer_descriptor.width;
58     gralloc_buffer_descriptor->height = hal_buffer_descriptor.height;
59   }
60   gralloc_buffer_descriptor->format = hal_buffer_descriptor.format;
61   gralloc_buffer_descriptor->producer_flags =
62       hal_buffer_descriptor.producer_flags;
63   gralloc_buffer_descriptor->consumer_flags =
64       hal_buffer_descriptor.consumer_flags;
65   gralloc_buffer_descriptor->num_buffers =
66       hal_buffer_descriptor.immediate_num_buffers;
67 }
68 
AllocateBuffers(const HalBufferDescriptor & buffer_descriptor,std::vector<buffer_handle_t> * buffers)69 status_t GrallocBufferAllocator::AllocateBuffers(
70     const HalBufferDescriptor& buffer_descriptor,
71     std::vector<buffer_handle_t>* buffers) {
72   ATRACE_CALL();
73 
74   BufferDescriptor gralloc_buffer_descriptor{0};
75   ConvertHalBufferDescriptor(buffer_descriptor, &gralloc_buffer_descriptor);
76 
77   status_t err = OK;
78   uint32_t stride = 0;
79   for (uint32_t i = 0; i < gralloc_buffer_descriptor.num_buffers; i++) {
80     buffer_handle_t buffer;
81     err = GraphicBufferAllocator::get().allocate(
82         gralloc_buffer_descriptor.width, gralloc_buffer_descriptor.height,
83         gralloc_buffer_descriptor.format, /*layerCount*/ 1u,
84         android_convertGralloc1To0Usage(
85             gralloc_buffer_descriptor.producer_flags,
86             gralloc_buffer_descriptor.consumer_flags),
87         &buffer, &stride, "GCHGrallocBufferAllocator");
88     if (err != OK) {
89       ALOGE("%s: Failed to allocate gralloc buffer: %s(%d)", __FUNCTION__,
90             strerror(-err), err);
91       break;
92     }
93     buffers->push_back(buffer);
94   }
95 
96   if (err != OK) {
97     FreeBuffers(buffers);
98     return INVALID_OPERATION;
99   }
100 
101   return OK;
102 }
103 
FreeBuffers(std::vector<buffer_handle_t> * buffers)104 void GrallocBufferAllocator::FreeBuffers(std::vector<buffer_handle_t>* buffers) {
105   ATRACE_CALL();
106   for (auto buffer : *buffers) {
107     if (buffer != nullptr) {
108       GraphicBufferAllocator::get().free(buffer);
109     }
110   }
111   buffers->clear();
112 }
113 
114 }  // namespace google_camera_hal
115 }  // namespace android
116