OSDN Git Service

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