1 /*
2 * Copyright (c) 2016-2018, 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(gralloc1_producer_usage_t producer_usage)115 static uint64_t ProducerUsageToBufferUsage(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(gralloc1_consumer_usage_t consumer_usage)136 static uint64_t ConsumerUsageToBufferUsage(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 gralloc1_error_t 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 GRALLOC1_ERROR_BAD_DESCRIPTOR;
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 GRALLOC1_ERROR_NONE;
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 gralloc1_error_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 dev->CreateBufferDescriptorLocked(out_descriptor);
295 }
296
DestroyBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)297 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
298 gralloc1_buffer_descriptor_t descriptor) {
299 if (!device) {
300 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
301 }
302 auto *dev = reinterpret_cast<GrallocImpl *>(device);
303 return dev->DestroyBufferDescriptorLocked(descriptor);
304 }
305
SetConsumerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_consumer_usage_t usage)306 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
307 gralloc1_buffer_descriptor_t descriptor,
308 gralloc1_consumer_usage_t usage) {
309 if (!device) {
310 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
311 } else {
312 auto *dev = reinterpret_cast<GrallocImpl *>(device);
313 return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetUsage,
314 ConsumerUsageToBufferUsage(usage));
315 }
316 }
317
SetBufferDimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)318 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
319 gralloc1_buffer_descriptor_t descriptor,
320 uint32_t width, uint32_t height) {
321 if (!device) {
322 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
323 } else {
324 auto *dev = reinterpret_cast<GrallocImpl *>(device);
325 return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetDimensions,
326 INT(width), INT(height));
327 }
328 }
329
SetColorFormat(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,int32_t format)330 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
331 gralloc1_buffer_descriptor_t descriptor,
332 int32_t format) {
333 if (!device) {
334 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
335 } else {
336 auto *dev = reinterpret_cast<GrallocImpl *>(device);
337 return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetColorFormat, format);
338 }
339 }
340
SetLayerCount(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layer_count)341 gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
342 gralloc1_buffer_descriptor_t descriptor,
343 uint32_t layer_count) {
344 if (!device) {
345 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
346 } else {
347 auto *dev = reinterpret_cast<GrallocImpl *>(device);
348 return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetLayerCount,
349 layer_count);
350 }
351 }
352
SetProducerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_producer_usage_t usage)353 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
354 gralloc1_buffer_descriptor_t descriptor,
355 gralloc1_producer_usage_t usage) {
356 if (!device) {
357 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
358 } else {
359 auto *dev = reinterpret_cast<GrallocImpl *>(device);
360 return dev->CallBufferDescriptorFunction(descriptor, &BufferDescriptor::SetUsage,
361 ProducerUsageToBufferUsage(usage));
362 }
363 }
364
GetBackingStore(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * out_backstore)365 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
366 gralloc1_backing_store_t *out_backstore) {
367 if (!device || !buffer) {
368 return GRALLOC1_ERROR_BAD_HANDLE;
369 }
370
371 *out_backstore =
372 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
373
374 return GRALLOC1_ERROR_NONE;
375 }
376
GetConsumerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_consumer_usage_t * outUsage)377 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
378 gralloc1_consumer_usage_t *outUsage) {
379 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
380 if (status == GRALLOC1_ERROR_NONE) {
381 *outUsage = static_cast<gralloc1_consumer_usage_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
382 }
383
384 return status;
385 }
386
GetBufferDimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)387 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
388 uint32_t *outWidth, uint32_t *outHeight) {
389 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
390 if (status == GRALLOC1_ERROR_NONE) {
391 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
392 *outWidth = UINT(hnd->GetUnalignedWidth());
393 *outHeight = UINT(hnd->GetUnalignedHeight());
394 }
395
396 return status;
397 }
398
GetColorFormat(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)399 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
400 int32_t *outFormat) {
401 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
402 if (status == GRALLOC1_ERROR_NONE) {
403 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
404 }
405
406 return status;
407 }
408
GetLayerCount(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)409 gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
410 uint32_t *outLayerCount) {
411 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
412 if (status == GRALLOC1_ERROR_NONE) {
413 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
414 }
415
416 return status;
417 }
418
GetProducerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t * outUsage)419 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
420 gralloc1_producer_usage_t *outUsage) {
421 if (!outUsage) {
422 return GRALLOC1_ERROR_BAD_VALUE;
423 }
424
425 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
426 if (status == GRALLOC1_ERROR_NONE) {
427 *outUsage = static_cast<gralloc1_producer_usage_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
428 }
429
430 return status;
431 }
432
GetBufferStride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)433 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
434 uint32_t *outStride) {
435 if (!outStride) {
436 return GRALLOC1_ERROR_BAD_VALUE;
437 }
438
439 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
440 if (status == GRALLOC1_ERROR_NONE) {
441 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
442 }
443
444 return status;
445 }
446
AllocateBuffer(const gralloc1_buffer_descriptor_t * descriptor_ids,buffer_handle_t * out_buffers)447 gralloc1_error_t GrallocImpl::AllocateBuffer(const gralloc1_buffer_descriptor_t *descriptor_ids,
448 buffer_handle_t *out_buffers) {
449 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
450
451 // Validate descriptor
452 std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
453 std::shared_ptr<gralloc::BufferDescriptor> descriptor;
454 const auto map_descriptor = descriptors_map_.find(descriptor_ids[0]);
455 if (map_descriptor == descriptors_map_.end()) {
456 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
457 } else {
458 descriptor = map_descriptor->second;
459 }
460
461 // Allocate separate buffer for each descriptor
462 if (buf_mgr_->AllocateBuffer(*descriptor, &out_buffers[0]) != Error::NONE) {
463 return GRALLOC1_ERROR_NO_RESOURCES;
464 }
465
466 return status;
467 }
468
AllocateBuffers(gralloc1_device_t * device,uint32_t num_descriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * out_buffers)469 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
470 const gralloc1_buffer_descriptor_t *descriptors,
471 buffer_handle_t *out_buffers) {
472 if (!num_descriptors || !descriptors) {
473 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
474 }
475
476 if (!device) {
477 return GRALLOC1_ERROR_BAD_VALUE;
478 }
479
480 if (num_descriptors != 1) {
481 return GRALLOC1_ERROR_UNSUPPORTED;
482 }
483
484 auto *dev = reinterpret_cast<GrallocImpl *>(device);
485 gralloc1_error_t status = dev->AllocateBuffer(descriptors, out_buffers);
486
487 return status;
488 }
489
RetainBuffer(gralloc1_device_t * device,buffer_handle_t buffer)490 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
491 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
492 if (status == GRALLOC1_ERROR_NONE) {
493 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
494 GrallocImpl const *dev = GRALLOC_IMPL(device);
495 status = ToError(dev->buf_mgr_->RetainBuffer(hnd));
496 }
497
498 return status;
499 }
500
ReleaseBuffer(gralloc1_device_t * device,buffer_handle_t buffer)501 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
502 if (!device || !buffer) {
503 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
504 }
505
506 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
507 GrallocImpl const *dev = GRALLOC_IMPL(device);
508 return ToError(dev->buf_mgr_->ReleaseBuffer(hnd));
509 }
510
GetFlexLayout(const private_handle_t * hnd,struct android_flex_layout * layout)511 gralloc1_error_t GrallocImpl::GetFlexLayout(const private_handle_t *hnd,
512 struct android_flex_layout *layout) {
513 if (!IsYuvFormat(hnd->format)) {
514 return GRALLOC1_ERROR_UNSUPPORTED;
515 }
516
517 android_ycbcr yuvPlaneInfo[2];
518 int err = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
519
520 if (err != 0) {
521 return GRALLOC1_ERROR_BAD_HANDLE;
522 }
523
524 layout->format = FLEX_FORMAT_YCbCr;
525 layout->num_planes = 3;
526
527 for (uint32_t i = 0; i < layout->num_planes; i++) {
528 layout->planes[i].bits_per_component = 8;
529 layout->planes[i].bits_used = 8;
530 layout->planes[i].h_increment = 1;
531 layout->planes[i].v_increment = 1;
532 layout->planes[i].h_subsampling = 2;
533 layout->planes[i].v_subsampling = 2;
534 }
535
536 // We are only returning flex layout for progressive or single field formats.
537 struct android_ycbcr ycbcr = yuvPlaneInfo[0];
538 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
539 layout->planes[0].component = FLEX_COMPONENT_Y;
540 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
541
542 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
543 layout->planes[1].component = FLEX_COMPONENT_Cb;
544 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
545 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
546
547 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
548 layout->planes[2].component = FLEX_COMPONENT_Cr;
549 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
550 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
551 return GRALLOC1_ERROR_NONE;
552 }
553
GetNumFlexPlanes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * out_num_planes)554 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
555 uint32_t *out_num_planes) {
556 if (!out_num_planes) {
557 return GRALLOC1_ERROR_BAD_VALUE;
558 }
559
560 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
561 if (status == GRALLOC1_ERROR_NONE) {
562 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
563 if (!IsYuvFormat(hnd->format)) {
564 status = GRALLOC1_ERROR_UNSUPPORTED;
565 } else {
566 *out_num_planes = 3;
567 }
568 }
569 return status;
570 }
571
CloseFdIfValid(int fd)572 static inline void CloseFdIfValid(int fd) {
573 if (fd > 0) {
574 close(fd);
575 }
576 }
577
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)578 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
579 gralloc1_producer_usage_t prod_usage,
580 gralloc1_consumer_usage_t cons_usage,
581 const gralloc1_rect_t *region, void **out_data,
582 int32_t acquire_fence) {
583 ATRACE_CALL();
584 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
585 if (status != GRALLOC1_ERROR_NONE || !out_data ||
586 !region) { // currently we ignore the region/rect client wants to lock
587 CloseFdIfValid(acquire_fence);
588 return status;
589 }
590
591 if (acquire_fence > 0) {
592 ATRACE_BEGIN("fence wait");
593 int error = sync_wait(acquire_fence, 1000);
594 ATRACE_END();
595 CloseFdIfValid(acquire_fence);
596 if (error < 0) {
597 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
598 return GRALLOC1_ERROR_UNDEFINED;
599 }
600 }
601
602 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
603 GrallocImpl const *dev = GRALLOC_IMPL(device);
604
605 // Either producer usage or consumer usage must be *_USAGE_NONE
606 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
607 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
608 // Current gralloc1 clients do not satisfy this restriction.
609 // See b/33588773 for details
610 // return GRALLOC1_ERROR_BAD_VALUE;
611 }
612
613 status = ToError(dev->buf_mgr_->LockBuffer(
614 hnd, ProducerUsageToBufferUsage(prod_usage) | ConsumerUsageToBufferUsage(cons_usage)));
615 *out_data = reinterpret_cast<void *>(hnd->base);
616
617 return status;
618 }
619
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)620 gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
621 gralloc1_producer_usage_t prod_usage,
622 gralloc1_consumer_usage_t cons_usage,
623 const gralloc1_rect_t *region,
624 struct android_flex_layout *out_flex_layout,
625 int32_t acquire_fence) {
626 if (!out_flex_layout) {
627 CloseFdIfValid(acquire_fence);
628 return GRALLOC1_ERROR_BAD_VALUE;
629 }
630
631 void *out_data{};
632 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
633 &out_data, acquire_fence);
634 if (status != GRALLOC1_ERROR_NONE) {
635 return status;
636 }
637
638 auto *dev = reinterpret_cast<GrallocImpl *>(device);
639 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
640 dev->GetFlexLayout(hnd, out_flex_layout);
641 return status;
642 }
643
UnlockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * release_fence)644 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
645 int32_t *release_fence) {
646 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
647 if (status != GRALLOC1_ERROR_NONE) {
648 return status;
649 }
650
651 if (!release_fence) {
652 return GRALLOC1_ERROR_BAD_VALUE;
653 }
654
655 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
656 GrallocImpl const *dev = GRALLOC_IMPL(device);
657
658 *release_fence = -1;
659
660 return ToError(dev->buf_mgr_->UnlockBuffer(hnd));
661 }
662
Perform(int operation,va_list args)663 static gralloc1_error_t Perform(int operation, va_list args) {
664 switch (operation) {
665 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
666 int width = va_arg(args, int);
667 int format = va_arg(args, int);
668 int *stride = va_arg(args, int *);
669 unsigned int alignedw = 0, alignedh = 0;
670
671 if (!stride) {
672 return GRALLOC1_ERROR_BAD_VALUE;
673 }
674
675 BufferInfo info(width, width, format);
676 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
677 *stride = INT(alignedw);
678 } break;
679
680 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
681 private_handle_t *hnd = va_arg(args, private_handle_t *);
682 int *stride = va_arg(args, int *);
683 if (private_handle_t::validate(hnd) != 0) {
684 return GRALLOC1_ERROR_BAD_HANDLE;
685 }
686
687 if (!stride) {
688 return GRALLOC1_ERROR_BAD_VALUE;
689 }
690
691 BufferDim_t buffer_dim;
692 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
693 *stride = buffer_dim.sliceWidth;
694 } else {
695 *stride = hnd->width;
696 }
697 } break;
698
699 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
700 private_handle_t *hnd = va_arg(args, private_handle_t *);
701 int *stride = va_arg(args, int *);
702 int *height = va_arg(args, int *);
703 if (private_handle_t::validate(hnd) != 0) {
704 return GRALLOC1_ERROR_BAD_HANDLE;
705 }
706
707 if (!stride || !height) {
708 return GRALLOC1_ERROR_BAD_VALUE;
709 }
710
711 GetCustomDimensions(hnd, stride, height);
712 } break;
713
714 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
715 int width = va_arg(args, int);
716 int height = va_arg(args, int);
717 int format = va_arg(args, int);
718 uint64_t usage = va_arg(args, uint64_t);
719 usage |= va_arg(args, uint64_t);
720
721 int *aligned_width = va_arg(args, int *);
722 int *aligned_height = va_arg(args, int *);
723 int *tile_enabled = va_arg(args, int *);
724 if (!aligned_width || !aligned_height || !tile_enabled) {
725 return GRALLOC1_ERROR_BAD_VALUE;
726 }
727
728 unsigned int alignedw, alignedh;
729 BufferInfo info(width, height, format, usage);
730 *tile_enabled = IsUBwcEnabled(format, usage);
731 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
732 *aligned_width = INT(alignedw);
733 *aligned_height = INT(alignedh);
734 } break;
735
736 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
737 private_handle_t *hnd = va_arg(args, private_handle_t *);
738 int *color_space = va_arg(args, int *);
739
740 if (private_handle_t::validate(hnd) != 0) {
741 return GRALLOC1_ERROR_BAD_HANDLE;
742 }
743
744 if (!color_space) {
745 return GRALLOC1_ERROR_BAD_VALUE;
746 }
747
748 *color_space = 0;
749 GetColorSpaceFromMetadata(hnd, color_space);
750 } break;
751 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
752 private_handle_t *hnd = va_arg(args, private_handle_t *);
753 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
754 if (private_handle_t::validate(hnd) != 0) {
755 return GRALLOC1_ERROR_BAD_HANDLE;
756 }
757
758 if (!ycbcr) {
759 return GRALLOC1_ERROR_BAD_VALUE;
760 }
761
762 if (GetYUVPlaneInfo(hnd, ycbcr)) {
763 return GRALLOC1_ERROR_UNDEFINED;
764 }
765 } break;
766
767 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
768 private_handle_t *hnd = va_arg(args, private_handle_t *);
769 int *map_secure_buffer = va_arg(args, int *);
770
771 if (private_handle_t::validate(hnd) != 0) {
772 return GRALLOC1_ERROR_BAD_HANDLE;
773 }
774
775 if (!map_secure_buffer) {
776 return GRALLOC1_ERROR_BAD_VALUE;
777 }
778
779 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
780 *map_secure_buffer = 0;
781 }
782 } break;
783
784 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
785 private_handle_t *hnd = va_arg(args, private_handle_t *);
786 int *flag = va_arg(args, int *);
787
788 if (private_handle_t::validate(hnd) != 0) {
789 return GRALLOC1_ERROR_BAD_HANDLE;
790 }
791
792 if (!flag) {
793 return GRALLOC1_ERROR_BAD_VALUE;
794 }
795
796 *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
797 int linear_format = 0;
798 if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) {
799 if (linear_format) {
800 *flag = 0;
801 }
802 }
803 } break;
804
805 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
806 private_handle_t *hnd = va_arg(args, private_handle_t *);
807 void **rgb_data = va_arg(args, void **);
808
809 if (private_handle_t::validate(hnd) != 0) {
810 return GRALLOC1_ERROR_BAD_HANDLE;
811 }
812
813 if (!rgb_data) {
814 return GRALLOC1_ERROR_BAD_VALUE;
815 }
816
817 if (GetRgbDataAddress(hnd, rgb_data)) {
818 return GRALLOC1_ERROR_UNDEFINED;
819 }
820 } break;
821
822 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
823 private_handle_t *hnd = va_arg(args, private_handle_t *);
824 int *flag = va_arg(args, int *);
825
826 if (private_handle_t::validate(hnd) != 0) {
827 return GRALLOC1_ERROR_BAD_HANDLE;
828 }
829
830 if (!flag) {
831 return GRALLOC1_ERROR_BAD_VALUE;
832 }
833
834 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
835 *flag = 0;
836 }
837 } break;
838
839 case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: {
840 private_handle_t *hnd = va_arg(args, private_handle_t *);
841 uint32_t *enable = va_arg(args, uint32_t *);
842 if (private_handle_t::validate(hnd) != 0) {
843 return GRALLOC1_ERROR_BAD_HANDLE;
844 }
845 if (setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable) != 0) {
846 return GRALLOC1_ERROR_UNSUPPORTED;
847 }
848 } break;
849
850 case GRALLOC_MODULE_PERFORM_GET_GRAPHICS_METADATA: {
851 private_handle_t* hnd = va_arg(args, private_handle_t *);
852
853 if (private_handle_t::validate(hnd) != 0) {
854 return GRALLOC1_ERROR_BAD_HANDLE;
855 }
856
857 void* graphic_metadata = va_arg(args, void*);
858
859 if (getMetaData(hnd, GET_GRAPHICS_METADATA, graphic_metadata) != 0) {
860 graphic_metadata = NULL;
861 return GRALLOC1_ERROR_UNSUPPORTED;
862 }
863 } break;
864
865 default:
866 break;
867 }
868 return GRALLOC1_ERROR_NONE;
869 }
870
Gralloc1Perform(gralloc1_device_t * device,int operation,...)871 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
872 if (!device) {
873 return GRALLOC1_ERROR_BAD_VALUE;
874 }
875
876 va_list args;
877 va_start(args, operation);
878 gralloc1_error_t err = Perform(operation, args);
879 va_end(args);
880
881 return err;
882 }
883
884 } // namespace gralloc
885