blob: 533f8cac09966529e6b5ef365dc8ebec496d73be [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
35static int64_t gralloc0_convert_flags(int flags)
36{
37 uint64_t usage = BO_USE_NONE;
38
39 if (flags & GRALLOC_USAGE_CURSOR)
40 usage |= BO_USE_NONE;
41 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
42 usage |= BO_USE_SW_READ_RARELY;
43 if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
44 usage |= BO_USE_SW_READ_OFTEN;
45 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
46 usage |= BO_USE_SW_WRITE_RARELY;
47 if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
48 usage |= BO_USE_SW_WRITE_OFTEN;
49 if (flags & GRALLOC_USAGE_HW_TEXTURE)
50 usage |= BO_USE_TEXTURE;
51 if (flags & GRALLOC_USAGE_HW_RENDER)
52 usage |= BO_USE_RENDERING;
53 if (flags & GRALLOC_USAGE_HW_2D)
54 usage |= BO_USE_RENDERING;
55 if (flags & GRALLOC_USAGE_HW_COMPOSER)
56 /* HWC wants to use display hardware, but can defer to OpenGL. */
57 usage |= BO_USE_SCANOUT | BO_USE_TEXTURE;
58 if (flags & GRALLOC_USAGE_HW_FB)
59 usage |= BO_USE_NONE;
60 if (flags & 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 usage |= BO_USE_NONE;
66 if (flags & GRALLOC_USAGE_PROTECTED)
67 usage |= BO_USE_PROTECTED;
68 if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER)
69 /*HACK: See b/30054495 */
70 usage |= BO_USE_SW_READ_OFTEN;
71 if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE)
Tomasz Figafd0b0162017-07-11 18:28:02 +090072 usage |= BO_USE_CAMERA_WRITE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070073 if (flags & GRALLOC_USAGE_HW_CAMERA_READ)
Tomasz Figafd0b0162017-07-11 18:28:02 +090074 usage |= BO_USE_CAMERA_READ;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070075 if (flags & GRALLOC_USAGE_RENDERSCRIPT)
Gurchetan Singh43ba07f2017-08-03 18:34:05 -070076 usage |= BO_USE_RENDERSCRIPT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070077
78 return usage;
79}
80
81static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
82 buffer_handle_t *handle, int *stride)
83{
84 int32_t ret;
85 bool supported;
86 struct cros_gralloc_buffer_descriptor descriptor;
87 auto mod = (struct gralloc0_module *)dev->common.module;
88
89 descriptor.width = w;
90 descriptor.height = h;
91 descriptor.droid_format = format;
92 descriptor.producer_usage = descriptor.consumer_usage = usage;
93 descriptor.drm_format = cros_gralloc_convert_format(format);
94 descriptor.drv_usage = gralloc0_convert_flags(usage);
95
96 supported = mod->driver->is_supported(&descriptor);
97 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
98 descriptor.drv_usage &= ~BO_USE_SCANOUT;
99 supported = mod->driver->is_supported(&descriptor);
100 }
101
102 if (!supported) {
103 cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, "
104 "drv_format: %4.4s, drv_flags: %llu",
Tomasz Figa23b85cb2017-07-11 18:07:11 +0900105 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700106 static_cast<unsigned long long>(descriptor.drv_usage));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900107 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700108 }
109
110 ret = mod->driver->allocate(&descriptor, handle);
111 if (ret)
112 return ret;
113
114 auto hnd = cros_gralloc_convert_handle(*handle);
115 *stride = hnd->pixel_stride;
116
Tomasz Figa90bb7432017-07-21 17:54:05 +0900117 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700118}
119
120static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
121{
122 auto mod = (struct gralloc0_module *)dev->common.module;
123 return mod->driver->release(handle);
124}
125
126static int gralloc0_close(struct hw_device_t *dev)
127{
128 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900129 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700130}
131
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700132static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
133{
134 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
135
136 if (mod->initialized)
137 return 0;
138
139 mod->driver = std::make_unique<cros_gralloc_driver>();
140 if (mod->driver->init()) {
141 cros_gralloc_error("Failed to initialize driver.");
142 return -ENODEV;
143 }
144
145 if (initialize_alloc) {
146 mod->alloc = std::make_unique<alloc_device_t>();
147 mod->alloc->alloc = gralloc0_alloc;
148 mod->alloc->free = gralloc0_free;
149 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
150 mod->alloc->common.version = 0;
151 mod->alloc->common.module = (hw_module_t *)mod;
152 mod->alloc->common.close = gralloc0_close;
153 }
154
155 mod->initialized = true;
156 return 0;
157}
158
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700159static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
160{
161 auto module = (struct gralloc0_module *)mod;
162
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700163 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700164 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900165 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700166 }
167
168 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
169 cros_gralloc_error("Incorrect device name - %s.", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900170 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700171 }
172
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700173 if (gralloc0_init(module, true))
174 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700175
176 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900177 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700178}
179
180static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
181{
182 auto mod = (struct gralloc0_module *)module;
183
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700184 if (!mod->initialized)
185 if (gralloc0_init(mod, false))
186 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700187
188 return mod->driver->retain(handle);
189}
190
191static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
192{
193 auto mod = (struct gralloc0_module *)module;
194 return mod->driver->release(handle);
195}
196
197static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
198 int l, int t, int w, int h, void **vaddr)
199{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700200 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700201}
202
203static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
204{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700205 int32_t fence_fd, ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700206 auto mod = (struct gralloc0_module *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700207 ret = mod->driver->unlock(handle, &fence_fd);
208 if (ret)
209 return ret;
210
211 ret = cros_gralloc_sync_wait(fence_fd);
212 if (ret)
213 return ret;
214
215 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700216}
217
218static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
219{
220 va_list args;
221 int32_t *out_format, ret;
222 uint64_t *out_store;
223 buffer_handle_t handle;
224 uint32_t *out_width, *out_height, *out_stride;
225 auto mod = (struct gralloc0_module *)module;
226
227 switch (op) {
228 case GRALLOC_DRM_GET_STRIDE:
229 case GRALLOC_DRM_GET_FORMAT:
230 case GRALLOC_DRM_GET_DIMENSIONS:
231 case GRALLOC_DRM_GET_BACKING_STORE:
232 break;
233 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900234 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700235 }
236
237 va_start(args, op);
238
Tomasz Figa90bb7432017-07-21 17:54:05 +0900239 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700240 handle = va_arg(args, buffer_handle_t);
241 auto hnd = cros_gralloc_convert_handle(handle);
242 if (!hnd) {
243 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900244 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700245 }
246
247 switch (op) {
248 case GRALLOC_DRM_GET_STRIDE:
249 out_stride = va_arg(args, uint32_t *);
250 *out_stride = hnd->pixel_stride;
251 break;
252 case GRALLOC_DRM_GET_FORMAT:
253 out_format = va_arg(args, int32_t *);
254 *out_format = hnd->droid_format;
255 break;
256 case GRALLOC_DRM_GET_DIMENSIONS:
257 out_width = va_arg(args, uint32_t *);
258 out_height = va_arg(args, uint32_t *);
259 *out_width = hnd->width;
260 *out_height = hnd->height;
261 break;
262 case GRALLOC_DRM_GET_BACKING_STORE:
263 out_store = va_arg(args, uint64_t *);
264 ret = mod->driver->get_backing_store(handle, out_store);
265 break;
266 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900267 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700268 }
269
270 va_end(args);
271
272 return ret;
273}
274
275static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
276 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
277{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700278 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
279}
280
281static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
282 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
283{
284 int32_t ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700285 uint64_t flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700286 uint8_t *addr[DRV_MAX_PLANES];
287 auto mod = (struct gralloc0_module *)module;
288
289 auto hnd = cros_gralloc_convert_handle(handle);
290 if (!hnd) {
291 cros_gralloc_error("Invalid handle.");
292 return -EINVAL;
293 }
294
295 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
296 cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.");
297 return -EINVAL;
298 }
299
300 flags = gralloc0_convert_flags(usage);
301 ret = mod->driver->lock(handle, fence_fd, flags, addr);
302 *vaddr = addr[0];
303 return ret;
304}
305
306static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
307 int *fence_fd)
308{
309 auto mod = (struct gralloc0_module *)module;
310 return mod->driver->unlock(handle, fence_fd);
311}
312
313static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
314 int usage, int l, int t, int w, int h,
315 struct android_ycbcr *ycbcr, int fence_fd)
316{
317 uint64_t flags;
318 int32_t ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700319 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
320 auto mod = (struct gralloc0_module *)module;
321
322 auto hnd = cros_gralloc_convert_handle(handle);
323 if (!hnd) {
324 cros_gralloc_error("Invalid handle.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900325 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700326 }
327
328 if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) &&
Tomasz Figaaff29fd2017-07-05 17:50:18 +0900329 (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) &&
330 (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700331 cros_gralloc_error("Non-YUV format not compatible.");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900332 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700333 }
334
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700335 flags = gralloc0_convert_flags(usage);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700336 ret = mod->driver->lock(handle, fence_fd, flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900337 if (ret)
338 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700339
340 switch (hnd->format) {
341 case DRM_FORMAT_NV12:
342 ycbcr->y = addr[0];
343 ycbcr->cb = addr[1];
344 ycbcr->cr = addr[1] + 1;
345 ycbcr->ystride = hnd->strides[0];
346 ycbcr->cstride = hnd->strides[1];
347 ycbcr->chroma_step = 2;
348 break;
349 case DRM_FORMAT_YVU420:
350 case DRM_FORMAT_YVU420_ANDROID:
351 ycbcr->y = addr[0];
352 ycbcr->cb = addr[2];
353 ycbcr->cr = addr[1];
354 ycbcr->ystride = hnd->strides[0];
355 ycbcr->cstride = hnd->strides[1];
356 ycbcr->chroma_step = 1;
357 break;
358 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700359 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900360 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700361 }
362
Tomasz Figa90bb7432017-07-21 17:54:05 +0900363 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700364}
365
Tomasz Figa4df286c2017-08-02 19:35:40 +0900366// clang-format off
367static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
368// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700369
370struct gralloc0_module HAL_MODULE_INFO_SYM = {
371 .base =
372 {
373 .common =
374 {
375 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700376 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700377 .hal_api_version = 0,
378 .id = GRALLOC_HARDWARE_MODULE_ID,
379 .name = "CrOS Gralloc",
380 .author = "Chrome OS",
381 .methods = &gralloc0_module_methods,
382 },
383
384 .registerBuffer = gralloc0_register_buffer,
385 .unregisterBuffer = gralloc0_unregister_buffer,
386 .lock = gralloc0_lock,
387 .unlock = gralloc0_unlock,
388 .perform = gralloc0_perform,
389 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700390 .lockAsync = gralloc0_lock_async,
391 .unlockAsync = gralloc0_unlock_async,
392 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700393 },
394
395 .alloc = nullptr,
396 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700397 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700398};