1 /*
2  * Copyright (c) 2016-2019, 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 <log/log.h>
32 #include <cutils/trace.h>
33 #include <sync/sync.h>
34 #include <utils/Trace.h>
35 #include <algorithm>
36 #include <sstream>
37 #include <string>
38 
39 #include "gr_buf_descriptor.h"
40 #include "gr_device_impl.h"
41 #include "gr_utils.h"
42 #include "gralloc_priv.h"
43 #include "qdMetaData.h"
44 #include "qd_utils.h"
45 
46 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
47 
48 int gralloc_device_close(struct hw_device_t *device);
49 
50 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
51 
52 struct gralloc_module_t HAL_MODULE_INFO_SYM = {
53     // clang-format off
54     .common = {
55             .tag = HARDWARE_MODULE_TAG,
56             .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
57             .hal_api_version = HARDWARE_HAL_API_VERSION,
58             .id = GRALLOC_HARDWARE_MODULE_ID,
59             .name = "Graphics Memory Module",
60             .author = "Code Aurora Forum",
61             .methods = &gralloc_module_methods,
62             .dso = 0,
63             .reserved = {0},
64         },
65     // clang-format on
66 };
67 
gralloc_device_open(const struct hw_module_t * module,const char * name,hw_device_t ** device)68 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
69   int status = -EINVAL;
70   if (module && device && !strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
71     gralloc::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc::GrallocImpl::GetInstance(module);
72     *device = reinterpret_cast<hw_device_t *>(dev);
73     if (dev) {
74       status = 0;
75     } else {
76       ALOGE("Fatal error opening gralloc1 device");
77     }
78   }
79   return status;
80 }
81 
82 namespace gralloc {
83 
84 std::atomic<uint64_t> GrallocImpl::next_descriptor_id_(1);
85 
GrallocImpl(const hw_module_t * module)86 GrallocImpl::GrallocImpl(const hw_module_t *module) {
87   common.tag = HARDWARE_DEVICE_TAG;
88   common.version = GRALLOC_MODULE_API_VERSION_1_0;
89   common.module = const_cast<hw_module_t *>(module);
90   common.close = CloseDevice;
91   getFunction = GetFunction;
92   getCapabilities = GetCapabilities;
93 
94   initialized_ = Init();
95 }
96 
ToError(Error error)97 inline gralloc1_error_t ToError(Error error) {
98   switch (error) {
99     case Error::NONE:
100       return GRALLOC1_ERROR_NONE;
101     case Error::BAD_DESCRIPTOR:
102       return GRALLOC1_ERROR_BAD_DESCRIPTOR;
103     case Error::BAD_BUFFER:
104       return GRALLOC1_ERROR_BAD_HANDLE;
105     case Error::BAD_VALUE:
106       return GRALLOC1_ERROR_BAD_VALUE;
107     case Error::NO_RESOURCES:
108       return GRALLOC1_ERROR_NO_RESOURCES;
109     case Error::UNSUPPORTED:
110     default:
111       return GRALLOC1_ERROR_UNSUPPORTED;
112   }
113 }
114 
ProducerUsageToBufferUsage(uint64_t producer_usage)115 static uint64_t ProducerUsageToBufferUsage(uint64_t /*gralloc1_producer_usage_t*/ producer_usage) {
116   uint64_t usage = producer_usage & ~(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
117                                       GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
118   if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) ==
119       GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) {
120     usage |= BufferUsage::CPU_READ_OFTEN;
121   } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) ==
122              GRALLOC1_PRODUCER_USAGE_CPU_READ) {
123     usage |= BufferUsage::CPU_READ_RARELY;
124   }
125 
126   if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) ==
127       GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
128     usage |= BufferUsage::CPU_WRITE_OFTEN;
129   } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) ==
130              GRALLOC1_PRODUCER_USAGE_CPU_WRITE) {
131     usage |= BufferUsage::CPU_WRITE_RARELY;
132   }
133   return usage;
134 }
135 
ConsumerUsageToBufferUsage(uint64_t consumer_usage)136 static uint64_t ConsumerUsageToBufferUsage(uint64_t /*gralloc1_consumer_usage_t*/ consumer_usage) {
137   uint64_t usage = consumer_usage & ~(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
138   if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
139       GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
140     usage |= BufferUsage::CPU_READ_OFTEN;
141   } else if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) ==
142              GRALLOC1_CONSUMER_USAGE_CPU_READ) {
143     usage |= BufferUsage::CPU_READ_RARELY;
144   }
145   return usage;
146 }
147 
Init()148 bool GrallocImpl::Init() {
149   buf_mgr_ = BufferManager::GetInstance();
150   return buf_mgr_ != nullptr;
151 }
152 
~GrallocImpl()153 GrallocImpl::~GrallocImpl() {}
154 
CreateBufferDescriptorLocked(gralloc1_buffer_descriptor_t * descriptor_id)155 gralloc1_error_t GrallocImpl::CreateBufferDescriptorLocked(
156     gralloc1_buffer_descriptor_t *descriptor_id) {
157   std::lock_guard<std::mutex> lock(descriptor_lock_);
158   auto descriptor = std::make_shared<BufferDescriptor>(next_descriptor_id_++);
159   *descriptor_id = static_cast<gralloc1_buffer_descriptor_t>(descriptor->GetId());
160   descriptors_map_.emplace(*descriptor_id, descriptor);
161   return GRALLOC1_ERROR_NONE;
162 }
163 
DestroyBufferDescriptorLocked(gralloc1_buffer_descriptor_t descriptor_id)164 gralloc1_error_t GrallocImpl::DestroyBufferDescriptorLocked(
165     gralloc1_buffer_descriptor_t descriptor_id) {
166   std::lock_guard<std::mutex> lock(descriptor_lock_);
167   const auto descriptor = descriptors_map_.find(descriptor_id);
168   if (descriptor == descriptors_map_.end()) {
169     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
170   }
171   descriptors_map_.erase(descriptor);
172   return GRALLOC1_ERROR_NONE;
173 }
174 
CloseDevice(hw_device_t * device __unused)175 int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
176   // No-op since the gralloc device is a singleton
177   return 0;
178 }
179 
GetCapabilities(struct gralloc1_device * device,uint32_t * out_count,int32_t * out_capabilities)180 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
181                                   int32_t /*gralloc1_capability_t*/ *out_capabilities) {
182   if (device != nullptr && out_count != nullptr) {
183     if (out_capabilities != nullptr && *out_count >= 3) {
184       out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
185       out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
186       out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE;
187     }
188     *out_count = 3;
189   }
190   return;
191 }
192 
GetFunction(gralloc1_device_t * device,int32_t function)193 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
194   if (!device) {
195     return NULL;
196   }
197 
198   switch (function) {
199     case GRALLOC1_FUNCTION_DUMP:
200       return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
201     case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
202       return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
203     case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
204       return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
205     case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
206       return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
207     case GRALLOC1_FUNCTION_SET_DIMENSIONS:
208       return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
209     case GRALLOC1_FUNCTION_SET_FORMAT:
210       return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
211     case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
212       return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
213     case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
214       return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
215     case GRALLOC1_FUNCTION_GET_BACKING_STORE:
216       return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
217     case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
218       return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
219     case GRALLOC1_FUNCTION_GET_DIMENSIONS:
220       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
221     case GRALLOC1_FUNCTION_GET_FORMAT:
222       return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
223     case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
224       return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
225     case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
226       return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
227     case GRALLOC1_FUNCTION_GET_STRIDE:
228       return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
229     case GRALLOC1_FUNCTION_ALLOCATE:
230       return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
231     case GRALLOC1_FUNCTION_RETAIN:
232       return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
233     case GRALLOC1_FUNCTION_RELEASE:
234       return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
235     case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
236       return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
237     case GRALLOC1_FUNCTION_LOCK:
238       return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
239     case GRALLOC1_FUNCTION_LOCK_FLEX:
240       return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
241     case GRALLOC1_FUNCTION_UNLOCK:
242       return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
243     case GRALLOC1_FUNCTION_PERFORM:
244       return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
245     default:
246       ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
247       return NULL;
248   }
249 
250   return NULL;
251 }
252 
Dump(gralloc1_device_t * device,uint32_t * out_size,char * out_buffer)253 void GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
254                                    char *out_buffer) {
255   if (!device || !out_size) {
256     ALOGE("Gralloc Error : device=%p", (void *)device);
257     return;
258   }
259   const size_t max_dump_size = 8192;
260   if (out_buffer == nullptr) {
261     *out_size = max_dump_size;
262   } else {
263     std::ostringstream os;
264     os << "-------------------------------" << std::endl;
265     os << "QTI gralloc dump:" << std::endl;
266     os << "-------------------------------" << std::endl;
267     GrallocImpl const *dev = GRALLOC_IMPL(device);
268     dev->buf_mgr_->Dump(&os);
269     os << "-------------------------------" << std::endl;
270     auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
271     *out_size = UINT(copied);
272   }
273 
274   return;
275 }
276 
CheckDeviceAndHandle(gralloc1_device_t * device,buffer_handle_t buffer)277 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
278                                                    buffer_handle_t buffer) {
279   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
280   if (!device || (private_handle_t::validate(hnd) != 0)) {
281     ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
282     return GRALLOC1_ERROR_BAD_HANDLE;
283   }
284 
285   return GRALLOC1_ERROR_NONE;
286 }
287 
CreateBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t * out_descriptor)288 int32_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
289                                                      gralloc1_buffer_descriptor_t *out_descriptor) {
290   if (!device || !out_descriptor) {
291     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
292   }
293   auto *dev = reinterpret_cast<GrallocImpl *>(device);
294   return static_cast<int32_t>(dev->CreateBufferDescriptorLocked(out_descriptor));
295 }
296 
DestroyBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)297 int32_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
298                                                       gralloc1_buffer_descriptor_t descriptor) {
299   if (!device) {
300     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
301   }
302   auto *dev = reinterpret_cast<GrallocImpl *>(device);
303   return static_cast<int32_t>(dev->DestroyBufferDescriptorLocked(descriptor));
304 }
305 
SetConsumerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint64_t usage)306 int32_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
307                                                gralloc1_buffer_descriptor_t descriptor,
308                                                uint64_t /*gralloc1_consumer_usage_t*/ usage) {
309   if (!device) {
310     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
311   } else {
312     auto *dev = reinterpret_cast<GrallocImpl *>(device);
313     return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
314                                 &BufferDescriptor::SetUsage,
315                                 ConsumerUsageToBufferUsage(usage)));
316   }
317 }
318 
SetBufferDimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)319 int32_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
320                                                   gralloc1_buffer_descriptor_t descriptor,
321                                                   uint32_t width, uint32_t height) {
322   if (!device) {
323     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
324   } else {
325     auto *dev = reinterpret_cast<GrallocImpl *>(device);
326     return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
327                                 &BufferDescriptor::SetDimensions,
328                                 INT(width), INT(height)));
329   }
330 }
331 
SetColorFormat(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,int32_t format)332 int32_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
333                                              gralloc1_buffer_descriptor_t descriptor,
334                                              int32_t format) {
335   if (!device) {
336     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
337   } else {
338     auto *dev = reinterpret_cast<GrallocImpl *>(device);
339     return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
340                                 &BufferDescriptor::SetColorFormat, format));
341   }
342 }
343 
SetLayerCount(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layer_count)344 int32_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
345                                             gralloc1_buffer_descriptor_t descriptor,
346                                             uint32_t layer_count) {
347   if (!device) {
348     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
349   } else {
350     auto *dev = reinterpret_cast<GrallocImpl *>(device);
351     return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
352                                 &BufferDescriptor::SetLayerCount, layer_count));
353   }
354 }
355 
SetProducerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint64_t usage)356 int32_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
357                                                gralloc1_buffer_descriptor_t descriptor,
358                                                uint64_t /*gralloc1_producer_usage_t*/ usage) {
359   if (!device) {
360     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
361   } else {
362     auto *dev = reinterpret_cast<GrallocImpl *>(device);
363     return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
364                                 &BufferDescriptor::SetUsage, ProducerUsageToBufferUsage(usage)));
365   }
366 }
367 
GetBackingStore(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * out_backstore)368 int32_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
369                                               gralloc1_backing_store_t *out_backstore) {
370   if (!out_backstore) {
371     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
372   }
373 
374   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
375   if (status == GRALLOC1_ERROR_NONE) {
376     *out_backstore =
377         static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
378   }
379 
380   return status;
381 }
382 
GetConsumerUsage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)383 int32_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
384                                                uint64_t /*gralloc1_consumer_usage_t*/ *outUsage) {
385   if (!outUsage) {
386     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
387   }
388 
389   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
390   if (status == GRALLOC1_ERROR_NONE) {
391     *outUsage = static_cast<uint64_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
392   }
393 
394   return static_cast<int32_t>(status);
395 }
396 
GetBufferDimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)397 int32_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
398                                                   uint32_t *outWidth, uint32_t *outHeight) {
399   if (!outWidth || !outHeight) {
400     return GRALLOC1_ERROR_BAD_VALUE;
401   }
402 
403   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
404   if (status == GRALLOC1_ERROR_NONE) {
405     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
406     *outWidth = UINT(hnd->GetUnalignedWidth());
407     *outHeight = UINT(hnd->GetUnalignedHeight());
408   }
409 
410   return static_cast<int32_t>(status);
411 }
412 
GetColorFormat(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)413 int32_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
414                                              int32_t *outFormat) {
415   if (!outFormat) {
416     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
417   }
418 
419   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
420   if (status == GRALLOC1_ERROR_NONE) {
421     *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
422   }
423 
424   return static_cast<int32_t>(status);
425 }
426 
GetLayerCount(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)427 int32_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
428                                             uint32_t *outLayerCount) {
429   if (!outLayerCount) {
430     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
431   }
432 
433   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
434   if (status == GRALLOC1_ERROR_NONE) {
435     *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
436   }
437 
438   return static_cast<int32_t>(status);
439 }
440 
GetProducerUsage(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t * outUsage)441 int32_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
442                                                uint64_t /*gralloc1_producer_usage_t*/ *outUsage) {
443   if (!outUsage) {
444     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
445   }
446 
447   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
448   if (status == GRALLOC1_ERROR_NONE) {
449     *outUsage = static_cast<uint64_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
450   }
451 
452   return static_cast<int32_t>(status);
453 }
454 
GetBufferStride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)455 int32_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
456                                               uint32_t *outStride) {
457   if (!outStride) {
458     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
459   }
460 
461   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
462   if (status == GRALLOC1_ERROR_NONE) {
463     *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
464   }
465 
466   return static_cast<int32_t>(status);
467 }
468 
AllocateBuffer(const gralloc1_buffer_descriptor_t * descriptor_ids,buffer_handle_t * out_buffers)469 gralloc1_error_t GrallocImpl::AllocateBuffer(const gralloc1_buffer_descriptor_t *descriptor_ids,
470                                              buffer_handle_t *out_buffers) {
471   gralloc1_error_t status = GRALLOC1_ERROR_NONE;
472 
473   // Validate descriptor
474   std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
475   std::shared_ptr<gralloc::BufferDescriptor> descriptor;
476   const auto map_descriptor = descriptors_map_.find(descriptor_ids[0]);
477   if (map_descriptor == descriptors_map_.end()) {
478     return GRALLOC1_ERROR_BAD_DESCRIPTOR;
479   } else {
480     descriptor = map_descriptor->second;
481   }
482 
483   // Allocate separate buffer for each descriptor
484   if (buf_mgr_->AllocateBuffer(*descriptor, &out_buffers[0]) != Error::NONE) {
485     return GRALLOC1_ERROR_NO_RESOURCES;
486   }
487 
488   return status;
489 }
490 
AllocateBuffers(gralloc1_device_t * device,uint32_t num_descriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * out_buffers)491 int32_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
492                                               const gralloc1_buffer_descriptor_t *descriptors,
493                                               buffer_handle_t *out_buffers) {
494   if (!num_descriptors || !descriptors) {
495     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
496   }
497 
498   if (!device) {
499     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
500   }
501 
502   if (num_descriptors != 1) {
503     return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
504   }
505 
506   auto *dev = reinterpret_cast<GrallocImpl *>(device);
507   gralloc1_error_t status = dev->AllocateBuffer(descriptors, out_buffers);
508 
509   return static_cast<int32_t>(status);
510 }
511 
RetainBuffer(gralloc1_device_t * device,buffer_handle_t buffer)512 int32_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
513   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
514   if (status == GRALLOC1_ERROR_NONE) {
515     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
516     GrallocImpl const *dev = GRALLOC_IMPL(device);
517     status = ToError(dev->buf_mgr_->RetainBuffer(hnd));
518   }
519 
520   return static_cast<int32_t>(status);
521 }
522 
ReleaseBuffer(gralloc1_device_t * device,buffer_handle_t buffer)523 int32_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
524   if (!device || !buffer) {
525     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
526   }
527 
528   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
529   GrallocImpl const *dev = GRALLOC_IMPL(device);
530   return static_cast<int32_t>((ToError(dev->buf_mgr_->ReleaseBuffer(hnd))));
531 }
532 
GetFlexLayout(const private_handle_t * hnd,struct android_flex_layout * layout)533 int32_t GrallocImpl::GetFlexLayout(const private_handle_t *hnd,
534                                             struct android_flex_layout *layout) {
535   if (!IsYuvFormat(hnd->format)) {
536     return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
537   }
538 
539   android_ycbcr yuvPlaneInfo[2];
540   int err = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
541 
542   if (err != 0) {
543     return GRALLOC1_ERROR_BAD_HANDLE;
544   }
545 
546   layout->format = FLEX_FORMAT_YCbCr;
547   layout->num_planes = 3;
548 
549   for (uint32_t i = 0; i < layout->num_planes; i++) {
550     layout->planes[i].bits_per_component = 8;
551     layout->planes[i].bits_used = 8;
552     layout->planes[i].h_increment = 1;
553     layout->planes[i].v_increment = 1;
554     layout->planes[i].h_subsampling = 2;
555     layout->planes[i].v_subsampling = 2;
556   }
557 
558   // We are only returning flex layout for progressive or single field formats.
559   struct android_ycbcr ycbcr = yuvPlaneInfo[0];
560   layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
561   layout->planes[0].component = FLEX_COMPONENT_Y;
562   layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
563 
564   layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
565   layout->planes[1].component = FLEX_COMPONENT_Cb;
566   layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
567   layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
568 
569   layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
570   layout->planes[2].component = FLEX_COMPONENT_Cr;
571   layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
572   layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
573   return static_cast<int32_t>(GRALLOC1_ERROR_NONE);
574 }
575 
GetNumFlexPlanes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * out_num_planes)576 int32_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
577                                                uint32_t *out_num_planes) {
578   if (!out_num_planes) {
579     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
580   }
581 
582   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
583   if (status == GRALLOC1_ERROR_NONE) {
584     const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
585     if (!IsYuvFormat(hnd->format)) {
586       status = GRALLOC1_ERROR_UNSUPPORTED;
587     } else {
588       *out_num_planes = 3;
589     }
590   }
591   return static_cast<int32_t>(status);
592 }
593 
CloseFdIfValid(int fd)594 static inline void CloseFdIfValid(int fd) {
595   if (fd > 0) {
596     close(fd);
597   }
598 }
599 
LockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t prod_usage,uint64_t cons_usage,const gralloc1_rect_t * region,void ** out_data,int32_t acquire_fence)600 int32_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
601                                          uint64_t /*gralloc1_producer_usage_t*/ prod_usage,
602                                          uint64_t /*gralloc1_consumer_usage_t*/ cons_usage,
603                                          const gralloc1_rect_t *region, void **out_data,
604                                          int32_t acquire_fence) {
605   ATRACE_CALL();
606   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
607   if (status != GRALLOC1_ERROR_NONE || !out_data ||
608       !region) {  // currently we ignore the region/rect client wants to lock
609     CloseFdIfValid(acquire_fence);
610     return static_cast<int32_t>(status);
611   }
612 
613   if (acquire_fence > 0) {
614     ATRACE_BEGIN("fence wait");
615     int error = sync_wait(acquire_fence, 1000);
616     ATRACE_END();
617     CloseFdIfValid(acquire_fence);
618     if (error < 0) {
619       ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
620       return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
621     }
622   }
623 
624   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
625   GrallocImpl const *dev = GRALLOC_IMPL(device);
626 
627   // Either producer usage or consumer usage must be *_USAGE_NONE
628   if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
629       (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
630     // Current gralloc1 clients do not satisfy this restriction.
631     // See b/33588773 for details
632     // return GRALLOC1_ERROR_BAD_VALUE;
633   }
634 
635   status = ToError(dev->buf_mgr_->LockBuffer(
636       hnd, ProducerUsageToBufferUsage(prod_usage) | ConsumerUsageToBufferUsage(cons_usage)));
637   *out_data = reinterpret_cast<void *>(hnd->base);
638 
639   return static_cast<int32_t>(status);
640 }
641 
LockFlex(gralloc1_device_t * device,buffer_handle_t buffer,uint64_t prod_usage,uint64_t cons_usage,const gralloc1_rect_t * region,struct android_flex_layout * out_flex_layout,int32_t acquire_fence)642 int32_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
643                                        uint64_t /*gralloc1_producer_usage_t*/ prod_usage,
644                                        uint64_t /*gralloc1_consumer_usage_t*/ cons_usage,
645                                        const gralloc1_rect_t *region,
646                                        struct android_flex_layout *out_flex_layout,
647                                        int32_t acquire_fence) {
648   if (!out_flex_layout) {
649     CloseFdIfValid(acquire_fence);
650     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
651   }
652 
653   void *out_data{};
654   int32_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
655                                                     &out_data, acquire_fence);
656   if (status != GRALLOC1_ERROR_NONE) {
657     return static_cast<int32_t>(status);
658   }
659 
660   auto *dev = reinterpret_cast<GrallocImpl *>(device);
661   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
662   dev->GetFlexLayout(hnd, out_flex_layout);
663   return static_cast<int32_t>(status);
664 }
665 
UnlockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * release_fence)666 int32_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
667                                            int32_t *release_fence) {
668   gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
669   if (status != GRALLOC1_ERROR_NONE) {
670     return static_cast<int32_t>(status);
671   }
672 
673   if (!release_fence) {
674     return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
675   }
676 
677   const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
678   GrallocImpl const *dev = GRALLOC_IMPL(device);
679 
680   *release_fence = -1;
681 
682   return static_cast<int32_t>(ToError(dev->buf_mgr_->UnlockBuffer(hnd)));
683 }
684 
Perform(int operation,va_list args)685 static int32_t Perform(int operation, va_list args) {
686   switch (operation) {
687     case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
688       int width = va_arg(args, int);
689       int format = va_arg(args, int);
690       int *stride = va_arg(args, int *);
691       unsigned int alignedw = 0, alignedh = 0;
692 
693       if (!stride) {
694         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
695       }
696 
697       BufferInfo info(width, width, format);
698       GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
699       *stride = INT(alignedw);
700     } break;
701 
702     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
703       private_handle_t *hnd = va_arg(args, private_handle_t *);
704       int *stride = va_arg(args, int *);
705       if (private_handle_t::validate(hnd) != 0) {
706         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
707       }
708 
709       if (!stride) {
710         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
711       }
712 
713       BufferDim_t buffer_dim;
714       if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
715         *stride = buffer_dim.sliceWidth;
716       } else {
717         *stride = hnd->width;
718       }
719     } break;
720 
721     case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
722       private_handle_t *hnd = va_arg(args, private_handle_t *);
723       int *stride = va_arg(args, int *);
724       int *height = va_arg(args, int *);
725       if (private_handle_t::validate(hnd) != 0) {
726         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
727       }
728 
729       if (!stride || !height) {
730         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
731       }
732 
733       GetCustomDimensions(hnd, stride, height);
734     } break;
735 
736     case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
737       int width = va_arg(args, int);
738       int height = va_arg(args, int);
739       int format = va_arg(args, int);
740       uint64_t usage = va_arg(args, uint64_t);
741       usage |= va_arg(args, uint64_t);
742 
743       int *aligned_width = va_arg(args, int *);
744       int *aligned_height = va_arg(args, int *);
745       int *tile_enabled = va_arg(args, int *);
746       if (!aligned_width || !aligned_height || !tile_enabled) {
747         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
748       }
749 
750       unsigned int alignedw, alignedh;
751       BufferInfo info(width, height, format, usage);
752       *tile_enabled = IsUBwcEnabled(format, usage);
753       GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
754       *aligned_width = INT(alignedw);
755       *aligned_height = INT(alignedh);
756     } break;
757 
758     case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
759       private_handle_t *hnd = va_arg(args, private_handle_t *);
760       int *color_space = va_arg(args, int *);
761 
762       if (private_handle_t::validate(hnd) != 0) {
763         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
764       }
765 
766       if (!color_space) {
767         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
768       }
769 
770       *color_space = 0;
771       GetColorSpaceFromMetadata(hnd, color_space);
772     } break;
773     case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
774       private_handle_t *hnd = va_arg(args, private_handle_t *);
775       android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
776       if (private_handle_t::validate(hnd) != 0) {
777         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
778       }
779 
780       if (!ycbcr) {
781         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
782       }
783 
784       if (GetYUVPlaneInfo(hnd, ycbcr)) {
785         return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
786       }
787     } break;
788 
789     case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
790       private_handle_t *hnd = va_arg(args, private_handle_t *);
791       int *map_secure_buffer = va_arg(args, int *);
792 
793       if (private_handle_t::validate(hnd) != 0) {
794         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
795       }
796 
797       if (!map_secure_buffer) {
798         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
799       }
800 
801       if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
802         *map_secure_buffer = 0;
803       }
804     } break;
805 
806     case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
807       private_handle_t *hnd = va_arg(args, private_handle_t *);
808       int *flag = va_arg(args, int *);
809 
810       if (private_handle_t::validate(hnd) != 0) {
811         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
812       }
813 
814       if (!flag) {
815         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
816       }
817 
818       *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
819       int linear_format = 0;
820       if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) {
821         if (linear_format) {
822           *flag = 0;
823         }
824       }
825     } break;
826 
827     case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
828       private_handle_t *hnd = va_arg(args, private_handle_t *);
829       void **rgb_data = va_arg(args, void **);
830 
831       if (private_handle_t::validate(hnd) != 0) {
832         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
833       }
834 
835       if (!rgb_data) {
836         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
837       }
838 
839       if (GetRgbDataAddress(hnd, rgb_data)) {
840         return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
841       }
842     } break;
843 
844     case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
845       private_handle_t *hnd = va_arg(args, private_handle_t *);
846       int *flag = va_arg(args, int *);
847 
848       if (private_handle_t::validate(hnd) != 0) {
849         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
850       }
851 
852       if (!flag) {
853         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
854       }
855 
856       if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
857         *flag = 0;
858       }
859     } break;
860 
861     case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: {
862       private_handle_t *hnd = va_arg(args, private_handle_t *);
863       uint32_t *enable = va_arg(args, uint32_t *);
864       if (private_handle_t::validate(hnd) != 0) {
865         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
866       }
867       if (setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable) != 0) {
868         return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
869       }
870     } break;
871 
872     case GRALLOC_MODULE_PERFORM_GET_GRAPHICS_METADATA: {
873       private_handle_t* hnd = va_arg(args, private_handle_t *);
874 
875       if (private_handle_t::validate(hnd) != 0) {
876         return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
877       }
878 
879       void* graphic_metadata = va_arg(args, void*);
880 
881       if (getMetaData(hnd, GET_GRAPHICS_METADATA, graphic_metadata) != 0) {
882         graphic_metadata = NULL;
883         return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
884       }
885     } break;
886 
887     default:
888       break;
889   }
890   return static_cast<int32_t>(GRALLOC1_ERROR_NONE);
891 }
892 
Gralloc1Perform(gralloc1_device_t * device,int operation,...)893 int32_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
894   if (!device) {
895     return GRALLOC1_ERROR_BAD_VALUE;
896   }
897 
898   va_list args;
899   va_start(args, operation);
900   int32_t err = Perform(operation, args);
901   va_end(args);
902 
903   return err;
904 }
905 
906 }  // namespace gralloc
907