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 int64_t gralloc0_convert_flags(int flags)
37 uint64_t usage = BO_USE_NONE;
39 if (flags & GRALLOC_USAGE_CURSOR)
41 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
42 usage |= BO_USE_SW_READ_RARELY;
43 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
44 usage |= BO_USE_SW_READ_OFTEN;
45 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
46 usage |= BO_USE_SW_WRITE_RARELY;
47 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
48 usage |= BO_USE_SW_WRITE_OFTEN;
49 if (flags & GRALLOC_USAGE_HW_TEXTURE)
50 usage |= BO_USE_TEXTURE;
51 if (flags & GRALLOC_USAGE_HW_RENDER)
52 usage |= BO_USE_RENDERING;
53 if (flags & GRALLOC_USAGE_HW_2D)
54 usage |= BO_USE_RENDERING;
55 if (flags & GRALLOC_USAGE_HW_COMPOSER)
56 /* HWC wants to use display hardware, but can defer to OpenGL. */
57 usage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
58 if (flags & GRALLOC_USAGE_HW_FB)
60 if (flags & 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.
66 if (flags & GRALLOC_USAGE_PROTECTED)
67 usage |= BO_USE_PROTECTED;
68 if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
69 /*HACK: See b/30054495 */
70 usage |= BO_USE_SW_READ_OFTEN;
71 if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
72 usage |= BO_USE_CAMERA_WRITE;
73 if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
74 usage |= BO_USE_CAMERA_READ;
75 if (flags & GRALLOC_USAGE_RENDERSCRIPT)
76 usage |= BO_USE_RENDERSCRIPT;
81 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
82 buffer_handle_t *handle, int *stride)
86 struct cros_gralloc_buffer_descriptor descriptor;
87 auto mod = (struct gralloc0_module *)dev->common.module;
90 descriptor.height = h;
91 descriptor.droid_format = format;
92 descriptor.producer_usage = descriptor.consumer_usage = usage;
93 descriptor.drm_format = cros_gralloc_convert_format(format);
94 descriptor.drv_usage = gralloc0_convert_flags(usage);
96 supported = mod->driver->is_supported(&descriptor);
97 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
98 descriptor.drv_usage &= ~BO_USE_SCANOUT;
99 supported = mod->driver->is_supported(&descriptor);
103 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, "
104 "drv_format: %4.4s, drv_flags: %llu",
105 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
106 static_cast<unsigned long long>(descriptor.drv_usage));
110 ret = mod->driver->allocate(&descriptor, handle);
114 auto hnd = cros_gralloc_convert_handle(*handle);
115 *stride = hnd->pixel_stride;
120 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
122 auto mod = (struct gralloc0_module *)dev->common.module;
123 return mod->driver->release(handle);
126 static int gralloc0_close(struct hw_device_t *dev)
128 /* Memory is freed by managed pointers on process close. */
132 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
134 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
136 if (mod->initialized)
139 mod->driver = std::make_unique<cros_gralloc_driver>();
140 if (mod->driver->init()) {
141 cros_gralloc_error("Failed to initialize driver.");
145 if (initialize_alloc) {
146 mod->alloc = std::make_unique<alloc_device_t>();
147 mod->alloc->alloc = gralloc0_alloc;
148 mod->alloc->free = gralloc0_free;
149 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
150 mod->alloc->common.version = 0;
151 mod->alloc->common.module = (hw_module_t *)mod;
152 mod->alloc->common.close = gralloc0_close;
155 mod->initialized = true;
159 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
161 auto module = (struct gralloc0_module *)mod;
163 if (module->initialized) {
164 *dev = &module->alloc->common;
168 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
169 cros_gralloc_error("Incorrect device name - %s.", name);
173 if (gralloc0_init(module, true))
176 *dev = &module->alloc->common;
180 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
182 auto mod = (struct gralloc0_module *)module;
184 if (!mod->initialized)
185 if (gralloc0_init(mod, false))
188 return mod->driver->retain(handle);
191 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
193 auto mod = (struct gralloc0_module *)module;
194 return mod->driver->release(handle);
197 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
198 int l, int t, int w, int h, void **vaddr)
200 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
203 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
205 int32_t fence_fd, ret;
206 auto mod = (struct gralloc0_module *)module;
207 ret = mod->driver->unlock(handle, &fence_fd);
211 ret = cros_gralloc_sync_wait(fence_fd);
218 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
221 int32_t *out_format, ret;
223 buffer_handle_t handle;
224 uint32_t *out_width, *out_height, *out_stride;
225 auto mod = (struct gralloc0_module *)module;
228 case GRALLOC_DRM_GET_STRIDE:
229 case GRALLOC_DRM_GET_FORMAT:
230 case GRALLOC_DRM_GET_DIMENSIONS:
231 case GRALLOC_DRM_GET_BACKING_STORE:
240 handle = va_arg(args, buffer_handle_t);
241 auto hnd = cros_gralloc_convert_handle(handle);
243 cros_gralloc_error("Invalid handle.");
248 case GRALLOC_DRM_GET_STRIDE:
249 out_stride = va_arg(args, uint32_t *);
250 *out_stride = hnd->pixel_stride;
252 case GRALLOC_DRM_GET_FORMAT:
253 out_format = va_arg(args, int32_t *);
254 *out_format = hnd->droid_format;
256 case GRALLOC_DRM_GET_DIMENSIONS:
257 out_width = va_arg(args, uint32_t *);
258 out_height = va_arg(args, uint32_t *);
259 *out_width = hnd->width;
260 *out_height = hnd->height;
262 case GRALLOC_DRM_GET_BACKING_STORE:
263 out_store = va_arg(args, uint64_t *);
264 ret = mod->driver->get_backing_store(handle, out_store);
275 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
276 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
278 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
281 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
282 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
286 uint8_t *addr[DRV_MAX_PLANES];
287 auto mod = (struct gralloc0_module *)module;
289 auto hnd = cros_gralloc_convert_handle(handle);
291 cros_gralloc_error("Invalid handle.");
295 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
296 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
300 flags = gralloc0_convert_flags(usage);
301 ret = mod->driver->lock(handle, fence_fd, flags, addr);
306 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
309 auto mod = (struct gralloc0_module *)module;
310 return mod->driver->unlock(handle, fence_fd);
313 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
314 int usage, int l, int t, int w, int h,
315 struct android_ycbcr *ycbcr, int fence_fd)
319 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
320 auto mod = (struct gralloc0_module *)module;
322 auto hnd = cros_gralloc_convert_handle(handle);
324 cros_gralloc_error("Invalid handle.");
328 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
329 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
330 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
331 cros_gralloc_error("Non-YUV format not compatible.");
335 flags = gralloc0_convert_flags(usage);
336 ret = mod->driver->lock(handle, fence_fd, flags, addr);
340 switch (hnd->format) {
341 case DRM_FORMAT_NV12:
344 ycbcr->cr = addr[1] + 1;
345 ycbcr->ystride = hnd->strides[0];
346 ycbcr->cstride = hnd->strides[1];
347 ycbcr->chroma_step = 2;
349 case DRM_FORMAT_YVU420:
350 case DRM_FORMAT_YVU420_ANDROID:
354 ycbcr->ystride = hnd->strides[0];
355 ycbcr->cstride = hnd->strides[1];
356 ycbcr->chroma_step = 1;
359 module->unlock(module, handle);
366 static struct hw_module_methods_t gralloc0_module_methods = {.open = gralloc0_open };
368 struct gralloc0_module HAL_MODULE_INFO_SYM = {
373 .tag = HARDWARE_MODULE_TAG,
374 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
375 .hal_api_version = 0,
376 .id = GRALLOC_HARDWARE_MODULE_ID,
377 .name = "CrOS Gralloc",
378 .author = "Chrome OS",
379 .methods = &gralloc0_module_methods,
382 .registerBuffer = gralloc0_register_buffer,
383 .unregisterBuffer = gralloc0_unregister_buffer,
384 .lock = gralloc0_lock,
385 .unlock = gralloc0_unlock,
386 .perform = gralloc0_perform,
387 .lock_ycbcr = gralloc0_lock_ycbcr,
388 .lockAsync = gralloc0_lock_async,
389 .unlockAsync = gralloc0_unlock_async,
390 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
395 .initialized = false,