OSDN Git Service

minigbm: cros_gralloc: map protected flag to linear
[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
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());
325         return -1;
326     }
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());
330         return -1;
331     }
332     return 0;
333 }
334
335 int convertToMapUsage(uint64_t grallocUsage, uint32_t* outMapUsage) {
336     uint32_t mapUsage = BO_MAP_NONE;
337
338     if (grallocUsage & BufferUsage::CPU_READ_MASK) {
339         mapUsage |= BO_MAP_READ;
340     }
341     if (grallocUsage & BufferUsage::CPU_WRITE_MASK) {
342         mapUsage |= BO_MAP_WRITE;
343     }
344
345     *outMapUsage = mapUsage;
346     return 0;
347 }
348
349 int convertToFenceFd(const hidl_handle& fenceHandle, int* outFenceFd) {
350     if (!outFenceFd) {
351         return -EINVAL;
352     }
353
354     const native_handle_t* nativeHandle = fenceHandle.getNativeHandle();
355     if (nativeHandle && nativeHandle->numFds > 1) {
356         return -EINVAL;
357     }
358
359     *outFenceFd = (nativeHandle && nativeHandle->numFds == 1) ? nativeHandle->data[0] : -1;
360     return 0;
361 }
362
363 int convertToFenceHandle(int fenceFd, hidl_handle* outFenceHandle) {
364     if (!outFenceHandle) {
365         return -EINVAL;
366     }
367     if (fenceFd < 0) {
368         return 0;
369     }
370
371     NATIVE_HANDLE_DECLARE_STORAGE(handleStorage, 1, 0);
372     auto fenceHandle = native_handle_init(handleStorage, 1, 0);
373     fenceHandle->data[0] = fenceFd;
374
375     *outFenceHandle = fenceHandle;
376     return 0;
377 }
378
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,
383                      {{
384                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
385                                              .offsetInBits = 0,
386                                              .sizeInBits = 8},
387                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
388                                              .offsetInBits = 8,
389                                              .sizeInBits = 8},
390                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
391                                              .offsetInBits = 16,
392                                              .sizeInBits = 8},
393                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
394                                              .offsetInBits = 24,
395                                              .sizeInBits = 8}},
396                              .sampleIncrementInBits = 32,
397                              .horizontalSubsampling = 1,
398                              .verticalSubsampling = 1,
399                      }}},
400
401                     {DRM_FORMAT_ABGR2101010,
402                      {{
403                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
404                                              .offsetInBits = 0,
405                                              .sizeInBits = 10},
406                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
407                                              .offsetInBits = 10,
408                                              .sizeInBits = 10},
409                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
410                                              .offsetInBits = 20,
411                                              .sizeInBits = 10},
412                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
413                                              .offsetInBits = 30,
414                                              .sizeInBits = 2}},
415                              .sampleIncrementInBits = 32,
416                              .horizontalSubsampling = 1,
417                              .verticalSubsampling = 1,
418                      }}},
419
420                     {DRM_FORMAT_ABGR16161616F,
421                      {{
422                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
423                                              .offsetInBits = 0,
424                                              .sizeInBits = 16},
425                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
426                                              .offsetInBits = 16,
427                                              .sizeInBits = 16},
428                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
429                                              .offsetInBits = 32,
430                                              .sizeInBits = 16},
431                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
432                                              .offsetInBits = 48,
433                                              .sizeInBits = 16}},
434                              .sampleIncrementInBits = 64,
435                              .horizontalSubsampling = 1,
436                              .verticalSubsampling = 1,
437                      }}},
438
439                     {DRM_FORMAT_ARGB8888,
440                      {{
441                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
442                                              .offsetInBits = 0,
443                                              .sizeInBits = 8},
444                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
445                                              .offsetInBits = 8,
446                                              .sizeInBits = 8},
447                                             {.type = android::gralloc4::PlaneLayoutComponentType_R,
448                                              .offsetInBits = 16,
449                                              .sizeInBits = 8},
450                                             {.type = android::gralloc4::PlaneLayoutComponentType_A,
451                                              .offsetInBits = 24,
452                                              .sizeInBits = 8}},
453                              .sampleIncrementInBits = 32,
454                              .horizontalSubsampling = 1,
455                              .verticalSubsampling = 1,
456                      }}},
457
458                     {DRM_FORMAT_NV12,
459                      {{
460                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
461                                               .offsetInBits = 0,
462                                               .sizeInBits = 8}},
463                               .sampleIncrementInBits = 8,
464                               .horizontalSubsampling = 1,
465                               .verticalSubsampling = 1,
466                       },
467                       {
468                               .components =
469                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
470                                         .offsetInBits = 0,
471                                         .sizeInBits = 8},
472                                        {.type = android::gralloc4::PlaneLayoutComponentType_CR,
473                                         .offsetInBits = 8,
474                                         .sizeInBits = 8}},
475                               .sampleIncrementInBits = 16,
476                               .horizontalSubsampling = 2,
477                               .verticalSubsampling = 2,
478                       }}},
479
480                     {DRM_FORMAT_NV21,
481                      {{
482                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
483                                               .offsetInBits = 0,
484                                               .sizeInBits = 8}},
485                               .sampleIncrementInBits = 8,
486                               .horizontalSubsampling = 1,
487                               .verticalSubsampling = 1,
488                       },
489                       {
490                               .components =
491                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CR,
492                                         .offsetInBits = 0,
493                                         .sizeInBits = 8},
494                                        {.type = android::gralloc4::PlaneLayoutComponentType_CB,
495                                         .offsetInBits = 8,
496                                         .sizeInBits = 8}},
497                               .sampleIncrementInBits = 16,
498                               .horizontalSubsampling = 2,
499                               .verticalSubsampling = 2,
500                       }}},
501
502                     {DRM_FORMAT_P010,
503                      {{
504                               .components = {{.type = android::gralloc4::PlaneLayoutComponentType_Y,
505                                               .offsetInBits = 6,
506                                               .sizeInBits = 10}},
507                               .sampleIncrementInBits = 16,
508                               .horizontalSubsampling = 1,
509                               .verticalSubsampling = 1,
510                       },
511                       {
512                               .components =
513                                       {{.type = android::gralloc4::PlaneLayoutComponentType_CB,
514                                         .offsetInBits = 6,
515                                         .sizeInBits = 10},
516                                        {.type = android::gralloc4::PlaneLayoutComponentType_CR,
517                                         .offsetInBits = 22,
518                                         .sizeInBits = 10}},
519                               .sampleIncrementInBits = 32,
520                               .horizontalSubsampling = 2,
521                               .verticalSubsampling = 2,
522                       }}},
523
524                     {DRM_FORMAT_R8,
525                      {{
526                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
527                                              .offsetInBits = 0,
528                                              .sizeInBits = 8}},
529                              .sampleIncrementInBits = 8,
530                              .horizontalSubsampling = 1,
531                              .verticalSubsampling = 1,
532                      }}},
533
534                     {DRM_FORMAT_R16,
535                      {{
536                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
537                                              .offsetInBits = 0,
538                                              .sizeInBits = 16}},
539                              .sampleIncrementInBits = 16,
540                              .horizontalSubsampling = 1,
541                              .verticalSubsampling = 1,
542                      }}},
543
544                     {DRM_FORMAT_RGB565,
545                      {{
546                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
547                                              .offsetInBits = 0,
548                                              .sizeInBits = 5},
549                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
550                                              .offsetInBits = 5,
551                                              .sizeInBits = 6},
552                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
553                                              .offsetInBits = 11,
554                                              .sizeInBits = 5}},
555                              .sampleIncrementInBits = 16,
556                              .horizontalSubsampling = 1,
557                              .verticalSubsampling = 1,
558                      }}},
559
560                     {DRM_FORMAT_RGB888,
561                      {{
562                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_R,
563                                              .offsetInBits = 0,
564                                              .sizeInBits = 8},
565                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
566                                              .offsetInBits = 8,
567                                              .sizeInBits = 8},
568                                             {.type = android::gralloc4::PlaneLayoutComponentType_B,
569                                              .offsetInBits = 16,
570                                              .sizeInBits = 8}},
571                              .sampleIncrementInBits = 24,
572                              .horizontalSubsampling = 1,
573                              .verticalSubsampling = 1,
574                      }}},
575
576                     {DRM_FORMAT_XBGR8888,
577                      {{
578                              .components = {{.type = android::gralloc4::PlaneLayoutComponentType_B,
579                                              .offsetInBits = 0,
580                                              .sizeInBits = 8},
581                                             {.type = android::gralloc4::PlaneLayoutComponentType_G,
582                                              .offsetInBits = 8,
583                                              .sizeInBits = 8},
584                                             {.type = android::gralloc4::PlaneLayoutComponentType_R,
585                                              .offsetInBits = 16,
586                                              .sizeInBits = 8}},
587                              .sampleIncrementInBits = 32,
588                              .horizontalSubsampling = 1,
589                              .verticalSubsampling = 1,
590                      }}},
591
592                     {DRM_FORMAT_YVU420,
593                      {
594                              {
595                                      .components = {{.type = android::gralloc4::
596                                                              PlaneLayoutComponentType_Y,
597                                                      .offsetInBits = 0,
598                                                      .sizeInBits = 8}},
599                                      .sampleIncrementInBits = 8,
600                                      .horizontalSubsampling = 1,
601                                      .verticalSubsampling = 1,
602                              },
603                              {
604                                      .components = {{.type = android::gralloc4::
605                                                              PlaneLayoutComponentType_CB,
606                                                      .offsetInBits = 0,
607                                                      .sizeInBits = 8}},
608                                      .sampleIncrementInBits = 8,
609                                      .horizontalSubsampling = 2,
610                                      .verticalSubsampling = 2,
611                              },
612                              {
613                                      .components = {{.type = android::gralloc4::
614                                                              PlaneLayoutComponentType_CR,
615                                                      .offsetInBits = 0,
616                                                      .sizeInBits = 8}},
617                                      .sampleIncrementInBits = 8,
618                                      .horizontalSubsampling = 2,
619                                      .verticalSubsampling = 2,
620                              },
621                      }},
622
623                     {DRM_FORMAT_YVU420_ANDROID,
624                      {
625                              {
626                                      .components = {{.type = android::gralloc4::
627                                                              PlaneLayoutComponentType_Y,
628                                                      .offsetInBits = 0,
629                                                      .sizeInBits = 8}},
630                                      .sampleIncrementInBits = 8,
631                                      .horizontalSubsampling = 1,
632                                      .verticalSubsampling = 1,
633                              },
634                              {
635                                      .components = {{.type = android::gralloc4::
636                                                              PlaneLayoutComponentType_CR,
637                                                      .offsetInBits = 0,
638                                                      .sizeInBits = 8}},
639                                      .sampleIncrementInBits = 8,
640                                      .horizontalSubsampling = 2,
641                                      .verticalSubsampling = 2,
642                              },
643                              {
644                                      .components = {{.type = android::gralloc4::
645                                                              PlaneLayoutComponentType_CB,
646                                                      .offsetInBits = 0,
647                                                      .sizeInBits = 8}},
648                                      .sampleIncrementInBits = 8,
649                                      .horizontalSubsampling = 2,
650                                      .verticalSubsampling = 2,
651                              },
652                      }},
653             });
654     return *kPlaneLayoutsMap;
655 }
656
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);
662         return -1;
663     }
664
665     *outPlaneLayouts = it->second;
666     return 0;
667 }