OSDN Git Service

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