blob: fab99588b14f4be37a9a00d116ecb63a4e0da1e0 [file] [log] [blame]
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2016 The Chromium OS Authors. All rights reserved.
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07003 * 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. Kristensenb1efbd82016-09-06 11:43:26 -07007#include <errno.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07008#include <fcntl.h>
Gurchetan Singh1647fbe2016-08-03 17:14:55 -07009#include <pthread.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070010#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +080015#include <sys/types.h>
16#include <unistd.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include <xf86drm.h>
18
19#include "drv_priv.h"
20#include "helpers.h"
21#include "util.h"
22
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053023#ifdef DRV_AMDGPU
24extern struct backend backend_amdgpu;
25#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070026extern struct backend backend_cirrus;
27extern struct backend backend_evdi;
28#ifdef DRV_EXYNOS
29extern struct backend backend_exynos;
30#endif
31extern struct backend backend_gma500;
32#ifdef DRV_I915
33extern struct backend backend_i915;
34#endif
35#ifdef DRV_MARVELL
36extern struct backend backend_marvell;
37#endif
38#ifdef DRV_MEDIATEK
39extern struct backend backend_mediatek;
40#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050041extern struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040042#ifdef DRV_RADEON
43extern struct backend backend_radeon;
44#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070045#ifdef DRV_ROCKCHIP
46extern struct backend backend_rockchip;
47#endif
48#ifdef DRV_TEGRA
49extern struct backend backend_tegra;
50#endif
51extern struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010052#ifdef DRV_VC4
53extern struct backend backend_vc4;
54#endif
Gurchetan Singh5521bde2016-09-30 14:53:32 -070055extern struct backend backend_vgem;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056extern struct backend backend_virtio_gpu;
57
58static struct backend *drv_get_backend(int fd)
59{
60 drmVersionPtr drm_version;
61 unsigned int i;
62
63 drm_version = drmGetVersion(fd);
64
65 if (!drm_version)
66 return NULL;
67
68 struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053069#ifdef DRV_AMDGPU
70 &backend_amdgpu,
71#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080072 &backend_cirrus, &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070073#ifdef DRV_EXYNOS
74 &backend_exynos,
75#endif
76 &backend_gma500,
77#ifdef DRV_I915
78 &backend_i915,
79#endif
80#ifdef DRV_MARVELL
81 &backend_marvell,
82#endif
83#ifdef DRV_MEDIATEK
84 &backend_mediatek,
85#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050086 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040087#ifdef DRV_RADEON
88 &backend_radeon,
89#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070090#ifdef DRV_ROCKCHIP
91 &backend_rockchip,
92#endif
93#ifdef DRV_TEGRA
94 &backend_tegra,
95#endif
96 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +010097#ifdef DRV_VC4
98 &backend_vc4,
99#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800100 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700101 };
102
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800103 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104 if (!strcmp(drm_version->name, backend_list[i]->name)) {
105 drmFreeVersion(drm_version);
106 return backend_list[i];
107 }
108
109 drmFreeVersion(drm_version);
110 return NULL;
111}
112
113struct driver *drv_create(int fd)
114{
115 struct driver *drv;
116 int ret;
117
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800118 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700119
120 if (!drv)
121 return NULL;
122
123 drv->fd = fd;
124 drv->backend = drv_get_backend(fd);
125
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700126 if (!drv->backend)
127 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800129 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700130 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700131
132 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700133 if (!drv->buffer_table)
134 goto free_lock;
135
136 drv->map_table = drmHashCreate();
137 if (!drv->map_table)
138 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700139
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800140 /* Start with a power of 2 number of allocations. */
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700141 drv->combos.allocations = 2;
142 drv->combos.size = 0;
143
144 drv->combos.data = calloc(drv->combos.allocations, sizeof(struct combination));
145 if (!drv->combos.data)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800146 goto free_map_table;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700147
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700148 if (drv->backend->init) {
149 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800150 if (ret) {
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700151 free(drv->combos.data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700152 goto free_map_table;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800153 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700154 }
155
156 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700157
158free_map_table:
159 drmHashDestroy(drv->map_table);
160free_buffer_table:
161 drmHashDestroy(drv->buffer_table);
162free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800163 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700164free_driver:
165 free(drv);
166 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700167}
168
169void drv_destroy(struct driver *drv)
170{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800171 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700172
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700173 if (drv->backend->close)
174 drv->backend->close(drv);
175
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700176 drmHashDestroy(drv->buffer_table);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700177 drmHashDestroy(drv->map_table);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700178
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700179 free(drv->combos.data);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800181 pthread_mutex_unlock(&drv->driver_lock);
182 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700183
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700184 free(drv);
185}
186
187int drv_get_fd(struct driver *drv)
188{
189 return drv->fd;
190}
191
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800192const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193{
194 return drv->backend->name;
195}
196
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800197struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t usage)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800199 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200
Gurchetan Singh458976f2016-11-23 17:32:33 -0800201 if (format == DRM_FORMAT_NONE || usage == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202 return 0;
203
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800204 best = NULL;
205 uint32_t i;
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700206 for (i = 0; i < drv->combos.size; i++) {
207 curr = &drv->combos.data[i];
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800208 if ((format == curr->format) && usage == (curr->usage & usage))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800209 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211 }
212
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800213 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214}
215
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700216struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
217 uint64_t flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218{
219
220 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800221 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222
223 if (!bo)
224 return NULL;
225
226 bo->drv = drv;
227 bo->width = width;
228 bo->height = height;
229 bo->format = format;
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700230 bo->flags = flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700231 bo->num_planes = drv_num_planes_from_format(format);
232
233 if (!bo->num_planes) {
234 free(bo);
235 return NULL;
236 }
237
238 return bo;
239}
240
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800241struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
242 uint64_t flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243{
244 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700245 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246 struct bo *bo;
247
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700248 bo = drv_bo_new(drv, width, height, format, flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700249
250 if (!bo)
251 return NULL;
252
253 ret = drv->backend->bo_create(bo, width, height, format, flags);
254
255 if (ret) {
256 free(bo);
257 return NULL;
258 }
259
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800260 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700261
262 for (plane = 0; plane < bo->num_planes; plane++)
263 drv_increment_reference_count(drv, bo, plane);
264
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800265 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700266
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700267 return bo;
268}
269
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800270struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
271 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700272{
273 int ret;
274 size_t plane;
275 struct bo *bo;
276
277 if (!drv->backend->bo_create_with_modifiers) {
278 errno = ENOENT;
279 return NULL;
280 }
281
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700282 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700283
284 if (!bo)
285 return NULL;
286
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800287 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700288
289 if (ret) {
290 free(bo);
291 return NULL;
292 }
293
294 pthread_mutex_lock(&drv->driver_lock);
295
296 for (plane = 0; plane < bo->num_planes; plane++)
297 drv_increment_reference_count(drv, bo, plane);
298
299 pthread_mutex_unlock(&drv->driver_lock);
300
301 return bo;
302}
303
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700304void drv_bo_destroy(struct bo *bo)
305{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700306 size_t plane;
307 uintptr_t total = 0;
308 struct driver *drv = bo->drv;
309
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800310 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700311
312 for (plane = 0; plane < bo->num_planes; plane++)
313 drv_decrement_reference_count(drv, bo, plane);
314
Gurchetan Singhde263f12017-01-24 13:30:06 -0800315 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700316 total += drv_get_reference_count(drv, bo, plane);
317
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800318 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700319
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900320 if (total == 0) {
Gurchetan Singhde263f12017-01-24 13:30:06 -0800321 assert(drv_map_info_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700322 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900323 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700325 free(bo);
326}
327
328struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
329{
330 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700331 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700332 struct bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700333
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700334 bo = drv_bo_new(drv, data->width, data->height, data->format, data->flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700335
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700336 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700337 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700338
Gurchetan Singh71611d62017-01-03 16:49:56 -0800339 ret = drv->backend->bo_import(bo, data);
340 if (ret) {
341 free(bo);
342 return NULL;
343 }
344
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700345 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700346 bo->strides[plane] = data->strides[plane];
347 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700348 bo->format_modifiers[plane] = data->format_modifiers[plane];
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800349 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0) {
350 bo->sizes[plane] =
351 lseek(data->fds[plane], 0, SEEK_END) - data->offsets[plane];
352 lseek(data->fds[plane], 0, SEEK_SET);
353 } else {
354 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
355 }
356
357 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700358 }
359
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700360 return bo;
361}
362
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800363void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
364 uint32_t flags, struct map_info **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700365{
366 void *ptr;
367 uint8_t *addr;
368 size_t offset;
369 struct map_info *data;
Joe Kniss65705852017-06-29 15:02:46 -0700370 int prot;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700371
372 assert(width > 0);
373 assert(height > 0);
374 assert(x + width <= drv_bo_get_width(bo));
375 assert(y + height <= drv_bo_get_height(bo));
Joe Kniss65705852017-06-29 15:02:46 -0700376 assert(BO_TRANSFER_READ_WRITE & flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700377
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800378 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700379
380 if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800381 data = (struct map_info *)ptr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700382 data->refcount++;
383 goto success;
384 }
385
386 data = calloc(1, sizeof(*data));
Joe Kniss65705852017-06-29 15:02:46 -0700387 prot = BO_TRANSFER_WRITE & flags ? PROT_WRITE | PROT_READ : PROT_READ;
388 addr = bo->drv->backend->bo_map(bo, data, plane, prot);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700389 if (addr == MAP_FAILED) {
390 *map_data = NULL;
391 free(data);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800392 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700393 return MAP_FAILED;
394 }
395
396 data->refcount = 1;
397 data->addr = addr;
398 data->handle = bo->handles[plane].u32;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800399 drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700400
401success:
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800402 *map_data = data;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700403 offset = drv_bo_get_plane_stride(bo, plane) * y;
404 offset += drv_stride_from_format(bo->format, x, plane);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800405 addr = (uint8_t *)data->addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700406 addr += drv_bo_get_plane_offset(bo, plane) + offset;
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800407 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700408
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800409 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700410}
411
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800412int drv_bo_unmap(struct bo *bo, struct map_info *data)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700413{
Gurchetan Singhff741412017-09-13 17:54:36 -0700414 int ret = drv_bo_flush(bo, data);
415 if (ret)
416 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700417
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800418 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700419
420 if (!--data->refcount) {
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700421 ret = bo->drv->backend->bo_unmap(bo, data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700422 drmHashDelete(bo->drv->map_table, data->handle);
423 free(data);
424 }
425
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800426 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427
428 return ret;
429}
430
Gurchetan Singhff741412017-09-13 17:54:36 -0700431int drv_bo_flush(struct bo *bo, struct map_info *data)
432{
433 int ret = 0;
434 assert(data);
435 assert(data->refcount >= 0);
436
437 if (bo->drv->backend->bo_flush)
438 ret = bo->drv->backend->bo_flush(bo, data);
439
440 return ret;
441}
442
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700443uint32_t drv_bo_get_width(struct bo *bo)
444{
445 return bo->width;
446}
447
448uint32_t drv_bo_get_height(struct bo *bo)
449{
450 return bo->height;
451}
452
453uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
454{
455 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
456}
457
458size_t drv_bo_get_num_planes(struct bo *bo)
459{
460 return bo->num_planes;
461}
462
463union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
464{
465 return bo->handles[plane];
466}
467
468#ifndef DRM_RDWR
469#define DRM_RDWR O_RDWR
470#endif
471
472int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
473{
474
475 int ret, fd;
476 assert(plane < bo->num_planes);
477
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800478 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700479
480 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700481}
482
483uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
484{
485 assert(plane < bo->num_planes);
486 return bo->offsets[plane];
487}
488
489uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
490{
491 assert(plane < bo->num_planes);
492 return bo->sizes[plane];
493}
494
495uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
496{
497 assert(plane < bo->num_planes);
498 return bo->strides[plane];
499}
500
501uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
502{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800503 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700504 return bo->format_modifiers[plane];
505}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700506
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800507uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700508{
509 return bo->format;
510}
511
Tomasz Figace1ae022017-07-05 18:15:06 +0900512uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t usage)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700513{
514 if (drv->backend->resolve_format)
Tomasz Figace1ae022017-07-05 18:15:06 +0900515 return drv->backend->resolve_format(format, usage);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700516
517 return format;
518}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700519
Gurchetan Singh2a119342016-11-02 10:40:51 -0700520size_t drv_num_planes_from_format(uint32_t format)
521{
522 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800523 case DRM_FORMAT_ABGR1555:
524 case DRM_FORMAT_ABGR2101010:
525 case DRM_FORMAT_ABGR4444:
526 case DRM_FORMAT_ABGR8888:
527 case DRM_FORMAT_ARGB1555:
528 case DRM_FORMAT_ARGB2101010:
529 case DRM_FORMAT_ARGB4444:
530 case DRM_FORMAT_ARGB8888:
531 case DRM_FORMAT_AYUV:
532 case DRM_FORMAT_BGR233:
533 case DRM_FORMAT_BGR565:
534 case DRM_FORMAT_BGR888:
535 case DRM_FORMAT_BGRA1010102:
536 case DRM_FORMAT_BGRA4444:
537 case DRM_FORMAT_BGRA5551:
538 case DRM_FORMAT_BGRA8888:
539 case DRM_FORMAT_BGRX1010102:
540 case DRM_FORMAT_BGRX4444:
541 case DRM_FORMAT_BGRX5551:
542 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800543 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800544 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800545 case DRM_FORMAT_R8:
546 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800547 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800548 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800549 case DRM_FORMAT_RGB888:
550 case DRM_FORMAT_RGBA1010102:
551 case DRM_FORMAT_RGBA4444:
552 case DRM_FORMAT_RGBA5551:
553 case DRM_FORMAT_RGBA8888:
554 case DRM_FORMAT_RGBX1010102:
555 case DRM_FORMAT_RGBX4444:
556 case DRM_FORMAT_RGBX5551:
557 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800558 case DRM_FORMAT_UYVY:
559 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800560 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800561 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800562 case DRM_FORMAT_XBGR4444:
563 case DRM_FORMAT_XBGR8888:
564 case DRM_FORMAT_XRGB1555:
565 case DRM_FORMAT_XRGB2101010:
566 case DRM_FORMAT_XRGB4444:
567 case DRM_FORMAT_XRGB8888:
568 case DRM_FORMAT_YUYV:
569 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700570 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800571 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530572 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700573 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800574 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800575 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700576 return 3;
577 }
578
579 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
580 return 0;
581}
582
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800583uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
Gurchetan Singh2a119342016-11-02 10:40:51 -0700584{
585 assert(plane < drv_num_planes_from_format(format));
586 uint32_t vertical_subsampling;
587
588 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800589 case DRM_FORMAT_NV12:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800590 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800591 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700592 vertical_subsampling = (plane == 0) ? 1 : 2;
593 break;
594 default:
595 vertical_subsampling = 1;
596 }
597
598 return stride * DIV_ROUND_UP(height, vertical_subsampling);
599}
600
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700601uint32_t drv_num_buffers_per_bo(struct bo *bo)
602{
603 uint32_t count = 0;
604 size_t plane, p;
605
606 for (plane = 0; plane < bo->num_planes; plane++) {
607 for (p = 0; p < plane; p++)
608 if (bo->handles[p].u32 == bo->handles[plane].u32)
609 break;
610 if (p == plane)
611 count++;
612 }
613
614 return count;
615}