OSDN Git Service

1296b0771e1b5f0ea44aaa797a580ecd6b1f902c
[android-x86/external-minigbm.git] / cros_gralloc / gralloc4 / CrosGralloc4Utils.cc
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
32 std::string getPixelFormatString(PixelFormat format) {
33     switch (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";
76         case PixelFormat::Y8:
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";
90     }
91     return android::base::StringPrintf("PixelFormat::Unknown(%d)", static_cast<uint32_t>(format));
92 }
93
94 std::string getUsageString(hidl_bitfield<BufferUsage> bufferUsage) {
95     using Underlying = typename std::underlying_type<BufferUsage>::type;
96
97     Underlying usage = static_cast<Underlying>(bufferUsage);
98
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");
103     }
104     if (usage & BufferUsage::CAMERA_OUTPUT) {
105         usage &= ~static_cast<Underlying>(BufferUsage::CAMERA_OUTPUT);
106         usages.push_back("BufferUsage::CAMERA_OUTPUT");
107     }
108     if (usage & BufferUsage::COMPOSER_CURSOR) {
109         usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_CURSOR);
110         usages.push_back("BufferUsage::COMPOSER_CURSOR");
111     }
112     if (usage & BufferUsage::COMPOSER_OVERLAY) {
113         usage &= ~static_cast<Underlying>(BufferUsage::COMPOSER_OVERLAY);
114         usages.push_back("BufferUsage::COMPOSER_OVERLAY");
115     }
116     if (usage & BufferUsage::CPU_READ_OFTEN) {
117         usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_OFTEN);
118         usages.push_back("BufferUsage::CPU_READ_OFTEN");
119     }
120     if (usage & BufferUsage::CPU_READ_NEVER) {
121         usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_NEVER);
122         usages.push_back("BufferUsage::CPU_READ_NEVER");
123     }
124     if (usage & BufferUsage::CPU_READ_RARELY) {
125         usage &= ~static_cast<Underlying>(BufferUsage::CPU_READ_RARELY);
126         usages.push_back("BufferUsage::CPU_READ_RARELY");
127     }
128     if (usage & BufferUsage::CPU_WRITE_NEVER) {
129         usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_NEVER);
130         usages.push_back("BufferUsage::CPU_WRITE_NEVER");
131     }
132     if (usage & BufferUsage::CPU_WRITE_OFTEN) {
133         usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_OFTEN);
134         usages.push_back("BufferUsage::CPU_WRITE_OFTEN");
135     }
136     if (usage & BufferUsage::CPU_WRITE_RARELY) {
137         usage &= ~static_cast<Underlying>(BufferUsage::CPU_WRITE_RARELY);
138         usages.push_back("BufferUsage::CPU_WRITE_RARELY");
139     }
140     if (usage & BufferUsage::GPU_RENDER_TARGET) {
141         usage &= ~static_cast<Underlying>(BufferUsage::GPU_RENDER_TARGET);
142         usages.push_back("BufferUsage::GPU_RENDER_TARGET");
143     }
144     if (usage & BufferUsage::GPU_TEXTURE) {
145         usage &= ~static_cast<Underlying>(BufferUsage::GPU_TEXTURE);
146         usages.push_back("BufferUsage::GPU_TEXTURE");
147     }
148     if (usage & BufferUsage::PROTECTED) {
149         usage &= ~static_cast<Underlying>(BufferUsage::PROTECTED);
150         usages.push_back("BufferUsage::PROTECTED");
151     }
152     if (usage & BufferUsage::RENDERSCRIPT) {
153         usage &= ~static_cast<Underlying>(BufferUsage::RENDERSCRIPT);
154         usages.push_back("BufferUsage::RENDERSCRIPT");
155     }
156     if (usage & BufferUsage::VIDEO_DECODER) {
157         usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_DECODER);
158         usages.push_back("BufferUsage::VIDEO_DECODER");
159     }
160     if (usage & BufferUsage::VIDEO_ENCODER) {
161         usage &= ~static_cast<Underlying>(BufferUsage::VIDEO_ENCODER);
162         usages.push_back("BufferUsage::VIDEO_ENCODER");
163     }
164
165     if (usage) {
166         usages.push_back(android::base::StringPrintf("UnknownUsageBits-%" PRIu64, usage));
167     }
168
169     return android::base::Join(usages, '|');
170 }
171
172 int convertToDrmFormat(PixelFormat format, uint32_t* outDrmFormat) {
173     switch (format) {
174         case PixelFormat::BGRA_8888:
175             *outDrmFormat = DRM_FORMAT_ARGB8888;
176             return 0;
177         /**
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.
181          */
182         case PixelFormat::BLOB:
183             *outDrmFormat = DRM_FORMAT_R8;
184             return 0;
185         case PixelFormat::DEPTH_16:
186             return -EINVAL;
187         case PixelFormat::DEPTH_24:
188             return -EINVAL;
189         case PixelFormat::DEPTH_24_STENCIL_8:
190             return -EINVAL;
191         case PixelFormat::DEPTH_32F:
192             return -EINVAL;
193         case PixelFormat::DEPTH_32F_STENCIL_8:
194             return -EINVAL;
195         case PixelFormat::HSV_888:
196             return -EINVAL;
197         case PixelFormat::IMPLEMENTATION_DEFINED:
198             *outDrmFormat = DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
199             return 0;
200         case PixelFormat::RAW10:
201             return -EINVAL;
202         case PixelFormat::RAW12:
203             return -EINVAL;
204         case PixelFormat::RAW16:
205             *outDrmFormat = DRM_FORMAT_R16;
206             return 0;
207         /* TODO use blob */
208         case PixelFormat::RAW_OPAQUE:
209             return -EINVAL;
210         case PixelFormat::RGBA_1010102:
211             *outDrmFormat = DRM_FORMAT_ABGR2101010;
212             return 0;
213         case PixelFormat::RGBA_8888:
214             *outDrmFormat = DRM_FORMAT_ABGR8888;
215             return 0;
216         case PixelFormat::RGBA_FP16:
217             *outDrmFormat = DRM_FORMAT_ABGR16161616F;
218             return 0;
219         case PixelFormat::RGBX_8888:
220             *outDrmFormat = DRM_FORMAT_XBGR8888;
221             return 0;
222         case PixelFormat::RGB_565:
223             *outDrmFormat = DRM_FORMAT_RGB565;
224             return 0;
225         case PixelFormat::RGB_888:
226             *outDrmFormat = DRM_FORMAT_RGB888;
227             return 0;
228         case PixelFormat::STENCIL_8:
229             return -EINVAL;
230         case PixelFormat::Y16:
231             *outDrmFormat = DRM_FORMAT_R16;
232             return 0;
233         case PixelFormat::Y8:
234             *outDrmFormat = DRM_FORMAT_R8;
235             return 0;
236         case PixelFormat::YCBCR_420_888:
237             *outDrmFormat = DRM_FORMAT_FLEX_YCbCr_420_888;
238             return 0;
239         case PixelFormat::YCBCR_422_SP:
240             return -EINVAL;
241         case PixelFormat::YCBCR_422_I:
242             return -EINVAL;
243         case PixelFormat::YCBCR_P010:
244             *outDrmFormat = DRM_FORMAT_P010;
245             return 0;
246         case PixelFormat::YCRCB_420_SP:
247             *outDrmFormat = DRM_FORMAT_NV21;
248             return 0;
249         case PixelFormat::YV12:
250             *outDrmFormat = DRM_FORMAT_YVU420_ANDROID;
251             return 0;
252     };
253     return -EINVAL;
254 }
255
256 int convertToBufferUsage(uint64_t grallocUsage, uint64_t* outBufferUsage) {
257     uint64_t bufferUsage = BO_USE_NONE;
258
259     if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
260         static_cast<uint64_t>(BufferUsage::CPU_READ_RARELY)) {
261         bufferUsage |= BO_USE_SW_READ_RARELY;
262     }
263     if ((grallocUsage & BufferUsage::CPU_READ_MASK) ==
264         static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN)) {
265         bufferUsage |= BO_USE_SW_READ_OFTEN;
266     }
267     if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
268         static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY)) {
269         bufferUsage |= BO_USE_SW_WRITE_RARELY;
270     }
271     if ((grallocUsage & BufferUsage::CPU_WRITE_MASK) ==
272         static_cast<uint64_t>(BufferUsage::CPU_WRITE_OFTEN)) {
273         bufferUsage |= BO_USE_SW_WRITE_OFTEN;
274     }
275     if (grallocUsage & BufferUsage::GPU_TEXTURE) {
276         bufferUsage |= BO_USE_TEXTURE;
277     }
278     if (grallocUsage & BufferUsage::GPU_RENDER_TARGET) {
279         bufferUsage |= BO_USE_RENDERING;
280     }
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;
284     }
285     /* Map this flag to linear until real HW protection is available on Android. */
286     if (grallocUsage & BufferUsage::PROTECTED) {
287         bufferUsage |= BO_USE_LINEAR;
288     }
289     if (grallocUsage & BufferUsage::COMPOSER_CURSOR) {
290         bufferUsage |= BO_USE_NONE;
291     }
292     if (grallocUsage & BufferUsage::VIDEO_ENCODER) {
293         /*HACK: See b/30054495 */
294         bufferUsage |= BO_USE_SW_READ_OFTEN;
295     }
296     if (grallocUsage & BufferUsage::CAMERA_OUTPUT) {
297         bufferUsage |= BO_USE_CAMERA_WRITE;
298     }
299     if (grallocUsage & BufferUsage::CAMERA_INPUT) {
300         bufferUsage |= BO_USE_CAMERA_READ;
301     }
302     if (grallocUsage & BufferUsage::RENDERSCRIPT) {
303         bufferUsage |= BO_USE_RENDERSCRIPT;
304     }
305     if (grallocUsage & BufferUsage::VIDEO_DECODER) {
306         bufferUsage |= BO_USE_HW_VIDEO_DECODER;
307     }
308
309     *outBufferUsage = bufferUsage;
310     return 0;
311 }
312
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);
324         return -1;
325     }
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());
329         return -1;
330     }
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());
334         return -1;
335     }
336     return 0;
337 }
338
339 int convertToMapUsage(uint64_t grallocUsage, uint32_t* outMapUsage) {
340     uint32_t mapUsage = BO_MAP_NONE;
341
342     if (grallocUsage & BufferUsage::CPU_READ_MASK) {
343         mapUsage |= BO_MAP_READ;
344     }
345     if (grallocUsage & BufferUsage::CPU_WRITE_MASK) {
346         mapUsage |= BO_MAP_WRITE;
347     }
348
349     *outMapUsage = mapUsage;
350     return 0;
351 }
352
353 int convertToFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) {
354     if (!outFenceFd) {
355         return -EINVAL;
356     }
357
358     const native_handle_t* nativeHandle = fenceHandle.getNativeHandle();
359     if (nativeHandle && nativeHandle->numFds > 1) {
360         return -EINVAL;
361     }
362
363     *outFenceFd = (nativeHandle && nativeHandle->numFds == 1) ? nativeHandle->data[0] : -1;
364     return 0;
365 }
366
367 int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
368     if (!outFenceHandle) {
369         return -EINVAL;
370     }
371     if (fenceFd < 0) {
372         return 0;
373     }
374
375     NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
376     auto fenceHandle = native_handle_init(handleStorage, 1, 0);
377     fenceHandle->data[0] = fenceFd;
378
379     *outFenceHandle = fenceHandle;
380     return 0;
381 }
382
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,
387                      {{
388                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
389                                              .offsetInBits = 0,
390                                              .sizeInBits = 8},
391                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
392                                              .offsetInBits = 8,
393                                              .sizeInBits = 8},
394                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
395                                              .offsetInBits = 16,
396                                              .sizeInBits = 8},
397                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
398                                              .offsetInBits = 24,
399                                              .sizeInBits = 8}},
400                              .sampleIncrementInBits = 32,
401                              .horizontalSubsampling = 1,
402                              .verticalSubsampling = 1,
403                      }}},
404
405                     {DRM_FORMAT_ABGR2101010,
406                      {{
407                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
408                                              .offsetInBits = 0,
409                                              .sizeInBits = 10},
410                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
411                                              .offsetInBits = 10,
412                                              .sizeInBits = 10},
413                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
414                                              .offsetInBits = 20,
415                                              .sizeInBits = 10},
416                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
417                                              .offsetInBits = 30,
418                                              .sizeInBits = 2}},
419                              .sampleIncrementInBits = 32,
420                              .horizontalSubsampling = 1,
421                              .verticalSubsampling = 1,
422                      }}},
423
424                     {DRM_FORMAT_ABGR16161616F,
425                      {{
426                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
427                                              .offsetInBits = 0,
428                                              .sizeInBits = 16},
429                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
430                                              .offsetInBits = 16,
431                                              .sizeInBits = 16},
432                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
433                                              .offsetInBits = 32,
434                                              .sizeInBits = 16},
435                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
436                                              .offsetInBits = 48,
437                                              .sizeInBits = 16}},
438                              .sampleIncrementInBits = 64,
439                              .horizontalSubsampling = 1,
440                              .verticalSubsampling = 1,
441                      }}},
442
443                     {DRM_FORMAT_ARGB8888,
444                      {{
445                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
446                                              .offsetInBits = 0,
447                                              .sizeInBits = 8},
448                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
449                                              .offsetInBits = 8,
450                                              .sizeInBits = 8},
451                                             {.type = android::gralloc4::PlaneLayoutComponentType_R,
452                                              .offsetInBits = 16,
453                                              .sizeInBits = 8},
454                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
455                                              .offsetInBits = 24,
456                                              .sizeInBits = 8}},
457                              .sampleIncrementInBits = 32,
458                              .horizontalSubsampling = 1,
459                              .verticalSubsampling = 1,
460                      }}},
461
462                     {DRM_FORMAT_ARGB4444,
463                      {{
464                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
465                                              .offsetInBits = 0,
466                                              .sizeInBits = 4},
467                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
468                                              .offsetInBits = 4,
469                                              .sizeInBits = 4},
470                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
471                                              .offsetInBits = 8,
472                                              .sizeInBits = 4},
473                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
474                                              .offsetInBits = 12,
475                                              .sizeInBits = 4}},
476                              .sampleIncrementInBits = 16,
477                              .horizontalSubsampling = 1,
478                              .verticalSubsampling = 1,
479                      }}},
480
481                     {DRM_FORMAT_NV12,
482                      {{
483                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
484                                               .offsetInBits = 0,
485                                               .sizeInBits = 8}},
486                               .sampleIncrementInBits = 8,
487                               .horizontalSubsampling = 1,
488                               .verticalSubsampling = 1,
489                       },
490                       {
491                               .components =
492                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
493                                         .offsetInBits = 0,
494                                         .sizeInBits = 8},
495                                        {.type = android::gralloc4::PlaneLayoutComponentType_CR,
496                                         .offsetInBits = 8,
497                                         .sizeInBits = 8}},
498                               .sampleIncrementInBits = 16,
499                               .horizontalSubsampling = 2,
500                               .verticalSubsampling = 2,
501                       }}},
502
503                     {DRM_FORMAT_NV21,
504                      {{
505                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
506                                               .offsetInBits = 0,
507                                               .sizeInBits = 8}},
508                               .sampleIncrementInBits = 8,
509                               .horizontalSubsampling = 1,
510                               .verticalSubsampling = 1,
511                       },
512                       {
513                               .components =
514                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CR,
515                                         .offsetInBits = 0,
516                                         .sizeInBits = 8},
517                                        {.type = android::gralloc4::PlaneLayoutComponentType_CB,
518                                         .offsetInBits = 8,
519                                         .sizeInBits = 8}},
520                               .sampleIncrementInBits = 16,
521                               .horizontalSubsampling = 2,
522                               .verticalSubsampling = 2,
523                       }}},
524
525                     {DRM_FORMAT_P010,
526                      {{
527                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
528                                               .offsetInBits = 6,
529                                               .sizeInBits = 10}},
530                               .sampleIncrementInBits = 16,
531                               .horizontalSubsampling = 1,
532                               .verticalSubsampling = 1,
533                       },
534                       {
535                               .components =
536                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
537                                         .offsetInBits = 6,
538                                         .sizeInBits = 10},
539                                        {.type = android::gralloc4::PlaneLayoutComponentType_CR,
540                                         .offsetInBits = 22,
541                                         .sizeInBits = 10}},
542                               .sampleIncrementInBits = 32,
543                               .horizontalSubsampling = 2,
544                               .verticalSubsampling = 2,
545                       }}},
546
547                     {DRM_FORMAT_R8,
548                      {{
549                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
550                                              .offsetInBits = 0,
551                                              .sizeInBits = 8}},
552                              .sampleIncrementInBits = 8,
553                              .horizontalSubsampling = 1,
554                              .verticalSubsampling = 1,
555                      }}},
556
557                     {DRM_FORMAT_R16,
558                      {{
559                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
560                                              .offsetInBits = 0,
561                                              .sizeInBits = 16}},
562                              .sampleIncrementInBits = 16,
563                              .horizontalSubsampling = 1,
564                              .verticalSubsampling = 1,
565                      }}},
566
567                     {DRM_FORMAT_RGB565,
568                      {{
569                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
570                                              .offsetInBits = 0,
571                                              .sizeInBits = 5},
572                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
573                                              .offsetInBits = 5,
574                                              .sizeInBits = 6},
575                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
576                                              .offsetInBits = 11,
577                                              .sizeInBits = 5}},
578                              .sampleIncrementInBits = 16,
579                              .horizontalSubsampling = 1,
580                              .verticalSubsampling = 1,
581                      }}},
582
583                     {DRM_FORMAT_RGB888,
584                      {{
585                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
586                                              .offsetInBits = 0,
587                                              .sizeInBits = 8},
588                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
589                                              .offsetInBits = 8,
590                                              .sizeInBits = 8},
591                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
592                                              .offsetInBits = 16,
593                                              .sizeInBits = 8}},
594                              .sampleIncrementInBits = 24,
595                              .horizontalSubsampling = 1,
596                              .verticalSubsampling = 1,
597                      }}},
598
599                     {DRM_FORMAT_XBGR8888,
600                      {{
601                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
602                                              .offsetInBits = 0,
603                                              .sizeInBits = 8},
604                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
605                                              .offsetInBits = 8,
606                                              .sizeInBits = 8},
607                                             {.type = android::gralloc4::PlaneLayoutComponentType_R,
608                                              .offsetInBits = 16,
609                                              .sizeInBits = 8}},
610                              .sampleIncrementInBits = 32,
611                              .horizontalSubsampling = 1,
612                              .verticalSubsampling = 1,
613                      }}},
614
615                     {DRM_FORMAT_YVU420,
616                      {
617                              {
618                                      .components = {{.type = android::gralloc4::
619                                                              PlaneLayoutComponentType_Y,
620                                                      .offsetInBits = 0,
621                                                      .sizeInBits = 8}},
622                                      .sampleIncrementInBits = 8,
623                                      .horizontalSubsampling = 1,
624                                      .verticalSubsampling = 1,
625                              },
626                              {
627                                      .components = {{.type = android::gralloc4::
628                                                              PlaneLayoutComponentType_CB,
629                                                      .offsetInBits = 0,
630                                                      .sizeInBits = 8}},
631                                      .sampleIncrementInBits = 8,
632                                      .horizontalSubsampling = 2,
633                                      .verticalSubsampling = 2,
634                              },
635                              {
636                                      .components = {{.type = android::gralloc4::
637                                                              PlaneLayoutComponentType_CR,
638                                                      .offsetInBits = 0,
639                                                      .sizeInBits = 8}},
640                                      .sampleIncrementInBits = 8,
641                                      .horizontalSubsampling = 2,
642                                      .verticalSubsampling = 2,
643                              },
644                      }},
645
646                     {DRM_FORMAT_YVU420_ANDROID,
647                      {
648                              {
649                                      .components = {{.type = android::gralloc4::
650                                                              PlaneLayoutComponentType_Y,
651                                                      .offsetInBits = 0,
652                                                      .sizeInBits = 8}},
653                                      .sampleIncrementInBits = 8,
654                                      .horizontalSubsampling = 1,
655                                      .verticalSubsampling = 1,
656                              },
657                              {
658                                      .components = {{.type = android::gralloc4::
659                                                              PlaneLayoutComponentType_CR,
660                                                      .offsetInBits = 0,
661                                                      .sizeInBits = 8}},
662                                      .sampleIncrementInBits = 8,
663                                      .horizontalSubsampling = 2,
664                                      .verticalSubsampling = 2,
665                              },
666                              {
667                                      .components = {{.type = android::gralloc4::
668                                                              PlaneLayoutComponentType_CB,
669                                                      .offsetInBits = 0,
670                                                      .sizeInBits = 8}},
671                                      .sampleIncrementInBits = 8,
672                                      .horizontalSubsampling = 2,
673                                      .verticalSubsampling = 2,
674                              },
675                      }},
676             });
677     return *kPlaneLayoutsMap;
678 }
679
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);
685         return -1;
686     }
687
688     *outPlaneLayouts = it->second;
689     return 0;
690 }