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;
322 if (convertToDrmFormat(descriptor.format, &outCrosDescriptor->drm_format)) {
323 std::string pixelFormatString = getPixelFormatString(descriptor.format);
324 drv_log("Failed to convert descriptor. Unsupported fomat %s\n", pixelFormatString.c_str());
327 if (convertToBufferUsage(descriptor.usage, &outCrosDescriptor->use_flags)) {
328 std::string usageString = getUsageString(descriptor.usage);
329 drv_log("Failed to convert descriptor. Unsupported usage flags %s\n", usageString.c_str());
335 int convertToMapUsage(uint64_t grallocUsage, uint32_t* outMapUsage) {
336 uint32_t mapUsage = BO_MAP_NONE;
338 if (grallocUsage & BufferUsage::CPU_READ_MASK) {
339 mapUsage |= BO_MAP_READ;
341 if (grallocUsage & BufferUsage::CPU_WRITE_MASK) {
342 mapUsage |= BO_MAP_WRITE;
345 *outMapUsage = mapUsage;
349 int convertToFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) {
354 const native_handle_t* nativeHandle = fenceHandle.getNativeHandle();
355 if (nativeHandle && nativeHandle->numFds > 1) {
359 *outFenceFd = (nativeHandle && nativeHandle->numFds == 1) ? nativeHandle->data[0] : -1;
363 int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
364 if (!outFenceHandle) {
371 NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
372 auto fenceHandle = native_handle_init(handleStorage, 1, 0);
373 fenceHandle->data[0] = fenceFd;
375 *outFenceHandle = fenceHandle;
379 const std::unordered_map<uint32_t, std::vector<PlaneLayout>>& GetPlaneLayoutsMap() {
380 static const auto* kPlaneLayoutsMap =
381 new std::unordered_map<uint32_t, std::vector<PlaneLayout>>({
382 {DRM_FORMAT_ABGR8888,
384 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
387 {.type = android::gralloc4::PlaneLayoutComponentType_G,
390 {.type = android::gralloc4::PlaneLayoutComponentType_B,
393 {.type = android::gralloc4::PlaneLayoutComponentType_A,
396 .sampleIncrementInBits = 32,
397 .horizontalSubsampling = 1,
398 .verticalSubsampling = 1,
401 {DRM_FORMAT_ABGR2101010,
403 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
406 {.type = android::gralloc4::PlaneLayoutComponentType_G,
409 {.type = android::gralloc4::PlaneLayoutComponentType_B,
412 {.type = android::gralloc4::PlaneLayoutComponentType_A,
415 .sampleIncrementInBits = 32,
416 .horizontalSubsampling = 1,
417 .verticalSubsampling = 1,
420 {DRM_FORMAT_ABGR16161616F,
422 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
425 {.type = android::gralloc4::PlaneLayoutComponentType_G,
428 {.type = android::gralloc4::PlaneLayoutComponentType_B,
431 {.type = android::gralloc4::PlaneLayoutComponentType_A,
434 .sampleIncrementInBits = 64,
435 .horizontalSubsampling = 1,
436 .verticalSubsampling = 1,
439 {DRM_FORMAT_ARGB8888,
441 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
444 {.type = android::gralloc4::PlaneLayoutComponentType_G,
447 {.type = android::gralloc4::PlaneLayoutComponentType_R,
450 {.type = android::gralloc4::PlaneLayoutComponentType_A,
453 .sampleIncrementInBits = 32,
454 .horizontalSubsampling = 1,
455 .verticalSubsampling = 1,
460 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
463 .sampleIncrementInBits = 8,
464 .horizontalSubsampling = 1,
465 .verticalSubsampling = 1,
469 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
472 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
475 .sampleIncrementInBits = 16,
476 .horizontalSubsampling = 2,
477 .verticalSubsampling = 2,
482 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
485 .sampleIncrementInBits = 8,
486 .horizontalSubsampling = 1,
487 .verticalSubsampling = 1,
491 {{.type = android::gralloc4::PlaneLayoutComponentType_CR,
494 {.type = android::gralloc4::PlaneLayoutComponentType_CB,
497 .sampleIncrementInBits = 16,
498 .horizontalSubsampling = 2,
499 .verticalSubsampling = 2,
504 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
507 .sampleIncrementInBits = 16,
508 .horizontalSubsampling = 1,
509 .verticalSubsampling = 1,
513 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
516 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
519 .sampleIncrementInBits = 32,
520 .horizontalSubsampling = 2,
521 .verticalSubsampling = 2,
526 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
529 .sampleIncrementInBits = 8,
530 .horizontalSubsampling = 1,
531 .verticalSubsampling = 1,
536 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
539 .sampleIncrementInBits = 16,
540 .horizontalSubsampling = 1,
541 .verticalSubsampling = 1,
546 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
549 {.type = android::gralloc4::PlaneLayoutComponentType_G,
552 {.type = android::gralloc4::PlaneLayoutComponentType_B,
555 .sampleIncrementInBits = 16,
556 .horizontalSubsampling = 1,
557 .verticalSubsampling = 1,
562 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
565 {.type = android::gralloc4::PlaneLayoutComponentType_G,
568 {.type = android::gralloc4::PlaneLayoutComponentType_B,
571 .sampleIncrementInBits = 24,
572 .horizontalSubsampling = 1,
573 .verticalSubsampling = 1,
576 {DRM_FORMAT_XBGR8888,
578 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
581 {.type = android::gralloc4::PlaneLayoutComponentType_G,
584 {.type = android::gralloc4::PlaneLayoutComponentType_R,
587 .sampleIncrementInBits = 32,
588 .horizontalSubsampling = 1,
589 .verticalSubsampling = 1,
595 .components = {{.type = android::gralloc4::
596 PlaneLayoutComponentType_Y,
599 .sampleIncrementInBits = 8,
600 .horizontalSubsampling = 1,
601 .verticalSubsampling = 1,
604 .components = {{.type = android::gralloc4::
605 PlaneLayoutComponentType_CB,
608 .sampleIncrementInBits = 8,
609 .horizontalSubsampling = 2,
610 .verticalSubsampling = 2,
613 .components = {{.type = android::gralloc4::
614 PlaneLayoutComponentType_CR,
617 .sampleIncrementInBits = 8,
618 .horizontalSubsampling = 2,
619 .verticalSubsampling = 2,
623 {DRM_FORMAT_YVU420_ANDROID,
626 .components = {{.type = android::gralloc4::
627 PlaneLayoutComponentType_Y,
630 .sampleIncrementInBits = 8,
631 .horizontalSubsampling = 1,
632 .verticalSubsampling = 1,
635 .components = {{.type = android::gralloc4::
636 PlaneLayoutComponentType_CR,
639 .sampleIncrementInBits = 8,
640 .horizontalSubsampling = 2,
641 .verticalSubsampling = 2,
644 .components = {{.type = android::gralloc4::
645 PlaneLayoutComponentType_CB,
648 .sampleIncrementInBits = 8,
649 .horizontalSubsampling = 2,
650 .verticalSubsampling = 2,
654 return *kPlaneLayoutsMap;
657 int getPlaneLayouts(uint32_t drmFormat, std::vector<PlaneLayout>* outPlaneLayouts) {
658 const auto& planeLayoutsMap = GetPlaneLayoutsMap();
659 const auto it = planeLayoutsMap.find(drmFormat);
660 if (it == planeLayoutsMap.end()) {
661 drv_log("Unknown plane layout for format %d\n", drmFormat);
665 *outPlaneLayouts = it->second;