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>
13 #include "../i915_private_android.h"
14 #include "../i915_private_android_types.h"
16 struct gralloc0_module {
17 gralloc_module_t base;
18 std::unique_ptr<alloc_device_t> alloc;
19 std::unique_ptr<cros_gralloc_driver> driver;
21 SpinLock initialization_mutex;
24 /* This enumeration must match the one in <gralloc_drm.h>.
25 * The functions supported by this gralloc's temporary private API are listed
26 * below. Use of these functions is highly discouraged and should only be
27 * reserved for cases where no alternative to get same information (such as
28 * querying ANativeWindow) exists.
32 GRALLOC_DRM_GET_STRIDE,
33 GRALLOC_DRM_GET_FORMAT,
34 GRALLOC_DRM_GET_DIMENSIONS,
35 GRALLOC_DRM_GET_BACKING_STORE,
39 static uint64_t gralloc0_convert_usage(int usage)
41 uint64_t use_flags = BO_USE_NONE;
43 if (usage & GRALLOC_USAGE_CURSOR)
44 use_flags |= BO_USE_CURSOR;
45 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
46 use_flags |= BO_USE_SW_READ_RARELY;
47 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
48 use_flags |= BO_USE_SW_READ_OFTEN;
49 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
50 use_flags |= BO_USE_SW_WRITE_RARELY;
51 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
52 use_flags |= BO_USE_SW_WRITE_OFTEN;
53 if (usage & GRALLOC_USAGE_HW_TEXTURE)
54 use_flags |= BO_USE_TEXTURE;
55 if (usage & GRALLOC_USAGE_HW_RENDER)
56 use_flags |= BO_USE_RENDERING;
57 if (usage & GRALLOC_USAGE_HW_2D)
58 use_flags |= BO_USE_RENDERING;
59 if (usage & GRALLOC_USAGE_HW_COMPOSER)
60 /* HWC wants to use display hardware, but can defer to OpenGL. */
61 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
62 if (usage & GRALLOC_USAGE_HW_FB)
63 use_flags |= BO_USE_NONE;
64 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
66 * This flag potentially covers external display for the normal drivers (i915,
67 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
69 use_flags |= BO_USE_NONE;
70 if (usage & GRALLOC_USAGE_PROTECTED)
71 use_flags |= BO_USE_PROTECTED;
72 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
73 /*HACK: See b/30054495 */
74 use_flags |= BO_USE_SW_READ_OFTEN;
75 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
76 use_flags |= BO_USE_CAMERA_WRITE;
77 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
78 use_flags |= BO_USE_CAMERA_READ;
79 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
80 use_flags |= BO_USE_RENDERSCRIPT;
85 static uint32_t gralloc0_convert_map_usage(int map_usage)
87 uint32_t map_flags = BO_MAP_NONE;
89 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
90 map_flags |= BO_MAP_READ;
91 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
92 map_flags |= BO_MAP_WRITE;
97 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
98 buffer_handle_t *handle, int *stride)
102 struct cros_gralloc_buffer_descriptor descriptor;
103 auto mod = (struct gralloc0_module *)dev->common.module;
105 descriptor.width = w;
106 descriptor.height = h;
107 descriptor.droid_format = format;
108 descriptor.producer_usage = descriptor.consumer_usage = usage;
109 descriptor.modifier = 0;
110 descriptor.drm_format = cros_gralloc_convert_format(format);
111 descriptor.use_flags = gralloc0_convert_usage(usage);
113 supported = mod->driver->is_supported(&descriptor);
114 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
115 descriptor.use_flags &= ~BO_USE_SCANOUT;
116 supported = mod->driver->is_supported(&descriptor);
120 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL usage: %u, "
121 "drv_format: %4.4s, use_flags: %llu",
122 format, usage, drmFormat2Str(descriptor.drm_format),
123 static_cast<unsigned long long>(descriptor.use_flags));
127 ret = mod->driver->allocate(&descriptor, handle);
131 auto hnd = cros_gralloc_convert_handle(*handle);
132 *stride = hnd->pixel_stride;
137 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
139 auto mod = (struct gralloc0_module *)dev->common.module;
140 return mod->driver->release(handle);
143 static int gralloc0_close(struct hw_device_t *dev)
145 /* Memory is freed by managed pointers on process close. */
149 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
151 SCOPED_SPIN_LOCK(mod->initialization_mutex);
153 if (mod->initialized)
156 mod->driver = std::make_unique<cros_gralloc_driver>();
157 if (mod->driver->init()) {
158 cros_gralloc_error("Failed to initialize driver.");
162 if (initialize_alloc) {
163 mod->alloc = std::make_unique<alloc_device_t>();
164 mod->alloc->alloc = gralloc0_alloc;
165 mod->alloc->free = gralloc0_free;
166 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
167 mod->alloc->common.version = 0;
168 mod->alloc->common.module = (hw_module_t *)mod;
169 mod->alloc->common.close = gralloc0_close;
172 mod->initialized = true;
176 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
178 auto module = (struct gralloc0_module *)mod;
180 if (module->initialized) {
181 *dev = &module->alloc->common;
185 /* On Android M, Surfaceflinger tries to open the gralloc device
186 * using name GRALLOC_HARDWARE_FB0.
188 if ((strcmp(name, GRALLOC_HARDWARE_GPU0)!=0) && (strcmp(name, GRALLOC_HARDWARE_FB0)!=0)) {
189 cros_gralloc_error("Incorrect device name - %s.", name);
193 if (gralloc0_init(module, true))
196 *dev = &module->alloc->common;
200 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
202 auto mod = (struct gralloc0_module *)module;
204 if (!mod->initialized)
205 if (gralloc0_init(mod, true))
208 return mod->driver->retain(handle);
211 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
213 auto mod = (struct gralloc0_module *)module;
214 return mod->driver->release(handle);
217 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
218 int l, int t, int w, int h, void **vaddr)
220 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
223 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
225 int32_t fence_fd, ret;
226 auto mod = (struct gralloc0_module *)module;
227 ret = mod->driver->unlock(handle, &fence_fd);
231 ret = cros_gralloc_sync_wait(fence_fd);
238 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
241 int32_t *out_format, ret;
243 buffer_handle_t handle;
244 uint32_t *out_width, *out_height, *out_stride;
245 auto mod = (struct gralloc0_module *)module;
248 case GRALLOC_DRM_GET_STRIDE:
249 case GRALLOC_DRM_GET_FORMAT:
250 case GRALLOC_DRM_GET_DIMENSIONS:
251 case GRALLOC_DRM_GET_BACKING_STORE:
260 handle = va_arg(args, buffer_handle_t);
261 auto hnd = cros_gralloc_convert_handle(handle);
263 cros_gralloc_error("Invalid handle.");
268 case GRALLOC_DRM_GET_STRIDE:
269 out_stride = va_arg(args, uint32_t *);
270 *out_stride = hnd->pixel_stride;
272 case GRALLOC_DRM_GET_FORMAT:
273 out_format = va_arg(args, int32_t *);
274 *out_format = hnd->droid_format;
276 case GRALLOC_DRM_GET_DIMENSIONS:
277 out_width = va_arg(args, uint32_t *);
278 out_height = va_arg(args, uint32_t *);
279 *out_width = hnd->width;
280 *out_height = hnd->height;
282 case GRALLOC_DRM_GET_BACKING_STORE:
283 out_store = va_arg(args, uint64_t *);
284 ret = mod->driver->get_backing_store(handle, out_store);
295 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
296 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
298 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
301 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
302 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
306 uint8_t *addr[DRV_MAX_PLANES];
307 auto mod = (struct gralloc0_module *)module;
309 auto hnd = cros_gralloc_convert_handle(handle);
311 cros_gralloc_error("Invalid handle.");
315 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
316 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
320 map_flags = gralloc0_convert_map_usage(usage);
321 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
326 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
329 auto mod = (struct gralloc0_module *)module;
330 return mod->driver->unlock(handle, fence_fd);
333 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
334 int usage, int l, int t, int w, int h,
335 struct android_ycbcr *ycbcr, int fence_fd)
339 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
340 auto mod = (struct gralloc0_module *)module;
342 auto hnd = cros_gralloc_convert_handle(handle);
344 cros_gralloc_error("Invalid handle.");
348 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
349 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
350 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) &&
351 !i915_private_supported_yuv_format(hnd->droid_format)) {
352 cros_gralloc_error("Non-YUV format not compatible.");
356 map_flags = gralloc0_convert_map_usage(usage);
357 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
361 switch (hnd->format) {
362 case DRM_FORMAT_NV12:
363 case DRM_FORMAT_NV12_Y_TILED_INTEL:
366 ycbcr->cr = addr[1] + 1;
367 ycbcr->ystride = hnd->strides[0];
368 ycbcr->cstride = hnd->strides[1];
369 ycbcr->chroma_step = 2;
371 case DRM_FORMAT_YVU420:
372 case DRM_FORMAT_YVU420_ANDROID:
376 ycbcr->ystride = hnd->strides[0];
377 ycbcr->cstride = hnd->strides[1];
378 ycbcr->chroma_step = 1;
381 module->unlock(module, handle);
389 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
392 struct gralloc0_module HAL_MODULE_INFO_SYM = {
397 .tag = HARDWARE_MODULE_TAG,
398 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
399 .hal_api_version = 0,
400 .id = GRALLOC_HARDWARE_MODULE_ID,
401 .name = "CrOS Gralloc",
402 .author = "Chrome OS",
403 .methods = &gralloc0_module_methods,
406 .registerBuffer = gralloc0_register_buffer,
407 .unregisterBuffer = gralloc0_unregister_buffer,
408 .lock = gralloc0_lock,
409 .unlock = gralloc0_unlock,
410 .perform = gralloc0_perform,
411 .lock_ycbcr = gralloc0_lock_ycbcr,
412 .lockAsync = gralloc0_lock_async,
413 .unlockAsync = gralloc0_unlock_async,
414 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
419 .initialized = false,