1 /*
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.
5 */
6
7 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
8
9 #include <array>
10 #include <unordered_map>
11
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>
18
19 #include "cros_gralloc/cros_gralloc_helpers.h"
20
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;
28
29 using BufferDescriptorInfo =
30 android::hardware::graphics::mapper::V4_0::IMapper::BufferDescriptorInfo;
31
getDrmFormatString(uint32_t drmFormat)32 std::string getDrmFormatString(uint32_t drmFormat) {
33 switch (drmFormat) {
34 case DRM_FORMAT_ABGR1555:
35 return "DRM_FORMAT_ABGR1555";
36 case DRM_FORMAT_ABGR2101010:
37 return "DRM_FORMAT_ABGR2101010";
38 case DRM_FORMAT_ABGR4444:
39 return "DRM_FORMAT_ABGR4444";
40 case DRM_FORMAT_ABGR8888:
41 return "DRM_FORMAT_ABGR8888";
42 case DRM_FORMAT_ARGB1555:
43 return "DRM_FORMAT_ARGB1555";
44 case DRM_FORMAT_ARGB2101010:
45 return "DRM_FORMAT_ARGB2101010";
46 case DRM_FORMAT_ARGB4444:
47 return "DRM_FORMAT_ARGB4444";
48 case DRM_FORMAT_ARGB8888:
49 return "DRM_FORMAT_ARGB8888";
50 case DRM_FORMAT_AYUV:
51 return "DRM_FORMAT_AYUV";
52 case DRM_FORMAT_BGR233:
53 return "DRM_FORMAT_BGR233";
54 case DRM_FORMAT_BGR565:
55 return "DRM_FORMAT_BGR565";
56 case DRM_FORMAT_BGR888:
57 return "DRM_FORMAT_BGR888";
58 case DRM_FORMAT_BGRA1010102:
59 return "DRM_FORMAT_BGRA1010102";
60 case DRM_FORMAT_BGRA4444:
61 return "DRM_FORMAT_BGRA4444";
62 case DRM_FORMAT_BGRA5551:
63 return "DRM_FORMAT_BGRA5551";
64 case DRM_FORMAT_BGRA8888:
65 return "DRM_FORMAT_BGRA8888";
66 case DRM_FORMAT_BGRX1010102:
67 return "DRM_FORMAT_BGRX1010102";
68 case DRM_FORMAT_BGRX4444:
69 return "DRM_FORMAT_BGRX4444";
70 case DRM_FORMAT_BGRX5551:
71 return "DRM_FORMAT_BGRX5551";
72 case DRM_FORMAT_BGRX8888:
73 return "DRM_FORMAT_BGRX8888";
74 case DRM_FORMAT_C8:
75 return "DRM_FORMAT_C8";
76 case DRM_FORMAT_GR88:
77 return "DRM_FORMAT_GR88";
78 case DRM_FORMAT_NV12:
79 return "DRM_FORMAT_NV12";
80 case DRM_FORMAT_NV21:
81 return "DRM_FORMAT_NV21";
82 case DRM_FORMAT_R8:
83 return "DRM_FORMAT_R8";
84 case DRM_FORMAT_RG88:
85 return "DRM_FORMAT_RG88";
86 case DRM_FORMAT_RGB332:
87 return "DRM_FORMAT_RGB332";
88 case DRM_FORMAT_RGB565:
89 return "DRM_FORMAT_RGB565";
90 case DRM_FORMAT_RGB888:
91 return "DRM_FORMAT_RGB888";
92 case DRM_FORMAT_RGBA1010102:
93 return "DRM_FORMAT_RGBA1010102";
94 case DRM_FORMAT_RGBA4444:
95 return "DRM_FORMAT_RGBA4444";
96 case DRM_FORMAT_RGBA5551:
97 return "DRM_FORMAT_RGBA5551";
98 case DRM_FORMAT_RGBA8888:
99 return "DRM_FORMAT_RGBA8888";
100 case DRM_FORMAT_RGBX1010102:
101 return "DRM_FORMAT_RGBX1010102";
102 case DRM_FORMAT_RGBX4444:
103 return "DRM_FORMAT_RGBX4444";
104 case DRM_FORMAT_RGBX5551:
105 return "DRM_FORMAT_RGBX5551";
106 case DRM_FORMAT_RGBX8888:
107 return "DRM_FORMAT_RGBX8888";
108 case DRM_FORMAT_UYVY:
109 return "DRM_FORMAT_UYVY";
110 case DRM_FORMAT_VYUY:
111 return "DRM_FORMAT_VYUY";
112 case DRM_FORMAT_XBGR1555:
113 return "DRM_FORMAT_XBGR1555";
114 case DRM_FORMAT_XBGR2101010:
115 return "DRM_FORMAT_XBGR2101010";
116 case DRM_FORMAT_XBGR4444:
117 return "DRM_FORMAT_XBGR4444";
118 case DRM_FORMAT_XBGR8888:
119 return "DRM_FORMAT_XBGR8888";
120 case DRM_FORMAT_XRGB1555:
121 return "DRM_FORMAT_XRGB1555";
122 case DRM_FORMAT_XRGB2101010:
123 return "DRM_FORMAT_XRGB2101010";
124 case DRM_FORMAT_XRGB4444:
125 return "DRM_FORMAT_XRGB4444";
126 case DRM_FORMAT_XRGB8888:
127 return "DRM_FORMAT_XRGB8888";
128 case DRM_FORMAT_YUYV:
129 return "DRM_FORMAT_YUYV";
130 case DRM_FORMAT_YVU420:
131 return "DRM_FORMAT_YVU420";
132 case DRM_FORMAT_YVYU:
133 return "DRM_FORMAT_YVYU";
134 }
135 return android::base::StringPrintf("Unknown(%d)", drmFormat);
136 }
137
getPixelFormatString(PixelFormat format)138 std::string getPixelFormatString(PixelFormat format) {
139 switch (format) {
140 case PixelFormat::BGRA_8888:
141 return "PixelFormat::BGRA_8888";
142 case PixelFormat::BLOB:
143 return "PixelFormat::BLOB";
144 case PixelFormat::DEPTH_16:
145 return "PixelFormat::DEPTH_16";
146 case PixelFormat::DEPTH_24:
147 return "PixelFormat::DEPTH_24";
148 case PixelFormat::DEPTH_24_STENCIL_8:
149 return "PixelFormat::DEPTH_24_STENCIL_8";
150 case PixelFormat::DEPTH_32F:
151 return "PixelFormat::DEPTH_24";
152 case PixelFormat::DEPTH_32F_STENCIL_8:
153 return "PixelFormat::DEPTH_24_STENCIL_8";
154 case PixelFormat::HSV_888:
155 return "PixelFormat::HSV_888";
156 case PixelFormat::IMPLEMENTATION_DEFINED:
157 return "PixelFormat::IMPLEMENTATION_DEFINED";
158 case PixelFormat::RAW10:
159 return "PixelFormat::RAW10";
160 case PixelFormat::RAW12:
161 return "PixelFormat::RAW12";
162 case PixelFormat::RAW16:
163 return "PixelFormat::RAW16";
164 case PixelFormat::RAW_OPAQUE:
165 return "PixelFormat::RAW_OPAQUE";
166 case PixelFormat::RGBA_1010102:
167 return "PixelFormat::RGBA_1010102";
168 case PixelFormat::RGBA_8888:
169 return "PixelFormat::RGBA_8888";
170 case PixelFormat::RGBA_FP16:
171 return "PixelFormat::RGBA_FP16";
172 case PixelFormat::RGBX_8888:
173 return "PixelFormat::RGBX_8888";
174 case PixelFormat::RGB_565:
175 return "PixelFormat::RGB_565";
176 case PixelFormat::RGB_888:
177 return "PixelFormat::RGB_888";
178 case PixelFormat::STENCIL_8:
179 return "PixelFormat::STENCIL_8";
180 case PixelFormat::Y16:
181 return "PixelFormat::Y16";
182 case PixelFormat::Y8:
183 return "PixelFormat::Y8";
184 case PixelFormat::YCBCR_420_888:
185 return "PixelFormat::YCBCR_420_888";
186 case PixelFormat::YCBCR_422_I:
187 return "PixelFormat::YCBCR_422_I";
188 case PixelFormat::YCBCR_422_SP:
189 return "PixelFormat::YCBCR_422_SP";
190 case PixelFormat::YCBCR_P010:
191 return "PixelFormat::YCBCR_P010";
192 case PixelFormat::YCRCB_420_SP:
193 return "PixelFormat::YCRCB_420_SP";
194 case PixelFormat::YV12:
195 return "PixelFormat::YV12";
196 }
197 return android::base::StringPrintf("PixelFormat::Unknown(%d)", static_cast<uint32_t>(format));
198 }
199
getUsageString(hidl_bitfield<BufferUsage> bufferUsage)200 std::string getUsageString(hidl_bitfield<BufferUsage> bufferUsage) {
201 using Underlying = typename std::underlying_type<BufferUsage>::type;
202
203 Underlying usage = static_cast<Underlying>(bufferUsage);
204
205 std::vector<std::string> usages;
206 if (usage & BufferUsage::CAMERA_INPUT) {
207 usage &= ~static_cast<Underlying>(BufferUsage::CAMERA_INPUT);
208 usages.push_back("BufferUsage::CAMERA_INPUT");
209 }
210 if (usage & BufferUsage::CAMERA_OUTPUT) {
211 usage &= ~static_cast<Underlying>(BufferUsage::CAMERA_OUTPUT);
212 usages.push_back("BufferUsage::CAMERA_OUTPUT");
213 }
214 if (usage & BufferUsage::COMPOSER_CURSOR) {
215 usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_CURSOR);
216 usages.push_back("BufferUsage::COMPOSER_CURSOR");
217 }
218 if (usage & BufferUsage::COMPOSER_OVERLAY) {
219 usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_OVERLAY);
220 usages.push_back("BufferUsage::COMPOSER_OVERLAY");
221 }
222 if (usage & BufferUsage::CPU_READ_OFTEN) {
223 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_OFTEN);
224 usages.push_back("BufferUsage::CPU_READ_OFTEN");
225 }
226 if (usage & BufferUsage::CPU_READ_NEVER) {
227 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_NEVER);
228 usages.push_back("BufferUsage::CPU_READ_NEVER");
229 }
230 if (usage & BufferUsage::CPU_READ_RARELY) {
231 usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_RARELY);
232 usages.push_back("BufferUsage::CPU_READ_RARELY");
233 }
234 if (usage & BufferUsage::CPU_WRITE_NEVER) {
235 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_NEVER);
236 usages.push_back("BufferUsage::CPU_WRITE_NEVER");
237 }
238 if (usage & BufferUsage::CPU_WRITE_OFTEN) {
239 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_OFTEN);
240 usages.push_back("BufferUsage::CPU_WRITE_OFTEN");
241 }
242 if (usage & BufferUsage::CPU_WRITE_RARELY) {
243 usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_RARELY);
244 usages.push_back("BufferUsage::CPU_WRITE_RARELY");
245 }
246 if (usage & BufferUsage::GPU_RENDER_TARGET) {
247 usage &= ~static_cast<Underlying>(BufferUsage::GPU_RENDER_TARGET);
248 usages.push_back("BufferUsage::GPU_RENDER_TARGET");
249 }
250 if (usage & BufferUsage::GPU_TEXTURE) {
251 usage &= ~static_cast<Underlying>(BufferUsage::GPU_TEXTURE);
252 usages.push_back("BufferUsage::GPU_TEXTURE");
253 }
254 if (usage & BufferUsage::PROTECTED) {
255 usage &= ~static_cast<Underlying>(BufferUsage::PROTECTED);
256 usages.push_back("BufferUsage::PROTECTED");
257 }
258 if (usage & BufferUsage::RENDERSCRIPT) {
259 usage &= ~static_cast<Underlying>(BufferUsage::RENDERSCRIPT);
260 usages.push_back("BufferUsage::RENDERSCRIPT");
261 }
262 if (usage & BufferUsage::VIDEO_DECODER) {
263 usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_DECODER);
264 usages.push_back("BufferUsage::VIDEO_DECODER");
265 }
266 if (usage & BufferUsage::VIDEO_ENCODER) {
267 usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_ENCODER);
268 usages.push_back("BufferUsage::VIDEO_ENCODER");
269 }
270
271 if (usage) {
272 usages.push_back(android::base::StringPrintf("UnknownUsageBits-%" PRIu64, usage));
273 }
274
275 return android::base::Join(usages, '|');
276 }
277
convertToDrmFormat(PixelFormat format,uint32_t * outDrmFormat)278 int convertToDrmFormat(PixelFormat format, uint32_t* outDrmFormat) {
279 switch (format) {
280 case PixelFormat::BGRA_8888:
281 *outDrmFormat = DRM_FORMAT_ARGB8888;
282 return 0;
283 /**
284 * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
285 * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
286 * equal to their size in bytes.
287 */
288 case PixelFormat::BLOB:
289 *outDrmFormat = DRM_FORMAT_R8;
290 return 0;
291 case PixelFormat::DEPTH_16:
292 return -EINVAL;
293 case PixelFormat::DEPTH_24:
294 return -EINVAL;
295 case PixelFormat::DEPTH_24_STENCIL_8:
296 return -EINVAL;
297 case PixelFormat::DEPTH_32F:
298 return -EINVAL;
299 case PixelFormat::DEPTH_32F_STENCIL_8:
300 return -EINVAL;
301 case PixelFormat::HSV_888:
302 return -EINVAL;
303 case PixelFormat::IMPLEMENTATION_DEFINED:
304 *outDrmFormat = DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
305 return 0;
306 case PixelFormat::RAW10:
307 return -EINVAL;
308 case PixelFormat::RAW12:
309 return -EINVAL;
310 case PixelFormat::RAW16:
311 *outDrmFormat = DRM_FORMAT_R16;
312 return 0;
313 /* TODO use blob */
314 case PixelFormat::RAW_OPAQUE:
315 return -EINVAL;
316 case PixelFormat::RGBA_1010102:
317 *outDrmFormat = DRM_FORMAT_ABGR2101010;
318 return 0;
319 case PixelFormat::RGBA_8888:
320 *outDrmFormat = DRM_FORMAT_ABGR8888;
321 return 0;
322 case PixelFormat::RGBA_FP16:
323 *outDrmFormat = DRM_FORMAT_ABGR16161616F;
324 return 0;
325 case PixelFormat::RGBX_8888:
326 *outDrmFormat = DRM_FORMAT_XBGR8888;
327 return 0;
328 case PixelFormat::RGB_565:
329 *outDrmFormat = DRM_FORMAT_RGB565;
330 return 0;
331 case PixelFormat::RGB_888:
332 *outDrmFormat = DRM_FORMAT_RGB888;
333 return 0;
334 case PixelFormat::STENCIL_8:
335 return -EINVAL;
336 case PixelFormat::Y16:
337 *outDrmFormat = DRM_FORMAT_R16;
338 return 0;
339 case PixelFormat::Y8:
340 *outDrmFormat = DRM_FORMAT_R8;
341 return 0;
342 case PixelFormat::YCBCR_420_888:
343 *outDrmFormat = DRM_FORMAT_FLEX_YCbCr_420_888;
344 return 0;
345 case PixelFormat::YCBCR_422_SP:
346 return -EINVAL;
347 case PixelFormat::YCBCR_422_I:
348 return -EINVAL;
349 case PixelFormat::YCBCR_P010:
350 *outDrmFormat = DRM_FORMAT_P010;
351 return 0;
352 case PixelFormat::YCRCB_420_SP:
353 *outDrmFormat = DRM_FORMAT_NV21;
354 return 0;
355 case PixelFormat::YV12:
356 *outDrmFormat = DRM_FORMAT_YVU420_ANDROID;
357 return 0;
358 };
359 return -EINVAL;
360 }
361
convertToBufferUsage(uint64_t grallocUsage,uint64_t * outBufferUsage)362 int convertToBufferUsage(uint64_t grallocUsage, uint64_t* outBufferUsage) {
363 uint64_t bufferUsage = BO_USE_NONE;
364
365 if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
366 static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
367 bufferUsage |= BO_USE_SW_READ_RARELY;
368 }
369 if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
370 static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN)) {
371 bufferUsage |= BO_USE_SW_READ_OFTEN;
372 }
373 if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
374 static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
375 bufferUsage |= BO_USE_SW_WRITE_RARELY;
376 }
377 if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
378 static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN)) {
379 bufferUsage |= BO_USE_SW_WRITE_OFTEN;
380 }
381 if (grallocUsage & BufferUsage::GPU_TEXTURE) {
382 bufferUsage |= BO_USE_TEXTURE;
383 }
384 if (grallocUsage & BufferUsage::GPU_RENDER_TARGET) {
385 bufferUsage |= BO_USE_RENDERING;
386 }
387 if (grallocUsage & BufferUsage::COMPOSER_OVERLAY) {
388 /* HWC wants to use display hardware, but can defer to OpenGL. */
389 bufferUsage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
390 }
391 if (grallocUsage & BufferUsage::PROTECTED) {
392 bufferUsage |= BO_USE_PROTECTED;
393 }
394 if (grallocUsage & BufferUsage::COMPOSER_CURSOR) {
395 bufferUsage |= BO_USE_NONE;
396 }
397 if (grallocUsage & BufferUsage::VIDEO_ENCODER) {
398 /*HACK: See b/30054495 */
399 bufferUsage |= BO_USE_SW_READ_OFTEN;
400 }
401 if (grallocUsage & BufferUsage::CAMERA_OUTPUT) {
402 bufferUsage |= BO_USE_CAMERA_WRITE;
403 }
404 if (grallocUsage & BufferUsage::CAMERA_INPUT) {
405 bufferUsage |= BO_USE_CAMERA_READ;
406 }
407 if (grallocUsage & BufferUsage::RENDERSCRIPT) {
408 bufferUsage |= BO_USE_RENDERSCRIPT;
409 }
410 if (grallocUsage & BufferUsage::VIDEO_DECODER) {
411 bufferUsage |= BO_USE_HW_VIDEO_DECODER;
412 }
413
414 *outBufferUsage = bufferUsage;
415 return 0;
416 }
417
convertToCrosDescriptor(const BufferDescriptorInfo & descriptor,struct cros_gralloc_buffer_descriptor * outCrosDescriptor)418 int convertToCrosDescriptor(const BufferDescriptorInfo& descriptor,
419 struct cros_gralloc_buffer_descriptor* outCrosDescriptor) {
420 outCrosDescriptor->name = descriptor.name;
421 outCrosDescriptor->width = descriptor.width;
422 outCrosDescriptor->height = descriptor.height;
423 outCrosDescriptor->droid_format = static_cast<int32_t>(descriptor.format);
424 outCrosDescriptor->droid_usage = descriptor.usage;
425 outCrosDescriptor->reserved_region_size = descriptor.reservedSize;
426
427 if (convertToDrmFormat(descriptor.format, &outCrosDescriptor->drm_format)) {
428 std::string pixelFormatString = getPixelFormatString(descriptor.format);
429 drv_log("Failed to convert descriptor. Unsupported fomat %s\n", pixelFormatString.c_str());
430 return -1;
431 }
432 if (convertToBufferUsage(descriptor.usage, &outCrosDescriptor->use_flags)) {
433 std::string usageString = getUsageString(descriptor.usage);
434 drv_log("Failed to convert descriptor. Unsupported usage flags %s\n", usageString.c_str());
435 return -1;
436 }
437 return 0;
438 }
439
convertToMapUsage(uint64_t grallocUsage,uint32_t * outMapUsage)440 int convertToMapUsage(uint64_t grallocUsage, uint32_t* outMapUsage) {
441 uint32_t mapUsage = BO_MAP_NONE;
442
443 if (grallocUsage & BufferUsage::CPU_READ_MASK) {
444 mapUsage |= BO_MAP_READ;
445 }
446 if (grallocUsage & BufferUsage::CPU_WRITE_MASK) {
447 mapUsage |= BO_MAP_WRITE;
448 }
449
450 *outMapUsage = mapUsage;
451 return 0;
452 }
453
convertToFenceFd(const hidl_handle & fenceHandle,int * outFenceFd)454 int convertToFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) {
455 if (!outFenceFd) {
456 return -EINVAL;
457 }
458
459 const native_handle_t* nativeHandle = fenceHandle.getNativeHandle();
460 if (nativeHandle && nativeHandle->numFds > 1) {
461 return -EINVAL;
462 }
463
464 *outFenceFd = (nativeHandle && nativeHandle->numFds == 1) ? nativeHandle->data[0] : -1;
465 return 0;
466 }
467
convertToFenceHandle(int fenceFd,hidl_handle * outFenceHandle)468 int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
469 if (!outFenceHandle) {
470 return -EINVAL;
471 }
472 if (fenceFd < 0) {
473 return 0;
474 }
475
476 NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
477 auto fenceHandle = native_handle_init(handleStorage, 1, 0);
478 fenceHandle->data[0] = fenceFd;
479
480 *outFenceHandle = fenceHandle;
481 return 0;
482 }
483
GetPlaneLayoutsMap()484 const std::unordered_map<uint32_t, std::vector<PlaneLayout>>& GetPlaneLayoutsMap() {
485 static const auto* kPlaneLayoutsMap =
486 new std::unordered_map<uint32_t, std::vector<PlaneLayout>>({
487 {DRM_FORMAT_ABGR8888,
488 {{
489 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
490 .offsetInBits = 0,
491 .sizeInBits = 8},
492 {.type = android::gralloc4::PlaneLayoutComponentType_G,
493 .offsetInBits = 8,
494 .sizeInBits = 8},
495 {.type = android::gralloc4::PlaneLayoutComponentType_B,
496 .offsetInBits = 16,
497 .sizeInBits = 8},
498 {.type = android::gralloc4::PlaneLayoutComponentType_A,
499 .offsetInBits = 24,
500 .sizeInBits = 8}},
501 .sampleIncrementInBits = 32,
502 .horizontalSubsampling = 1,
503 .verticalSubsampling = 1,
504 }}},
505
506 {DRM_FORMAT_ABGR2101010,
507 {{
508 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
509 .offsetInBits = 0,
510 .sizeInBits = 10},
511 {.type = android::gralloc4::PlaneLayoutComponentType_G,
512 .offsetInBits = 10,
513 .sizeInBits = 10},
514 {.type = android::gralloc4::PlaneLayoutComponentType_B,
515 .offsetInBits = 20,
516 .sizeInBits = 10},
517 {.type = android::gralloc4::PlaneLayoutComponentType_A,
518 .offsetInBits = 30,
519 .sizeInBits = 2}},
520 .sampleIncrementInBits = 32,
521 .horizontalSubsampling = 1,
522 .verticalSubsampling = 1,
523 }}},
524
525 {DRM_FORMAT_ABGR16161616F,
526 {{
527 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
528 .offsetInBits = 0,
529 .sizeInBits = 16},
530 {.type = android::gralloc4::PlaneLayoutComponentType_G,
531 .offsetInBits = 16,
532 .sizeInBits = 16},
533 {.type = android::gralloc4::PlaneLayoutComponentType_B,
534 .offsetInBits = 32,
535 .sizeInBits = 16},
536 {.type = android::gralloc4::PlaneLayoutComponentType_A,
537 .offsetInBits = 48,
538 .sizeInBits = 16}},
539 .sampleIncrementInBits = 64,
540 .horizontalSubsampling = 1,
541 .verticalSubsampling = 1,
542 }}},
543
544 {DRM_FORMAT_ARGB8888,
545 {{
546 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
547 .offsetInBits = 0,
548 .sizeInBits = 8},
549 {.type = android::gralloc4::PlaneLayoutComponentType_G,
550 .offsetInBits = 8,
551 .sizeInBits = 8},
552 {.type = android::gralloc4::PlaneLayoutComponentType_R,
553 .offsetInBits = 16,
554 .sizeInBits = 8},
555 {.type = android::gralloc4::PlaneLayoutComponentType_A,
556 .offsetInBits = 24,
557 .sizeInBits = 8}},
558 .sampleIncrementInBits = 32,
559 .horizontalSubsampling = 1,
560 .verticalSubsampling = 1,
561 }}},
562
563 {DRM_FORMAT_NV12,
564 {{
565 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
566 .offsetInBits = 0,
567 .sizeInBits = 8}},
568 .sampleIncrementInBits = 8,
569 .horizontalSubsampling = 1,
570 .verticalSubsampling = 1,
571 },
572 {
573 .components =
574 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
575 .offsetInBits = 0,
576 .sizeInBits = 8},
577 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
578 .offsetInBits = 8,
579 .sizeInBits = 8}},
580 .sampleIncrementInBits = 16,
581 .horizontalSubsampling = 2,
582 .verticalSubsampling = 2,
583 }}},
584
585 {DRM_FORMAT_NV21,
586 {{
587 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
588 .offsetInBits = 0,
589 .sizeInBits = 8}},
590 .sampleIncrementInBits = 8,
591 .horizontalSubsampling = 1,
592 .verticalSubsampling = 1,
593 },
594 {
595 .components =
596 {{.type = android::gralloc4::PlaneLayoutComponentType_CR,
597 .offsetInBits = 0,
598 .sizeInBits = 8},
599 {.type = android::gralloc4::PlaneLayoutComponentType_CB,
600 .offsetInBits = 8,
601 .sizeInBits = 8}},
602 .sampleIncrementInBits = 16,
603 .horizontalSubsampling = 2,
604 .verticalSubsampling = 2,
605 }}},
606
607 {DRM_FORMAT_P010,
608 {{
609 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
610 .offsetInBits = 6,
611 .sizeInBits = 10}},
612 .sampleIncrementInBits = 16,
613 .horizontalSubsampling = 1,
614 .verticalSubsampling = 1,
615 },
616 {
617 .components =
618 {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
619 .offsetInBits = 6,
620 .sizeInBits = 10},
621 {.type = android::gralloc4::PlaneLayoutComponentType_CR,
622 .offsetInBits = 22,
623 .sizeInBits = 10}},
624 .sampleIncrementInBits = 32,
625 .horizontalSubsampling = 2,
626 .verticalSubsampling = 2,
627 }}},
628
629 {DRM_FORMAT_R8,
630 {{
631 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
632 .offsetInBits = 0,
633 .sizeInBits = 8}},
634 .sampleIncrementInBits = 8,
635 .horizontalSubsampling = 1,
636 .verticalSubsampling = 1,
637 }}},
638
639 {DRM_FORMAT_R16,
640 {{
641 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
642 .offsetInBits = 0,
643 .sizeInBits = 16}},
644 .sampleIncrementInBits = 16,
645 .horizontalSubsampling = 1,
646 .verticalSubsampling = 1,
647 }}},
648
649 {DRM_FORMAT_RGB565,
650 {{
651 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
652 .offsetInBits = 0,
653 .sizeInBits = 5},
654 {.type = android::gralloc4::PlaneLayoutComponentType_G,
655 .offsetInBits = 5,
656 .sizeInBits = 6},
657 {.type = android::gralloc4::PlaneLayoutComponentType_B,
658 .offsetInBits = 11,
659 .sizeInBits = 5}},
660 .sampleIncrementInBits = 16,
661 .horizontalSubsampling = 1,
662 .verticalSubsampling = 1,
663 }}},
664
665 {DRM_FORMAT_RGB888,
666 {{
667 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
668 .offsetInBits = 0,
669 .sizeInBits = 8},
670 {.type = android::gralloc4::PlaneLayoutComponentType_G,
671 .offsetInBits = 8,
672 .sizeInBits = 8},
673 {.type = android::gralloc4::PlaneLayoutComponentType_B,
674 .offsetInBits = 16,
675 .sizeInBits = 8}},
676 .sampleIncrementInBits = 24,
677 .horizontalSubsampling = 1,
678 .verticalSubsampling = 1,
679 }}},
680
681 {DRM_FORMAT_XBGR8888,
682 {{
683 .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
684 .offsetInBits = 0,
685 .sizeInBits = 8},
686 {.type = android::gralloc4::PlaneLayoutComponentType_G,
687 .offsetInBits = 8,
688 .sizeInBits = 8},
689 {.type = android::gralloc4::PlaneLayoutComponentType_R,
690 .offsetInBits = 16,
691 .sizeInBits = 8}},
692 .sampleIncrementInBits = 32,
693 .horizontalSubsampling = 1,
694 .verticalSubsampling = 1,
695 }}},
696
697 {DRM_FORMAT_YVU420,
698 {
699 {
700 .components = {{.type = android::gralloc4::
701 PlaneLayoutComponentType_Y,
702 .offsetInBits = 0,
703 .sizeInBits = 8}},
704 .sampleIncrementInBits = 8,
705 .horizontalSubsampling = 1,
706 .verticalSubsampling = 1,
707 },
708 {
709 .components = {{.type = android::gralloc4::
710 PlaneLayoutComponentType_CB,
711 .offsetInBits = 0,
712 .sizeInBits = 8}},
713 .sampleIncrementInBits = 8,
714 .horizontalSubsampling = 2,
715 .verticalSubsampling = 2,
716 },
717 {
718 .components = {{.type = android::gralloc4::
719 PlaneLayoutComponentType_CR,
720 .offsetInBits = 0,
721 .sizeInBits = 8}},
722 .sampleIncrementInBits = 8,
723 .horizontalSubsampling = 2,
724 .verticalSubsampling = 2,
725 },
726 }},
727
728 {DRM_FORMAT_YVU420_ANDROID,
729 {
730 {
731 .components = {{.type = android::gralloc4::
732 PlaneLayoutComponentType_Y,
733 .offsetInBits = 0,
734 .sizeInBits = 8}},
735 .sampleIncrementInBits = 8,
736 .horizontalSubsampling = 1,
737 .verticalSubsampling = 1,
738 },
739 {
740 .components = {{.type = android::gralloc4::
741 PlaneLayoutComponentType_CR,
742 .offsetInBits = 0,
743 .sizeInBits = 8}},
744 .sampleIncrementInBits = 8,
745 .horizontalSubsampling = 2,
746 .verticalSubsampling = 2,
747 },
748 {
749 .components = {{.type = android::gralloc4::
750 PlaneLayoutComponentType_CB,
751 .offsetInBits = 0,
752 .sizeInBits = 8}},
753 .sampleIncrementInBits = 8,
754 .horizontalSubsampling = 2,
755 .verticalSubsampling = 2,
756 },
757 }},
758 });
759 return *kPlaneLayoutsMap;
760 }
761
getPlaneLayouts(uint32_t drmFormat,std::vector<PlaneLayout> * outPlaneLayouts)762 int getPlaneLayouts(uint32_t drmFormat, std::vector<PlaneLayout>* outPlaneLayouts) {
763 const auto& planeLayoutsMap = GetPlaneLayoutsMap();
764 const auto it = planeLayoutsMap.find(drmFormat);
765 if (it == planeLayoutsMap.end()) {
766 drv_log("Unknown plane layout for format %d\n", drmFormat);
767 return -1;
768 }
769
770 *outPlaneLayouts = it->second;
771 return 0;
772 }