blob: 3ea46e6b617299346edab9139af31050cb99b4a9 [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
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700262 for (plane = 0; plane < bo->num_planes; plane++) {
263 if (plane > 0)
264 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
265
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700266 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700267 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700268
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800269 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700270
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700271 return bo;
272}
273
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800274struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
275 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700276{
277 int ret;
278 size_t plane;
279 struct bo *bo;
280
281 if (!drv->backend->bo_create_with_modifiers) {
282 errno = ENOENT;
283 return NULL;
284 }
285
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700286 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700287
288 if (!bo)
289 return NULL;
290
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800291 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700292
293 if (ret) {
294 free(bo);
295 return NULL;
296 }
297
298 pthread_mutex_lock(&drv->driver_lock);
299
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700300 for (plane = 0; plane < bo->num_planes; plane++) {
301 if (plane > 0)
302 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
303
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700304 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700305 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700306
307 pthread_mutex_unlock(&drv->driver_lock);
308
309 return bo;
310}
311
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700312void drv_bo_destroy(struct bo *bo)
313{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700314 size_t plane;
315 uintptr_t total = 0;
316 struct driver *drv = bo->drv;
317
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800318 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700319
320 for (plane = 0; plane < bo->num_planes; plane++)
321 drv_decrement_reference_count(drv, bo, plane);
322
Gurchetan Singhde263f12017-01-24 13:30:06 -0800323 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324 total += drv_get_reference_count(drv, bo, plane);
325
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800326 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700327
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900328 if (total == 0) {
Gurchetan Singhde263f12017-01-24 13:30:06 -0800329 assert(drv_map_info_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700330 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900331 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700332
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700333 free(bo);
334}
335
336struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
337{
338 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700339 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700340 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700341 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700342
Gurchetan Singha1892b22017-09-28 16:40:52 -0700343 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700345 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700346 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700347
Gurchetan Singh71611d62017-01-03 16:49:56 -0800348 ret = drv->backend->bo_import(bo, data);
349 if (ret) {
350 free(bo);
351 return NULL;
352 }
353
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700354 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700355 bo->strides[plane] = data->strides[plane];
356 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700357 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700358
359 seek_end = lseek(data->fds[plane], 0, SEEK_END);
360 if (seek_end == (off_t)(-1)) {
361 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
362 goto destroy_bo;
363 }
364
365 lseek(data->fds[plane], 0, SEEK_SET);
366 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
367 bo->sizes[plane] = seek_end - data->offsets[plane];
368 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800369 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700370
371 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
372 fprintf(stderr, "drv: buffer size is too large.\n");
373 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800374 }
375
376 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700377 }
378
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700379 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700380
381destroy_bo:
382 drv_bo_destroy(bo);
383 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700384}
385
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800386void *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 -0700387 uint32_t map_flags, struct map_info **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700388{
389 void *ptr;
390 uint8_t *addr;
391 size_t offset;
392 struct map_info *data;
393
394 assert(width > 0);
395 assert(height > 0);
396 assert(x + width <= drv_bo_get_width(bo));
397 assert(y + height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700398 assert(BO_MAP_READ_WRITE & map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700399
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800400 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700401
402 if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800403 data = (struct map_info *)ptr;
Gurchetan Singhe29a6402017-10-05 14:52:24 -0700404 /* TODO(gsingh): support mapping same buffer with different flags. */
405 assert(data->map_flags == map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700406 data->refcount++;
407 goto success;
408 }
409
410 data = calloc(1, sizeof(*data));
Gurchetan Singhcfb88762017-09-28 17:14:50 -0700411 addr = bo->drv->backend->bo_map(bo, data, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700412 if (addr == MAP_FAILED) {
413 *map_data = NULL;
414 free(data);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800415 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700416 return MAP_FAILED;
417 }
418
419 data->refcount = 1;
420 data->addr = addr;
421 data->handle = bo->handles[plane].u32;
Gurchetan Singhe29a6402017-10-05 14:52:24 -0700422 data->map_flags = map_flags;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800423 drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700424
425success:
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700426 drv_bo_invalidate(bo, data);
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800427 *map_data = data;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700428 offset = drv_bo_get_plane_stride(bo, plane) * y;
429 offset += drv_stride_from_format(bo->format, x, plane);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800430 addr = (uint8_t *)data->addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700431 addr += drv_bo_get_plane_offset(bo, plane) + offset;
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800432 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700433
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800434 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700435}
436
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800437int drv_bo_unmap(struct bo *bo, struct map_info *data)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700438{
Gurchetan Singhff741412017-09-13 17:54:36 -0700439 int ret = drv_bo_flush(bo, data);
440 if (ret)
441 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700442
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800443 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700444
445 if (!--data->refcount) {
Gurchetan Singhba6bd502017-09-18 15:29:47 -0700446 ret = bo->drv->backend->bo_unmap(bo, data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700447 drmHashDelete(bo->drv->map_table, data->handle);
448 free(data);
449 }
450
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800451 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700452
453 return ret;
454}
455
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700456int drv_bo_invalidate(struct bo *bo, struct map_info *data)
457{
458 int ret = 0;
459 assert(data);
460 assert(data->refcount >= 0);
461
462 if (bo->drv->backend->bo_invalidate)
463 ret = bo->drv->backend->bo_invalidate(bo, data);
464
465 return ret;
466}
467
Gurchetan Singhff741412017-09-13 17:54:36 -0700468int drv_bo_flush(struct bo *bo, struct map_info *data)
469{
470 int ret = 0;
471 assert(data);
472 assert(data->refcount >= 0);
473
474 if (bo->drv->backend->bo_flush)
475 ret = bo->drv->backend->bo_flush(bo, data);
476
477 return ret;
478}
479
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700480uint32_t drv_bo_get_width(struct bo *bo)
481{
482 return bo->width;
483}
484
485uint32_t drv_bo_get_height(struct bo *bo)
486{
487 return bo->height;
488}
489
490uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
491{
492 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
493}
494
495size_t drv_bo_get_num_planes(struct bo *bo)
496{
497 return bo->num_planes;
498}
499
500union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
501{
502 return bo->handles[plane];
503}
504
505#ifndef DRM_RDWR
506#define DRM_RDWR O_RDWR
507#endif
508
509int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
510{
511
512 int ret, fd;
513 assert(plane < bo->num_planes);
514
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800515 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700516
517 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700518}
519
520uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
521{
522 assert(plane < bo->num_planes);
523 return bo->offsets[plane];
524}
525
526uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
527{
528 assert(plane < bo->num_planes);
529 return bo->sizes[plane];
530}
531
532uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
533{
534 assert(plane < bo->num_planes);
535 return bo->strides[plane];
536}
537
538uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
539{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800540 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700541 return bo->format_modifiers[plane];
542}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700543
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800544uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700545{
546 return bo->format;
547}
548
Gurchetan Singha1892b22017-09-28 16:40:52 -0700549uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700550{
551 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700552 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700553
554 return format;
555}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700556
Gurchetan Singh2a119342016-11-02 10:40:51 -0700557size_t drv_num_planes_from_format(uint32_t format)
558{
559 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800560 case DRM_FORMAT_ABGR1555:
561 case DRM_FORMAT_ABGR2101010:
562 case DRM_FORMAT_ABGR4444:
563 case DRM_FORMAT_ABGR8888:
564 case DRM_FORMAT_ARGB1555:
565 case DRM_FORMAT_ARGB2101010:
566 case DRM_FORMAT_ARGB4444:
567 case DRM_FORMAT_ARGB8888:
568 case DRM_FORMAT_AYUV:
569 case DRM_FORMAT_BGR233:
570 case DRM_FORMAT_BGR565:
571 case DRM_FORMAT_BGR888:
572 case DRM_FORMAT_BGRA1010102:
573 case DRM_FORMAT_BGRA4444:
574 case DRM_FORMAT_BGRA5551:
575 case DRM_FORMAT_BGRA8888:
576 case DRM_FORMAT_BGRX1010102:
577 case DRM_FORMAT_BGRX4444:
578 case DRM_FORMAT_BGRX5551:
579 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800580 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800581 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800582 case DRM_FORMAT_R8:
583 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800584 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800585 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800586 case DRM_FORMAT_RGB888:
587 case DRM_FORMAT_RGBA1010102:
588 case DRM_FORMAT_RGBA4444:
589 case DRM_FORMAT_RGBA5551:
590 case DRM_FORMAT_RGBA8888:
591 case DRM_FORMAT_RGBX1010102:
592 case DRM_FORMAT_RGBX4444:
593 case DRM_FORMAT_RGBX5551:
594 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800595 case DRM_FORMAT_UYVY:
596 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800597 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800598 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800599 case DRM_FORMAT_XBGR4444:
600 case DRM_FORMAT_XBGR8888:
601 case DRM_FORMAT_XRGB1555:
602 case DRM_FORMAT_XRGB2101010:
603 case DRM_FORMAT_XRGB4444:
604 case DRM_FORMAT_XRGB8888:
605 case DRM_FORMAT_YUYV:
606 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700607 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800608 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530609 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700610 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800611 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800612 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700613 return 3;
614 }
615
616 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
617 return 0;
618}
619
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700620uint32_t drv_num_buffers_per_bo(struct bo *bo)
621{
622 uint32_t count = 0;
623 size_t plane, p;
624
625 for (plane = 0; plane < bo->num_planes; plane++) {
626 for (p = 0; p < plane; p++)
627 if (bo->handles[p].u32 == bo->handles[plane].u32)
628 break;
629 if (p == plane)
630 count++;
631 }
632
633 return count;
634}