blob: 943c8e8a5c487249a3c3fae722e7ab699fafa3c8 [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
Gurchetan Singhbc4f0232019-06-27 20:05:54 -07007#include "../../util.h"
Gurchetan Singhd6b8b032017-05-31 14:31:31 -07008#include "../cros_gralloc_driver.h"
9
Tomasz Mikolajewskica2938a2017-11-17 20:30:56 +090010#include <cassert>
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070011#include <hardware/gralloc.h>
12#include <memory.h>
13
14struct gralloc0_module {
15 gralloc_module_t base;
16 std::unique_ptr<alloc_device_t> alloc;
17 std::unique_ptr<cros_gralloc_driver> driver;
Gurchetan Singhbcfd7582017-08-01 15:02:24 -070018 bool initialized;
19 std::mutex initialization_mutex;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070020};
21
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -070022struct cros_gralloc0_buffer_info {
23 uint32_t drm_fourcc;
24 int num_fds;
25 int fds[4];
26 uint64_t modifier;
27 uint32_t offset[4];
28 uint32_t stride[4];
29};
30
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070031/* This enumeration must match the one in <gralloc_drm.h>.
32 * The functions supported by this gralloc's temporary private API are listed
33 * below. Use of these functions is highly discouraged and should only be
34 * reserved for cases where no alternative to get same information (such as
35 * querying ANativeWindow) exists.
36 */
37// clang-format off
38enum {
39 GRALLOC_DRM_GET_STRIDE,
40 GRALLOC_DRM_GET_FORMAT,
41 GRALLOC_DRM_GET_DIMENSIONS,
42 GRALLOC_DRM_GET_BACKING_STORE,
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -070043 GRALLOC_DRM_GET_BUFFER_INFO,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070044};
45// clang-format on
46
David Stevens6116b312019-09-03 10:49:50 +090047// Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
48// passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
49// entirety, so we can detect the video decoder flag passed by IAllocator clients.
50#define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
51
Gurchetan Singha1892b22017-09-28 16:40:52 -070052static uint64_t gralloc0_convert_usage(int usage)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070053{
Gurchetan Singha1892b22017-09-28 16:40:52 -070054 uint64_t use_flags = BO_USE_NONE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070055
Gurchetan Singha1892b22017-09-28 16:40:52 -070056 if (usage & GRALLOC_USAGE_CURSOR)
57 use_flags |= BO_USE_NONE;
58 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
59 use_flags |= BO_USE_SW_READ_RARELY;
60 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
61 use_flags |= BO_USE_SW_READ_OFTEN;
62 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
63 use_flags |= BO_USE_SW_WRITE_RARELY;
64 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
65 use_flags |= BO_USE_SW_WRITE_OFTEN;
66 if (usage & GRALLOC_USAGE_HW_TEXTURE)
67 use_flags |= BO_USE_TEXTURE;
68 if (usage & GRALLOC_USAGE_HW_RENDER)
69 use_flags |= BO_USE_RENDERING;
70 if (usage & GRALLOC_USAGE_HW_2D)
71 use_flags |= BO_USE_RENDERING;
72 if (usage & GRALLOC_USAGE_HW_COMPOSER)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070073 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070074 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
75 if (usage & GRALLOC_USAGE_HW_FB)
76 use_flags |= BO_USE_NONE;
77 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070078 /*
79 * This flag potentially covers external display for the normal drivers (i915,
80 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
81 * */
Gurchetan Singha1892b22017-09-28 16:40:52 -070082 use_flags |= BO_USE_NONE;
83 if (usage & GRALLOC_USAGE_PROTECTED)
84 use_flags |= BO_USE_PROTECTED;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090085 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
86 use_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070087 /*HACK: See b/30054495 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070088 use_flags |= BO_USE_SW_READ_OFTEN;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090089 }
Gurchetan Singha1892b22017-09-28 16:40:52 -070090 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
91 use_flags |= BO_USE_CAMERA_WRITE;
92 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
93 use_flags |= BO_USE_CAMERA_READ;
94 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
95 use_flags |= BO_USE_RENDERSCRIPT;
David Stevens6116b312019-09-03 10:49:50 +090096 if (usage & BUFFER_USAGE_VIDEO_DECODER)
97 use_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070098
Gurchetan Singha1892b22017-09-28 16:40:52 -070099 return use_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700100}
101
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700102static uint32_t gralloc0_convert_map_usage(int map_usage)
103{
104 uint32_t map_flags = BO_MAP_NONE;
105
106 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
107 map_flags |= BO_MAP_READ;
108 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
109 map_flags |= BO_MAP_WRITE;
110
111 return map_flags;
112}
113
Hirokazu Honda758cf122019-12-03 11:01:59 +0900114static int gralloc0_droid_yuv_format(int droid_format)
115{
116
117 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
118 droid_format == HAL_PIXEL_FORMAT_YV12);
119}
120
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700121static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
122 buffer_handle_t *handle, int *stride)
123{
124 int32_t ret;
125 bool supported;
126 struct cros_gralloc_buffer_descriptor descriptor;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700127 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700128
129 descriptor.width = w;
130 descriptor.height = h;
131 descriptor.droid_format = format;
Jason Macnak1de7f662020-01-24 15:05:57 -0800132 descriptor.droid_usage = usage;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700133 descriptor.drm_format = cros_gralloc_convert_format(format);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700134 descriptor.use_flags = gralloc0_convert_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800135 descriptor.reserved_region_size = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700136
137 supported = mod->driver->is_supported(&descriptor);
138 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700139 descriptor.use_flags &= ~BO_USE_SCANOUT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700140 supported = mod->driver->is_supported(&descriptor);
141 }
Hirokazu Honda758cf122019-12-03 11:01:59 +0900142 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
143 !gralloc0_droid_yuv_format(format)) {
144 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
145 // because they are not input to a hw encoder but used as an
146 // intermediate format (e.g. camera).
147 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
148 supported = mod->driver->is_supported(&descriptor);
149 }
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700150
151 if (!supported) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700152 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
153 "drv_format: %4.4s, use_flags: %llu\n",
154 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
155 static_cast<unsigned long long>(descriptor.use_flags));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900156 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700157 }
158
159 ret = mod->driver->allocate(&descriptor, handle);
160 if (ret)
161 return ret;
162
163 auto hnd = cros_gralloc_convert_handle(*handle);
164 *stride = hnd->pixel_stride;
165
Tomasz Figa90bb7432017-07-21 17:54:05 +0900166 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700167}
168
169static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
170{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700171 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700172 return mod->driver->release(handle);
173}
174
175static int gralloc0_close(struct hw_device_t *dev)
176{
177 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900178 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700179}
180
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700181static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
182{
183 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
184
185 if (mod->initialized)
186 return 0;
187
188 mod->driver = std::make_unique<cros_gralloc_driver>();
189 if (mod->driver->init()) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700190 drv_log("Failed to initialize driver.\n");
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700191 return -ENODEV;
192 }
193
194 if (initialize_alloc) {
195 mod->alloc = std::make_unique<alloc_device_t>();
196 mod->alloc->alloc = gralloc0_alloc;
197 mod->alloc->free = gralloc0_free;
198 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
199 mod->alloc->common.version = 0;
200 mod->alloc->common.module = (hw_module_t *)mod;
201 mod->alloc->common.close = gralloc0_close;
202 }
203
204 mod->initialized = true;
205 return 0;
206}
207
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700208static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
209{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700210 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
211 auto module = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700212
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700213 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700214 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900215 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700216 }
217
218 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700219 drv_log("Incorrect device name - %s.\n", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900220 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700221 }
222
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700223 if (gralloc0_init(module, true))
224 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700225
226 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900227 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700228}
229
230static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
231{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700232 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
233 auto mod = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700234
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700235 if (!mod->initialized)
236 if (gralloc0_init(mod, false))
237 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700238
239 return mod->driver->retain(handle);
240}
241
242static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
243{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700244 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700245 return mod->driver->release(handle);
246}
247
248static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
249 int l, int t, int w, int h, void **vaddr)
250{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700251 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700252}
253
254static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
255{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700256 int32_t fence_fd, ret;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700257 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700258 ret = mod->driver->unlock(handle, &fence_fd);
259 if (ret)
260 return ret;
261
Jason Macnak1de7f662020-01-24 15:05:57 -0800262 ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700263 if (ret)
264 return ret;
265
266 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700267}
268
269static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
270{
271 va_list args;
272 int32_t *out_format, ret;
273 uint64_t *out_store;
274 buffer_handle_t handle;
275 uint32_t *out_width, *out_height, *out_stride;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700276 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
277 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700278 struct cros_gralloc0_buffer_info *info;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700279 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700280
281 switch (op) {
282 case GRALLOC_DRM_GET_STRIDE:
283 case GRALLOC_DRM_GET_FORMAT:
284 case GRALLOC_DRM_GET_DIMENSIONS:
285 case GRALLOC_DRM_GET_BACKING_STORE:
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700286 case GRALLOC_DRM_GET_BUFFER_INFO:
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700287 break;
288 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900289 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700290 }
291
292 va_start(args, op);
293
Tomasz Figa90bb7432017-07-21 17:54:05 +0900294 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700295 handle = va_arg(args, buffer_handle_t);
296 auto hnd = cros_gralloc_convert_handle(handle);
297 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700298 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900299 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700300 }
301
302 switch (op) {
303 case GRALLOC_DRM_GET_STRIDE:
304 out_stride = va_arg(args, uint32_t *);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700305 ret = mod->driver->resource_info(handle, strides, offsets);
306 if (ret)
307 break;
308
309 if (strides[0] != hnd->strides[0]) {
310 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
311 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
312 } else {
313 *out_stride = hnd->pixel_stride;
314 }
315
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700316 break;
317 case GRALLOC_DRM_GET_FORMAT:
318 out_format = va_arg(args, int32_t *);
319 *out_format = hnd->droid_format;
320 break;
321 case GRALLOC_DRM_GET_DIMENSIONS:
322 out_width = va_arg(args, uint32_t *);
323 out_height = va_arg(args, uint32_t *);
324 *out_width = hnd->width;
325 *out_height = hnd->height;
326 break;
327 case GRALLOC_DRM_GET_BACKING_STORE:
328 out_store = va_arg(args, uint64_t *);
329 ret = mod->driver->get_backing_store(handle, out_store);
330 break;
Kristian H. Kristensenff5ffe62020-08-12 15:16:33 -0700331 case GRALLOC_DRM_GET_BUFFER_INFO:
332 info = va_arg(args, struct cros_gralloc0_buffer_info *);
333 info->drm_fourcc = hnd->format;
334 info->num_fds = hnd->num_planes;
335 info->modifier = hnd->format_modifier;
336 for (uint32_t i = 0; i < hnd->num_planes; i++) {
337 info->fds[i] = hnd->fds[i];
338 info->offset[i] = hnd->offsets[i];
339 info->stride[i] = hnd->strides[i];
340 }
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -0700341 break;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700342 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900343 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700344 }
345
346 va_end(args);
347
348 return ret;
349}
350
351static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
352 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
353{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700354 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
355}
356
357static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
358 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
359{
360 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700361 uint32_t map_flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700362 uint8_t *addr[DRV_MAX_PLANES];
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700363 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700364 struct rectangle rect = { .x = static_cast<uint32_t>(l),
365 .y = static_cast<uint32_t>(t),
366 .width = static_cast<uint32_t>(w),
367 .height = static_cast<uint32_t>(h) };
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700368
369 auto hnd = cros_gralloc_convert_handle(handle);
370 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700371 drv_log("Invalid handle.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700372 return -EINVAL;
373 }
374
375 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700376 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700377 return -EINVAL;
378 }
379
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800380 assert(l >= 0);
381 assert(t >= 0);
382 assert(w >= 0);
383 assert(h >= 0);
384
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700385 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800386 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700387 *vaddr = addr[0];
388 return ret;
389}
390
391static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
392 int *fence_fd)
393{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700394 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700395 return mod->driver->unlock(handle, fence_fd);
396}
397
398static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
399 int usage, int l, int t, int w, int h,
400 struct android_ycbcr *ycbcr, int fence_fd)
401{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700402 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700403 uint32_t map_flags;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700404 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
405 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700406 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700407 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700408 struct rectangle rect = { .x = static_cast<uint32_t>(l),
409 .y = static_cast<uint32_t>(t),
410 .width = static_cast<uint32_t>(w),
411 .height = static_cast<uint32_t>(h) };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700412
413 auto hnd = cros_gralloc_convert_handle(handle);
414 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700415 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900416 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700417 }
418
Hirokazu Honda758cf122019-12-03 11:01:59 +0900419 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
420 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700421 drv_log("Non-YUV format not compatible.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900422 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700423 }
424
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800425 assert(l >= 0);
426 assert(t >= 0);
427 assert(w >= 0);
428 assert(h >= 0);
429
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700430 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800431 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900432 if (ret)
433 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700434
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700435 if (!map_flags) {
436 ret = mod->driver->resource_info(handle, strides, offsets);
437 if (ret)
438 return ret;
439
440 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
Jason Macnaka03926e2020-05-14 10:57:17 -0700441 addr[plane] =
442 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700443 }
444
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700445 switch (hnd->format) {
446 case DRM_FORMAT_NV12:
447 ycbcr->y = addr[0];
448 ycbcr->cb = addr[1];
449 ycbcr->cr = addr[1] + 1;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700450 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
451 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700452 ycbcr->chroma_step = 2;
453 break;
454 case DRM_FORMAT_YVU420:
455 case DRM_FORMAT_YVU420_ANDROID:
456 ycbcr->y = addr[0];
457 ycbcr->cb = addr[2];
458 ycbcr->cr = addr[1];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700459 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
460 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700461 ycbcr->chroma_step = 1;
462 break;
463 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700464 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900465 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700466 }
467
Tomasz Figa90bb7432017-07-21 17:54:05 +0900468 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700469}
470
Tomasz Figa4df286c2017-08-02 19:35:40 +0900471// clang-format off
472static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
473// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700474
475struct gralloc0_module HAL_MODULE_INFO_SYM = {
476 .base =
477 {
478 .common =
479 {
480 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700481 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700482 .hal_api_version = 0,
483 .id = GRALLOC_HARDWARE_MODULE_ID,
484 .name = "CrOS Gralloc",
485 .author = "Chrome OS",
486 .methods = &gralloc0_module_methods,
487 },
488
489 .registerBuffer = gralloc0_register_buffer,
490 .unregisterBuffer = gralloc0_unregister_buffer,
491 .lock = gralloc0_lock,
492 .unlock = gralloc0_unlock,
493 .perform = gralloc0_perform,
494 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700495 .lockAsync = gralloc0_lock_async,
496 .unlockAsync = gralloc0_unlock_async,
497 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700498 },
499
500 .alloc = nullptr,
501 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700502 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700503};