1 /*
2  * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
3 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *   * Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *   * Redistributions in binary form must reproduce the above
10  *     copyright notice, this list of conditions and the following
11  *     disclaimer in the documentation and/or other materials provided
12  *     with the distribution.
13  *   * Neither the name of The Linux Foundation nor the names of its
14  *     contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <cutils/log.h>
31 #include <sync/sync.h>
32 #include <algorithm>
33 #include <sstream>
34 #include <string>
35 
36 #include "gr_device_impl.h"
37 #include "gr_buf_descriptor.h"
38 #include "gralloc_priv.h"
39 #include "qd_utils.h"
40 #include "qdMetaData.h"
41 #include "gr_utils.h"
42 
43 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
44 
45 int gralloc_device_close(struct hw_device_t *device);
46 
47 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
48 
49 struct gralloc_module_t HAL_MODULE_INFO_SYM = {
50   .common = {
51     .tag = HARDWARE_MODULE_TAG,
52     .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
53     .hal_api_version    = HARDWARE_HAL_API_VERSION,
54     .id = GRALLOC_HARDWARE_MODULE_ID,
55     .name = "Graphics Memory Module",
56     .author = "Code Aurora Forum",
57     .methods = &gralloc_module_methods,
58     .dso = 0,
59     .reserved = {0},
60   },
61 };
62 
gralloc_device_open(const struct hw_module_t * module,const char * name,hw_device_t ** device)63 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
64   int status = -EINVAL;
65   if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
66     gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
67     *device = reinterpret_cast<hw_device_t *>(dev);
68     if (dev) {
69       status = 0;
70     } else {
71       ALOGE("Fatal error opening gralloc1 device");
72     }
73   }
74   return status;
75 }
76 
77 namespace gralloc1 {
78 
GrallocImpl(const hw_module_t * module)79 GrallocImpl::GrallocImpl(const hw_module_t *module) {
80   common.tag = HARDWARE_DEVICE_TAG;
81   common.version = GRALLOC_MODULE_API_VERSION_1_0;
82   common.module = const_cast<hw_module_t *>(module);
83   common.close = CloseDevice;
84   getFunction = GetFunction;
85   getCapabilities = GetCapabilities;
86 
87   initalized_ = Init();
88 }
89 
Init()90 bool GrallocImpl::Init() {
91   buf_mgr_ = BufferManager::GetInstance();
92   return buf_mgr_ != nullptr;
93 }
94 
~GrallocImpl()95 GrallocImpl::~GrallocImpl() {
96 }
97 
CloseDevice(hw_device_t * device __unused)98 int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
99   // No-op since the gralloc device is a singleton
100   return 0;
101 }
102 
GetCapabilities(struct gralloc1_device * device,uint32_t * out_count,int32_t * out_capabilities)103 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
104                                   int32_t  /*gralloc1_capability_t*/ *out_capabilities) {
105   if (device != nullptr) {
106     if (out_capabilities != nullptr && *out_count >= 3) {
107       out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
108       out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
109       out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE;
110     }
111     *out_count = 3;
112   }
113   return;
114 }
115 
GetFunction(gralloc1_device_t * device,int32_t function)116 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
117   if (!device) {
118     return NULL;
119   }
120 
121   switch (function) {
122     case GRALLOC1_FUNCTION_DUMP:
123       return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
124     case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
125       return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
126     case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
127       return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
128     case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
129       return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
130     case GRALLOC1_FUNCTION_SET_DIMENSIONS:
131       return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
132     case GRALLOC1_FUNCTION_SET_FORMAT:
133       return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
134     case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
135       return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
136     case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
137       return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
138     case GRALLOC1_FUNCTION_GET_BACKING_STORE:
139       return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
140     case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
141       return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
142     case GRALLOC1_FUNCTION_GET_DIMENSIONS:
143       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
144     case GRALLOC1_FUNCTION_GET_FORMAT:
145       return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
146     case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
147       return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
148     case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
149       return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
150     case GRALLOC1_FUNCTION_GET_STRIDE:
151       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
152     case GRALLOC1_FUNCTION_ALLOCATE:
153       return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
154     case GRALLOC1_FUNCTION_RETAIN:
155       return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
156     case GRALLOC1_FUNCTION_RELEASE:
157       return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
158     case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
159       return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
160     case GRALLOC1_FUNCTION_LOCK:
161       return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
162     case GRALLOC1_FUNCTION_LOCK_FLEX:
163       return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
164     case GRALLOC1_FUNCTION_UNLOCK:
165       return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
166     case GRALLOC1_FUNCTION_PERFORM:
167       return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
168     default:
169       ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
170       return NULL;
171   }
172 
173   return NULL;
174 }
175 
Dump(gralloc1_device_t * device,uint32_t * out_size,char * out_buffer)176 gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
177                                    char *out_buffer) {
178   if (!device) {
179     ALOGE("Gralloc Error : device=%p", (void *)device);
180     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
181   }
182   const size_t max_dump_size = 8192;
183   if (out_buffer == nullptr) {
184     *out_size = max_dump_size;
185   } else {
186     std::ostringstream os;
187     os << "-------------------------------" << std::endl;
188     os << "QTI gralloc dump:" << std::endl;
189     os << "-------------------------------" << std::endl;
190     GrallocImpl const *dev = GRALLOC_IMPL(device);
191     dev->buf_mgr_->Dump(&os);
192     os << "-------------------------------" << std::endl;
193     auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
194     *out_size = UINT(copied);
195   }
196 
197   return GRALLOC1_ERROR_NONE;
198 }
199 
CheckDeviceAndHandle(gralloc1_device_t * device,buffer_handle_t buffer)200 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
201                                                    buffer_handle_t buffer) {
202   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
203   if (!device || (private_handle_t::validate(hnd) != 0)) {
204     ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
205     return GRALLOC1_ERROR_BAD_HANDLE;
206   }
207 
208   return GRALLOC1_ERROR_NONE;
209 }
210 
CreateBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t * out_descriptor)211 gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
212                                                      gralloc1_buffer_descriptor_t *out_descriptor) {
213   if (!device) {
214     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
215   }
216   GrallocImpl const *dev = GRALLOC_IMPL(device);
217   return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
218 }
219 
DestroyBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)220 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
221                                                       gralloc1_buffer_descriptor_t descriptor) {
222   if (!device) {
223     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
224   }
225   GrallocImpl const *dev = GRALLOC_IMPL(device);
226   return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
227 }
228 
SetConsumerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_consumer_usage_t usage)229 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
230                                                gralloc1_buffer_descriptor_t descriptor,
231                                                gralloc1_consumer_usage_t usage) {
232   if (!device) {
233     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
234   } else {
235     GrallocImpl const *dev = GRALLOC_IMPL(device);
236     return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
237                                                        &BufferDescriptor::SetConsumerUsage, usage);
238   }
239 }
240 
SetBufferDimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)241 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
242                                                   gralloc1_buffer_descriptor_t descriptor,
243                                                   uint32_t width, uint32_t height) {
244   if (!device) {
245     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
246   } else {
247     GrallocImpl const *dev = GRALLOC_IMPL(device);
248     return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
249                                                        &BufferDescriptor::SetDimensions,
250                                                        INT(width), INT(height));
251   }
252 }
253 
SetColorFormat(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,int32_t format)254 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
255                                              gralloc1_buffer_descriptor_t descriptor,
256                                              int32_t format) {
257   if (!device) {
258     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
259   } else {
260     GrallocImpl const *dev = GRALLOC_IMPL(device);
261     return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
262                                                        &BufferDescriptor::SetColorFormat, format);
263   }
264 }
265 
SetLayerCount(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layer_count)266 gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
267                                             gralloc1_buffer_descriptor_t descriptor,
268                                             uint32_t layer_count) {
269   if (!device) {
270     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
271   } else {
272     GrallocImpl const *dev = GRALLOC_IMPL(device);
273     return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
274                                                        &BufferDescriptor::SetLayerCount,
275                                                        layer_count);
276   }
277 }
278 
SetProducerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_producer_usage_t usage)279 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
280                                                gralloc1_buffer_descriptor_t descriptor,
281                                                gralloc1_producer_usage_t usage) {
282   if (!device) {
283     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
284   } else {
285     GrallocImpl const *dev = GRALLOC_IMPL(device);
286     return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
287                                                        &BufferDescriptor::SetProducerUsage, usage);
288   }
289 }
290 
GetBackingStore(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * out_backstore)291 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
292                                               gralloc1_backing_store_t *out_backstore) {
293   if (!device || !buffer) {
294     return GRALLOC1_ERROR_BAD_HANDLE;
295   }
296 
297   *out_backstore =
298       static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
299 
300   return GRALLOC1_ERROR_NONE;
301 }
302 
GetConsumerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_consumer_usage_t * outUsage)303 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
304                                                gralloc1_consumer_usage_t *outUsage) {
305   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
306   if (status == GRALLOC1_ERROR_NONE) {
307     *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
308   }
309 
310   return status;
311 }
312 
GetBufferDimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)313 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
314                                                   uint32_t *outWidth, uint32_t *outHeight) {
315   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
316   if (status == GRALLOC1_ERROR_NONE) {
317     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
318     *outWidth = UINT(hnd->GetUnalignedWidth());
319     *outHeight = UINT(hnd->GetUnalignedHeight());
320   }
321 
322   return status;
323 }
324 
GetColorFormat(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)325 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
326                                              int32_t *outFormat) {
327   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
328   if (status == GRALLOC1_ERROR_NONE) {
329     *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
330   }
331 
332   return status;
333 }
334 
GetLayerCount(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)335 gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
336                                             uint32_t *outLayerCount) {
337   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
338   if (status == GRALLOC1_ERROR_NONE) {
339     *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
340   }
341 
342   return status;
343 }
344 
GetProducerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t * outUsage)345 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
346                                                gralloc1_producer_usage_t *outUsage) {
347   if (!outUsage) {
348     return GRALLOC1_ERROR_BAD_VALUE;
349   }
350 
351   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
352   if (status == GRALLOC1_ERROR_NONE) {
353     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
354     *outUsage = hnd->GetProducerUsage();
355   }
356 
357   return status;
358 }
359 
GetBufferStride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)360 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
361                                               uint32_t *outStride) {
362   if (!outStride) {
363     return GRALLOC1_ERROR_BAD_VALUE;
364   }
365 
366   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
367   if (status == GRALLOC1_ERROR_NONE) {
368     *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
369   }
370 
371   return status;
372 }
373 
AllocateBuffers(gralloc1_device_t * device,uint32_t num_descriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * out_buffers)374 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
375                                               const gralloc1_buffer_descriptor_t *descriptors,
376                                               buffer_handle_t *out_buffers) {
377   if (!num_descriptors || !descriptors) {
378     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
379   }
380 
381   if (!device) {
382     return GRALLOC1_ERROR_BAD_VALUE;
383   }
384 
385   GrallocImpl const *dev = GRALLOC_IMPL(device);
386   gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
387                                                            out_buffers);
388 
389   return status;
390 }
391 
RetainBuffer(gralloc1_device_t * device,buffer_handle_t buffer)392 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
393   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
394   if (status == GRALLOC1_ERROR_NONE) {
395     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
396     GrallocImpl const *dev = GRALLOC_IMPL(device);
397     status = dev->buf_mgr_->RetainBuffer(hnd);
398   }
399 
400   return status;
401 }
402 
ReleaseBuffer(gralloc1_device_t * device,buffer_handle_t buffer)403 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
404   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
405   if (status == GRALLOC1_ERROR_NONE) {
406     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
407     GrallocImpl const *dev = GRALLOC_IMPL(device);
408     status = dev->buf_mgr_->ReleaseBuffer(hnd);
409   }
410 
411   return status;
412 }
413 
GetNumFlexPlanes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * out_num_planes)414 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
415                                                uint32_t *out_num_planes) {
416   if (!out_num_planes) {
417     return GRALLOC1_ERROR_BAD_VALUE;
418   }
419 
420   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
421   if (status == GRALLOC1_ERROR_NONE) {
422     GrallocImpl const *dev = GRALLOC_IMPL(device);
423     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
424     status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
425   }
426   return status;
427 }
428 
CloseFdIfValid(int fd)429 static inline void CloseFdIfValid(int fd) {
430   if (fd > 0) {
431     close(fd);
432   }
433 }
434 
LockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage,const gralloc1_rect_t * region,void ** out_data,int32_t acquire_fence)435 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
436                                          gralloc1_producer_usage_t prod_usage,
437                                          gralloc1_consumer_usage_t cons_usage,
438                                          const gralloc1_rect_t *region, void **out_data,
439                                          int32_t acquire_fence) {
440   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
441   if (status != GRALLOC1_ERROR_NONE || !out_data ||
442       !region) {  // currently we ignore the region/rect client wants to lock
443     CloseFdIfValid(acquire_fence);
444     return status;
445   }
446 
447   if (acquire_fence > 0) {
448     int error = sync_wait(acquire_fence, 1000);
449     CloseFdIfValid(acquire_fence);
450     if (error < 0) {
451       ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
452       return GRALLOC1_ERROR_UNDEFINED;
453     }
454   }
455 
456   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
457   GrallocImpl const *dev = GRALLOC_IMPL(device);
458 
459   // Either producer usage or consumer usage must be *_USAGE_NONE
460   if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
461       (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
462     // Current gralloc1 clients do not satisfy this restriction.
463     // See b/33588773 for details
464     // return GRALLOC1_ERROR_BAD_VALUE;
465   }
466 
467   // TODO(user): Need to check if buffer was allocated with the same flags
468   status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
469   *out_data = reinterpret_cast<void *>(hnd->base);
470 
471   return status;
472 }
473 
LockFlex(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage,const gralloc1_rect_t * region,struct android_flex_layout * out_flex_layout,int32_t acquire_fence)474 gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
475                                        gralloc1_producer_usage_t prod_usage,
476                                        gralloc1_consumer_usage_t cons_usage,
477                                        const gralloc1_rect_t *region,
478                                        struct android_flex_layout *out_flex_layout,
479                                        int32_t acquire_fence) {
480   if (!out_flex_layout) {
481     CloseFdIfValid(acquire_fence);
482     return GRALLOC1_ERROR_BAD_VALUE;
483   }
484 
485   void *out_data {};
486   gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
487                                                     &out_data, acquire_fence);
488   if (status != GRALLOC1_ERROR_NONE) {
489     return status;
490   }
491 
492   GrallocImpl const *dev = GRALLOC_IMPL(device);
493   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
494   dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
495   return status;
496 }
497 
UnlockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * release_fence)498 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
499                                            int32_t *release_fence) {
500   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
501   if (status != GRALLOC1_ERROR_NONE) {
502     return status;
503   }
504 
505   if (!release_fence) {
506     return GRALLOC1_ERROR_BAD_VALUE;
507   }
508 
509   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
510   GrallocImpl const *dev = GRALLOC_IMPL(device);
511 
512   *release_fence = -1;
513 
514   return dev->buf_mgr_->UnlockBuffer(hnd);
515 }
516 
Gralloc1Perform(gralloc1_device_t * device,int operation,...)517 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
518   if (!device) {
519     return GRALLOC1_ERROR_BAD_VALUE;
520   }
521 
522   va_list args;
523   va_start(args, operation);
524   GrallocImpl const *dev = GRALLOC_IMPL(device);
525   gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
526   va_end(args);
527 
528   return err;
529 }
530 
531 }  // namespace gralloc1
532