1 /*
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.
5 */
6
7 #include "../../util.h"
8 #include "../cros_gralloc_driver.h"
9
10 #include <cassert>
11 #include <hardware/gralloc.h>
12 #include <memory.h>
13
14 struct gralloc0_module {
15 gralloc_module_t base;
16 std::unique_ptr<alloc_device_t> alloc;
17 std::unique_ptr<cros_gralloc_driver> driver;
18 bool initialized;
19 std::mutex initialization_mutex;
20 };
21
22 /* This enumeration must match the one in <gralloc_drm.h>.
23 * The functions supported by this gralloc's temporary private API are listed
24 * below. Use of these functions is highly discouraged and should only be
25 * reserved for cases where no alternative to get same information (such as
26 * querying ANativeWindow) exists.
27 */
28 // clang-format off
29 enum {
30 GRALLOC_DRM_GET_STRIDE,
31 GRALLOC_DRM_GET_FORMAT,
32 GRALLOC_DRM_GET_DIMENSIONS,
33 GRALLOC_DRM_GET_BACKING_STORE,
34 };
35 // clang-format on
36
37 // Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
38 // passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
39 // entirety, so we can detect the video decoder flag passed by IAllocator clients.
40 #define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
41
gralloc0_convert_usage(int usage)42 static uint64_t gralloc0_convert_usage(int usage)
43 {
44 uint64_t use_flags = BO_USE_NONE;
45
46 if (usage & GRALLOC_USAGE_CURSOR)
47 use_flags |= BO_USE_NONE;
48 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
49 use_flags |= BO_USE_SW_READ_RARELY;
50 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
51 use_flags |= BO_USE_SW_READ_OFTEN;
52 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
53 use_flags |= BO_USE_SW_WRITE_RARELY;
54 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
55 use_flags |= BO_USE_SW_WRITE_OFTEN;
56 if (usage & GRALLOC_USAGE_HW_TEXTURE)
57 use_flags |= BO_USE_TEXTURE;
58 if (usage & GRALLOC_USAGE_HW_RENDER)
59 use_flags |= BO_USE_RENDERING;
60 if (usage & GRALLOC_USAGE_HW_2D)
61 use_flags |= BO_USE_RENDERING;
62 if (usage & GRALLOC_USAGE_HW_COMPOSER)
63 /* HWC wants to use display hardware, but can defer to OpenGL. */
64 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
65 if (usage & GRALLOC_USAGE_HW_FB)
66 use_flags |= BO_USE_NONE;
67 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
68 /*
69 * This flag potentially covers external display for the normal drivers (i915,
70 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
71 * */
72 use_flags |= BO_USE_NONE;
73 if (usage & GRALLOC_USAGE_PROTECTED)
74 use_flags |= BO_USE_PROTECTED;
75 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
76 use_flags |= BO_USE_HW_VIDEO_ENCODER;
77 /*HACK: See b/30054495 */
78 use_flags |= BO_USE_SW_READ_OFTEN;
79 }
80 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
81 use_flags |= BO_USE_CAMERA_WRITE;
82 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
83 use_flags |= BO_USE_CAMERA_READ;
84 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
85 use_flags |= BO_USE_RENDERSCRIPT;
86 if (usage & BUFFER_USAGE_VIDEO_DECODER)
87 use_flags |= BO_USE_HW_VIDEO_DECODER;
88
89 return use_flags;
90 }
91
gralloc0_convert_map_usage(int map_usage)92 static uint32_t gralloc0_convert_map_usage(int map_usage)
93 {
94 uint32_t map_flags = BO_MAP_NONE;
95
96 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
97 map_flags |= BO_MAP_READ;
98 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
99 map_flags |= BO_MAP_WRITE;
100
101 return map_flags;
102 }
103
gralloc0_droid_yuv_format(int droid_format)104 static int gralloc0_droid_yuv_format(int droid_format)
105 {
106
107 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
108 droid_format == HAL_PIXEL_FORMAT_YV12);
109 }
110
gralloc0_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * handle,int * stride)111 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
112 buffer_handle_t *handle, int *stride)
113 {
114 int32_t ret;
115 bool supported;
116 struct cros_gralloc_buffer_descriptor descriptor;
117 auto mod = (struct gralloc0_module const *)dev->common.module;
118
119 descriptor.width = w;
120 descriptor.height = h;
121 descriptor.droid_format = format;
122 descriptor.droid_usage = usage;
123 descriptor.drm_format = cros_gralloc_convert_format(format);
124 descriptor.use_flags = gralloc0_convert_usage(usage);
125 descriptor.reserved_region_size = 0;
126
127 supported = mod->driver->is_supported(&descriptor);
128 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
129 descriptor.use_flags &= ~BO_USE_SCANOUT;
130 supported = mod->driver->is_supported(&descriptor);
131 }
132 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
133 !gralloc0_droid_yuv_format(format)) {
134 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
135 // because they are not input to a hw encoder but used as an
136 // intermediate format (e.g. camera).
137 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
138 supported = mod->driver->is_supported(&descriptor);
139 }
140
141 if (!supported) {
142 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
143 "drv_format: %4.4s, use_flags: %llu\n",
144 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
145 static_cast<unsigned long long>(descriptor.use_flags));
146 return -EINVAL;
147 }
148
149 ret = mod->driver->allocate(&descriptor, handle);
150 if (ret)
151 return ret;
152
153 auto hnd = cros_gralloc_convert_handle(*handle);
154 *stride = hnd->pixel_stride;
155
156 return 0;
157 }
158
gralloc0_free(alloc_device_t * dev,buffer_handle_t handle)159 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
160 {
161 auto mod = (struct gralloc0_module const *)dev->common.module;
162 return mod->driver->release(handle);
163 }
164
gralloc0_close(struct hw_device_t * dev)165 static int gralloc0_close(struct hw_device_t *dev)
166 {
167 /* Memory is freed by managed pointers on process close. */
168 return 0;
169 }
170
gralloc0_init(struct gralloc0_module * mod,bool initialize_alloc)171 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
172 {
173 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
174
175 if (mod->initialized)
176 return 0;
177
178 mod->driver = std::make_unique<cros_gralloc_driver>();
179 if (mod->driver->init()) {
180 drv_log("Failed to initialize driver.\n");
181 return -ENODEV;
182 }
183
184 if (initialize_alloc) {
185 mod->alloc = std::make_unique<alloc_device_t>();
186 mod->alloc->alloc = gralloc0_alloc;
187 mod->alloc->free = gralloc0_free;
188 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
189 mod->alloc->common.version = 0;
190 mod->alloc->common.module = (hw_module_t *)mod;
191 mod->alloc->common.close = gralloc0_close;
192 }
193
194 mod->initialized = true;
195 return 0;
196 }
197
gralloc0_open(const struct hw_module_t * mod,const char * name,struct hw_device_t ** dev)198 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
199 {
200 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
201 auto module = const_cast<struct gralloc0_module *>(const_module);
202
203 if (module->initialized) {
204 *dev = &module->alloc->common;
205 return 0;
206 }
207
208 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
209 drv_log("Incorrect device name - %s.\n", name);
210 return -EINVAL;
211 }
212
213 if (gralloc0_init(module, true))
214 return -ENODEV;
215
216 *dev = &module->alloc->common;
217 return 0;
218 }
219
gralloc0_register_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)220 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
221 {
222 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
223 auto mod = const_cast<struct gralloc0_module *>(const_module);
224
225 if (!mod->initialized)
226 if (gralloc0_init(mod, false))
227 return -ENODEV;
228
229 return mod->driver->retain(handle);
230 }
231
gralloc0_unregister_buffer(struct gralloc_module_t const * module,buffer_handle_t handle)232 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
233 {
234 auto mod = (struct gralloc0_module const *)module;
235 return mod->driver->release(handle);
236 }
237
gralloc0_lock(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr)238 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
239 int l, int t, int w, int h, void **vaddr)
240 {
241 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
242 }
243
gralloc0_unlock(struct gralloc_module_t const * module,buffer_handle_t handle)244 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
245 {
246 int32_t fence_fd, ret;
247 auto mod = (struct gralloc0_module const *)module;
248 ret = mod->driver->unlock(handle, &fence_fd);
249 if (ret)
250 return ret;
251
252 ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
253 if (ret)
254 return ret;
255
256 return 0;
257 }
258
gralloc0_perform(struct gralloc_module_t const * module,int op,...)259 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
260 {
261 va_list args;
262 int32_t *out_format, ret;
263 uint64_t *out_store;
264 buffer_handle_t handle;
265 uint32_t *out_width, *out_height, *out_stride;
266 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
267 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
268 auto mod = (struct gralloc0_module const *)module;
269
270 switch (op) {
271 case GRALLOC_DRM_GET_STRIDE:
272 case GRALLOC_DRM_GET_FORMAT:
273 case GRALLOC_DRM_GET_DIMENSIONS:
274 case GRALLOC_DRM_GET_BACKING_STORE:
275 break;
276 default:
277 return -EINVAL;
278 }
279
280 va_start(args, op);
281
282 ret = 0;
283 handle = va_arg(args, buffer_handle_t);
284 auto hnd = cros_gralloc_convert_handle(handle);
285 if (!hnd) {
286 drv_log("Invalid handle.\n");
287 return -EINVAL;
288 }
289
290 switch (op) {
291 case GRALLOC_DRM_GET_STRIDE:
292 out_stride = va_arg(args, uint32_t *);
293 ret = mod->driver->resource_info(handle, strides, offsets);
294 if (ret)
295 break;
296
297 if (strides[0] != hnd->strides[0]) {
298 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
299 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
300 } else {
301 *out_stride = hnd->pixel_stride;
302 }
303
304 break;
305 case GRALLOC_DRM_GET_FORMAT:
306 out_format = va_arg(args, int32_t *);
307 *out_format = hnd->droid_format;
308 break;
309 case GRALLOC_DRM_GET_DIMENSIONS:
310 out_width = va_arg(args, uint32_t *);
311 out_height = va_arg(args, uint32_t *);
312 *out_width = hnd->width;
313 *out_height = hnd->height;
314 break;
315 case GRALLOC_DRM_GET_BACKING_STORE:
316 out_store = va_arg(args, uint64_t *);
317 ret = mod->driver->get_backing_store(handle, out_store);
318 break;
319 default:
320 ret = -EINVAL;
321 }
322
323 va_end(args);
324
325 return ret;
326 }
327
gralloc0_lock_ycbcr(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,struct android_ycbcr * ycbcr)328 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
329 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
330 {
331 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
332 }
333
gralloc0_lock_async(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,void ** vaddr,int fence_fd)334 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
335 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
336 {
337 int32_t ret;
338 uint32_t map_flags;
339 uint8_t *addr[DRV_MAX_PLANES];
340 auto mod = (struct gralloc0_module const *)module;
341 struct rectangle rect = { .x = static_cast<uint32_t>(l),
342 .y = static_cast<uint32_t>(t),
343 .width = static_cast<uint32_t>(w),
344 .height = static_cast<uint32_t>(h) };
345
346 auto hnd = cros_gralloc_convert_handle(handle);
347 if (!hnd) {
348 drv_log("Invalid handle.\n");
349 return -EINVAL;
350 }
351
352 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
353 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
354 return -EINVAL;
355 }
356
357 assert(l >= 0);
358 assert(t >= 0);
359 assert(w >= 0);
360 assert(h >= 0);
361
362 map_flags = gralloc0_convert_map_usage(usage);
363 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
364 *vaddr = addr[0];
365 return ret;
366 }
367
gralloc0_unlock_async(struct gralloc_module_t const * module,buffer_handle_t handle,int * fence_fd)368 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
369 int *fence_fd)
370 {
371 auto mod = (struct gralloc0_module const *)module;
372 return mod->driver->unlock(handle, fence_fd);
373 }
374
gralloc0_lock_async_ycbcr(struct gralloc_module_t const * module,buffer_handle_t handle,int usage,int l,int t,int w,int h,struct android_ycbcr * ycbcr,int fence_fd)375 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
376 int usage, int l, int t, int w, int h,
377 struct android_ycbcr *ycbcr, int fence_fd)
378 {
379 int32_t ret;
380 uint32_t map_flags;
381 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
382 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
383 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
384 auto mod = (struct gralloc0_module const *)module;
385 struct rectangle rect = { .x = static_cast<uint32_t>(l),
386 .y = static_cast<uint32_t>(t),
387 .width = static_cast<uint32_t>(w),
388 .height = static_cast<uint32_t>(h) };
389
390 auto hnd = cros_gralloc_convert_handle(handle);
391 if (!hnd) {
392 drv_log("Invalid handle.\n");
393 return -EINVAL;
394 }
395
396 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
397 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
398 drv_log("Non-YUV format not compatible.\n");
399 return -EINVAL;
400 }
401
402 assert(l >= 0);
403 assert(t >= 0);
404 assert(w >= 0);
405 assert(h >= 0);
406
407 map_flags = gralloc0_convert_map_usage(usage);
408 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
409 if (ret)
410 return ret;
411
412 if (!map_flags) {
413 ret = mod->driver->resource_info(handle, strides, offsets);
414 if (ret)
415 return ret;
416
417 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
418 addr[plane] =
419 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
420 }
421
422 switch (hnd->format) {
423 case DRM_FORMAT_NV12:
424 ycbcr->y = addr[0];
425 ycbcr->cb = addr[1];
426 ycbcr->cr = addr[1] + 1;
427 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
428 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
429 ycbcr->chroma_step = 2;
430 break;
431 case DRM_FORMAT_YVU420:
432 case DRM_FORMAT_YVU420_ANDROID:
433 ycbcr->y = addr[0];
434 ycbcr->cb = addr[2];
435 ycbcr->cr = addr[1];
436 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
437 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
438 ycbcr->chroma_step = 1;
439 break;
440 default:
441 module->unlock(module, handle);
442 return -EINVAL;
443 }
444
445 return 0;
446 }
447
448 // clang-format off
449 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
450 // clang-format on
451
452 struct gralloc0_module HAL_MODULE_INFO_SYM = {
453 .base =
454 {
455 .common =
456 {
457 .tag = HARDWARE_MODULE_TAG,
458 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
459 .hal_api_version = 0,
460 .id = GRALLOC_HARDWARE_MODULE_ID,
461 .name = "CrOS Gralloc",
462 .author = "Chrome OS",
463 .methods = &gralloc0_module_methods,
464 },
465
466 .registerBuffer = gralloc0_register_buffer,
467 .unregisterBuffer = gralloc0_unregister_buffer,
468 .lock = gralloc0_lock,
469 .unlock = gralloc0_unlock,
470 .perform = gralloc0_perform,
471 .lock_ycbcr = gralloc0_lock_ycbcr,
472 .lockAsync = gralloc0_lock_async,
473 .unlockAsync = gralloc0_unlock_async,
474 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
475 .validateBufferSize = NULL,
476 .getTransportSize = NULL,
477 },
478
479 .alloc = nullptr,
480 .driver = nullptr,
481 .initialized = false,
482 };
483