blob: ce6e63f0bfd4bb892f4855a694fb398a097aac23 [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>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070015#include <xf86drm.h>
16
17#include "drv_priv.h"
18#include "helpers.h"
19#include "util.h"
20
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053021#ifdef DRV_AMDGPU
22extern struct backend backend_amdgpu;
23#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070024extern struct backend backend_cirrus;
25extern struct backend backend_evdi;
26#ifdef DRV_EXYNOS
27extern struct backend backend_exynos;
28#endif
29extern struct backend backend_gma500;
30#ifdef DRV_I915
31extern struct backend backend_i915;
32#endif
33#ifdef DRV_MARVELL
34extern struct backend backend_marvell;
35#endif
36#ifdef DRV_MEDIATEK
37extern struct backend backend_mediatek;
38#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050039extern struct backend backend_nouveau;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070040#ifdef DRV_ROCKCHIP
41extern struct backend backend_rockchip;
42#endif
43#ifdef DRV_TEGRA
44extern struct backend backend_tegra;
45#endif
46extern struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010047#ifdef DRV_VC4
48extern struct backend backend_vc4;
49#endif
Gurchetan Singh5521bde2016-09-30 14:53:32 -070050extern struct backend backend_vgem;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070051extern struct backend backend_virtio_gpu;
52
53static struct backend *drv_get_backend(int fd)
54{
55 drmVersionPtr drm_version;
56 unsigned int i;
57
58 drm_version = drmGetVersion(fd);
59
60 if (!drm_version)
61 return NULL;
62
63 struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053064#ifdef DRV_AMDGPU
65 &backend_amdgpu,
66#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080067 &backend_cirrus, &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070068#ifdef DRV_EXYNOS
69 &backend_exynos,
70#endif
71 &backend_gma500,
72#ifdef DRV_I915
73 &backend_i915,
74#endif
75#ifdef DRV_MARVELL
76 &backend_marvell,
77#endif
78#ifdef DRV_MEDIATEK
79 &backend_mediatek,
80#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -050081 &backend_nouveau,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_ROCKCHIP
83 &backend_rockchip,
84#endif
85#ifdef DRV_TEGRA
86 &backend_tegra,
87#endif
88 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +010089#ifdef DRV_VC4
90 &backend_vc4,
91#endif
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080092 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070093 };
94
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -080095 for (i = 0; i < ARRAY_SIZE(backend_list); i++)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070096 if (!strcmp(drm_version->name, backend_list[i]->name)) {
97 drmFreeVersion(drm_version);
98 return backend_list[i];
99 }
100
101 drmFreeVersion(drm_version);
102 return NULL;
103}
104
105struct driver *drv_create(int fd)
106{
107 struct driver *drv;
108 int ret;
109
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800110 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700111
112 if (!drv)
113 return NULL;
114
115 drv->fd = fd;
116 drv->backend = drv_get_backend(fd);
117
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700118 if (!drv->backend)
119 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700120
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800121 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700122 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700123
124 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700125 if (!drv->buffer_table)
126 goto free_lock;
127
128 drv->map_table = drmHashCreate();
129 if (!drv->map_table)
130 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700131
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800132 /* Start with a power of 2 number of allocations. */
133 drv->backend->combos.allocations = 2;
134 drv->backend->combos.size = 0;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800135 drv->backend->combos.data =
136 calloc(drv->backend->combos.allocations, sizeof(struct combination));
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800137 if (!drv->backend->combos.data)
138 goto free_map_table;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700139
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700140 if (drv->backend->init) {
141 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800142 if (ret) {
143 free(drv->backend->combos.data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700144 goto free_map_table;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800145 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 }
147
148 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700149
150free_map_table:
151 drmHashDestroy(drv->map_table);
152free_buffer_table:
153 drmHashDestroy(drv->buffer_table);
154free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800155 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700156free_driver:
157 free(drv);
158 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700159}
160
161void drv_destroy(struct driver *drv)
162{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800163 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700164
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700165 if (drv->backend->close)
166 drv->backend->close(drv);
167
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700168 drmHashDestroy(drv->buffer_table);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700169 drmHashDestroy(drv->map_table);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700170
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800171 free(drv->backend->combos.data);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700172
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800173 pthread_mutex_unlock(&drv->driver_lock);
174 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700175
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700176 free(drv);
177}
178
179int drv_get_fd(struct driver *drv)
180{
181 return drv->fd;
182}
183
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800184const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700185{
186 return drv->backend->name;
187}
188
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800189struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t usage)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800191 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192
Gurchetan Singh458976f2016-11-23 17:32:33 -0800193 if (format == DRM_FORMAT_NONE || usage == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700194 return 0;
195
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800196 best = NULL;
197 uint32_t i;
198 for (i = 0; i < drv->backend->combos.size; i++) {
199 curr = &drv->backend->combos.data[i];
200 if ((format == curr->format) && usage == (curr->usage & usage))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800201 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800202 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700203 }
204
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800205 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700206}
207
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800208struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209{
210
211 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800212 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700213
214 if (!bo)
215 return NULL;
216
217 bo->drv = drv;
218 bo->width = width;
219 bo->height = height;
220 bo->format = format;
221 bo->num_planes = drv_num_planes_from_format(format);
222
223 if (!bo->num_planes) {
224 free(bo);
225 return NULL;
226 }
227
228 return bo;
229}
230
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800231struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
232 uint64_t flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700233{
234 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700235 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700236 struct bo *bo;
237
238 bo = drv_bo_new(drv, width, height, format);
239
240 if (!bo)
241 return NULL;
242
243 ret = drv->backend->bo_create(bo, width, height, format, flags);
244
245 if (ret) {
246 free(bo);
247 return NULL;
248 }
249
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800250 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700251
252 for (plane = 0; plane < bo->num_planes; plane++)
253 drv_increment_reference_count(drv, bo, plane);
254
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800255 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700256
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700257 return bo;
258}
259
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800260struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
261 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700262{
263 int ret;
264 size_t plane;
265 struct bo *bo;
266
267 if (!drv->backend->bo_create_with_modifiers) {
268 errno = ENOENT;
269 return NULL;
270 }
271
272 bo = drv_bo_new(drv, width, height, format);
273
274 if (!bo)
275 return NULL;
276
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800277 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers, count);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700278
279 if (ret) {
280 free(bo);
281 return NULL;
282 }
283
284 pthread_mutex_lock(&drv->driver_lock);
285
286 for (plane = 0; plane < bo->num_planes; plane++)
287 drv_increment_reference_count(drv, bo, plane);
288
289 pthread_mutex_unlock(&drv->driver_lock);
290
291 return bo;
292}
293
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700294void drv_bo_destroy(struct bo *bo)
295{
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700296 size_t plane;
297 uintptr_t total = 0;
298 struct driver *drv = bo->drv;
299
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800300 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700301
302 for (plane = 0; plane < bo->num_planes; plane++)
303 drv_decrement_reference_count(drv, bo, plane);
304
305 for (plane = 0; plane < bo->num_planes; plane++)
306 total += drv_get_reference_count(drv, bo, plane);
307
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800308 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700309
310 if (total == 0)
311 bo->drv->backend->bo_destroy(bo);
312
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700313 free(bo);
314}
315
316struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
317{
318 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700319 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700320 struct bo *bo;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700321
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700322 bo = drv_bo_new(drv, data->width, data->height, data->format);
323
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700324 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700325 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700326
Gurchetan Singh71611d62017-01-03 16:49:56 -0800327 ret = drv->backend->bo_import(bo, data);
328 if (ret) {
329 free(bo);
330 return NULL;
331 }
332
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700333 for (plane = 0; plane < bo->num_planes; plane++) {
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700334 bo->strides[plane] = data->strides[plane];
335 bo->offsets[plane] = data->offsets[plane];
336 bo->sizes[plane] = data->sizes[plane];
Kristian H. Kristensen33459772016-09-16 11:14:16 -0700337 bo->format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700338 bo->total_size += data->sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700339 }
340
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700341 return bo;
342}
343
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800344void *drv_bo_map(struct bo *bo, uint32_t x, uint32_t y, uint32_t width, uint32_t height,
345 uint32_t flags, struct map_info **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700346{
347 void *ptr;
348 uint8_t *addr;
349 size_t offset;
350 struct map_info *data;
351
352 assert(width > 0);
353 assert(height > 0);
354 assert(x + width <= drv_bo_get_width(bo));
355 assert(y + height <= drv_bo_get_height(bo));
356
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800357 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700358
359 if (!drmHashLookup(bo->drv->map_table, bo->handles[plane].u32, &ptr)) {
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800360 data = (struct map_info *)ptr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700361 data->refcount++;
362 goto success;
363 }
364
365 data = calloc(1, sizeof(*data));
366 addr = bo->drv->backend->bo_map(bo, data, plane);
367 if (addr == MAP_FAILED) {
368 *map_data = NULL;
369 free(data);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800370 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700371 return MAP_FAILED;
372 }
373
374 data->refcount = 1;
375 data->addr = addr;
376 data->handle = bo->handles[plane].u32;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800377 drmHashInsert(bo->drv->map_table, bo->handles[plane].u32, (void *)data);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700378
379success:
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800380 *map_data = data;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700381 offset = drv_bo_get_plane_stride(bo, plane) * y;
382 offset += drv_stride_from_format(bo->format, x, plane);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800383 addr = (uint8_t *)data->addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700384 addr += drv_bo_get_plane_offset(bo, plane) + offset;
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800385 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700386
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800387 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700388}
389
Gurchetan Singh80fc2b92017-02-14 17:47:02 -0800390int drv_bo_unmap(struct bo *bo, struct map_info *data)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700391{
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700392 int ret = 0;
393
394 assert(data);
395 assert(data->refcount >= 0);
396
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800397 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700398
399 if (!--data->refcount) {
Gurchetan Singh44d1fe42016-12-14 08:51:28 -0800400 if (bo->drv->backend->bo_unmap)
401 ret = bo->drv->backend->bo_unmap(bo, data);
402 else
403 ret = munmap(data->addr, data->length);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700404 drmHashDelete(bo->drv->map_table, data->handle);
405 free(data);
406 }
407
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800408 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700409
410 return ret;
411}
412
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700413uint32_t drv_bo_get_width(struct bo *bo)
414{
415 return bo->width;
416}
417
418uint32_t drv_bo_get_height(struct bo *bo)
419{
420 return bo->height;
421}
422
423uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
424{
425 return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
426}
427
428size_t drv_bo_get_num_planes(struct bo *bo)
429{
430 return bo->num_planes;
431}
432
433union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
434{
435 return bo->handles[plane];
436}
437
438#ifndef DRM_RDWR
439#define DRM_RDWR O_RDWR
440#endif
441
442int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
443{
444
445 int ret, fd;
446 assert(plane < bo->num_planes);
447
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800448 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700449
450 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700451}
452
453uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
454{
455 assert(plane < bo->num_planes);
456 return bo->offsets[plane];
457}
458
459uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
460{
461 assert(plane < bo->num_planes);
462 return bo->sizes[plane];
463}
464
465uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
466{
467 assert(plane < bo->num_planes);
468 return bo->strides[plane];
469}
470
471uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
472{
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800473 assert(plane < bo->num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700474 return bo->format_modifiers[plane];
475}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700476
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800477uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700478{
479 return bo->format;
480}
481
Tomasz Figace1ae022017-07-05 18:15:06 +0900482uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t usage)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700483{
484 if (drv->backend->resolve_format)
Tomasz Figace1ae022017-07-05 18:15:06 +0900485 return drv->backend->resolve_format(format, usage);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700486
487 return format;
488}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700489
Gurchetan Singh2a119342016-11-02 10:40:51 -0700490size_t drv_num_planes_from_format(uint32_t format)
491{
492 switch (format) {
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800493 case DRM_FORMAT_ABGR1555:
494 case DRM_FORMAT_ABGR2101010:
495 case DRM_FORMAT_ABGR4444:
496 case DRM_FORMAT_ABGR8888:
497 case DRM_FORMAT_ARGB1555:
498 case DRM_FORMAT_ARGB2101010:
499 case DRM_FORMAT_ARGB4444:
500 case DRM_FORMAT_ARGB8888:
501 case DRM_FORMAT_AYUV:
502 case DRM_FORMAT_BGR233:
503 case DRM_FORMAT_BGR565:
504 case DRM_FORMAT_BGR888:
505 case DRM_FORMAT_BGRA1010102:
506 case DRM_FORMAT_BGRA4444:
507 case DRM_FORMAT_BGRA5551:
508 case DRM_FORMAT_BGRA8888:
509 case DRM_FORMAT_BGRX1010102:
510 case DRM_FORMAT_BGRX4444:
511 case DRM_FORMAT_BGRX5551:
512 case DRM_FORMAT_BGRX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800513 case DRM_FORMAT_C8:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800514 case DRM_FORMAT_GR88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800515 case DRM_FORMAT_R8:
516 case DRM_FORMAT_RG88:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800517 case DRM_FORMAT_RGB332:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800518 case DRM_FORMAT_RGB565:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800519 case DRM_FORMAT_RGB888:
520 case DRM_FORMAT_RGBA1010102:
521 case DRM_FORMAT_RGBA4444:
522 case DRM_FORMAT_RGBA5551:
523 case DRM_FORMAT_RGBA8888:
524 case DRM_FORMAT_RGBX1010102:
525 case DRM_FORMAT_RGBX4444:
526 case DRM_FORMAT_RGBX5551:
527 case DRM_FORMAT_RGBX8888:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800528 case DRM_FORMAT_UYVY:
529 case DRM_FORMAT_VYUY:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800530 case DRM_FORMAT_XBGR1555:
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800531 case DRM_FORMAT_XBGR2101010:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800532 case DRM_FORMAT_XBGR4444:
533 case DRM_FORMAT_XBGR8888:
534 case DRM_FORMAT_XRGB1555:
535 case DRM_FORMAT_XRGB2101010:
536 case DRM_FORMAT_XRGB4444:
537 case DRM_FORMAT_XRGB8888:
538 case DRM_FORMAT_YUYV:
539 case DRM_FORMAT_YVYU:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700540 return 1;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800541 case DRM_FORMAT_NV12:
Shirish Sdf423df2017-04-18 16:21:59 +0530542 case DRM_FORMAT_NV21:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700543 return 2;
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800544 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800545 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700546 return 3;
547 }
548
549 fprintf(stderr, "drv: UNKNOWN FORMAT %d\n", format);
550 return 0;
551}
552
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800553uint32_t drv_size_from_format(uint32_t format, uint32_t stride, uint32_t height, size_t plane)
Gurchetan Singh2a119342016-11-02 10:40:51 -0700554{
555 assert(plane < drv_num_planes_from_format(format));
556 uint32_t vertical_subsampling;
557
558 switch (format) {
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800559 case DRM_FORMAT_NV12:
Gurchetan Singhe8ab0a52016-11-21 11:36:31 -0800560 case DRM_FORMAT_YVU420:
Gurchetan Singh03f13562017-02-08 15:21:14 -0800561 case DRM_FORMAT_YVU420_ANDROID:
Gurchetan Singh2a119342016-11-02 10:40:51 -0700562 vertical_subsampling = (plane == 0) ? 1 : 2;
563 break;
564 default:
565 vertical_subsampling = 1;
566 }
567
568 return stride * DIV_ROUND_UP(height, vertical_subsampling);
569}
570
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700571uint32_t drv_num_buffers_per_bo(struct bo *bo)
572{
573 uint32_t count = 0;
574 size_t plane, p;
575
576 for (plane = 0; plane < bo->num_planes; plane++) {
577 for (p = 0; p < plane; p++)
578 if (bo->handles[p].u32 == bo->handles[plane].u32)
579 break;
580 if (p == plane)
581 count++;
582 }
583
584 return count;
585}