Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 1 | /* |
Daniele Castagna | 7a755de | 2016-12-16 17:32:30 -0500 | [diff] [blame] | 2 | * Copyright 2016 The Chromium OS Authors. All rights reserved. |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | #include <assert.h> |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 7 | #include <errno.h> |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 9 | #include <pthread.h> |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
Gurchetan Singh | ef92053 | 2016-08-12 16:38:25 -0700 | [diff] [blame] | 14 | #include <sys/mman.h> |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 15 | #include <xf86drm.h> |
| 16 | |
| 17 | #include "drv_priv.h" |
| 18 | #include "helpers.h" |
| 19 | #include "util.h" |
| 20 | |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 21 | #ifdef DRV_AMDGPU |
| 22 | extern struct backend backend_amdgpu; |
| 23 | #endif |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 24 | extern struct backend backend_cirrus; |
| 25 | extern struct backend backend_evdi; |
| 26 | #ifdef DRV_EXYNOS |
| 27 | extern struct backend backend_exynos; |
| 28 | #endif |
| 29 | extern struct backend backend_gma500; |
| 30 | #ifdef DRV_I915 |
| 31 | extern struct backend backend_i915; |
| 32 | #endif |
| 33 | #ifdef DRV_MARVELL |
| 34 | extern struct backend backend_marvell; |
| 35 | #endif |
| 36 | #ifdef DRV_MEDIATEK |
| 37 | extern struct backend backend_mediatek; |
| 38 | #endif |
Daniele Castagna | 71db2b5 | 2016-12-16 16:24:06 -0500 | [diff] [blame] | 39 | extern struct backend backend_nouveau; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 40 | #ifdef DRV_ROCKCHIP |
| 41 | extern struct backend backend_rockchip; |
| 42 | #endif |
| 43 | #ifdef DRV_TEGRA |
| 44 | extern struct backend backend_tegra; |
| 45 | #endif |
| 46 | extern struct backend backend_udl; |
Niklas Schulze | 878fed4 | 2017-02-08 15:29:21 +0100 | [diff] [blame] | 47 | #ifdef DRV_VC4 |
| 48 | extern struct backend backend_vc4; |
| 49 | #endif |
Gurchetan Singh | 5521bde | 2016-09-30 14:53:32 -0700 | [diff] [blame] | 50 | extern struct backend backend_vgem; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 51 | extern struct backend backend_virtio_gpu; |
| 52 | |
| 53 | static struct backend *drv_get_backend(int fd) |
| 54 | { |
| 55 | drmVersionPtr drm_version; |
| 56 | unsigned int i; |
| 57 | |
| 58 | drm_version = drmGetVersion(fd); |
| 59 | |
| 60 | if (!drm_version) |
| 61 | return NULL; |
| 62 | |
| 63 | struct backend *backend_list[] = { |
Akshu Agrawal | 0337d9b | 2016-07-28 15:35:45 +0530 | [diff] [blame] | 64 | #ifdef DRV_AMDGPU |
| 65 | &backend_amdgpu, |
| 66 | #endif |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 67 | &backend_cirrus, &backend_evdi, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 68 | #ifdef DRV_EXYNOS |
| 69 | &backend_exynos, |
| 70 | #endif |
| 71 | &backend_gma500, |
| 72 | #ifdef DRV_I915 |
| 73 | &backend_i915, |
| 74 | #endif |
| 75 | #ifdef DRV_MARVELL |
| 76 | &backend_marvell, |
| 77 | #endif |
| 78 | #ifdef DRV_MEDIATEK |
| 79 | &backend_mediatek, |
| 80 | #endif |
Daniele Castagna | 71db2b5 | 2016-12-16 16:24:06 -0500 | [diff] [blame] | 81 | &backend_nouveau, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 82 | #ifdef DRV_ROCKCHIP |
| 83 | &backend_rockchip, |
| 84 | #endif |
| 85 | #ifdef DRV_TEGRA |
| 86 | &backend_tegra, |
| 87 | #endif |
| 88 | &backend_udl, |
Niklas Schulze | 878fed4 | 2017-02-08 15:29:21 +0100 | [diff] [blame] | 89 | #ifdef DRV_VC4 |
| 90 | &backend_vc4, |
| 91 | #endif |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 92 | &backend_vgem, &backend_virtio_gpu, |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 95 | for (i = 0; i < ARRAY_SIZE(backend_list); i++) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 96 | if (!strcmp(drm_version->name, backend_list[i]->name)) { |
| 97 | drmFreeVersion(drm_version); |
| 98 | return backend_list[i]; |
| 99 | } |
| 100 | |
| 101 | drmFreeVersion(drm_version); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | struct driver *drv_create(int fd) |
| 106 | { |
| 107 | struct driver *drv; |
| 108 | int ret; |
| 109 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 110 | drv = (struct driver *)calloc(1, sizeof(*drv)); |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 111 | |
| 112 | if (!drv) |
| 113 | return NULL; |
| 114 | |
| 115 | drv->fd = fd; |
| 116 | drv->backend = drv_get_backend(fd); |
| 117 | |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 118 | if (!drv->backend) |
| 119 | goto free_driver; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 120 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 121 | if (pthread_mutex_init(&drv->driver_lock, NULL)) |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 122 | goto free_driver; |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 123 | |
| 124 | drv->buffer_table = drmHashCreate(); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 125 | if (!drv->buffer_table) |
| 126 | goto free_lock; |
| 127 | |
| 128 | drv->map_table = drmHashCreate(); |
| 129 | if (!drv->map_table) |
| 130 | goto free_buffer_table; |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 131 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 132 | /* Start with a power of 2 number of allocations. */ |
| 133 | drv->backend->combos.allocations = 2; |
| 134 | drv->backend->combos.size = 0; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 135 | drv->backend->combos.data = |
| 136 | calloc(drv->backend->combos.allocations, sizeof(struct combination)); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 137 | if (!drv->backend->combos.data) |
| 138 | goto free_map_table; |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 139 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 140 | if (drv->backend->init) { |
| 141 | ret = drv->backend->init(drv); |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 142 | if (ret) { |
| 143 | free(drv->backend->combos.data); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 144 | goto free_map_table; |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 145 | } |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return drv; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 149 | |
| 150 | free_map_table: |
| 151 | drmHashDestroy(drv->map_table); |
| 152 | free_buffer_table: |
| 153 | drmHashDestroy(drv->buffer_table); |
| 154 | free_lock: |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 155 | pthread_mutex_destroy(&drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 156 | free_driver: |
| 157 | free(drv); |
| 158 | return NULL; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void drv_destroy(struct driver *drv) |
| 162 | { |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 163 | pthread_mutex_lock(&drv->driver_lock); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 164 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 165 | if (drv->backend->close) |
| 166 | drv->backend->close(drv); |
| 167 | |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 168 | drmHashDestroy(drv->buffer_table); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 169 | drmHashDestroy(drv->map_table); |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 170 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 171 | free(drv->backend->combos.data); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 172 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 173 | pthread_mutex_unlock(&drv->driver_lock); |
| 174 | pthread_mutex_destroy(&drv->driver_lock); |
Gurchetan Singh | 179687e | 2016-10-28 10:07:35 -0700 | [diff] [blame] | 175 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 176 | free(drv); |
| 177 | } |
| 178 | |
| 179 | int drv_get_fd(struct driver *drv) |
| 180 | { |
| 181 | return drv->fd; |
| 182 | } |
| 183 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 184 | const char *drv_get_name(struct driver *drv) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 185 | { |
| 186 | return drv->backend->name; |
| 187 | } |
| 188 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 189 | struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t usage) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 190 | { |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 191 | struct combination *curr, *best; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 192 | |
Gurchetan Singh | 458976f | 2016-11-23 17:32:33 -0800 | [diff] [blame] | 193 | if (format == DRM_FORMAT_NONE || usage == BO_USE_NONE) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 194 | return 0; |
| 195 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 196 | best = NULL; |
| 197 | uint32_t i; |
| 198 | for (i = 0; i < drv->backend->combos.size; i++) { |
| 199 | curr = &drv->backend->combos.data[i]; |
| 200 | if ((format == curr->format) && usage == (curr->usage & usage)) |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 201 | if (!best || best->metadata.priority < curr->metadata.priority) |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 202 | best = curr; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Gurchetan Singh | 6b41fb5 | 2017-03-01 20:14:39 -0800 | [diff] [blame] | 205 | return best; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 208 | struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 209 | { |
| 210 | |
| 211 | struct bo *bo; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 212 | bo = (struct bo *)calloc(1, sizeof(*bo)); |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 213 | |
| 214 | if (!bo) |
| 215 | return NULL; |
| 216 | |
| 217 | bo->drv = drv; |
| 218 | bo->width = width; |
| 219 | bo->height = height; |
| 220 | bo->format = format; |
| 221 | bo->num_planes = drv_num_planes_from_format(format); |
| 222 | |
| 223 | if (!bo->num_planes) { |
| 224 | free(bo); |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | return bo; |
| 229 | } |
| 230 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 231 | struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format, |
| 232 | uint64_t flags) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 233 | { |
| 234 | int ret; |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 235 | size_t plane; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 236 | struct bo *bo; |
| 237 | |
| 238 | bo = drv_bo_new(drv, width, height, format); |
| 239 | |
| 240 | if (!bo) |
| 241 | return NULL; |
| 242 | |
| 243 | ret = drv->backend->bo_create(bo, width, height, format, flags); |
| 244 | |
| 245 | if (ret) { |
| 246 | free(bo); |
| 247 | return NULL; |
| 248 | } |
| 249 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 250 | pthread_mutex_lock(&drv->driver_lock); |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 251 | |
| 252 | for (plane = 0; plane < bo->num_planes; plane++) |
| 253 | drv_increment_reference_count(drv, bo, plane); |
| 254 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 255 | pthread_mutex_unlock(&drv->driver_lock); |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 256 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 257 | return bo; |
| 258 | } |
| 259 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 260 | struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height, |
| 261 | uint32_t format, const uint64_t *modifiers, uint32_t count) |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 262 | { |
| 263 | int ret; |
| 264 | size_t plane; |
| 265 | struct bo *bo; |
| 266 | |
| 267 | if (!drv->backend->bo_create_with_modifiers) { |
| 268 | errno = ENOENT; |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | bo = drv_bo_new(drv, width, height, format); |
| 273 | |
| 274 | if (!bo) |
| 275 | return NULL; |
| 276 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 277 | ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count); |
Kristian H. Kristensen | b1efbd8 | 2016-09-06 11:43:26 -0700 | [diff] [blame] | 278 | |
| 279 | if (ret) { |
| 280 | free(bo); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | pthread_mutex_lock(&drv->driver_lock); |
| 285 | |
| 286 | for (plane = 0; plane < bo->num_planes; plane++) |
| 287 | drv_increment_reference_count(drv, bo, plane); |
| 288 | |
| 289 | pthread_mutex_unlock(&drv->driver_lock); |
| 290 | |
| 291 | return bo; |
| 292 | } |
| 293 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 294 | void drv_bo_destroy(struct bo *bo) |
| 295 | { |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 296 | size_t plane; |
| 297 | uintptr_t total = 0; |
| 298 | struct driver *drv = bo->drv; |
| 299 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 300 | pthread_mutex_lock(&drv->driver_lock); |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 301 | |
| 302 | for (plane = 0; plane < bo->num_planes; plane++) |
| 303 | drv_decrement_reference_count(drv, bo, plane); |
| 304 | |
| 305 | for (plane = 0; plane < bo->num_planes; plane++) |
| 306 | total += drv_get_reference_count(drv, bo, plane); |
| 307 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 308 | pthread_mutex_unlock(&drv->driver_lock); |
Gurchetan Singh | 1647fbe | 2016-08-03 17:14:55 -0700 | [diff] [blame] | 309 | |
| 310 | if (total == 0) |
| 311 | bo->drv->backend->bo_destroy(bo); |
| 312 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 313 | free(bo); |
| 314 | } |
| 315 | |
| 316 | struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data) |
| 317 | { |
| 318 | int ret; |
Gurchetan Singh | b72badb | 2016-08-19 16:26:46 -0700 | [diff] [blame] | 319 | size_t plane; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 320 | struct bo *bo; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 321 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 322 | bo = drv_bo_new(drv, data->width, data->height, data->format); |
| 323 | |
Gurchetan Singh | b72badb | 2016-08-19 16:26:46 -0700 | [diff] [blame] | 324 | if (!bo) |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 325 | return NULL; |
Gurchetan Singh | b72badb | 2016-08-19 16:26:46 -0700 | [diff] [blame] | 326 | |
Gurchetan Singh | 71611d6 | 2017-01-03 16:49:56 -0800 | [diff] [blame] | 327 | ret = drv->backend->bo_import(bo, data); |
| 328 | if (ret) { |
| 329 | free(bo); |
| 330 | return NULL; |
| 331 | } |
| 332 | |
Gurchetan Singh | b72badb | 2016-08-19 16:26:46 -0700 | [diff] [blame] | 333 | for (plane = 0; plane < bo->num_planes; plane++) { |
Gurchetan Singh | b72badb | 2016-08-19 16:26:46 -0700 | [diff] [blame] | 334 | bo->strides[plane] = data->strides[plane]; |
| 335 | bo->offsets[plane] = data->offsets[plane]; |
| 336 | bo->sizes[plane] = data->sizes[plane]; |
Kristian H. Kristensen | 3345977 | 2016-09-16 11:14:16 -0700 | [diff] [blame] | 337 | bo->format_modifiers[plane] = data->format_modifiers[plane]; |
Gurchetan Singh | 2e786ad | 2016-08-24 18:31:23 -0700 | [diff] [blame] | 338 | bo->total_size += data->sizes[plane]; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 341 | return bo; |
| 342 | } |
| 343 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 344 | void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height, |
| 345 | uint32_t flags, struct map_info **map_data, size_t plane) |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 346 | { |
| 347 | void *ptr; |
| 348 | uint8_t *addr; |
| 349 | size_t offset; |
| 350 | struct map_info *data; |
| 351 | |
| 352 | assert(width > 0); |
| 353 | assert(height > 0); |
| 354 | assert(x + width <= drv_bo_get_width(bo)); |
| 355 | assert(y + height <= drv_bo_get_height(bo)); |
| 356 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 357 | pthread_mutex_lock(&bo->drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 358 | |
| 359 | if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) { |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 360 | data = (struct map_info *)ptr; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 361 | data->refcount++; |
| 362 | goto success; |
| 363 | } |
| 364 | |
| 365 | data = calloc(1, sizeof(*data)); |
| 366 | addr = bo->drv->backend->bo_map(bo, data, plane); |
| 367 | if (addr == MAP_FAILED) { |
| 368 | *map_data = NULL; |
| 369 | free(data); |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 370 | pthread_mutex_unlock(&bo->drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 371 | return MAP_FAILED; |
| 372 | } |
| 373 | |
| 374 | data->refcount = 1; |
| 375 | data->addr = addr; |
| 376 | data->handle = bo->handles[plane].u32; |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 377 | drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 378 | |
| 379 | success: |
Gurchetan Singh | 80fc2b9 | 2017-02-14 17:47:02 -0800 | [diff] [blame] | 380 | *map_data = data; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 381 | offset = drv_bo_get_plane_stride(bo, plane) * y; |
| 382 | offset += drv_stride_from_format(bo->format, x, plane); |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 383 | addr = (uint8_t *)data->addr; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 384 | addr += drv_bo_get_plane_offset(bo, plane) + offset; |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 385 | pthread_mutex_unlock(&bo->drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 386 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 387 | return (void *)addr; |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Gurchetan Singh | 80fc2b9 | 2017-02-14 17:47:02 -0800 | [diff] [blame] | 390 | int drv_bo_unmap(struct bo *bo, struct map_info *data) |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 391 | { |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 392 | int ret = 0; |
| 393 | |
| 394 | assert(data); |
| 395 | assert(data->refcount >= 0); |
| 396 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 397 | pthread_mutex_lock(&bo->drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 398 | |
| 399 | if (!--data->refcount) { |
Gurchetan Singh | 44d1fe4 | 2016-12-14 08:51:28 -0800 | [diff] [blame] | 400 | if (bo->drv->backend->bo_unmap) |
| 401 | ret = bo->drv->backend->bo_unmap(bo, data); |
| 402 | else |
| 403 | ret = munmap(data->addr, data->length); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 404 | drmHashDelete(bo->drv->map_table, data->handle); |
| 405 | free(data); |
| 406 | } |
| 407 | |
Gurchetan Singh | 27dd470 | 2016-11-23 17:27:52 -0800 | [diff] [blame] | 408 | pthread_mutex_unlock(&bo->drv->driver_lock); |
Gurchetan Singh | 1a31e60 | 2016-10-06 10:58:00 -0700 | [diff] [blame] | 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 413 | uint32_t drv_bo_get_width(struct bo *bo) |
| 414 | { |
| 415 | return bo->width; |
| 416 | } |
| 417 | |
| 418 | uint32_t drv_bo_get_height(struct bo *bo) |
| 419 | { |
| 420 | return bo->height; |
| 421 | } |
| 422 | |
| 423 | uint32_t drv_bo_get_stride_or_tiling(struct bo *bo) |
| 424 | { |
| 425 | return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0); |
| 426 | } |
| 427 | |
| 428 | size_t drv_bo_get_num_planes(struct bo *bo) |
| 429 | { |
| 430 | return bo->num_planes; |
| 431 | } |
| 432 | |
| 433 | union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane) |
| 434 | { |
| 435 | return bo->handles[plane]; |
| 436 | } |
| 437 | |
| 438 | #ifndef DRM_RDWR |
| 439 | #define DRM_RDWR O_RDWR |
| 440 | #endif |
| 441 | |
| 442 | int drv_bo_get_plane_fd(struct bo *bo, size_t plane) |
| 443 | { |
| 444 | |
| 445 | int ret, fd; |
| 446 | assert(plane < bo->num_planes); |
| 447 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 448 | ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd); |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 449 | |
| 450 | return (ret) ? ret : fd; |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane) |
| 454 | { |
| 455 | assert(plane < bo->num_planes); |
| 456 | return bo->offsets[plane]; |
| 457 | } |
| 458 | |
| 459 | uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane) |
| 460 | { |
| 461 | assert(plane < bo->num_planes); |
| 462 | return bo->sizes[plane]; |
| 463 | } |
| 464 | |
| 465 | uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane) |
| 466 | { |
| 467 | assert(plane < bo->num_planes); |
| 468 | return bo->strides[plane]; |
| 469 | } |
| 470 | |
| 471 | uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane) |
| 472 | { |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 473 | assert(plane < bo->num_planes); |
Gurchetan Singh | 46faf6b | 2016-08-05 14:40:07 -0700 | [diff] [blame] | 474 | return bo->format_modifiers[plane]; |
| 475 | } |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 476 | |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 477 | uint32_t drv_bo_get_format(struct bo *bo) |
Gurchetan Singh | 2e786ad | 2016-08-24 18:31:23 -0700 | [diff] [blame] | 478 | { |
| 479 | return bo->format; |
| 480 | } |
| 481 | |
Tomasz Figa | ce1ae02 | 2017-07-05 18:15:06 +0900 | [diff] [blame^] | 482 | uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t usage) |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 483 | { |
| 484 | if (drv->backend->resolve_format) |
Tomasz Figa | ce1ae02 | 2017-07-05 18:15:06 +0900 | [diff] [blame^] | 485 | return drv->backend->resolve_format(format, usage); |
Gurchetan Singh | bfba8c2 | 2016-08-16 17:57:10 -0700 | [diff] [blame] | 486 | |
| 487 | return format; |
| 488 | } |
Gurchetan Singh | 2e786ad | 2016-08-24 18:31:23 -0700 | [diff] [blame] | 489 | |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 490 | size_t drv_num_planes_from_format(uint32_t format) |
| 491 | { |
| 492 | switch (format) { |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 493 | case DRM_FORMAT_ABGR1555: |
| 494 | case DRM_FORMAT_ABGR2101010: |
| 495 | case DRM_FORMAT_ABGR4444: |
| 496 | case DRM_FORMAT_ABGR8888: |
| 497 | case DRM_FORMAT_ARGB1555: |
| 498 | case DRM_FORMAT_ARGB2101010: |
| 499 | case DRM_FORMAT_ARGB4444: |
| 500 | case DRM_FORMAT_ARGB8888: |
| 501 | case DRM_FORMAT_AYUV: |
| 502 | case DRM_FORMAT_BGR233: |
| 503 | case DRM_FORMAT_BGR565: |
| 504 | case DRM_FORMAT_BGR888: |
| 505 | case DRM_FORMAT_BGRA1010102: |
| 506 | case DRM_FORMAT_BGRA4444: |
| 507 | case DRM_FORMAT_BGRA5551: |
| 508 | case DRM_FORMAT_BGRA8888: |
| 509 | case DRM_FORMAT_BGRX1010102: |
| 510 | case DRM_FORMAT_BGRX4444: |
| 511 | case DRM_FORMAT_BGRX5551: |
| 512 | case DRM_FORMAT_BGRX8888: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 513 | case DRM_FORMAT_C8: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 514 | case DRM_FORMAT_GR88: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 515 | case DRM_FORMAT_R8: |
| 516 | case DRM_FORMAT_RG88: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 517 | case DRM_FORMAT_RGB332: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 518 | case DRM_FORMAT_RGB565: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 519 | case DRM_FORMAT_RGB888: |
| 520 | case DRM_FORMAT_RGBA1010102: |
| 521 | case DRM_FORMAT_RGBA4444: |
| 522 | case DRM_FORMAT_RGBA5551: |
| 523 | case DRM_FORMAT_RGBA8888: |
| 524 | case DRM_FORMAT_RGBX1010102: |
| 525 | case DRM_FORMAT_RGBX4444: |
| 526 | case DRM_FORMAT_RGBX5551: |
| 527 | case DRM_FORMAT_RGBX8888: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 528 | case DRM_FORMAT_UYVY: |
| 529 | case DRM_FORMAT_VYUY: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 530 | case DRM_FORMAT_XBGR1555: |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 531 | case DRM_FORMAT_XBGR2101010: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 532 | case DRM_FORMAT_XBGR4444: |
| 533 | case DRM_FORMAT_XBGR8888: |
| 534 | case DRM_FORMAT_XRGB1555: |
| 535 | case DRM_FORMAT_XRGB2101010: |
| 536 | case DRM_FORMAT_XRGB4444: |
| 537 | case DRM_FORMAT_XRGB8888: |
| 538 | case DRM_FORMAT_YUYV: |
| 539 | case DRM_FORMAT_YVYU: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 540 | return 1; |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 541 | case DRM_FORMAT_NV12: |
Shirish S | df423df | 2017-04-18 16:21:59 +0530 | [diff] [blame] | 542 | case DRM_FORMAT_NV21: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 543 | return 2; |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 544 | case DRM_FORMAT_YVU420: |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 545 | case DRM_FORMAT_YVU420_ANDROID: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 546 | return 3; |
| 547 | } |
| 548 | |
| 549 | fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format); |
| 550 | return 0; |
| 551 | } |
| 552 | |
Gurchetan Singh | 1b1d56a | 2017-03-10 16:25:23 -0800 | [diff] [blame] | 553 | uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane) |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 554 | { |
| 555 | assert(plane < drv_num_planes_from_format(format)); |
| 556 | uint32_t vertical_subsampling; |
| 557 | |
| 558 | switch (format) { |
Gurchetan Singh | f3b22da | 2016-11-21 10:46:38 -0800 | [diff] [blame] | 559 | case DRM_FORMAT_NV12: |
Gurchetan Singh | e8ab0a5 | 2016-11-21 11:36:31 -0800 | [diff] [blame] | 560 | case DRM_FORMAT_YVU420: |
Gurchetan Singh | 03f1356 | 2017-02-08 15:21:14 -0800 | [diff] [blame] | 561 | case DRM_FORMAT_YVU420_ANDROID: |
Gurchetan Singh | 2a11934 | 2016-11-02 10:40:51 -0700 | [diff] [blame] | 562 | vertical_subsampling = (plane == 0) ? 1 : 2; |
| 563 | break; |
| 564 | default: |
| 565 | vertical_subsampling = 1; |
| 566 | } |
| 567 | |
| 568 | return stride * DIV_ROUND_UP(height, vertical_subsampling); |
| 569 | } |
| 570 | |
Gurchetan Singh | 2e786ad | 2016-08-24 18:31:23 -0700 | [diff] [blame] | 571 | uint32_t drv_num_buffers_per_bo(struct bo *bo) |
| 572 | { |
| 573 | uint32_t count = 0; |
| 574 | size_t plane, p; |
| 575 | |
| 576 | for (plane = 0; plane < bo->num_planes; plane++) { |
| 577 | for (p = 0; p < plane; p++) |
| 578 | if (bo->handles[p].u32 == bo->handles[plane].u32) |
| 579 | break; |
| 580 | if (p == plane) |
| 581 | count++; |
| 582 | } |
| 583 | |
| 584 | return count; |
| 585 | } |