OSDN Git Service

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