blob: 72df2605f0f30cd59f2975904c71dacf2fc38f49 [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
Roman Stratiienko142dd9c2020-12-14 17:34:09 +02007#include "../../helpers.h"
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07008#include "../../util.h"
Gurchetan Singhd6b8b032017-05-31 14:31:31 -07009#include "../cros_gralloc_driver.h"
10
Tomasz Mikolajewskica2938a2017-11-17 20:30:56 +090011#include <cassert>
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070012#include <hardware/gralloc.h>
13#include <memory.h>
14
15struct gralloc0_module {
16 gralloc_module_t base;
17 std::unique_ptr<alloc_device_t> alloc;
18 std::unique_ptr<cros_gralloc_driver> driver;
Gurchetan Singhbcfd7582017-08-01 15:02:24 -070019 bool initialized;
20 std::mutex initialization_mutex;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070021};
22
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -070023struct cros_gralloc0_buffer_info {
24 uint32_t drm_fourcc;
25 int num_fds;
26 int fds[4];
27 uint64_t modifier;
28 uint32_t offset[4];
29 uint32_t stride[4];
30};
31
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070032/* This enumeration must match the one in <gralloc_drm.h>.
33 * The functions supported by this gralloc's temporary private API are listed
34 * below. Use of these functions is highly discouraged and should only be
35 * reserved for cases where no alternative to get same information (such as
36 * querying ANativeWindow) exists.
37 */
38// clang-format off
39enum {
40 GRALLOC_DRM_GET_STRIDE,
41 GRALLOC_DRM_GET_FORMAT,
42 GRALLOC_DRM_GET_DIMENSIONS,
43 GRALLOC_DRM_GET_BACKING_STORE,
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -070044 GRALLOC_DRM_GET_BUFFER_INFO,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070045};
46// clang-format on
47
David Stevens6116b312019-09-03 10:49:50 +090048// Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
49// passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
50// entirety, so we can detect the video decoder flag passed by IAllocator clients.
51#define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
52
Gurchetan Singha1892b22017-09-28 16:40:52 -070053static uint64_t gralloc0_convert_usage(int usage)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070054{
Gurchetan Singha1892b22017-09-28 16:40:52 -070055 uint64_t use_flags = BO_USE_NONE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070056
Gurchetan Singha1892b22017-09-28 16:40:52 -070057 if (usage & GRALLOC_USAGE_CURSOR)
58 use_flags |= BO_USE_NONE;
59 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
60 use_flags |= BO_USE_SW_READ_RARELY;
61 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
62 use_flags |= BO_USE_SW_READ_OFTEN;
63 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
64 use_flags |= BO_USE_SW_WRITE_RARELY;
65 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
66 use_flags |= BO_USE_SW_WRITE_OFTEN;
67 if (usage & GRALLOC_USAGE_HW_TEXTURE)
68 use_flags |= BO_USE_TEXTURE;
69 if (usage & GRALLOC_USAGE_HW_RENDER)
70 use_flags |= BO_USE_RENDERING;
71 if (usage & GRALLOC_USAGE_HW_2D)
72 use_flags |= BO_USE_RENDERING;
73 if (usage & GRALLOC_USAGE_HW_COMPOSER)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070074 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070075 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
76 if (usage & GRALLOC_USAGE_HW_FB)
77 use_flags |= BO_USE_NONE;
78 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070079 /*
80 * This flag potentially covers external display for the normal drivers (i915,
81 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
82 * */
Gurchetan Singha1892b22017-09-28 16:40:52 -070083 use_flags |= BO_USE_NONE;
Gurchetan Singhb7edf5d2020-10-16 10:28:04 -070084 /* Map this flag to linear until real HW protection is available on Android. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070085 if (usage & GRALLOC_USAGE_PROTECTED)
Gurchetan Singhb7edf5d2020-10-16 10:28:04 -070086 use_flags |= BO_USE_LINEAR;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090087 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
88 use_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070089 /*HACK: See b/30054495 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070090 use_flags |= BO_USE_SW_READ_OFTEN;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090091 }
Gurchetan Singha1892b22017-09-28 16:40:52 -070092 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
93 use_flags |= BO_USE_CAMERA_WRITE;
94 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
95 use_flags |= BO_USE_CAMERA_READ;
96 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
97 use_flags |= BO_USE_RENDERSCRIPT;
David Stevens6116b312019-09-03 10:49:50 +090098 if (usage & BUFFER_USAGE_VIDEO_DECODER)
99 use_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700100
Gurchetan Singha1892b22017-09-28 16:40:52 -0700101 return use_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700102}
103
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700104static uint32_t gralloc0_convert_map_usage(int map_usage)
105{
106 uint32_t map_flags = BO_MAP_NONE;
107
108 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
109 map_flags |= BO_MAP_READ;
110 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
111 map_flags |= BO_MAP_WRITE;
112
113 return map_flags;
114}
115
Hirokazu Honda758cf122019-12-03 11:01:59 +0900116static int gralloc0_droid_yuv_format(int droid_format)
117{
118
119 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
120 droid_format == HAL_PIXEL_FORMAT_YV12);
121}
122
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700123static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
124 buffer_handle_t *handle, int *stride)
125{
126 int32_t ret;
127 bool supported;
128 struct cros_gralloc_buffer_descriptor descriptor;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700129 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700130
131 descriptor.width = w;
132 descriptor.height = h;
133 descriptor.droid_format = format;
Jason Macnak1de7f662020-01-24 15:05:57 -0800134 descriptor.droid_usage = usage;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700135 descriptor.drm_format = cros_gralloc_convert_format(format);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700136 descriptor.use_flags = gralloc0_convert_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800137 descriptor.reserved_region_size = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700138
139 supported = mod->driver->is_supported(&descriptor);
140 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700141 descriptor.use_flags &= ~BO_USE_SCANOUT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700142 supported = mod->driver->is_supported(&descriptor);
143 }
Hirokazu Honda758cf122019-12-03 11:01:59 +0900144 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
David Stevens150b4962020-12-14 10:43:32 +0900145 format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
146 // Unmask BO_USE_HW_VIDEO_ENCODER for other formats. They are mostly
147 // intermediate formats not passed directly to the encoder (e.g.
148 // camera). YV12 is passed to the encoder component, but it is converted
149 // to YCbCr_420_888 before being passed to the hw encoder.
Hirokazu Honda758cf122019-12-03 11:01:59 +0900150 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
David Stevens150b4962020-12-14 10:43:32 +0900151 drv_log("Retrying format %u allocation without encoder flag", format);
Hirokazu Honda758cf122019-12-03 11:01:59 +0900152 supported = mod->driver->is_supported(&descriptor);
153 }
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700154
155 if (!supported) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700156 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
157 "drv_format: %4.4s, use_flags: %llu\n",
158 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
159 static_cast<unsigned long long>(descriptor.use_flags));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900160 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700161 }
162
163 ret = mod->driver->allocate(&descriptor, handle);
164 if (ret)
165 return ret;
166
167 auto hnd = cros_gralloc_convert_handle(*handle);
168 *stride = hnd->pixel_stride;
169
Tomasz Figa90bb7432017-07-21 17:54:05 +0900170 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700171}
172
173static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
174{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700175 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700176 return mod->driver->release(handle);
177}
178
179static int gralloc0_close(struct hw_device_t *dev)
180{
181 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900182 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700183}
184
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700185static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
186{
187 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
188
189 if (mod->initialized)
190 return 0;
191
192 mod->driver = std::make_unique<cros_gralloc_driver>();
193 if (mod->driver->init()) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700194 drv_log("Failed to initialize driver.\n");
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700195 return -ENODEV;
196 }
197
198 if (initialize_alloc) {
199 mod->alloc = std::make_unique<alloc_device_t>();
200 mod->alloc->alloc = gralloc0_alloc;
201 mod->alloc->free = gralloc0_free;
202 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
203 mod->alloc->common.version = 0;
204 mod->alloc->common.module = (hw_module_t *)mod;
205 mod->alloc->common.close = gralloc0_close;
206 }
207
208 mod->initialized = true;
209 return 0;
210}
211
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700212static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
213{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700214 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
215 auto module = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700216
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700217 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700218 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900219 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700220 }
221
222 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700223 drv_log("Incorrect device name - %s.\n", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900224 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700225 }
226
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700227 if (gralloc0_init(module, true))
228 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700229
230 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900231 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700232}
233
234static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
235{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700236 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
237 auto mod = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700238
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700239 if (!mod->initialized)
240 if (gralloc0_init(mod, false))
241 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700242
243 return mod->driver->retain(handle);
244}
245
246static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
247{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700248 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700249 return mod->driver->release(handle);
250}
251
252static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
253 int l, int t, int w, int h, void **vaddr)
254{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700255 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700256}
257
258static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
259{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700260 int32_t fence_fd, ret;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700261 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700262 ret = mod->driver->unlock(handle, &fence_fd);
263 if (ret)
264 return ret;
265
Jason Macnak1de7f662020-01-24 15:05:57 -0800266 ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700267 if (ret)
268 return ret;
269
270 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700271}
272
273static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
274{
275 va_list args;
276 int32_t *out_format, ret;
277 uint64_t *out_store;
278 buffer_handle_t handle;
279 uint32_t *out_width, *out_height, *out_stride;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700280 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
281 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700282 struct cros_gralloc0_buffer_info *info;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700283 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700284
285 switch (op) {
286 case GRALLOC_DRM_GET_STRIDE:
287 case GRALLOC_DRM_GET_FORMAT:
288 case GRALLOC_DRM_GET_DIMENSIONS:
289 case GRALLOC_DRM_GET_BACKING_STORE:
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700290 case GRALLOC_DRM_GET_BUFFER_INFO:
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700291 break;
292 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900293 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700294 }
295
296 va_start(args, op);
297
Tomasz Figa90bb7432017-07-21 17:54:05 +0900298 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700299 handle = va_arg(args, buffer_handle_t);
300 auto hnd = cros_gralloc_convert_handle(handle);
301 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700302 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900303 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700304 }
305
306 switch (op) {
307 case GRALLOC_DRM_GET_STRIDE:
308 out_stride = va_arg(args, uint32_t *);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700309 ret = mod->driver->resource_info(handle, strides, offsets);
310 if (ret)
311 break;
312
313 if (strides[0] != hnd->strides[0]) {
314 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
315 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
316 } else {
317 *out_stride = hnd->pixel_stride;
318 }
319
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700320 break;
321 case GRALLOC_DRM_GET_FORMAT:
322 out_format = va_arg(args, int32_t *);
323 *out_format = hnd->droid_format;
324 break;
325 case GRALLOC_DRM_GET_DIMENSIONS:
326 out_width = va_arg(args, uint32_t *);
327 out_height = va_arg(args, uint32_t *);
328 *out_width = hnd->width;
329 *out_height = hnd->height;
330 break;
331 case GRALLOC_DRM_GET_BACKING_STORE:
332 out_store = va_arg(args, uint64_t *);
333 ret = mod->driver->get_backing_store(handle, out_store);
334 break;
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700335 case GRALLOC_DRM_GET_BUFFER_INFO:
336 info = va_arg(args, struct cros_gralloc0_buffer_info *);
Roman Stratiienko142dd9c2020-12-14 17:34:09 +0200337 info->drm_fourcc = drv_get_standard_fourcc(hnd->format);
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700338 info->num_fds = hnd->num_planes;
339 info->modifier = hnd->format_modifier;
340 for (uint32_t i = 0; i < hnd->num_planes; i++) {
341 info->fds[i] = hnd->fds[i];
342 info->offset[i] = hnd->offsets[i];
343 info->stride[i] = hnd->strides[i];
344 }
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -0700345 break;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700346 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900347 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700348 }
349
350 va_end(args);
351
352 return ret;
353}
354
355static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
356 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
357{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700358 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
359}
360
361static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
362 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
363{
364 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700365 uint32_t map_flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700366 uint8_t *addr[DRV_MAX_PLANES];
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700367 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700368 struct rectangle rect = { .x = static_cast<uint32_t>(l),
369 .y = static_cast<uint32_t>(t),
370 .width = static_cast<uint32_t>(w),
371 .height = static_cast<uint32_t>(h) };
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700372
373 auto hnd = cros_gralloc_convert_handle(handle);
374 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700375 drv_log("Invalid handle.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700376 return -EINVAL;
377 }
378
379 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700380 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700381 return -EINVAL;
382 }
383
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800384 assert(l >= 0);
385 assert(t >= 0);
386 assert(w >= 0);
387 assert(h >= 0);
388
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700389 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800390 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700391 *vaddr = addr[0];
392 return ret;
393}
394
395static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
396 int *fence_fd)
397{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700398 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700399 return mod->driver->unlock(handle, fence_fd);
400}
401
402static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
403 int usage, int l, int t, int w, int h,
404 struct android_ycbcr *ycbcr, int fence_fd)
405{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700406 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700407 uint32_t map_flags;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700408 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
409 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700410 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700411 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700412 struct rectangle rect = { .x = static_cast<uint32_t>(l),
413 .y = static_cast<uint32_t>(t),
414 .width = static_cast<uint32_t>(w),
415 .height = static_cast<uint32_t>(h) };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700416
417 auto hnd = cros_gralloc_convert_handle(handle);
418 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700419 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900420 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700421 }
422
Hirokazu Honda758cf122019-12-03 11:01:59 +0900423 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
424 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700425 drv_log("Non-YUV format not compatible.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900426 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700427 }
428
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800429 assert(l >= 0);
430 assert(t >= 0);
431 assert(w >= 0);
432 assert(h >= 0);
433
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700434 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800435 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900436 if (ret)
437 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700438
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700439 if (!map_flags) {
440 ret = mod->driver->resource_info(handle, strides, offsets);
441 if (ret)
442 return ret;
443
444 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
Jason Macnaka03926e2020-05-14 10:57:17 -0700445 addr[plane] =
446 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700447 }
448
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700449 switch (hnd->format) {
450 case DRM_FORMAT_NV12:
451 ycbcr->y = addr[0];
452 ycbcr->cb = addr[1];
453 ycbcr->cr = addr[1] + 1;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700454 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
455 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700456 ycbcr->chroma_step = 2;
457 break;
458 case DRM_FORMAT_YVU420:
459 case DRM_FORMAT_YVU420_ANDROID:
460 ycbcr->y = addr[0];
461 ycbcr->cb = addr[2];
462 ycbcr->cr = addr[1];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700463 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
464 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700465 ycbcr->chroma_step = 1;
466 break;
467 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700468 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900469 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700470 }
471
Tomasz Figa90bb7432017-07-21 17:54:05 +0900472 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700473}
474
Tomasz Figa4df286c2017-08-02 19:35:40 +0900475// clang-format off
476static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
477// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700478
479struct gralloc0_module HAL_MODULE_INFO_SYM = {
480 .base =
481 {
482 .common =
483 {
484 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700485 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700486 .hal_api_version = 0,
487 .id = GRALLOC_HARDWARE_MODULE_ID,
488 .name = "CrOS Gralloc",
489 .author = "Chrome OS",
490 .methods = &gralloc0_module_methods,
491 },
492
493 .registerBuffer = gralloc0_register_buffer,
494 .unregisterBuffer = gralloc0_unregister_buffer,
495 .lock = gralloc0_lock,
496 .unlock = gralloc0_unlock,
497 .perform = gralloc0_perform,
498 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700499 .lockAsync = gralloc0_lock_async,
500 .unlockAsync = gralloc0_unlock_async,
501 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700502 },
503
504 .alloc = nullptr,
505 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700506 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700507};