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