blob: d5484482102c59edeca088d56abd42ca4d86a7b8 [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 Singh46faf6b2016-08-05 14:40:07 -070030#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070031extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070032#endif
33#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070034extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070035#endif
36#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070037extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070038#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080039#ifdef DRV_MESON
40extern const struct backend backend_meson;
41#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053042#ifdef DRV_MSM
43extern const struct backend backend_msm;
44#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070045extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040046#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040048#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070049#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070051#endif
52#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070053extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070054#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070055extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010056#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070057extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010058#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070059extern const struct backend backend_vgem;
60extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070061
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070062static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063{
64 drmVersionPtr drm_version;
65 unsigned int i;
66
67 drm_version = drmGetVersion(fd);
68
69 if (!drm_version)
70 return NULL;
71
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070072 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053073#ifdef DRV_AMDGPU
74 &backend_amdgpu,
75#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070076 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070077#ifdef DRV_EXYNOS
78 &backend_exynos,
79#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070080#ifdef DRV_I915
81 &backend_i915,
82#endif
83#ifdef DRV_MARVELL
84 &backend_marvell,
85#endif
86#ifdef DRV_MEDIATEK
87 &backend_mediatek,
88#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080089#ifdef DRV_MESON
90 &backend_meson,
91#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053092#ifdef DRV_MSM
93 &backend_msm,
94#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050095 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -040096#ifdef DRV_RADEON
97 &backend_radeon,
98#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070099#ifdef DRV_ROCKCHIP
100 &backend_rockchip,
101#endif
102#ifdef DRV_TEGRA
103 &backend_tegra,
104#endif
105 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100106#ifdef DRV_VC4
107 &backend_vc4,
108#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800109 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700110 };
111
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800112 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700113 if (!strcmp(drm_version->name, backend_list[i]->name)) {
114 drmFreeVersion(drm_version);
115 return backend_list[i];
116 }
117
118 drmFreeVersion(drm_version);
119 return NULL;
120}
121
122struct driver *drv_create(int fd)
123{
124 struct driver *drv;
125 int ret;
126
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800127 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128
129 if (!drv)
130 return NULL;
131
132 drv->fd = fd;
133 drv->backend = drv_get_backend(fd);
134
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 if (!drv->backend)
136 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700137
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800138 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700139 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700140
141 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700142 if (!drv->buffer_table)
143 goto free_lock;
144
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700145 drv->mappings = drv_array_init(sizeof(struct mapping));
146 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700147 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700148
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700149 drv->combos = drv_array_init(sizeof(struct combination));
150 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700151 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700152
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700153 if (drv->backend->init) {
154 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800155 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700156 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700157 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800158 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700159 }
160
161 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700163free_mappings:
164 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700165free_buffer_table:
166 drmHashDestroy(drv->buffer_table);
167free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800168 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700169free_driver:
170 free(drv);
171 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700172}
173
174void drv_destroy(struct driver *drv)
175{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800176 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700178 if (drv->backend->close)
179 drv->backend->close(drv);
180
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700181 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700182 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700183 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700184
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800185 pthread_mutex_unlock(&drv->driver_lock);
186 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700187
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700188 free(drv);
189}
190
191int drv_get_fd(struct driver *drv)
192{
193 return drv->fd;
194}
195
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800196const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197{
198 return drv->backend->name;
199}
200
Gurchetan Singha1892b22017-09-28 16:40:52 -0700201struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800203 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700204
Gurchetan Singha1892b22017-09-28 16:40:52 -0700205 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700206 return 0;
207
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800208 best = NULL;
209 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700210 for (i = 0; i < drv_array_size(drv->combos); i++) {
211 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700212 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800213 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800214 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700215 }
216
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800217 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218}
219
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700220struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700221 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222{
223
224 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800225 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700226
227 if (!bo)
228 return NULL;
229
230 bo->drv = drv;
231 bo->width = width;
232 bo->height = height;
233 bo->format = format;
Gurchetan Singha1892b22017-09-28 16:40:52 -0700234 bo->use_flags = use_flags;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235 bo->num_planes = drv_num_planes_from_format(format);
236
237 if (!bo->num_planes) {
238 free(bo);
239 return NULL;
240 }
241
242 return bo;
243}
244
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800245struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700246 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247{
248 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700249 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700250 struct bo *bo;
251
Gurchetan Singha1892b22017-09-28 16:40:52 -0700252 bo = drv_bo_new(drv, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700253
254 if (!bo)
255 return NULL;
256
Gurchetan Singha1892b22017-09-28 16:40:52 -0700257 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700258
259 if (ret) {
260 free(bo);
261 return NULL;
262 }
263
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800264 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700266 for (plane = 0; plane < bo->num_planes; plane++) {
267 if (plane > 0)
268 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
269
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700270 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700271 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700272
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800273 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700274
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700275 return bo;
276}
277
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800278struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
279 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700280{
281 int ret;
282 size_t plane;
283 struct bo *bo;
284
285 if (!drv->backend->bo_create_with_modifiers) {
286 errno = ENOENT;
287 return NULL;
288 }
289
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700290 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700291
292 if (!bo)
293 return NULL;
294
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800295 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700296
297 if (ret) {
298 free(bo);
299 return NULL;
300 }
301
302 pthread_mutex_lock(&drv->driver_lock);
303
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700304 for (plane = 0; plane < bo->num_planes; plane++) {
305 if (plane > 0)
306 assert(bo->offsets[plane] >= bo->offsets[plane - 1]);
307
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700308 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700309 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700310
311 pthread_mutex_unlock(&drv->driver_lock);
312
313 return bo;
314}
315
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700316void drv_bo_destroy(struct bo *bo)
317{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700318 size_t plane;
319 uintptr_t total = 0;
320 struct driver *drv = bo->drv;
321
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800322 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700323
324 for (plane = 0; plane < bo->num_planes; plane++)
325 drv_decrement_reference_count(drv, bo, plane);
326
Gurchetan Singhde263f12017-01-24 13:30:06 -0800327 for (plane = 0; plane < bo->num_planes; plane++)
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700328 total += drv_get_reference_count(drv, bo, plane);
329
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800330 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700331
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900332 if (total == 0) {
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700333 assert(drv_mapping_destroy(bo) == 0);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700334 bo->drv->backend->bo_destroy(bo);
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900335 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700336
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700337 free(bo);
338}
339
340struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
341{
342 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700343 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700344 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700345 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700346
Gurchetan Singha1892b22017-09-28 16:40:52 -0700347 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700348
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700349 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700350 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700351
Gurchetan Singh71611d62017-01-03 16:49:56 -0800352 ret = drv->backend->bo_import(bo, data);
353 if (ret) {
354 free(bo);
355 return NULL;
356 }
357
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700358 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700359 bo->strides[plane] = data->strides[plane];
360 bo->offsets[plane] = data->offsets[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700361 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700362
363 seek_end = lseek(data->fds[plane], 0, SEEK_END);
364 if (seek_end == (off_t)(-1)) {
365 fprintf(stderr, "drv: lseek() failed with %s\n", strerror(errno));
366 goto destroy_bo;
367 }
368
369 lseek(data->fds[plane], 0, SEEK_SET);
370 if (plane == bo->num_planes - 1 || data->offsets[plane + 1] == 0)
371 bo->sizes[plane] = seek_end - data->offsets[plane];
372 else
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800373 bo->sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700374
375 if ((int64_t)bo->offsets[plane] + bo->sizes[plane] > seek_end) {
376 fprintf(stderr, "drv: buffer size is too large.\n");
377 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800378 }
379
380 bo->total_size += bo->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700381 }
382
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700383 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700384
385destroy_bo:
386 drv_bo_destroy(bo);
387 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700388}
389
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800390void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
391 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700392{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700393 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700394 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700395 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700396
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800397 assert(rect->width >= 0);
398 assert(rect->height >= 0);
399 assert(rect->x + rect->width <= drv_bo_get_width(bo));
400 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700401 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900402 /* No CPU access for protected buffers. */
403 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700404
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700405 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800406 mapping.rect = *rect;
407 mapping.refcount = 1;
408
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800409 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700410
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700411 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
412 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
413 if (prior->vma->handle != bo->handles[plane].u32 ||
414 prior->vma->map_flags != map_flags)
415 continue;
416
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800417 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
418 rect->width != prior->rect.width || rect->height != prior->rect.height)
419 continue;
420
421 prior->refcount++;
422 *map_data = prior;
423 goto exact_match;
424 }
425
426 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
427 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
428 if (prior->vma->handle != bo->handles[plane].u32 ||
429 prior->vma->map_flags != map_flags)
430 continue;
431
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700432 prior->vma->refcount++;
433 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700434 goto success;
435 }
436
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700437 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800438 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439 if (addr == MAP_FAILED) {
440 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700441 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800442 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700443 return MAP_FAILED;
444 }
445
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700446 mapping.vma->refcount = 1;
447 mapping.vma->addr = addr;
448 mapping.vma->handle = bo->handles[plane].u32;
449 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700450
451success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700452 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800453exact_match:
454 drv_bo_invalidate(bo, *map_data);
455 addr = (uint8_t *)((*map_data)->vma->addr);
456 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800457 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800458 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700459}
460
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700461int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700462{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700463 uint32_t i;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700464 int ret = drv_bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700465 if (ret)
466 return ret;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700467
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800468 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700469
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800470 if (--mapping->refcount)
471 goto out;
472
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700473 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800474 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700475 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700476 }
477
478 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
479 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
480 drv_array_remove(bo->drv->mappings, i);
481 break;
482 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700483 }
484
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800485out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800486 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700487 return ret;
488}
489
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700490int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700491{
492 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700493
494 assert(mapping);
495 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800496 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700497 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700498
499 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700500 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700501
502 return ret;
503}
504
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700505int drv_bo_flush(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700506{
507 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700508
509 assert(mapping);
510 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800511 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512 assert(mapping->vma->refcount > 0);
Tomasz Figae0807b12017-08-04 12:50:03 +0900513 assert(!(bo->use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700514
515 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700516 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700517
518 return ret;
519}
520
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700521uint32_t drv_bo_get_width(struct bo *bo)
522{
523 return bo->width;
524}
525
526uint32_t drv_bo_get_height(struct bo *bo)
527{
528 return bo->height;
529}
530
531uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
532{
533 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
534}
535
536size_t drv_bo_get_num_planes(struct bo *bo)
537{
538 return bo->num_planes;
539}
540
541union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
542{
543 return bo->handles[plane];
544}
545
546#ifndef DRM_RDWR
547#define DRM_RDWR O_RDWR
548#endif
549
550int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
551{
552
553 int ret, fd;
554 assert(plane < bo->num_planes);
555
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800556 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700557
558 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700559}
560
561uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
562{
563 assert(plane < bo->num_planes);
564 return bo->offsets[plane];
565}
566
567uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
568{
569 assert(plane < bo->num_planes);
570 return bo->sizes[plane];
571}
572
573uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
574{
575 assert(plane < bo->num_planes);
576 return bo->strides[plane];
577}
578
579uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
580{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800581 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700582 return bo->format_modifiers[plane];
583}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700584
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800585uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700586{
587 return bo->format;
588}
589
Gurchetan Singha1892b22017-09-28 16:40:52 -0700590uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700591{
592 if (drv->backend->resolve_format)
Gurchetan Singha1892b22017-09-28 16:40:52 -0700593 return drv->backend->resolve_format(format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700594
595 return format;
596}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700597
Gurchetan Singh2a119342016-11-02 10:40:51 -0700598size_t drv_num_planes_from_format(uint32_t format)
599{
600 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800601 case DRM_FORMAT_ABGR1555:
602 case DRM_FORMAT_ABGR2101010:
603 case DRM_FORMAT_ABGR4444:
604 case DRM_FORMAT_ABGR8888:
605 case DRM_FORMAT_ARGB1555:
606 case DRM_FORMAT_ARGB2101010:
607 case DRM_FORMAT_ARGB4444:
608 case DRM_FORMAT_ARGB8888:
609 case DRM_FORMAT_AYUV:
610 case DRM_FORMAT_BGR233:
611 case DRM_FORMAT_BGR565:
612 case DRM_FORMAT_BGR888:
613 case DRM_FORMAT_BGRA1010102:
614 case DRM_FORMAT_BGRA4444:
615 case DRM_FORMAT_BGRA5551:
616 case DRM_FORMAT_BGRA8888:
617 case DRM_FORMAT_BGRX1010102:
618 case DRM_FORMAT_BGRX4444:
619 case DRM_FORMAT_BGRX5551:
620 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800621 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800622 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800623 case DRM_FORMAT_R8:
624 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800625 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800626 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800627 case DRM_FORMAT_RGB888:
628 case DRM_FORMAT_RGBA1010102:
629 case DRM_FORMAT_RGBA4444:
630 case DRM_FORMAT_RGBA5551:
631 case DRM_FORMAT_RGBA8888:
632 case DRM_FORMAT_RGBX1010102:
633 case DRM_FORMAT_RGBX4444:
634 case DRM_FORMAT_RGBX5551:
635 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800636 case DRM_FORMAT_UYVY:
637 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800638 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800639 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800640 case DRM_FORMAT_XBGR4444:
641 case DRM_FORMAT_XBGR8888:
642 case DRM_FORMAT_XRGB1555:
643 case DRM_FORMAT_XRGB2101010:
644 case DRM_FORMAT_XRGB4444:
645 case DRM_FORMAT_XRGB8888:
646 case DRM_FORMAT_YUYV:
647 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700648 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800649 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530650 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700651 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800652 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800653 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700654 return 3;
655 }
656
657 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
658 return 0;
659}
660
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700661uint32_t drv_num_buffers_per_bo(struct bo *bo)
662{
663 uint32_t count = 0;
664 size_t plane, p;
665
666 for (plane = 0; plane < bo->num_planes; plane++) {
667 for (p = 0; p < plane; p++)
668 if (bo->handles[p].u32 == bo->handles[plane].u32)
669 break;
670 if (p == plane)
671 count++;
672 }
673
674 return count;
675}