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 "../../helpers.h"
8 #include "../../util.h"
9 #include "../cros_gralloc_driver.h"
12 #include <hardware/gralloc.h>
15 struct gralloc0_module {
16 gralloc_module_t base;
17 std::unique_ptr<alloc_device_t> alloc;
18 std::unique_ptr<cros_gralloc_driver> driver;
20 std::mutex initialization_mutex;
23 struct cros_gralloc0_buffer_info {
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.
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,
48 // Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
49 // passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
50 // entirety, so we can detect the video decoder flag passed by IAllocator clients.
51 #define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
53 static uint64_t gralloc0_convert_usage(int usage)
55 uint64_t use_flags = BO_USE_NONE;
57 if (usage & GRALLOC_USAGE_CURSOR)
58 use_flags |= BO_USE_NONE;
59 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
60 use_flags |= BO_USE_SW_READ_RARELY;
61 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
62 use_flags |= BO_USE_SW_READ_OFTEN;
63 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
64 use_flags |= BO_USE_SW_WRITE_RARELY;
65 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
66 use_flags |= BO_USE_SW_WRITE_OFTEN;
67 if (usage & GRALLOC_USAGE_HW_TEXTURE)
68 use_flags |= BO_USE_TEXTURE;
69 if (usage & GRALLOC_USAGE_HW_RENDER)
70 use_flags |= BO_USE_RENDERING;
71 if (usage & GRALLOC_USAGE_HW_2D)
72 use_flags |= BO_USE_RENDERING;
73 if (usage & GRALLOC_USAGE_HW_COMPOSER)
74 /* HWC wants to use display hardware, but can defer to OpenGL. */
75 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
76 if (usage & GRALLOC_USAGE_HW_FB)
77 use_flags |= BO_USE_COMPOSER_TARGET;
78 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
80 * This flag potentially covers external display for the normal drivers (i915,
81 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
83 use_flags |= BO_USE_NONE;
84 /* Map this flag to linear until real HW protection is available on Android. */
85 if (usage & GRALLOC_USAGE_PROTECTED)
86 use_flags |= BO_USE_LINEAR;
87 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
88 use_flags |= BO_USE_HW_VIDEO_ENCODER;
89 /*HACK: See b/30054495 */
90 use_flags |= BO_USE_SW_READ_OFTEN;
92 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
93 use_flags |= BO_USE_CAMERA_WRITE;
94 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
95 use_flags |= BO_USE_CAMERA_READ;
96 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
97 use_flags |= BO_USE_RENDERSCRIPT;
98 if (usage & BUFFER_USAGE_VIDEO_DECODER)
99 use_flags |= BO_USE_HW_VIDEO_DECODER;
104 static uint32_t gralloc0_convert_map_usage(int map_usage)
106 uint32_t map_flags = BO_MAP_NONE;
108 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
109 map_flags |= BO_MAP_READ;
110 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
111 map_flags |= BO_MAP_WRITE;
116 static int gralloc0_droid_yuv_format(int droid_format)
119 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
120 droid_format == HAL_PIXEL_FORMAT_YV12);
123 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
124 buffer_handle_t *handle, int *stride)
128 struct cros_gralloc_buffer_descriptor descriptor;
129 auto mod = (struct gralloc0_module const *)dev->common.module;
131 descriptor.width = w;
132 descriptor.height = h;
133 descriptor.droid_format = format;
134 descriptor.droid_usage = usage;
135 descriptor.drm_format = cros_gralloc_convert_format(format);
136 descriptor.use_flags = gralloc0_convert_usage(usage);
137 descriptor.reserved_region_size = 0;
139 supported = mod->driver->is_supported(&descriptor);
140 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
141 descriptor.use_flags &= ~(BO_USE_SCANOUT | BO_USE_COMPOSER_TARGET);
142 supported = mod->driver->is_supported(&descriptor);
144 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
145 !gralloc0_droid_yuv_format(format)) {
146 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
147 // because they are not input to a hw encoder but used as an
148 // intermediate format (e.g. camera).
149 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
150 supported = mod->driver->is_supported(&descriptor);
154 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
155 "drv_format: %4.4s, use_flags: %llu\n",
156 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
157 static_cast<unsigned long long>(descriptor.use_flags));
161 ret = mod->driver->allocate(&descriptor, handle);
165 auto hnd = cros_gralloc_convert_handle(*handle);
166 *stride = hnd->pixel_stride;
171 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
173 auto mod = (struct gralloc0_module const *)dev->common.module;
174 return mod->driver->release(handle);
177 static int gralloc0_close(struct hw_device_t *dev)
179 /* Memory is freed by managed pointers on process close. */
183 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
185 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
187 if (mod->initialized)
190 mod->driver = std::make_unique<cros_gralloc_driver>();
191 if (mod->driver->init()) {
192 drv_log("Failed to initialize driver.\n");
196 if (initialize_alloc) {
197 mod->alloc = std::make_unique<alloc_device_t>();
198 mod->alloc->alloc = gralloc0_alloc;
199 mod->alloc->free = gralloc0_free;
200 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
201 mod->alloc->common.version = 0;
202 mod->alloc->common.module = (hw_module_t *)mod;
203 mod->alloc->common.close = gralloc0_close;
206 mod->initialized = true;
210 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
212 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
213 auto module = const_cast<struct gralloc0_module *>(const_module);
215 if (module->initialized) {
216 *dev = &module->alloc->common;
220 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
221 drv_log("Incorrect device name - %s.\n", name);
225 if (gralloc0_init(module, true))
228 *dev = &module->alloc->common;
232 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
234 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
235 auto mod = const_cast<struct gralloc0_module *>(const_module);
237 if (!mod->initialized)
238 if (gralloc0_init(mod, false))
241 return mod->driver->retain(handle);
244 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
246 auto mod = (struct gralloc0_module const *)module;
247 return mod->driver->release(handle);
250 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
251 int l, int t, int w, int h, void **vaddr)
253 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
256 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
258 int32_t fence_fd, ret;
259 auto mod = (struct gralloc0_module const *)module;
260 ret = mod->driver->unlock(handle, &fence_fd);
264 ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
271 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
274 int32_t *out_format, ret;
276 buffer_handle_t handle;
277 uint32_t *out_width, *out_height, *out_stride;
278 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
279 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
280 struct cros_gralloc0_buffer_info *info;
281 auto mod = (struct gralloc0_module const *)module;
284 case GRALLOC_DRM_GET_STRIDE:
285 case GRALLOC_DRM_GET_FORMAT:
286 case GRALLOC_DRM_GET_DIMENSIONS:
287 case GRALLOC_DRM_GET_BACKING_STORE:
288 case GRALLOC_DRM_GET_BUFFER_INFO:
297 handle = va_arg(args, buffer_handle_t);
298 auto hnd = cros_gralloc_convert_handle(handle);
300 drv_log("Invalid handle.\n");
305 case GRALLOC_DRM_GET_STRIDE:
306 out_stride = va_arg(args, uint32_t *);
307 ret = mod->driver->resource_info(handle, strides, offsets);
311 if (strides[0] != hnd->strides[0]) {
312 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
313 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
315 *out_stride = hnd->pixel_stride;
319 case GRALLOC_DRM_GET_FORMAT:
320 out_format = va_arg(args, int32_t *);
321 *out_format = hnd->droid_format;
323 case GRALLOC_DRM_GET_DIMENSIONS:
324 out_width = va_arg(args, uint32_t *);
325 out_height = va_arg(args, uint32_t *);
326 *out_width = hnd->width;
327 *out_height = hnd->height;
329 case GRALLOC_DRM_GET_BACKING_STORE:
330 out_store = va_arg(args, uint64_t *);
331 ret = mod->driver->get_backing_store(handle, out_store);
333 case GRALLOC_DRM_GET_BUFFER_INFO:
334 info = va_arg(args, struct cros_gralloc0_buffer_info *);
335 info->drm_fourcc = drv_get_standard_fourcc(hnd->format);
336 info->num_fds = hnd->num_planes;
337 info->modifier = hnd->format_modifier;
338 for (uint32_t i = 0; i < hnd->num_planes; i++) {
339 info->fds[i] = hnd->fds[i];
340 info->offset[i] = hnd->offsets[i];
341 info->stride[i] = hnd->strides[i];
353 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
354 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
356 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
359 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
360 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
364 uint8_t *addr[DRV_MAX_PLANES];
365 auto mod = (struct gralloc0_module const *)module;
366 struct rectangle rect = { .x = static_cast<uint32_t>(l),
367 .y = static_cast<uint32_t>(t),
368 .width = static_cast<uint32_t>(w),
369 .height = static_cast<uint32_t>(h) };
371 auto hnd = cros_gralloc_convert_handle(handle);
373 drv_log("Invalid handle.\n");
377 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
378 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
387 map_flags = gralloc0_convert_map_usage(usage);
388 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
393 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
396 auto mod = (struct gralloc0_module const *)module;
397 return mod->driver->unlock(handle, fence_fd);
400 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
401 int usage, int l, int t, int w, int h,
402 struct android_ycbcr *ycbcr, int fence_fd)
406 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
407 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
408 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
409 auto mod = (struct gralloc0_module const *)module;
410 struct rectangle rect = { .x = static_cast<uint32_t>(l),
411 .y = static_cast<uint32_t>(t),
412 .width = static_cast<uint32_t>(w),
413 .height = static_cast<uint32_t>(h) };
415 auto hnd = cros_gralloc_convert_handle(handle);
417 drv_log("Invalid handle.\n");
421 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
422 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
423 drv_log("Non-YUV format not compatible.\n");
432 map_flags = gralloc0_convert_map_usage(usage);
433 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
438 ret = mod->driver->resource_info(handle, strides, offsets);
442 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
444 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
447 switch (hnd->format) {
448 case DRM_FORMAT_NV12:
451 ycbcr->cr = addr[1] + 1;
452 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
453 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
454 ycbcr->chroma_step = 2;
456 case DRM_FORMAT_YVU420:
457 case DRM_FORMAT_YVU420_ANDROID:
461 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
462 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
463 ycbcr->chroma_step = 1;
466 module->unlock(module, handle);
474 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
477 struct gralloc0_module HAL_MODULE_INFO_SYM = {
482 .tag = HARDWARE_MODULE_TAG,
483 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
484 .hal_api_version = 0,
485 .id = GRALLOC_HARDWARE_MODULE_ID,
486 .name = "CrOS Gralloc",
487 .author = "Chrome OS",
488 .methods = &gralloc0_module_methods,
491 .registerBuffer = gralloc0_register_buffer,
492 .unregisterBuffer = gralloc0_unregister_buffer,
493 .lock = gralloc0_lock,
494 .unlock = gralloc0_unlock,
495 .perform = gralloc0_perform,
496 .lock_ycbcr = gralloc0_lock_ycbcr,
497 .lockAsync = gralloc0_lock_async,
498 .unlockAsync = gralloc0_unlock_async,
499 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
500 .validateBufferSize = NULL,
501 .getTransportSize = NULL,
506 .initialized = false,