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