1 /*
2 * Copyright 2016 The Chromium OS Authors. All rights reserved.
3 * 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>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <xf86drm.h>
17
18 #ifdef __ANDROID__
19 #include <cutils/log.h>
20 #include <libgen.h>
21 #endif
22
23 #include "drv_helpers.h"
24 #include "drv_priv.h"
25 #include "util.h"
26
27 #ifdef DRV_AMDGPU
28 extern const struct backend backend_amdgpu;
29 #endif
30 #ifdef DRV_I915
31 extern const struct backend backend_i915;
32 #endif
33 #ifdef DRV_MSM
34 extern const struct backend backend_msm;
35 #endif
36 #ifdef DRV_VC4
37 extern const struct backend backend_vc4;
38 #endif
39
40 // Dumb / generic drivers
41 extern const struct backend backend_evdi;
42 extern const struct backend backend_marvell;
43 extern const struct backend backend_mediatek;
44 extern const struct backend backend_meson;
45 extern const struct backend backend_nouveau;
46 extern const struct backend backend_komeda;
47 extern const struct backend backend_radeon;
48 extern const struct backend backend_rockchip;
49 extern const struct backend backend_sun4i_drm;
50 extern const struct backend backend_synaptics;
51 extern const struct backend backend_virtgpu;
52 extern const struct backend backend_udl;
53 extern const struct backend backend_vkms;
54
55 static const struct backend *drv_backend_list[] = {
56 #ifdef DRV_AMDGPU
57 &backend_amdgpu,
58 #endif
59 #ifdef DRV_I915
60 &backend_i915,
61 #endif
62 #ifdef DRV_MSM
63 &backend_msm,
64 #endif
65 #ifdef DRV_VC4
66 &backend_vc4,
67 #endif
68 &backend_evdi, &backend_komeda, &backend_marvell, &backend_mediatek,
69 &backend_meson, &backend_nouveau, &backend_radeon, &backend_rockchip,
70 &backend_sun4i_drm, &backend_synaptics, &backend_udl, &backend_virtgpu,
71 &backend_vkms
72 };
73
drv_preload(bool load)74 void drv_preload(bool load)
75 {
76 unsigned int i;
77 for (i = 0; i < ARRAY_SIZE(drv_backend_list); i++) {
78 const struct backend *b = drv_backend_list[i];
79 if (b->preload)
80 b->preload(load);
81 }
82 }
83
drv_get_backend(int fd)84 static const struct backend *drv_get_backend(int fd)
85 {
86 drmVersionPtr drm_version;
87 unsigned int i;
88
89 drm_version = drmGetVersion(fd);
90
91 if (!drm_version)
92 return NULL;
93
94 for (i = 0; i < ARRAY_SIZE(drv_backend_list); i++) {
95 const struct backend *b = drv_backend_list[i];
96 if (!strcmp(drm_version->name, b->name)) {
97 drmFreeVersion(drm_version);
98 return b;
99 }
100 }
101
102 drmFreeVersion(drm_version);
103 return NULL;
104 }
105
drv_create(int fd)106 struct driver *drv_create(int fd)
107 {
108 struct driver *drv;
109 int ret;
110
111 drv = (struct driver *)calloc(1, sizeof(*drv));
112
113 if (!drv)
114 return NULL;
115
116 char *minigbm_debug;
117 minigbm_debug = getenv("MINIGBM_DEBUG");
118 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
119
120 drv->fd = fd;
121 drv->backend = drv_get_backend(fd);
122
123 if (!drv->backend)
124 goto free_driver;
125
126 if (pthread_mutex_init(&drv->buffer_table_lock, NULL))
127 goto free_driver;
128
129 drv->buffer_table = drmHashCreate();
130 if (!drv->buffer_table)
131 goto free_buffer_table_lock;
132
133 if (pthread_mutex_init(&drv->mappings_lock, NULL))
134 goto free_buffer_table;
135
136 drv->mappings = drv_array_init(sizeof(struct mapping));
137 if (!drv->mappings)
138 goto free_mappings_lock;
139
140 drv->combos = drv_array_init(sizeof(struct combination));
141 if (!drv->combos)
142 goto free_mappings;
143
144 if (drv->backend->init) {
145 ret = drv->backend->init(drv);
146 if (ret) {
147 drv_array_destroy(drv->combos);
148 goto free_mappings;
149 }
150 }
151
152 return drv;
153
154 free_mappings:
155 drv_array_destroy(drv->mappings);
156 free_mappings_lock:
157 pthread_mutex_destroy(&drv->mappings_lock);
158 free_buffer_table:
159 drmHashDestroy(drv->buffer_table);
160 free_buffer_table_lock:
161 pthread_mutex_destroy(&drv->buffer_table_lock);
162 free_driver:
163 free(drv);
164 return NULL;
165 }
166
drv_destroy(struct driver * drv)167 void drv_destroy(struct driver *drv)
168 {
169 if (drv->backend->close)
170 drv->backend->close(drv);
171
172 drv_array_destroy(drv->combos);
173
174 drv_array_destroy(drv->mappings);
175 pthread_mutex_destroy(&drv->mappings_lock);
176
177 drmHashDestroy(drv->buffer_table);
178 pthread_mutex_destroy(&drv->buffer_table_lock);
179
180 free(drv);
181 }
182
drv_get_fd(struct driver * drv)183 int drv_get_fd(struct driver *drv)
184 {
185 return drv->fd;
186 }
187
drv_get_name(struct driver * drv)188 const char *drv_get_name(struct driver *drv)
189 {
190 return drv->backend->name;
191 }
192
drv_get_combination(struct driver * drv,uint32_t format,uint64_t use_flags)193 struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
194 {
195 struct combination *curr, *best;
196
197 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
198 return 0;
199
200 best = NULL;
201 uint32_t i;
202 for (i = 0; i < drv_array_size(drv->combos); i++) {
203 curr = drv_array_at_idx(drv->combos, i);
204 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
205 if (!best || best->metadata.priority < curr->metadata.priority)
206 best = curr;
207 }
208
209 return best;
210 }
211
drv_bo_new(struct driver * drv,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags,bool is_test_buffer)212 struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
213 uint64_t use_flags, bool is_test_buffer)
214 {
215
216 struct bo *bo;
217 bo = (struct bo *)calloc(1, sizeof(*bo));
218
219 if (!bo)
220 return NULL;
221
222 bo->drv = drv;
223 bo->meta.width = width;
224 bo->meta.height = height;
225 bo->meta.format = format;
226 bo->meta.use_flags = use_flags;
227 bo->meta.num_planes = drv_num_planes_from_format(format);
228 bo->is_test_buffer = is_test_buffer;
229
230 if (!bo->meta.num_planes) {
231 free(bo);
232 errno = EINVAL;
233 return NULL;
234 }
235
236 return bo;
237 }
238
drv_bo_mapping_destroy(struct bo * bo)239 static void drv_bo_mapping_destroy(struct bo *bo)
240 {
241 struct driver *drv = bo->drv;
242 uint32_t idx = 0;
243
244 /*
245 * This function is called right before the buffer is destroyed. It will free any mappings
246 * associated with the buffer.
247 */
248 pthread_mutex_lock(&drv->mappings_lock);
249 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
250 while (idx < drv_array_size(drv->mappings)) {
251 struct mapping *mapping =
252 (struct mapping *)drv_array_at_idx(drv->mappings, idx);
253 if (mapping->vma->handle != bo->handles[plane].u32) {
254 idx++;
255 continue;
256 }
257
258 if (!--mapping->vma->refcount) {
259 int ret = drv->backend->bo_unmap(bo, mapping->vma);
260 if (ret) {
261 pthread_mutex_unlock(&drv->mappings_lock);
262 assert(ret);
263 drv_loge("munmap failed\n");
264 return;
265 }
266
267 free(mapping->vma);
268 }
269
270 /* This shrinks and shifts the array, so don't increment idx. */
271 drv_array_remove(drv->mappings, idx);
272 }
273 }
274 pthread_mutex_unlock(&drv->mappings_lock);
275 }
276
277 /*
278 * Acquire a reference on plane buffers of the bo.
279 */
drv_bo_acquire(struct bo * bo)280 static void drv_bo_acquire(struct bo *bo)
281 {
282 struct driver *drv = bo->drv;
283
284 pthread_mutex_lock(&drv->buffer_table_lock);
285 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
286 uintptr_t num = 0;
287
288 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num))
289 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
290
291 drmHashInsert(drv->buffer_table, bo->handles[plane].u32, (void *)(num + 1));
292 }
293 pthread_mutex_unlock(&drv->buffer_table_lock);
294 }
295
296 /*
297 * Release a reference on plane buffers of the bo. Return true when the bo has lost all its
298 * references. Otherwise, return false.
299 */
drv_bo_release(struct bo * bo)300 static bool drv_bo_release(struct bo *bo)
301 {
302 struct driver *drv = bo->drv;
303 uintptr_t num;
304
305 if (drv->backend->bo_release)
306 drv->backend->bo_release(bo);
307
308 pthread_mutex_lock(&drv->buffer_table_lock);
309 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
310 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num)) {
311 drmHashDelete(drv->buffer_table, bo->handles[plane].u32);
312
313 if (num > 1) {
314 drmHashInsert(drv->buffer_table, bo->handles[plane].u32,
315 (void *)(num - 1));
316 }
317 }
318 }
319
320 /* The same buffer can back multiple planes with different offsets. */
321 for (size_t plane = 0; plane < bo->meta.num_planes; plane++) {
322 if (!drmHashLookup(drv->buffer_table, bo->handles[plane].u32, (void **)&num)) {
323 /* num is positive if found in the hashmap. */
324 pthread_mutex_unlock(&drv->buffer_table_lock);
325 return false;
326 }
327 }
328 pthread_mutex_unlock(&drv->buffer_table_lock);
329
330 return true;
331 }
332
drv_bo_create(struct driver * drv,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)333 struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
334 uint64_t use_flags)
335 {
336 int ret;
337 struct bo *bo;
338 bool is_test_alloc;
339
340 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
341 use_flags &= ~BO_USE_TEST_ALLOC;
342
343 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
344
345 if (!bo)
346 return NULL;
347
348 ret = -EINVAL;
349 if (drv->backend->bo_compute_metadata) {
350 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
351 0);
352 if (!is_test_alloc && ret == 0)
353 ret = drv->backend->bo_create_from_metadata(bo);
354 } else if (!is_test_alloc) {
355 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
356 }
357
358 if (ret) {
359 errno = -ret;
360 free(bo);
361 return NULL;
362 }
363
364 drv_bo_acquire(bo);
365
366 return bo;
367 }
368
drv_bo_create_with_modifiers(struct driver * drv,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t count)369 struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
370 uint32_t format, const uint64_t *modifiers, uint32_t count)
371 {
372 int ret;
373 struct bo *bo;
374
375 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
376 errno = ENOENT;
377 return NULL;
378 }
379
380 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
381
382 if (!bo)
383 return NULL;
384
385 ret = -EINVAL;
386 if (drv->backend->bo_compute_metadata) {
387 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
388 modifiers, count);
389 if (ret == 0)
390 ret = drv->backend->bo_create_from_metadata(bo);
391 } else {
392 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
393 count);
394 }
395
396 if (ret) {
397 free(bo);
398 return NULL;
399 }
400
401 drv_bo_acquire(bo);
402
403 return bo;
404 }
405
drv_bo_destroy(struct bo * bo)406 void drv_bo_destroy(struct bo *bo)
407 {
408 if (!bo->is_test_buffer && drv_bo_release(bo)) {
409 drv_bo_mapping_destroy(bo);
410 bo->drv->backend->bo_destroy(bo);
411 }
412
413 free(bo);
414 }
415
drv_bo_import(struct driver * drv,struct drv_import_fd_data * data)416 struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
417 {
418 int ret;
419 size_t plane;
420 struct bo *bo;
421 off_t seek_end;
422
423 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
424
425 if (!bo)
426 return NULL;
427
428 ret = drv->backend->bo_import(bo, data);
429 if (ret) {
430 free(bo);
431 return NULL;
432 }
433
434 drv_bo_acquire(bo);
435
436 bo->meta.format_modifier = data->format_modifier;
437 for (plane = 0; plane < bo->meta.num_planes; plane++) {
438 bo->meta.strides[plane] = data->strides[plane];
439 bo->meta.offsets[plane] = data->offsets[plane];
440
441 seek_end = lseek(data->fds[plane], 0, SEEK_END);
442 if (seek_end == (off_t)(-1)) {
443 drv_loge("lseek() failed with %s\n", strerror(errno));
444 goto destroy_bo;
445 }
446
447 lseek(data->fds[plane], 0, SEEK_SET);
448 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
449 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
450 else
451 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
452
453 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
454 drv_loge("buffer size is too large.\n");
455 goto destroy_bo;
456 }
457
458 bo->meta.total_size += bo->meta.sizes[plane];
459 }
460
461 return bo;
462
463 destroy_bo:
464 drv_bo_destroy(bo);
465 return NULL;
466 }
467
drv_bo_map(struct bo * bo,const struct rectangle * rect,uint32_t map_flags,struct mapping ** map_data,size_t plane)468 void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
469 struct mapping **map_data, size_t plane)
470 {
471 struct driver *drv = bo->drv;
472 uint32_t i;
473 uint8_t *addr;
474 struct mapping mapping = { 0 };
475
476 assert(rect->width >= 0);
477 assert(rect->height >= 0);
478 assert(rect->x + rect->width <= drv_bo_get_width(bo));
479 assert(rect->y + rect->height <= drv_bo_get_height(bo));
480 assert(BO_MAP_READ_WRITE & map_flags);
481 /* No CPU access for protected buffers. */
482 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
483
484 if (bo->is_test_buffer)
485 return MAP_FAILED;
486
487 mapping.rect = *rect;
488 mapping.refcount = 1;
489
490 pthread_mutex_lock(&drv->mappings_lock);
491
492 for (i = 0; i < drv_array_size(drv->mappings); i++) {
493 struct mapping *prior = (struct mapping *)drv_array_at_idx(drv->mappings, i);
494 if (prior->vma->handle != bo->handles[plane].u32 ||
495 prior->vma->map_flags != map_flags)
496 continue;
497
498 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
499 rect->width != prior->rect.width || rect->height != prior->rect.height)
500 continue;
501
502 prior->refcount++;
503 *map_data = prior;
504 goto exact_match;
505 }
506
507 for (i = 0; i < drv_array_size(drv->mappings); i++) {
508 struct mapping *prior = (struct mapping *)drv_array_at_idx(drv->mappings, i);
509 if (prior->vma->handle != bo->handles[plane].u32 ||
510 prior->vma->map_flags != map_flags)
511 continue;
512
513 prior->vma->refcount++;
514 mapping.vma = prior->vma;
515 goto success;
516 }
517
518 mapping.vma = calloc(1, sizeof(*mapping.vma));
519 if (!mapping.vma) {
520 *map_data = NULL;
521 pthread_mutex_unlock(&drv->mappings_lock);
522 return MAP_FAILED;
523 }
524
525 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
526 addr = drv->backend->bo_map(bo, mapping.vma, map_flags);
527 if (addr == MAP_FAILED) {
528 *map_data = NULL;
529 free(mapping.vma);
530 pthread_mutex_unlock(&drv->mappings_lock);
531 return MAP_FAILED;
532 }
533
534 mapping.vma->refcount = 1;
535 mapping.vma->addr = addr;
536 mapping.vma->handle = bo->handles[plane].u32;
537 mapping.vma->map_flags = map_flags;
538
539 success:
540 *map_data = drv_array_append(drv->mappings, &mapping);
541 exact_match:
542 drv_bo_invalidate(bo, *map_data);
543 addr = (uint8_t *)((*map_data)->vma->addr);
544 addr += drv_bo_get_plane_offset(bo, plane);
545 pthread_mutex_unlock(&drv->mappings_lock);
546 return (void *)addr;
547 }
548
drv_bo_unmap(struct bo * bo,struct mapping * mapping)549 int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
550 {
551 struct driver *drv = bo->drv;
552 uint32_t i;
553 int ret = 0;
554
555 pthread_mutex_lock(&drv->mappings_lock);
556
557 if (--mapping->refcount)
558 goto out;
559
560 if (!--mapping->vma->refcount) {
561 ret = drv->backend->bo_unmap(bo, mapping->vma);
562 free(mapping->vma);
563 }
564
565 for (i = 0; i < drv_array_size(drv->mappings); i++) {
566 if (mapping == (struct mapping *)drv_array_at_idx(drv->mappings, i)) {
567 drv_array_remove(drv->mappings, i);
568 break;
569 }
570 }
571
572 out:
573 pthread_mutex_unlock(&drv->mappings_lock);
574 return ret;
575 }
576
drv_bo_invalidate(struct bo * bo,struct mapping * mapping)577 int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
578 {
579 int ret = 0;
580
581 assert(mapping);
582 assert(mapping->vma);
583 assert(mapping->refcount > 0);
584 assert(mapping->vma->refcount > 0);
585
586 if (bo->drv->backend->bo_invalidate)
587 ret = bo->drv->backend->bo_invalidate(bo, mapping);
588
589 return ret;
590 }
591
drv_bo_flush(struct bo * bo,struct mapping * mapping)592 int drv_bo_flush(struct bo *bo, struct mapping *mapping)
593 {
594 int ret = 0;
595
596 assert(mapping);
597 assert(mapping->vma);
598 assert(mapping->refcount > 0);
599 assert(mapping->vma->refcount > 0);
600
601 if (bo->drv->backend->bo_flush)
602 ret = bo->drv->backend->bo_flush(bo, mapping);
603
604 return ret;
605 }
606
drv_bo_flush_or_unmap(struct bo * bo,struct mapping * mapping)607 int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
608 {
609 int ret = 0;
610
611 assert(mapping);
612 assert(mapping->vma);
613 assert(mapping->refcount > 0);
614 assert(mapping->vma->refcount > 0);
615 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
616
617 if (bo->drv->backend->bo_flush)
618 ret = bo->drv->backend->bo_flush(bo, mapping);
619 else
620 ret = drv_bo_unmap(bo, mapping);
621
622 return ret;
623 }
624
drv_bo_get_width(struct bo * bo)625 uint32_t drv_bo_get_width(struct bo *bo)
626 {
627 return bo->meta.width;
628 }
629
drv_bo_get_height(struct bo * bo)630 uint32_t drv_bo_get_height(struct bo *bo)
631 {
632 return bo->meta.height;
633 }
634
drv_bo_get_num_planes(struct bo * bo)635 size_t drv_bo_get_num_planes(struct bo *bo)
636 {
637 return bo->meta.num_planes;
638 }
639
drv_bo_get_plane_handle(struct bo * bo,size_t plane)640 union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
641 {
642 return bo->handles[plane];
643 }
644
645 #ifndef DRM_RDWR
646 #define DRM_RDWR O_RDWR
647 #endif
648
drv_bo_get_plane_fd(struct bo * bo,size_t plane)649 int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
650 {
651
652 int ret, fd;
653 assert(plane < bo->meta.num_planes);
654
655 if (bo->is_test_buffer)
656 return -EINVAL;
657
658 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
659
660 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
661 if (ret)
662 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
663
664 if (ret)
665 drv_loge("Failed to get plane fd: %s\n", strerror(errno));
666
667 return (ret) ? ret : fd;
668 }
669
drv_bo_get_plane_offset(struct bo * bo,size_t plane)670 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
671 {
672 assert(plane < bo->meta.num_planes);
673 return bo->meta.offsets[plane];
674 }
675
drv_bo_get_plane_size(struct bo * bo,size_t plane)676 uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
677 {
678 assert(plane < bo->meta.num_planes);
679 return bo->meta.sizes[plane];
680 }
681
drv_bo_get_plane_stride(struct bo * bo,size_t plane)682 uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
683 {
684 assert(plane < bo->meta.num_planes);
685 return bo->meta.strides[plane];
686 }
687
drv_bo_get_format_modifier(struct bo * bo)688 uint64_t drv_bo_get_format_modifier(struct bo *bo)
689 {
690 return bo->meta.format_modifier;
691 }
692
drv_bo_get_format(struct bo * bo)693 uint32_t drv_bo_get_format(struct bo *bo)
694 {
695 return bo->meta.format;
696 }
697
drv_bo_get_tiling(struct bo * bo)698 uint32_t drv_bo_get_tiling(struct bo *bo)
699 {
700 return bo->meta.tiling;
701 }
702
drv_bo_get_use_flags(struct bo * bo)703 uint64_t drv_bo_get_use_flags(struct bo *bo)
704 {
705 return bo->meta.use_flags;
706 }
707
drv_bo_get_total_size(struct bo * bo)708 size_t drv_bo_get_total_size(struct bo *bo)
709 {
710 return bo->meta.total_size;
711 }
712
713 /*
714 * Map internal fourcc codes back to standard fourcc codes.
715 */
drv_get_standard_fourcc(uint32_t fourcc_internal)716 uint32_t drv_get_standard_fourcc(uint32_t fourcc_internal)
717 {
718 return (fourcc_internal == DRM_FORMAT_YVU420_ANDROID) ? DRM_FORMAT_YVU420 : fourcc_internal;
719 }
720
drv_resolve_format_and_use_flags(struct driver * drv,uint32_t format,uint64_t use_flags,uint32_t * out_format,uint64_t * out_use_flags)721 void drv_resolve_format_and_use_flags(struct driver *drv, uint32_t format, uint64_t use_flags,
722 uint32_t *out_format, uint64_t *out_use_flags)
723 {
724 assert(drv->backend->resolve_format_and_use_flags);
725
726 drv->backend->resolve_format_and_use_flags(drv, format, use_flags, out_format,
727 out_use_flags);
728 }
729
drv_num_buffers_per_bo(struct bo * bo)730 uint32_t drv_num_buffers_per_bo(struct bo *bo)
731 {
732 uint32_t count = 0;
733 size_t plane, p;
734
735 if (bo->is_test_buffer)
736 return 0;
737
738 for (plane = 0; plane < bo->meta.num_planes; plane++) {
739 for (p = 0; p < plane; p++)
740 if (bo->handles[p].u32 == bo->handles[plane].u32)
741 break;
742 if (p == plane)
743 count++;
744 }
745
746 return count;
747 }
748
drv_log_prefix(enum drv_log_level level,const char * prefix,const char * file,int line,const char * format,...)749 void drv_log_prefix(enum drv_log_level level, const char *prefix, const char *file, int line,
750 const char *format, ...)
751 {
752 char buf[50];
753 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
754
755 va_list args;
756 va_start(args, format);
757 #ifdef __ANDROID__
758 int prio = ANDROID_LOG_ERROR;
759 switch (level) {
760 case DRV_LOGV:
761 prio = ANDROID_LOG_VERBOSE;
762 break;
763 case DRV_LOGD:
764 prio = ANDROID_LOG_DEBUG;
765 break;
766 case DRV_LOGI:
767 prio = ANDROID_LOG_INFO;
768 break;
769 case DRV_LOGE:
770 default:
771 break;
772 };
773 __android_log_vprint(prio, buf, format, args);
774 #else
775 if (level == DRV_LOGE) {
776 fprintf(stderr, "%s ", buf);
777 vfprintf(stderr, format, args);
778 } else {
779 fprintf(stdout, "%s ", buf);
780 vfprintf(stdout, format, args);
781 }
782 #endif
783 va_end(args);
784 }
785
drv_resource_info(struct bo * bo,uint32_t strides[DRV_MAX_PLANES],uint32_t offsets[DRV_MAX_PLANES],uint64_t * format_modifier)786 int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
787 uint32_t offsets[DRV_MAX_PLANES], uint64_t *format_modifier)
788 {
789 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
790 strides[plane] = bo->meta.strides[plane];
791 offsets[plane] = bo->meta.offsets[plane];
792 }
793 *format_modifier = bo->meta.format_modifier;
794
795 if (bo->drv->backend->resource_info)
796 return bo->drv->backend->resource_info(bo, strides, offsets, format_modifier);
797
798 return 0;
799 }
800
drv_get_max_texture_2d_size(struct driver * drv)801 uint32_t drv_get_max_texture_2d_size(struct driver *drv)
802 {
803 if (drv->backend->get_max_texture_2d_size)
804 return drv->backend->get_max_texture_2d_size(drv);
805
806 return UINT32_MAX;
807 }
808