blob: b7a8f917561c06d7d1f8e282c3177123e49a5845 [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 Singha1892b22017-09-28 16:40:52 -0700197struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
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 Singha1892b22017-09-28 16:40:52 -0700201 if (format == DRM_FORMAT_NONE || use_flags == 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 Singha1892b22017-09-28 16:40:52 -0700208 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
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,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700217 uint64_t use_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 Singha1892b22017-09-28 16:40:52 -0700230 bo->use_flags = use_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,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700242 uint64_t use_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 Singha1892b22017-09-28 16:40:52 -0700248 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700249
250 if (!bo)
251 return NULL;
252
Gurchetan Singha1892b22017-09-28 16:40:52 -0700253 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254
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 Singha1892b22017-09-28 16:40:52 -0700334 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_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,
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700364 uint32_t map_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;
370
371 assert(width > 0);
372 assert(height > 0);
373 assert(x + width <= drv_bo_get_width(bo));
374 assert(y + height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700375 assert(BO_MAP_READ_WRITE & map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700376
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800377 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700378
379 if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800380 data = (struct map_info *)ptr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700381 data->refcount++;
382 goto success;
383 }
384
385 data = calloc(1, sizeof(*data));
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700386 addr = bo->drv->backend->bo_map(bo, data, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700387 if (addr == MAP_FAILED) {
388 *map_data = NULL;
389 free(data);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800390 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700391 return MAP_FAILED;
392 }
393
394 data->refcount = 1;
395 data->addr = addr;
396 data->handle = bo->handles[plane].u32;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800397 drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700398
399success:
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800400 *map_data = data;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700401 offset = drv_bo_get_plane_stride(bo, plane) * y;
402 offset += drv_stride_from_format(bo->format, x, plane);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800403 addr = (uint8_t *)data->addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700404 addr += drv_bo_get_plane_offset(bo, plane) + offset;
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800405 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700406
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800407 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700408}
409
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800410int drv_bo_unmap(struct bo *bo, struct map_info *data)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700411{
Gurchetan Singhff741412017-09-13 17:54:36 -0700412 int ret = drv_bo_flush(bo, data);
413 if (ret)
414 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700415
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800416 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700417
418 if (!--data->refcount) {
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700419 ret = bo->drv->backend->bo_unmap(bo, data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700420 drmHashDelete(bo->drv->map_table, data->handle);
421 free(data);
422 }
423
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800424 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700425
426 return ret;
427}
428
Gurchetan Singhff741412017-09-13 17:54:36 -0700429int drv_bo_flush(struct bo *bo, struct map_info *data)
430{
431 int ret = 0;
432 assert(data);
433 assert(data->refcount >= 0);
434
435 if (bo->drv->backend->bo_flush)
436 ret = bo->drv->backend->bo_flush(bo, data);
437
438 return ret;
439}
440
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700441uint32_t drv_bo_get_width(struct bo *bo)
442{
443 return bo->width;
444}
445
446uint32_t drv_bo_get_height(struct bo *bo)
447{
448 return bo->height;
449}
450
451uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
452{
453 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
454}
455
456size_t drv_bo_get_num_planes(struct bo *bo)
457{
458 return bo->num_planes;
459}
460
461union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
462{
463 return bo->handles[plane];
464}
465
466#ifndef DRM_RDWR
467#define DRM_RDWR O_RDWR
468#endif
469
470int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
471{
472
473 int ret, fd;
474 assert(plane < bo->num_planes);
475
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800476 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700477
478 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700479}
480
481uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
482{
483 assert(plane < bo->num_planes);
484 return bo->offsets[plane];
485}
486
487uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
488{
489 assert(plane < bo->num_planes);
490 return bo->sizes[plane];
491}
492
493uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
494{
495 assert(plane < bo->num_planes);
496 return bo->strides[plane];
497}
498
499uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
500{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800501 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700502 return bo->format_modifiers[plane];
503}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700504
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800505uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700506{
507 return bo->format;
508}
509
Gurchetan Singha1892b22017-09-28 16:40:52 -0700510uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700511{
512 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700513 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700514
515 return format;
516}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700517
Gurchetan Singh2a119342016-11-02 10:40:51 -0700518size_t drv_num_planes_from_format(uint32_t format)
519{
520 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800521 case DRM_FORMAT_ABGR1555:
522 case DRM_FORMAT_ABGR2101010:
523 case DRM_FORMAT_ABGR4444:
524 case DRM_FORMAT_ABGR8888:
525 case DRM_FORMAT_ARGB1555:
526 case DRM_FORMAT_ARGB2101010:
527 case DRM_FORMAT_ARGB4444:
528 case DRM_FORMAT_ARGB8888:
529 case DRM_FORMAT_AYUV:
530 case DRM_FORMAT_BGR233:
531 case DRM_FORMAT_BGR565:
532 case DRM_FORMAT_BGR888:
533 case DRM_FORMAT_BGRA1010102:
534 case DRM_FORMAT_BGRA4444:
535 case DRM_FORMAT_BGRA5551:
536 case DRM_FORMAT_BGRA8888:
537 case DRM_FORMAT_BGRX1010102:
538 case DRM_FORMAT_BGRX4444:
539 case DRM_FORMAT_BGRX5551:
540 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800541 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800542 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800543 case DRM_FORMAT_R8:
544 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800545 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800546 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800547 case DRM_FORMAT_RGB888:
548 case DRM_FORMAT_RGBA1010102:
549 case DRM_FORMAT_RGBA4444:
550 case DRM_FORMAT_RGBA5551:
551 case DRM_FORMAT_RGBA8888:
552 case DRM_FORMAT_RGBX1010102:
553 case DRM_FORMAT_RGBX4444:
554 case DRM_FORMAT_RGBX5551:
555 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800556 case DRM_FORMAT_UYVY:
557 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800558 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800559 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800560 case DRM_FORMAT_XBGR4444:
561 case DRM_FORMAT_XBGR8888:
562 case DRM_FORMAT_XRGB1555:
563 case DRM_FORMAT_XRGB2101010:
564 case DRM_FORMAT_XRGB4444:
565 case DRM_FORMAT_XRGB8888:
566 case DRM_FORMAT_YUYV:
567 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700568 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800569 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530570 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700571 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800572 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800573 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700574 return 3;
575 }
576
577 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
578 return 0;
579}
580
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700581uint32_t drv_num_buffers_per_bo(struct bo *bo)
582{
583 uint32_t count = 0;
584 size_t plane, p;
585
586 for (plane = 0; plane < bo->num_planes; plane++) {
587 for (p = 0; p < plane; p++)
588 if (bo->handles[p].u32 == bo->handles[plane].u32)
589 break;
590 if (p == plane)
591 count++;
592 }
593
594 return count;
595}