OSDN Git Service

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