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 static uint64_t gralloc0_convert_usage(int usage)
38 uint64_t use_flags = BO_USE_NONE;
40 if (usage & GRALLOC_USAGE_CURSOR)
41 use_flags |= BO_USE_NONE;
42 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
43 use_flags |= BO_USE_SW_READ_RARELY;
44 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
45 use_flags |= BO_USE_SW_READ_OFTEN;
46 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
47 use_flags |= BO_USE_SW_WRITE_RARELY;
48 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
49 use_flags |= BO_USE_SW_WRITE_OFTEN;
50 if (usage & GRALLOC_USAGE_HW_TEXTURE)
51 use_flags |= BO_USE_TEXTURE;
52 if (usage & GRALLOC_USAGE_HW_RENDER)
53 use_flags |= BO_USE_RENDERING;
54 if (usage & GRALLOC_USAGE_HW_2D)
55 use_flags |= BO_USE_RENDERING;
56 if (usage & GRALLOC_USAGE_HW_COMPOSER)
57 /* HWC wants to use display hardware, but can defer to OpenGL. */
58 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
59 if (usage & GRALLOC_USAGE_HW_FB)
60 use_flags |= BO_USE_NONE;
61 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
63 * This flag potentially covers external display for the normal drivers (i915,
64 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
66 use_flags |= BO_USE_NONE;
67 if (usage & GRALLOC_USAGE_PROTECTED)
68 use_flags |= BO_USE_PROTECTED;
69 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
70 /*HACK: See b/30054495 */
71 use_flags |= BO_USE_SW_READ_OFTEN;
72 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
73 use_flags |= BO_USE_CAMERA_WRITE;
74 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
75 use_flags |= BO_USE_CAMERA_READ;
76 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
77 use_flags |= BO_USE_RENDERSCRIPT;
82 static uint32_t gralloc0_convert_map_usage(int map_usage)
84 uint32_t map_flags = BO_MAP_NONE;
86 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
87 map_flags |= BO_MAP_READ;
88 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
89 map_flags |= BO_MAP_WRITE;
94 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
95 buffer_handle_t *handle, int *stride)
99 struct cros_gralloc_buffer_descriptor descriptor;
100 auto mod = (struct gralloc0_module const *)dev->common.module;
102 descriptor.width = w;
103 descriptor.height = h;
104 descriptor.droid_format = format;
105 descriptor.producer_usage = descriptor.consumer_usage = usage;
106 descriptor.drm_format = cros_gralloc_convert_format(format);
107 descriptor.use_flags = gralloc0_convert_usage(usage);
109 supported = mod->driver->is_supported(&descriptor);
110 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
111 descriptor.use_flags &= ~BO_USE_SCANOUT;
112 supported = mod->driver->is_supported(&descriptor);
116 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
117 "drv_format: %4.4s, use_flags: %llu\n",
118 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
119 static_cast<unsigned long long>(descriptor.use_flags));
123 ret = mod->driver->allocate(&descriptor, handle);
127 auto hnd = cros_gralloc_convert_handle(*handle);
128 *stride = hnd->pixel_stride;
133 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
135 auto mod = (struct gralloc0_module const *)dev->common.module;
136 return mod->driver->release(handle);
139 static int gralloc0_close(struct hw_device_t *dev)
141 /* Memory is freed by managed pointers on process close. */
145 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
147 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
149 if (mod->initialized)
152 mod->driver = std::make_unique<cros_gralloc_driver>();
153 if (mod->driver->init()) {
154 drv_log("Failed to initialize driver.\n");
158 if (initialize_alloc) {
159 mod->alloc = std::make_unique<alloc_device_t>();
160 mod->alloc->alloc = gralloc0_alloc;
161 mod->alloc->free = gralloc0_free;
162 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
163 mod->alloc->common.version = 0;
164 mod->alloc->common.module = (hw_module_t *)mod;
165 mod->alloc->common.close = gralloc0_close;
168 mod->initialized = true;
172 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
174 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
175 auto module = const_cast<struct gralloc0_module *>(const_module);
177 if (module->initialized) {
178 *dev = &module->alloc->common;
182 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
183 drv_log("Incorrect device name - %s.\n", name);
187 if (gralloc0_init(module, true))
190 *dev = &module->alloc->common;
194 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
196 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
197 auto mod = const_cast<struct gralloc0_module *>(const_module);
199 if (!mod->initialized)
200 if (gralloc0_init(mod, false))
203 return mod->driver->retain(handle);
206 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
208 auto mod = (struct gralloc0_module const *)module;
209 return mod->driver->release(handle);
212 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
213 int l, int t, int w, int h, void **vaddr)
215 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
218 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
220 int32_t fence_fd, ret;
221 auto mod = (struct gralloc0_module const *)module;
222 ret = mod->driver->unlock(handle, &fence_fd);
226 ret = cros_gralloc_sync_wait(fence_fd);
233 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
236 int32_t *out_format, ret;
238 buffer_handle_t handle;
239 uint32_t *out_width, *out_height, *out_stride;
240 auto mod = (struct gralloc0_module const *)module;
243 case GRALLOC_DRM_GET_STRIDE:
244 case GRALLOC_DRM_GET_FORMAT:
245 case GRALLOC_DRM_GET_DIMENSIONS:
246 case GRALLOC_DRM_GET_BACKING_STORE:
255 handle = va_arg(args, buffer_handle_t);
256 auto hnd = cros_gralloc_convert_handle(handle);
258 drv_log("Invalid handle.\n");
263 case GRALLOC_DRM_GET_STRIDE:
264 out_stride = va_arg(args, uint32_t *);
265 *out_stride = hnd->pixel_stride;
267 case GRALLOC_DRM_GET_FORMAT:
268 out_format = va_arg(args, int32_t *);
269 *out_format = hnd->droid_format;
271 case GRALLOC_DRM_GET_DIMENSIONS:
272 out_width = va_arg(args, uint32_t *);
273 out_height = va_arg(args, uint32_t *);
274 *out_width = hnd->width;
275 *out_height = hnd->height;
277 case GRALLOC_DRM_GET_BACKING_STORE:
278 out_store = va_arg(args, uint64_t *);
279 ret = mod->driver->get_backing_store(handle, out_store);
290 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
291 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
293 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
296 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
297 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
301 uint8_t *addr[DRV_MAX_PLANES];
302 auto mod = (struct gralloc0_module const *)module;
303 struct rectangle rect = { .x = static_cast<uint32_t>(l),
304 .y = static_cast<uint32_t>(t),
305 .width = static_cast<uint32_t>(w),
306 .height = static_cast<uint32_t>(h) };
308 auto hnd = cros_gralloc_convert_handle(handle);
310 drv_log("Invalid handle.\n");
314 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
315 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
324 map_flags = gralloc0_convert_map_usage(usage);
325 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
330 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
333 auto mod = (struct gralloc0_module const *)module;
334 return mod->driver->unlock(handle, fence_fd);
337 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
338 int usage, int l, int t, int w, int h,
339 struct android_ycbcr *ycbcr, int fence_fd)
343 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
344 auto mod = (struct gralloc0_module const *)module;
345 struct rectangle rect = { .x = static_cast<uint32_t>(l),
346 .y = static_cast<uint32_t>(t),
347 .width = static_cast<uint32_t>(w),
348 .height = static_cast<uint32_t>(h) };
350 auto hnd = cros_gralloc_convert_handle(handle);
352 drv_log("Invalid handle.\n");
356 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
357 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
358 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
359 drv_log("Non-YUV format not compatible.\n");
368 map_flags = gralloc0_convert_map_usage(usage);
369 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
373 switch (hnd->format) {
374 case DRM_FORMAT_NV12:
377 ycbcr->cr = addr[1] + 1;
378 ycbcr->ystride = hnd->strides[0];
379 ycbcr->cstride = hnd->strides[1];
380 ycbcr->chroma_step = 2;
382 case DRM_FORMAT_YVU420:
383 case DRM_FORMAT_YVU420_ANDROID:
387 ycbcr->ystride = hnd->strides[0];
388 ycbcr->cstride = hnd->strides[1];
389 ycbcr->chroma_step = 1;
392 module->unlock(module, handle);
400 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
403 struct gralloc0_module HAL_MODULE_INFO_SYM = {
408 .tag = HARDWARE_MODULE_TAG,
409 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
410 .hal_api_version = 0,
411 .id = GRALLOC_HARDWARE_MODULE_ID,
412 .name = "CrOS Gralloc",
413 .author = "Chrome OS",
414 .methods = &gralloc0_module_methods,
417 .registerBuffer = gralloc0_register_buffer,
418 .unregisterBuffer = gralloc0_unregister_buffer,
419 .lock = gralloc0_lock,
420 .unlock = gralloc0_unlock,
421 .perform = gralloc0_perform,
422 .lock_ycbcr = gralloc0_lock_ycbcr,
423 .lockAsync = gralloc0_lock_async,
424 .unlockAsync = gralloc0_unlock_async,
425 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
430 .initialized = false,