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.
7 #include "../../util.h"
8 #include "../cros_gralloc_driver.h"
11 #include <hardware/gralloc.h>
14 struct gralloc0_module {
15 gralloc_module_t base;
16 std::unique_ptr<alloc_device_t> alloc;
17 std::unique_ptr<cros_gralloc_driver> driver;
19 std::mutex initialization_mutex;
22 /* This enumeration must match the one in <gralloc_drm.h>.
23 * The functions supported by this gralloc's temporary private API are listed
24 * below. Use of these functions is highly discouraged and should only be
25 * reserved for cases where no alternative to get same information (such as
26 * querying ANativeWindow) exists.
30 GRALLOC_DRM_GET_STRIDE,
31 GRALLOC_DRM_GET_FORMAT,
32 GRALLOC_DRM_GET_DIMENSIONS,
33 GRALLOC_DRM_GET_BACKING_STORE,
37 // Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
38 // passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
39 // entirety, so we can detect the video decoder flag passed by IAllocator clients.
40 #define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
42 static uint64_t gralloc0_convert_usage(int usage)
44 uint64_t use_flags = BO_USE_NONE;
46 if (usage & GRALLOC_USAGE_CURSOR)
47 use_flags |= BO_USE_NONE;
48 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
49 use_flags |= BO_USE_SW_READ_RARELY;
50 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
51 use_flags |= BO_USE_SW_READ_OFTEN;
52 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
53 use_flags |= BO_USE_SW_WRITE_RARELY;
54 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
55 use_flags |= BO_USE_SW_WRITE_OFTEN;
56 if (usage & GRALLOC_USAGE_HW_TEXTURE)
57 use_flags |= BO_USE_TEXTURE;
58 if (usage & GRALLOC_USAGE_HW_RENDER)
59 use_flags |= BO_USE_RENDERING;
60 if (usage & GRALLOC_USAGE_HW_2D)
61 use_flags |= BO_USE_RENDERING;
62 if (usage & GRALLOC_USAGE_HW_COMPOSER)
63 /* HWC wants to use display hardware, but can defer to OpenGL. */
64 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
65 if (usage & GRALLOC_USAGE_HW_FB)
66 use_flags |= BO_USE_NONE;
67 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
69 * This flag potentially covers external display for the normal drivers (i915,
70 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
72 use_flags |= BO_USE_NONE;
73 if (usage & GRALLOC_USAGE_PROTECTED)
74 use_flags |= BO_USE_PROTECTED;
75 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
76 use_flags |= BO_USE_HW_VIDEO_ENCODER;
77 /*HACK: See b/30054495 */
78 use_flags |= BO_USE_SW_READ_OFTEN;
80 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
81 use_flags |= BO_USE_CAMERA_WRITE;
82 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
83 use_flags |= BO_USE_CAMERA_READ;
84 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
85 use_flags |= BO_USE_RENDERSCRIPT;
86 if (usage & BUFFER_USAGE_VIDEO_DECODER)
87 use_flags |= BO_USE_HW_VIDEO_DECODER;
92 static uint32_t gralloc0_convert_map_usage(int map_usage)
94 uint32_t map_flags = BO_MAP_NONE;
96 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
97 map_flags |= BO_MAP_READ;
98 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
99 map_flags |= BO_MAP_WRITE;
104 static int gralloc0_droid_yuv_format(int droid_format)
107 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
108 droid_format == HAL_PIXEL_FORMAT_YV12);
111 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
112 buffer_handle_t *handle, int *stride)
116 struct cros_gralloc_buffer_descriptor descriptor;
117 auto mod = (struct gralloc0_module const *)dev->common.module;
119 descriptor.width = w;
120 descriptor.height = h;
121 descriptor.droid_format = format;
122 descriptor.producer_usage = descriptor.consumer_usage = usage;
123 descriptor.drm_format = cros_gralloc_convert_format(format);
124 descriptor.use_flags = gralloc0_convert_usage(usage);
126 supported = mod->driver->is_supported(&descriptor);
127 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
128 descriptor.use_flags &= ~BO_USE_SCANOUT;
129 supported = mod->driver->is_supported(&descriptor);
131 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
132 !gralloc0_droid_yuv_format(format)) {
133 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
134 // because they are not input to a hw encoder but used as an
135 // intermediate format (e.g. camera).
136 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
137 supported = mod->driver->is_supported(&descriptor);
141 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
142 "drv_format: %4.4s, use_flags: %llu\n",
143 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
144 static_cast<unsigned long long>(descriptor.use_flags));
148 ret = mod->driver->allocate(&descriptor, handle);
152 auto hnd = cros_gralloc_convert_handle(*handle);
153 *stride = hnd->pixel_stride;
158 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
160 auto mod = (struct gralloc0_module const *)dev->common.module;
161 return mod->driver->release(handle);
164 static int gralloc0_close(struct hw_device_t *dev)
166 /* Memory is freed by managed pointers on process close. */
170 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
172 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
174 if (mod->initialized)
177 mod->driver = std::make_unique<cros_gralloc_driver>();
178 if (mod->driver->init()) {
179 drv_log("Failed to initialize driver.\n");
183 if (initialize_alloc) {
184 mod->alloc = std::make_unique<alloc_device_t>();
185 mod->alloc->alloc = gralloc0_alloc;
186 mod->alloc->free = gralloc0_free;
187 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
188 mod->alloc->common.version = 0;
189 mod->alloc->common.module = (hw_module_t *)mod;
190 mod->alloc->common.close = gralloc0_close;
193 mod->initialized = true;
197 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
199 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
200 auto module = const_cast<struct gralloc0_module *>(const_module);
202 if (module->initialized) {
203 *dev = &module->alloc->common;
207 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
208 drv_log("Incorrect device name - %s.\n", name);
212 if (gralloc0_init(module, true))
215 *dev = &module->alloc->common;
219 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
221 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
222 auto mod = const_cast<struct gralloc0_module *>(const_module);
224 if (!mod->initialized)
225 if (gralloc0_init(mod, false))
228 return mod->driver->retain(handle);
231 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
233 auto mod = (struct gralloc0_module const *)module;
234 return mod->driver->release(handle);
237 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
238 int l, int t, int w, int h, void **vaddr)
240 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
243 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
245 int32_t fence_fd, ret;
246 auto mod = (struct gralloc0_module const *)module;
247 ret = mod->driver->unlock(handle, &fence_fd);
251 ret = cros_gralloc_sync_wait(fence_fd);
258 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
261 int32_t *out_format, ret;
263 buffer_handle_t handle;
264 uint32_t *out_width, *out_height, *out_stride;
265 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
266 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
267 auto mod = (struct gralloc0_module const *)module;
270 case GRALLOC_DRM_GET_STRIDE:
271 case GRALLOC_DRM_GET_FORMAT:
272 case GRALLOC_DRM_GET_DIMENSIONS:
273 case GRALLOC_DRM_GET_BACKING_STORE:
282 handle = va_arg(args, buffer_handle_t);
283 auto hnd = cros_gralloc_convert_handle(handle);
285 drv_log("Invalid handle.\n");
290 case GRALLOC_DRM_GET_STRIDE:
291 out_stride = va_arg(args, uint32_t *);
292 ret = mod->driver->resource_info(handle, strides, offsets);
296 if (strides[0] != hnd->strides[0]) {
297 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
298 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
300 *out_stride = hnd->pixel_stride;
304 case GRALLOC_DRM_GET_FORMAT:
305 out_format = va_arg(args, int32_t *);
306 *out_format = hnd->droid_format;
308 case GRALLOC_DRM_GET_DIMENSIONS:
309 out_width = va_arg(args, uint32_t *);
310 out_height = va_arg(args, uint32_t *);
311 *out_width = hnd->width;
312 *out_height = hnd->height;
314 case GRALLOC_DRM_GET_BACKING_STORE:
315 out_store = va_arg(args, uint64_t *);
316 ret = mod->driver->get_backing_store(handle, out_store);
327 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
328 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
330 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
333 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
334 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
338 uint8_t *addr[DRV_MAX_PLANES];
339 auto mod = (struct gralloc0_module const *)module;
340 struct rectangle rect = { .x = static_cast<uint32_t>(l),
341 .y = static_cast<uint32_t>(t),
342 .width = static_cast<uint32_t>(w),
343 .height = static_cast<uint32_t>(h) };
345 auto hnd = cros_gralloc_convert_handle(handle);
347 drv_log("Invalid handle.\n");
351 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
352 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
361 map_flags = gralloc0_convert_map_usage(usage);
362 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
367 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
370 auto mod = (struct gralloc0_module const *)module;
371 return mod->driver->unlock(handle, fence_fd);
374 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
375 int usage, int l, int t, int w, int h,
376 struct android_ycbcr *ycbcr, int fence_fd)
380 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
381 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
382 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
383 auto mod = (struct gralloc0_module const *)module;
384 struct rectangle rect = { .x = static_cast<uint32_t>(l),
385 .y = static_cast<uint32_t>(t),
386 .width = static_cast<uint32_t>(w),
387 .height = static_cast<uint32_t>(h) };
389 auto hnd = cros_gralloc_convert_handle(handle);
391 drv_log("Invalid handle.\n");
395 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
396 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
397 drv_log("Non-YUV format not compatible.\n");
406 map_flags = gralloc0_convert_map_usage(usage);
407 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
412 ret = mod->driver->resource_info(handle, strides, offsets);
416 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
418 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
421 switch (hnd->format) {
422 case DRM_FORMAT_NV12:
425 ycbcr->cr = addr[1] + 1;
426 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
427 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
428 ycbcr->chroma_step = 2;
430 case DRM_FORMAT_YVU420:
431 case DRM_FORMAT_YVU420_ANDROID:
435 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
436 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
437 ycbcr->chroma_step = 1;
440 module->unlock(module, handle);
448 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
451 struct gralloc0_module HAL_MODULE_INFO_SYM = {
456 .tag = HARDWARE_MODULE_TAG,
457 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
458 .hal_api_version = 0,
459 .id = GRALLOC_HARDWARE_MODULE_ID,
460 .name = "CrOS Gralloc",
461 .author = "Chrome OS",
462 .methods = &gralloc0_module_methods,
465 .registerBuffer = gralloc0_register_buffer,
466 .unregisterBuffer = gralloc0_unregister_buffer,
467 .lock = gralloc0_lock,
468 .unlock = gralloc0_unlock,
469 .perform = gralloc0_perform,
470 .lock_ycbcr = gralloc0_lock_ycbcr,
471 .lockAsync = gralloc0_lock_async,
472 .unlockAsync = gralloc0_unlock_async,
473 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
474 .validateBufferSize = NULL,
475 .getTransportSize = NULL,
480 .initialized = false,