OSDN Git Service

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