blob: dcca838bac74d7638b564dd9b1785bf3ccc932f9 [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
57#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070058extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070059#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070060extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010061#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070062extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010063#endif
Kazuhiro Inaba5b91ec02019-09-19 03:18:31 +000064extern const struct backend backend_vgem;
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070066
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070068{
69 drmVersionPtr drm_version;
70 unsigned int i;
71
72 drm_version = drmGetVersion(fd);
73
74 if (!drm_version)
75 return NULL;
76
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070077 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053078#ifdef DRV_AMDGPU
79 &backend_amdgpu,
80#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070081 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_EXYNOS
83 &backend_exynos,
84#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070085#ifdef DRV_I915
86 &backend_i915,
87#endif
88#ifdef DRV_MARVELL
89 &backend_marvell,
90#endif
91#ifdef DRV_MEDIATEK
92 &backend_mediatek,
93#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080094#ifdef DRV_MESON
95 &backend_meson,
96#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053097#ifdef DRV_MSM
98 &backend_msm,
99#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -0500100 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -0400101#ifdef DRV_RADEON
102 &backend_radeon,
103#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104#ifdef DRV_ROCKCHIP
105 &backend_rockchip,
106#endif
107#ifdef DRV_TEGRA
108 &backend_tegra,
109#endif
110 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100111#ifdef DRV_VC4
112 &backend_vc4,
113#endif
Junichi Uekawad441f4e2020-01-29 15:45:16 +0900114 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 };
116
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000117 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
118 if (!strcmp(drm_version->name, backend_list[i]->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700119 drmFreeVersion(drm_version);
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000120 return backend_list[i];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700121 }
122
123 drmFreeVersion(drm_version);
124 return NULL;
125}
126
127struct driver *drv_create(int fd)
128{
129 struct driver *drv;
130 int ret;
131
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800132 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700133
134 if (!drv)
135 return NULL;
136
137 drv->fd = fd;
138 drv->backend = drv_get_backend(fd);
139
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700140 if (!drv->backend)
141 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700142
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800143 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700144 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700145
146 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700147 if (!drv->buffer_table)
148 goto free_lock;
149
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700150 drv->mappings = drv_array_init(sizeof(struct mapping));
151 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700152 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700153
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700154 drv->combos = drv_array_init(sizeof(struct combination));
155 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700156 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700157
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700158 if (drv->backend->init) {
159 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800160 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700161 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700162 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800163 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700164 }
165
166 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700167
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700168free_mappings:
169 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700170free_buffer_table:
171 drmHashDestroy(drv->buffer_table);
172free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800173 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700174free_driver:
175 free(drv);
176 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700177}
178
179void drv_destroy(struct driver *drv)
180{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800181 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700182
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700183 if (drv->backend->close)
184 drv->backend->close(drv);
185
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700186 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700187 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700188 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700189
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800190 pthread_mutex_unlock(&drv->driver_lock);
191 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700192
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193 free(drv);
194}
195
196int drv_get_fd(struct driver *drv)
197{
198 return drv->fd;
199}
200
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800201const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202{
203 return drv->backend->name;
204}
205
Gurchetan Singha1892b22017-09-28 16:40:52 -0700206struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700207{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800208 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209
Gurchetan Singha1892b22017-09-28 16:40:52 -0700210 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211 return 0;
212
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800213 best = NULL;
214 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700215 for (i = 0; i < drv_array_size(drv->combos); i++) {
216 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700217 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800218 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800219 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700220 }
221
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800222 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700223}
224
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700225struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000226 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700227{
228
229 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800230 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700231
232 if (!bo)
233 return NULL;
234
235 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700236 bo->meta.width = width;
237 bo->meta.height = height;
238 bo->meta.format = format;
239 bo->meta.use_flags = use_flags;
240 bo->meta.num_planes = drv_num_planes_from_format(format);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241
Gurchetan Singh298b7572019-09-19 09:55:18 -0700242 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243 free(bo);
244 return NULL;
245 }
246
247 return bo;
248}
249
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800250struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700251 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252{
253 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700254 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700255 struct bo *bo;
256
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000257 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700258
259 if (!bo)
260 return NULL;
261
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000262 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700263
264 if (ret) {
265 free(bo);
266 return NULL;
267 }
268
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800269 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700270
Gurchetan Singh298b7572019-09-19 09:55:18 -0700271 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700272 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700273 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700274
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700275 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700276 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700277
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800278 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700279
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700280 return bo;
281}
282
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800283struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
284 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700285{
286 int ret;
287 size_t plane;
288 struct bo *bo;
289
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000290 if (!drv->backend->bo_create_with_modifiers) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700291 errno = ENOENT;
292 return NULL;
293 }
294
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000295 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700296
297 if (!bo)
298 return NULL;
299
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000300 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700301
302 if (ret) {
303 free(bo);
304 return NULL;
305 }
306
307 pthread_mutex_lock(&drv->driver_lock);
308
Gurchetan Singh298b7572019-09-19 09:55:18 -0700309 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700310 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700311 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700312
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700313 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700314 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700315
316 pthread_mutex_unlock(&drv->driver_lock);
317
318 return bo;
319}
320
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700321void drv_bo_destroy(struct bo *bo)
322{
Jason Macnak0907d822019-11-12 10:53:05 -0800323 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700324 size_t plane;
325 uintptr_t total = 0;
326 struct driver *drv = bo->drv;
327
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000328 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700329
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000330 for (plane = 0; plane < bo->meta.num_planes; plane++)
331 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700332
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000333 for (plane = 0; plane < bo->meta.num_planes; plane++)
334 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700335
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000336 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700337
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000338 if (total == 0) {
339 ret = drv_mapping_destroy(bo);
340 assert(ret == 0);
341 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900342 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700343
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344 free(bo);
345}
346
347struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
348{
349 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700350 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700351 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700352 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700353
Ilja H. Friedel08d8dbf2020-03-08 04:48:13 +0000354 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700355
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700356 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700357 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700358
Gurchetan Singh71611d62017-01-03 16:49:56 -0800359 ret = drv->backend->bo_import(bo, data);
360 if (ret) {
361 free(bo);
362 return NULL;
363 }
364
Gurchetan Singh298b7572019-09-19 09:55:18 -0700365 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530366 pthread_mutex_lock(&bo->drv->driver_lock);
367 drv_increment_reference_count(bo->drv, bo, plane);
368 pthread_mutex_unlock(&bo->drv->driver_lock);
369 }
370
Gurchetan Singh298b7572019-09-19 09:55:18 -0700371 for (plane = 0; plane < bo->meta.num_planes; plane++) {
372 bo->meta.strides[plane] = data->strides[plane];
373 bo->meta.offsets[plane] = data->offsets[plane];
374 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700375
376 seek_end = lseek(data->fds[plane], 0, SEEK_END);
377 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700378 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700379 goto destroy_bo;
380 }
381
382 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700383 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
384 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700385 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700386 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700387
Gurchetan Singh298b7572019-09-19 09:55:18 -0700388 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700389 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700390 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800391 }
392
Gurchetan Singh298b7572019-09-19 09:55:18 -0700393 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700394 }
395
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700396 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700397
398destroy_bo:
399 drv_bo_destroy(bo);
400 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700401}
402
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800403void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
404 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700405{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700406 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700407 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700408 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700409
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800410 assert(rect->width >= 0);
411 assert(rect->height >= 0);
412 assert(rect->x + rect->width <= drv_bo_get_width(bo));
413 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700414 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900415 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700416 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700417
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700418 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800419 mapping.rect = *rect;
420 mapping.refcount = 1;
421
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800422 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700423
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700424 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
425 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
426 if (prior->vma->handle != bo->handles[plane].u32 ||
427 prior->vma->map_flags != map_flags)
428 continue;
429
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800430 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
431 rect->width != prior->rect.width || rect->height != prior->rect.height)
432 continue;
433
434 prior->refcount++;
435 *map_data = prior;
436 goto exact_match;
437 }
438
439 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
440 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
441 if (prior->vma->handle != bo->handles[plane].u32 ||
442 prior->vma->map_flags != map_flags)
443 continue;
444
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700445 prior->vma->refcount++;
446 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700447 goto success;
448 }
449
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700450 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700451 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800452 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700453 if (addr == MAP_FAILED) {
454 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700455 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800456 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700457 return MAP_FAILED;
458 }
459
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700460 mapping.vma->refcount = 1;
461 mapping.vma->addr = addr;
462 mapping.vma->handle = bo->handles[plane].u32;
463 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700464
465success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700466 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800467exact_match:
468 drv_bo_invalidate(bo, *map_data);
469 addr = (uint8_t *)((*map_data)->vma->addr);
470 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800471 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800472 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473}
474
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700475int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700476{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700477 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700478 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700479
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800480 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700481
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800482 if (--mapping->refcount)
483 goto out;
484
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700485 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800486 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700487 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700488 }
489
490 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
491 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
492 drv_array_remove(bo->drv->mappings, i);
493 break;
494 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495 }
496
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800497out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800498 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700499 return ret;
500}
501
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700502int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700503{
504 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700505
506 assert(mapping);
507 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800508 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700509 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700510
511 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700513
514 return ret;
515}
516
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700517int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700518{
519 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700520
521 assert(mapping);
522 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800523 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700524 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700525 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700526
527 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700528 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700529 else
530 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700531
532 return ret;
533}
534
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700535uint32_t drv_bo_get_width(struct bo *bo)
536{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700537 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700538}
539
540uint32_t drv_bo_get_height(struct bo *bo)
541{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700542 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700543}
544
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700545size_t drv_bo_get_num_planes(struct bo *bo)
546{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700547 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700548}
549
550union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
551{
552 return bo->handles[plane];
553}
554
555#ifndef DRM_RDWR
556#define DRM_RDWR O_RDWR
557#endif
558
559int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
560{
561
562 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700563 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700564
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800565 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700566
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700567 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
568 if (ret)
569 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
570
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700571 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700572}
573
574uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
575{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700576 assert(plane < bo->meta.num_planes);
577 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700578}
579
580uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
581{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700582 assert(plane < bo->meta.num_planes);
583 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700584}
585
586uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
587{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700588 assert(plane < bo->meta.num_planes);
589 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700590}
591
592uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
593{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700594 assert(plane < bo->meta.num_planes);
595 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700596}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700597
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800598uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700599{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700600 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700601}
602
Gurchetan Singha1892b22017-09-28 16:40:52 -0700603uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700604{
605 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700606 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700607
608 return format;
609}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700610
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700611uint32_t drv_num_buffers_per_bo(struct bo *bo)
612{
613 uint32_t count = 0;
614 size_t plane, p;
615
Gurchetan Singh298b7572019-09-19 09:55:18 -0700616 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700617 for (p = 0; p < plane; p++)
618 if (bo->handles[p].u32 == bo->handles[plane].u32)
619 break;
620 if (p == plane)
621 count++;
622 }
623
624 return count;
625}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700626
627void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
628{
629 char buf[50];
630 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
631
632 va_list args;
633 va_start(args, format);
634#ifdef __ANDROID__
635 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
636#else
637 fprintf(stderr, "%s ", buf);
638 vfprintf(stderr, format, args);
639#endif
640 va_end(args);
641}