• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gralloc_buffer_allocator.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
Create()28 std::unique_ptr<IHalBufferAllocator> GrallocBufferAllocator::Create() {
29   ATRACE_CALL();
30   auto gralloc_buffer =
31       std::unique_ptr<GrallocBufferAllocator>(new GrallocBufferAllocator());
32   if (gralloc_buffer == nullptr) {
33     ALOGE("%s: Creating gralloc_buffer failed.", __FUNCTION__);
34     return nullptr;
35   }
36 
37   status_t result = gralloc_buffer->Initialize();
38   if (result != OK) {
39     ALOGE("%s: GrallocBuffer Initialize failed.", __FUNCTION__);
40     return nullptr;
41   }
42 
43   std::unique_ptr<IHalBufferAllocator> base_allocator;
44   base_allocator.reset(gralloc_buffer.release());
45 
46   return base_allocator;
47 }
48 
~GrallocBufferAllocator()49 GrallocBufferAllocator::~GrallocBufferAllocator() {
50   if (device_ != nullptr) {
51     gralloc1_close(device_);
52   }
53 }
54 
Initialize()55 status_t GrallocBufferAllocator::Initialize() {
56   ATRACE_CALL();
57   int32_t error =
58       hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t**)&module_);
59 
60   if (error < 0) {
61     ALOGE("%s: Could not load GRALLOC HAL module: %d (%s)", __FUNCTION__, error,
62           strerror(-error));
63     return INVALID_OPERATION;
64   }
65 
66   gralloc1_open(module_, &device_);
67   if (device_ == nullptr) {
68     ALOGE("%s: gralloc1 open failed", __FUNCTION__);
69     return INVALID_OPERATION;
70   }
71 
72   InitGrallocInterface(GRALLOC1_FUNCTION_CREATE_DESCRIPTOR, &create_descriptor_);
73   InitGrallocInterface(GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR,
74                        &destroy_descriptor_);
75   InitGrallocInterface(GRALLOC1_FUNCTION_SET_DIMENSIONS, &set_dimensions_);
76   InitGrallocInterface(GRALLOC1_FUNCTION_SET_FORMAT, &set_format_);
77   InitGrallocInterface(GRALLOC1_FUNCTION_SET_CONSUMER_USAGE,
78                        &set_consumer_usage_);
79   InitGrallocInterface(GRALLOC1_FUNCTION_SET_PRODUCER_USAGE,
80                        &set_producer_usage_);
81   InitGrallocInterface(GRALLOC1_FUNCTION_GET_STRIDE, &get_stride_);
82   InitGrallocInterface(GRALLOC1_FUNCTION_ALLOCATE, &allocate_);
83   InitGrallocInterface(GRALLOC1_FUNCTION_RELEASE, &release_);
84   return OK;
85 }
86 
87 template <typename T>
InitGrallocInterface(gralloc1_function_descriptor_t desc,T * output_function)88 void GrallocBufferAllocator::InitGrallocInterface(
89     gralloc1_function_descriptor_t desc, T* output_function) {
90   ATRACE_CALL();
91   auto function = device_->getFunction(device_, desc);
92   if (!function) {
93     ALOGE("%s: failed to get gralloc1 function %d", __FUNCTION__, desc);
94   }
95   *output_function = reinterpret_cast<T>(function);
96 }
97 
SetupDescriptor(const BufferDescriptor & buffer_descriptor,gralloc1_buffer_descriptor_t * output_descriptor)98 status_t GrallocBufferAllocator::SetupDescriptor(
99     const BufferDescriptor& buffer_descriptor,
100     gralloc1_buffer_descriptor_t* output_descriptor) {
101   ATRACE_CALL();
102   int32_t error =
103       set_dimensions_(device_, *output_descriptor, buffer_descriptor.width,
104                       buffer_descriptor.height);
105   if (error != GRALLOC1_ERROR_NONE) {
106     ALOGE("%s: set_dimensions failed", __FUNCTION__);
107     return INVALID_OPERATION;
108   }
109 
110   error = set_format_(device_, *output_descriptor, buffer_descriptor.format);
111   if (error != GRALLOC1_ERROR_NONE) {
112     ALOGE("%s: set_format failed", __FUNCTION__);
113     return INVALID_OPERATION;
114   }
115 
116   error = set_producer_usage_(device_, *output_descriptor,
117                               buffer_descriptor.producer_flags);
118   if (error != GRALLOC1_ERROR_NONE) {
119     ALOGE("%s: set_producer_usage failed", __FUNCTION__);
120     return INVALID_OPERATION;
121   }
122 
123   error = set_consumer_usage_(device_, *output_descriptor,
124                               buffer_descriptor.consumer_flags);
125   if (error != GRALLOC1_ERROR_NONE) {
126     ALOGE("%s: set_consumer_usage_ failed", __FUNCTION__);
127     return INVALID_OPERATION;
128   }
129 
130   return OK;
131 }
132 
ConvertHalBufferDescriptor(const HalBufferDescriptor & hal_buffer_descriptor,BufferDescriptor * gralloc_buffer_descriptor)133 void GrallocBufferAllocator::ConvertHalBufferDescriptor(
134     const HalBufferDescriptor& hal_buffer_descriptor,
135     BufferDescriptor* gralloc_buffer_descriptor) {
136   ATRACE_CALL();
137   // For BLOB format, the gralloc buffer width should be the actual size, and
138   // height should be 1.
139   if (hal_buffer_descriptor.format == HAL_PIXEL_FORMAT_BLOB) {
140     gralloc_buffer_descriptor->width =
141         hal_buffer_descriptor.width * hal_buffer_descriptor.height;
142     gralloc_buffer_descriptor->height = 1;
143   } else {
144     gralloc_buffer_descriptor->width = hal_buffer_descriptor.width;
145     gralloc_buffer_descriptor->height = hal_buffer_descriptor.height;
146   }
147   gralloc_buffer_descriptor->format = hal_buffer_descriptor.format;
148   gralloc_buffer_descriptor->producer_flags =
149       hal_buffer_descriptor.producer_flags;
150   gralloc_buffer_descriptor->consumer_flags =
151       hal_buffer_descriptor.consumer_flags;
152   gralloc_buffer_descriptor->num_buffers =
153       hal_buffer_descriptor.immediate_num_buffers;
154 }
155 
AllocateBuffers(const HalBufferDescriptor & buffer_descriptor,std::vector<buffer_handle_t> * buffers)156 status_t GrallocBufferAllocator::AllocateBuffers(
157     const HalBufferDescriptor& buffer_descriptor,
158     std::vector<buffer_handle_t>* buffers) {
159   ATRACE_CALL();
160   // This function runs four operations.
161   // 1. create descriptor
162   // 2. setup descriptor
163   // 3. allocate buffer via descriptor
164   // 4. destroy descriptor_
165   gralloc1_buffer_descriptor_t descriptor;
166   int32_t error = create_descriptor_(device_, &descriptor);
167   if (error != GRALLOC1_ERROR_NONE) {
168     ALOGE("%s: create descriptor failed", __FUNCTION__);
169     return INVALID_OPERATION;
170   }
171 
172   BufferDescriptor gralloc_buffer_descriptor{0};
173   ConvertHalBufferDescriptor(buffer_descriptor, &gralloc_buffer_descriptor);
174   status_t result = SetupDescriptor(gralloc_buffer_descriptor, &descriptor);
175   if (result != OK) {
176     ALOGE("%s: SetupDescriptor failed", __FUNCTION__);
177     destroy_descriptor_(device_, descriptor);
178     return INVALID_OPERATION;
179   }
180 
181   uint32_t stride = 0, temp_stride = 0;
182   for (uint32_t i = 0; i < gralloc_buffer_descriptor.num_buffers; i++) {
183     buffer_handle_t buffer;
184     error = allocate_(device_, 1, &descriptor, &buffer);
185     if (error != GRALLOC1_ERROR_NONE) {
186       ALOGE("%s: buffer(%d) allocate failed", __FUNCTION__, i);
187       break;
188     }
189     buffers->push_back(buffer);
190 
191     error = get_stride_(device_, buffer, &temp_stride);
192     if (error != GRALLOC1_ERROR_NONE) {
193       ALOGE("%s: buffer(%d) get_stride failed", __FUNCTION__, i);
194       break;
195     }
196     // Check non-uniform strides
197     if (stride == 0) {
198       stride = temp_stride;
199     } else if (stride != temp_stride) {
200       ALOGE("%s: non-uniform strides (%d) != (%d)", __FUNCTION__, stride,
201             temp_stride);
202       error = GRALLOC1_ERROR_UNSUPPORTED;
203       break;
204     }
205   }
206 
207   destroy_descriptor_(device_, descriptor);
208   if (error != GRALLOC1_ERROR_NONE) {
209     FreeBuffers(buffers);
210     return INVALID_OPERATION;
211   }
212   return OK;
213 }
214 
FreeBuffers(std::vector<buffer_handle_t> * buffers)215 void GrallocBufferAllocator::FreeBuffers(std::vector<buffer_handle_t>* buffers) {
216   ATRACE_CALL();
217   for (auto buffer : *buffers) {
218     if (buffer != nullptr) {
219       release_(device_, buffer);
220     }
221   }
222   buffers->clear();
223 }
224 
225 }  // namespace google_camera_hal
226 }  // namespace android
227