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