Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include "../cros_gralloc_driver.h" |
| 8 | |
| 9 | #include <hardware/gralloc.h> |
| 10 | #include <memory.h> |
| 11 | |
| 12 | struct gralloc0_module { |
| 13 | gralloc_module_t base; |
| 14 | std::unique_ptr<alloc_device_t> alloc; |
| 15 | std::unique_ptr<cros_gralloc_driver> driver; |
| 16 | }; |
| 17 | |
| 18 | /* This enumeration must match the one in <gralloc_drm.h>. |
| 19 | * The functions supported by this gralloc's temporary private API are listed |
| 20 | * below. Use of these functions is highly discouraged and should only be |
| 21 | * reserved for cases where no alternative to get same information (such as |
| 22 | * querying ANativeWindow) exists. |
| 23 | */ |
| 24 | // clang-format off |
| 25 | enum { |
| 26 | GRALLOC_DRM_GET_STRIDE, |
| 27 | GRALLOC_DRM_GET_FORMAT, |
| 28 | GRALLOC_DRM_GET_DIMENSIONS, |
| 29 | GRALLOC_DRM_GET_BACKING_STORE, |
| 30 | }; |
| 31 | // clang-format on |
| 32 | |
| 33 | static int64_t gralloc0_convert_flags(int flags) |
| 34 | { |
| 35 | uint64_t usage = BO_USE_NONE; |
| 36 | |
| 37 | if (flags & GRALLOC_USAGE_CURSOR) |
| 38 | usage |= BO_USE_NONE; |
| 39 | if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_RARELY) |
| 40 | usage |= BO_USE_SW_READ_RARELY; |
| 41 | if ((flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN) |
| 42 | usage |= BO_USE_SW_READ_OFTEN; |
| 43 | if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_RARELY) |
| 44 | usage |= BO_USE_SW_WRITE_RARELY; |
| 45 | if ((flags & GRALLOC_USAGE_SW_WRITE_MASK) == GRALLOC_USAGE_SW_WRITE_OFTEN) |
| 46 | usage |= BO_USE_SW_WRITE_OFTEN; |
| 47 | if (flags & GRALLOC_USAGE_HW_TEXTURE) |
| 48 | usage |= BO_USE_TEXTURE; |
| 49 | if (flags & GRALLOC_USAGE_HW_RENDER) |
| 50 | usage |= BO_USE_RENDERING; |
| 51 | if (flags & GRALLOC_USAGE_HW_2D) |
| 52 | usage |= BO_USE_RENDERING; |
| 53 | if (flags & GRALLOC_USAGE_HW_COMPOSER) |
| 54 | /* HWC wants to use display hardware, but can defer to OpenGL. */ |
| 55 | usage |= BO_USE_SCANOUT | BO_USE_TEXTURE; |
| 56 | if (flags & GRALLOC_USAGE_HW_FB) |
| 57 | usage |= BO_USE_NONE; |
| 58 | if (flags & GRALLOC_USAGE_EXTERNAL_DISP) |
| 59 | /* |
| 60 | * This flag potentially covers external display for the normal drivers (i915, |
| 61 | * rockchip) and usb monitors (evdi/udl). It's complicated so ignore it. |
| 62 | * */ |
| 63 | usage |= BO_USE_NONE; |
| 64 | if (flags & GRALLOC_USAGE_PROTECTED) |
| 65 | usage |= BO_USE_PROTECTED; |
| 66 | if (flags & GRALLOC_USAGE_HW_VIDEO_ENCODER) |
| 67 | /*HACK: See b/30054495 */ |
| 68 | usage |= BO_USE_SW_READ_OFTEN; |
| 69 | if (flags & GRALLOC_USAGE_HW_CAMERA_WRITE) |
| 70 | usage |= BO_USE_HW_CAMERA_WRITE; |
| 71 | if (flags & GRALLOC_USAGE_HW_CAMERA_READ) |
| 72 | usage |= BO_USE_HW_CAMERA_READ; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 73 | if (flags & GRALLOC_USAGE_RENDERSCRIPT) |
| 74 | /* We use CPU for compute. */ |
| 75 | usage |= BO_USE_LINEAR; |
| 76 | |
| 77 | return usage; |
| 78 | } |
| 79 | |
| 80 | static int gralloc0_alloc(alloc_device_t *dev, int w, int h, int format, int usage, |
| 81 | buffer_handle_t *handle, int *stride) |
| 82 | { |
| 83 | int32_t ret; |
| 84 | bool supported; |
| 85 | struct cros_gralloc_buffer_descriptor descriptor; |
| 86 | auto mod = (struct gralloc0_module *)dev->common.module; |
| 87 | |
| 88 | descriptor.width = w; |
| 89 | descriptor.height = h; |
| 90 | descriptor.droid_format = format; |
| 91 | descriptor.producer_usage = descriptor.consumer_usage = usage; |
| 92 | descriptor.drm_format = cros_gralloc_convert_format(format); |
| 93 | descriptor.drv_usage = gralloc0_convert_flags(usage); |
| 94 | |
| 95 | supported = mod->driver->is_supported(&descriptor); |
| 96 | if (!supported && (usage & GRALLOC_USAGE_HW_COMPOSER)) { |
| 97 | descriptor.drv_usage &= ~BO_USE_SCANOUT; |
| 98 | supported = mod->driver->is_supported(&descriptor); |
| 99 | } |
| 100 | |
| 101 | if (!supported) { |
| 102 | cros_gralloc_error("Unsupported combination -- HAL format: %u, HAL flags: %u, " |
| 103 | "drv_format: %4.4s, drv_flags: %llu", |
Tomasz Figa | 23b85cb | 2017-07-11 18:07:11 +0900 | [diff] [blame^] | 104 | format, usage, reinterpret_cast<char *>(&descriptor.drm_format), |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 105 | static_cast<unsigned long long>(descriptor.drv_usage)); |
| 106 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 107 | } |
| 108 | |
| 109 | ret = mod->driver->allocate(&descriptor, handle); |
| 110 | if (ret) |
| 111 | return ret; |
| 112 | |
| 113 | auto hnd = cros_gralloc_convert_handle(*handle); |
| 114 | *stride = hnd->pixel_stride; |
| 115 | |
| 116 | return CROS_GRALLOC_ERROR_NONE; |
| 117 | } |
| 118 | |
| 119 | static int gralloc0_free(alloc_device_t *dev, buffer_handle_t handle) |
| 120 | { |
| 121 | auto mod = (struct gralloc0_module *)dev->common.module; |
| 122 | return mod->driver->release(handle); |
| 123 | } |
| 124 | |
| 125 | static int gralloc0_close(struct hw_device_t *dev) |
| 126 | { |
| 127 | /* Memory is freed by managed pointers on process close. */ |
| 128 | return CROS_GRALLOC_ERROR_NONE; |
| 129 | } |
| 130 | |
| 131 | static int gralloc0_open(const struct hw_module_t *mod, const char *name, struct hw_device_t **dev) |
| 132 | { |
| 133 | auto module = (struct gralloc0_module *)mod; |
| 134 | |
| 135 | if (module->alloc) { |
| 136 | *dev = &module->alloc->common; |
| 137 | return CROS_GRALLOC_ERROR_NONE; |
| 138 | } |
| 139 | |
| 140 | if (strcmp(name, GRALLOC_HARDWARE_GPU0)) { |
| 141 | cros_gralloc_error("Incorrect device name - %s.", name); |
| 142 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 143 | } |
| 144 | |
| 145 | module->driver = std::make_unique<cros_gralloc_driver>(); |
| 146 | if (module->driver->init()) { |
| 147 | cros_gralloc_error("Failed to initialize driver."); |
| 148 | return CROS_GRALLOC_ERROR_NO_RESOURCES; |
| 149 | } |
| 150 | |
| 151 | module->alloc = std::make_unique<alloc_device_t>(); |
| 152 | |
| 153 | module->alloc->alloc = gralloc0_alloc; |
| 154 | module->alloc->free = gralloc0_free; |
| 155 | module->alloc->common.tag = HARDWARE_DEVICE_TAG; |
| 156 | module->alloc->common.version = 0; |
| 157 | module->alloc->common.module = (hw_module_t *)mod; |
| 158 | module->alloc->common.close = gralloc0_close; |
| 159 | |
| 160 | *dev = &module->alloc->common; |
| 161 | return CROS_GRALLOC_ERROR_NONE; |
| 162 | } |
| 163 | |
| 164 | static int gralloc0_register_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 165 | { |
| 166 | auto mod = (struct gralloc0_module *)module; |
| 167 | |
| 168 | if (!mod->driver) { |
| 169 | mod->driver = std::make_unique<cros_gralloc_driver>(); |
| 170 | if (mod->driver->init()) { |
| 171 | cros_gralloc_error("Failed to initialize driver."); |
| 172 | return CROS_GRALLOC_ERROR_NO_RESOURCES; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return mod->driver->retain(handle); |
| 177 | } |
| 178 | |
| 179 | static int gralloc0_unregister_buffer(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 180 | { |
| 181 | auto mod = (struct gralloc0_module *)module; |
| 182 | return mod->driver->release(handle); |
| 183 | } |
| 184 | |
| 185 | static int gralloc0_lock(struct gralloc_module_t const *module, buffer_handle_t handle, int usage, |
| 186 | int l, int t, int w, int h, void **vaddr) |
| 187 | { |
| 188 | int32_t ret, fence; |
| 189 | uint64_t flags; |
| 190 | uint8_t *addr[DRV_MAX_PLANES]; |
| 191 | auto mod = (struct gralloc0_module *)module; |
| 192 | |
| 193 | auto hnd = cros_gralloc_convert_handle(handle); |
| 194 | if (!hnd) { |
| 195 | cros_gralloc_error("Invalid handle."); |
| 196 | return CROS_GRALLOC_ERROR_BAD_HANDLE; |
| 197 | } |
| 198 | |
| 199 | if ((hnd->droid_format == HAL_PIXEL_FORMAT_YCbCr_420_888)) { |
| 200 | cros_gralloc_error("HAL_PIXEL_FORMAT_YCbCr_*_888 format not compatible."); |
| 201 | return CROS_GRALLOC_ERROR_BAD_HANDLE; |
| 202 | } |
| 203 | |
| 204 | fence = -1; |
| 205 | flags = gralloc0_convert_flags(usage); |
| 206 | ret = mod->driver->lock(handle, fence, flags, addr); |
| 207 | *vaddr = addr[0]; |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int gralloc0_unlock(struct gralloc_module_t const *module, buffer_handle_t handle) |
| 212 | { |
| 213 | auto mod = (struct gralloc0_module *)module; |
| 214 | return mod->driver->unlock(handle); |
| 215 | } |
| 216 | |
| 217 | static int gralloc0_perform(struct gralloc_module_t const *module, int op, ...) |
| 218 | { |
| 219 | va_list args; |
| 220 | int32_t *out_format, ret; |
| 221 | uint64_t *out_store; |
| 222 | buffer_handle_t handle; |
| 223 | uint32_t *out_width, *out_height, *out_stride; |
| 224 | auto mod = (struct gralloc0_module *)module; |
| 225 | |
| 226 | switch (op) { |
| 227 | case GRALLOC_DRM_GET_STRIDE: |
| 228 | case GRALLOC_DRM_GET_FORMAT: |
| 229 | case GRALLOC_DRM_GET_DIMENSIONS: |
| 230 | case GRALLOC_DRM_GET_BACKING_STORE: |
| 231 | break; |
| 232 | default: |
| 233 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 234 | } |
| 235 | |
| 236 | va_start(args, op); |
| 237 | |
| 238 | ret = CROS_GRALLOC_ERROR_NONE; |
| 239 | handle = va_arg(args, buffer_handle_t); |
| 240 | auto hnd = cros_gralloc_convert_handle(handle); |
| 241 | if (!hnd) { |
| 242 | cros_gralloc_error("Invalid handle."); |
| 243 | return CROS_GRALLOC_ERROR_BAD_HANDLE; |
| 244 | } |
| 245 | |
| 246 | switch (op) { |
| 247 | case GRALLOC_DRM_GET_STRIDE: |
| 248 | out_stride = va_arg(args, uint32_t *); |
| 249 | *out_stride = hnd->pixel_stride; |
| 250 | break; |
| 251 | case GRALLOC_DRM_GET_FORMAT: |
| 252 | out_format = va_arg(args, int32_t *); |
| 253 | *out_format = hnd->droid_format; |
| 254 | break; |
| 255 | case GRALLOC_DRM_GET_DIMENSIONS: |
| 256 | out_width = va_arg(args, uint32_t *); |
| 257 | out_height = va_arg(args, uint32_t *); |
| 258 | *out_width = hnd->width; |
| 259 | *out_height = hnd->height; |
| 260 | break; |
| 261 | case GRALLOC_DRM_GET_BACKING_STORE: |
| 262 | out_store = va_arg(args, uint64_t *); |
| 263 | ret = mod->driver->get_backing_store(handle, out_store); |
| 264 | break; |
| 265 | default: |
| 266 | ret = CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 267 | } |
| 268 | |
| 269 | va_end(args); |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static int gralloc0_lock_ycbcr(struct gralloc_module_t const *module, buffer_handle_t handle, |
| 275 | int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr) |
| 276 | { |
| 277 | uint64_t flags; |
| 278 | int32_t fence, ret; |
| 279 | uint8_t *addr[DRV_MAX_PLANES] = { nullptr, nullptr, nullptr, nullptr }; |
| 280 | auto mod = (struct gralloc0_module *)module; |
| 281 | |
| 282 | auto hnd = cros_gralloc_convert_handle(handle); |
| 283 | if (!hnd) { |
| 284 | cros_gralloc_error("Invalid handle."); |
| 285 | return CROS_GRALLOC_ERROR_BAD_HANDLE; |
| 286 | } |
| 287 | |
| 288 | if ((hnd->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) && |
Tomasz Figa | aff29fd | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 289 | (hnd->droid_format != HAL_PIXEL_FORMAT_YV12) && |
| 290 | (hnd->droid_format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED)) { |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 291 | cros_gralloc_error("Non-YUV format not compatible."); |
| 292 | return CROS_GRALLOC_ERROR_BAD_HANDLE; |
| 293 | } |
| 294 | |
| 295 | fence = -1; |
| 296 | flags = gralloc0_convert_flags(usage); |
| 297 | ret = mod->driver->lock(handle, fence, flags, addr); |
Tomasz Figa | addd5f2 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 298 | if (ret) |
| 299 | return ret; |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 300 | |
| 301 | switch (hnd->format) { |
| 302 | case DRM_FORMAT_NV12: |
| 303 | ycbcr->y = addr[0]; |
| 304 | ycbcr->cb = addr[1]; |
| 305 | ycbcr->cr = addr[1] + 1; |
| 306 | ycbcr->ystride = hnd->strides[0]; |
| 307 | ycbcr->cstride = hnd->strides[1]; |
| 308 | ycbcr->chroma_step = 2; |
| 309 | break; |
| 310 | case DRM_FORMAT_YVU420: |
| 311 | case DRM_FORMAT_YVU420_ANDROID: |
| 312 | ycbcr->y = addr[0]; |
| 313 | ycbcr->cb = addr[2]; |
| 314 | ycbcr->cr = addr[1]; |
| 315 | ycbcr->ystride = hnd->strides[0]; |
| 316 | ycbcr->cstride = hnd->strides[1]; |
| 317 | ycbcr->chroma_step = 1; |
| 318 | break; |
| 319 | default: |
Tomasz Figa | addd5f2 | 2017-07-05 17:50:18 +0900 | [diff] [blame] | 320 | mod->driver->unlock(handle); |
Gurchetan Singh | d6b8b03 | 2017-05-31 14:31:31 -0700 | [diff] [blame] | 321 | return CROS_GRALLOC_ERROR_UNSUPPORTED; |
| 322 | } |
| 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | static struct hw_module_methods_t gralloc0_module_methods = {.open = gralloc0_open }; |
| 328 | |
| 329 | struct gralloc0_module HAL_MODULE_INFO_SYM = { |
| 330 | .base = |
| 331 | { |
| 332 | .common = |
| 333 | { |
| 334 | .tag = HARDWARE_MODULE_TAG, |
| 335 | .module_api_version = GRALLOC_MODULE_API_VERSION_0_2, |
| 336 | .hal_api_version = 0, |
| 337 | .id = GRALLOC_HARDWARE_MODULE_ID, |
| 338 | .name = "CrOS Gralloc", |
| 339 | .author = "Chrome OS", |
| 340 | .methods = &gralloc0_module_methods, |
| 341 | }, |
| 342 | |
| 343 | .registerBuffer = gralloc0_register_buffer, |
| 344 | .unregisterBuffer = gralloc0_unregister_buffer, |
| 345 | .lock = gralloc0_lock, |
| 346 | .unlock = gralloc0_unlock, |
| 347 | .perform = gralloc0_perform, |
| 348 | .lock_ycbcr = gralloc0_lock_ycbcr, |
| 349 | }, |
| 350 | |
| 351 | .alloc = nullptr, |
| 352 | .driver = nullptr, |
| 353 | }; |