blob: bc1f782d009093cfba01b32e0433128803514c24 [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
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070064extern const struct backend backend_vgem;
65extern 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
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800114 &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;
236 bo->width = width;
237 bo->height = height;
238 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700239 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700240 bo->num_planes = drv_num_planes_from_format(format);
241
242 if (!bo->num_planes) {
243 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 Singhc26fd1e2017-09-29 10:18:59 -0700271 for (plane = 0; plane < bo->num_planes; plane++) {
272 if (plane > 0)
273 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
274
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 Singhc26fd1e2017-09-29 10:18:59 -0700309 for (plane = 0; plane < bo->num_planes; plane++) {
310 if (plane > 0)
311 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
312
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
329 for (plane = 0; plane < bo->num_planes; plane++)
330 drv_decrement_reference_count(drv, bo, plane);
331
Gurchetan Singhde263f12017-01-24 13:30:06 -0800332 for (plane = 0; plane < bo->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 Singhb72badb2016-08-19 16:26:46 -0700363 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700364 bo->strides[plane] = data->strides[plane];
365 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700366 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700367
368 seek_end = lseek(data->fds[plane], 0, SEEK_END);
369 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700370 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700371 goto destroy_bo;
372 }
373
374 lseek(data->fds[plane], 0, SEEK_SET);
375 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
376 bo->sizes[plane] = seek_end - data->offsets[plane];
377 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800378 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700379
380 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700381 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700382 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800383 }
384
385 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700386 }
387
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700388 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700389
390destroy_bo:
391 drv_bo_destroy(bo);
392 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700393}
394
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800395void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
396 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700397{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700398 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700399 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700400 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700401
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800402 assert(rect->width >= 0);
403 assert(rect->height >= 0);
404 assert(rect->x + rect->width <= drv_bo_get_width(bo));
405 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700406 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900407 /* No CPU access for protected buffers. */
408 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700409
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700410 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800411 mapping.rect = *rect;
412 mapping.refcount = 1;
413
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800414 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700415
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700416 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
417 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
418 if (prior->vma->handle != bo->handles[plane].u32 ||
419 prior->vma->map_flags != map_flags)
420 continue;
421
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800422 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
423 rect->width != prior->rect.width || rect->height != prior->rect.height)
424 continue;
425
426 prior->refcount++;
427 *map_data = prior;
428 goto exact_match;
429 }
430
431 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
432 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
433 if (prior->vma->handle != bo->handles[plane].u32 ||
434 prior->vma->map_flags != map_flags)
435 continue;
436
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700437 prior->vma->refcount++;
438 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439 goto success;
440 }
441
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700442 mapping.vma = calloc(1, sizeof(*mapping.vma));
Satyajit Sahu77b70552018-05-03 16:35:24 +0530443 memcpy(mapping.vma->map_strides, bo->strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800444 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700445 if (addr == MAP_FAILED) {
446 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700447 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800448 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700449 return MAP_FAILED;
450 }
451
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700452 mapping.vma->refcount = 1;
453 mapping.vma->addr = addr;
454 mapping.vma->handle = bo->handles[plane].u32;
455 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700456
457success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700458 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800459exact_match:
460 drv_bo_invalidate(bo, *map_data);
461 addr = (uint8_t *)((*map_data)->vma->addr);
462 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800463 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800464 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700465}
466
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700467int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700468{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700469 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700470 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700471
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800472 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800474 if (--mapping->refcount)
475 goto out;
476
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700477 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800478 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700479 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700480 }
481
482 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
483 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
484 drv_array_remove(bo->drv->mappings, i);
485 break;
486 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700487 }
488
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800489out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800490 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700491 return ret;
492}
493
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700494int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700495{
496 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700497
498 assert(mapping);
499 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800500 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700502
503 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700504 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700505
506 return ret;
507}
508
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700509int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700510{
511 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512
513 assert(mapping);
514 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800515 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700516 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900517 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700518
519 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700520 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700521 else
522 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700523
524 return ret;
525}
526
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700527uint32_t drv_bo_get_width(struct bo *bo)
528{
529 return bo->width;
530}
531
532uint32_t drv_bo_get_height(struct bo *bo)
533{
534 return bo->height;
535}
536
537uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
538{
539 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
540}
541
542size_t drv_bo_get_num_planes(struct bo *bo)
543{
544 return bo->num_planes;
545}
546
547union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
548{
549 return bo->handles[plane];
550}
551
552#ifndef DRM_RDWR
553#define DRM_RDWR O_RDWR
554#endif
555
556int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
557{
558
559 int ret, fd;
560 assert(plane < bo->num_planes);
561
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800562 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700563
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700564 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
565 if (ret)
566 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
567
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700568 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700569}
570
571uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
572{
573 assert(plane < bo->num_planes);
574 return bo->offsets[plane];
575}
576
577uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
578{
579 assert(plane < bo->num_planes);
580 return bo->sizes[plane];
581}
582
583uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
584{
585 assert(plane < bo->num_planes);
586 return bo->strides[plane];
587}
588
589uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
590{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800591 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700592 return bo->format_modifiers[plane];
593}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700594
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800595uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700596{
597 return bo->format;
598}
599
Gurchetan Singha1892b22017-09-28 16:40:52 -0700600uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700601{
602 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700603 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700604
605 return format;
606}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700607
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700608uint32_t drv_num_buffers_per_bo(struct bo *bo)
609{
610 uint32_t count = 0;
611 size_t plane, p;
612
613 for (plane = 0; plane < bo->num_planes; plane++) {
614 for (p = 0; p < plane; p++)
615 if (bo->handles[p].u32 == bo->handles[plane].u32)
616 break;
617 if (p == plane)
618 count++;
619 }
620
621 return count;
622}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700623
624void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
625{
626 char buf[50];
627 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
628
629 va_list args;
630 va_start(args, format);
631#ifdef __ANDROID__
632 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
633#else
634 fprintf(stderr, "%s ", buf);
635 vfprintf(stderr, format, args);
636#endif
637 va_end(args);
638}