blob: 881eefde35196ceca98bb6281c11889c048a249e [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
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070024extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053025#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070026extern const struct backend backend_evdi;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070027#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070028extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070029#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070030extern const struct backend backend_gma500;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070031#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070032extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#endif
34#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
37#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070038extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070039#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070040extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040041#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040043#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070044#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070045extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070046#endif
47#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070048extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010051#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010053#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070054extern const struct backend backend_vgem;
55extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070057static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058{
59 drmVersionPtr drm_version;
60 unsigned int i;
61
62 drm_version = drmGetVersion(fd);
63
64 if (!drm_version)
65 return NULL;
66
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053068#ifdef DRV_AMDGPU
69 &backend_amdgpu,
70#endif
Gurchetan Singh08052342018-01-11 15:42:36 -080071#ifdef DRV_AMLOGIC
72 &backend_amlogic,
73#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070074 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070075#ifdef DRV_EXYNOS
76 &backend_exynos,
77#endif
78 &backend_gma500,
79#ifdef DRV_I915
80 &backend_i915,
81#endif
82#ifdef DRV_MARVELL
83 &backend_marvell,
84#endif
85#ifdef DRV_MEDIATEK
86 &backend_mediatek,
87#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050088 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040089#ifdef DRV_RADEON
90 &backend_radeon,
91#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070092#ifdef DRV_ROCKCHIP
93 &backend_rockchip,
94#endif
95#ifdef DRV_TEGRA
96 &backend_tegra,
97#endif
98 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +010099#ifdef DRV_VC4
100 &backend_vc4,
101#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800102 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700103 };
104
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800105 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700106 if (!strcmp(drm_version->name, backend_list[i]->name)) {
107 drmFreeVersion(drm_version);
108 return backend_list[i];
109 }
110
111 drmFreeVersion(drm_version);
112 return NULL;
113}
114
115struct driver *drv_create(int fd)
116{
117 struct driver *drv;
118 int ret;
119
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800120 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700121
122 if (!drv)
123 return NULL;
124
125 drv->fd = fd;
126 drv->backend = drv_get_backend(fd);
127
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700128 if (!drv->backend)
129 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800131 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700132 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700133
134 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 if (!drv->buffer_table)
136 goto free_lock;
137
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700138 drv->mappings = drv_array_init(sizeof(struct mapping));
139 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700140 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700141
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700142 drv->combos = drv_array_init(sizeof(struct combination));
143 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700144 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 if (drv->backend->init) {
147 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800148 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700149 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700150 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800151 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152 }
153
154 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700155
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700156free_mappings:
157 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700158free_buffer_table:
159 drmHashDestroy(drv->buffer_table);
160free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800161 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162free_driver:
163 free(drv);
164 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700165}
166
167void drv_destroy(struct driver *drv)
168{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800169 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700170
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700171 if (drv->backend->close)
172 drv->backend->close(drv);
173
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700174 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700175 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700176 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800178 pthread_mutex_unlock(&drv->driver_lock);
179 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 free(drv);
182}
183
184int drv_get_fd(struct driver *drv)
185{
186 return drv->fd;
187}
188
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800189const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190{
191 return drv->backend->name;
192}
193
Gurchetan Singha1892b22017-09-28 16:40:52 -0700194struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700195{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800196 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197
Gurchetan Singha1892b22017-09-28 16:40:52 -0700198 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199 return 0;
200
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800201 best = NULL;
202 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700203 for (i = 0; i < drv_array_size(drv->combos); i++) {
204 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700205 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800206 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208 }
209
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211}
212
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700213struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700214 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700215{
216
217 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800218 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219
220 if (!bo)
221 return NULL;
222
223 bo->drv = drv;
224 bo->width = width;
225 bo->height = height;
226 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700227 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700228 bo->num_planes = drv_num_planes_from_format(format);
229
230 if (!bo->num_planes) {
231 free(bo);
232 return NULL;
233 }
234
235 return bo;
236}
237
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800238struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700239 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700240{
241 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700242 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243 struct bo *bo;
244
Gurchetan Singha1892b22017-09-28 16:40:52 -0700245 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246
247 if (!bo)
248 return NULL;
249
Gurchetan Singha1892b22017-09-28 16:40:52 -0700250 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700251
252 if (ret) {
253 free(bo);
254 return NULL;
255 }
256
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800257 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700258
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700259 for (plane = 0; plane < bo->num_planes; plane++) {
260 if (plane > 0)
261 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
262
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700263 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700264 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800266 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700267
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700268 return bo;
269}
270
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800271struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
272 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700273{
274 int ret;
275 size_t plane;
276 struct bo *bo;
277
278 if (!drv->backend->bo_create_with_modifiers) {
279 errno = ENOENT;
280 return NULL;
281 }
282
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700283 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700284
285 if (!bo)
286 return NULL;
287
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800288 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700289
290 if (ret) {
291 free(bo);
292 return NULL;
293 }
294
295 pthread_mutex_lock(&drv->driver_lock);
296
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700297 for (plane = 0; plane < bo->num_planes; plane++) {
298 if (plane > 0)
299 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
300
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700301 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700302 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700303
304 pthread_mutex_unlock(&drv->driver_lock);
305
306 return bo;
307}
308
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700309void drv_bo_destroy(struct bo *bo)
310{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700311 size_t plane;
312 uintptr_t total = 0;
313 struct driver *drv = bo->drv;
314
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800315 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700316
317 for (plane = 0; plane < bo->num_planes; plane++)
318 drv_decrement_reference_count(drv, bo, plane);
319
Gurchetan Singhde263f12017-01-24 13:30:06 -0800320 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700321 total += drv_get_reference_count(drv, bo, plane);
322
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800323 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900325 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700326 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700327 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900328 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700329
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700330 free(bo);
331}
332
333struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
334{
335 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700336 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700337 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700338 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700339
Gurchetan Singha1892b22017-09-28 16:40:52 -0700340 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700341
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700342 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700343 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700344
Gurchetan Singh71611d62017-01-03 16:49:56 -0800345 ret = drv->backend->bo_import(bo, data);
346 if (ret) {
347 free(bo);
348 return NULL;
349 }
350
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700351 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700352 bo->strides[plane] = data->strides[plane];
353 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700354 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700355
356 seek_end = lseek(data->fds[plane], 0, SEEK_END);
357 if (seek_end == (off_t)(-1)) {
358 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
359 goto destroy_bo;
360 }
361
362 lseek(data->fds[plane], 0, SEEK_SET);
363 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
364 bo->sizes[plane] = seek_end - data->offsets[plane];
365 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800366 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700367
368 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
369 fprintf(stderr, "drv: buffer size is too large.\n");
370 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800371 }
372
373 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700374 }
375
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700376 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700377
378destroy_bo:
379 drv_bo_destroy(bo);
380 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700381}
382
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800383void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
384 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700385{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700386 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700387 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700388 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700389
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800390 assert(rect->width >= 0);
391 assert(rect->height >= 0);
392 assert(rect->x + rect->width <= drv_bo_get_width(bo));
393 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700394 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900395 /* No CPU access for protected buffers. */
396 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700397
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700398 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800399 mapping.rect = *rect;
400 mapping.refcount = 1;
401
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800402 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700403
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700404 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
405 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
406 if (prior->vma->handle != bo->handles[plane].u32 ||
407 prior->vma->map_flags != map_flags)
408 continue;
409
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800410 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
411 rect->width != prior->rect.width || rect->height != prior->rect.height)
412 continue;
413
414 prior->refcount++;
415 *map_data = prior;
416 goto exact_match;
417 }
418
419 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
420 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
421 if (prior->vma->handle != bo->handles[plane].u32 ||
422 prior->vma->map_flags != map_flags)
423 continue;
424
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700425 prior->vma->refcount++;
426 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427 goto success;
428 }
429
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700430 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800431 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700432 if (addr == MAP_FAILED) {
433 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700434 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800435 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700436 return MAP_FAILED;
437 }
438
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700439 mapping.vma->refcount = 1;
440 mapping.vma->addr = addr;
441 mapping.vma->handle = bo->handles[plane].u32;
442 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700443
444success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700445 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800446exact_match:
447 drv_bo_invalidate(bo, *map_data);
448 addr = (uint8_t *)((*map_data)->vma->addr);
449 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800450 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800451 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700452}
453
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700454int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700455{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700456 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700457 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700458 if (ret)
459 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700460
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800461 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700462
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800463 if (--mapping->refcount)
464 goto out;
465
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700466 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800467 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700468 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700469 }
470
471 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
472 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
473 drv_array_remove(bo->drv->mappings, i);
474 break;
475 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700476 }
477
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800478out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800479 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700480 return ret;
481}
482
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700483int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700484{
485 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700486
487 assert(mapping);
488 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800489 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700490 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700491
492 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700493 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700494
495 return ret;
496}
497
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700498int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700499{
500 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501
502 assert(mapping);
503 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800504 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700505 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900506 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700507
508 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700509 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700510
511 return ret;
512}
513
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700514uint32_t drv_bo_get_width(struct bo *bo)
515{
516 return bo->width;
517}
518
519uint32_t drv_bo_get_height(struct bo *bo)
520{
521 return bo->height;
522}
523
524uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
525{
526 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
527}
528
529size_t drv_bo_get_num_planes(struct bo *bo)
530{
531 return bo->num_planes;
532}
533
534union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
535{
536 return bo->handles[plane];
537}
538
539#ifndef DRM_RDWR
540#define DRM_RDWR O_RDWR
541#endif
542
543int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
544{
545
546 int ret, fd;
547 assert(plane < bo->num_planes);
548
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800549 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700550
551 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700552}
553
554uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
555{
556 assert(plane < bo->num_planes);
557 return bo->offsets[plane];
558}
559
560uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
561{
562 assert(plane < bo->num_planes);
563 return bo->sizes[plane];
564}
565
566uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
567{
568 assert(plane < bo->num_planes);
569 return bo->strides[plane];
570}
571
572uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
573{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800574 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700575 return bo->format_modifiers[plane];
576}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700577
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800578uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700579{
580 return bo->format;
581}
582
Gurchetan Singha1892b22017-09-28 16:40:52 -0700583uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700584{
585 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700586 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700587
588 return format;
589}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700590
Gurchetan Singh2a119342016-11-02 10:40:51 -0700591size_t drv_num_planes_from_format(uint32_t format)
592{
593 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800594 case DRM_FORMAT_ABGR1555:
595 case DRM_FORMAT_ABGR2101010:
596 case DRM_FORMAT_ABGR4444:
597 case DRM_FORMAT_ABGR8888:
598 case DRM_FORMAT_ARGB1555:
599 case DRM_FORMAT_ARGB2101010:
600 case DRM_FORMAT_ARGB4444:
601 case DRM_FORMAT_ARGB8888:
602 case DRM_FORMAT_AYUV:
603 case DRM_FORMAT_BGR233:
604 case DRM_FORMAT_BGR565:
605 case DRM_FORMAT_BGR888:
606 case DRM_FORMAT_BGRA1010102:
607 case DRM_FORMAT_BGRA4444:
608 case DRM_FORMAT_BGRA5551:
609 case DRM_FORMAT_BGRA8888:
610 case DRM_FORMAT_BGRX1010102:
611 case DRM_FORMAT_BGRX4444:
612 case DRM_FORMAT_BGRX5551:
613 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800614 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800615 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800616 case DRM_FORMAT_R8:
617 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800618 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800619 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800620 case DRM_FORMAT_RGB888:
621 case DRM_FORMAT_RGBA1010102:
622 case DRM_FORMAT_RGBA4444:
623 case DRM_FORMAT_RGBA5551:
624 case DRM_FORMAT_RGBA8888:
625 case DRM_FORMAT_RGBX1010102:
626 case DRM_FORMAT_RGBX4444:
627 case DRM_FORMAT_RGBX5551:
628 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800629 case DRM_FORMAT_UYVY:
630 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800631 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800632 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800633 case DRM_FORMAT_XBGR4444:
634 case DRM_FORMAT_XBGR8888:
635 case DRM_FORMAT_XRGB1555:
636 case DRM_FORMAT_XRGB2101010:
637 case DRM_FORMAT_XRGB4444:
638 case DRM_FORMAT_XRGB8888:
639 case DRM_FORMAT_YUYV:
640 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700641 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800642 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530643 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700644 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800645 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800646 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700647 return 3;
648 }
649
650 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
651 return 0;
652}
653
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700654uint32_t drv_num_buffers_per_bo(struct bo *bo)
655{
656 uint32_t count = 0;
657 size_t plane, p;
658
659 for (plane = 0; plane < bo->num_planes; plane++) {
660 for (p = 0; p < plane; p++)
661 if (bo->handles[p].u32 == bo->handles[plane].u32)
662 break;
663 if (p == plane)
664 count++;
665 }
666
667 return count;
668}