• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	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.reserved_region_size = 0;
85 
86 	if (!mod->driver->is_supported(&descriptor)) {
87 		drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
88 			"drv_format: %4.4s, use_flags: %llu\n",
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 	ret = mod->driver->release(handle);
111 	if (ret)
112 		return ret;
113 
114 	auto hnd = const_cast<native_handle_t *>(handle);
115 	native_handle_close(hnd);
116 	native_handle_delete(hnd);
117 
118 	return 0;
119 }
120 
gralloc0_close(struct hw_device_t * dev)121 static int gralloc0_close(struct hw_device_t *dev)
122 {
123 	/* Memory is freed by managed pointers on process close. */
124 	return 0;
125 }
126 
gralloc0_init(struct gralloc0_module * mod,bool initialize_alloc)127 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
128 {
129 	std::lock_guard<std::mutex> lock(mod->initialization_mutex);
130 
131 	if (mod->initialized)
132 		return 0;
133 
134 	mod->driver = cros_gralloc_driver::get_instance();
135 	if (!mod->driver)
136 		return -ENODEV;
137 
138 	if (initialize_alloc) {
139 		mod->alloc = std::make_unique<alloc_device_t>();
140 		mod->alloc->alloc = gralloc0_alloc;
141 		mod->alloc->free = gralloc0_free;
142 		mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
143 		mod->alloc->common.version = 0;
144 		mod->alloc->common.module = (hw_module_t *)mod;
145 		mod->alloc->common.close = gralloc0_close;
146 	}
147 
148 	mod->initialized = true;
149 	return 0;
150 }
151 
gralloc0_open(const struct hw_module_t * mod,const char * name,struct hw_device_t ** dev)152 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
153 {
154 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
155 	auto module = const_cast<struct gralloc0_module *>(const_module);
156 
157 	if (module->initialized) {
158 		*dev = &module->alloc->common;
159 		return 0;
160 	}
161 
162 	if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
163 		drv_log("Incorrect device name - %s.\n", name);
164 		return -EINVAL;
165 	}
166 
167 	if (gralloc0_init(module, true))
168 		return -ENODEV;
169 
170 	*dev = &module->alloc->common;
171 	return 0;
172 }
173 
gralloc0_register_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)174 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
175 {
176 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
177 	auto mod = const_cast<struct gralloc0_module *>(const_module);
178 
179 	if (!mod->initialized) {
180 		if (gralloc0_init(mod, false))
181 			return -ENODEV;
182 	}
183 
184 	return mod->driver->retain(handle);
185 }
186 
gralloc0_unregister_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)187 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
188 {
189 	auto mod = (struct gralloc0_module const *)module;
190 	return mod->driver->release(handle);
191 }
192 
gralloc0_lock(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr)193 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
194 			 int l, int t, int w, int h, void **vaddr)
195 {
196 	return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
197 }
198 
gralloc0_unlock(struct gralloc_module_t const * module,buffer_handle_t handle)199 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
200 {
201 	int32_t fence_fd, ret;
202 	auto mod = (struct gralloc0_module const *)module;
203 	ret = mod->driver->unlock(handle, &fence_fd);
204 	if (ret)
205 		return ret;
206 
207 	ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
208 	if (ret)
209 		return ret;
210 
211 	return 0;
212 }
213 
gralloc0_perform(struct gralloc_module_t const * module,int op,...)214 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
215 {
216 	va_list args;
217 	int32_t *out_format, ret;
218 	uint64_t *out_store;
219 	buffer_handle_t handle;
220 	cros_gralloc_handle_t hnd;
221 	uint32_t *out_width, *out_height, *out_stride;
222 	uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
223 	uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
224 	uint64_t format_modifier = 0;
225 	struct cros_gralloc0_buffer_info *info;
226 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
227 	auto mod = const_cast<struct gralloc0_module *>(const_module);
228 	uint32_t req_usage;
229 	uint32_t gralloc_usage = 0;
230 	uint32_t *out_gralloc_usage;
231 
232 	if (!mod->initialized) {
233 		if (gralloc0_init(mod, false))
234 			return -ENODEV;
235 	}
236 
237 	va_start(args, op);
238 
239 	switch (op) {
240 	case GRALLOC_DRM_GET_STRIDE:
241 	case GRALLOC_DRM_GET_FORMAT:
242 	case GRALLOC_DRM_GET_DIMENSIONS:
243 	case GRALLOC_DRM_GET_BACKING_STORE:
244 	case GRALLOC_DRM_GET_BUFFER_INFO:
245 		/* retrieve handles for ops with buffer_handle_t */
246 		handle = va_arg(args, buffer_handle_t);
247 		hnd = cros_gralloc_convert_handle(handle);
248 		if (!hnd) {
249 			va_end(args);
250 			drv_log("Invalid handle.\n");
251 			return -EINVAL;
252 		}
253 		break;
254 	case GRALLOC_DRM_GET_USAGE:
255 		break;
256 	default:
257 		va_end(args);
258 		return -EINVAL;
259 	}
260 
261 	ret = 0;
262 	switch (op) {
263 	case GRALLOC_DRM_GET_STRIDE:
264 		out_stride = va_arg(args, uint32_t *);
265 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
266 		if (ret)
267 			break;
268 
269 		if (strides[0] != hnd->strides[0]) {
270 			uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
271 			*out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
272 		} else {
273 			*out_stride = hnd->pixel_stride;
274 		}
275 
276 		break;
277 	case GRALLOC_DRM_GET_FORMAT:
278 		out_format = va_arg(args, int32_t *);
279 		*out_format = hnd->droid_format;
280 		break;
281 	case GRALLOC_DRM_GET_DIMENSIONS:
282 		out_width = va_arg(args, uint32_t *);
283 		out_height = va_arg(args, uint32_t *);
284 		*out_width = hnd->width;
285 		*out_height = hnd->height;
286 		break;
287 	case GRALLOC_DRM_GET_BACKING_STORE:
288 		out_store = va_arg(args, uint64_t *);
289 		ret = mod->driver->get_backing_store(handle, out_store);
290 		break;
291 	case GRALLOC_DRM_GET_BUFFER_INFO:
292 		info = va_arg(args, struct cros_gralloc0_buffer_info *);
293 		memset(info, 0, sizeof(*info));
294 		info->drm_fourcc = drv_get_standard_fourcc(hnd->format);
295 		info->num_fds = hnd->num_planes;
296 		for (int i = 0; i < info->num_fds; i++)
297 			info->fds[i] = hnd->fds[i];
298 
299 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
300 		if (ret)
301 			break;
302 
303 		info->modifier = format_modifier ? format_modifier : hnd->format_modifier;
304 		for (uint32_t i = 0; i < DRV_MAX_PLANES; i++) {
305 			if (!strides[i])
306 				break;
307 
308 			info->stride[i] = strides[i];
309 			info->offset[i] = offsets[i];
310 		}
311 		break;
312 	case GRALLOC_DRM_GET_USAGE:
313 		req_usage = va_arg(args, uint32_t);
314 		out_gralloc_usage = va_arg(args, uint32_t *);
315 		if (req_usage & GRALLOC_DRM_GET_USAGE_FRONT_RENDERING_BIT)
316 			gralloc_usage |= BUFFER_USAGE_FRONT_RENDERING;
317 		*out_gralloc_usage = gralloc_usage;
318 		break;
319 	default:
320 		ret = -EINVAL;
321 	}
322 
323 	va_end(args);
324 
325 	return ret;
326 }
327 
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)328 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
329 			       int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
330 {
331 	return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
332 }
333 
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)334 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
335 			       int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
336 {
337 	int32_t ret;
338 	uint32_t map_flags;
339 	uint8_t *addr[DRV_MAX_PLANES];
340 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
341 	auto mod = const_cast<struct gralloc0_module *>(const_module);
342 	struct rectangle rect = { .x = static_cast<uint32_t>(l),
343 				  .y = static_cast<uint32_t>(t),
344 				  .width = static_cast<uint32_t>(w),
345 				  .height = static_cast<uint32_t>(h) };
346 
347 	if (!mod->initialized) {
348 		if (gralloc0_init(mod, false))
349 			return -ENODEV;
350 	}
351 
352 	auto hnd = cros_gralloc_convert_handle(handle);
353 	if (!hnd) {
354 		drv_log("Invalid handle.\n");
355 		return -EINVAL;
356 	}
357 
358 	if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
359 		drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
360 		return -EINVAL;
361 	}
362 
363 	assert(l >= 0);
364 	assert(t >= 0);
365 	assert(w >= 0);
366 	assert(h >= 0);
367 
368 	map_flags = cros_gralloc_convert_map_usage(static_cast<uint64_t>(usage));
369 	ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
370 	*vaddr = addr[0];
371 	return ret;
372 }
373 
gralloc0_unlock_async(struct gralloc_module_t const * module,buffer_handle_t handle,int * fence_fd)374 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
375 				 int *fence_fd)
376 {
377 	auto mod = (struct gralloc0_module const *)module;
378 	return mod->driver->unlock(handle, fence_fd);
379 }
380 
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)381 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
382 				     int usage, int l, int t, int w, int h,
383 				     struct android_ycbcr *ycbcr, int fence_fd)
384 {
385 	int32_t ret;
386 	uint32_t map_flags;
387 	uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
388 	uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
389 	uint64_t format_modifier = 0;
390 	uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
391 	auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
392 	auto mod = const_cast<struct gralloc0_module *>(const_module);
393 	struct rectangle rect = { .x = static_cast<uint32_t>(l),
394 				  .y = static_cast<uint32_t>(t),
395 				  .width = static_cast<uint32_t>(w),
396 				  .height = static_cast<uint32_t>(h) };
397 
398 	if (!mod->initialized) {
399 		if (gralloc0_init(mod, false))
400 			return -ENODEV;
401 	}
402 
403 	auto hnd = cros_gralloc_convert_handle(handle);
404 	if (!hnd) {
405 		drv_log("Invalid handle.\n");
406 		return -EINVAL;
407 	}
408 
409 	if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
410 	    hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
411 		drv_log("Non-YUV format not compatible.\n");
412 		return -EINVAL;
413 	}
414 
415 	assert(l >= 0);
416 	assert(t >= 0);
417 	assert(w >= 0);
418 	assert(h >= 0);
419 
420 	map_flags = cros_gralloc_convert_map_usage(static_cast<uint64_t>(usage));
421 	ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
422 	if (ret)
423 		return ret;
424 
425 	if (!map_flags) {
426 		ret = mod->driver->resource_info(handle, strides, offsets, &format_modifier);
427 		if (ret)
428 			return ret;
429 
430 		for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
431 			addr[plane] =
432 			    reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
433 	}
434 
435 	switch (hnd->format) {
436 	case DRM_FORMAT_NV12:
437 		ycbcr->y = addr[0];
438 		ycbcr->cb = addr[1];
439 		ycbcr->cr = addr[1] + 1;
440 		ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
441 		ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
442 		ycbcr->chroma_step = 2;
443 		break;
444 	case DRM_FORMAT_YVU420:
445 	case DRM_FORMAT_YVU420_ANDROID:
446 		ycbcr->y = addr[0];
447 		ycbcr->cb = addr[2];
448 		ycbcr->cr = addr[1];
449 		ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
450 		ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
451 		ycbcr->chroma_step = 1;
452 		break;
453 	default:
454 		module->unlock(module, handle);
455 		return -EINVAL;
456 	}
457 
458 	return 0;
459 }
460 
461 // clang-format off
462 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
463 // clang-format on
464 
465 struct gralloc0_module HAL_MODULE_INFO_SYM = {
466 	.base =
467 	    {
468 		.common =
469 		    {
470 			.tag = HARDWARE_MODULE_TAG,
471 			.module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
472 			.hal_api_version = 0,
473 			.id = GRALLOC_HARDWARE_MODULE_ID,
474 			.name = "CrOS Gralloc",
475 			.author = "Chrome OS",
476 			.methods = &gralloc0_module_methods,
477 		    },
478 
479 		.registerBuffer = gralloc0_register_buffer,
480 		.unregisterBuffer = gralloc0_unregister_buffer,
481 		.lock = gralloc0_lock,
482 		.unlock = gralloc0_unlock,
483 		.perform = gralloc0_perform,
484 		.lock_ycbcr = gralloc0_lock_ycbcr,
485 		.lockAsync = gralloc0_lock_async,
486 		.unlockAsync = gralloc0_unlock_async,
487 		.lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
488                 .validateBufferSize = NULL,
489                 .getTransportSize = NULL,
490 	    },
491 
492 	.alloc = nullptr,
493 	.driver = nullptr,
494 	.initialized = false,
495 };
496