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, |
Kristian H. Kristensen | e77c32c | 2020-07-23 16:04:47 -0700 | [diff] [blame] | 34 | GRALLOC_DRM_GET_MODIFIER, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 35 | }; |
| 36 | // clang-format on |
| 37 | |
David Stevens | 6116b31 | 2019-09-03 10:49:50 +0900 | [diff] [blame] | 38 | // 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 Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 43 | static uint64_t gralloc0_convert_usage(int usage) |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 44 | { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 45 | uint64_t use_flags = BO_USE_NONE; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 46 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 47 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 64 | /* HWC wants to use display hardware, but can defer to OpenGL. */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 65 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 69 | /* |
| 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 Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 73 | use_flags |= BO_USE_NONE; |
| 74 | if (usage & GRALLOC_USAGE_PROTECTED) |
| 75 | use_flags |= BO_USE_PROTECTED; |
Hirokazu Honda | 5e55f95 | 2019-11-08 12:57:55 +0900 | [diff] [blame] | 76 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { |
| 77 | use_flags |= BO_USE_HW_VIDEO_ENCODER; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 78 | /*HACK: See b/30054495 */ |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 79 | use_flags |= BO_USE_SW_READ_OFTEN; |
Hirokazu Honda | 5e55f95 | 2019-11-08 12:57:55 +0900 | [diff] [blame] | 80 | } |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 81 | 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 Stevens | 6116b31 | 2019-09-03 10:49:50 +0900 | [diff] [blame] | 87 | if (usage & BUFFER_USAGE_VIDEO_DECODER) |
| 88 | use_flags |= BO_USE_HW_VIDEO_DECODER; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 89 | |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 90 | return use_flags; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 93 | static 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 Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 105 | static 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 112 | static 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 Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 118 | auto mod = (struct gralloc0_module const *)dev->common.module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 119 | |
| 120 | descriptor.width = w; |
| 121 | descriptor.height = h; |
| 122 | descriptor.droid_format = format; |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 123 | descriptor.droid_usage = usage; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 124 | descriptor.drm_format = cros_gralloc_convert_format(format); |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 125 | descriptor.use_flags = gralloc0_convert_usage(usage); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 126 | descriptor.reserved_region_size = 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 127 | |
| 128 | supported = mod->driver->is_supported(&descriptor); |
| 129 | if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) { |
Gurchetan Singh | a1892b2 | 2017-09-28 16:40:52 -0700 | [diff] [blame] | 130 | descriptor.use_flags &= ~BO_USE_SCANOUT; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 131 | supported = mod->driver->is_supported(&descriptor); |
| 132 | } |
Hirokazu Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 133 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 141 | |
| 142 | if (!supported) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 143 | 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 147 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 148 | } |
| 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 Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 157 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle) |
| 161 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 162 | auto mod = (struct gralloc0_module const *)dev->common.module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 163 | return mod->driver->release(handle); |
| 164 | } |
| 165 | |
| 166 | static int gralloc0_close(struct hw_device_t *dev) |
| 167 | { |
| 168 | /* Memory is freed by managed pointers on process close. */ |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 169 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 172 | static 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 Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 181 | drv_log("Failed to initialize driver.\n"); |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 182 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 199 | static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev) |
| 200 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 201 | auto const_module = reinterpret_cast<const struct gralloc0_module *>(mod); |
| 202 | auto module = const_cast<struct gralloc0_module *>(const_module); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 203 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 204 | if (module->initialized) { |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 205 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 206 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | if (strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 210 | drv_log("Incorrect device name - %s.\n", name); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 211 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 214 | if (gralloc0_init(module, true)) |
| 215 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 216 | |
| 217 | *dev = &module->alloc->common; |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 218 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 222 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 223 | auto const_module = reinterpret_cast<const struct gralloc0_module *>(module); |
| 224 | auto mod = const_cast<struct gralloc0_module *>(const_module); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 225 | |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 226 | if (!mod->initialized) |
| 227 | if (gralloc0_init(mod, false)) |
| 228 | return -ENODEV; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 229 | |
| 230 | return mod->driver->retain(handle); |
| 231 | } |
| 232 | |
| 233 | static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 234 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 235 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 236 | return mod->driver->release(handle); |
| 237 | } |
| 238 | |
| 239 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 242 | return module->lockAsync(module, handle, usage, l, t, w, h, vaddr, -1); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 246 | { |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 247 | int32_t fence_fd, ret; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 248 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 249 | ret = mod->driver->unlock(handle, &fence_fd); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 253 | ret = cros_gralloc_sync_wait(fence_fd, /*close_acquire_fence=*/true); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 254 | if (ret) |
| 255 | return ret; |
| 256 | |
| 257 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static 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. Kristensen | e77c32c | 2020-07-23 16:04:47 -0700 | [diff] [blame] | 265 | uint64_t *out_modifier; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 266 | buffer_handle_t handle; |
| 267 | uint32_t *out_width, *out_height, *out_stride; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 268 | uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
| 269 | uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 270 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 271 | |
| 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. Kristensen | e77c32c | 2020-07-23 16:04:47 -0700 | [diff] [blame] | 277 | case GRALLOC_DRM_GET_MODIFIER: |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 278 | break; |
| 279 | default: |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 280 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | va_start(args, op); |
| 284 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 285 | ret = 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 286 | handle = va_arg(args, buffer_handle_t); |
| 287 | auto hnd = cros_gralloc_convert_handle(handle); |
| 288 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 289 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 290 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | switch (op) { |
| 294 | case GRALLOC_DRM_GET_STRIDE: |
| 295 | out_stride = va_arg(args, uint32_t *); |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 296 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 307 | 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. Kristensen | e77c32c | 2020-07-23 16:04:47 -0700 | [diff] [blame] | 322 | case GRALLOC_DRM_GET_MODIFIER: |
| 323 | out_modifier = va_arg(args, uint64_t *); |
| 324 | *out_modifier = hnd->format_modifier; |
| 325 | break; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 326 | default: |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 327 | ret = -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | va_end(args); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 338 | return module->lockAsync_ycbcr(module, handle, usage, l, t, w, h, ycbcr, -1); |
| 339 | } |
| 340 | |
| 341 | static 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 Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 345 | uint32_t map_flags; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 346 | uint8_t *addr[DRV_MAX_PLANES]; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 347 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 348 | 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 352 | |
| 353 | auto hnd = cros_gralloc_convert_handle(handle); |
| 354 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 355 | drv_log("Invalid handle.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 356 | return -EINVAL; |
| 357 | } |
| 358 | |
| 359 | if (hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 360 | drv_log("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible.\n"); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 361 | return -EINVAL; |
| 362 | } |
| 363 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 364 | assert(l >= 0); |
| 365 | assert(t >= 0); |
| 366 | assert(w >= 0); |
| 367 | assert(h >= 0); |
| 368 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 369 | map_flags = gralloc0_convert_map_usage(usage); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 370 | ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr); |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 371 | *vaddr = addr[0]; |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static int gralloc0_unlock_async(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 376 | int *fence_fd) |
| 377 | { |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 378 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 379 | return mod->driver->unlock(handle, fence_fd); |
| 380 | } |
| 381 | |
| 382 | static 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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 386 | int32_t ret; |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 387 | uint32_t map_flags; |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 388 | uint32_t strides[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
| 389 | uint32_t offsets[DRV_MAX_PLANES] = { 0, 0, 0, 0 }; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 390 | uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr }; |
Alistair Strachan | f8ff0f5 | 2018-04-09 18:49:51 -0700 | [diff] [blame] | 391 | auto mod = (struct gralloc0_module const *)module; |
Gurchetan Singh | 2b1d689 | 2018-09-17 16:58:16 -0700 | [diff] [blame] | 392 | 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 Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 396 | |
| 397 | auto hnd = cros_gralloc_convert_handle(handle); |
| 398 | if (!hnd) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 399 | drv_log("Invalid handle.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 400 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Hirokazu Honda | 758cf12 | 2019-12-03 11:01:59 +0900 | [diff] [blame] | 403 | if (!gralloc0_droid_yuv_format(hnd->droid_format) && |
| 404 | hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
Alistair Strachan | 0cfaaa5 | 2018-03-19 14:03:23 -0700 | [diff] [blame] | 405 | drv_log("Non-YUV format not compatible.\n"); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 406 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Gurchetan Singh | 1ef809e | 2017-11-06 11:07:52 -0800 | [diff] [blame] | 409 | assert(l >= 0); |
| 410 | assert(t >= 0); |
| 411 | assert(w >= 0); |
| 412 | assert(h >= 0); |
| 413 | |
Gurchetan Singh | f7f633a | 2017-09-28 17:02:12 -0700 | [diff] [blame] | 414 | map_flags = gralloc0_convert_map_usage(usage); |
Jason Macnak | 1de7f66 | 2020-01-24 15:05:57 -0800 | [diff] [blame] | 415 | ret = mod->driver->lock(handle, fence_fd, true, &rect, map_flags, addr); |
Tomasz Figa | addd5f2 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 416 | if (ret) |
| 417 | return ret; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 418 | |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 419 | 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 Macnak | a03926e | 2020-05-14 10:57:17 -0700 | [diff] [blame] | 425 | addr[plane] = |
| 426 | reinterpret_cast<uint8_t *>(static_cast<uintptr_t>(offsets[plane])); |
Gurchetan Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 429 | 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 Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 434 | ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0]; |
| 435 | ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1]; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 436 | 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 Singh | bc4f023 | 2019-06-27 20:05:54 -0700 | [diff] [blame] | 443 | ycbcr->ystride = (!map_flags) ? strides[0] : hnd->strides[0]; |
| 444 | ycbcr->cstride = (!map_flags) ? strides[1] : hnd->strides[1]; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 445 | ycbcr->chroma_step = 1; |
| 446 | break; |
| 447 | default: |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 448 | module->unlock(module, handle); |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 449 | return -EINVAL; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Tomasz Figa | 90bb743 | 2017-07-21 17:54:05 +0900 | [diff] [blame] | 452 | return 0; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Tomasz Figa | 4df286c | 2017-08-02 19:35:40 +0900 | [diff] [blame] | 455 | // clang-format off |
| 456 | static struct hw_module_methods_t gralloc0_module_methods = { .open = gralloc0_open }; |
| 457 | // clang-format on |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 458 | |
| 459 | struct gralloc0_module HAL_MODULE_INFO_SYM = { |
| 460 | .base = |
| 461 | { |
| 462 | .common = |
| 463 | { |
| 464 | .tag = HARDWARE_MODULE_TAG, |
Gurchetan Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 465 | .module_api_version = GRALLOC_MODULE_API_VERSION_0_3, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 466 | .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 Singh | 4b5d0bf | 2017-06-22 18:38:37 -0700 | [diff] [blame] | 479 | .lockAsync = gralloc0_lock_async, |
| 480 | .unlockAsync = gralloc0_unlock_async, |
| 481 | .lockAsync_ycbcr = gralloc0_lock_async_ycbcr, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 482 | }, |
| 483 | |
| 484 | .alloc = nullptr, |
| 485 | .driver = nullptr, |
Gurchetan Singh | bcfd758 | 2017-08-01 15:02:24 -0700 | [diff] [blame] | 486 | .initialized = false, |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 487 | }; |