OSDN Git Service

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