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 | |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 7 | #include "../../util.h" |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 8 | #include "../cros_gralloc_driver.h" |
| 9 | |
Tomasz Mikolajewski | ca2938a | 2017-11-17 20:30:56 +0900 | [diff] [blame] | 10 | #include <cassert> |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 11 | #include <hardware/gralloc.h> |
| 12 | #include <memory.h> |
| 13 | |
| 14 | struct gralloc0_module { |
| 15 | gralloc_module_t base; |
| 16 | std::unique_ptr<alloc_device_t> alloc; |
| 17 | std::unique_ptr<cros_gralloc_driver> driver; |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 18 | bool initialized; |
| 19 | std::mutex initialization_mutex; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 20 | }; |
| 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 |
| 29 | enum { |
| 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 Stevens | 6116b31 | 2019-09-03 10:49:50 +0900 | [diff] [blame] | 37 | // 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 Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 42 | static uint64_t gralloc0_convert_usage(int usage) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 43 | { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 44 | uint64_t use_flags = BO_USE_NONE; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 45 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 46 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 63 | /* HWC wants to use display hardware, but can defer to OpenGL. */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 64 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 68 | /* |
| 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 Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 72 | use_flags |= BO_USE_NONE; |
| 73 | if (usage & GRALLOC_USAGE_PROTECTED) |
| 74 | use_flags |= BO_USE_PROTECTED; |
Hirokazu Honda | 5e55f95 | 2019-11-08 12:57:55 +0900 | [diff] [blame] | 75 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { |
| 76 | use_flags |= BO_USE_HW_VIDEO_ENCODER; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 77 | /*HACK: See b/30054495 */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 78 | use_flags |= BO_USE_SW_READ_OFTEN; |
Hirokazu Honda | 5e55f95 | 2019-11-08 12:57:55 +0900 | [diff] [blame] | 79 | } |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 80 | 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 Stevens | 6116b31 | 2019-09-03 10:49:50 +0900 | [diff] [blame] | 86 | if (usage & BUFFER_USAGE_VIDEO_DECODER) |
| 87 | use_flags |= BO_USE_HW_VIDEO_DECODER; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 88 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 89 | return use_flags; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 92 | static 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 Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 104 | static 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 111 | static 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 Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 117 | auto mod = (struct gralloc0_module const *)dev->common.module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 118 | |
| 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 Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 124 | descriptor.use_flags = gralloc0_convert_usage(usage); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 125 | |
| 126 | supported = mod->driver->is_supported(&descriptor); |
| 127 | if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 128 | descriptor.use_flags &= ~BO_USE_SCANOUT; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 129 | supported = mod->driver->is_supported(&descriptor); |
| 130 | } |
Hirokazu Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 131 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 139 | |
| 140 | if (!supported) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 141 | 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 145 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 146 | } |
| 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 155 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle) |
| 159 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 160 | auto mod = (struct gralloc0_module const *)dev->common.module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 161 | return mod->driver->release(handle); |
| 162 | } |
| 163 | |
| 164 | static int gralloc0_close(struct hw_device_t *dev) |
| 165 | { |
| 166 | /* Memory is freed by managed pointers on process close. */ |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 167 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 170 | static 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 Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 179 | drv_log("Failed to initialize driver.\n"); |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 180 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 197 | static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev) |
| 198 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 199 | auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod); |
| 200 | auto module = const_cast<struct gralloc0_module *>(const_module); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 201 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 202 | if (module->initialized) { |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 203 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 204 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 208 | drv_log("Incorrect device name - %s.\n", name); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 209 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 212 | if (gralloc0_init(module, true)) |
| 213 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 214 | |
| 215 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 216 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 220 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 221 | auto const_module = reinterpret_cast<const struct gralloc0_module *>(module); |
| 222 | auto mod = const_cast<struct gralloc0_module *>(const_module); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 223 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 224 | if (!mod->initialized) |
| 225 | if (gralloc0_init(mod, false)) |
| 226 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 227 | |
| 228 | return mod->driver->retain(handle); |
| 229 | } |
| 230 | |
| 231 | static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 232 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 233 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 234 | return mod->driver->release(handle); |
| 235 | } |
| 236 | |
| 237 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 240 | return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 244 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 245 | int32_t fence_fd, ret; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 246 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 247 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static 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 Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 265 | uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
| 266 | uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 267 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 268 | |
| 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 276 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | va_start(args, op); |
| 280 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 281 | ret = 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 282 | handle = va_arg(args, buffer_handle_t); |
| 283 | auto hnd = cros_gralloc_convert_handle(handle); |
| 284 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 285 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 286 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | switch (op) { |
| 290 | case GRALLOC_DRM_GET_STRIDE: |
| 291 | out_stride = va_arg(args, uint32_t *); |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 292 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 303 | 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 319 | ret = -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | va_end(args); |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 330 | return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1); |
| 331 | } |
| 332 | |
| 333 | static 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 Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 337 | uint32_t map_flags; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 338 | uint8_t *addr[DRV_MAX_PLANES]; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 339 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 340 | 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 344 | |
| 345 | auto hnd = cros_gralloc_convert_handle(handle); |
| 346 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 347 | drv_log("Invalid handle.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 348 | return -EINVAL; |
| 349 | } |
| 350 | |
| 351 | if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 352 | drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 353 | return -EINVAL; |
| 354 | } |
| 355 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 356 | assert(l >= 0); |
| 357 | assert(t >= 0); |
| 358 | assert(w >= 0); |
| 359 | assert(h >= 0); |
| 360 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 361 | map_flags = gralloc0_convert_map_usage(usage); |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 362 | ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 363 | *vaddr = addr[0]; |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 368 | int *fence_fd) |
| 369 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 370 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 371 | return mod->driver->unlock(handle, fence_fd); |
| 372 | } |
| 373 | |
| 374 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 378 | int32_t ret; |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 379 | uint32_t map_flags; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 380 | uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
| 381 | uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 382 | uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr }; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 383 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 384 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 388 | |
| 389 | auto hnd = cros_gralloc_convert_handle(handle); |
| 390 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 391 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 392 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Hirokazu Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 395 | if (!gralloc0_droid_yuv_format(hnd->droid_format) && |
| 396 | hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 397 | drv_log("Non-YUV format not compatible.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 398 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 401 | assert(l >= 0); |
| 402 | assert(t >= 0); |
| 403 | assert(w >= 0); |
| 404 | assert(h >= 0); |
| 405 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 406 | map_flags = gralloc0_convert_map_usage(usage); |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 407 | ret = mod->driver->lock(handle, fence_fd, &rect, map_flags, addr); |
Tomasz Figa | addd5f2 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 408 | if (ret) |
| 409 | return ret; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 410 | |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 411 | 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++) |
Jason Macnak | a03926e | 2020-05-14 10:57:17 -0700 | [diff] [blame^] | 417 | addr[plane] = |
| 418 | reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane])); |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 421 | switch (hnd->format) { |
| 422 | case DRM_FORMAT_NV12: |
| 423 | ycbcr->y = addr[0]; |
| 424 | ycbcr->cb = addr[1]; |
| 425 | ycbcr->cr = addr[1] + 1; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 426 | ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0]; |
| 427 | ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1]; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 428 | ycbcr->chroma_step = 2; |
| 429 | break; |
| 430 | case DRM_FORMAT_YVU420: |
| 431 | case DRM_FORMAT_YVU420_ANDROID: |
| 432 | ycbcr->y = addr[0]; |
| 433 | ycbcr->cb = addr[2]; |
| 434 | ycbcr->cr = addr[1]; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 435 | ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0]; |
| 436 | ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1]; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 437 | ycbcr->chroma_step = 1; |
| 438 | break; |
| 439 | default: |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 440 | module->unlock(module, handle); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 441 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 444 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Tomasz Figa | 4df286c | 2017-08-02 19:35:40 +0900 | [diff] [blame] | 447 | // clang-format off |
| 448 | static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open }; |
| 449 | // clang-format on |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 450 | |
| 451 | struct gralloc0_module HAL_MODULE_INFO_SYM = { |
| 452 | .base = |
| 453 | { |
| 454 | .common = |
| 455 | { |
| 456 | .tag = HARDWARE_MODULE_TAG, |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 457 | .module_api_version = GRALLOC_MODULE_API_VERSION_0_3, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 458 | .hal_api_version = 0, |
| 459 | .id = GRALLOC_HARDWARE_MODULE_ID, |
| 460 | .name = "CrOS Gralloc", |
| 461 | .author = "Chrome OS", |
| 462 | .methods = &gralloc0_module_methods, |
| 463 | }, |
| 464 | |
| 465 | .registerBuffer = gralloc0_register_buffer, |
| 466 | .unregisterBuffer = gralloc0_unregister_buffer, |
| 467 | .lock = gralloc0_lock, |
| 468 | .unlock = gralloc0_unlock, |
| 469 | .perform = gralloc0_perform, |
| 470 | .lock_ycbcr = gralloc0_lock_ycbcr, |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 471 | .lockAsync = gralloc0_lock_async, |
| 472 | .unlockAsync = gralloc0_unlock_async, |
| 473 | .lockAsync_ycbcr = gralloc0_lock_async_ycbcr, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 474 | }, |
| 475 | |
| 476 | .alloc = nullptr, |
| 477 | .driver = nullptr, |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 478 | .initialized = false, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 479 | }; |