blob: a1be24532ced0cfa28f1b63cf0be9549b308a2b4 [file] [log] [blame]
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2016 The Chromium OS Authors. All rights reserved.
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07003 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6#include <assert.h>
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -07007#include <errno.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07008#include <fcntl.h>
Gurchetan Singh1647fbe2016-08-03 17:14:55 -07009#include <pthread.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070010#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +080015#include <sys/types.h>
16#include <unistd.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include <xf86drm.h>
18
19#include "drv_priv.h"
20#include "helpers.h"
21#include "util.h"
22
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053023#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070024extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053025#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070026extern const struct backend backend_evdi;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070027#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070028extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070029#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070030extern const struct backend backend_gma500;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070031#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070032extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#endif
34#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
37#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070038extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070039#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070040extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040041#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040043#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070044#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070045extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070046#endif
47#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070048extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010051#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010053#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070054extern const struct backend backend_vgem;
55extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070057static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070058{
59 drmVersionPtr drm_version;
60 unsigned int i;
61
62 drm_version = drmGetVersion(fd);
63
64 if (!drm_version)
65 return NULL;
66
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053068#ifdef DRV_AMDGPU
69 &backend_amdgpu,
70#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070071 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070072#ifdef DRV_EXYNOS
73 &backend_exynos,
74#endif
75 &backend_gma500,
76#ifdef DRV_I915
77 &backend_i915,
78#endif
79#ifdef DRV_MARVELL
80 &backend_marvell,
81#endif
82#ifdef DRV_MEDIATEK
83 &backend_mediatek,
84#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050085 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040086#ifdef DRV_RADEON
87 &backend_radeon,
88#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070089#ifdef DRV_ROCKCHIP
90 &backend_rockchip,
91#endif
92#ifdef DRV_TEGRA
93 &backend_tegra,
94#endif
95 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +010096#ifdef DRV_VC4
97 &backend_vc4,
98#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080099 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 };
101
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800102 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700103 if (!strcmp(drm_version->name, backend_list[i]->name)) {
104 drmFreeVersion(drm_version);
105 return backend_list[i];
106 }
107
108 drmFreeVersion(drm_version);
109 return NULL;
110}
111
112struct driver *drv_create(int fd)
113{
114 struct driver *drv;
115 int ret;
116
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800117 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700118
119 if (!drv)
120 return NULL;
121
122 drv->fd = fd;
123 drv->backend = drv_get_backend(fd);
124
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700125 if (!drv->backend)
126 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700127
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800128 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700129 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700130
131 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700132 if (!drv->buffer_table)
133 goto free_lock;
134
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700135 drv->mappings = drv_array_init(sizeof(struct mapping));
136 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700137 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700138
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800139 /* Start with a power of 2 number of allocations. */
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700140 drv->combos.allocations = 2;
141 drv->combos.size = 0;
142
143 drv->combos.data = calloc(drv->combos.allocations, sizeof(struct combination));
144 if (!drv->combos.data)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700145 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700146
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700147 if (drv->backend->init) {
148 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800149 if (ret) {
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700150 free(drv->combos.data);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700151 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800152 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 }
154
155 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700156
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700157free_mappings:
158 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700159free_buffer_table:
160 drmHashDestroy(drv->buffer_table);
161free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800162 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700163free_driver:
164 free(drv);
165 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700166}
167
168void drv_destroy(struct driver *drv)
169{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800170 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700171
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700172 if (drv->backend->close)
173 drv->backend->close(drv);
174
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700175 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700176 drv_array_destroy(drv->mappings);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700177
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700178 free(drv->combos.data);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700179
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800180 pthread_mutex_unlock(&drv->driver_lock);
181 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700182
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700183 free(drv);
184}
185
186int drv_get_fd(struct driver *drv)
187{
188 return drv->fd;
189}
190
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800191const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192{
193 return drv->backend->name;
194}
195
Gurchetan Singha1892b22017-09-28 16:40:52 -0700196struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800198 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199
Gurchetan Singha1892b22017-09-28 16:40:52 -0700200 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700201 return 0;
202
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800203 best = NULL;
204 uint32_t i;
Ege Mihmanli96b7d462017-09-19 20:13:26 -0700205 for (i = 0; i < drv->combos.size; i++) {
206 curr = &drv->combos.data[i];
Gurchetan Singha1892b22017-09-28 16:40:52 -0700207 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800208 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800209 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700210 }
211
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800212 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700213}
214
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700215struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700216 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217{
218
219 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800220 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221
222 if (!bo)
223 return NULL;
224
225 bo->drv = drv;
226 bo->width = width;
227 bo->height = height;
228 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700229 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230 bo->num_planes = drv_num_planes_from_format(format);
231
232 if (!bo->num_planes) {
233 free(bo);
234 return NULL;
235 }
236
237 return bo;
238}
239
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800240struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700241 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242{
243 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700244 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700245 struct bo *bo;
246
Gurchetan Singha1892b22017-09-28 16:40:52 -0700247 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700248
249 if (!bo)
250 return NULL;
251
Gurchetan Singha1892b22017-09-28 16:40:52 -0700252 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700253
254 if (ret) {
255 free(bo);
256 return NULL;
257 }
258
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800259 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700260
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700261 for (plane = 0; plane < bo->num_planes; plane++) {
262 if (plane > 0)
263 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
264
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700266 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700267
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800268 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700269
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270 return bo;
271}
272
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800273struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
274 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700275{
276 int ret;
277 size_t plane;
278 struct bo *bo;
279
280 if (!drv->backend->bo_create_with_modifiers) {
281 errno = ENOENT;
282 return NULL;
283 }
284
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700285 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700286
287 if (!bo)
288 return NULL;
289
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800290 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700291
292 if (ret) {
293 free(bo);
294 return NULL;
295 }
296
297 pthread_mutex_lock(&drv->driver_lock);
298
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700299 for (plane = 0; plane < bo->num_planes; plane++) {
300 if (plane > 0)
301 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
302
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700303 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700304 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700305
306 pthread_mutex_unlock(&drv->driver_lock);
307
308 return bo;
309}
310
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700311void drv_bo_destroy(struct bo *bo)
312{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700313 size_t plane;
314 uintptr_t total = 0;
315 struct driver *drv = bo->drv;
316
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800317 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700318
319 for (plane = 0; plane < bo->num_planes; plane++)
320 drv_decrement_reference_count(drv, bo, plane);
321
Gurchetan Singhde263f12017-01-24 13:30:06 -0800322 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700323 total += drv_get_reference_count(drv, bo, plane);
324
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800325 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700326
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900327 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700328 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700329 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900330 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700331
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700332 free(bo);
333}
334
335struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
336{
337 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700338 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700339 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700340 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700341
Gurchetan Singha1892b22017-09-28 16:40:52 -0700342 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700343
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700344 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700345 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700346
Gurchetan Singh71611d62017-01-03 16:49:56 -0800347 ret = drv->backend->bo_import(bo, data);
348 if (ret) {
349 free(bo);
350 return NULL;
351 }
352
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700353 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700354 bo->strides[plane] = data->strides[plane];
355 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700356 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700357
358 seek_end = lseek(data->fds[plane], 0, SEEK_END);
359 if (seek_end == (off_t)(-1)) {
360 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
361 goto destroy_bo;
362 }
363
364 lseek(data->fds[plane], 0, SEEK_SET);
365 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
366 bo->sizes[plane] = seek_end - data->offsets[plane];
367 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800368 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700369
370 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
371 fprintf(stderr, "drv: buffer size is too large.\n");
372 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800373 }
374
375 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700376 }
377
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700378 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700379
380destroy_bo:
381 drv_bo_destroy(bo);
382 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700383}
384
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800385void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
386 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700387{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700388 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700389 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700390 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700391
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800392 assert(rect->width >= 0);
393 assert(rect->height >= 0);
394 assert(rect->x + rect->width <= drv_bo_get_width(bo));
395 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700396 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900397 /* No CPU access for protected buffers. */
398 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700399
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700400 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800401 mapping.rect = *rect;
402 mapping.refcount = 1;
403
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800404 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700405
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700406 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
407 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
408 if (prior->vma->handle != bo->handles[plane].u32 ||
409 prior->vma->map_flags != map_flags)
410 continue;
411
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800412 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
413 rect->width != prior->rect.width || rect->height != prior->rect.height)
414 continue;
415
416 prior->refcount++;
417 *map_data = prior;
418 goto exact_match;
419 }
420
421 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
422 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
423 if (prior->vma->handle != bo->handles[plane].u32 ||
424 prior->vma->map_flags != map_flags)
425 continue;
426
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700427 prior->vma->refcount++;
428 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700429 goto success;
430 }
431
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700432 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800433 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700434 if (addr == MAP_FAILED) {
435 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700436 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800437 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700438 return MAP_FAILED;
439 }
440
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700441 mapping.vma->refcount = 1;
442 mapping.vma->addr = addr;
443 mapping.vma->handle = bo->handles[plane].u32;
444 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700445
446success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700447 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800448exact_match:
449 drv_bo_invalidate(bo, *map_data);
450 addr = (uint8_t *)((*map_data)->vma->addr);
451 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800452 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800453 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700454}
455
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700456int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700457{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700458 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700459 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700460 if (ret)
461 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700462
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800463 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700464
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800465 if (--mapping->refcount)
466 goto out;
467
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700468 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800469 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700470 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700471 }
472
473 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
474 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
475 drv_array_remove(bo->drv->mappings, i);
476 break;
477 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700478 }
479
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800480out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800481 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700482 return ret;
483}
484
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700485int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700486{
487 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700488
489 assert(mapping);
490 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800491 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700492 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700493
494 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700495 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700496
497 return ret;
498}
499
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700500int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -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);
Tomasz Figae0807b12017-08-04 12:50:03 +0900508 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700509
510 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700511 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700512
513 return ret;
514}
515
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700516uint32_t drv_bo_get_width(struct bo *bo)
517{
518 return bo->width;
519}
520
521uint32_t drv_bo_get_height(struct bo *bo)
522{
523 return bo->height;
524}
525
526uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
527{
528 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
529}
530
531size_t drv_bo_get_num_planes(struct bo *bo)
532{
533 return bo->num_planes;
534}
535
536union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
537{
538 return bo->handles[plane];
539}
540
541#ifndef DRM_RDWR
542#define DRM_RDWR O_RDWR
543#endif
544
545int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
546{
547
548 int ret, fd;
549 assert(plane < bo->num_planes);
550
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800551 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700552
553 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700554}
555
556uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
557{
558 assert(plane < bo->num_planes);
559 return bo->offsets[plane];
560}
561
562uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
563{
564 assert(plane < bo->num_planes);
565 return bo->sizes[plane];
566}
567
568uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
569{
570 assert(plane < bo->num_planes);
571 return bo->strides[plane];
572}
573
574uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
575{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800576 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700577 return bo->format_modifiers[plane];
578}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700579
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800580uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700581{
582 return bo->format;
583}
584
Gurchetan Singha1892b22017-09-28 16:40:52 -0700585uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700586{
587 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700588 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700589
590 return format;
591}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700592
Gurchetan Singh2a119342016-11-02 10:40:51 -0700593size_t drv_num_planes_from_format(uint32_t format)
594{
595 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800596 case DRM_FORMAT_ABGR1555:
597 case DRM_FORMAT_ABGR2101010:
598 case DRM_FORMAT_ABGR4444:
599 case DRM_FORMAT_ABGR8888:
600 case DRM_FORMAT_ARGB1555:
601 case DRM_FORMAT_ARGB2101010:
602 case DRM_FORMAT_ARGB4444:
603 case DRM_FORMAT_ARGB8888:
604 case DRM_FORMAT_AYUV:
605 case DRM_FORMAT_BGR233:
606 case DRM_FORMAT_BGR565:
607 case DRM_FORMAT_BGR888:
608 case DRM_FORMAT_BGRA1010102:
609 case DRM_FORMAT_BGRA4444:
610 case DRM_FORMAT_BGRA5551:
611 case DRM_FORMAT_BGRA8888:
612 case DRM_FORMAT_BGRX1010102:
613 case DRM_FORMAT_BGRX4444:
614 case DRM_FORMAT_BGRX5551:
615 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800616 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800617 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800618 case DRM_FORMAT_R8:
619 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800620 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800621 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800622 case DRM_FORMAT_RGB888:
623 case DRM_FORMAT_RGBA1010102:
624 case DRM_FORMAT_RGBA4444:
625 case DRM_FORMAT_RGBA5551:
626 case DRM_FORMAT_RGBA8888:
627 case DRM_FORMAT_RGBX1010102:
628 case DRM_FORMAT_RGBX4444:
629 case DRM_FORMAT_RGBX5551:
630 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800631 case DRM_FORMAT_UYVY:
632 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800633 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800634 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800635 case DRM_FORMAT_XBGR4444:
636 case DRM_FORMAT_XBGR8888:
637 case DRM_FORMAT_XRGB1555:
638 case DRM_FORMAT_XRGB2101010:
639 case DRM_FORMAT_XRGB4444:
640 case DRM_FORMAT_XRGB8888:
641 case DRM_FORMAT_YUYV:
642 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700643 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800644 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530645 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700646 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800647 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800648 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700649 return 3;
650 }
651
652 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
653 return 0;
654}
655
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700656uint32_t drv_num_buffers_per_bo(struct bo *bo)
657{
658 uint32_t count = 0;
659 size_t plane, p;
660
661 for (plane = 0; plane < bo->num_planes; plane++) {
662 for (p = 0; p < plane; p++)
663 if (bo->handles[p].u32 == bo->handles[plane].u32)
664 break;
665 if (p == plane)
666 count++;
667 }
668
669 return count;
670}