xref: /aosp_15_r20/external/minigbm/cros_gralloc/gralloc0/gralloc0.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 "../../util.h"
8 #include "../cros_gralloc_driver.h"
9 
10 #include <cassert>
11 #include <cutils/native_handle.h>
12 #include <hardware/gralloc.h>
13 #include <memory.h>
14 
15 struct gralloc0_module {
16 	gralloc_module_t base;
17 	std::unique_ptr<alloc_device_t> alloc;
18 	std::shared_ptr<cros_gralloc_driver> driver;
19 	bool initialized;
20 	std::mutex initialization_mutex;
21 };
22 
23 struct cros_gralloc0_buffer_info {
24 	uint32_t drm_fourcc;
25 	int num_fds;
26 	int fds[4];
27 	uint64_t modifier;
28 	uint32_t offset[4];
29 	uint32_t stride[4];
30 };
31 
32 /* This enumeration must match the one in <gralloc_drm.h>.
33  * The functions supported by this gralloc's temporary private API are listed
34  * below. Use of these functions is highly discouraged and should only be
35  * reserved for cases where no alternative to get same information (such as
36  * querying ANativeWindow) exists.
37  */
38 // clang-format off
39 enum {
40 	GRALLOC_DRM_GET_STRIDE,
41 	GRALLOC_DRM_GET_FORMAT,
42 	GRALLOC_DRM_GET_DIMENSIONS,
43 	GRALLOC_DRM_GET_BACKING_STORE,
44 	GRALLOC_DRM_GET_BUFFER_INFO,
45 	GRALLOC_DRM_GET_USAGE,
46 };
47 
48 /* This enumeration corresponds to the GRALLOC_DRM_GET_USAGE query op, which
49  * defines a set of bit flags used by the client to query vendor usage bits.
50  *
51  * Here is the common flow:
52  * 1) EGL/Vulkan calls GRALLOC_DRM_GET_USAGE to append one or multiple vendor
53  *    usage bits to the existing usage and sets onto the ANativeWindow.
54  * 2) Some implicit GL draw cmd or the explicit vkCreateSwapchainKHR kicks off
55  *    the next dequeueBuffer on the ANativeWindow with the combined usage.
56  * 3) dequeueBuffer then asks gralloc hal for an allocation/re-allocation, and
57  *    calls into the below `gralloc0_alloc(...)` api.
58  */
59 enum {
60 	GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT = 0x00000001,
61 };
62 // clang-format on
63 
gralloc0_droid_yuv_format(int droid_format)64 static int gralloc0_droid_yuv_format(int droid_format)
65 {
66 	return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
67 		droid_format == HAL_PIXEL_FORMAT_YV12);
68 }
69 
gralloc0_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * out_handle,int * out_stride)70 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
71 			  buffer_handle_t *out_handle, int *out_stride)
72 {
73 	int32_t ret;
74 	native_handle_t *handle;
75 	struct cros_gralloc_buffer_descriptor descriptor;
76 	auto mod = (struct gralloc0_module const *)dev->common.module;
77 
78 	descriptor.width = w;
79 	descriptor.height = h;
80 	descriptor.droid_format = format;
81 	descriptor.droid_usage = usage;
82 	descriptor.drm_format = cros_gralloc_convert_format(format);
83 	descriptor.use_flags = cros_gralloc_convert_usage(usage);
84 	descriptor.enable_metadata_fd = false;
85 
86 	if (!mod->driver->is_supported(&descriptor)) {
87 		ALOGE("Unsupported combination -- HAL format: %u, HAL usage: %u, "
88 		      "drv_format: %4.4s, use_flags: %llu",
89 		      format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
90 		      static_cast<unsigned long long>(descriptor.use_flags));
91 		return -EINVAL;
92 	}
93 
94 	ret = mod->driver->allocate(&descriptor, &handle);
95 	if (ret)
96 		return ret;
97 
98 	auto hnd = cros_gralloc_convert_handle(handle);
99 	*out_handle = handle;
100 	*out_stride = hnd->pixel_stride;
101 
102 	return 0;
103 }
104 
gralloc0_free(alloc_device_t * dev,buffer_handle_t handle)105 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
106 {
107 	int32_t ret;
108 	auto mod = (struct gralloc0_module const *)dev->common.module;
109 
110 	if (!handle) {
111 		ALOGE("Invalid buffer handle.");
112 		return -EINVAL;
113 	}
114 
115 	ret = mod->driver->release(handle);
116 	if (ret)
117 		return ret;
118 
119 	auto hnd = const_cast<native_handle_t *>(handle);
120 	native_handle_close(hnd);
121 	native_handle_delete(hnd);
122 
123 	return 0;
124 }
125 
gralloc0_close(struct hw_device_t * dev)126 static int gralloc0_close(struct hw_device_t *dev)
127 {
128 	/* Memory is freed by managed pointers on process close. */
129 	return 0;
130 }
131 
gralloc0_init(struct gralloc0_module * mod,bool initialize_alloc)132 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
133 {
134 	std::lock_guard<std::mutex> lock(mod->initialization_mutex);
135 
136 	if (mod->initialized)
137 		return 0;
138 
139 	mod->driver = cros_gralloc_driver::get_instance();
140 	if (!mod->driver)
141 		return -ENODEV;
142 
143 	if (initialize_alloc) {
144 		mod->alloc = std::make_unique<alloc_device_t>();
145 		mod->alloc->alloc = gralloc0_alloc;
146 		mod->alloc->free = gralloc0_free;
147 		mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
148 		mod->alloc->common.version = 0;
149 		mod->alloc->common.module = (hw_module_t *)mod;
150 		mod->alloc->common.close = gralloc0_close;
151 	}
152 
153 	mod->initialized = true;
154 	return 0;
155 }
156 
gralloc0_open(const struct hw_module_t * mod,const char * name,struct hw_device_t ** dev)157 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
158 {
159 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
160 	auto module = const_cast<struct gralloc0_module *>(const_module);
161 
162 	if (module->initialized) {
163 		*dev = &module->alloc->common;
164 		return 0;
165 	}
166 
167 	if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
168 		ALOGE("Incorrect device name - %s.", name);
169 		return -EINVAL;
170 	}
171 
172 	if (gralloc0_init(module, true))
173 		return -ENODEV;
174 
175 	*dev = &module->alloc->common;
176 	return 0;
177 }
178 
gralloc0_register_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)179 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
180 {
181 	if (!handle) {
182 		ALOGE("Invalid buffer handle.");
183 		return -EINVAL;
184 	}
185 
186 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
187 	auto mod = const_cast<struct gralloc0_module *>(const_module);
188 
189 	if (!mod->initialized) {
190 		if (gralloc0_init(mod, false))
191 			return -ENODEV;
192 	}
193 
194 	return mod->driver->retain(handle);
195 }
196 
gralloc0_unregister_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)197 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
198 {
199 	if (!handle) {
200 		ALOGE("Invalid buffer handle.");
201 		return -EINVAL;
202 	}
203 
204 	auto mod = (struct gralloc0_module const *)module;
205 	return mod->driver->release(handle);
206 }
207 
gralloc0_lock(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr)208 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
209 			 int l, int t, int w, int h, void **vaddr)
210 {
211 	if (!handle) {
212 		ALOGE("Invalid buffer handle.");
213 		return -EINVAL;
214 	}
215 
216 	return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
217 }
218 
gralloc0_unlock(struct gralloc_module_t const * module,buffer_handle_t handle)219 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
220 {
221 	int32_t fence_fd, ret;
222 
223 	if (!handle) {
224 		ALOGE("Invalid buffer handle.");
225 		return -EINVAL;
226 	}
227 
228 	auto mod = (struct gralloc0_module const *)module;
229 	ret = mod->driver->unlock(handle, &fence_fd);
230 	if (ret)
231 		return ret;
232 
233 	ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
234 	if (ret)
235 		return ret;
236 
237 	return 0;
238 }
239 
gralloc0_perform(struct gralloc_module_t const * module,int op,...)240 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
241 {
242 	va_list args;
243 	int32_t *out_format, ret;
244 	uint64_t *out_store;
245 	buffer_handle_t handle;
246 	cros_gralloc_handle_t hnd;
247 	uint32_t *out_width, *out_height, *out_stride;
248 	uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
249 	uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
250 	uint64_t format_modifier = 0;
251 	struct cros_gralloc0_buffer_info *info;
252 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
253 	auto mod = const_cast<struct gralloc0_module *>(const_module);
254 	uint32_t req_usage;
255 	uint32_t gralloc_usage = 0;
256 	uint32_t *out_gralloc_usage;
257 
258 	if (!mod->initialized) {
259 		if (gralloc0_init(mod, false))
260 			return -ENODEV;
261 	}
262 
263 	va_start(args, op);
264 
265 	switch (op) {
266 	case GRALLOC_DRM_GET_STRIDE:
267 	case GRALLOC_DRM_GET_FORMAT:
268 	case GRALLOC_DRM_GET_DIMENSIONS:
269 	case GRALLOC_DRM_GET_BACKING_STORE:
270 	case GRALLOC_DRM_GET_BUFFER_INFO:
271 		/* retrieve handles for ops with buffer_handle_t */
272 		handle = va_arg(args, buffer_handle_t);
273 		if (!handle) {
274 			ALOGE("Invalid buffer handle.");
275 			return -EINVAL;
276 		}
277 		hnd = cros_gralloc_convert_handle(handle);
278 		if (!hnd) {
279 			va_end(args);
280 			ALOGE("Invalid handle.");
281 			return -EINVAL;
282 		}
283 		break;
284 	case GRALLOC_DRM_GET_USAGE:
285 		break;
286 	default:
287 		va_end(args);
288 		return -EINVAL;
289 	}
290 
291 	ret = 0;
292 	switch (op) {
293 	case GRALLOC_DRM_GET_STRIDE:
294 		out_stride = va_arg(args, uint32_t *);
295 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
296 		if (ret)
297 			break;
298 
299 		if (strides[0] != hnd->strides[0]) {
300 			uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
301 			*out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
302 		} else {
303 			*out_stride = hnd->pixel_stride;
304 		}
305 
306 		break;
307 	case GRALLOC_DRM_GET_FORMAT:
308 		out_format = va_arg(args, int32_t *);
309 		*out_format = hnd->droid_format;
310 		break;
311 	case GRALLOC_DRM_GET_DIMENSIONS:
312 		out_width = va_arg(args, uint32_t *);
313 		out_height = va_arg(args, uint32_t *);
314 		*out_width = hnd->width;
315 		*out_height = hnd->height;
316 		break;
317 	case GRALLOC_DRM_GET_BACKING_STORE:
318 		out_store = va_arg(args, uint64_t *);
319 		ret = mod->driver->get_backing_store(handle, out_store);
320 		break;
321 	case GRALLOC_DRM_GET_BUFFER_INFO:
322 		info = va_arg(args, struct cros_gralloc0_buffer_info *);
323 		memset(info, 0, sizeof(*info));
324 		info->drm_fourcc = drv_get_standard_fourcc(hnd->format);
325 		info->num_fds = hnd->num_planes;
326 		for (int i = 0; i < info->num_fds; i++)
327 			info->fds[i] = hnd->fds[i];
328 
329 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
330 		if (ret)
331 			break;
332 
333 		info->modifier = format_modifier ? format_modifier : hnd->format_modifier;
334 		for (uint32_t i = 0; i < DRV_MAX_PLANES; i++) {
335 			if (!strides[i])
336 				break;
337 
338 			info->stride[i] = strides[i];
339 			info->offset[i] = offsets[i];
340 		}
341 		break;
342 	case GRALLOC_DRM_GET_USAGE:
343 		req_usage = va_arg(args, uint32_t);
344 		out_gralloc_usage = va_arg(args, uint32_t *);
345 		if (req_usage & GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT)
346 			gralloc_usage |= BUFFER_USAGE_FRONT_RENDERING;
347 		*out_gralloc_usage = gralloc_usage;
348 		break;
349 	default:
350 		ret = -EINVAL;
351 	}
352 
353 	va_end(args);
354 
355 	return ret;
356 }
357 
gralloc0_lock_ycbcr(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,struct android_ycbcr * ycbcr)358 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
359 			       int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
360 {
361 	return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
362 }
363 
gralloc0_lock_async(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr,int fence_fd)364 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
365 			       int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
366 {
367 	int32_t ret;
368 	uint32_t map_flags;
369 	uint8_t *addr[DRV_MAX_PLANES];
370 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
371 	auto mod = const_cast<struct gralloc0_module *>(const_module);
372 	struct rectangle rect = { .x = static_cast<uint32_t>(l),
373 				  .y = static_cast<uint32_t>(t),
374 				  .width = static_cast<uint32_t>(w),
375 				  .height = static_cast<uint32_t>(h) };
376 
377 	if (!mod->initialized) {
378 		if (gralloc0_init(mod, false))
379 			return -ENODEV;
380 	}
381 
382 	if (!handle) {
383 		ALOGE("Invalid buffer handle.");
384 		return -EINVAL;
385 	}
386 
387 	auto hnd = cros_gralloc_convert_handle(handle);
388 	if (!hnd) {
389 		ALOGE("Invalid handle.");
390 		return -EINVAL;
391 	}
392 
393 	if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
394 		ALOGE("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
395 		return -EINVAL;
396 	}
397 
398 	assert(l >= 0);
399 	assert(t >= 0);
400 	assert(w >= 0);
401 	assert(h >= 0);
402 
403 	map_flags = cros_gralloc_convert_map_usage(static_cast<uint64_t>(usage));
404 	ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
405 	*vaddr = addr[0];
406 	return ret;
407 }
408 
gralloc0_unlock_async(struct gralloc_module_t const * module,buffer_handle_t handle,int * fence_fd)409 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
410 				 int *fence_fd)
411 {
412 	auto mod = (struct gralloc0_module const *)module;
413 
414 	if (!handle) {
415 		ALOGE("Invalid buffer handle.");
416 		return -EINVAL;
417 	}
418 
419 	return mod->driver->unlock(handle, fence_fd);
420 }
421 
gralloc0_lock_async_ycbcr(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,struct android_ycbcr * ycbcr,int fence_fd)422 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
423 				     int usage, int l, int t, int w, int h,
424 				     struct android_ycbcr *ycbcr, int fence_fd)
425 {
426 	int32_t ret;
427 	uint32_t map_flags;
428 	uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
429 	uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
430 	uint64_t format_modifier = 0;
431 	uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
432 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
433 	auto mod = const_cast<struct gralloc0_module *>(const_module);
434 	struct rectangle rect = { .x = static_cast<uint32_t>(l),
435 				  .y = static_cast<uint32_t>(t),
436 				  .width = static_cast<uint32_t>(w),
437 				  .height = static_cast<uint32_t>(h) };
438 
439 	if (!mod->initialized) {
440 		if (gralloc0_init(mod, false))
441 			return -ENODEV;
442 	}
443 
444 	if (!handle) {
445 		ALOGE("Invalid buffer handle.");
446 		return -EINVAL;
447 	}
448 
449 	auto hnd = cros_gralloc_convert_handle(handle);
450 	if (!hnd) {
451 		ALOGE("Invalid handle.");
452 		return -EINVAL;
453 	}
454 
455 	if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
456 	    hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
457 		ALOGE("Non-YUV format not compatible.");
458 		return -EINVAL;
459 	}
460 
461 	assert(l >= 0);
462 	assert(t >= 0);
463 	assert(w >= 0);
464 	assert(h >= 0);
465 
466 	map_flags = cros_gralloc_convert_map_usage(static_cast<uint64_t>(usage));
467 	ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
468 	if (ret)
469 		return ret;
470 
471 	if (!map_flags) {
472 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
473 		if (ret)
474 			return ret;
475 
476 		for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
477 			addr[plane] =
478 			    reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
479 	}
480 
481 	switch (hnd->format) {
482 	case DRM_FORMAT_NV12:
483 		ycbcr->y = addr[0];
484 		ycbcr->cb = addr[1];
485 		ycbcr->cr = addr[1] + 1;
486 		ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
487 		ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
488 		ycbcr->chroma_step = 2;
489 		break;
490 	case DRM_FORMAT_YVU420:
491 	case DRM_FORMAT_YVU420_ANDROID:
492 		ycbcr->y = addr[0];
493 		ycbcr->cb = addr[2];
494 		ycbcr->cr = addr[1];
495 		ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
496 		ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
497 		ycbcr->chroma_step = 1;
498 		break;
499 	default:
500 		module->unlock(module, handle);
501 		return -EINVAL;
502 	}
503 
504 	return 0;
505 }
506 
507 // clang-format off
508 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
509 // clang-format on
510 
511 struct gralloc0_module HAL_MODULE_INFO_SYM = {
512 	.base =
513 	    {
514 		.common =
515 		    {
516 			.tag = HARDWARE_MODULE_TAG,
517 			.module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
518 			.hal_api_version = 0,
519 			.id = GRALLOC_HARDWARE_MODULE_ID,
520 			.name = "CrOS Gralloc",
521 			.author = "Chrome OS",
522 			.methods = &gralloc0_module_methods,
523 		    },
524 
525 		.registerBuffer = gralloc0_register_buffer,
526 		.unregisterBuffer = gralloc0_unregister_buffer,
527 		.lock = gralloc0_lock,
528 		.unlock = gralloc0_unlock,
529 		.perform = gralloc0_perform,
530 		.lock_ycbcr = gralloc0_lock_ycbcr,
531 		.lockAsync = gralloc0_lock_async,
532 		.unlockAsync = gralloc0_unlock_async,
533 		.lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
534                 .validateBufferSize = NULL,
535                 .getTransportSize = NULL,
536 	    },
537 
538 	.alloc = nullptr,
539 	.driver = nullptr,
540 	.initialized = false,
541 };
542