blob: 920cf4db4e715d40adec38ba3fd0800435f3dfb1 [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
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070019#ifdef __ANDROID__
20#include <cutils/log.h>
21#include <libgen.h>
22#endif
23
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070024#include "drv_priv.h"
25#include "helpers.h"
26#include "util.h"
27
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053028#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070029extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053030#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070031extern const struct backend backend_evdi;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070032#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070033extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070034#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070035#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070036extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070037#endif
38#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070039extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070040#endif
41#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070043#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080044#ifdef DRV_MESON
45extern const struct backend backend_meson;
46#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053047#ifdef DRV_MSM
48extern const struct backend backend_msm;
49#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040051#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040053#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070054#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070055extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056#endif
Leona Chou8f708a12020-03-19 15:06:12 +080057#ifdef DRV_SYNAPTICS
58extern const struct backend backend_synaptics;
59#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070060#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070061extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070062#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070063extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010064#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010066#endif
Kazuhiro Inaba5b91ec02019-09-19 03:18:31 +000067extern const struct backend backend_vgem;
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070068extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070069
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070070static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070071{
72 drmVersionPtr drm_version;
73 unsigned int i;
74
75 drm_version = drmGetVersion(fd);
76
77 if (!drm_version)
78 return NULL;
79
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070080 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053081#ifdef DRV_AMDGPU
82 &backend_amdgpu,
83#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070084 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070085#ifdef DRV_EXYNOS
86 &backend_exynos,
87#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070088#ifdef DRV_I915
89 &backend_i915,
90#endif
91#ifdef DRV_MARVELL
92 &backend_marvell,
93#endif
94#ifdef DRV_MEDIATEK
95 &backend_mediatek,
96#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080097#ifdef DRV_MESON
98 &backend_meson,
99#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +0530100#ifdef DRV_MSM
101 &backend_msm,
102#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -0500103 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -0400104#ifdef DRV_RADEON
105 &backend_radeon,
106#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700107#ifdef DRV_ROCKCHIP
108 &backend_rockchip,
109#endif
Leona Chou8f708a12020-03-19 15:06:12 +0800110#ifdef DRV_SYNAPTICS
111 &backend_synaptics,
112#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700113#ifdef DRV_TEGRA
114 &backend_tegra,
115#endif
116 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100117#ifdef DRV_VC4
118 &backend_vc4,
119#endif
Gurchetan Singh8d884742020-03-24 13:48:54 -0700120 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700121 };
122
David Stevens26fe6822020-03-09 12:23:42 +0000123 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
124 const struct backend *b = backend_list[i];
125 // Exactly one of the main create functions must be defined.
126 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
127 // Either both or neither must be implemented.
128 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
129 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
130 assert((b->bo_create_with_modifiers == NULL) ||
131 (b->bo_create_from_metadata == NULL));
132
133 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700134 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000135 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700136 }
David Stevens26fe6822020-03-09 12:23:42 +0000137 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700138
139 drmFreeVersion(drm_version);
140 return NULL;
141}
142
143struct driver *drv_create(int fd)
144{
145 struct driver *drv;
146 int ret;
147
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800148 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700149
150 if (!drv)
151 return NULL;
152
153 drv->fd = fd;
154 drv->backend = drv_get_backend(fd);
155
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700156 if (!drv->backend)
157 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700158
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800159 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700160 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700161
162 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700163 if (!drv->buffer_table)
164 goto free_lock;
165
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700166 drv->mappings = drv_array_init(sizeof(struct mapping));
167 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700168 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700169
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700170 drv->combos = drv_array_init(sizeof(struct combination));
171 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700172 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700173
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 if (drv->backend->init) {
175 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800176 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700177 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700178 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800179 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700180 }
181
182 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700183
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700184free_mappings:
185 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700186free_buffer_table:
187 drmHashDestroy(drv->buffer_table);
188free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800189 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700190free_driver:
191 free(drv);
192 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193}
194
195void drv_destroy(struct driver *drv)
196{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800197 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700198
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199 if (drv->backend->close)
200 drv->backend->close(drv);
201
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700202 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700203 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700204 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700205
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800206 pthread_mutex_unlock(&drv->driver_lock);
207 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700208
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209 free(drv);
210}
211
212int drv_get_fd(struct driver *drv)
213{
214 return drv->fd;
215}
216
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800217const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218{
219 return drv->backend->name;
220}
221
Gurchetan Singha1892b22017-09-28 16:40:52 -0700222struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700223{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800224 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700225
Gurchetan Singha1892b22017-09-28 16:40:52 -0700226 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227 return 0;
228
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800229 best = NULL;
230 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700231 for (i = 0; i < drv_array_size(drv->combos); i++) {
232 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700233 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800234 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800235 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700236 }
237
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800238 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700239}
240
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700241struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000242 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243{
244
245 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800246 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247
248 if (!bo)
249 return NULL;
250
251 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700252 bo->meta.width = width;
253 bo->meta.height = height;
254 bo->meta.format = format;
255 bo->meta.use_flags = use_flags;
256 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000257 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700258
Gurchetan Singh298b7572019-09-19 09:55:18 -0700259 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700260 free(bo);
261 return NULL;
262 }
263
264 return bo;
265}
266
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800267struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700268 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700269{
270 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700271 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700272 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000273 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700274
David Stevens26fe6822020-03-09 12:23:42 +0000275 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
276 use_flags &= ~BO_USE_TEST_ALLOC;
277
278 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700279
280 if (!bo)
281 return NULL;
282
David Stevens26fe6822020-03-09 12:23:42 +0000283 ret = -EINVAL;
284 if (drv->backend->bo_compute_metadata) {
285 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
286 0);
287 if (!is_test_alloc && ret == 0)
288 ret = drv->backend->bo_create_from_metadata(bo);
289 } else if (!is_test_alloc) {
290 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
291 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700292
293 if (ret) {
294 free(bo);
295 return NULL;
296 }
297
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800298 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700299
Gurchetan Singh298b7572019-09-19 09:55:18 -0700300 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700301 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700302 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700303
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700304 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700305 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700306
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800307 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700308
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700309 return bo;
310}
311
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800312struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
313 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700314{
315 int ret;
316 size_t plane;
317 struct bo *bo;
318
David Stevens26fe6822020-03-09 12:23:42 +0000319 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700320 errno = ENOENT;
321 return NULL;
322 }
323
David Stevens26fe6822020-03-09 12:23:42 +0000324 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700325
326 if (!bo)
327 return NULL;
328
David Stevens26fe6822020-03-09 12:23:42 +0000329 ret = -EINVAL;
330 if (drv->backend->bo_compute_metadata) {
331 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
332 modifiers, count);
333 if (ret == 0)
334 ret = drv->backend->bo_create_from_metadata(bo);
335 } else {
336 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
337 count);
338 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700339
340 if (ret) {
341 free(bo);
342 return NULL;
343 }
344
345 pthread_mutex_lock(&drv->driver_lock);
346
Gurchetan Singh298b7572019-09-19 09:55:18 -0700347 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700348 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700349 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700350
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700351 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700352 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700353
354 pthread_mutex_unlock(&drv->driver_lock);
355
356 return bo;
357}
358
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700359void drv_bo_destroy(struct bo *bo)
360{
Jason Macnak0907d822019-11-12 10:53:05 -0800361 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700362 size_t plane;
363 uintptr_t total = 0;
364 struct driver *drv = bo->drv;
365
David Stevens26fe6822020-03-09 12:23:42 +0000366 if (!bo->is_test_buffer) {
367 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700368
David Stevens26fe6822020-03-09 12:23:42 +0000369 for (plane = 0; plane < bo->meta.num_planes; plane++)
370 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700371
David Stevens26fe6822020-03-09 12:23:42 +0000372 for (plane = 0; plane < bo->meta.num_planes; plane++)
373 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700374
David Stevens26fe6822020-03-09 12:23:42 +0000375 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700376
David Stevens26fe6822020-03-09 12:23:42 +0000377 if (total == 0) {
378 ret = drv_mapping_destroy(bo);
379 assert(ret == 0);
380 bo->drv->backend->bo_destroy(bo);
381 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900382 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700383
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700384 free(bo);
385}
386
387struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
388{
389 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700390 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700391 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700392 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700393
David Stevens26fe6822020-03-09 12:23:42 +0000394 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700395
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700396 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700397 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700398
Gurchetan Singh71611d62017-01-03 16:49:56 -0800399 ret = drv->backend->bo_import(bo, data);
400 if (ret) {
401 free(bo);
402 return NULL;
403 }
404
Gurchetan Singh298b7572019-09-19 09:55:18 -0700405 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530406 pthread_mutex_lock(&bo->drv->driver_lock);
407 drv_increment_reference_count(bo->drv, bo, plane);
408 pthread_mutex_unlock(&bo->drv->driver_lock);
409 }
410
Gurchetan Singh298b7572019-09-19 09:55:18 -0700411 for (plane = 0; plane < bo->meta.num_planes; plane++) {
412 bo->meta.strides[plane] = data->strides[plane];
413 bo->meta.offsets[plane] = data->offsets[plane];
414 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700415
416 seek_end = lseek(data->fds[plane], 0, SEEK_END);
417 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700418 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700419 goto destroy_bo;
420 }
421
422 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700423 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
424 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700425 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700426 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700427
Gurchetan Singh298b7572019-09-19 09:55:18 -0700428 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700429 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700430 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800431 }
432
Gurchetan Singh298b7572019-09-19 09:55:18 -0700433 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700434 }
435
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700436 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700437
438destroy_bo:
439 drv_bo_destroy(bo);
440 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700441}
442
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800443void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
444 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700445{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700446 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700447 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700448 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700449
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800450 assert(rect->width >= 0);
451 assert(rect->height >= 0);
452 assert(rect->x + rect->width <= drv_bo_get_width(bo));
453 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700454 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900455 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700456 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700457
David Stevens26fe6822020-03-09 12:23:42 +0000458 if (bo->is_test_buffer) {
459 return MAP_FAILED;
460 }
461
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700462 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800463 mapping.rect = *rect;
464 mapping.refcount = 1;
465
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800466 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700467
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700468 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
469 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
470 if (prior->vma->handle != bo->handles[plane].u32 ||
471 prior->vma->map_flags != map_flags)
472 continue;
473
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800474 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
475 rect->width != prior->rect.width || rect->height != prior->rect.height)
476 continue;
477
478 prior->refcount++;
479 *map_data = prior;
480 goto exact_match;
481 }
482
483 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
484 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
485 if (prior->vma->handle != bo->handles[plane].u32 ||
486 prior->vma->map_flags != map_flags)
487 continue;
488
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700489 prior->vma->refcount++;
490 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700491 goto success;
492 }
493
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700494 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700495 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800496 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700497 if (addr == MAP_FAILED) {
498 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700499 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800500 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700501 return MAP_FAILED;
502 }
503
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700504 mapping.vma->refcount = 1;
505 mapping.vma->addr = addr;
506 mapping.vma->handle = bo->handles[plane].u32;
507 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700508
509success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700510 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800511exact_match:
512 drv_bo_invalidate(bo, *map_data);
513 addr = (uint8_t *)((*map_data)->vma->addr);
514 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800515 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800516 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700517}
518
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700519int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700520{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700521 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700522 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700523
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800524 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700525
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800526 if (--mapping->refcount)
527 goto out;
528
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700529 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800530 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700531 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700532 }
533
534 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
535 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
536 drv_array_remove(bo->drv->mappings, i);
537 break;
538 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700539 }
540
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800541out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800542 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700543 return ret;
544}
545
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700546int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700547{
548 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700549
550 assert(mapping);
551 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800552 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700553 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700554
555 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700556 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700557
558 return ret;
559}
560
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700561int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700562{
563 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700564
565 assert(mapping);
566 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800567 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700568 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700569 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700570
571 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700572 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700573 else
574 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700575
576 return ret;
577}
578
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700579uint32_t drv_bo_get_width(struct bo *bo)
580{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700581 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700582}
583
584uint32_t drv_bo_get_height(struct bo *bo)
585{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700586 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700587}
588
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700589size_t drv_bo_get_num_planes(struct bo *bo)
590{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700591 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700592}
593
594union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
595{
596 return bo->handles[plane];
597}
598
599#ifndef DRM_RDWR
600#define DRM_RDWR O_RDWR
601#endif
602
603int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
604{
605
606 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700607 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700608
David Stevens26fe6822020-03-09 12:23:42 +0000609 if (bo->is_test_buffer) {
610 return -EINVAL;
611 }
612
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800613 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700614
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700615 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
616 if (ret)
617 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
618
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700619 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700620}
621
622uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
623{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700624 assert(plane < bo->meta.num_planes);
625 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700626}
627
628uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
629{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700630 assert(plane < bo->meta.num_planes);
631 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700632}
633
634uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
635{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700636 assert(plane < bo->meta.num_planes);
637 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700638}
639
640uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
641{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700642 assert(plane < bo->meta.num_planes);
643 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700644}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700645
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800646uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700647{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700648 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700649}
650
Gurchetan Singha1892b22017-09-28 16:40:52 -0700651uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700652{
653 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700654 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700655
656 return format;
657}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700658
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700659uint32_t drv_num_buffers_per_bo(struct bo *bo)
660{
661 uint32_t count = 0;
662 size_t plane, p;
663
David Stevens26fe6822020-03-09 12:23:42 +0000664 if (bo->is_test_buffer) {
665 return 0;
666 }
667
Gurchetan Singh298b7572019-09-19 09:55:18 -0700668 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700669 for (p = 0; p < plane; p++)
670 if (bo->handles[p].u32 == bo->handles[plane].u32)
671 break;
672 if (p == plane)
673 count++;
674 }
675
676 return count;
677}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700678
679void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
680{
681 char buf[50];
682 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
683
684 va_list args;
685 va_start(args, format);
686#ifdef __ANDROID__
687 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
688#else
689 fprintf(stderr, "%s ", buf);
690 vfprintf(stderr, format, args);
691#endif
692 va_end(args);
693}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700694
695int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
696 uint32_t offsets[DRV_MAX_PLANES])
697{
698 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
699 strides[plane] = bo->meta.strides[plane];
700 offsets[plane] = bo->meta.offsets[plane];
701 }
702
703 if (bo->drv->backend->resource_info)
704 return bo->drv->backend->resource_info(bo, strides, offsets);
705
706 return 0;
707}