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/CrosGralloc4Utils.h"
10 #include <unordered_map>
12 #include <aidl/android/hardware/graphics/common/PlaneLayoutComponent.h>
13 #include <aidl/android/hardware/graphics/common/PlaneLayoutComponentType.h>
14 #include <android-base/stringprintf.h>
15 #include <android-base/strings.h>
16 #include <cutils/native_handle.h>
17 #include <gralloctypes/Gralloc4.h>
19 #include "cros_gralloc/cros_gralloc_helpers.h"
21 using aidl::android::hardware::graphics::common::PlaneLayout;
22 using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
23 using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
24 using android::hardware::hidl_bitfield;
25 using android::hardware::hidl_handle;
26 using android::hardware::graphics::common::V1_2::BufferUsage;
27 using android::hardware::graphics::common::V1_2::PixelFormat;
29 using BufferDescriptorInfo =
30 android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
32 std::string getPixelFormatString(PixelFormat format) {
34 case PixelFormat::BGRA_8888:
35 return "PixelFormat::BGRA_8888";
36 case PixelFormat::BLOB:
37 return "PixelFormat::BLOB";
38 case PixelFormat::DEPTH_16:
39 return "PixelFormat::DEPTH_16";
40 case PixelFormat::DEPTH_24:
41 return "PixelFormat::DEPTH_24";
42 case PixelFormat::DEPTH_24_STENCIL_8:
43 return "PixelFormat::DEPTH_24_STENCIL_8";
44 case PixelFormat::DEPTH_32F:
45 return "PixelFormat::DEPTH_24";
46 case PixelFormat::DEPTH_32F_STENCIL_8:
47 return "PixelFormat::DEPTH_24_STENCIL_8";
48 case PixelFormat::HSV_888:
49 return "PixelFormat::HSV_888";
50 case PixelFormat::IMPLEMENTATION_DEFINED:
51 return "PixelFormat::IMPLEMENTATION_DEFINED";
52 case PixelFormat::RAW10:
53 return "PixelFormat::RAW10";
54 case PixelFormat::RAW12:
55 return "PixelFormat::RAW12";
56 case PixelFormat::RAW16:
57 return "PixelFormat::RAW16";
58 case PixelFormat::RAW_OPAQUE:
59 return "PixelFormat::RAW_OPAQUE";
60 case PixelFormat::RGBA_1010102:
61 return "PixelFormat::RGBA_1010102";
62 case PixelFormat::RGBA_8888:
63 return "PixelFormat::RGBA_8888";
64 case PixelFormat::RGBA_FP16:
65 return "PixelFormat::RGBA_FP16";
66 case PixelFormat::RGBX_8888:
67 return "PixelFormat::RGBX_8888";
68 case PixelFormat::RGB_565:
69 return "PixelFormat::RGB_565";
70 case PixelFormat::RGB_888:
71 return "PixelFormat::RGB_888";
72 case PixelFormat::STENCIL_8:
73 return "PixelFormat::STENCIL_8";
74 case PixelFormat::Y16:
75 return "PixelFormat::Y16";
77 return "PixelFormat::Y8";
78 case PixelFormat::YCBCR_420_888:
79 return "PixelFormat::YCBCR_420_888";
80 case PixelFormat::YCBCR_422_I:
81 return "PixelFormat::YCBCR_422_I";
82 case PixelFormat::YCBCR_422_SP:
83 return "PixelFormat::YCBCR_422_SP";
84 case PixelFormat::YCBCR_P010:
85 return "PixelFormat::YCBCR_P010";
86 case PixelFormat::YCRCB_420_SP:
87 return "PixelFormat::YCRCB_420_SP";
88 case PixelFormat::YV12:
89 return "PixelFormat::YV12";
91 return android::base::StringPrintf("PixelFormat::Unknown(%d)", static_cast<uint32_t>(format));
94 std::string getUsageString(hidl_bitfield<BufferUsage> bufferUsage) {
95 using Underlying = typename std::underlying_type<BufferUsage>::type;
97 Underlying usage = static_cast<Underlying>(bufferUsage);
99 std::vector<std::string> usages;
100 if (usage & BufferUsage::CAMERA_INPUT) {
101 usage &= ~static_cast<Underlying>(BufferUsage::CAMERA_INPUT);
102 usages.push_back("BufferUsage::CAMERA_INPUT");
104 if (usage & BufferUsage::CAMERA_OUTPUT) {
105 usage &= ~static_cast<Underlying>(BufferUsage::CAMERA_OUTPUT);
106 usages.push_back("BufferUsage::CAMERA_OUTPUT");
108 if (usage & BufferUsage::COMPOSER_CURSOR) {
109 usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_CURSOR);
110 usages.push_back("BufferUsage::COMPOSER_CURSOR");
112 if (usage & BufferUsage::COMPOSER_OVERLAY) {
113 usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_OVERLAY);
114 usages.push_back("BufferUsage::COMPOSER_OVERLAY");
116 if (usage & BufferUsage::CPU_READ_OFTEN) {
117 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_OFTEN);
118 usages.push_back("BufferUsage::CPU_READ_OFTEN");
120 if (usage & BufferUsage::CPU_READ_NEVER) {
121 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_NEVER);
122 usages.push_back("BufferUsage::CPU_READ_NEVER");
124 if (usage & BufferUsage::CPU_READ_RARELY) {
125 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_RARELY);
126 usages.push_back("BufferUsage::CPU_READ_RARELY");
128 if (usage & BufferUsage::CPU_WRITE_NEVER) {
129 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_NEVER);
130 usages.push_back("BufferUsage::CPU_WRITE_NEVER");
132 if (usage & BufferUsage::CPU_WRITE_OFTEN) {
133 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_OFTEN);
134 usages.push_back("BufferUsage::CPU_WRITE_OFTEN");
136 if (usage & BufferUsage::CPU_WRITE_RARELY) {
137 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_RARELY);
138 usages.push_back("BufferUsage::CPU_WRITE_RARELY");
140 if (usage & BufferUsage::GPU_RENDER_TARGET) {
141 usage &= ~static_cast<Underlying>(BufferUsage::GPU_RENDER_TARGET);
142 usages.push_back("BufferUsage::GPU_RENDER_TARGET");
144 if (usage & BufferUsage::GPU_TEXTURE) {
145 usage &= ~static_cast<Underlying>(BufferUsage::GPU_TEXTURE);
146 usages.push_back("BufferUsage::GPU_TEXTURE");
148 if (usage & BufferUsage::PROTECTED) {
149 usage &= ~static_cast<Underlying>(BufferUsage::PROTECTED);
150 usages.push_back("BufferUsage::PROTECTED");
152 if (usage & BufferUsage::RENDERSCRIPT) {
153 usage &= ~static_cast<Underlying>(BufferUsage::RENDERSCRIPT);
154 usages.push_back("BufferUsage::RENDERSCRIPT");
156 if (usage & BufferUsage::VIDEO_DECODER) {
157 usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_DECODER);
158 usages.push_back("BufferUsage::VIDEO_DECODER");
160 if (usage & BufferUsage::VIDEO_ENCODER) {
161 usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_ENCODER);
162 usages.push_back("BufferUsage::VIDEO_ENCODER");
166 usages.push_back(android::base::StringPrintf("UnknownUsageBits-%" PRIu64, usage));
169 return android::base::Join(usages, '|');
172 int convertToDrmFormat(PixelFormat format, uint32_t* outDrmFormat) {
174 case PixelFormat::BGRA_8888:
175 *outDrmFormat = DRM_FORMAT_ARGB8888;
178 * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
179 * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
180 * equal to their size in bytes.
182 case PixelFormat::BLOB:
183 *outDrmFormat = DRM_FORMAT_R8;
185 case PixelFormat::DEPTH_16:
187 case PixelFormat::DEPTH_24:
189 case PixelFormat::DEPTH_24_STENCIL_8:
191 case PixelFormat::DEPTH_32F:
193 case PixelFormat::DEPTH_32F_STENCIL_8:
195 case PixelFormat::HSV_888:
197 case PixelFormat::IMPLEMENTATION_DEFINED:
198 *outDrmFormat = DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
200 case PixelFormat::RAW10:
202 case PixelFormat::RAW12:
204 case PixelFormat::RAW16:
205 *outDrmFormat = DRM_FORMAT_R16;
208 case PixelFormat::RAW_OPAQUE:
210 case PixelFormat::RGBA_1010102:
211 *outDrmFormat = DRM_FORMAT_ABGR2101010;
213 case PixelFormat::RGBA_8888:
214 *outDrmFormat = DRM_FORMAT_ABGR8888;
216 case PixelFormat::RGBA_FP16:
217 *outDrmFormat = DRM_FORMAT_ABGR16161616F;
219 case PixelFormat::RGBX_8888:
220 *outDrmFormat = DRM_FORMAT_XBGR8888;
222 case PixelFormat::RGB_565:
223 *outDrmFormat = DRM_FORMAT_RGB565;
225 case PixelFormat::RGB_888:
226 *outDrmFormat = DRM_FORMAT_RGB888;
228 case PixelFormat::STENCIL_8:
230 case PixelFormat::Y16:
231 *outDrmFormat = DRM_FORMAT_R16;
233 case PixelFormat::Y8:
234 *outDrmFormat = DRM_FORMAT_R8;
236 case PixelFormat::YCBCR_420_888:
237 *outDrmFormat = DRM_FORMAT_FLEX_YCbCr_420_888;
239 case PixelFormat::YCBCR_422_SP:
241 case PixelFormat::YCBCR_422_I:
243 case PixelFormat::YCBCR_P010:
244 *outDrmFormat = DRM_FORMAT_P010;
246 case PixelFormat::YCRCB_420_SP:
247 *outDrmFormat = DRM_FORMAT_NV21;
249 case PixelFormat::YV12:
250 *outDrmFormat = DRM_FORMAT_YVU420_ANDROID;
256 int convertToBufferUsage(uint64_t grallocUsage, uint64_t* outBufferUsage) {
257 uint64_t bufferUsage = BO_USE_NONE;
259 if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
260 static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
261 bufferUsage |= BO_USE_SW_READ_RARELY;
263 if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
264 static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN)) {
265 bufferUsage |= BO_USE_SW_READ_OFTEN;
267 if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
268 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
269 bufferUsage |= BO_USE_SW_WRITE_RARELY;
271 if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
272 static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN)) {
273 bufferUsage |= BO_USE_SW_WRITE_OFTEN;
275 if (grallocUsage & BufferUsage::GPU_TEXTURE) {
276 bufferUsage |= BO_USE_TEXTURE;
278 if (grallocUsage & BufferUsage::GPU_RENDER_TARGET) {
279 bufferUsage |= BO_USE_RENDERING;
281 if (grallocUsage & BufferUsage::COMPOSER_OVERLAY) {
282 /* HWC wants to use display hardware, but can defer to OpenGL. */
283 bufferUsage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
285 /* Map this flag to linear until real HW protection is available on Android. */
286 if (grallocUsage & BufferUsage::PROTECTED) {
287 bufferUsage |= BO_USE_LINEAR;
289 if (grallocUsage & BufferUsage::COMPOSER_CURSOR) {
290 bufferUsage |= BO_USE_NONE;
292 if (grallocUsage & BufferUsage::VIDEO_ENCODER) {
293 /*HACK: See b/30054495 */
294 bufferUsage |= BO_USE_SW_READ_OFTEN;
296 if (grallocUsage & BufferUsage::CAMERA_OUTPUT) {
297 bufferUsage |= BO_USE_CAMERA_WRITE;
299 if (grallocUsage & BufferUsage::CAMERA_INPUT) {
300 bufferUsage |= BO_USE_CAMERA_READ;
302 if (grallocUsage & BufferUsage::RENDERSCRIPT) {
303 bufferUsage |= BO_USE_RENDERSCRIPT;
305 if (grallocUsage & BufferUsage::VIDEO_DECODER) {
306 bufferUsage |= BO_USE_HW_VIDEO_DECODER;
309 *outBufferUsage = bufferUsage;
313 int convertToCrosDescriptor(const BufferDescriptorInfo& descriptor,
314 struct cros_gralloc_buffer_descriptor* outCrosDescriptor) {
315 outCrosDescriptor->name = descriptor.name;
316 outCrosDescriptor->width = descriptor.width;
317 outCrosDescriptor->height = descriptor.height;
318 outCrosDescriptor->droid_format = static_cast<int32_t>(descriptor.format);
319 outCrosDescriptor->droid_usage = descriptor.usage;
320 outCrosDescriptor->reserved_region_size = descriptor.reservedSize;
321 if (descriptor.layerCount > 1) {
322 drv_log("Failed to convert descriptor. Unsupported layerCount: %d\n",
323 descriptor.layerCount);
326 if (convertToDrmFormat(descriptor.format, &outCrosDescriptor->drm_format)) {
327 std::string pixelFormatString = getPixelFormatString(descriptor.format);
328 drv_log("Failed to convert descriptor. Unsupported format %s\n", pixelFormatString.c_str());
331 if (convertToBufferUsage(descriptor.usage, &outCrosDescriptor->use_flags)) {
332 std::string usageString = getUsageString(descriptor.usage);
333 drv_log("Failed to convert descriptor. Unsupported usage flags %s\n", usageString.c_str());
339 int convertToMapUsage(uint64_t grallocUsage, uint32_t* outMapUsage) {
340 uint32_t mapUsage = BO_MAP_NONE;
342 if (grallocUsage & BufferUsage::CPU_READ_MASK) {
343 mapUsage |= BO_MAP_READ;
345 if (grallocUsage & BufferUsage::CPU_WRITE_MASK) {
346 mapUsage |= BO_MAP_WRITE;
349 *outMapUsage = mapUsage;
353 int convertToFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) {
358 const native_handle_t* nativeHandle = fenceHandle.getNativeHandle();
359 if (nativeHandle && nativeHandle->numFds > 1) {
363 *outFenceFd = (nativeHandle && nativeHandle->numFds == 1) ? nativeHandle->data[0] : -1;
367 int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
368 if (!outFenceHandle) {
375 NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
376 auto fenceHandle = native_handle_init(handleStorage, 1, 0);
377 fenceHandle->data[0] = fenceFd;
379 *outFenceHandle = fenceHandle;
383 const std::unordered_map<uint32_t, std::vector<PlaneLayout>>& GetPlaneLayoutsMap() {
384 static const auto* kPlaneLayoutsMap =
385 new std::unordered_map<uint32_t, std::vector<PlaneLayout>>({
386 {DRM_FORMAT_ABGR8888,
388 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
391 {.type = android::gralloc4::PlaneLayoutComponentType_G,
394 {.type = android::gralloc4::PlaneLayoutComponentType_B,
397 {.type = android::gralloc4::PlaneLayoutComponentType_A,
400 .sampleIncrementInBits = 32,
401 .horizontalSubsampling = 1,
402 .verticalSubsampling = 1,
405 {DRM_FORMAT_ABGR2101010,
407 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
410 {.type = android::gralloc4::PlaneLayoutComponentType_G,
413 {.type = android::gralloc4::PlaneLayoutComponentType_B,
416 {.type = android::gralloc4::PlaneLayoutComponentType_A,
419 .sampleIncrementInBits = 32,
420 .horizontalSubsampling = 1,
421 .verticalSubsampling = 1,
424 {DRM_FORMAT_ABGR16161616F,
426 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
429 {.type = android::gralloc4::PlaneLayoutComponentType_G,
432 {.type = android::gralloc4::PlaneLayoutComponentType_B,
435 {.type = android::gralloc4::PlaneLayoutComponentType_A,
438 .sampleIncrementInBits = 64,
439 .horizontalSubsampling = 1,
440 .verticalSubsampling = 1,
443 {DRM_FORMAT_ARGB8888,
445 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
448 {.type = android::gralloc4::PlaneLayoutComponentType_G,
451 {.type = android::gralloc4::PlaneLayoutComponentType_R,
454 {.type = android::gralloc4::PlaneLayoutComponentType_A,
457 .sampleIncrementInBits = 32,
458 .horizontalSubsampling = 1,
459 .verticalSubsampling = 1,
462 {DRM_FORMAT_ARGB4444,
464 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
467 {.type = android::gralloc4::PlaneLayoutComponentType_G,
470 {.type = android::gralloc4::PlaneLayoutComponentType_B,
473 {.type = android::gralloc4::PlaneLayoutComponentType_A,
476 .sampleIncrementInBits = 16,
477 .horizontalSubsampling = 1,
478 .verticalSubsampling = 1,
483 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
486 .sampleIncrementInBits = 8,
487 .horizontalSubsampling = 1,
488 .verticalSubsampling = 1,
492 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
495 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
498 .sampleIncrementInBits = 16,
499 .horizontalSubsampling = 2,
500 .verticalSubsampling = 2,
505 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
508 .sampleIncrementInBits = 8,
509 .horizontalSubsampling = 1,
510 .verticalSubsampling = 1,
514 {{.type = android::gralloc4::PlaneLayoutComponentType_CR,
517 {.type = android::gralloc4::PlaneLayoutComponentType_CB,
520 .sampleIncrementInBits = 16,
521 .horizontalSubsampling = 2,
522 .verticalSubsampling = 2,
527 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
530 .sampleIncrementInBits = 16,
531 .horizontalSubsampling = 1,
532 .verticalSubsampling = 1,
536 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
539 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
542 .sampleIncrementInBits = 32,
543 .horizontalSubsampling = 2,
544 .verticalSubsampling = 2,
549 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
552 .sampleIncrementInBits = 8,
553 .horizontalSubsampling = 1,
554 .verticalSubsampling = 1,
559 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
562 .sampleIncrementInBits = 16,
563 .horizontalSubsampling = 1,
564 .verticalSubsampling = 1,
569 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
572 {.type = android::gralloc4::PlaneLayoutComponentType_G,
575 {.type = android::gralloc4::PlaneLayoutComponentType_B,
578 .sampleIncrementInBits = 16,
579 .horizontalSubsampling = 1,
580 .verticalSubsampling = 1,
585 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
588 {.type = android::gralloc4::PlaneLayoutComponentType_G,
591 {.type = android::gralloc4::PlaneLayoutComponentType_B,
594 .sampleIncrementInBits = 24,
595 .horizontalSubsampling = 1,
596 .verticalSubsampling = 1,
599 {DRM_FORMAT_XBGR8888,
601 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
604 {.type = android::gralloc4::PlaneLayoutComponentType_G,
607 {.type = android::gralloc4::PlaneLayoutComponentType_R,
610 .sampleIncrementInBits = 32,
611 .horizontalSubsampling = 1,
612 .verticalSubsampling = 1,
618 .components = {{.type = android::gralloc4::
619 PlaneLayoutComponentType_Y,
622 .sampleIncrementInBits = 8,
623 .horizontalSubsampling = 1,
624 .verticalSubsampling = 1,
627 .components = {{.type = android::gralloc4::
628 PlaneLayoutComponentType_CB,
631 .sampleIncrementInBits = 8,
632 .horizontalSubsampling = 2,
633 .verticalSubsampling = 2,
636 .components = {{.type = android::gralloc4::
637 PlaneLayoutComponentType_CR,
640 .sampleIncrementInBits = 8,
641 .horizontalSubsampling = 2,
642 .verticalSubsampling = 2,
646 {DRM_FORMAT_YVU420_ANDROID,
649 .components = {{.type = android::gralloc4::
650 PlaneLayoutComponentType_Y,
653 .sampleIncrementInBits = 8,
654 .horizontalSubsampling = 1,
655 .verticalSubsampling = 1,
658 .components = {{.type = android::gralloc4::
659 PlaneLayoutComponentType_CR,
662 .sampleIncrementInBits = 8,
663 .horizontalSubsampling = 2,
664 .verticalSubsampling = 2,
667 .components = {{.type = android::gralloc4::
668 PlaneLayoutComponentType_CB,
671 .sampleIncrementInBits = 8,
672 .horizontalSubsampling = 2,
673 .verticalSubsampling = 2,
677 return *kPlaneLayoutsMap;
680 int getPlaneLayouts(uint32_t drmFormat, std::vector<PlaneLayout>* outPlaneLayouts) {
681 const auto& planeLayoutsMap = GetPlaneLayoutsMap();
682 const auto it = planeLayoutsMap.find(drmFormat);
683 if (it == planeLayoutsMap.end()) {
684 drv_log("Unknown plane layout for format %d\n", drmFormat);
688 *outPlaneLayouts = it->second;