OSDN Git Service

a70498a450972c27f5cc2c23f47e609973a924ba
[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 "../../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
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
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
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
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.producer_usage = descriptor.consumer_usage = usage;
123         descriptor.drm_format = cros_gralloc_convert_format(format);
124         descriptor.use_flags = gralloc0_convert_usage(usage);
125
126         supported = mod->driver->is_supported(&descriptor);
127         if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
128                 descriptor.use_flags &= ~BO_USE_SCANOUT;
129                 supported = mod->driver->is_supported(&descriptor);
130         }
131         if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
132             !gralloc0_droid_yuv_format(format)) {
133                 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
134                 // because they are not input to a hw encoder but used as an
135                 // intermediate format (e.g. camera).
136                 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
137                 supported = mod->driver->is_supported(&descriptor);
138         }
139
140         if (!supported) {
141                 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
142                         "drv_format: %4.4s, use_flags: %llu\n",
143                         format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
144                         static_cast<unsigned long long>(descriptor.use_flags));
145                 return -EINVAL;
146         }
147
148         ret = mod->driver->allocate(&descriptor, handle);
149         if (ret)
150                 return ret;
151
152         auto hnd = cros_gralloc_convert_handle(*handle);
153         *stride = hnd->pixel_stride;
154
155         return 0;
156 }
157
158 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
159 {
160         auto mod = (struct gralloc0_module const *)dev->common.module;
161         return mod->driver->release(handle);
162 }
163
164 static int gralloc0_close(struct hw_device_t *dev)
165 {
166         /* Memory is freed by managed pointers on process close. */
167         return 0;
168 }
169
170 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
171 {
172         std::lock_guard<std::mutex> lock(mod->initialization_mutex);
173
174         if (mod->initialized)
175                 return 0;
176
177         mod->driver = std::make_unique<cros_gralloc_driver>();
178         if (mod->driver->init()) {
179                 drv_log("Failed to initialize driver.\n");
180                 return -ENODEV;
181         }
182
183         if (initialize_alloc) {
184                 mod->alloc = std::make_unique<alloc_device_t>();
185                 mod->alloc->alloc = gralloc0_alloc;
186                 mod->alloc->free = gralloc0_free;
187                 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
188                 mod->alloc->common.version = 0;
189                 mod->alloc->common.module = (hw_module_t *)mod;
190                 mod->alloc->common.close = gralloc0_close;
191         }
192
193         mod->initialized = true;
194         return 0;
195 }
196
197 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
198 {
199         auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
200         auto module = const_cast<struct gralloc0_module *>(const_module);
201
202         if (module->initialized) {
203                 *dev = &module->alloc->common;
204                 return 0;
205         }
206
207         if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
208                 drv_log("Incorrect device name - %s.\n", name);
209                 return -EINVAL;
210         }
211
212         if (gralloc0_init(module, true))
213                 return -ENODEV;
214
215         *dev = &module->alloc->common;
216         return 0;
217 }
218
219 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
220 {
221         auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
222         auto mod = const_cast<struct gralloc0_module *>(const_module);
223
224         if (!mod->initialized)
225                 if (gralloc0_init(mod, false))
226                         return -ENODEV;
227
228         return mod->driver->retain(handle);
229 }
230
231 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
232 {
233         auto mod = (struct gralloc0_module const *)module;
234         return mod->driver->release(handle);
235 }
236
237 static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
238                          int l, int t, int w, int h, void **vaddr)
239 {
240         return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
241 }
242
243 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
244 {
245         int32_t fence_fd, ret;
246         auto mod = (struct gralloc0_module const *)module;
247         ret = mod->driver->unlock(handle, &fence_fd);
248         if (ret)
249                 return ret;
250
251         ret = cros_gralloc_sync_wait(fence_fd);
252         if (ret)
253                 return ret;
254
255         return 0;
256 }
257
258 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
259 {
260         va_list args;
261         int32_t *out_format, ret;
262         uint64_t *out_store;
263         buffer_handle_t handle;
264         uint32_t *out_width, *out_height, *out_stride;
265         uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
266         uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
267         auto mod = (struct gralloc0_module const *)module;
268
269         switch (op) {
270         case GRALLOC_DRM_GET_STRIDE:
271         case GRALLOC_DRM_GET_FORMAT:
272         case GRALLOC_DRM_GET_DIMENSIONS:
273         case GRALLOC_DRM_GET_BACKING_STORE:
274                 break;
275         default:
276                 return -EINVAL;
277         }
278
279         va_start(args, op);
280
281         ret = 0;
282         handle = va_arg(args, buffer_handle_t);
283         auto hnd = cros_gralloc_convert_handle(handle);
284         if (!hnd) {
285                 drv_log("Invalid handle.\n");
286                 return -EINVAL;
287         }
288
289         switch (op) {
290         case GRALLOC_DRM_GET_STRIDE:
291                 out_stride = va_arg(args, uint32_t *);
292                 ret = mod->driver->resource_info(handle, strides, offsets);
293                 if (ret)
294                         break;
295
296                 if (strides[0] != hnd->strides[0]) {
297                         uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
298                         *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
299                 } else {
300                         *out_stride = hnd->pixel_stride;
301                 }
302
303                 break;
304         case GRALLOC_DRM_GET_FORMAT:
305                 out_format = va_arg(args, int32_t *);
306                 *out_format = hnd->droid_format;
307                 break;
308         case GRALLOC_DRM_GET_DIMENSIONS:
309                 out_width = va_arg(args, uint32_t *);
310                 out_height = va_arg(args, uint32_t *);
311                 *out_width = hnd->width;
312                 *out_height = hnd->height;
313                 break;
314         case GRALLOC_DRM_GET_BACKING_STORE:
315                 out_store = va_arg(args, uint64_t *);
316                 ret = mod->driver->get_backing_store(handle, out_store);
317                 break;
318         default:
319                 ret = -EINVAL;
320         }
321
322         va_end(args);
323
324         return ret;
325 }
326
327 static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
328                                int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
329 {
330         return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
331 }
332
333 static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
334                                int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
335 {
336         int32_t ret;
337         uint32_t map_flags;
338         uint8_t *addr[DRV_MAX_PLANES];
339         auto mod = (struct gralloc0_module const *)module;
340         struct rectangle rect = { .x = static_cast<uint32_t>(l),
341                                   .y = static_cast<uint32_t>(t),
342                                   .width = static_cast<uint32_t>(w),
343                                   .height = static_cast<uint32_t>(h) };
344
345         auto hnd = cros_gralloc_convert_handle(handle);
346         if (!hnd) {
347                 drv_log("Invalid handle.\n");
348                 return -EINVAL;
349         }
350
351         if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
352                 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
353                 return -EINVAL;
354         }
355
356         assert(l >= 0);
357         assert(t >= 0);
358         assert(w >= 0);
359         assert(h >= 0);
360
361         map_flags = gralloc0_convert_map_usage(usage);
362         ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
363         *vaddr = addr[0];
364         return ret;
365 }
366
367 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
368                                  int *fence_fd)
369 {
370         auto mod = (struct gralloc0_module const *)module;
371         return mod->driver->unlock(handle, fence_fd);
372 }
373
374 static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
375                                      int usage, int l, int t, int w, int h,
376                                      struct android_ycbcr *ycbcr, int fence_fd)
377 {
378         int32_t ret;
379         uint32_t map_flags;
380         uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
381         uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
382         uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
383         auto mod = (struct gralloc0_module const *)module;
384         struct rectangle rect = { .x = static_cast<uint32_t>(l),
385                                   .y = static_cast<uint32_t>(t),
386                                   .width = static_cast<uint32_t>(w),
387                                   .height = static_cast<uint32_t>(h) };
388
389         auto hnd = cros_gralloc_convert_handle(handle);
390         if (!hnd) {
391                 drv_log("Invalid handle.\n");
392                 return -EINVAL;
393         }
394
395         if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
396             hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
397                 drv_log("Non-YUV format not compatible.\n");
398                 return -EINVAL;
399         }
400
401         assert(l >= 0);
402         assert(t >= 0);
403         assert(w >= 0);
404         assert(h >= 0);
405
406         map_flags = gralloc0_convert_map_usage(usage);
407         ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
408         if (ret)
409                 return ret;
410
411         if (!map_flags) {
412                 ret = mod->driver->resource_info(handle, strides, offsets);
413                 if (ret)
414                         return ret;
415
416                 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
417                         addr[plane] =
418                             reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
419         }
420
421         switch (hnd->format) {
422         case DRM_FORMAT_NV12:
423                 ycbcr->y = addr[0];
424                 ycbcr->cb = addr[1];
425                 ycbcr->cr = addr[1] + 1;
426                 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
427                 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
428                 ycbcr->chroma_step = 2;
429                 break;
430         case DRM_FORMAT_YVU420:
431         case DRM_FORMAT_YVU420_ANDROID:
432                 ycbcr->y = addr[0];
433                 ycbcr->cb = addr[2];
434                 ycbcr->cr = addr[1];
435                 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
436                 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
437                 ycbcr->chroma_step = 1;
438                 break;
439         default:
440                 module->unlock(module, handle);
441                 return -EINVAL;
442         }
443
444         return 0;
445 }
446
447 // clang-format off
448 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
449 // clang-format on
450
451 struct gralloc0_module HAL_MODULE_INFO_SYM = {
452         .base =
453             {
454                 .common =
455                     {
456                         .tag = HARDWARE_MODULE_TAG,
457                         .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
458                         .hal_api_version = 0,
459                         .id = GRALLOC_HARDWARE_MODULE_ID,
460                         .name = "CrOS Gralloc",
461                         .author = "Chrome OS",
462                         .methods = &gralloc0_module_methods,
463                     },
464
465                 .registerBuffer = gralloc0_register_buffer,
466                 .unregisterBuffer = gralloc0_unregister_buffer,
467                 .lock = gralloc0_lock,
468                 .unlock = gralloc0_unlock,
469                 .perform = gralloc0_perform,
470                 .lock_ycbcr = gralloc0_lock_ycbcr,
471                 .lockAsync = gralloc0_lock_async,
472                 .unlockAsync = gralloc0_unlock_async,
473                 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
474                 .validateBufferSize = NULL,
475                 .getTransportSize = NULL,
476             },
477
478         .alloc = nullptr,
479         .driver = nullptr,
480         .initialized = false,
481 };