1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Drm.h"
18 
19 #include <drm_fourcc.h>
20 #include <log/log.h>
21 #include <system/graphics.h>
22 
23 namespace aidl::android::hardware::graphics::composer3::impl {
24 
GetDrmFormatString(uint32_t drm_format)25 const char* GetDrmFormatString(uint32_t drm_format) {
26     switch (drm_format) {
27         case DRM_FORMAT_ABGR1555:
28             return "DRM_FORMAT_ABGR1555";
29         case DRM_FORMAT_ABGR2101010:
30             return "DRM_FORMAT_ABGR2101010";
31         case DRM_FORMAT_ABGR4444:
32             return "DRM_FORMAT_ABGR4444";
33         case DRM_FORMAT_ABGR8888:
34             return "DRM_FORMAT_ABGR8888";
35         case DRM_FORMAT_ARGB1555:
36             return "DRM_FORMAT_ARGB1555";
37         case DRM_FORMAT_ARGB2101010:
38             return "DRM_FORMAT_ARGB2101010";
39         case DRM_FORMAT_ARGB4444:
40             return "DRM_FORMAT_ARGB4444";
41         case DRM_FORMAT_ARGB8888:
42             return "DRM_FORMAT_ARGB8888";
43         case DRM_FORMAT_AYUV:
44             return "DRM_FORMAT_AYUV";
45         case DRM_FORMAT_BGR233:
46             return "DRM_FORMAT_BGR233";
47         case DRM_FORMAT_BGR565:
48             return "DRM_FORMAT_BGR565";
49         case DRM_FORMAT_BGR888:
50             return "DRM_FORMAT_BGR888";
51         case DRM_FORMAT_BGRA1010102:
52             return "DRM_FORMAT_BGRA1010102";
53         case DRM_FORMAT_BGRA4444:
54             return "DRM_FORMAT_BGRA4444";
55         case DRM_FORMAT_BGRA5551:
56             return "DRM_FORMAT_BGRA5551";
57         case DRM_FORMAT_BGRA8888:
58             return "DRM_FORMAT_BGRA8888";
59         case DRM_FORMAT_BGRX1010102:
60             return "DRM_FORMAT_BGRX1010102";
61         case DRM_FORMAT_BGRX4444:
62             return "DRM_FORMAT_BGRX4444";
63         case DRM_FORMAT_BGRX5551:
64             return "DRM_FORMAT_BGRX5551";
65         case DRM_FORMAT_BGRX8888:
66             return "DRM_FORMAT_BGRX8888";
67         case DRM_FORMAT_C8:
68             return "DRM_FORMAT_C8";
69         case DRM_FORMAT_GR88:
70             return "DRM_FORMAT_GR88";
71         case DRM_FORMAT_NV12:
72             return "DRM_FORMAT_NV12";
73         case DRM_FORMAT_NV21:
74             return "DRM_FORMAT_NV21";
75         case DRM_FORMAT_R8:
76             return "DRM_FORMAT_R8";
77         case DRM_FORMAT_RG88:
78             return "DRM_FORMAT_RG88";
79         case DRM_FORMAT_RGB332:
80             return "DRM_FORMAT_RGB332";
81         case DRM_FORMAT_RGB565:
82             return "DRM_FORMAT_RGB565";
83         case DRM_FORMAT_RGB888:
84             return "DRM_FORMAT_RGB888";
85         case DRM_FORMAT_RGBA1010102:
86             return "DRM_FORMAT_RGBA1010102";
87         case DRM_FORMAT_RGBA4444:
88             return "DRM_FORMAT_RGBA4444";
89         case DRM_FORMAT_RGBA5551:
90             return "DRM_FORMAT_RGBA5551";
91         case DRM_FORMAT_RGBA8888:
92             return "DRM_FORMAT_RGBA8888";
93         case DRM_FORMAT_RGBX1010102:
94             return "DRM_FORMAT_RGBX1010102";
95         case DRM_FORMAT_RGBX4444:
96             return "DRM_FORMAT_RGBX4444";
97         case DRM_FORMAT_RGBX5551:
98             return "DRM_FORMAT_RGBX5551";
99         case DRM_FORMAT_RGBX8888:
100             return "DRM_FORMAT_RGBX8888";
101         case DRM_FORMAT_UYVY:
102             return "DRM_FORMAT_UYVY";
103         case DRM_FORMAT_VYUY:
104             return "DRM_FORMAT_VYUY";
105         case DRM_FORMAT_XBGR1555:
106             return "DRM_FORMAT_XBGR1555";
107         case DRM_FORMAT_XBGR2101010:
108             return "DRM_FORMAT_XBGR2101010";
109         case DRM_FORMAT_XBGR4444:
110             return "DRM_FORMAT_XBGR4444";
111         case DRM_FORMAT_XBGR8888:
112             return "DRM_FORMAT_XBGR8888";
113         case DRM_FORMAT_XRGB1555:
114             return "DRM_FORMAT_XRGB1555";
115         case DRM_FORMAT_XRGB2101010:
116             return "DRM_FORMAT_XRGB2101010";
117         case DRM_FORMAT_XRGB4444:
118             return "DRM_FORMAT_XRGB4444";
119         case DRM_FORMAT_XRGB8888:
120             return "DRM_FORMAT_XRGB8888";
121         case DRM_FORMAT_YUYV:
122             return "DRM_FORMAT_YUYV";
123         case DRM_FORMAT_YVU420:
124             return "DRM_FORMAT_YVU420";
125         case DRM_FORMAT_YVYU:
126             return "DRM_FORMAT_YVYU";
127     }
128     return "Unknown";
129 }
130 
GetDrmFormatBytesPerPixel(uint32_t drm_format)131 uint32_t GetDrmFormatBytesPerPixel(uint32_t drm_format) {
132     switch (drm_format) {
133         case DRM_FORMAT_ABGR8888:
134         case DRM_FORMAT_ARGB8888:
135         case DRM_FORMAT_XBGR8888:
136             return 4;
137         case DRM_FORMAT_BGR888:
138             return 3;
139         case DRM_FORMAT_RGB565:
140         case DRM_FORMAT_YVU420:
141 #ifdef GRALLOC_MODULE_API_VERSION_0_2
142         case DRM_FORMAT_FLEX_YCbCr_420_888:
143 #endif
144             return 2;
145         case DRM_FORMAT_R8:
146             return 1;
147     }
148     ALOGE("%s: format size unknown %d(%s)", __FUNCTION__, drm_format,
149           GetDrmFormatString(drm_format));
150     return 8;
151 }
152 
GetDrmFormatFromHalFormat(int hal_format)153 uint32_t GetDrmFormatFromHalFormat(int hal_format) {
154     switch (hal_format) {
155         case HAL_PIXEL_FORMAT_RGBA_FP16:
156             return DRM_FORMAT_ABGR16161616F;
157         case HAL_PIXEL_FORMAT_RGBA_8888:
158             return DRM_FORMAT_ABGR8888;
159         case HAL_PIXEL_FORMAT_RGBX_8888:
160             return DRM_FORMAT_XBGR8888;
161         case HAL_PIXEL_FORMAT_BGRA_8888:
162             return DRM_FORMAT_ARGB8888;
163         case HAL_PIXEL_FORMAT_RGB_888:
164             return DRM_FORMAT_BGR888;
165         case HAL_PIXEL_FORMAT_RGB_565:
166             return DRM_FORMAT_BGR565;
167         case HAL_PIXEL_FORMAT_YV12:
168             return DRM_FORMAT_YVU420;
169         case HAL_PIXEL_FORMAT_YCbCr_420_888:
170             return DRM_FORMAT_YVU420;
171         case HAL_PIXEL_FORMAT_BLOB:
172             return DRM_FORMAT_R8;
173         default:
174             break;
175     }
176     ALOGE("%s unhandled hal format: %d", __FUNCTION__, hal_format);
177     return 0;
178 }
179 
180 }  // namespace aidl::android::hardware::graphics::composer3::impl