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