blob: b055f65ffd37ad50815808f2b4a938f953d03216 [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;
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800300 struct rectangle rect = { .x = static_cast<uint32_t>(l),
301 .y = static_cast<uint32_t>(t),
302 .width = static_cast<uint32_t>(w),
303 .height = static_cast<uint32_t>(h) };
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700304
305 auto hnd = cros_gralloc_convert_handle(handle);
306 if (!hnd) {
307 cros_gralloc_error("Invalid handle.");
308 return -EINVAL;
309 }
310
311 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
312 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
313 return -EINVAL;
314 }
315
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800316 assert(l >= 0);
317 assert(t >= 0);
318 assert(w >= 0);
319 assert(h >= 0);
320
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700321 map_flags = gralloc0_convert_map_usage(usage);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800322 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700323 *vaddr = addr[0];
324 return ret;
325}
326
327static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
328 int *fence_fd)
329{
330 auto mod = (struct gralloc0_module *)module;
331 return mod->driver->unlock(handle, fence_fd);
332}
333
334static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
335 int usage, int l, int t, int w, int h,
336 struct android_ycbcr *ycbcr, int fence_fd)
337{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700338 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700339 uint32_t map_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700340 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
341 auto mod = (struct gralloc0_module *)module;
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800342 struct rectangle rect = { .x = static_cast<uint32_t>(l),
343 .y = static_cast<uint32_t>(t),
344 .width = static_cast<uint32_t>(w),
345 .height = static_cast<uint32_t>(h) };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700346
347 auto hnd = cros_gralloc_convert_handle(handle);
348 if (!hnd) {
349 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900350 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700351 }
352
353 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
Tomasz Figaaff29fd2017-07-05 17:50:18 +0900354 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
355 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700356 cros_gralloc_error("Non-YUV format not compatible.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900357 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700358 }
359
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800360 assert(l >= 0);
361 assert(t >= 0);
362 assert(w >= 0);
363 assert(h >= 0);
364
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700365 map_flags = gralloc0_convert_map_usage(usage);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800366 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900367 if (ret)
368 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700369
370 switch (hnd->format) {
371 case DRM_FORMAT_NV12:
372 ycbcr->y = addr[0];
373 ycbcr->cb = addr[1];
374 ycbcr->cr = addr[1] + 1;
375 ycbcr->ystride = hnd->strides[0];
376 ycbcr->cstride = hnd->strides[1];
377 ycbcr->chroma_step = 2;
378 break;
379 case DRM_FORMAT_YVU420:
380 case DRM_FORMAT_YVU420_ANDROID:
381 ycbcr->y = addr[0];
382 ycbcr->cb = addr[2];
383 ycbcr->cr = addr[1];
384 ycbcr->ystride = hnd->strides[0];
385 ycbcr->cstride = hnd->strides[1];
386 ycbcr->chroma_step = 1;
387 break;
388 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700389 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900390 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700391 }
392
Tomasz Figa90bb7432017-07-21 17:54:05 +0900393 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700394}
395
Tomasz Figa4df286c2017-08-02 19:35:40 +0900396// clang-format off
397static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
398// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700399
400struct gralloc0_module HAL_MODULE_INFO_SYM = {
401 .base =
402 {
403 .common =
404 {
405 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700406 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700407 .hal_api_version = 0,
408 .id = GRALLOC_HARDWARE_MODULE_ID,
409 .name = "CrOS Gralloc",
410 .author = "Chrome OS",
411 .methods = &gralloc0_module_methods,
412 },
413
414 .registerBuffer = gralloc0_register_buffer,
415 .unregisterBuffer = gralloc0_unregister_buffer,
416 .lock = gralloc0_lock,
417 .unlock = gralloc0_unlock,
418 .perform = gralloc0_perform,
419 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700420 .lockAsync = gralloc0_lock_async,
421 .unlockAsync = gralloc0_unlock_async,
422 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700423 },
424
425 .alloc = nullptr,
426 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700427 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700428};