gbm: Axe buffer import format conversion table
[mesa.git] / src / gbm / backends / dri / gbm_dri.c
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <string.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <assert.h>
37
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <dlfcn.h>
41 #include <xf86drm.h>
42 #include <drm_fourcc.h>
43
44 #include <GL/gl.h> /* dri_interface needs GL types */
45 #include <GL/internal/dri_interface.h>
46
47 #include "gbm_driint.h"
48
49 #include "gbmint.h"
50 #include "loader.h"
51
52 /* For importing wl_buffer */
53 #if HAVE_WAYLAND_PLATFORM
54 #include "../../../egl/wayland/wayland-drm/wayland-drm.h"
55 #endif
56
57 #ifndef DRM_FORMAT_MOD_INVALID
58 #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
59 #endif
60
61 #ifndef DRM_FORMAT_MOD_LINEAR
62 #define DRM_FORMAT_MOD_LINEAR 0
63 #endif
64
65 static __DRIimage *
66 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
67 {
68 struct gbm_dri_device *dri = data;
69
70 if (dri->lookup_image == NULL)
71 return NULL;
72
73 return dri->lookup_image(screen, image, dri->lookup_user_data);
74 }
75
76 static __DRIbuffer *
77 dri_get_buffers(__DRIdrawable * driDrawable,
78 int *width, int *height,
79 unsigned int *attachments, int count,
80 int *out_count, void *data)
81 {
82 struct gbm_dri_surface *surf = data;
83 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
84
85 if (dri->get_buffers == NULL)
86 return NULL;
87
88 return dri->get_buffers(driDrawable, width, height, attachments,
89 count, out_count, surf->dri_private);
90 }
91
92 static void
93 dri_flush_front_buffer(__DRIdrawable * driDrawable, void *data)
94 {
95 struct gbm_dri_surface *surf = data;
96 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
97
98 if (dri->flush_front_buffer != NULL)
99 dri->flush_front_buffer(driDrawable, surf->dri_private);
100 }
101
102 static __DRIbuffer *
103 dri_get_buffers_with_format(__DRIdrawable * driDrawable,
104 int *width, int *height,
105 unsigned int *attachments, int count,
106 int *out_count, void *data)
107 {
108 struct gbm_dri_surface *surf = data;
109 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
110
111 if (dri->get_buffers_with_format == NULL)
112 return NULL;
113
114 return
115 dri->get_buffers_with_format(driDrawable, width, height, attachments,
116 count, out_count, surf->dri_private);
117 }
118
119 static int
120 image_get_buffers(__DRIdrawable *driDrawable,
121 unsigned int format,
122 uint32_t *stamp,
123 void *loaderPrivate,
124 uint32_t buffer_mask,
125 struct __DRIimageList *buffers)
126 {
127 struct gbm_dri_surface *surf = loaderPrivate;
128 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
129
130 if (dri->image_get_buffers == NULL)
131 return 0;
132
133 return dri->image_get_buffers(driDrawable, format, stamp,
134 surf->dri_private, buffer_mask, buffers);
135 }
136
137 static void
138 swrast_get_drawable_info(__DRIdrawable *driDrawable,
139 int *x,
140 int *y,
141 int *width,
142 int *height,
143 void *loaderPrivate)
144 {
145 struct gbm_dri_surface *surf = loaderPrivate;
146
147 *x = 0;
148 *y = 0;
149 *width = surf->base.width;
150 *height = surf->base.height;
151 }
152
153 static void
154 swrast_put_image2(__DRIdrawable *driDrawable,
155 int op,
156 int x,
157 int y,
158 int width,
159 int height,
160 int stride,
161 char *data,
162 void *loaderPrivate)
163 {
164 struct gbm_dri_surface *surf = loaderPrivate;
165 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
166
167 dri->swrast_put_image2(driDrawable,
168 op, x, y,
169 width, height, stride,
170 data, surf->dri_private);
171 }
172
173 static void
174 swrast_put_image(__DRIdrawable *driDrawable,
175 int op,
176 int x,
177 int y,
178 int width,
179 int height,
180 char *data,
181 void *loaderPrivate)
182 {
183 return swrast_put_image2(driDrawable, op, x, y, width, height,
184 width * 4, data, loaderPrivate);
185 }
186
187 static void
188 swrast_get_image(__DRIdrawable *driDrawable,
189 int x,
190 int y,
191 int width,
192 int height,
193 char *data,
194 void *loaderPrivate)
195 {
196 struct gbm_dri_surface *surf = loaderPrivate;
197 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
198
199 dri->swrast_get_image(driDrawable,
200 x, y,
201 width, height,
202 data, surf->dri_private);
203 }
204
205 static const __DRIuseInvalidateExtension use_invalidate = {
206 .base = { __DRI_USE_INVALIDATE, 1 }
207 };
208
209 static const __DRIimageLookupExtension image_lookup_extension = {
210 .base = { __DRI_IMAGE_LOOKUP, 1 },
211
212 .lookupEGLImage = dri_lookup_egl_image
213 };
214
215 static const __DRIdri2LoaderExtension dri2_loader_extension = {
216 .base = { __DRI_DRI2_LOADER, 3 },
217
218 .getBuffers = dri_get_buffers,
219 .flushFrontBuffer = dri_flush_front_buffer,
220 .getBuffersWithFormat = dri_get_buffers_with_format,
221 };
222
223 static const __DRIimageLoaderExtension image_loader_extension = {
224 .base = { __DRI_IMAGE_LOADER, 1 },
225
226 .getBuffers = image_get_buffers,
227 .flushFrontBuffer = dri_flush_front_buffer,
228 };
229
230 static const __DRIswrastLoaderExtension swrast_loader_extension = {
231 .base = { __DRI_SWRAST_LOADER, 2 },
232
233 .getDrawableInfo = swrast_get_drawable_info,
234 .putImage = swrast_put_image,
235 .getImage = swrast_get_image,
236 .putImage2 = swrast_put_image2
237 };
238
239 static const __DRIextension *gbm_dri_screen_extensions[] = {
240 &image_lookup_extension.base,
241 &use_invalidate.base,
242 &dri2_loader_extension.base,
243 &image_loader_extension.base,
244 &swrast_loader_extension.base,
245 NULL,
246 };
247
248 struct dri_extension_match {
249 const char *name;
250 int version;
251 int offset;
252 int optional;
253 };
254
255 static struct dri_extension_match dri_core_extensions[] = {
256 { __DRI2_FLUSH, 1, offsetof(struct gbm_dri_device, flush) },
257 { __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image) },
258 { __DRI2_FENCE, 1, offsetof(struct gbm_dri_device, fence), 1 },
259 { NULL, 0, 0 }
260 };
261
262 static struct dri_extension_match gbm_dri_device_extensions[] = {
263 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core) },
264 { __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2) },
265 { NULL, 0, 0 }
266 };
267
268 static struct dri_extension_match gbm_swrast_device_extensions[] = {
269 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), },
270 { __DRI_SWRAST, 1, offsetof(struct gbm_dri_device, swrast) },
271 { NULL, 0, 0 }
272 };
273
274 static int
275 dri_bind_extensions(struct gbm_dri_device *dri,
276 struct dri_extension_match *matches,
277 const __DRIextension **extensions)
278 {
279 int i, j, ret = 0;
280 void *field;
281
282 for (i = 0; extensions[i]; i++) {
283 for (j = 0; matches[j].name; j++) {
284 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
285 extensions[i]->version >= matches[j].version) {
286 field = ((char *) dri + matches[j].offset);
287 *(const __DRIextension **) field = extensions[i];
288 }
289 }
290 }
291
292 for (j = 0; matches[j].name; j++) {
293 field = ((char *) dri + matches[j].offset);
294 if ((*(const __DRIextension **) field == NULL) && !matches[j].optional) {
295 ret = -1;
296 }
297 }
298
299 return ret;
300 }
301
302 static const __DRIextension **
303 dri_open_driver(struct gbm_dri_device *dri)
304 {
305 const __DRIextension **extensions = NULL;
306 char path[PATH_MAX], *search_paths, *p, *next, *end;
307 char *get_extensions_name;
308
309 search_paths = NULL;
310 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH or GBM_DRIVERS_PATH */
311 if (geteuid() == getuid()) {
312 /* Read GBM_DRIVERS_PATH first for compatibility, but LIBGL_DRIVERS_PATH
313 * is recommended over GBM_DRIVERS_PATH.
314 */
315 search_paths = getenv("GBM_DRIVERS_PATH");
316
317 /* Read LIBGL_DRIVERS_PATH if GBM_DRIVERS_PATH was not set.
318 * LIBGL_DRIVERS_PATH is recommended over GBM_DRIVERS_PATH.
319 */
320 if (search_paths == NULL) {
321 search_paths = getenv("LIBGL_DRIVERS_PATH");
322 }
323 }
324 if (search_paths == NULL)
325 search_paths = DEFAULT_DRIVER_DIR;
326
327 /* Temporarily work around dri driver libs that need symbols in libglapi
328 * but don't automatically link it in.
329 */
330 /* XXX: Library name differs on per platforms basis. Update this as
331 * osx/cygwin/windows/bsd gets support for GBM..
332 */
333 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
334
335 dri->driver = NULL;
336 end = search_paths + strlen(search_paths);
337 for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
338 int len;
339 next = strchr(p, ':');
340 if (next == NULL)
341 next = end;
342
343 len = next - p;
344 #if GLX_USE_TLS
345 snprintf(path, sizeof path,
346 "%.*s/tls/%s_dri.so", len, p, dri->driver_name);
347 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
348 #endif
349 if (dri->driver == NULL) {
350 snprintf(path, sizeof path,
351 "%.*s/%s_dri.so", len, p, dri->driver_name);
352 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
353 }
354 /* not need continue to loop all paths once the driver is found */
355 if (dri->driver != NULL)
356 break;
357 }
358
359 if (dri->driver == NULL) {
360 fprintf(stderr, "gbm: failed to open any driver (search paths %s)\n",
361 search_paths);
362 fprintf(stderr, "gbm: Last dlopen error: %s\n", dlerror());
363 return NULL;
364 }
365
366 get_extensions_name = loader_get_extensions_name(dri->driver_name);
367 if (get_extensions_name) {
368 const __DRIextension **(*get_extensions)(void);
369
370 get_extensions = dlsym(dri->driver, get_extensions_name);
371 free(get_extensions_name);
372
373 if (get_extensions)
374 extensions = get_extensions();
375 }
376
377 if (!extensions)
378 extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
379 if (extensions == NULL) {
380 fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
381 dlclose(dri->driver);
382 }
383
384 return extensions;
385 }
386
387 static int
388 dri_load_driver(struct gbm_dri_device *dri)
389 {
390 const __DRIextension **extensions;
391
392 extensions = dri_open_driver(dri);
393 if (!extensions)
394 return -1;
395
396 if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
397 dlclose(dri->driver);
398 fprintf(stderr, "failed to bind extensions\n");
399 return -1;
400 }
401
402 dri->driver_extensions = extensions;
403
404 return 0;
405 }
406
407 static int
408 dri_load_driver_swrast(struct gbm_dri_device *dri)
409 {
410 const __DRIextension **extensions;
411
412 extensions = dri_open_driver(dri);
413 if (!extensions)
414 return -1;
415
416 if (dri_bind_extensions(dri, gbm_swrast_device_extensions, extensions) < 0) {
417 dlclose(dri->driver);
418 fprintf(stderr, "failed to bind extensions\n");
419 return -1;
420 }
421
422 dri->driver_extensions = extensions;
423
424 return 0;
425 }
426
427 static int
428 dri_screen_create_dri2(struct gbm_dri_device *dri, char *driver_name)
429 {
430 const __DRIextension **extensions;
431 int ret = 0;
432
433 dri->driver_name = driver_name;
434 if (dri->driver_name == NULL)
435 return -1;
436
437 ret = dri_load_driver(dri);
438 if (ret) {
439 fprintf(stderr, "failed to load driver: %s\n", dri->driver_name);
440 return ret;
441 };
442
443 dri->loader_extensions = gbm_dri_screen_extensions;
444
445 if (dri->dri2 == NULL)
446 return -1;
447
448 if (dri->dri2->base.version >= 4) {
449 dri->screen = dri->dri2->createNewScreen2(0, dri->base.fd,
450 dri->loader_extensions,
451 dri->driver_extensions,
452 &dri->driver_configs, dri);
453 } else {
454 dri->screen = dri->dri2->createNewScreen(0, dri->base.fd,
455 dri->loader_extensions,
456 &dri->driver_configs, dri);
457 }
458 if (dri->screen == NULL)
459 return -1;
460
461 extensions = dri->core->getExtensions(dri->screen);
462 if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
463 ret = -1;
464 goto free_screen;
465 }
466
467 dri->lookup_image = NULL;
468 dri->lookup_user_data = NULL;
469
470 return 0;
471
472 free_screen:
473 dri->core->destroyScreen(dri->screen);
474
475 return ret;
476 }
477
478 static int
479 dri_screen_create_swrast(struct gbm_dri_device *dri)
480 {
481 int ret;
482
483 dri->driver_name = strdup("swrast");
484 if (dri->driver_name == NULL)
485 return -1;
486
487 ret = dri_load_driver_swrast(dri);
488 if (ret) {
489 fprintf(stderr, "failed to load swrast driver\n");
490 return ret;
491 }
492
493 dri->loader_extensions = gbm_dri_screen_extensions;
494
495 if (dri->swrast == NULL)
496 return -1;
497
498 if (dri->swrast->base.version >= 4) {
499 dri->screen = dri->swrast->createNewScreen2(0, dri->loader_extensions,
500 dri->driver_extensions,
501 &dri->driver_configs, dri);
502 } else {
503 dri->screen = dri->swrast->createNewScreen(0, dri->loader_extensions,
504 &dri->driver_configs, dri);
505 }
506 if (dri->screen == NULL)
507 return -1;
508
509 dri->lookup_image = NULL;
510 dri->lookup_user_data = NULL;
511
512 return 0;
513 }
514
515 static int
516 dri_screen_create(struct gbm_dri_device *dri)
517 {
518 char *driver_name;
519
520 driver_name = loader_get_driver_for_fd(dri->base.fd);
521 if (!driver_name)
522 return -1;
523
524 return dri_screen_create_dri2(dri, driver_name);
525 }
526
527 static int
528 dri_screen_create_sw(struct gbm_dri_device *dri)
529 {
530 char *driver_name;
531 int ret;
532
533 driver_name = strdup("kms_swrast");
534 if (!driver_name)
535 return -errno;
536
537 ret = dri_screen_create_dri2(dri, driver_name);
538 if (ret == 0)
539 return ret;
540
541 return dri_screen_create_swrast(dri);
542 }
543
544 static int
545 gbm_dri_is_format_supported(struct gbm_device *gbm,
546 uint32_t format,
547 uint32_t usage)
548 {
549 switch (format) {
550 case GBM_BO_FORMAT_XRGB8888:
551 case GBM_FORMAT_XBGR8888:
552 case GBM_FORMAT_XRGB8888:
553 break;
554 case GBM_BO_FORMAT_ARGB8888:
555 case GBM_FORMAT_ARGB8888:
556 if (usage & GBM_BO_USE_SCANOUT)
557 return 0;
558 break;
559 default:
560 return 0;
561 }
562
563 if (usage & GBM_BO_USE_CURSOR &&
564 usage & GBM_BO_USE_RENDERING)
565 return 0;
566
567 return 1;
568 }
569
570 static int
571 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
572 {
573 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
574
575 if (bo->image != NULL) {
576 errno = EINVAL;
577 return -1;
578 }
579
580 memcpy(bo->map, buf, count);
581
582 return 0;
583 }
584
585 static int
586 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
587 {
588 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
589 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
590 int fd;
591
592 if (bo->image == NULL)
593 return -1;
594
595 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd))
596 return -1;
597
598 return fd;
599 }
600
601 static int
602 get_number_planes(struct gbm_dri_device *dri, __DRIimage *image)
603 {
604 int num_planes = 0;
605
606 /* Dumb buffers are single-plane only. */
607 if (!image)
608 return 1;
609
610 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
611
612 if (num_planes <= 0)
613 num_planes = 1;
614
615 return num_planes;
616 }
617
618 static int
619 gbm_dri_bo_get_planes(struct gbm_bo *_bo)
620 {
621 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
622 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
623
624 return get_number_planes(dri, bo->image);
625 }
626
627 static union gbm_bo_handle
628 gbm_dri_bo_get_handle_for_plane(struct gbm_bo *_bo, int plane)
629 {
630 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
631 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
632 union gbm_bo_handle ret;
633 ret.s32 = -1;
634
635 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
636 errno = ENOSYS;
637 return ret;
638 }
639
640 if (plane >= get_number_planes(dri, bo->image)) {
641 errno = EINVAL;
642 return ret;
643 }
644
645 /* dumb BOs can only utilize non-planar formats */
646 if (!bo->image) {
647 assert(plane == 0);
648 ret.s32 = bo->handle;
649 return ret;
650 }
651
652 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
653 if (image) {
654 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
655 dri->image->destroyImage(image);
656 } else {
657 assert(plane == 0);
658 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
659 }
660
661 return ret;
662 }
663
664 static uint32_t
665 gbm_dri_bo_get_stride(struct gbm_bo *_bo, int plane)
666 {
667 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
668 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
669 __DRIimage *image;
670 int stride = 0;
671
672 if (!dri->image || dri->image->base.version < 11 || !dri->image->fromPlanar) {
673 /* Preserve legacy behavior if plane is 0 */
674 if (plane == 0)
675 return _bo->stride;
676
677 errno = ENOSYS;
678 return 0;
679 }
680
681 if (plane >= get_number_planes(dri, bo->image)) {
682 errno = EINVAL;
683 return 0;
684 }
685
686 if (bo->image == NULL) {
687 assert(plane == 0);
688 return _bo->stride;
689 }
690
691 image = dri->image->fromPlanar(bo->image, plane, NULL);
692 if (image) {
693 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
694 dri->image->destroyImage(image);
695 } else {
696 assert(plane == 0);
697 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
698 }
699
700 return (uint32_t)stride;
701 }
702
703 static uint32_t
704 gbm_dri_bo_get_offset(struct gbm_bo *_bo, int plane)
705 {
706 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
707 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
708 int offset = 0;
709
710 /* These error cases do not actually return an error code, as the user
711 * will also fail to obtain the handle/FD from the BO. In that case, the
712 * offset is irrelevant, as they have no buffer to offset into, so
713 * returning 0 is harmless.
714 */
715 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar)
716 return 0;
717
718 if (plane >= get_number_planes(dri, bo->image))
719 return 0;
720
721 /* Dumb images have no offset */
722 if (bo->image == NULL) {
723 assert(plane == 0);
724 return 0;
725 }
726
727 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
728 if (image) {
729 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
730 dri->image->destroyImage(image);
731 } else {
732 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
733 }
734
735 return (uint32_t)offset;
736 }
737
738 static uint64_t
739 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
740 {
741 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
742 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
743
744 if (!dri->image || dri->image->base.version < 14) {
745 errno = ENOSYS;
746 return DRM_FORMAT_MOD_INVALID;
747 }
748
749 /* Dumb buffers have no modifiers */
750 if (!bo->image)
751 return DRM_FORMAT_MOD_LINEAR;
752
753 uint64_t ret = 0;
754 int mod;
755 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
756 &mod))
757 return DRM_FORMAT_MOD_INVALID;
758
759 ret = (uint64_t)mod << 32;
760
761 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
762 &mod))
763 return DRM_FORMAT_MOD_INVALID;
764
765 ret |= (uint64_t)(mod & 0xffffffff);
766
767 return ret;
768 }
769
770 static void
771 gbm_dri_bo_destroy(struct gbm_bo *_bo)
772 {
773 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
774 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
775 struct drm_mode_destroy_dumb arg;
776
777 if (bo->image != NULL) {
778 dri->image->destroyImage(bo->image);
779 } else {
780 gbm_dri_bo_unmap_dumb(bo);
781 memset(&arg, 0, sizeof(arg));
782 arg.handle = bo->handle;
783 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
784 }
785
786 free(bo);
787 }
788
789 static uint32_t
790 gbm_dri_to_gbm_format(uint32_t dri_format)
791 {
792 uint32_t ret = 0;
793
794 switch (dri_format) {
795 case __DRI_IMAGE_FORMAT_RGB565:
796 ret = GBM_FORMAT_RGB565;
797 break;
798 case __DRI_IMAGE_FORMAT_XRGB8888:
799 ret = GBM_FORMAT_XRGB8888;
800 break;
801 case __DRI_IMAGE_FORMAT_ARGB8888:
802 ret = GBM_FORMAT_ARGB8888;
803 break;
804 case __DRI_IMAGE_FORMAT_XBGR8888:
805 ret = GBM_FORMAT_XBGR8888;
806 break;
807 case __DRI_IMAGE_FORMAT_ABGR8888:
808 ret = GBM_FORMAT_ABGR8888;
809 break;
810 default:
811 ret = 0;
812 break;
813 }
814
815 return ret;
816 }
817
818 static struct gbm_bo *
819 gbm_dri_bo_import(struct gbm_device *gbm,
820 uint32_t type, void *buffer, uint32_t usage)
821 {
822 struct gbm_dri_device *dri = gbm_dri_device(gbm);
823 struct gbm_dri_bo *bo;
824 __DRIimage *image;
825 unsigned dri_use = 0;
826 int gbm_format;
827
828 /* Required for query image WIDTH & HEIGHT */
829 if (dri->image == NULL || dri->image->base.version < 4) {
830 errno = ENOSYS;
831 return NULL;
832 }
833
834 switch (type) {
835 #if HAVE_WAYLAND_PLATFORM
836 case GBM_BO_IMPORT_WL_BUFFER:
837 {
838 struct wl_drm_buffer *wb;
839
840 if (!dri->wl_drm) {
841 errno = EINVAL;
842 return NULL;
843 }
844
845 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
846 if (!wb) {
847 errno = EINVAL;
848 return NULL;
849 }
850
851 image = dri->image->dupImage(wb->driver_buffer, NULL);
852
853 /* GBM_FORMAT_* is identical to WL_DRM_FORMAT_*, so no conversion
854 * required. */
855 gbm_format = wb->format;
856 break;
857 }
858 #endif
859
860 case GBM_BO_IMPORT_EGL_IMAGE:
861 {
862 int dri_format;
863 if (dri->lookup_image == NULL) {
864 errno = EINVAL;
865 return NULL;
866 }
867
868 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
869 image = dri->image->dupImage(image, NULL);
870 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
871 gbm_format = gbm_dri_to_gbm_format(dri_format);
872 if (gbm_format == 0) {
873 errno = EINVAL;
874 dri->image->destroyImage(image);
875 return NULL;
876 }
877 break;
878 }
879
880 case GBM_BO_IMPORT_FD:
881 {
882 struct gbm_import_fd_data *fd_data = buffer;
883 int stride = fd_data->stride, offset = 0;
884 int fourcc;
885
886 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
887 * tokens accepted by createImageFromFds, except for not supporting
888 * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
889 * their GBM_FORMAT_* equivalents, so remap them here. */
890 switch (fd_data->format) {
891 case GBM_BO_FORMAT_XRGB8888:
892 fourcc = GBM_FORMAT_XRGB8888;
893 break;
894 case GBM_BO_FORMAT_ARGB8888:
895 fourcc = GBM_FORMAT_ARGB8888;
896 break;
897 default:
898 fourcc = fd_data->format;
899 }
900
901 image = dri->image->createImageFromFds(dri->screen,
902 fd_data->width,
903 fd_data->height,
904 fourcc,
905 &fd_data->fd, 1,
906 &stride, &offset,
907 NULL);
908 if (image == NULL) {
909 errno = EINVAL;
910 return NULL;
911 }
912 gbm_format = fd_data->format;
913 break;
914 }
915
916 case GBM_BO_IMPORT_FD_MODIFIER:
917 {
918 struct gbm_import_fd_modifier_data *fd_data = buffer;
919 unsigned int error;
920 int fourcc;
921
922 /* Import with modifier requires createImageFromDmaBufs2 */
923 if (dri->image == NULL || dri->image->base.version < 15 ||
924 dri->image->createImageFromDmaBufs2 == NULL) {
925 errno = ENOSYS;
926 return NULL;
927 }
928
929 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
930 * tokens accepted by createImageFromDmaBufs2, except for not supporting
931 * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
932 * their GBM_FORMAT_* equivalents, so remap them here. */
933 switch (fd_data->format) {
934 case GBM_BO_FORMAT_XRGB8888:
935 fourcc = GBM_FORMAT_XRGB8888;
936 break;
937 case GBM_BO_FORMAT_ARGB8888:
938 fourcc = GBM_FORMAT_ARGB8888;
939 break;
940 default:
941 fourcc = fd_data->format;
942 }
943
944 image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
945 fd_data->height, fourcc,
946 fd_data->modifier,
947 fd_data->fds,
948 fd_data->num_fds,
949 fd_data->strides,
950 fd_data->offsets,
951 0, 0, 0, 0,
952 &error, NULL);
953 if (image == NULL) {
954 errno = ENOSYS;
955 return NULL;
956 }
957
958 gbm_format = fourcc;
959 break;
960 }
961
962 default:
963 errno = ENOSYS;
964 return NULL;
965 }
966
967
968 bo = calloc(1, sizeof *bo);
969 if (bo == NULL) {
970 dri->image->destroyImage(image);
971 return NULL;
972 }
973
974 bo->image = image;
975
976 if (usage & GBM_BO_USE_SCANOUT)
977 dri_use |= __DRI_IMAGE_USE_SCANOUT;
978 if (usage & GBM_BO_USE_CURSOR)
979 dri_use |= __DRI_IMAGE_USE_CURSOR;
980 if (dri->image->base.version >= 2 &&
981 !dri->image->validateUsage(bo->image, dri_use)) {
982 errno = EINVAL;
983 dri->image->destroyImage(bo->image);
984 free(bo);
985 return NULL;
986 }
987
988 bo->base.gbm = gbm;
989 bo->base.format = gbm_format;
990
991 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
992 (int*)&bo->base.width);
993 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
994 (int*)&bo->base.height);
995 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
996 (int*)&bo->base.stride);
997 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
998 &bo->base.handle.s32);
999
1000 return &bo->base;
1001 }
1002
1003 static bool
1004 is_planar_format(uint32_t format)
1005 {
1006 switch (format) {
1007 case GBM_FORMAT_NV12:
1008 case GBM_FORMAT_NV21:
1009 case GBM_FORMAT_NV16:
1010 case GBM_FORMAT_NV61:
1011 case GBM_FORMAT_YUV410:
1012 case GBM_FORMAT_YVU410:
1013 case GBM_FORMAT_YUV411:
1014 case GBM_FORMAT_YVU411:
1015 case GBM_FORMAT_YUV420:
1016 case GBM_FORMAT_YVU420:
1017 case GBM_FORMAT_YUV422:
1018 case GBM_FORMAT_YVU422:
1019 case GBM_FORMAT_YUV444:
1020 case GBM_FORMAT_YVU444:
1021 return true;
1022 default:
1023 return false;
1024 }
1025
1026 }
1027
1028 static struct gbm_bo *
1029 create_dumb(struct gbm_device *gbm,
1030 uint32_t width, uint32_t height,
1031 uint32_t format, uint32_t usage)
1032 {
1033 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1034 struct drm_mode_create_dumb create_arg;
1035 struct gbm_dri_bo *bo;
1036 struct drm_mode_destroy_dumb destroy_arg;
1037 int ret;
1038 int is_cursor, is_scanout;
1039
1040 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1041 format == GBM_FORMAT_ARGB8888;
1042 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1043 (format == GBM_FORMAT_XRGB8888 || format == GBM_FORMAT_XBGR8888);
1044 if (!is_cursor && !is_scanout) {
1045 errno = EINVAL;
1046 return NULL;
1047 }
1048
1049 if (is_planar_format(format)) {
1050 errno = EINVAL;
1051 return NULL;
1052 }
1053
1054 bo = calloc(1, sizeof *bo);
1055 if (bo == NULL)
1056 return NULL;
1057
1058 memset(&create_arg, 0, sizeof(create_arg));
1059 create_arg.bpp = 32;
1060 create_arg.width = width;
1061 create_arg.height = height;
1062
1063 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1064 if (ret)
1065 goto free_bo;
1066
1067 bo->base.gbm = gbm;
1068 bo->base.width = width;
1069 bo->base.height = height;
1070 bo->base.stride = create_arg.pitch;
1071 bo->base.format = format;
1072 bo->base.handle.u32 = create_arg.handle;
1073 bo->handle = create_arg.handle;
1074 bo->size = create_arg.size;
1075
1076 if (gbm_dri_bo_map_dumb(bo) == NULL)
1077 goto destroy_dumb;
1078
1079 return &bo->base;
1080
1081 destroy_dumb:
1082 memset(&destroy_arg, 0, sizeof destroy_arg);
1083 destroy_arg.handle = create_arg.handle;
1084 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1085 free_bo:
1086 free(bo);
1087
1088 return NULL;
1089 }
1090
1091 static struct gbm_bo *
1092 gbm_dri_bo_create(struct gbm_device *gbm,
1093 uint32_t width, uint32_t height,
1094 uint32_t format, uint32_t usage,
1095 const uint64_t *modifiers,
1096 const unsigned int count)
1097 {
1098 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1099 struct gbm_dri_bo *bo;
1100 int dri_format;
1101 unsigned dri_use = 0;
1102
1103 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1104 * newer modifier interface deprecates the older usage flags.
1105 */
1106 assert(!(usage && count));
1107
1108 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1109 return create_dumb(gbm, width, height, format, usage);
1110
1111 bo = calloc(1, sizeof *bo);
1112 if (bo == NULL)
1113 return NULL;
1114
1115 bo->base.gbm = gbm;
1116 bo->base.width = width;
1117 bo->base.height = height;
1118 bo->base.format = format;
1119
1120 switch (format) {
1121 case GBM_FORMAT_R8:
1122 dri_format = __DRI_IMAGE_FORMAT_R8;
1123 break;
1124 case GBM_FORMAT_GR88:
1125 dri_format = __DRI_IMAGE_FORMAT_GR88;
1126 break;
1127 case GBM_FORMAT_RGB565:
1128 dri_format = __DRI_IMAGE_FORMAT_RGB565;
1129 break;
1130 case GBM_FORMAT_XRGB8888:
1131 case GBM_BO_FORMAT_XRGB8888:
1132 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
1133 break;
1134 case GBM_FORMAT_ARGB8888:
1135 case GBM_BO_FORMAT_ARGB8888:
1136 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
1137 break;
1138 case GBM_FORMAT_ABGR8888:
1139 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
1140 break;
1141 case GBM_FORMAT_XBGR8888:
1142 dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
1143 break;
1144 case GBM_FORMAT_ARGB2101010:
1145 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
1146 break;
1147 case GBM_FORMAT_XRGB2101010:
1148 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
1149 break;
1150 default:
1151 errno = EINVAL;
1152 goto failed;
1153 }
1154
1155 if (usage & GBM_BO_USE_SCANOUT)
1156 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1157 if (usage & GBM_BO_USE_CURSOR)
1158 dri_use |= __DRI_IMAGE_USE_CURSOR;
1159 if (usage & GBM_BO_USE_LINEAR)
1160 dri_use |= __DRI_IMAGE_USE_LINEAR;
1161
1162 /* Gallium drivers requires shared in order to get the handle/stride */
1163 dri_use |= __DRI_IMAGE_USE_SHARE;
1164
1165 if (modifiers) {
1166 if (!dri->image || dri->image->base.version < 14 ||
1167 !dri->image->createImageWithModifiers) {
1168 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1169 errno = ENOSYS;
1170 goto failed;
1171 }
1172
1173 /* It's acceptable to create an image with INVALID modifier in the list,
1174 * but it cannot be on the only modifier (since it will certainly fail
1175 * later). While we could easily catch this after modifier creation, doing
1176 * the check here is a convenient debug check likely pointing at whatever
1177 * interface the client is using to build its modifier list.
1178 */
1179 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1180 fprintf(stderr, "Only invalid modifier specified\n");
1181 errno = EINVAL;
1182 goto failed;
1183 }
1184
1185 bo->image =
1186 dri->image->createImageWithModifiers(dri->screen,
1187 width, height,
1188 dri_format,
1189 modifiers, count,
1190 bo);
1191
1192 if (bo->image) {
1193 /* The client passed in a list of invalid modifiers */
1194 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1195 }
1196 } else {
1197 bo->image = dri->image->createImage(dri->screen, width, height,
1198 dri_format, dri_use, bo);
1199 }
1200
1201 if (bo->image == NULL)
1202 goto failed;
1203
1204 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1205 &bo->base.handle.s32);
1206 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1207 (int *) &bo->base.stride);
1208
1209 return &bo->base;
1210
1211 failed:
1212 free(bo);
1213 return NULL;
1214 }
1215
1216 static void *
1217 gbm_dri_bo_map(struct gbm_bo *_bo,
1218 uint32_t x, uint32_t y,
1219 uint32_t width, uint32_t height,
1220 uint32_t flags, uint32_t *stride, void **map_data)
1221 {
1222 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1223 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1224
1225 /* If it's a dumb buffer, we already have a mapping */
1226 if (bo->map) {
1227 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1228 *stride = bo->base.stride;
1229 return *map_data;
1230 }
1231
1232 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1233 errno = ENOSYS;
1234 return NULL;
1235 }
1236
1237 mtx_lock(&dri->mutex);
1238 if (!dri->context)
1239 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1240 NULL, NULL);
1241 assert(dri->context);
1242 mtx_unlock(&dri->mutex);
1243
1244 /* GBM flags and DRI flags are the same, so just pass them on */
1245 return dri->image->mapImage(dri->context, bo->image, x, y,
1246 width, height, flags, (int *)stride,
1247 map_data);
1248 }
1249
1250 static void
1251 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1252 {
1253 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1254 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1255
1256 /* Check if it's a dumb buffer and check the pointer is in range */
1257 if (bo->map) {
1258 assert(map_data >= bo->map);
1259 assert(map_data < (bo->map + bo->size));
1260 return;
1261 }
1262
1263 if (!dri->context || !dri->image ||
1264 dri->image->base.version < 12 || !dri->image->unmapImage)
1265 return;
1266
1267 dri->image->unmapImage(dri->context, bo->image, map_data);
1268
1269 /*
1270 * Not all DRI drivers use direct maps. They may queue up DMA operations
1271 * on the mapping context. Since there is no explicit gbm flush
1272 * mechanism, we need to flush here.
1273 */
1274 if (dri->flush->base.version >= 4)
1275 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1276 }
1277
1278
1279 static struct gbm_surface *
1280 gbm_dri_surface_create(struct gbm_device *gbm,
1281 uint32_t width, uint32_t height,
1282 uint32_t format, uint32_t flags,
1283 const uint64_t *modifiers, const unsigned count)
1284 {
1285 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1286 struct gbm_dri_surface *surf;
1287
1288 if (modifiers &&
1289 (!dri->image || dri->image->base.version < 14 ||
1290 !dri->image->createImageWithModifiers)) {
1291 errno = ENOSYS;
1292 return NULL;
1293 }
1294
1295 if (count)
1296 assert(modifiers);
1297
1298 /* It's acceptable to create an image with INVALID modifier in the list,
1299 * but it cannot be on the only modifier (since it will certainly fail
1300 * later). While we could easily catch this after modifier creation, doing
1301 * the check here is a convenient debug check likely pointing at whatever
1302 * interface the client is using to build its modifier list.
1303 */
1304 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1305 fprintf(stderr, "Only invalid modifier specified\n");
1306 errno = EINVAL;
1307 }
1308
1309 surf = calloc(1, sizeof *surf);
1310 if (surf == NULL) {
1311 errno = ENOMEM;
1312 return NULL;
1313 }
1314
1315 surf->base.gbm = gbm;
1316 surf->base.width = width;
1317 surf->base.height = height;
1318 surf->base.format = format;
1319 surf->base.flags = flags;
1320 if (!modifiers) {
1321 assert(!count);
1322 return &surf->base;
1323 }
1324
1325 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1326 if (count && !surf->base.modifiers) {
1327 errno = ENOMEM;
1328 free(surf);
1329 return NULL;
1330 }
1331
1332 /* TODO: We are deferring validation of modifiers until the image is actually
1333 * created. This deferred creation can fail due to a modifier-format
1334 * mismatch. The result is the client has a surface but no object to back it.
1335 */
1336 surf->base.count = count;
1337 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1338
1339 return &surf->base;
1340 }
1341
1342 static void
1343 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1344 {
1345 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1346
1347 free(surf->base.modifiers);
1348 free(surf);
1349 }
1350
1351 static void
1352 dri_destroy(struct gbm_device *gbm)
1353 {
1354 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1355 unsigned i;
1356
1357 if (dri->context)
1358 dri->core->destroyContext(dri->context);
1359
1360 dri->core->destroyScreen(dri->screen);
1361 for (i = 0; dri->driver_configs[i]; i++)
1362 free((__DRIconfig *) dri->driver_configs[i]);
1363 free(dri->driver_configs);
1364 dlclose(dri->driver);
1365 free(dri->driver_name);
1366
1367 free(dri);
1368 }
1369
1370 static struct gbm_device *
1371 dri_device_create(int fd)
1372 {
1373 struct gbm_dri_device *dri;
1374 int ret, force_sw;
1375
1376 dri = calloc(1, sizeof *dri);
1377 if (!dri)
1378 return NULL;
1379
1380 dri->base.fd = fd;
1381 dri->base.bo_create = gbm_dri_bo_create;
1382 dri->base.bo_import = gbm_dri_bo_import;
1383 dri->base.bo_map = gbm_dri_bo_map;
1384 dri->base.bo_unmap = gbm_dri_bo_unmap;
1385 dri->base.is_format_supported = gbm_dri_is_format_supported;
1386 dri->base.bo_write = gbm_dri_bo_write;
1387 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1388 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1389 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1390 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1391 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1392 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1393 dri->base.bo_destroy = gbm_dri_bo_destroy;
1394 dri->base.destroy = dri_destroy;
1395 dri->base.surface_create = gbm_dri_surface_create;
1396 dri->base.surface_destroy = gbm_dri_surface_destroy;
1397
1398 dri->base.name = "drm";
1399
1400 mtx_init(&dri->mutex, mtx_plain);
1401
1402 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1403 if (!force_sw) {
1404 ret = dri_screen_create(dri);
1405 if (ret)
1406 ret = dri_screen_create_sw(dri);
1407 } else {
1408 ret = dri_screen_create_sw(dri);
1409 }
1410
1411 if (ret)
1412 goto err_dri;
1413
1414 return &dri->base;
1415
1416 err_dri:
1417 free(dri);
1418
1419 return NULL;
1420 }
1421
1422 struct gbm_backend gbm_dri_backend = {
1423 .backend_name = "dri",
1424 .create_device = dri_device_create,
1425 };