Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Tomasz Mikolajewski | ca2938a | 2017-11-17 20:30:56 +0900 | [diff] [blame] | 9 | #include <cassert> |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 10 | #include <hardware/gralloc.h> |
| 11 | #include <memory.h> |
| 12 | |
| 13 | struct gralloc0_module { |
| 14 | gralloc_module_t base; |
| 15 | std::unique_ptr<alloc_device_t> alloc; |
| 16 | std::unique_ptr<cros_gralloc_driver> driver; |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 17 | bool initialized; |
| 18 | std::mutex initialization_mutex; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | /* This enumeration must match the one in <gralloc_drm.h>. |
| 22 | * The functions supported by this gralloc's temporary private API are listed |
| 23 | * below. Use of these functions is highly discouraged and should only be |
| 24 | * reserved for cases where no alternative to get same information (such as |
| 25 | * querying ANativeWindow) exists. |
| 26 | */ |
| 27 | // clang-format off |
| 28 | enum { |
| 29 | GRALLOC_DRM_GET_STRIDE, |
| 30 | GRALLOC_DRM_GET_FORMAT, |
| 31 | GRALLOC_DRM_GET_DIMENSIONS, |
| 32 | GRALLOC_DRM_GET_BACKING_STORE, |
| 33 | }; |
| 34 | // clang-format on |
| 35 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 36 | static uint64_t gralloc0_convert_usage(int usage) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 37 | { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 38 | uint64_t use_flags = BO_USE_NONE; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 39 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 40 | if (usage & GRALLOC_USAGE_CURSOR) |
| 41 | use_flags |= BO_USE_NONE; |
| 42 | if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY) |
| 43 | use_flags |= BO_USE_SW_READ_RARELY; |
| 44 | if ((usage & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN) |
| 45 | use_flags |= BO_USE_SW_READ_OFTEN; |
| 46 | if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) |
| 47 | use_flags |= BO_USE_SW_WRITE_RARELY; |
| 48 | if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN) |
| 49 | use_flags |= BO_USE_SW_WRITE_OFTEN; |
| 50 | if (usage & GRALLOC_USAGE_HW_TEXTURE) |
| 51 | use_flags |= BO_USE_TEXTURE; |
| 52 | if (usage & GRALLOC_USAGE_HW_RENDER) |
| 53 | use_flags |= BO_USE_RENDERING; |
| 54 | if (usage & GRALLOC_USAGE_HW_2D) |
| 55 | use_flags |= BO_USE_RENDERING; |
| 56 | if (usage & GRALLOC_USAGE_HW_COMPOSER) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 57 | /* HWC wants to use display hardware, but can defer to OpenGL. */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 58 | use_flags |= BO_USE_SCANOUT | BO_USE_TEXTURE; |
| 59 | if (usage & GRALLOC_USAGE_HW_FB) |
| 60 | use_flags |= BO_USE_NONE; |
| 61 | if (usage & GRALLOC_USAGE_EXTERNAL_DISP) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 62 | /* |
| 63 | * This flag potentially covers external display for the normal drivers (i915, |
| 64 | * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it. |
| 65 | * */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 66 | use_flags |= BO_USE_NONE; |
| 67 | if (usage & GRALLOC_USAGE_PROTECTED) |
| 68 | use_flags |= BO_USE_PROTECTED; |
| 69 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 70 | /*HACK: See b/30054495 */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 71 | use_flags |= BO_USE_SW_READ_OFTEN; |
| 72 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) |
| 73 | use_flags |= BO_USE_CAMERA_WRITE; |
| 74 | if (usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 75 | use_flags |= BO_USE_CAMERA_READ; |
| 76 | if (usage & GRALLOC_USAGE_RENDERSCRIPT) |
| 77 | use_flags |= BO_USE_RENDERSCRIPT; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 78 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 79 | return use_flags; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 82 | static uint32_t gralloc0_convert_map_usage(int map_usage) |
| 83 | { |
| 84 | uint32_t map_flags = BO_MAP_NONE; |
| 85 | |
| 86 | if (map_usage & GRALLOC_USAGE_SW_READ_MASK) |
| 87 | map_flags |= BO_MAP_READ; |
| 88 | if (map_usage & GRALLOC_USAGE_SW_WRITE_MASK) |
| 89 | map_flags |= BO_MAP_WRITE; |
| 90 | |
| 91 | return map_flags; |
| 92 | } |
| 93 | |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 94 | static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage, |
| 95 | buffer_handle_t *handle, int *stride) |
| 96 | { |
| 97 | int32_t ret; |
| 98 | bool supported; |
| 99 | struct cros_gralloc_buffer_descriptor descriptor; |
| 100 | auto mod = (struct gralloc0_module *)dev->common.module; |
| 101 | |
| 102 | descriptor.width = w; |
| 103 | descriptor.height = h; |
| 104 | descriptor.droid_format = format; |
| 105 | descriptor.producer_usage = descriptor.consumer_usage = usage; |
| 106 | descriptor.drm_format = cros_gralloc_convert_format(format); |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 107 | descriptor.use_flags = gralloc0_convert_usage(usage); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 108 | |
| 109 | supported = mod->driver->is_supported(&descriptor); |
| 110 | if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 111 | descriptor.use_flags &= ~BO_USE_SCANOUT; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 112 | supported = mod->driver->is_supported(&descriptor); |
| 113 | } |
| 114 | |
| 115 | if (!supported) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 116 | drv_log("Unsupported combination -- HAL format: %u, HAL usage: %u, " |
| 117 | "drv_format: %4.4s, use_flags: %llu\n", |
| 118 | format, usage, reinterpret_cast<char *>(&descriptor.drm_format), |
| 119 | static_cast<unsigned long long>(descriptor.use_flags)); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 120 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | ret = mod->driver->allocate(&descriptor, handle); |
| 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | auto hnd = cros_gralloc_convert_handle(*handle); |
| 128 | *stride = hnd->pixel_stride; |
| 129 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 130 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle) |
| 134 | { |
| 135 | auto mod = (struct gralloc0_module *)dev->common.module; |
| 136 | return mod->driver->release(handle); |
| 137 | } |
| 138 | |
| 139 | static int gralloc0_close(struct hw_device_t *dev) |
| 140 | { |
| 141 | /* Memory is freed by managed pointers on process close. */ |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 142 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 145 | static int gralloc0_init(struct gralloc0_module *mod, bool initialize_alloc) |
| 146 | { |
| 147 | std::lock_guard<std::mutex> lock(mod->initialization_mutex); |
| 148 | |
| 149 | if (mod->initialized) |
| 150 | return 0; |
| 151 | |
| 152 | mod->driver = std::make_unique<cros_gralloc_driver>(); |
| 153 | if (mod->driver->init()) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 154 | drv_log("Failed to initialize driver.\n"); |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 155 | return -ENODEV; |
| 156 | } |
| 157 | |
| 158 | if (initialize_alloc) { |
| 159 | mod->alloc = std::make_unique<alloc_device_t>(); |
| 160 | mod->alloc->alloc = gralloc0_alloc; |
| 161 | mod->alloc->free = gralloc0_free; |
| 162 | mod->alloc->common.tag = HARDWARE_DEVICE_TAG; |
| 163 | mod->alloc->common.version = 0; |
| 164 | mod->alloc->common.module = (hw_module_t *)mod; |
| 165 | mod->alloc->common.close = gralloc0_close; |
| 166 | } |
| 167 | |
| 168 | mod->initialized = true; |
| 169 | return 0; |
| 170 | } |
| 171 | |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 172 | static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev) |
| 173 | { |
| 174 | auto module = (struct gralloc0_module *)mod; |
| 175 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 176 | if (module->initialized) { |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 177 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 178 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 182 | drv_log("Incorrect device name - %s.\n", name); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 183 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 186 | if (gralloc0_init(module, true)) |
| 187 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 188 | |
| 189 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 190 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 194 | { |
| 195 | auto mod = (struct gralloc0_module *)module; |
| 196 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 197 | if (!mod->initialized) |
| 198 | if (gralloc0_init(mod, false)) |
| 199 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 200 | |
| 201 | return mod->driver->retain(handle); |
| 202 | } |
| 203 | |
| 204 | static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 205 | { |
| 206 | auto mod = (struct gralloc0_module *)module; |
| 207 | return mod->driver->release(handle); |
| 208 | } |
| 209 | |
| 210 | static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage, |
| 211 | int l, int t, int w, int h, void **vaddr) |
| 212 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 213 | return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 217 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 218 | int32_t fence_fd, ret; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 219 | auto mod = (struct gralloc0_module *)module; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 220 | ret = mod->driver->unlock(handle, &fence_fd); |
| 221 | if (ret) |
| 222 | return ret; |
| 223 | |
| 224 | ret = cros_gralloc_sync_wait(fence_fd); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...) |
| 232 | { |
| 233 | va_list args; |
| 234 | int32_t *out_format, ret; |
| 235 | uint64_t *out_store; |
| 236 | buffer_handle_t handle; |
| 237 | uint32_t *out_width, *out_height, *out_stride; |
| 238 | auto mod = (struct gralloc0_module *)module; |
| 239 | |
| 240 | switch (op) { |
| 241 | case GRALLOC_DRM_GET_STRIDE: |
| 242 | case GRALLOC_DRM_GET_FORMAT: |
| 243 | case GRALLOC_DRM_GET_DIMENSIONS: |
| 244 | case GRALLOC_DRM_GET_BACKING_STORE: |
| 245 | break; |
| 246 | default: |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 247 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | va_start(args, op); |
| 251 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 252 | ret = 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 253 | handle = va_arg(args, buffer_handle_t); |
| 254 | auto hnd = cros_gralloc_convert_handle(handle); |
| 255 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 256 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 257 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | switch (op) { |
| 261 | case GRALLOC_DRM_GET_STRIDE: |
| 262 | out_stride = va_arg(args, uint32_t *); |
| 263 | *out_stride = hnd->pixel_stride; |
| 264 | break; |
| 265 | case GRALLOC_DRM_GET_FORMAT: |
| 266 | out_format = va_arg(args, int32_t *); |
| 267 | *out_format = hnd->droid_format; |
| 268 | break; |
| 269 | case GRALLOC_DRM_GET_DIMENSIONS: |
| 270 | out_width = va_arg(args, uint32_t *); |
| 271 | out_height = va_arg(args, uint32_t *); |
| 272 | *out_width = hnd->width; |
| 273 | *out_height = hnd->height; |
| 274 | break; |
| 275 | case GRALLOC_DRM_GET_BACKING_STORE: |
| 276 | out_store = va_arg(args, uint64_t *); |
| 277 | ret = mod->driver->get_backing_store(handle, out_store); |
| 278 | break; |
| 279 | default: |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 280 | ret = -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | va_end(args); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 289 | int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr) |
| 290 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 291 | return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1); |
| 292 | } |
| 293 | |
| 294 | static int gralloc0_lock_async(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 295 | int usage, int l, int t, int w, int h, void **vaddr, int fence_fd) |
| 296 | { |
| 297 | int32_t ret; |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 298 | uint32_t map_flags; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 299 | uint8_t *addr[DRV_MAX_PLANES]; |
| 300 | auto mod = (struct gralloc0_module *)module; |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 301 | struct rectangle rect = { .x = static_cast<uint32_t>(l), |
| 302 | .y = static_cast<uint32_t>(t), |
| 303 | .width = static_cast<uint32_t>(w), |
| 304 | .height = static_cast<uint32_t>(h) }; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 305 | |
| 306 | auto hnd = cros_gralloc_convert_handle(handle); |
| 307 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 308 | drv_log("Invalid handle.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 309 | return -EINVAL; |
| 310 | } |
| 311 | |
| 312 | if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 313 | drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 314 | return -EINVAL; |
| 315 | } |
| 316 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 317 | assert(l >= 0); |
| 318 | assert(t >= 0); |
| 319 | assert(w >= 0); |
| 320 | assert(h >= 0); |
| 321 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 322 | map_flags = gralloc0_convert_map_usage(usage); |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 323 | ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 324 | *vaddr = addr[0]; |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 329 | int *fence_fd) |
| 330 | { |
| 331 | auto mod = (struct gralloc0_module *)module; |
| 332 | return mod->driver->unlock(handle, fence_fd); |
| 333 | } |
| 334 | |
| 335 | static int gralloc0_lock_async_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 336 | int usage, int l, int t, int w, int h, |
| 337 | struct android_ycbcr *ycbcr, int fence_fd) |
| 338 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 339 | int32_t ret; |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 340 | uint32_t map_flags; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 341 | uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr }; |
| 342 | auto mod = (struct gralloc0_module *)module; |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 343 | struct rectangle rect = { .x = static_cast<uint32_t>(l), |
| 344 | .y = static_cast<uint32_t>(t), |
| 345 | .width = static_cast<uint32_t>(w), |
| 346 | .height = static_cast<uint32_t>(h) }; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 347 | |
| 348 | auto hnd = cros_gralloc_convert_handle(handle); |
| 349 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 350 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 351 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) && |
Tomasz Figa | aff29fd | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 355 | (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) && |
| 356 | (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame^] | 357 | drv_log("Non-YUV format not compatible.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 358 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 361 | assert(l >= 0); |
| 362 | assert(t >= 0); |
| 363 | assert(w >= 0); |
| 364 | assert(h >= 0); |
| 365 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 366 | map_flags = gralloc0_convert_map_usage(usage); |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 367 | ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr); |
Tomasz Figa | addd5f2 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 368 | if (ret) |
| 369 | return ret; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 370 | |
| 371 | switch (hnd->format) { |
| 372 | case DRM_FORMAT_NV12: |
| 373 | ycbcr->y = addr[0]; |
| 374 | ycbcr->cb = addr[1]; |
| 375 | ycbcr->cr = addr[1] + 1; |
| 376 | ycbcr->ystride = hnd->strides[0]; |
| 377 | ycbcr->cstride = hnd->strides[1]; |
| 378 | ycbcr->chroma_step = 2; |
| 379 | break; |
| 380 | case DRM_FORMAT_YVU420: |
| 381 | case DRM_FORMAT_YVU420_ANDROID: |
| 382 | ycbcr->y = addr[0]; |
| 383 | ycbcr->cb = addr[2]; |
| 384 | ycbcr->cr = addr[1]; |
| 385 | ycbcr->ystride = hnd->strides[0]; |
| 386 | ycbcr->cstride = hnd->strides[1]; |
| 387 | ycbcr->chroma_step = 1; |
| 388 | break; |
| 389 | default: |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 390 | module->unlock(module, handle); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 391 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 394 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Tomasz Figa | 4df286c | 2017-08-02 19:35:40 +0900 | [diff] [blame] | 397 | // clang-format off |
| 398 | static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open }; |
| 399 | // clang-format on |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 400 | |
| 401 | struct gralloc0_module HAL_MODULE_INFO_SYM = { |
| 402 | .base = |
| 403 | { |
| 404 | .common = |
| 405 | { |
| 406 | .tag = HARDWARE_MODULE_TAG, |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 407 | .module_api_version = GRALLOC_MODULE_API_VERSION_0_3, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 408 | .hal_api_version = 0, |
| 409 | .id = GRALLOC_HARDWARE_MODULE_ID, |
| 410 | .name = "CrOS Gralloc", |
| 411 | .author = "Chrome OS", |
| 412 | .methods = &gralloc0_module_methods, |
| 413 | }, |
| 414 | |
| 415 | .registerBuffer = gralloc0_register_buffer, |
| 416 | .unregisterBuffer = gralloc0_unregister_buffer, |
| 417 | .lock = gralloc0_lock, |
| 418 | .unlock = gralloc0_unlock, |
| 419 | .perform = gralloc0_perform, |
| 420 | .lock_ycbcr = gralloc0_lock_ycbcr, |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 421 | .lockAsync = gralloc0_lock_async, |
| 422 | .unlockAsync = gralloc0_unlock_async, |
| 423 | .lockAsync_ycbcr = gralloc0_lock_async_ycbcr, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 424 | }, |
| 425 | |
| 426 | .alloc = nullptr, |
| 427 | .driver = nullptr, |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 428 | .initialized = false, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 429 | }; |