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 "../cros_gralloc_driver.h"
10 #include <hardware/gralloc.h>
13 struct gralloc0_module {
14 gralloc_module_t base;
15 std::unique_ptr<alloc_device_t> alloc;
16 std::unique_ptr<cros_gralloc_driver> driver;
18 std::mutex initialization_mutex;
21 /* This enumeration must match the one in <gralloc_drm.h>.
22 * The functions supported by this gralloc's temporary private API are listed
23 * below. Use of these functions is highly discouraged and should only be
24 * reserved for cases where no alternative to get same information (such as
25 * querying ANativeWindow) exists.
29 GRALLOC_DRM_GET_STRIDE,
30 GRALLOC_DRM_GET_FORMAT,
31 GRALLOC_DRM_GET_DIMENSIONS,
32 GRALLOC_DRM_GET_BACKING_STORE,
36 // Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
37 // passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
38 // entirety, so we can detect the video decoder flag passed by IAllocator clients.
39 #define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
41 static uint64_t gralloc0_convert_usage(int usage)
43 uint64_t use_flags = BO_USE_NONE;
45 if (usage & GRALLOC_USAGE_CURSOR)
46 use_flags |= BO_USE_NONE;
47 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
48 use_flags |= BO_USE_SW_READ_RARELY;
49 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
50 use_flags |= BO_USE_SW_READ_OFTEN;
51 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
52 use_flags |= BO_USE_SW_WRITE_RARELY;
53 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
54 use_flags |= BO_USE_SW_WRITE_OFTEN;
55 if (usage & GRALLOC_USAGE_HW_TEXTURE)
56 use_flags |= BO_USE_TEXTURE;
57 if (usage & GRALLOC_USAGE_HW_RENDER)
58 use_flags |= BO_USE_RENDERING;
59 if (usage & GRALLOC_USAGE_HW_2D)
60 use_flags |= BO_USE_RENDERING;
61 if (usage & GRALLOC_USAGE_HW_COMPOSER)
62 /* HWC wants to use display hardware, but can defer to OpenGL. */
63 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
64 if (usage & GRALLOC_USAGE_HW_FB)
65 use_flags |= BO_USE_NONE;
66 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
68 * This flag potentially covers external display for the normal drivers (i915,
69 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
71 use_flags |= BO_USE_NONE;
72 if (usage & GRALLOC_USAGE_PROTECTED)
73 use_flags |= BO_USE_PROTECTED;
74 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
75 use_flags |= BO_USE_HW_VIDEO_ENCODER;
76 /*HACK: See b/30054495 */
77 use_flags |= BO_USE_SW_READ_OFTEN;
79 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
80 use_flags |= BO_USE_CAMERA_WRITE;
81 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
82 use_flags |= BO_USE_CAMERA_READ;
83 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
84 use_flags |= BO_USE_RENDERSCRIPT;
85 if (usage & BUFFER_USAGE_VIDEO_DECODER)
86 use_flags |= BO_USE_HW_VIDEO_DECODER;
91 static uint32_t gralloc0_convert_map_usage(int map_usage)
93 uint32_t map_flags = BO_MAP_NONE;
95 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
96 map_flags |= BO_MAP_READ;
97 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
98 map_flags |= BO_MAP_WRITE;
103 static int gralloc0_droid_yuv_format(int droid_format)
106 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
107 droid_format == HAL_PIXEL_FORMAT_YV12);
110 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
111 buffer_handle_t *handle, int *stride)
115 struct cros_gralloc_buffer_descriptor descriptor;
116 auto mod = (struct gralloc0_module const *)dev->common.module;
118 descriptor.width = w;
119 descriptor.height = h;
120 descriptor.droid_format = format;
121 descriptor.producer_usage = descriptor.consumer_usage = usage;
122 descriptor.drm_format = cros_gralloc_convert_format(format);
123 descriptor.use_flags = gralloc0_convert_usage(usage);
125 supported = mod->driver->is_supported(&descriptor);
126 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
127 descriptor.use_flags &= ~BO_USE_SCANOUT;
128 supported = mod->driver->is_supported(&descriptor);
130 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
131 !gralloc0_droid_yuv_format(format)) {
132 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
133 // because they are not input to a hw encoder but used as an
134 // intermediate format (e.g. camera).
135 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
136 supported = mod->driver->is_supported(&descriptor);
140 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
141 "drv_format: %4.4s, use_flags: %llu\n",
142 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
143 static_cast<unsigned long long>(descriptor.use_flags));
147 ret = mod->driver->allocate(&descriptor, handle);
151 auto hnd = cros_gralloc_convert_handle(*handle);
152 *stride = hnd->pixel_stride;
157 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
159 auto mod = (struct gralloc0_module const *)dev->common.module;
160 return mod->driver->release(handle);
163 static int gralloc0_close(struct hw_device_t *dev)
165 /* Memory is freed by managed pointers on process close. */
169 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
171 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
173 if (mod->initialized)
176 mod->driver = std::make_unique<cros_gralloc_driver>();
177 if (mod->driver->init()) {
178 drv_log("Failed to initialize driver.\n");
182 if (initialize_alloc) {
183 mod->alloc = std::make_unique<alloc_device_t>();
184 mod->alloc->alloc = gralloc0_alloc;
185 mod->alloc->free = gralloc0_free;
186 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
187 mod->alloc->common.version = 0;
188 mod->alloc->common.module = (hw_module_t *)mod;
189 mod->alloc->common.close = gralloc0_close;
192 mod->initialized = true;
196 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
198 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
199 auto module = const_cast<struct gralloc0_module *>(const_module);
201 if (module->initialized) {
202 *dev = &module->alloc->common;
206 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
207 drv_log("Incorrect device name - %s.\n", name);
211 if (gralloc0_init(module, true))
214 *dev = &module->alloc->common;
218 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
220 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
221 auto mod = const_cast<struct gralloc0_module *>(const_module);
223 if (!mod->initialized)
224 if (gralloc0_init(mod, false))
227 return mod->driver->retain(handle);
230 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
232 auto mod = (struct gralloc0_module const *)module;
233 return mod->driver->release(handle);
236 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
237 int l, int t, int w, int h, void **vaddr)
239 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
242 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
244 int32_t fence_fd, ret;
245 auto mod = (struct gralloc0_module const *)module;
246 ret = mod->driver->unlock(handle, &fence_fd);
250 ret = cros_gralloc_sync_wait(fence_fd);
257 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
260 int32_t *out_format, ret;
262 buffer_handle_t handle;
263 uint32_t *out_width, *out_height, *out_stride;
264 auto mod = (struct gralloc0_module const *)module;
267 case GRALLOC_DRM_GET_STRIDE:
268 case GRALLOC_DRM_GET_FORMAT:
269 case GRALLOC_DRM_GET_DIMENSIONS:
270 case GRALLOC_DRM_GET_BACKING_STORE:
279 handle = va_arg(args, buffer_handle_t);
280 auto hnd = cros_gralloc_convert_handle(handle);
282 drv_log("Invalid handle.\n");
287 case GRALLOC_DRM_GET_STRIDE:
288 out_stride = va_arg(args, uint32_t *);
289 *out_stride = hnd->pixel_stride;
291 case GRALLOC_DRM_GET_FORMAT:
292 out_format = va_arg(args, int32_t *);
293 *out_format = hnd->droid_format;
295 case GRALLOC_DRM_GET_DIMENSIONS:
296 out_width = va_arg(args, uint32_t *);
297 out_height = va_arg(args, uint32_t *);
298 *out_width = hnd->width;
299 *out_height = hnd->height;
301 case GRALLOC_DRM_GET_BACKING_STORE:
302 out_store = va_arg(args, uint64_t *);
303 ret = mod->driver->get_backing_store(handle, out_store);
314 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
315 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
317 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
320 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
321 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
325 uint8_t *addr[DRV_MAX_PLANES];
326 auto mod = (struct gralloc0_module const *)module;
327 struct rectangle rect = { .x = static_cast<uint32_t>(l),
328 .y = static_cast<uint32_t>(t),
329 .width = static_cast<uint32_t>(w),
330 .height = static_cast<uint32_t>(h) };
332 auto hnd = cros_gralloc_convert_handle(handle);
334 drv_log("Invalid handle.\n");
338 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
339 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
348 map_flags = gralloc0_convert_map_usage(usage);
349 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
354 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
357 auto mod = (struct gralloc0_module const *)module;
358 return mod->driver->unlock(handle, fence_fd);
361 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
362 int usage, int l, int t, int w, int h,
363 struct android_ycbcr *ycbcr, int fence_fd)
367 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
368 auto mod = (struct gralloc0_module const *)module;
369 struct rectangle rect = { .x = static_cast<uint32_t>(l),
370 .y = static_cast<uint32_t>(t),
371 .width = static_cast<uint32_t>(w),
372 .height = static_cast<uint32_t>(h) };
374 auto hnd = cros_gralloc_convert_handle(handle);
376 drv_log("Invalid handle.\n");
380 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
381 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
382 drv_log("Non-YUV format not compatible.\n");
391 map_flags = gralloc0_convert_map_usage(usage);
392 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
396 switch (hnd->format) {
397 case DRM_FORMAT_NV12:
400 ycbcr->cr = addr[1] + 1;
401 ycbcr->ystride = hnd->strides[0];
402 ycbcr->cstride = hnd->strides[1];
403 ycbcr->chroma_step = 2;
405 case DRM_FORMAT_YVU420:
406 case DRM_FORMAT_YVU420_ANDROID:
410 ycbcr->ystride = hnd->strides[0];
411 ycbcr->cstride = hnd->strides[1];
412 ycbcr->chroma_step = 1;
415 module->unlock(module, handle);
423 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
426 struct gralloc0_module HAL_MODULE_INFO_SYM = {
431 .tag = HARDWARE_MODULE_TAG,
432 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
433 .hal_api_version = 0,
434 .id = GRALLOC_HARDWARE_MODULE_ID,
435 .name = "CrOS Gralloc",
436 .author = "Chrome OS",
437 .methods = &gralloc0_module_methods,
440 .registerBuffer = gralloc0_register_buffer,
441 .unregisterBuffer = gralloc0_unregister_buffer,
442 .lock = gralloc0_lock,
443 .unlock = gralloc0_unlock,
444 .perform = gralloc0_perform,
445 .lock_ycbcr = gralloc0_lock_ycbcr,
446 .lockAsync = gralloc0_lock_async,
447 .unlockAsync = gralloc0_unlock_async,
448 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
453 .initialized = false,