blob: ef5cbc55c4ebfa24299c6dd08efe81e9556de4fc [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
Kazuhiro Inaba5b91ec02019-09-19 03:18:31 +0000114 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 };
116
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800117 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700118 if (!strcmp(drm_version->name, backend_list[i]->name)) {
119 drmFreeVersion(drm_version);
120 return backend_list[i];
121 }
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,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700226 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
Gurchetan Singha1892b22017-09-28 16:40:52 -0700257 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
Gurchetan Singha1892b22017-09-28 16:40:52 -0700262 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
290 if (!drv->backend->bo_create_with_modifiers) {
291 errno = ENOENT;
292 return NULL;
293 }
294
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700295 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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800300 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{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700323 size_t plane;
324 uintptr_t total = 0;
325 struct driver *drv = bo->drv;
326
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800327 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700328
Gurchetan Singh298b7572019-09-19 09:55:18 -0700329 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700330 drv_decrement_reference_count(drv, bo, plane);
331
Gurchetan Singh298b7572019-09-19 09:55:18 -0700332 for (plane = 0; plane < bo->meta.num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700333 total += drv_get_reference_count(drv, bo, plane);
334
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800335 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700336
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900337 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700338 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700339 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900340 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700341
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700342 free(bo);
343}
344
345struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
346{
347 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700348 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700349 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700350 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700351
Gurchetan Singha1892b22017-09-28 16:40:52 -0700352 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700353
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700354 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700355 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700356
Gurchetan Singh71611d62017-01-03 16:49:56 -0800357 ret = drv->backend->bo_import(bo, data);
358 if (ret) {
359 free(bo);
360 return NULL;
361 }
362
Gurchetan Singh298b7572019-09-19 09:55:18 -0700363 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530364 pthread_mutex_lock(&bo->drv->driver_lock);
365 drv_increment_reference_count(bo->drv, bo, plane);
366 pthread_mutex_unlock(&bo->drv->driver_lock);
367 }
368
Gurchetan Singh298b7572019-09-19 09:55:18 -0700369 for (plane = 0; plane < bo->meta.num_planes; plane++) {
370 bo->meta.strides[plane] = data->strides[plane];
371 bo->meta.offsets[plane] = data->offsets[plane];
372 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700373
374 seek_end = lseek(data->fds[plane], 0, SEEK_END);
375 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700376 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700377 goto destroy_bo;
378 }
379
380 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700381 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
382 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700383 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700384 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700385
Gurchetan Singh298b7572019-09-19 09:55:18 -0700386 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700387 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700388 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800389 }
390
Gurchetan Singh298b7572019-09-19 09:55:18 -0700391 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700392 }
393
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700394 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700395
396destroy_bo:
397 drv_bo_destroy(bo);
398 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700399}
400
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800401void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
402 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700403{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700404 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700405 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700406 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700407
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800408 assert(rect->width >= 0);
409 assert(rect->height >= 0);
410 assert(rect->x + rect->width <= drv_bo_get_width(bo));
411 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700412 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900413 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700414 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700415
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700416 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800417 mapping.rect = *rect;
418 mapping.refcount = 1;
419
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800420 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700421
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700422 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
423 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
424 if (prior->vma->handle != bo->handles[plane].u32 ||
425 prior->vma->map_flags != map_flags)
426 continue;
427
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800428 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
429 rect->width != prior->rect.width || rect->height != prior->rect.height)
430 continue;
431
432 prior->refcount++;
433 *map_data = prior;
434 goto exact_match;
435 }
436
437 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
438 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
439 if (prior->vma->handle != bo->handles[plane].u32 ||
440 prior->vma->map_flags != map_flags)
441 continue;
442
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700443 prior->vma->refcount++;
444 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700445 goto success;
446 }
447
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700448 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700449 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800450 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700451 if (addr == MAP_FAILED) {
452 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700453 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800454 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700455 return MAP_FAILED;
456 }
457
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700458 mapping.vma->refcount = 1;
459 mapping.vma->addr = addr;
460 mapping.vma->handle = bo->handles[plane].u32;
461 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700462
463success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700464 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800465exact_match:
466 drv_bo_invalidate(bo, *map_data);
467 addr = (uint8_t *)((*map_data)->vma->addr);
468 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800469 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800470 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700471}
472
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700473int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700474{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700475 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700476 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700477
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800478 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700479
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800480 if (--mapping->refcount)
481 goto out;
482
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700483 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800484 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700485 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700486 }
487
488 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
489 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
490 drv_array_remove(bo->drv->mappings, i);
491 break;
492 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700493 }
494
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800495out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800496 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700497 return ret;
498}
499
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700500int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700501{
502 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700503
504 assert(mapping);
505 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800506 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700507 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700508
509 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700510 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700511
512 return ret;
513}
514
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700515int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700516{
517 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700518
519 assert(mapping);
520 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800521 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700522 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700523 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700524
525 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700526 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700527 else
528 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700529
530 return ret;
531}
532
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700533uint32_t drv_bo_get_width(struct bo *bo)
534{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700535 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700536}
537
538uint32_t drv_bo_get_height(struct bo *bo)
539{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700540 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700541}
542
543uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
544{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700545 return bo->meta.tiling ? bo->meta.tiling : drv_bo_get_plane_stride(bo, 0);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700546}
547
548size_t drv_bo_get_num_planes(struct bo *bo)
549{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700550 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700551}
552
553union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
554{
555 return bo->handles[plane];
556}
557
558#ifndef DRM_RDWR
559#define DRM_RDWR O_RDWR
560#endif
561
562int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
563{
564
565 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700566 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700567
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800568 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700569
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700570 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
571 if (ret)
572 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
573
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700574 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700575}
576
577uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
578{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700579 assert(plane < bo->meta.num_planes);
580 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700581}
582
583uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
584{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700585 assert(plane < bo->meta.num_planes);
586 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700587}
588
589uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
590{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700591 assert(plane < bo->meta.num_planes);
592 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700593}
594
595uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
596{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700597 assert(plane < bo->meta.num_planes);
598 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700599}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700600
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800601uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700602{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700603 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700604}
605
Gurchetan Singha1892b22017-09-28 16:40:52 -0700606uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700607{
608 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700609 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700610
611 return format;
612}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700613
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700614uint32_t drv_num_buffers_per_bo(struct bo *bo)
615{
616 uint32_t count = 0;
617 size_t plane, p;
618
Gurchetan Singh298b7572019-09-19 09:55:18 -0700619 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700620 for (p = 0; p < plane; p++)
621 if (bo->handles[p].u32 == bo->handles[plane].u32)
622 break;
623 if (p == plane)
624 count++;
625 }
626
627 return count;
628}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700629
630void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
631{
632 char buf[50];
633 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
634
635 va_list args;
636 va_start(args, format);
637#ifdef __ANDROID__
638 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
639#else
640 fprintf(stderr, "%s ", buf);
641 vfprintf(stderr, format, args);
642#endif
643 va_end(args);
644}