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"
9 #include <hardware/gralloc.h>
12 struct gralloc0_module {
13 gralloc_module_t base;
14 std::unique_ptr<alloc_device_t> alloc;
15 std::unique_ptr<cros_gralloc_driver> driver;
17 std::mutex initialization_mutex;
20 /* This enumeration must match the one in <gralloc_drm.h>.
21 * The functions supported by this gralloc's temporary private API are listed
22 * below. Use of these functions is highly discouraged and should only be
23 * reserved for cases where no alternative to get same information (such as
24 * querying ANativeWindow) exists.
28 GRALLOC_DRM_GET_STRIDE,
29 GRALLOC_DRM_GET_FORMAT,
30 GRALLOC_DRM_GET_DIMENSIONS,
31 GRALLOC_DRM_GET_BACKING_STORE,
35 static uint64_t gralloc0_convert_usage(int usage)
37 uint64_t use_flags = BO_USE_NONE;
39 if (usage & GRALLOC_USAGE_CURSOR)
40 use_flags |= BO_USE_NONE;
41 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
42 use_flags |= BO_USE_SW_READ_RARELY;
43 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
44 use_flags |= BO_USE_SW_READ_OFTEN;
45 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
46 use_flags |= BO_USE_SW_WRITE_RARELY;
47 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
48 use_flags |= BO_USE_SW_WRITE_OFTEN;
49 if (usage & GRALLOC_USAGE_HW_TEXTURE)
50 use_flags |= BO_USE_TEXTURE;
51 if (usage & GRALLOC_USAGE_HW_RENDER)
52 use_flags |= BO_USE_RENDERING;
53 if (usage & GRALLOC_USAGE_HW_2D)
54 use_flags |= BO_USE_RENDERING;
55 if (usage & GRALLOC_USAGE_HW_COMPOSER)
56 /* HWC wants to use display hardware, but can defer to OpenGL. */
57 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
58 if (usage & GRALLOC_USAGE_HW_FB)
59 use_flags |= BO_USE_NONE;
60 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
62 * This flag potentially covers external display for the normal drivers (i915,
63 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
65 use_flags |= BO_USE_NONE;
66 if (usage & GRALLOC_USAGE_PROTECTED)
67 use_flags |= BO_USE_PROTECTED;
68 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
69 /*HACK: See b/30054495 */
70 use_flags |= BO_USE_SW_READ_OFTEN;
71 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
72 use_flags |= BO_USE_CAMERA_WRITE;
73 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
74 use_flags |= BO_USE_CAMERA_READ;
75 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
76 use_flags |= BO_USE_RENDERSCRIPT;
81 static uint32_t gralloc0_convert_map_usage(int map_usage)
83 uint32_t map_flags = BO_MAP_NONE;
85 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
86 map_flags |= BO_MAP_READ;
87 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
88 map_flags |= BO_MAP_WRITE;
93 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
94 buffer_handle_t *handle, int *stride)
98 struct cros_gralloc_buffer_descriptor descriptor;
99 auto mod = (struct gralloc0_module *)dev->common.module;
101 descriptor.width = w;
102 descriptor.height = h;
103 descriptor.droid_format = format;
104 descriptor.producer_usage = descriptor.consumer_usage = usage;
105 descriptor.drm_format = cros_gralloc_convert_format(format);
106 descriptor.use_flags = gralloc0_convert_usage(usage);
108 supported = mod->driver->is_supported(&descriptor);
109 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
110 descriptor.use_flags &= ~BO_USE_SCANOUT;
111 supported = mod->driver->is_supported(&descriptor);
115 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL usage: %u, "
116 "drv_format: %4.4s, use_flags: %llu",
117 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
118 static_cast<unsigned long long>(descriptor.use_flags));
122 ret = mod->driver->allocate(&descriptor, handle);
126 auto hnd = cros_gralloc_convert_handle(*handle);
127 *stride = hnd->pixel_stride;
132 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
134 auto mod = (struct gralloc0_module *)dev->common.module;
135 return mod->driver->release(handle);
138 static int gralloc0_close(struct hw_device_t *dev)
140 /* Memory is freed by managed pointers on process close. */
144 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
146 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
148 if (mod->initialized)
151 mod->driver = std::make_unique<cros_gralloc_driver>();
152 if (mod->driver->init()) {
153 cros_gralloc_error("Failed to initialize driver.");
157 if (initialize_alloc) {
158 mod->alloc = std::make_unique<alloc_device_t>();
159 mod->alloc->alloc = gralloc0_alloc;
160 mod->alloc->free = gralloc0_free;
161 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
162 mod->alloc->common.version = 0;
163 mod->alloc->common.module = (hw_module_t *)mod;
164 mod->alloc->common.close = gralloc0_close;
167 mod->initialized = true;
171 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
173 auto module = (struct gralloc0_module *)mod;
175 if (module->initialized) {
176 *dev = &module->alloc->common;
180 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
181 cros_gralloc_error("Incorrect device name - %s.", name);
185 if (gralloc0_init(module, true))
188 *dev = &module->alloc->common;
192 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
194 auto mod = (struct gralloc0_module *)module;
196 if (!mod->initialized)
197 if (gralloc0_init(mod, false))
200 return mod->driver->retain(handle);
203 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
205 auto mod = (struct gralloc0_module *)module;
206 return mod->driver->release(handle);
209 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
210 int l, int t, int w, int h, void **vaddr)
212 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
215 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
217 int32_t fence_fd, ret;
218 auto mod = (struct gralloc0_module *)module;
219 ret = mod->driver->unlock(handle, &fence_fd);
223 ret = cros_gralloc_sync_wait(fence_fd);
230 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
233 int32_t *out_format, ret;
235 buffer_handle_t handle;
236 uint32_t *out_width, *out_height, *out_stride;
237 auto mod = (struct gralloc0_module *)module;
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:
252 handle = va_arg(args, buffer_handle_t);
253 auto hnd = cros_gralloc_convert_handle(handle);
255 cros_gralloc_error("Invalid handle.");
260 case GRALLOC_DRM_GET_STRIDE:
261 out_stride = va_arg(args, uint32_t *);
262 *out_stride = hnd->pixel_stride;
264 case GRALLOC_DRM_GET_FORMAT:
265 out_format = va_arg(args, int32_t *);
266 *out_format = hnd->droid_format;
268 case GRALLOC_DRM_GET_DIMENSIONS:
269 out_width = va_arg(args, uint32_t *);
270 out_height = va_arg(args, uint32_t *);
271 *out_width = hnd->width;
272 *out_height = hnd->height;
274 case GRALLOC_DRM_GET_BACKING_STORE:
275 out_store = va_arg(args, uint64_t *);
276 ret = mod->driver->get_backing_store(handle, out_store);
287 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
288 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
290 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
293 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
294 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
298 uint8_t *addr[DRV_MAX_PLANES];
299 auto mod = (struct gralloc0_module *)module;
301 auto hnd = cros_gralloc_convert_handle(handle);
303 cros_gralloc_error("Invalid handle.");
307 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
308 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
312 map_flags = gralloc0_convert_map_usage(usage);
313 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
318 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
321 auto mod = (struct gralloc0_module *)module;
322 return mod->driver->unlock(handle, fence_fd);
325 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
326 int usage, int l, int t, int w, int h,
327 struct android_ycbcr *ycbcr, int fence_fd)
331 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
332 auto mod = (struct gralloc0_module *)module;
334 auto hnd = cros_gralloc_convert_handle(handle);
336 cros_gralloc_error("Invalid handle.");
340 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
341 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
342 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
343 cros_gralloc_error("Non-YUV format not compatible.");
347 map_flags = gralloc0_convert_map_usage(usage);
348 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
352 switch (hnd->format) {
353 case DRM_FORMAT_NV12:
356 ycbcr->cr = addr[1] + 1;
357 ycbcr->ystride = hnd->strides[0];
358 ycbcr->cstride = hnd->strides[1];
359 ycbcr->chroma_step = 2;
361 case DRM_FORMAT_YVU420:
362 case DRM_FORMAT_YVU420_ANDROID:
366 ycbcr->ystride = hnd->strides[0];
367 ycbcr->cstride = hnd->strides[1];
368 ycbcr->chroma_step = 1;
371 module->unlock(module, handle);
379 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
382 struct gralloc0_module HAL_MODULE_INFO_SYM = {
387 .tag = HARDWARE_MODULE_TAG,
388 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
389 .hal_api_version = 0,
390 .id = GRALLOC_HARDWARE_MODULE_ID,
391 .name = "CrOS Gralloc",
392 .author = "Chrome OS",
393 .methods = &gralloc0_module_methods,
396 .registerBuffer = gralloc0_register_buffer,
397 .unregisterBuffer = gralloc0_unregister_buffer,
398 .lock = gralloc0_lock,
399 .unlock = gralloc0_unlock,
400 .perform = gralloc0_perform,
401 .lock_ycbcr = gralloc0_lock_ycbcr,
402 .lockAsync = gralloc0_lock_async,
403 .unlockAsync = gralloc0_unlock_async,
404 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
409 .initialized = false,