OSDN Git Service

gralloc0_register_buffer: initialize gralloc0 when needed
[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 <hardware/gralloc.h>
10 #include <memory.h>
11 #include <errno.h>
12
13 #include "../i915_private_android.h"
14 #include "../i915_private_android_types.h"
15
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;
20         bool initialized;
21         SpinLock initialization_mutex;
22 };
23
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.
29  */
30 // clang-format off
31 enum {
32         GRALLOC_DRM_GET_STRIDE,
33         GRALLOC_DRM_GET_FORMAT,
34         GRALLOC_DRM_GET_DIMENSIONS,
35         GRALLOC_DRM_GET_BACKING_STORE,
36 };
37 // clang-format on
38
39 static uint64_t gralloc0_convert_usage(int usage)
40 {
41         uint64_t use_flags = BO_USE_NONE;
42
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)
65                 /*
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.
68                  * */
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;
81
82         return use_flags;
83 }
84
85 static uint32_t gralloc0_convert_map_usage(int map_usage)
86 {
87         uint32_t map_flags = BO_MAP_NONE;
88
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;
93
94         return map_flags;
95 }
96
97 static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
98                           buffer_handle_t *handle, int *stride)
99 {
100         int32_t ret;
101         bool supported;
102         struct cros_gralloc_buffer_descriptor descriptor;
103         auto mod = (struct gralloc0_module *)dev->common.module;
104
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);
112
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);
117         }
118
119         if (!supported) {
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));
124                 return -EINVAL;
125         }
126
127         ret = mod->driver->allocate(&descriptor, handle);
128         if (ret)
129                 return ret;
130
131         auto hnd = cros_gralloc_convert_handle(*handle);
132         *stride = hnd->pixel_stride;
133
134         return 0;
135 }
136
137 static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
138 {
139         auto mod = (struct gralloc0_module *)dev->common.module;
140         return mod->driver->release(handle);
141 }
142
143 static int gralloc0_close(struct hw_device_t *dev)
144 {
145         /* Memory is freed by managed pointers on process close. */
146         return 0;
147 }
148
149 static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
150 {
151         SCOPED_SPIN_LOCK(mod->initialization_mutex);
152
153         if (mod->initialized)
154                 return 0;
155
156         mod->driver = std::make_unique<cros_gralloc_driver>();
157         if (mod->driver->init()) {
158                 cros_gralloc_error("Failed to initialize driver.");
159                 return -ENODEV;
160         }
161
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;
170         }
171
172         mod->initialized = true;
173         return 0;
174 }
175
176 static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
177 {
178         auto module = (struct gralloc0_module *)mod;
179
180         if (module->initialized) {
181                 *dev = &module->alloc->common;
182                 return 0;
183         }
184
185         /* On Android M, Surfaceflinger tries to open the gralloc device
186          * using name GRALLOC_HARDWARE_FB0.
187          */
188         if ((strcmp(name, GRALLOC_HARDWARE_GPU0)!=0) && (strcmp(name, GRALLOC_HARDWARE_FB0)!=0)) {
189                 cros_gralloc_error("Incorrect device name - %s.", name);
190                 return -EINVAL;
191         }
192
193         if (gralloc0_init(module, true))
194                 return -ENODEV;
195
196         *dev = &module->alloc->common;
197         return 0;
198 }
199
200 static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
201 {
202         auto mod = (struct gralloc0_module *)module;
203
204         if (!mod->initialized)
205                 if (gralloc0_init(mod, true))
206                         return -ENODEV;
207
208         return mod->driver->retain(handle);
209 }
210
211 static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
212 {
213         auto mod = (struct gralloc0_module *)module;
214         return mod->driver->release(handle);
215 }
216
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)
219 {
220         return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
221 }
222
223 static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
224 {
225         int32_t fence_fd, ret;
226         auto mod = (struct gralloc0_module *)module;
227         ret = mod->driver->unlock(handle, &fence_fd);
228         if (ret)
229                 return ret;
230
231         ret = cros_gralloc_sync_wait(fence_fd);
232         if (ret)
233                 return ret;
234
235         return 0;
236 }
237
238 static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
239 {
240         va_list args;
241         int32_t *out_format, ret;
242         uint64_t *out_store;
243         buffer_handle_t handle;
244         uint32_t *out_width, *out_height, *out_stride;
245         auto mod = (struct gralloc0_module *)module;
246
247         switch (op) {
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:
252                 break;
253         default:
254                 return -EINVAL;
255         }
256
257         va_start(args, op);
258
259         ret = 0;
260         handle = va_arg(args, buffer_handle_t);
261         auto hnd = cros_gralloc_convert_handle(handle);
262         if (!hnd) {
263                 cros_gralloc_error("Invalid handle.");
264                 return -EINVAL;
265         }
266
267         switch (op) {
268         case GRALLOC_DRM_GET_STRIDE:
269                 out_stride = va_arg(args, uint32_t *);
270                 *out_stride = hnd->pixel_stride;
271                 break;
272         case GRALLOC_DRM_GET_FORMAT:
273                 out_format = va_arg(args, int32_t *);
274                 *out_format = hnd->droid_format;
275                 break;
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;
281                 break;
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);
285                 break;
286         default:
287                 ret = -EINVAL;
288         }
289
290         va_end(args);
291
292         return ret;
293 }
294
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)
297 {
298         return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
299 }
300
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)
303 {
304         int32_t ret;
305         uint32_t map_flags;
306         uint8_t *addr[DRV_MAX_PLANES];
307         auto mod = (struct gralloc0_module *)module;
308
309         auto hnd = cros_gralloc_convert_handle(handle);
310         if (!hnd) {
311                 cros_gralloc_error("Invalid handle.");
312                 return -EINVAL;
313         }
314
315         if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
316                 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
317                 return -EINVAL;
318         }
319
320         map_flags = gralloc0_convert_map_usage(usage);
321         ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
322         *vaddr = addr[0];
323         return ret;
324 }
325
326 static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
327                                  int *fence_fd)
328 {
329         auto mod = (struct gralloc0_module *)module;
330         return mod->driver->unlock(handle, fence_fd);
331 }
332
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)
336 {
337         int32_t ret;
338         uint32_t map_flags;
339         uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
340         auto mod = (struct gralloc0_module *)module;
341
342         auto hnd = cros_gralloc_convert_handle(handle);
343         if (!hnd) {
344                 cros_gralloc_error("Invalid handle.");
345                 return -EINVAL;
346         }
347
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.");
353                 return -EINVAL;
354         }
355
356         map_flags = gralloc0_convert_map_usage(usage);
357         ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
358         if (ret)
359                 return ret;
360
361         switch (hnd->format) {
362         case DRM_FORMAT_NV12:
363         case DRM_FORMAT_NV12_Y_TILED_INTEL:
364                 ycbcr->y = addr[0];
365                 ycbcr->cb = addr[1];
366                 ycbcr->cr = addr[1] + 1;
367                 ycbcr->ystride = hnd->strides[0];
368                 ycbcr->cstride = hnd->strides[1];
369                 ycbcr->chroma_step = 2;
370                 break;
371         case DRM_FORMAT_YVU420:
372         case DRM_FORMAT_YVU420_ANDROID:
373                 ycbcr->y = addr[0];
374                 ycbcr->cb = addr[2];
375                 ycbcr->cr = addr[1];
376                 ycbcr->ystride = hnd->strides[0];
377                 ycbcr->cstride = hnd->strides[1];
378                 ycbcr->chroma_step = 1;
379                 break;
380         default:
381                 module->unlock(module, handle);
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 // clang-format off
389 static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
390 // clang-format on
391
392 struct gralloc0_module HAL_MODULE_INFO_SYM = {
393         .base =
394             {
395                 .common =
396                     {
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,
404                     },
405
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,
415             },
416
417         .alloc = nullptr,
418         .driver = nullptr,
419         .initialized = false,
420 };