blob: ec897bed109c88f7b44955d706b4c627282f89cd [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;
16};
17
18/* This enumeration must match the one in <gralloc_drm.h>.
19 * The functions supported by this gralloc's temporary private API are listed
20 * below. Use of these functions is highly discouraged and should only be
21 * reserved for cases where no alternative to get same information (such as
22 * querying ANativeWindow) exists.
23 */
24// clang-format off
25enum {
26 GRALLOC_DRM_GET_STRIDE,
27 GRALLOC_DRM_GET_FORMAT,
28 GRALLOC_DRM_GET_DIMENSIONS,
29 GRALLOC_DRM_GET_BACKING_STORE,
30};
31// clang-format on
32
33static int64_t gralloc0_convert_flags(int flags)
34{
35 uint64_t usage = BO_USE_NONE;
36
37 if (flags & GRALLOC_USAGE_CURSOR)
38 usage |= BO_USE_NONE;
39 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
40 usage |= BO_USE_SW_READ_RARELY;
41 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
42 usage |= BO_USE_SW_READ_OFTEN;
43 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
44 usage |= BO_USE_SW_WRITE_RARELY;
45 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
46 usage |= BO_USE_SW_WRITE_OFTEN;
47 if (flags & GRALLOC_USAGE_HW_TEXTURE)
48 usage |= BO_USE_TEXTURE;
49 if (flags & GRALLOC_USAGE_HW_RENDER)
50 usage |= BO_USE_RENDERING;
51 if (flags & GRALLOC_USAGE_HW_2D)
52 usage |= BO_USE_RENDERING;
53 if (flags & GRALLOC_USAGE_HW_COMPOSER)
54 /* HWC wants to use display hardware, but can defer to OpenGL. */
55 usage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
56 if (flags & GRALLOC_USAGE_HW_FB)
57 usage |= BO_USE_NONE;
58 if (flags & GRALLOC_USAGE_EXTERNAL_DISP)
59 /*
60 * This flag potentially covers external display for the normal drivers (i915,
61 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
62 * */
63 usage |= BO_USE_NONE;
64 if (flags & GRALLOC_USAGE_PROTECTED)
65 usage |= BO_USE_PROTECTED;
66 if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
67 /*HACK: See b/30054495 */
68 usage |= BO_USE_SW_READ_OFTEN;
69 if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
Tomasz Figafd0b0162017-07-11 18:28:02 +090070 usage |= BO_USE_CAMERA_WRITE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070071 if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
Tomasz Figafd0b0162017-07-11 18:28:02 +090072 usage |= BO_USE_CAMERA_READ;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070073 if (flags & GRALLOC_USAGE_RENDERSCRIPT)
74 /* We use CPU for compute. */
75 usage |= BO_USE_LINEAR;
76
77 return usage;
78}
79
80static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
81 buffer_handle_t *handle, int *stride)
82{
83 int32_t ret;
84 bool supported;
85 struct cros_gralloc_buffer_descriptor descriptor;
86 auto mod = (struct gralloc0_module *)dev->common.module;
87
88 descriptor.width = w;
89 descriptor.height = h;
90 descriptor.droid_format = format;
91 descriptor.producer_usage = descriptor.consumer_usage = usage;
92 descriptor.drm_format = cros_gralloc_convert_format(format);
93 descriptor.drv_usage = gralloc0_convert_flags(usage);
94
95 supported = mod->driver->is_supported(&descriptor);
96 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
97 descriptor.drv_usage &= ~BO_USE_SCANOUT;
98 supported = mod->driver->is_supported(&descriptor);
99 }
100
101 if (!supported) {
102 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, "
103 "drv_format: %4.4s, drv_flags: %llu",
Tomasz Figa23b85cb2017-07-11 18:07:11 +0900104 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700105 static_cast<unsigned long long>(descriptor.drv_usage));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900106 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700107 }
108
109 ret = mod->driver->allocate(&descriptor, handle);
110 if (ret)
111 return ret;
112
113 auto hnd = cros_gralloc_convert_handle(*handle);
114 *stride = hnd->pixel_stride;
115
Tomasz Figa90bb7432017-07-21 17:54:05 +0900116 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700117}
118
119static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
120{
121 auto mod = (struct gralloc0_module *)dev->common.module;
122 return mod->driver->release(handle);
123}
124
125static int gralloc0_close(struct hw_device_t *dev)
126{
127 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900128 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700129}
130
131static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
132{
133 auto module = (struct gralloc0_module *)mod;
134
135 if (module->alloc) {
136 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900137 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700138 }
139
140 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
141 cros_gralloc_error("Incorrect device name - %s.", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900142 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700143 }
144
145 module->driver = std::make_unique<cros_gralloc_driver>();
146 if (module->driver->init()) {
147 cros_gralloc_error("Failed to initialize driver.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900148 return -ENOMEM;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700149 }
150
151 module->alloc = std::make_unique<alloc_device_t>();
152
153 module->alloc->alloc = gralloc0_alloc;
154 module->alloc->free = gralloc0_free;
155 module->alloc->common.tag = HARDWARE_DEVICE_TAG;
156 module->alloc->common.version = 0;
157 module->alloc->common.module = (hw_module_t *)mod;
158 module->alloc->common.close = gralloc0_close;
159
160 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900161 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700162}
163
164static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
165{
166 auto mod = (struct gralloc0_module *)module;
167
168 if (!mod->driver) {
169 mod->driver = std::make_unique<cros_gralloc_driver>();
170 if (mod->driver->init()) {
171 cros_gralloc_error("Failed to initialize driver.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900172 return -ENOMEM;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700173 }
174 }
175
176 return mod->driver->retain(handle);
177}
178
179static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
180{
181 auto mod = (struct gralloc0_module *)module;
182 return mod->driver->release(handle);
183}
184
185static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
186 int l, int t, int w, int h, void **vaddr)
187{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700188 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700189}
190
191static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
192{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700193 int32_t fence_fd, ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700194 auto mod = (struct gralloc0_module *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700195 ret = mod->driver->unlock(handle, &fence_fd);
196 if (ret)
197 return ret;
198
199 ret = cros_gralloc_sync_wait(fence_fd);
200 if (ret)
201 return ret;
202
203 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700204}
205
206static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
207{
208 va_list args;
209 int32_t *out_format, ret;
210 uint64_t *out_store;
211 buffer_handle_t handle;
212 uint32_t *out_width, *out_height, *out_stride;
213 auto mod = (struct gralloc0_module *)module;
214
215 switch (op) {
216 case GRALLOC_DRM_GET_STRIDE:
217 case GRALLOC_DRM_GET_FORMAT:
218 case GRALLOC_DRM_GET_DIMENSIONS:
219 case GRALLOC_DRM_GET_BACKING_STORE:
220 break;
221 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900222 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700223 }
224
225 va_start(args, op);
226
Tomasz Figa90bb7432017-07-21 17:54:05 +0900227 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700228 handle = va_arg(args, buffer_handle_t);
229 auto hnd = cros_gralloc_convert_handle(handle);
230 if (!hnd) {
231 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900232 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700233 }
234
235 switch (op) {
236 case GRALLOC_DRM_GET_STRIDE:
237 out_stride = va_arg(args, uint32_t *);
238 *out_stride = hnd->pixel_stride;
239 break;
240 case GRALLOC_DRM_GET_FORMAT:
241 out_format = va_arg(args, int32_t *);
242 *out_format = hnd->droid_format;
243 break;
244 case GRALLOC_DRM_GET_DIMENSIONS:
245 out_width = va_arg(args, uint32_t *);
246 out_height = va_arg(args, uint32_t *);
247 *out_width = hnd->width;
248 *out_height = hnd->height;
249 break;
250 case GRALLOC_DRM_GET_BACKING_STORE:
251 out_store = va_arg(args, uint64_t *);
252 ret = mod->driver->get_backing_store(handle, out_store);
253 break;
254 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900255 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700256 }
257
258 va_end(args);
259
260 return ret;
261}
262
263static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
264 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
265{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700266 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
267}
268
269static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
270 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
271{
272 int32_t ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700273 uint64_t flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700274 uint8_t *addr[DRV_MAX_PLANES];
275 auto mod = (struct gralloc0_module *)module;
276
277 auto hnd = cros_gralloc_convert_handle(handle);
278 if (!hnd) {
279 cros_gralloc_error("Invalid handle.");
280 return -EINVAL;
281 }
282
283 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
284 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
285 return -EINVAL;
286 }
287
288 flags = gralloc0_convert_flags(usage);
289 ret = mod->driver->lock(handle, fence_fd, flags, addr);
290 *vaddr = addr[0];
291 return ret;
292}
293
294static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
295 int *fence_fd)
296{
297 auto mod = (struct gralloc0_module *)module;
298 return mod->driver->unlock(handle, fence_fd);
299}
300
301static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
302 int usage, int l, int t, int w, int h,
303 struct android_ycbcr *ycbcr, int fence_fd)
304{
305 uint64_t flags;
306 int32_t ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700307 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
308 auto mod = (struct gralloc0_module *)module;
309
310 auto hnd = cros_gralloc_convert_handle(handle);
311 if (!hnd) {
312 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900313 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700314 }
315
316 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
Tomasz Figaaff29fd2017-07-05 17:50:18 +0900317 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
318 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700319 cros_gralloc_error("Non-YUV format not compatible.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900320 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700321 }
322
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700323 flags = gralloc0_convert_flags(usage);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700324 ret = mod->driver->lock(handle, fence_fd, flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900325 if (ret)
326 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700327
328 switch (hnd->format) {
329 case DRM_FORMAT_NV12:
330 ycbcr->y = addr[0];
331 ycbcr->cb = addr[1];
332 ycbcr->cr = addr[1] + 1;
333 ycbcr->ystride = hnd->strides[0];
334 ycbcr->cstride = hnd->strides[1];
335 ycbcr->chroma_step = 2;
336 break;
337 case DRM_FORMAT_YVU420:
338 case DRM_FORMAT_YVU420_ANDROID:
339 ycbcr->y = addr[0];
340 ycbcr->cb = addr[2];
341 ycbcr->cr = addr[1];
342 ycbcr->ystride = hnd->strides[0];
343 ycbcr->cstride = hnd->strides[1];
344 ycbcr->chroma_step = 1;
345 break;
346 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700347 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900348 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700349 }
350
Tomasz Figa90bb7432017-07-21 17:54:05 +0900351 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700352}
353
354static struct hw_module_methods_t gralloc0_module_methods = {.open = gralloc0_open };
355
356struct gralloc0_module HAL_MODULE_INFO_SYM = {
357 .base =
358 {
359 .common =
360 {
361 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700362 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700363 .hal_api_version = 0,
364 .id = GRALLOC_HARDWARE_MODULE_ID,
365 .name = "CrOS Gralloc",
366 .author = "Chrome OS",
367 .methods = &gralloc0_module_methods,
368 },
369
370 .registerBuffer = gralloc0_register_buffer,
371 .unregisterBuffer = gralloc0_unregister_buffer,
372 .lock = gralloc0_lock,
373 .unlock = gralloc0_unlock,
374 .perform = gralloc0_perform,
375 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700376 .lockAsync = gralloc0_lock_async,
377 .unlockAsync = gralloc0_unlock_async,
378 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700379 },
380
381 .alloc = nullptr,
382 .driver = nullptr,
383};