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