xref: /aosp_15_r20/external/minigbm/cros_gralloc/cros_gralloc_helpers.cc (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
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 	case HAL_PIXEL_FORMAT_DEPTH_16:
82 		return DRM_FORMAT_DEPTH16;
83 	case HAL_PIXEL_FORMAT_DEPTH_24:
84 		return DRM_FORMAT_DEPTH24;
85 	case HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
86 		return DRM_FORMAT_DEPTH24_STENCIL8;
87 	case HAL_PIXEL_FORMAT_DEPTH_32F:
88 		return DRM_FORMAT_DEPTH32;
89 	case HAL_PIXEL_FORMAT_DEPTH_32F_STENCIL_8:
90 		return DRM_FORMAT_DEPTH32_STENCIL8;
91 	}
92 
93 	return DRM_FORMAT_NONE;
94 }
95 
handle_usage(uint64_t * gralloc_usage,uint64_t gralloc_mask,uint64_t * bo_use_flags,uint64_t bo_mask)96 static inline void handle_usage(uint64_t *gralloc_usage, uint64_t gralloc_mask,
97 				uint64_t *bo_use_flags, uint64_t bo_mask)
98 {
99 	if (((*gralloc_usage) & gralloc_mask) == gralloc_mask) {
100 		(*gralloc_usage) &= ~gralloc_mask;
101 		(*bo_use_flags) |= bo_mask;
102 	}
103 }
104 
cros_gralloc_convert_usage(uint64_t usage)105 uint64_t cros_gralloc_convert_usage(uint64_t usage)
106 {
107 	uint64_t use_flags = BO_USE_NONE;
108 
109 	/*
110 	 * GRALLOC_USAGE_SW_READ_OFTEN contains GRALLOC_USAGE_SW_READ_RARELY, thus OFTEN must be
111 	 * handled first. The same applies to GRALLOC_USAGE_SW_WRITE_OFTEN.
112 	 */
113 	handle_usage(&usage, GRALLOC_USAGE_SW_READ_OFTEN, &use_flags, BO_USE_SW_READ_OFTEN);
114 	handle_usage(&usage, GRALLOC_USAGE_SW_READ_RARELY, &use_flags, BO_USE_SW_READ_RARELY);
115 	handle_usage(&usage, GRALLOC_USAGE_SW_WRITE_OFTEN, &use_flags, BO_USE_SW_WRITE_OFTEN);
116 	handle_usage(&usage, GRALLOC_USAGE_SW_WRITE_RARELY, &use_flags, BO_USE_SW_WRITE_RARELY);
117 	handle_usage(&usage, GRALLOC_USAGE_HW_TEXTURE, &use_flags, BO_USE_TEXTURE);
118 	handle_usage(&usage, GRALLOC_USAGE_HW_RENDER, &use_flags, BO_USE_RENDERING);
119 	handle_usage(&usage, GRALLOC_USAGE_HW_2D, &use_flags, BO_USE_RENDERING);
120 	/* HWC wants to use display hardware, but can defer to OpenGL. */
121 	handle_usage(&usage, GRALLOC_USAGE_HW_COMPOSER, &use_flags,
122 		     BO_USE_SCANOUT | BO_USE_TEXTURE);
123 	handle_usage(&usage, GRALLOC_USAGE_HW_FB, &use_flags, BO_USE_NONE);
124 	/*
125 	 * This flag potentially covers external display for the normal drivers (i915/rockchip) and
126 	 * usb monitors (evdi/udl). It's complicated so ignore it.
127 	 */
128 	handle_usage(&usage, GRALLOC_USAGE_EXTERNAL_DISP, &use_flags, BO_USE_NONE);
129 	/* Map PROTECTED to linear until real HW protection is available on Android. */
130 	handle_usage(&usage, GRALLOC_USAGE_PROTECTED, &use_flags, BO_USE_LINEAR);
131 	handle_usage(&usage, GRALLOC_USAGE_CURSOR, &use_flags, BO_USE_NONE);
132 	/* HACK: See b/30054495 for BO_USE_SW_READ_OFTEN. */
133 	handle_usage(&usage, GRALLOC_USAGE_HW_VIDEO_ENCODER, &use_flags,
134 		     BO_USE_HW_VIDEO_ENCODER | BO_USE_SW_READ_OFTEN);
135 	handle_usage(&usage, GRALLOC_USAGE_HW_CAMERA_WRITE, &use_flags, BO_USE_CAMERA_WRITE);
136 	handle_usage(&usage, GRALLOC_USAGE_HW_CAMERA_READ, &use_flags, BO_USE_CAMERA_READ);
137 	handle_usage(&usage, GRALLOC_USAGE_RENDERSCRIPT, &use_flags, BO_USE_RENDERSCRIPT);
138 	handle_usage(&usage, BUFFER_USAGE_VIDEO_DECODER, &use_flags, BO_USE_HW_VIDEO_DECODER);
139 	handle_usage(&usage, BUFFER_USAGE_SENSOR_DIRECT_DATA, &use_flags,
140 		     BO_USE_SENSOR_DIRECT_DATA);
141 	handle_usage(&usage, BUFFER_USAGE_GPU_DATA_BUFFER, &use_flags, BO_USE_GPU_DATA_BUFFER);
142 	handle_usage(&usage, BUFFER_USAGE_FRONT_RENDERING, &use_flags, BO_USE_FRONT_RENDERING);
143 	handle_usage(&usage, BUFFER_USAGE_FRONT_RENDERING_PRIVATE, &use_flags,
144 		     BO_USE_FRONT_RENDERING);
145 
146 	if (usage) {
147 		ALOGE("Unhandled gralloc usage: %llx", (unsigned long long)usage);
148 		return BO_USE_NONE;
149 	}
150 
151 	return use_flags;
152 }
153 
cros_gralloc_convert_map_usage(uint64_t usage)154 uint32_t cros_gralloc_convert_map_usage(uint64_t usage)
155 {
156 	uint32_t map_flags = BO_MAP_NONE;
157 
158 	if (usage & GRALLOC_USAGE_SW_READ_MASK)
159 		map_flags |= BO_MAP_READ;
160 	if (usage & GRALLOC_USAGE_SW_WRITE_MASK)
161 		map_flags |= BO_MAP_WRITE;
162 
163 	return map_flags;
164 }
165 
cros_gralloc_convert_handle(buffer_handle_t handle)166 cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle)
167 {
168 	if (sizeof(native_handle_t) + (sizeof(int) * (handle->numFds + handle->numInts)) !=
169 	    sizeof(struct cros_gralloc_handle))
170 		return nullptr;
171 
172 	auto hnd = reinterpret_cast<cros_gralloc_handle_t>(handle);
173 	if (!hnd || hnd->magic != cros_gralloc_magic)
174 		return nullptr;
175 
176 	// if hnd->reserved_region_size == 0, handle->numFds is hnd->num_planes
177 	// if hnd->reserved_region_size > 0, handle->numFds is hnd->num_planes + 1
178 	if ((uint32_t)handle->numFds != hnd->num_planes + (hnd->reserved_region_size > 0))
179 		return nullptr;
180 
181 	return hnd;
182 }
183 
cros_gralloc_sync_wait(int32_t fence,bool close_fence)184 int32_t cros_gralloc_sync_wait(int32_t fence, bool close_fence)
185 {
186 	if (fence < 0)
187 		return 0;
188 
189 	/*
190 	 * Wait initially for 1000 ms, and then wait indefinitely. The SYNC_IOC_WAIT
191 	 * documentation states the caller waits indefinitely on the fence if timeout < 0.
192 	 */
193 	int err = sync_wait(fence, 1000);
194 	if (err < 0) {
195 		ALOGE("Timed out on sync wait, err = %s", strerror(errno));
196 		err = sync_wait(fence, -1);
197 		if (err < 0) {
198 			ALOGE("sync wait error = %s", strerror(errno));
199 			return -errno;
200 		}
201 	}
202 
203 	if (close_fence) {
204 		err = close(fence);
205 		if (err) {
206 			ALOGE("Unable to close fence fd, err = %s", strerror(errno));
207 			return -errno;
208 		}
209 	}
210 
211 	return 0;
212 }
213 
get_drm_format_string(uint32_t drm_format)214 std::string get_drm_format_string(uint32_t drm_format)
215 {
216 	char *sequence = (char *)&drm_format;
217 	std::string s(sequence, 4);
218 	return "DRM_FOURCC_" + s;
219 }
220