blob: 2a7b87f69e067e67781b4816b00762bcebe8efb7 [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
22/* This enumeration must match the one in <gralloc_drm.h>.
23 * The functions supported by this gralloc's temporary private API are listed
24 * below. Use of these functions is highly discouraged and should only be
25 * reserved for cases where no alternative to get same information (such as
26 * querying ANativeWindow) exists.
27 */
28// clang-format off
29enum {
30 GRALLOC_DRM_GET_STRIDE,
31 GRALLOC_DRM_GET_FORMAT,
32 GRALLOC_DRM_GET_DIMENSIONS,
33 GRALLOC_DRM_GET_BACKING_STORE,
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -070034 GRALLOC_DRM_GET_MODIFIER,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070035};
36// clang-format on
37
David Stevens6116b312019-09-03 10:49:50 +090038// Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
39// passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
40// entirety, so we can detect the video decoder flag passed by IAllocator clients.
41#define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
42
Gurchetan Singha1892b22017-09-28 16:40:52 -070043static uint64_t gralloc0_convert_usage(int usage)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070044{
Gurchetan Singha1892b22017-09-28 16:40:52 -070045 uint64_t use_flags = BO_USE_NONE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070046
Gurchetan Singha1892b22017-09-28 16:40:52 -070047 if (usage & GRALLOC_USAGE_CURSOR)
48 use_flags |= BO_USE_NONE;
49 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
50 use_flags |= BO_USE_SW_READ_RARELY;
51 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
52 use_flags |= BO_USE_SW_READ_OFTEN;
53 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
54 use_flags |= BO_USE_SW_WRITE_RARELY;
55 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
56 use_flags |= BO_USE_SW_WRITE_OFTEN;
57 if (usage & GRALLOC_USAGE_HW_TEXTURE)
58 use_flags |= BO_USE_TEXTURE;
59 if (usage & GRALLOC_USAGE_HW_RENDER)
60 use_flags |= BO_USE_RENDERING;
61 if (usage & GRALLOC_USAGE_HW_2D)
62 use_flags |= BO_USE_RENDERING;
63 if (usage & GRALLOC_USAGE_HW_COMPOSER)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070064 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070065 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
66 if (usage & GRALLOC_USAGE_HW_FB)
67 use_flags |= BO_USE_NONE;
68 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070069 /*
70 * This flag potentially covers external display for the normal drivers (i915,
71 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
72 * */
Gurchetan Singha1892b22017-09-28 16:40:52 -070073 use_flags |= BO_USE_NONE;
74 if (usage & GRALLOC_USAGE_PROTECTED)
75 use_flags |= BO_USE_PROTECTED;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090076 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
77 use_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070078 /*HACK: See b/30054495 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070079 use_flags |= BO_USE_SW_READ_OFTEN;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090080 }
Gurchetan Singha1892b22017-09-28 16:40:52 -070081 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
82 use_flags |= BO_USE_CAMERA_WRITE;
83 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
84 use_flags |= BO_USE_CAMERA_READ;
85 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
86 use_flags |= BO_USE_RENDERSCRIPT;
David Stevens6116b312019-09-03 10:49:50 +090087 if (usage & BUFFER_USAGE_VIDEO_DECODER)
88 use_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070089
Gurchetan Singha1892b22017-09-28 16:40:52 -070090 return use_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070091}
92
Gurchetan Singhf7f633a2017-09-28 17:02:12 -070093static uint32_t gralloc0_convert_map_usage(int map_usage)
94{
95 uint32_t map_flags = BO_MAP_NONE;
96
97 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
98 map_flags |= BO_MAP_READ;
99 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
100 map_flags |= BO_MAP_WRITE;
101
102 return map_flags;
103}
104
Hirokazu Honda758cf122019-12-03 11:01:59 +0900105static int gralloc0_droid_yuv_format(int droid_format)
106{
107
108 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
109 droid_format == HAL_PIXEL_FORMAT_YV12);
110}
111
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700112static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
113 buffer_handle_t *handle, int *stride)
114{
115 int32_t ret;
116 bool supported;
117 struct cros_gralloc_buffer_descriptor descriptor;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700118 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700119
120 descriptor.width = w;
121 descriptor.height = h;
122 descriptor.droid_format = format;
Jason Macnak1de7f662020-01-24 15:05:57 -0800123 descriptor.droid_usage = usage;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700124 descriptor.drm_format = cros_gralloc_convert_format(format);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700125 descriptor.use_flags = gralloc0_convert_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800126 descriptor.reserved_region_size = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700127
128 supported = mod->driver->is_supported(&descriptor);
129 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700130 descriptor.use_flags &= ~BO_USE_SCANOUT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700131 supported = mod->driver->is_supported(&descriptor);
132 }
Hirokazu Honda758cf122019-12-03 11:01:59 +0900133 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
134 !gralloc0_droid_yuv_format(format)) {
135 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
136 // because they are not input to a hw encoder but used as an
137 // intermediate format (e.g. camera).
138 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
139 supported = mod->driver->is_supported(&descriptor);
140 }
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700141
142 if (!supported) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700143 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
144 "drv_format: %4.4s, use_flags: %llu\n",
145 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
146 static_cast<unsigned long long>(descriptor.use_flags));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900147 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700148 }
149
150 ret = mod->driver->allocate(&descriptor, handle);
151 if (ret)
152 return ret;
153
154 auto hnd = cros_gralloc_convert_handle(*handle);
155 *stride = hnd->pixel_stride;
156
Tomasz Figa90bb7432017-07-21 17:54:05 +0900157 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700158}
159
160static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
161{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700162 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700163 return mod->driver->release(handle);
164}
165
166static int gralloc0_close(struct hw_device_t *dev)
167{
168 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900169 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700170}
171
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700172static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
173{
174 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
175
176 if (mod->initialized)
177 return 0;
178
179 mod->driver = std::make_unique<cros_gralloc_driver>();
180 if (mod->driver->init()) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700181 drv_log("Failed to initialize driver.\n");
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700182 return -ENODEV;
183 }
184
185 if (initialize_alloc) {
186 mod->alloc = std::make_unique<alloc_device_t>();
187 mod->alloc->alloc = gralloc0_alloc;
188 mod->alloc->free = gralloc0_free;
189 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
190 mod->alloc->common.version = 0;
191 mod->alloc->common.module = (hw_module_t *)mod;
192 mod->alloc->common.close = gralloc0_close;
193 }
194
195 mod->initialized = true;
196 return 0;
197}
198
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700199static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
200{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700201 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
202 auto module = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700203
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700204 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700205 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900206 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700207 }
208
209 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700210 drv_log("Incorrect device name - %s.\n", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900211 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700212 }
213
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700214 if (gralloc0_init(module, true))
215 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700216
217 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900218 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700219}
220
221static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
222{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700223 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
224 auto mod = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700225
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700226 if (!mod->initialized)
227 if (gralloc0_init(mod, false))
228 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700229
230 return mod->driver->retain(handle);
231}
232
233static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
234{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700235 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700236 return mod->driver->release(handle);
237}
238
239static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
240 int l, int t, int w, int h, void **vaddr)
241{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700242 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700243}
244
245static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
246{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700247 int32_t fence_fd, ret;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700248 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700249 ret = mod->driver->unlock(handle, &fence_fd);
250 if (ret)
251 return ret;
252
Jason Macnak1de7f662020-01-24 15:05:57 -0800253 ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700254 if (ret)
255 return ret;
256
257 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700258}
259
260static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
261{
262 va_list args;
263 int32_t *out_format, ret;
264 uint64_t *out_store;
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -0700265 uint64_t *out_modifier;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700266 buffer_handle_t handle;
267 uint32_t *out_width, *out_height, *out_stride;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700268 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
269 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700270 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700271
272 switch (op) {
273 case GRALLOC_DRM_GET_STRIDE:
274 case GRALLOC_DRM_GET_FORMAT:
275 case GRALLOC_DRM_GET_DIMENSIONS:
276 case GRALLOC_DRM_GET_BACKING_STORE:
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -0700277 case GRALLOC_DRM_GET_MODIFIER:
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700278 break;
279 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900280 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700281 }
282
283 va_start(args, op);
284
Tomasz Figa90bb7432017-07-21 17:54:05 +0900285 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700286 handle = va_arg(args, buffer_handle_t);
287 auto hnd = cros_gralloc_convert_handle(handle);
288 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700289 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900290 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700291 }
292
293 switch (op) {
294 case GRALLOC_DRM_GET_STRIDE:
295 out_stride = va_arg(args, uint32_t *);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700296 ret = mod->driver->resource_info(handle, strides, offsets);
297 if (ret)
298 break;
299
300 if (strides[0] != hnd->strides[0]) {
301 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
302 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
303 } else {
304 *out_stride = hnd->pixel_stride;
305 }
306
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700307 break;
308 case GRALLOC_DRM_GET_FORMAT:
309 out_format = va_arg(args, int32_t *);
310 *out_format = hnd->droid_format;
311 break;
312 case GRALLOC_DRM_GET_DIMENSIONS:
313 out_width = va_arg(args, uint32_t *);
314 out_height = va_arg(args, uint32_t *);
315 *out_width = hnd->width;
316 *out_height = hnd->height;
317 break;
318 case GRALLOC_DRM_GET_BACKING_STORE:
319 out_store = va_arg(args, uint64_t *);
320 ret = mod->driver->get_backing_store(handle, out_store);
321 break;
Kristian H. Kristensene77c32c2020-07-23 16:04:47 -0700322 case GRALLOC_DRM_GET_MODIFIER:
323 out_modifier = va_arg(args, uint64_t *);
324 *out_modifier = hnd->format_modifier;
325 break;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700326 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900327 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700328 }
329
330 va_end(args);
331
332 return ret;
333}
334
335static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
336 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
337{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700338 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
339}
340
341static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
342 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
343{
344 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700345 uint32_t map_flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700346 uint8_t *addr[DRV_MAX_PLANES];
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700347 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700348 struct rectangle rect = { .x = static_cast<uint32_t>(l),
349 .y = static_cast<uint32_t>(t),
350 .width = static_cast<uint32_t>(w),
351 .height = static_cast<uint32_t>(h) };
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700352
353 auto hnd = cros_gralloc_convert_handle(handle);
354 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700355 drv_log("Invalid handle.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700356 return -EINVAL;
357 }
358
359 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700360 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700361 return -EINVAL;
362 }
363
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800364 assert(l >= 0);
365 assert(t >= 0);
366 assert(w >= 0);
367 assert(h >= 0);
368
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700369 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800370 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700371 *vaddr = addr[0];
372 return ret;
373}
374
375static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
376 int *fence_fd)
377{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700378 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700379 return mod->driver->unlock(handle, fence_fd);
380}
381
382static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
383 int usage, int l, int t, int w, int h,
384 struct android_ycbcr *ycbcr, int fence_fd)
385{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700386 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700387 uint32_t map_flags;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700388 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
389 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700390 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700391 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700392 struct rectangle rect = { .x = static_cast<uint32_t>(l),
393 .y = static_cast<uint32_t>(t),
394 .width = static_cast<uint32_t>(w),
395 .height = static_cast<uint32_t>(h) };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700396
397 auto hnd = cros_gralloc_convert_handle(handle);
398 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700399 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900400 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700401 }
402
Hirokazu Honda758cf122019-12-03 11:01:59 +0900403 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
404 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700405 drv_log("Non-YUV format not compatible.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900406 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700407 }
408
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800409 assert(l >= 0);
410 assert(t >= 0);
411 assert(w >= 0);
412 assert(h >= 0);
413
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700414 map_flags = gralloc0_convert_map_usage(usage);
Jason Macnak1de7f662020-01-24 15:05:57 -0800415 ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900416 if (ret)
417 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700418
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700419 if (!map_flags) {
420 ret = mod->driver->resource_info(handle, strides, offsets);
421 if (ret)
422 return ret;
423
424 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
Jason Macnaka03926e2020-05-14 10:57:17 -0700425 addr[plane] =
426 reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane]));
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700427 }
428
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700429 switch (hnd->format) {
430 case DRM_FORMAT_NV12:
431 ycbcr->y = addr[0];
432 ycbcr->cb = addr[1];
433 ycbcr->cr = addr[1] + 1;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700434 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
435 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700436 ycbcr->chroma_step = 2;
437 break;
438 case DRM_FORMAT_YVU420:
439 case DRM_FORMAT_YVU420_ANDROID:
440 ycbcr->y = addr[0];
441 ycbcr->cb = addr[2];
442 ycbcr->cr = addr[1];
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700443 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
444 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700445 ycbcr->chroma_step = 1;
446 break;
447 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700448 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900449 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700450 }
451
Tomasz Figa90bb7432017-07-21 17:54:05 +0900452 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700453}
454
Tomasz Figa4df286c2017-08-02 19:35:40 +0900455// clang-format off
456static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
457// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700458
459struct gralloc0_module HAL_MODULE_INFO_SYM = {
460 .base =
461 {
462 .common =
463 {
464 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700465 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700466 .hal_api_version = 0,
467 .id = GRALLOC_HARDWARE_MODULE_ID,
468 .name = "CrOS Gralloc",
469 .author = "Chrome OS",
470 .methods = &gralloc0_module_methods,
471 },
472
473 .registerBuffer = gralloc0_register_buffer,
474 .unregisterBuffer = gralloc0_unregister_buffer,
475 .lock = gralloc0_lock,
476 .unlock = gralloc0_unlock,
477 .perform = gralloc0_perform,
478 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700479 .lockAsync = gralloc0_lock_async,
480 .unlockAsync = gralloc0_unlock_async,
481 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700482 },
483
484 .alloc = nullptr,
485 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700486 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700487};