2 * Copyright 2020 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
7 #include "cros_gralloc/gralloc4/CrosGralloc4Mapper.h"
9 #include <aidl/android/hardware/graphics/common/BlendMode.h>
10 #include <aidl/android/hardware/graphics/common/Dataspace.h>
11 #include <aidl/android/hardware/graphics/common/PlaneLayout.h>
12 #include <aidl/android/hardware/graphics/common/Rect.h>
13 #include <cutils/native_handle.h>
14 #include <gralloctypes/Gralloc4.h>
16 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
19 using aidl::android::hardware::graphics::common::BlendMode;
20 using aidl::android::hardware::graphics::common::Dataspace;
21 using aidl::android::hardware::graphics::common::PlaneLayout;
22 using aidl::android::hardware::graphics::common::Rect;
23 using android::hardware::hidl_handle;
24 using android::hardware::hidl_vec;
25 using android::hardware::Return;
26 using android::hardware::Void;
27 using android::hardware::graphics::common::V1_2::BufferUsage;
28 using android::hardware::graphics::common::V1_2::PixelFormat;
29 using android::hardware::graphics::mapper::V4_0::Error;
30 using android::hardware::graphics::mapper::V4_0::IMapper;
32 CrosGralloc4Mapper::CrosGralloc4Mapper() : mDriver(std::make_unique<cros_gralloc_driver>()) {
33 if (mDriver->init()) {
34 drv_log("Failed to initialize driver.\n");
39 Return<void> CrosGralloc4Mapper::createDescriptor(const BufferDescriptorInfo& description,
40 createDescriptor_cb hidlCb) {
41 hidl_vec<uint8_t> descriptor;
43 if (description.width == 0) {
44 drv_log("Failed to createDescriptor. Bad width: %d.\n", description.width);
45 hidlCb(Error::BAD_VALUE, descriptor);
49 if (description.height == 0) {
50 drv_log("Failed to createDescriptor. Bad height: %d.\n", description.height);
51 hidlCb(Error::BAD_VALUE, descriptor);
55 if (description.layerCount == 0) {
56 drv_log("Failed to createDescriptor. Bad layer count: %d.\n", description.layerCount);
57 hidlCb(Error::BAD_VALUE, descriptor);
61 int ret = android::gralloc4::encodeBufferDescriptorInfo(description, &descriptor);
63 drv_log("Failed to createDescriptor. Failed to encode: %d.\n", ret);
64 hidlCb(Error::BAD_VALUE, descriptor);
68 hidlCb(Error::NONE, descriptor);
72 Return<void> CrosGralloc4Mapper::importBuffer(const hidl_handle& handle, importBuffer_cb hidlCb) {
74 drv_log("Failed to import buffer. Driver is uninitialized.\n");
75 hidlCb(Error::NO_RESOURCES, nullptr);
79 const native_handle_t* bufferHandle = handle.getNativeHandle();
80 if (!bufferHandle || bufferHandle->numFds == 0) {
81 drv_log("Failed to importBuffer. Bad handle.\n");
82 hidlCb(Error::BAD_BUFFER, nullptr);
86 native_handle_t* importedBufferHandle = native_handle_clone(bufferHandle);
87 if (!importedBufferHandle) {
88 drv_log("Failed to importBuffer. Handle clone failed: %s.\n", strerror(errno));
89 hidlCb(Error::NO_RESOURCES, nullptr);
93 int ret = mDriver->retain(importedBufferHandle);
95 native_handle_close(importedBufferHandle);
96 native_handle_delete(importedBufferHandle);
97 hidlCb(Error::NO_RESOURCES, nullptr);
101 hidlCb(Error::NONE, importedBufferHandle);
105 Return<Error> CrosGralloc4Mapper::freeBuffer(void* rawHandle) {
107 drv_log("Failed to freeBuffer. Driver is uninitialized.\n");
108 return Error::NO_RESOURCES;
111 native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
113 drv_log("Failed to freeBuffer. Empty handle.\n");
114 return Error::BAD_BUFFER;
117 int ret = mDriver->release(bufferHandle);
119 return Error::BAD_BUFFER;
122 native_handle_close(bufferHandle);
123 native_handle_delete(bufferHandle);
127 Return<Error> CrosGralloc4Mapper::validateBufferSize(void* rawHandle,
128 const BufferDescriptorInfo& descriptor,
131 drv_log("Failed to validateBufferSize. Driver is uninitialized.\n");
132 return Error::NO_RESOURCES;
135 native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
137 drv_log("Failed to validateBufferSize. Empty handle.\n");
138 return Error::BAD_BUFFER;
141 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
143 drv_log("Failed to validateBufferSize. Invalid handle.\n");
144 return Error::BAD_BUFFER;
147 PixelFormat crosHandleFormat = static_cast<PixelFormat>(crosHandle->droid_format);
148 if (descriptor.format != crosHandleFormat) {
149 drv_log("Failed to validateBufferSize. Format mismatch.\n");
150 return Error::BAD_BUFFER;
153 if (descriptor.width != crosHandle->width) {
154 drv_log("Failed to validateBufferSize. Width mismatch (%d vs %d).\n", descriptor.width,
156 return Error::BAD_VALUE;
159 if (descriptor.height != crosHandle->height) {
160 drv_log("Failed to validateBufferSize. Height mismatch (%d vs %d).\n", descriptor.height,
162 return Error::BAD_VALUE;
165 if (stride != crosHandle->pixel_stride) {
166 drv_log("Failed to validateBufferSize. Stride mismatch (%d vs %d).\n", stride,
167 crosHandle->pixel_stride);
168 return Error::BAD_VALUE;
174 Return<void> CrosGralloc4Mapper::getTransportSize(void* rawHandle, getTransportSize_cb hidlCb) {
176 drv_log("Failed to getTransportSize. Driver is uninitialized.\n");
177 hidlCb(Error::BAD_BUFFER, 0, 0);
181 native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
183 drv_log("Failed to getTransportSize. Bad handle.\n");
184 hidlCb(Error::BAD_BUFFER, 0, 0);
188 // No local process data is currently stored on the native handle.
189 hidlCb(Error::NONE, bufferHandle->numFds, bufferHandle->numInts);
193 Return<void> CrosGralloc4Mapper::lock(void* rawBuffer, uint64_t cpuUsage, const Rect& region,
194 const hidl_handle& acquireFence, lock_cb hidlCb) {
196 drv_log("Failed to lock. Driver is uninitialized.\n");
197 hidlCb(Error::NO_RESOURCES, nullptr);
201 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawBuffer);
203 drv_log("Failed to lock. Empty handle.\n");
204 hidlCb(Error::BAD_BUFFER, nullptr);
209 drv_log("Failed to lock. Bad cpu usage: %" PRIu64 ".\n", cpuUsage);
210 hidlCb(Error::BAD_VALUE, nullptr);
214 uint32_t mapUsage = 0;
215 int ret = convertToMapUsage(cpuUsage, &mapUsage);
217 drv_log("Failed to lock. Convert usage failed.\n");
218 hidlCb(Error::BAD_VALUE, nullptr);
222 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
223 if (crosHandle == nullptr) {
224 drv_log("Failed to lock. Invalid handle.\n");
225 hidlCb(Error::BAD_VALUE, nullptr);
229 if (region.left < 0) {
230 drv_log("Failed to lock. Invalid region: negative left value %d.\n", region.left);
231 hidlCb(Error::BAD_VALUE, nullptr);
235 if (region.top < 0) {
236 drv_log("Failed to lock. Invalid region: negative top value %d.\n", region.top);
237 hidlCb(Error::BAD_VALUE, nullptr);
241 if (region.width < 0) {
242 drv_log("Failed to lock. Invalid region: negative width value %d.\n", region.width);
243 hidlCb(Error::BAD_VALUE, nullptr);
247 if (region.height < 0) {
248 drv_log("Failed to lock. Invalid region: negative height value %d.\n", region.height);
249 hidlCb(Error::BAD_VALUE, nullptr);
253 if (region.width > crosHandle->width) {
254 drv_log("Failed to lock. Invalid region: width greater than buffer width (%d vs %d).\n",
255 region.width, crosHandle->width);
256 hidlCb(Error::BAD_VALUE, nullptr);
260 if (region.height > crosHandle->height) {
261 drv_log("Failed to lock. Invalid region: height greater than buffer height (%d vs %d).\n",
262 region.height, crosHandle->height);
263 hidlCb(Error::BAD_VALUE, nullptr);
267 struct rectangle rect = {static_cast<uint32_t>(region.left), static_cast<uint32_t>(region.top),
268 static_cast<uint32_t>(region.width),
269 static_cast<uint32_t>(region.height)};
271 // An access region of all zeros means the entire buffer.
272 if (rect.x == 0 && rect.y == 0 && rect.width == 0 && rect.height == 0) {
273 rect.width = crosHandle->width;
274 rect.height = crosHandle->height;
277 int acquireFenceFd = -1;
278 ret = convertToFenceFd(acquireFence, &acquireFenceFd);
280 drv_log("Failed to lock. Bad acquire fence.\n");
281 hidlCb(Error::BAD_VALUE, nullptr);
285 uint8_t* addr[DRV_MAX_PLANES];
286 ret = mDriver->lock(bufferHandle, acquireFenceFd, /*close_acquire_fence=*/false, &rect,
289 hidlCb(Error::BAD_VALUE, nullptr);
293 hidlCb(Error::NONE, addr[0]);
297 Return<void> CrosGralloc4Mapper::unlock(void* rawHandle, unlock_cb hidlCb) {
299 drv_log("Failed to unlock. Driver is uninitialized.\n");
300 hidlCb(Error::BAD_BUFFER, nullptr);
304 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
306 drv_log("Failed to unlock. Empty handle.\n");
307 hidlCb(Error::BAD_BUFFER, nullptr);
311 int releaseFenceFd = -1;
312 int ret = mDriver->unlock(bufferHandle, &releaseFenceFd);
314 drv_log("Failed to unlock.\n");
315 hidlCb(Error::BAD_BUFFER, nullptr);
319 hidl_handle releaseFenceHandle;
320 ret = convertToFenceHandle(releaseFenceFd, &releaseFenceHandle);
322 drv_log("Failed to unlock. Failed to convert release fence to handle.\n");
323 hidlCb(Error::BAD_BUFFER, nullptr);
327 hidlCb(Error::NONE, releaseFenceHandle);
331 Return<void> CrosGralloc4Mapper::flushLockedBuffer(void* rawHandle, flushLockedBuffer_cb hidlCb) {
333 drv_log("Failed to flushLockedBuffer. Driver is uninitialized.\n");
334 hidlCb(Error::NO_RESOURCES, nullptr);
338 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
340 drv_log("Failed to flushLockedBuffer. Empty handle.\n");
341 hidlCb(Error::BAD_BUFFER, nullptr);
345 int releaseFenceFd = -1;
346 int ret = mDriver->flush(bufferHandle, &releaseFenceFd);
348 drv_log("Failed to flushLockedBuffer. Flush failed.\n");
349 hidlCb(Error::BAD_BUFFER, nullptr);
353 hidl_handle releaseFenceHandle;
354 ret = convertToFenceHandle(releaseFenceFd, &releaseFenceHandle);
356 drv_log("Failed to flushLockedBuffer. Failed to convert release fence to handle.\n");
357 hidlCb(Error::BAD_BUFFER, nullptr);
361 hidlCb(Error::NONE, releaseFenceHandle);
365 Return<Error> CrosGralloc4Mapper::rereadLockedBuffer(void* rawHandle) {
367 drv_log("Failed to rereadLockedBuffer. Driver is uninitialized.\n");
368 return Error::NO_RESOURCES;
371 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
373 drv_log("Failed to rereadLockedBuffer. Empty handle.\n");
374 return Error::BAD_BUFFER;
377 int ret = mDriver->invalidate(bufferHandle);
379 drv_log("Failed to rereadLockedBuffer. Failed to invalidate.\n");
380 return Error::BAD_BUFFER;
386 Return<void> CrosGralloc4Mapper::isSupported(const BufferDescriptorInfo& descriptor,
387 isSupported_cb hidlCb) {
389 drv_log("Failed to isSupported. Driver is uninitialized.\n");
390 hidlCb(Error::BAD_VALUE, false);
394 struct cros_gralloc_buffer_descriptor crosDescriptor;
395 if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
396 hidlCb(Error::NONE, false);
400 bool supported = mDriver->is_supported(&crosDescriptor);
402 crosDescriptor.use_flags &= ~BO_USE_SCANOUT;
403 supported = mDriver->is_supported(&crosDescriptor);
406 hidlCb(Error::NONE, supported);
410 Return<void> CrosGralloc4Mapper::get(void* rawHandle, const MetadataType& metadataType,
412 hidl_vec<uint8_t> encodedMetadata;
415 drv_log("Failed to get. Driver is uninitialized.\n");
416 hidlCb(Error::NO_RESOURCES, encodedMetadata);
420 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
422 drv_log("Failed to get. Empty handle.\n");
423 hidlCb(Error::BAD_BUFFER, encodedMetadata);
427 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
429 drv_log("Failed to get. Invalid handle.\n");
430 hidlCb(Error::BAD_BUFFER, encodedMetadata);
434 get(crosHandle, metadataType, hidlCb);
438 Return<void> CrosGralloc4Mapper::get(cros_gralloc_handle_t crosHandle,
439 const MetadataType& metadataType, get_cb hidlCb) {
440 hidl_vec<uint8_t> encodedMetadata;
443 drv_log("Failed to get. Driver is uninitialized.\n");
444 hidlCb(Error::NO_RESOURCES, encodedMetadata);
449 drv_log("Failed to get. Invalid handle.\n");
450 hidlCb(Error::BAD_BUFFER, encodedMetadata);
454 android::status_t status = android::NO_ERROR;
455 if (metadataType == android::gralloc4::MetadataType_BufferId) {
456 status = android::gralloc4::encodeBufferId(crosHandle->id, &encodedMetadata);
457 } else if (metadataType == android::gralloc4::MetadataType_Name) {
458 const char* name = (const char*)(&crosHandle->base.data[crosHandle->name_offset]);
459 status = android::gralloc4::encodeName(name, &encodedMetadata);
460 } else if (metadataType == android::gralloc4::MetadataType_Width) {
461 status = android::gralloc4::encodeWidth(crosHandle->width, &encodedMetadata);
462 } else if (metadataType == android::gralloc4::MetadataType_Height) {
463 status = android::gralloc4::encodeHeight(crosHandle->height, &encodedMetadata);
464 } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
465 status = android::gralloc4::encodeLayerCount(1, &encodedMetadata);
466 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
467 PixelFormat pixelFormat = static_cast<PixelFormat>(crosHandle->droid_format);
468 status = android::gralloc4::encodePixelFormatRequested(pixelFormat, &encodedMetadata);
469 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatFourCC) {
470 uint32_t format = crosHandle->format;
471 // Map internal fourcc codes back to standard fourcc codes.
472 if (format == DRM_FORMAT_YVU420_ANDROID) {
473 format = DRM_FORMAT_YVU420;
475 status = android::gralloc4::encodePixelFormatFourCC(format, &encodedMetadata);
476 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatModifier) {
477 status = android::gralloc4::encodePixelFormatModifier(crosHandle->format_modifier,
479 } else if (metadataType == android::gralloc4::MetadataType_Usage) {
480 uint64_t usage = static_cast<uint64_t>(crosHandle->usage);
481 status = android::gralloc4::encodeUsage(usage, &encodedMetadata);
482 } else if (metadataType == android::gralloc4::MetadataType_AllocationSize) {
483 status = android::gralloc4::encodeAllocationSize(crosHandle->total_size, &encodedMetadata);
484 } else if (metadataType == android::gralloc4::MetadataType_ProtectedContent) {
485 uint64_t hasProtectedContent = crosHandle->usage & BufferUsage::PROTECTED ? 1 : 0;
486 status = android::gralloc4::encodeProtectedContent(hasProtectedContent, &encodedMetadata);
487 } else if (metadataType == android::gralloc4::MetadataType_Compression) {
488 status = android::gralloc4::encodeCompression(android::gralloc4::Compression_None,
490 } else if (metadataType == android::gralloc4::MetadataType_Interlaced) {
491 status = android::gralloc4::encodeInterlaced(android::gralloc4::Interlaced_None,
493 } else if (metadataType == android::gralloc4::MetadataType_ChromaSiting) {
494 status = android::gralloc4::encodeChromaSiting(android::gralloc4::ChromaSiting_None,
496 } else if (metadataType == android::gralloc4::MetadataType_PlaneLayouts) {
497 std::vector<PlaneLayout> planeLayouts;
498 getPlaneLayouts(crosHandle->format, &planeLayouts);
500 for (size_t plane = 0; plane < planeLayouts.size(); plane++) {
501 PlaneLayout& planeLayout = planeLayouts[plane];
502 planeLayout.offsetInBytes = crosHandle->offsets[plane];
503 planeLayout.strideInBytes = crosHandle->strides[plane];
504 planeLayout.totalSizeInBytes = crosHandle->sizes[plane];
505 planeLayout.widthInSamples = crosHandle->width;
506 planeLayout.heightInSamples = crosHandle->height;
509 status = android::gralloc4::encodePlaneLayouts(planeLayouts, &encodedMetadata);
510 } else if (metadataType == android::gralloc4::MetadataType_Crop) {
511 std::vector<aidl::android::hardware::graphics::common::Rect> crops;
512 for (size_t plane = 0; plane < crosHandle->num_planes; plane++) {
513 aidl::android::hardware::graphics::common::Rect crop;
516 crop.right = crosHandle->width;
517 crop.bottom = crosHandle->height;
518 crops.push_back(crop);
521 status = android::gralloc4::encodeCrop(crops, &encodedMetadata);
522 } else if (metadataType == android::gralloc4::MetadataType_Dataspace) {
523 status = android::gralloc4::encodeDataspace(Dataspace::UNKNOWN, &encodedMetadata);
524 } else if (metadataType == android::gralloc4::MetadataType_BlendMode) {
525 status = android::gralloc4::encodeBlendMode(BlendMode::INVALID, &encodedMetadata);
526 } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) {
527 status = android::gralloc4::encodeSmpte2086(std::nullopt, &encodedMetadata);
528 } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) {
529 status = android::gralloc4::encodeCta861_3(std::nullopt, &encodedMetadata);
530 } else if (metadataType == android::gralloc4::MetadataType_Smpte2094_40) {
531 status = android::gralloc4::encodeSmpte2094_40(std::nullopt, &encodedMetadata);
533 hidlCb(Error::UNSUPPORTED, encodedMetadata);
537 if (status != android::NO_ERROR) {
538 hidlCb(Error::NO_RESOURCES, encodedMetadata);
539 drv_log("Failed to get. Failed to encode metadata.\n");
543 hidlCb(Error::NONE, encodedMetadata);
547 Return<Error> CrosGralloc4Mapper::set(void* rawHandle, const MetadataType& metadataType,
548 const hidl_vec<uint8_t>& /*metadata*/) {
550 drv_log("Failed to set. Driver is uninitialized.\n");
551 return Error::NO_RESOURCES;
554 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
556 drv_log("Failed to set. Empty handle.\n");
557 return Error::BAD_BUFFER;
560 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
562 drv_log("Failed to set. Invalid handle.\n");
563 return Error::BAD_BUFFER;
566 if (metadataType == android::gralloc4::MetadataType_BufferId) {
567 return Error::BAD_VALUE;
568 } else if (metadataType == android::gralloc4::MetadataType_Name) {
569 return Error::BAD_VALUE;
570 } else if (metadataType == android::gralloc4::MetadataType_Width) {
571 return Error::BAD_VALUE;
572 } else if (metadataType == android::gralloc4::MetadataType_Height) {
573 return Error::BAD_VALUE;
574 } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
575 return Error::BAD_VALUE;
576 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
577 return Error::BAD_VALUE;
578 } else if (metadataType == android::gralloc4::MetadataType_Usage) {
579 return Error::BAD_VALUE;
582 return Error::UNSUPPORTED;
585 int CrosGralloc4Mapper::getResolvedDrmFormat(PixelFormat pixelFormat, uint64_t bufferUsage,
586 uint32_t* outDrmFormat) {
588 if (convertToDrmFormat(pixelFormat, &drmFormat)) {
589 std::string pixelFormatString = getPixelFormatString(pixelFormat);
590 drv_log("Failed to getResolvedDrmFormat. Failed to convert format %s\n",
591 pixelFormatString.c_str());
596 if (convertToBufferUsage(bufferUsage, &usage)) {
597 std::string usageString = getUsageString(bufferUsage);
598 drv_log("Failed to getResolvedDrmFormat. Failed to convert usage %s\n",
599 usageString.c_str());
603 uint32_t resolvedDrmFormat = mDriver->get_resolved_drm_format(drmFormat, usage);
604 if (resolvedDrmFormat == DRM_FORMAT_INVALID) {
605 std::string drmFormatString = getDrmFormatString(drmFormat);
606 drv_log("Failed to getResolvedDrmFormat. Failed to resolve drm format %s\n",
607 drmFormatString.c_str());
611 *outDrmFormat = resolvedDrmFormat;
616 Return<void> CrosGralloc4Mapper::getFromBufferDescriptorInfo(
617 const BufferDescriptorInfo& descriptor, const MetadataType& metadataType,
618 getFromBufferDescriptorInfo_cb hidlCb) {
619 hidl_vec<uint8_t> encodedMetadata;
622 drv_log("Failed to getFromBufferDescriptorInfo. Driver is uninitialized.\n");
623 hidlCb(Error::NO_RESOURCES, encodedMetadata);
627 android::status_t status = android::NO_ERROR;
628 if (metadataType == android::gralloc4::MetadataType_Name) {
629 status = android::gralloc4::encodeName(descriptor.name, &encodedMetadata);
630 } else if (metadataType == android::gralloc4::MetadataType_Width) {
631 status = android::gralloc4::encodeWidth(descriptor.width, &encodedMetadata);
632 } else if (metadataType == android::gralloc4::MetadataType_Height) {
633 status = android::gralloc4::encodeHeight(descriptor.height, &encodedMetadata);
634 } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
635 status = android::gralloc4::encodeLayerCount(1, &encodedMetadata);
636 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
637 status = android::gralloc4::encodePixelFormatRequested(descriptor.format, &encodedMetadata);
638 } else if (metadataType == android::gralloc4::MetadataType_PixelFormatFourCC) {
640 if (getResolvedDrmFormat(descriptor.format, descriptor.usage, &drmFormat)) {
641 hidlCb(Error::BAD_VALUE, encodedMetadata);
644 status = android::gralloc4::encodePixelFormatFourCC(drmFormat, &encodedMetadata);
645 } else if (metadataType == android::gralloc4::MetadataType_Usage) {
646 status = android::gralloc4::encodeUsage(descriptor.usage, &encodedMetadata);
647 } else if (metadataType == android::gralloc4::MetadataType_ProtectedContent) {
648 uint64_t hasProtectedContent = descriptor.usage & BufferUsage::PROTECTED ? 1 : 0;
649 status = android::gralloc4::encodeProtectedContent(hasProtectedContent, &encodedMetadata);
650 } else if (metadataType == android::gralloc4::MetadataType_Compression) {
651 status = android::gralloc4::encodeCompression(android::gralloc4::Compression_None,
653 } else if (metadataType == android::gralloc4::MetadataType_Interlaced) {
654 status = android::gralloc4::encodeInterlaced(android::gralloc4::Interlaced_None,
656 } else if (metadataType == android::gralloc4::MetadataType_ChromaSiting) {
657 status = android::gralloc4::encodeChromaSiting(android::gralloc4::ChromaSiting_None,
659 } else if (metadataType == android::gralloc4::MetadataType_Crop) {
661 if (getResolvedDrmFormat(descriptor.format, descriptor.usage, &drmFormat)) {
662 hidlCb(Error::BAD_VALUE, encodedMetadata);
666 size_t numPlanes = drv_num_planes_from_format(drmFormat);
668 std::vector<aidl::android::hardware::graphics::common::Rect> crops;
669 for (size_t plane = 0; plane < numPlanes; plane++) {
670 aidl::android::hardware::graphics::common::Rect crop;
673 crop.right = descriptor.width;
674 crop.bottom = descriptor.height;
675 crops.push_back(crop);
677 status = android::gralloc4::encodeCrop(crops, &encodedMetadata);
678 } else if (metadataType == android::gralloc4::MetadataType_Dataspace) {
679 status = android::gralloc4::encodeDataspace(Dataspace::UNKNOWN, &encodedMetadata);
680 } else if (metadataType == android::gralloc4::MetadataType_BlendMode) {
681 status = android::gralloc4::encodeBlendMode(BlendMode::INVALID, &encodedMetadata);
682 } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) {
683 status = android::gralloc4::encodeSmpte2086(std::nullopt, &encodedMetadata);
684 } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) {
685 status = android::gralloc4::encodeCta861_3(std::nullopt, &encodedMetadata);
686 } else if (metadataType == android::gralloc4::MetadataType_Smpte2094_40) {
687 status = android::gralloc4::encodeSmpte2094_40(std::nullopt, &encodedMetadata);
689 hidlCb(Error::UNSUPPORTED, encodedMetadata);
693 if (status != android::NO_ERROR) {
694 hidlCb(Error::NO_RESOURCES, encodedMetadata);
698 hidlCb(Error::NONE, encodedMetadata);
702 Return<void> CrosGralloc4Mapper::listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidlCb) {
703 hidl_vec<MetadataTypeDescription> supported;
706 drv_log("Failed to listSupportedMetadataTypes. Driver is uninitialized.\n");
707 hidlCb(Error::NO_RESOURCES, supported);
711 supported = hidl_vec<IMapper::MetadataTypeDescription>({
713 android::gralloc4::MetadataType_BufferId,
716 /*isSettable=*/false,
719 android::gralloc4::MetadataType_Name,
722 /*isSettable=*/false,
725 android::gralloc4::MetadataType_Width,
728 /*isSettable=*/false,
731 android::gralloc4::MetadataType_Height,
734 /*isSettable=*/false,
737 android::gralloc4::MetadataType_LayerCount,
740 /*isSettable=*/false,
743 android::gralloc4::MetadataType_PixelFormatRequested,
746 /*isSettable=*/false,
749 android::gralloc4::MetadataType_PixelFormatFourCC,
752 /*isSettable=*/false,
755 android::gralloc4::MetadataType_PixelFormatModifier,
758 /*isSettable=*/false,
761 android::gralloc4::MetadataType_Usage,
764 /*isSettable=*/false,
767 android::gralloc4::MetadataType_AllocationSize,
770 /*isSettable=*/false,
773 android::gralloc4::MetadataType_ProtectedContent,
776 /*isSettable=*/false,
779 android::gralloc4::MetadataType_Compression,
782 /*isSettable=*/false,
785 android::gralloc4::MetadataType_Interlaced,
788 /*isSettable=*/false,
791 android::gralloc4::MetadataType_ChromaSiting,
794 /*isSettable=*/false,
797 android::gralloc4::MetadataType_PlaneLayouts,
800 /*isSettable=*/false,
803 android::gralloc4::MetadataType_Dataspace,
806 /*isSettable=*/false,
809 android::gralloc4::MetadataType_BlendMode,
812 /*isSettable=*/false,
815 android::gralloc4::MetadataType_Smpte2086,
818 /*isSettable=*/false,
821 android::gralloc4::MetadataType_Cta861_3,
824 /*isSettable=*/false,
827 android::gralloc4::MetadataType_Smpte2094_40,
830 /*isSettable=*/false,
834 hidlCb(Error::NONE, supported);
838 Return<void> CrosGralloc4Mapper::dumpBuffer(void* rawHandle, dumpBuffer_cb hidlCb) {
839 BufferDump bufferDump;
842 drv_log("Failed to dumpBuffer. Driver is uninitialized.\n");
843 hidlCb(Error::NO_RESOURCES, bufferDump);
847 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
849 drv_log("Failed to dumpBuffer. Empty handle.\n");
850 hidlCb(Error::BAD_BUFFER, bufferDump);
854 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
856 drv_log("Failed to dumpBuffer. Invalid handle.\n");
857 hidlCb(Error::BAD_BUFFER, bufferDump);
861 return dumpBuffer(crosHandle, hidlCb);
864 Return<void> CrosGralloc4Mapper::dumpBuffer(cros_gralloc_handle_t crosHandle,
865 dumpBuffer_cb hidlCb) {
866 BufferDump bufferDump;
869 drv_log("Failed to dumpBuffer. Driver is uninitialized.\n");
870 hidlCb(Error::NO_RESOURCES, bufferDump);
875 drv_log("Failed to dumpBuffer. Invalid handle.\n");
876 hidlCb(Error::BAD_BUFFER, bufferDump);
880 std::vector<MetadataDump> metadataDumps;
882 MetadataType metadataType = android::gralloc4::MetadataType_BufferId;
883 auto metadata_get_callback = [&](Error, hidl_vec<uint8_t> metadata) {
884 MetadataDump metadataDump;
885 metadataDump.metadataType = metadataType;
886 metadataDump.metadata = metadata;
887 metadataDumps.push_back(metadataDump);
890 metadataType = android::gralloc4::MetadataType_BufferId;
891 get(crosHandle, metadataType, metadata_get_callback);
893 metadataType = android::gralloc4::MetadataType_Name;
894 get(crosHandle, metadataType, metadata_get_callback);
896 metadataType = android::gralloc4::MetadataType_Width;
897 get(crosHandle, metadataType, metadata_get_callback);
899 metadataType = android::gralloc4::MetadataType_Height;
900 get(crosHandle, metadataType, metadata_get_callback);
902 metadataType = android::gralloc4::MetadataType_LayerCount;
903 get(crosHandle, metadataType, metadata_get_callback);
905 metadataType = android::gralloc4::MetadataType_PixelFormatRequested;
906 get(crosHandle, metadataType, metadata_get_callback);
908 metadataType = android::gralloc4::MetadataType_PixelFormatFourCC;
909 get(crosHandle, metadataType, metadata_get_callback);
911 metadataType = android::gralloc4::MetadataType_PixelFormatModifier;
912 get(crosHandle, metadataType, metadata_get_callback);
914 metadataType = android::gralloc4::MetadataType_Usage;
915 get(crosHandle, metadataType, metadata_get_callback);
917 metadataType = android::gralloc4::MetadataType_AllocationSize;
918 get(crosHandle, metadataType, metadata_get_callback);
920 metadataType = android::gralloc4::MetadataType_ProtectedContent;
921 get(crosHandle, metadataType, metadata_get_callback);
923 metadataType = android::gralloc4::MetadataType_Compression;
924 get(crosHandle, metadataType, metadata_get_callback);
926 metadataType = android::gralloc4::MetadataType_Interlaced;
927 get(crosHandle, metadataType, metadata_get_callback);
929 metadataType = android::gralloc4::MetadataType_ChromaSiting;
930 get(crosHandle, metadataType, metadata_get_callback);
932 metadataType = android::gralloc4::MetadataType_PlaneLayouts;
933 get(crosHandle, metadataType, metadata_get_callback);
935 metadataType = android::gralloc4::MetadataType_Dataspace;
936 get(crosHandle, metadataType, metadata_get_callback);
938 metadataType = android::gralloc4::MetadataType_BlendMode;
939 get(crosHandle, metadataType, metadata_get_callback);
941 bufferDump.metadataDump = metadataDumps;
942 hidlCb(Error::NONE, bufferDump);
946 Return<void> CrosGralloc4Mapper::dumpBuffers(dumpBuffers_cb hidlCb) {
947 std::vector<BufferDump> bufferDumps;
950 drv_log("Failed to dumpBuffers. Driver is uninitialized.\n");
951 hidlCb(Error::NO_RESOURCES, bufferDumps);
955 Error error = Error::NONE;
957 auto handleCallback = [&](cros_gralloc_handle_t crosHandle) {
958 auto dumpBufferCallback = [&](Error err, BufferDump bufferDump) {
960 if (error == Error::NONE) {
961 bufferDumps.push_back(bufferDump);
965 dumpBuffer(crosHandle, dumpBufferCallback);
967 mDriver->for_each_handle(handleCallback);
969 hidlCb(error, bufferDumps);
973 Return<void> CrosGralloc4Mapper::getReservedRegion(void* rawHandle, getReservedRegion_cb hidlCb) {
975 drv_log("Failed to getReservedRegion. Driver is uninitialized.\n");
976 hidlCb(Error::NO_RESOURCES, nullptr, 0);
980 buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
982 drv_log("Failed to getReservedRegion. Empty handle.\n");
983 hidlCb(Error::BAD_BUFFER, nullptr, 0);
987 cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
989 drv_log("Failed to getReservedRegion. Invalid handle.\n");
990 hidlCb(Error::BAD_BUFFER, nullptr, 0);
994 void* reservedRegionAddr = nullptr;
995 uint64_t reservedRegionSize = 0;
996 int ret = mDriver->get_reserved_region(bufferHandle, &reservedRegionAddr, &reservedRegionSize);
998 drv_log("Failed to getReservedRegion.\n");
999 hidlCb(Error::BAD_BUFFER, nullptr, 0);
1003 hidlCb(Error::NONE, reservedRegionAddr, reservedRegionSize);
1007 android::hardware::graphics::mapper::V4_0::IMapper* HIDL_FETCH_IMapper(const char* /*name*/) {
1008 return static_cast<android::hardware::graphics::mapper::V4_0::IMapper*>(new CrosGralloc4Mapper);