blob: ab053763cb751a2332864fab811c37e8dc836e75 [file] [log] [blame]
Gurchetan Singhd6b8b032017-05-31 14:31:31 -07001/*
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
12struct gralloc0_module {
13 gralloc_module_t base;
14 std::unique_ptr<alloc_device_t> alloc;
15 std::unique_ptr<cros_gralloc_driver> driver;
Gurchetan Singhbcfd7582017-08-01 15:02:24 -070016 bool initialized;
17 std::mutex initialization_mutex;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070018};
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
27enum {
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
Gurchetan Singha1892b22017-09-28 16:40:52 -070035static uint64_t gralloc0_convert_usage(int usage)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070036{
Gurchetan Singha1892b22017-09-28 16:40:52 -070037 uint64_t use_flags = BO_USE_NONE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070038
Gurchetan Singha1892b22017-09-28 16:40:52 -070039 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)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070056 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070057 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)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070061 /*
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 * */
Gurchetan Singha1892b22017-09-28 16:40:52 -070065 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)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070069 /*HACK: See b/30054495 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070070 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;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070077
Gurchetan Singha1892b22017-09-28 16:40:52 -070078 return use_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070079}
80
Gurchetan Singhf7f633a2017-09-28 17:02:12 -070081static 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
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070093static 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);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700106 descriptor.use_flags = gralloc0_convert_usage(usage);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700107
108 supported = mod->driver->is_supported(&descriptor);
109 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700110 descriptor.use_flags &= ~BO_USE_SCANOUT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700111 supported = mod->driver->is_supported(&descriptor);
112 }
113
114 if (!supported) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700115 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL usage: %u, "
116 "drv_format: %4.4s, use_flags: %llu",
Tomasz Figa23b85cb2017-07-11 18:07:11 +0900117 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
Gurchetan Singha1892b22017-09-28 16:40:52 -0700118 static_cast<unsigned long long>(descriptor.use_flags));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900119 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700120 }
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
Tomasz Figa90bb7432017-07-21 17:54:05 +0900129 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700130}
131
132static 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
138static int gralloc0_close(struct hw_device_t *dev)
139{
140 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900141 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700142}
143
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700144static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
145{
146 std::lock_guard<std::mutex> 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
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700171static 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
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700175 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700176 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900177 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700178 }
179
180 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
181 cros_gralloc_error("Incorrect device name - %s.", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900182 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700183 }
184
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700185 if (gralloc0_init(module, true))
186 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700187
188 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900189 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700190}
191
192static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
193{
194 auto mod = (struct gralloc0_module *)module;
195
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700196 if (!mod->initialized)
197 if (gralloc0_init(mod, false))
198 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700199
200 return mod->driver->retain(handle);
201}
202
203static 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
209static 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{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700212 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700213}
214
215static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
216{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700217 int32_t fence_fd, ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700218 auto mod = (struct gralloc0_module *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700219 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;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700228}
229
230static 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:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900246 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700247 }
248
249 va_start(args, op);
250
Tomasz Figa90bb7432017-07-21 17:54:05 +0900251 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700252 handle = va_arg(args, buffer_handle_t);
253 auto hnd = cros_gralloc_convert_handle(handle);
254 if (!hnd) {
255 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900256 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700257 }
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:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900279 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700280 }
281
282 va_end(args);
283
284 return ret;
285}
286
287static 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{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700290 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
291}
292
293static 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;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700297 uint32_t map_flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700298 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
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700312 map_flags = gralloc0_convert_map_usage(usage);
313 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700314 *vaddr = addr[0];
315 return ret;
316}
317
318static 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
325static 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{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700329 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700330 uint32_t map_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700331 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.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900337 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700338 }
339
340 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
Tomasz Figaaff29fd2017-07-05 17:50:18 +0900341 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
342 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700343 cros_gralloc_error("Non-YUV format not compatible.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900344 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700345 }
346
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700347 map_flags = gralloc0_convert_map_usage(usage);
348 ret = mod->driver->lock(handle, fence_fd, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900349 if (ret)
350 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700351
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:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700371 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900372 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700373 }
374
Tomasz Figa90bb7432017-07-21 17:54:05 +0900375 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700376}
377
Tomasz Figa4df286c2017-08-02 19:35:40 +0900378// clang-format off
379static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
380// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700381
382struct gralloc0_module HAL_MODULE_INFO_SYM = {
383 .base =
384 {
385 .common =
386 {
387 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700388 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700389 .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,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700402 .lockAsync = gralloc0_lock_async,
403 .unlockAsync = gralloc0_unlock_async,
404 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700405 },
406
407 .alloc = nullptr,
408 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700409 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700410};