blob: 6c49d3ae3e1d53da0912f855c9ded71ef2960c49 [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,
34};
35// clang-format on
36
David Stevens6116b312019-09-03 10:49:50 +090037// Gralloc0 doesn't define a video decoder flag. However, the IAllocator gralloc0
38// passthrough gives the low 32-bits of the BufferUsage flags to gralloc0 in their
39// entirety, so we can detect the video decoder flag passed by IAllocator clients.
40#define BUFFER_USAGE_VIDEO_DECODER (1 << 22)
41
Gurchetan Singha1892b22017-09-28 16:40:52 -070042static uint64_t gralloc0_convert_usage(int usage)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070043{
Gurchetan Singha1892b22017-09-28 16:40:52 -070044 uint64_t use_flags = BO_USE_NONE;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070045
Gurchetan Singha1892b22017-09-28 16:40:52 -070046 if (usage & GRALLOC_USAGE_CURSOR)
47 use_flags |= BO_USE_NONE;
48 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY)
49 use_flags |= BO_USE_SW_READ_RARELY;
50 if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)
51 use_flags |= BO_USE_SW_READ_OFTEN;
52 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY)
53 use_flags |= BO_USE_SW_WRITE_RARELY;
54 if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN)
55 use_flags |= BO_USE_SW_WRITE_OFTEN;
56 if (usage & GRALLOC_USAGE_HW_TEXTURE)
57 use_flags |= BO_USE_TEXTURE;
58 if (usage & GRALLOC_USAGE_HW_RENDER)
59 use_flags |= BO_USE_RENDERING;
60 if (usage & GRALLOC_USAGE_HW_2D)
61 use_flags |= BO_USE_RENDERING;
62 if (usage & GRALLOC_USAGE_HW_COMPOSER)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070063 /* HWC wants to use display hardware, but can defer to OpenGL. */
Gurchetan Singha1892b22017-09-28 16:40:52 -070064 use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE;
65 if (usage & GRALLOC_USAGE_HW_FB)
66 use_flags |= BO_USE_NONE;
67 if (usage & GRALLOC_USAGE_EXTERNAL_DISP)
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070068 /*
69 * This flag potentially covers external display for the normal drivers (i915,
70 * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it.
71 * */
Gurchetan Singha1892b22017-09-28 16:40:52 -070072 use_flags |= BO_USE_NONE;
73 if (usage & GRALLOC_USAGE_PROTECTED)
74 use_flags |= BO_USE_PROTECTED;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090075 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
76 use_flags |= BO_USE_HW_VIDEO_ENCODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070077 /*HACK: See b/30054495 */
Gurchetan Singha1892b22017-09-28 16:40:52 -070078 use_flags |= BO_USE_SW_READ_OFTEN;
Hirokazu Honda5e55f952019-11-08 12:57:55 +090079 }
Gurchetan Singha1892b22017-09-28 16:40:52 -070080 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
81 use_flags |= BO_USE_CAMERA_WRITE;
82 if (usage & GRALLOC_USAGE_HW_CAMERA_READ)
83 use_flags |= BO_USE_CAMERA_READ;
84 if (usage & GRALLOC_USAGE_RENDERSCRIPT)
85 use_flags |= BO_USE_RENDERSCRIPT;
David Stevens6116b312019-09-03 10:49:50 +090086 if (usage & BUFFER_USAGE_VIDEO_DECODER)
87 use_flags |= BO_USE_HW_VIDEO_DECODER;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070088
Gurchetan Singha1892b22017-09-28 16:40:52 -070089 return use_flags;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -070090}
91
Gurchetan Singhf7f633a2017-09-28 17:02:12 -070092static uint32_t gralloc0_convert_map_usage(int map_usage)
93{
94 uint32_t map_flags = BO_MAP_NONE;
95
96 if (map_usage & GRALLOC_USAGE_SW_READ_MASK)
97 map_flags |= BO_MAP_READ;
98 if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK)
99 map_flags |= BO_MAP_WRITE;
100
101 return map_flags;
102}
103
Hirokazu Honda758cf122019-12-03 11:01:59 +0900104static int gralloc0_droid_yuv_format(int droid_format)
105{
106
107 return (droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888 ||
108 droid_format == HAL_PIXEL_FORMAT_YV12);
109}
110
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700111static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage,
112 buffer_handle_t *handle, int *stride)
113{
114 int32_t ret;
115 bool supported;
116 struct cros_gralloc_buffer_descriptor descriptor;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700117 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700118
119 descriptor.width = w;
120 descriptor.height = h;
121 descriptor.droid_format = format;
122 descriptor.producer_usage = descriptor.consumer_usage = usage;
123 descriptor.drm_format = cros_gralloc_convert_format(format);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700124 descriptor.use_flags = gralloc0_convert_usage(usage);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700125
126 supported = mod->driver->is_supported(&descriptor);
127 if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) {
Gurchetan Singha1892b22017-09-28 16:40:52 -0700128 descriptor.use_flags &= ~BO_USE_SCANOUT;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700129 supported = mod->driver->is_supported(&descriptor);
130 }
Hirokazu Honda758cf122019-12-03 11:01:59 +0900131 if (!supported && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
132 !gralloc0_droid_yuv_format(format)) {
133 // Unmask BO_USE_HW_VIDEO_ENCODER in the case of non-yuv formats
134 // because they are not input to a hw encoder but used as an
135 // intermediate format (e.g. camera).
136 descriptor.use_flags &= ~BO_USE_HW_VIDEO_ENCODER;
137 supported = mod->driver->is_supported(&descriptor);
138 }
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700139
140 if (!supported) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700141 drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, "
142 "drv_format: %4.4s, use_flags: %llu\n",
143 format, usage, reinterpret_cast<char *>(&descriptor.drm_format),
144 static_cast<unsigned long long>(descriptor.use_flags));
Tomasz Figa90bb7432017-07-21 17:54:05 +0900145 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700146 }
147
148 ret = mod->driver->allocate(&descriptor, handle);
149 if (ret)
150 return ret;
151
152 auto hnd = cros_gralloc_convert_handle(*handle);
153 *stride = hnd->pixel_stride;
154
Tomasz Figa90bb7432017-07-21 17:54:05 +0900155 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700156}
157
158static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle)
159{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700160 auto mod = (struct gralloc0_module const *)dev->common.module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700161 return mod->driver->release(handle);
162}
163
164static int gralloc0_close(struct hw_device_t *dev)
165{
166 /* Memory is freed by managed pointers on process close. */
Tomasz Figa90bb7432017-07-21 17:54:05 +0900167 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700168}
169
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700170static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc)
171{
172 std::lock_guard<std::mutex> lock(mod->initialization_mutex);
173
174 if (mod->initialized)
175 return 0;
176
177 mod->driver = std::make_unique<cros_gralloc_driver>();
178 if (mod->driver->init()) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700179 drv_log("Failed to initialize driver.\n");
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700180 return -ENODEV;
181 }
182
183 if (initialize_alloc) {
184 mod->alloc = std::make_unique<alloc_device_t>();
185 mod->alloc->alloc = gralloc0_alloc;
186 mod->alloc->free = gralloc0_free;
187 mod->alloc->common.tag = HARDWARE_DEVICE_TAG;
188 mod->alloc->common.version = 0;
189 mod->alloc->common.module = (hw_module_t *)mod;
190 mod->alloc->common.close = gralloc0_close;
191 }
192
193 mod->initialized = true;
194 return 0;
195}
196
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700197static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev)
198{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700199 auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod);
200 auto module = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700201
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700202 if (module->initialized) {
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700203 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900204 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700205 }
206
207 if (strcmp(name, GRALLOC_HARDWARE_GPU0)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700208 drv_log("Incorrect device name - %s.\n", name);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900209 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700210 }
211
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700212 if (gralloc0_init(module, true))
213 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700214
215 *dev = &module->alloc->common;
Tomasz Figa90bb7432017-07-21 17:54:05 +0900216 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700217}
218
219static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
220{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700221 auto const_module = reinterpret_cast<const struct gralloc0_module *>(module);
222 auto mod = const_cast<struct gralloc0_module *>(const_module);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700223
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700224 if (!mod->initialized)
225 if (gralloc0_init(mod, false))
226 return -ENODEV;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700227
228 return mod->driver->retain(handle);
229}
230
231static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle)
232{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700233 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700234 return mod->driver->release(handle);
235}
236
237static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage,
238 int l, int t, int w, int h, void **vaddr)
239{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700240 return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1);
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700241}
242
243static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle)
244{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700245 int32_t fence_fd, ret;
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700246 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700247 ret = mod->driver->unlock(handle, &fence_fd);
248 if (ret)
249 return ret;
250
251 ret = cros_gralloc_sync_wait(fence_fd);
252 if (ret)
253 return ret;
254
255 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700256}
257
258static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...)
259{
260 va_list args;
261 int32_t *out_format, ret;
262 uint64_t *out_store;
263 buffer_handle_t handle;
264 uint32_t *out_width, *out_height, *out_stride;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700265 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
266 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700267 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700268
269 switch (op) {
270 case GRALLOC_DRM_GET_STRIDE:
271 case GRALLOC_DRM_GET_FORMAT:
272 case GRALLOC_DRM_GET_DIMENSIONS:
273 case GRALLOC_DRM_GET_BACKING_STORE:
274 break;
275 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900276 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700277 }
278
279 va_start(args, op);
280
Tomasz Figa90bb7432017-07-21 17:54:05 +0900281 ret = 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700282 handle = va_arg(args, buffer_handle_t);
283 auto hnd = cros_gralloc_convert_handle(handle);
284 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700285 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900286 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700287 }
288
289 switch (op) {
290 case GRALLOC_DRM_GET_STRIDE:
291 out_stride = va_arg(args, uint32_t *);
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700292 ret = mod->driver->resource_info(handle, strides, offsets);
293 if (ret)
294 break;
295
296 if (strides[0] != hnd->strides[0]) {
297 uint32_t bytes_per_pixel = drv_bytes_per_pixel_from_format(hnd->format, 0);
298 *out_stride = DIV_ROUND_UP(strides[0], bytes_per_pixel);
299 } else {
300 *out_stride = hnd->pixel_stride;
301 }
302
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700303 break;
304 case GRALLOC_DRM_GET_FORMAT:
305 out_format = va_arg(args, int32_t *);
306 *out_format = hnd->droid_format;
307 break;
308 case GRALLOC_DRM_GET_DIMENSIONS:
309 out_width = va_arg(args, uint32_t *);
310 out_height = va_arg(args, uint32_t *);
311 *out_width = hnd->width;
312 *out_height = hnd->height;
313 break;
314 case GRALLOC_DRM_GET_BACKING_STORE:
315 out_store = va_arg(args, uint64_t *);
316 ret = mod->driver->get_backing_store(handle, out_store);
317 break;
318 default:
Tomasz Figa90bb7432017-07-21 17:54:05 +0900319 ret = -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700320 }
321
322 va_end(args);
323
324 return ret;
325}
326
327static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
328 int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr)
329{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700330 return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1);
331}
332
333static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
334 int usage, int l, int t, int w, int h, void **vaddr, int fence_fd)
335{
336 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700337 uint32_t map_flags;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700338 uint8_t *addr[DRV_MAX_PLANES];
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700339 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700340 struct rectangle rect = { .x = static_cast<uint32_t>(l),
341 .y = static_cast<uint32_t>(t),
342 .width = static_cast<uint32_t>(w),
343 .height = static_cast<uint32_t>(h) };
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700344
345 auto hnd = cros_gralloc_convert_handle(handle);
346 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700347 drv_log("Invalid handle.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700348 return -EINVAL;
349 }
350
351 if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700352 drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n");
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700353 return -EINVAL;
354 }
355
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800356 assert(l >= 0);
357 assert(t >= 0);
358 assert(w >= 0);
359 assert(h >= 0);
360
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700361 map_flags = gralloc0_convert_map_usage(usage);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800362 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700363 *vaddr = addr[0];
364 return ret;
365}
366
367static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle,
368 int *fence_fd)
369{
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700370 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700371 return mod->driver->unlock(handle, fence_fd);
372}
373
374static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle,
375 int usage, int l, int t, int w, int h,
376 struct android_ycbcr *ycbcr, int fence_fd)
377{
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700378 int32_t ret;
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700379 uint32_t map_flags;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700380 uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
381 uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700382 uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr };
Alistair Strachanf8ff0f52018-04-09 18:49:51 -0700383 auto mod = (struct gralloc0_module const *)module;
Gurchetan Singh2b1d6892018-09-17 16:58:16 -0700384 struct rectangle rect = { .x = static_cast<uint32_t>(l),
385 .y = static_cast<uint32_t>(t),
386 .width = static_cast<uint32_t>(w),
387 .height = static_cast<uint32_t>(h) };
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700388
389 auto hnd = cros_gralloc_convert_handle(handle);
390 if (!hnd) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700391 drv_log("Invalid handle.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900392 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700393 }
394
Hirokazu Honda758cf122019-12-03 11:01:59 +0900395 if (!gralloc0_droid_yuv_format(hnd->droid_format) &&
396 hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700397 drv_log("Non-YUV format not compatible.\n");
Tomasz Figa90bb7432017-07-21 17:54:05 +0900398 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700399 }
400
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800401 assert(l >= 0);
402 assert(t >= 0);
403 assert(w >= 0);
404 assert(h >= 0);
405
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700406 map_flags = gralloc0_convert_map_usage(usage);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800407 ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr);
Tomasz Figaaddd5f22017-07-05 17:50:18 +0900408 if (ret)
409 return ret;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700410
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700411 if (!map_flags) {
412 ret = mod->driver->resource_info(handle, strides, offsets);
413 if (ret)
414 return ret;
415
416 for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
417 addr[plane] = static_cast<uint8_t *>(nullptr) + offsets[plane];
418 }
419
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700420 switch (hnd->format) {
421 case DRM_FORMAT_NV12:
422 ycbcr->y = addr[0];
423 ycbcr->cb = addr[1];
424 ycbcr->cr = addr[1] + 1;
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700425 ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0];
426 ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1];
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700427 ycbcr->chroma_step = 2;
428 break;
429 case DRM_FORMAT_YVU420:
430 case DRM_FORMAT_YVU420_ANDROID:
431 ycbcr->y = addr[0];
432 ycbcr->cb = addr[2];
433 ycbcr->cr = addr[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 = 1;
437 break;
438 default:
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700439 module->unlock(module, handle);
Tomasz Figa90bb7432017-07-21 17:54:05 +0900440 return -EINVAL;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700441 }
442
Tomasz Figa90bb7432017-07-21 17:54:05 +0900443 return 0;
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700444}
445
Tomasz Figa4df286c2017-08-02 19:35:40 +0900446// clang-format off
447static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open };
448// clang-format on
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700449
450struct gralloc0_module HAL_MODULE_INFO_SYM = {
451 .base =
452 {
453 .common =
454 {
455 .tag = HARDWARE_MODULE_TAG,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700456 .module_api_version = GRALLOC_MODULE_API_VERSION_0_3,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700457 .hal_api_version = 0,
458 .id = GRALLOC_HARDWARE_MODULE_ID,
459 .name = "CrOS Gralloc",
460 .author = "Chrome OS",
461 .methods = &gralloc0_module_methods,
462 },
463
464 .registerBuffer = gralloc0_register_buffer,
465 .unregisterBuffer = gralloc0_unregister_buffer,
466 .lock = gralloc0_lock,
467 .unlock = gralloc0_unlock,
468 .perform = gralloc0_perform,
469 .lock_ycbcr = gralloc0_lock_ycbcr,
Gurchetan Singh4b5d0bf2017-06-22 18:38:37 -0700470 .lockAsync = gralloc0_lock_async,
471 .unlockAsync = gralloc0_unlock_async,
472 .lockAsync_ycbcr = gralloc0_lock_async_ycbcr,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700473 },
474
475 .alloc = nullptr,
476 .driver = nullptr,
Gurchetan Singhbcfd7582017-08-01 15:02:24 -0700477 .initialized = false,
Gurchetan Singhd6b8b032017-05-31 14:31:31 -0700478};