1 /*
2 * Copyright 2016 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_helpers.h"
8
9 #include <hardware/gralloc.h>
10 #include <sync/sync.h>
11
12 /* Define to match AIDL BufferUsage::VIDEO_DECODER. */
13 #define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
14
15 /* Define to match AIDL BufferUsage::SENSOR_DIRECT_DATA. */
16 #define BUFFER_USAGE_SENSOR_DIRECT_DATA (1 << 23)
17
18 /* Define to match AIDL BufferUsage::GPU_DATA_BUFFER. */
19 #define BUFFER_USAGE_GPU_DATA_BUFFER (1 << 24)
20
21 /* Define to match AIDL PixelFormat::R_8. */
22 #define HAL_PIXEL_FORMAT_R8 0x38
23
cros_gralloc_convert_format(int format)24 uint32_t cros_gralloc_convert_format(int format)
25 {
26 /*
27 * Conversion from HAL to fourcc-based DRV formats based on
28 * platform_android.c in mesa.
29 */
30
31 switch (format) {
32 case HAL_PIXEL_FORMAT_RGBA_8888:
33 return DRM_FORMAT_ABGR8888;
34 case HAL_PIXEL_FORMAT_RGBX_8888:
35 return DRM_FORMAT_XBGR8888;
36 case HAL_PIXEL_FORMAT_RGB_888:
37 return DRM_FORMAT_BGR888;
38 /*
39 * Confusingly, HAL_PIXEL_FORMAT_RGB_565 is defined as:
40 *
41 * "16-bit packed format that has 5-bit R, 6-bit G, and 5-bit B components, in that
42 * order, from the most-sigfinicant bits to the least-significant bits."
43 *
44 * so the order of the components is intentionally not flipped between the pixel
45 * format and the DRM format.
46 */
47 case HAL_PIXEL_FORMAT_RGB_565:
48 return DRM_FORMAT_RGB565;
49 case HAL_PIXEL_FORMAT_BGRA_8888:
50 return DRM_FORMAT_ARGB8888;
51 case HAL_PIXEL_FORMAT_RAW16:
52 return DRM_FORMAT_R16;
53 /*
54 * Choose DRM_FORMAT_R8 because <system/graphics.h> requires the buffers
55 * with a format HAL_PIXEL_FORMAT_BLOB have a height of 1, and width
56 * equal to their size in bytes.
57 */
58 case HAL_PIXEL_FORMAT_BLOB:
59 case HAL_PIXEL_FORMAT_R8:
60 return DRM_FORMAT_R8;
61 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
62 return DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED;
63 case HAL_PIXEL_FORMAT_YCbCr_420_888:
64 return DRM_FORMAT_FLEX_YCbCr_420_888;
65 case HAL_PIXEL_FORMAT_Y8:
66 return DRM_FORMAT_R8;
67 case HAL_PIXEL_FORMAT_Y16:
68 return DRM_FORMAT_R16;
69 case HAL_PIXEL_FORMAT_YV12:
70 return DRM_FORMAT_YVU420_ANDROID;
71 #if ANDROID_API_LEVEL >= 29
72 case HAL_PIXEL_FORMAT_RGBA_FP16:
73 return DRM_FORMAT_ABGR16161616F;
74 case HAL_PIXEL_FORMAT_RGBA_1010102:
75 return DRM_FORMAT_ABGR2101010;
76 #endif
77 #if ANDROID_API_LEVEL >= 30
78 case HAL_PIXEL_FORMAT_YCBCR_P010:
79 return DRM_FORMAT_P010;
80 #endif
81 }
82
83 return DRM_FORMAT_NONE;
84 }
85
handle_usage(uint64_t * gralloc_usage,uint64_t gralloc_mask,uint64_t * bo_use_flags,uint64_t bo_mask)86 static inline void handle_usage(uint64_t *gralloc_usage, uint64_t gralloc_mask,
87 uint64_t *bo_use_flags, uint64_t bo_mask)
88 {
89 if ((*gralloc_usage) & gralloc_mask) {
90 (*gralloc_usage) &= ~gralloc_mask;
91 (*bo_use_flags) |= bo_mask;
92 }
93 }
94
cros_gralloc_convert_usage(uint64_t usage)95 uint64_t cros_gralloc_convert_usage(uint64_t usage)
96 {
97 uint64_t use_flags = BO_USE_NONE;
98
99 /*
100 * GRALLOC_USAGE_SW_READ_OFTEN contains GRALLOC_USAGE_SW_READ_RARELY, thus OFTEN must be
101 * handled first. The same applies to GRALLOC_USAGE_SW_WRITE_OFTEN.
102 */
103 handle_usage(&usage, GRALLOC_USAGE_SW_READ_OFTEN, &use_flags, BO_USE_SW_READ_OFTEN);
104 handle_usage(&usage, GRALLOC_USAGE_SW_READ_RARELY, &use_flags, BO_USE_SW_READ_RARELY);
105 handle_usage(&usage, GRALLOC_USAGE_SW_WRITE_OFTEN, &use_flags, BO_USE_SW_WRITE_OFTEN);
106 handle_usage(&usage, GRALLOC_USAGE_SW_WRITE_RARELY, &use_flags, BO_USE_SW_WRITE_RARELY);
107 handle_usage(&usage, GRALLOC_USAGE_HW_TEXTURE, &use_flags, BO_USE_TEXTURE);
108 handle_usage(&usage, GRALLOC_USAGE_HW_RENDER, &use_flags, BO_USE_RENDERING);
109 handle_usage(&usage, GRALLOC_USAGE_HW_2D, &use_flags, BO_USE_RENDERING);
110 /* HWC wants to use display hardware, but can defer to OpenGL. */
111 handle_usage(&usage, GRALLOC_USAGE_HW_COMPOSER, &use_flags,
112 BO_USE_SCANOUT | BO_USE_TEXTURE);
113 handle_usage(&usage, GRALLOC_USAGE_HW_FB, &use_flags, BO_USE_NONE);
114 /*
115 * This flag potentially covers external display for the normal drivers (i915/rockchip) and
116 * usb monitors (evdi/udl). It's complicated so ignore it.
117 */
118 handle_usage(&usage, GRALLOC_USAGE_EXTERNAL_DISP, &use_flags, BO_USE_NONE);
119 /* Map PROTECTED to linear until real HW protection is available on Android. */
120 handle_usage(&usage, GRALLOC_USAGE_PROTECTED, &use_flags, BO_USE_LINEAR);
121 handle_usage(&usage, GRALLOC_USAGE_CURSOR, &use_flags, BO_USE_NONE);
122 /* HACK: See b/30054495 for BO_USE_SW_READ_OFTEN. */
123 handle_usage(&usage, GRALLOC_USAGE_HW_VIDEO_ENCODER, &use_flags,
124 BO_USE_HW_VIDEO_ENCODER | BO_USE_SW_READ_OFTEN);
125 handle_usage(&usage, GRALLOC_USAGE_HW_CAMERA_WRITE, &use_flags, BO_USE_CAMERA_WRITE);
126 handle_usage(&usage, GRALLOC_USAGE_HW_CAMERA_READ, &use_flags, BO_USE_CAMERA_READ);
127 handle_usage(&usage, GRALLOC_USAGE_RENDERSCRIPT, &use_flags, BO_USE_RENDERSCRIPT);
128 handle_usage(&usage, BUFFER_USAGE_VIDEO_DECODER, &use_flags, BO_USE_HW_VIDEO_DECODER);
129 handle_usage(&usage, BUFFER_USAGE_SENSOR_DIRECT_DATA, &use_flags,
130 BO_USE_SENSOR_DIRECT_DATA);
131 handle_usage(&usage, BUFFER_USAGE_GPU_DATA_BUFFER, &use_flags, BO_USE_GPU_DATA_BUFFER);
132 handle_usage(&usage, BUFFER_USAGE_FRONT_RENDERING_MASK, &use_flags, BO_USE_FRONT_RENDERING);
133
134 if (usage) {
135 ALOGE("Unhandled gralloc usage: %llx", (unsigned long long)usage);
136 return BO_USE_NONE;
137 }
138
139 return use_flags;
140 }
141
cros_gralloc_convert_map_usage(uint64_t usage)142 uint32_t cros_gralloc_convert_map_usage(uint64_t usage)
143 {
144 uint32_t map_flags = BO_MAP_NONE;
145
146 if (usage & GRALLOC_USAGE_SW_READ_MASK)
147 map_flags |= BO_MAP_READ;
148 if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
149 map_flags |= BO_MAP_WRITE;
150
151 return map_flags;
152 }
153
cros_gralloc_convert_handle(buffer_handle_t handle)154 cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
155 {
156 auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
157 if (!hnd || hnd->magic != cros_gralloc_magic)
158 return nullptr;
159
160 return hnd;
161 }
162
cros_gralloc_sync_wait(int32_t fence,bool close_fence)163 int32_t cros_gralloc_sync_wait(int32_t fence, bool close_fence)
164 {
165 if (fence < 0)
166 return 0;
167
168 /*
169 * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
170 * documentation states the caller waits indefinitely on the fence if timeout < 0.
171 */
172 int err = sync_wait(fence, 1000);
173 if (err < 0) {
174 ALOGE("Timed out on sync wait, err = %s", strerror(errno));
175 err = sync_wait(fence, -1);
176 if (err < 0) {
177 ALOGE("sync wait error = %s", strerror(errno));
178 return -errno;
179 }
180 }
181
182 if (close_fence) {
183 err = close(fence);
184 if (err) {
185 ALOGE("Unable to close fence fd, err = %s", strerror(errno));
186 return -errno;
187 }
188 }
189
190 return 0;
191 }
192
get_drm_format_string(uint32_t drm_format)193 std::string get_drm_format_string(uint32_t drm_format)
194 {
195 char *sequence = (char *)&drm_format;
196 std::string s(sequence, 4);
197 return "DRM_FOURCC_" + s;
198 }
199