7fb569078b27cdd7e620ad092814124105f905b8
[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 #ifdef ANDROID
359 snprintf(path, sizeof path, "%.*s/gallium_dri.so", len, p);
360 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
361 if (dri->driver == NULL)
362 sprintf("failed to open %s: %s\n", path, dlerror());
363 else
364 break;
365 #endif
366 }
367
368 if (dri->driver == NULL) {
369 fprintf(stderr, "gbm: failed to open any driver (search paths %s)\n",
370 search_paths);
371 fprintf(stderr, "gbm: Last dlopen error: %s\n", dlerror());
372 return NULL;
373 }
374
375 get_extensions_name = loader_get_extensions_name(dri->driver_name);
376 if (get_extensions_name) {
377 const __DRIextension **(*get_extensions)(void);
378
379 get_extensions = dlsym(dri->driver, get_extensions_name);
380 free(get_extensions_name);
381
382 if (get_extensions)
383 extensions = get_extensions();
384 }
385
386 if (!extensions)
387 extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
388 if (extensions == NULL) {
389 fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
390 dlclose(dri->driver);
391 }
392
393 return extensions;
394 }
395
396 static int
397 dri_load_driver(struct gbm_dri_device *dri)
398 {
399 const __DRIextension **extensions;
400
401 extensions = dri_open_driver(dri);
402 if (!extensions)
403 return -1;
404
405 if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
406 dlclose(dri->driver);
407 fprintf(stderr, "failed to bind extensions\n");
408 return -1;
409 }
410
411 dri->driver_extensions = extensions;
412
413 return 0;
414 }
415
416 static int
417 dri_load_driver_swrast(struct gbm_dri_device *dri)
418 {
419 const __DRIextension **extensions;
420
421 extensions = dri_open_driver(dri);
422 if (!extensions)
423 return -1;
424
425 if (dri_bind_extensions(dri, gbm_swrast_device_extensions, extensions) < 0) {
426 dlclose(dri->driver);
427 fprintf(stderr, "failed to bind extensions\n");
428 return -1;
429 }
430
431 dri->driver_extensions = extensions;
432
433 return 0;
434 }
435
436 static int
437 dri_screen_create_dri2(struct gbm_dri_device *dri, char *driver_name)
438 {
439 const __DRIextension **extensions;
440 int ret = 0;
441
442 dri->driver_name = driver_name;
443 if (dri->driver_name == NULL)
444 return -1;
445
446 ret = dri_load_driver(dri);
447 if (ret) {
448 fprintf(stderr, "failed to load driver: %s\n", dri->driver_name);
449 return ret;
450 };
451
452 dri->loader_extensions = gbm_dri_screen_extensions;
453
454 if (dri->dri2 == NULL)
455 return -1;
456
457 if (dri->dri2->base.version >= 4) {
458 dri->screen = dri->dri2->createNewScreen2(0, dri->base.fd,
459 dri->loader_extensions,
460 dri->driver_extensions,
461 &dri->driver_configs, dri);
462 } else {
463 dri->screen = dri->dri2->createNewScreen(0, dri->base.fd,
464 dri->loader_extensions,
465 &dri->driver_configs, dri);
466 }
467 if (dri->screen == NULL)
468 return -1;
469
470 extensions = dri->core->getExtensions(dri->screen);
471 if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
472 ret = -1;
473 goto free_screen;
474 }
475
476 dri->lookup_image = NULL;
477 dri->lookup_user_data = NULL;
478
479 return 0;
480
481 free_screen:
482 dri->core->destroyScreen(dri->screen);
483
484 return ret;
485 }
486
487 static int
488 dri_screen_create_swrast(struct gbm_dri_device *dri)
489 {
490 int ret;
491
492 dri->driver_name = strdup("swrast");
493 if (dri->driver_name == NULL)
494 return -1;
495
496 ret = dri_load_driver_swrast(dri);
497 if (ret) {
498 fprintf(stderr, "failed to load swrast driver\n");
499 return ret;
500 }
501
502 dri->loader_extensions = gbm_dri_screen_extensions;
503
504 if (dri->swrast == NULL)
505 return -1;
506
507 if (dri->swrast->base.version >= 4) {
508 dri->screen = dri->swrast->createNewScreen2(0, dri->loader_extensions,
509 dri->driver_extensions,
510 &dri->driver_configs, dri);
511 } else {
512 dri->screen = dri->swrast->createNewScreen(0, dri->loader_extensions,
513 &dri->driver_configs, dri);
514 }
515 if (dri->screen == NULL)
516 return -1;
517
518 dri->lookup_image = NULL;
519 dri->lookup_user_data = NULL;
520
521 return 0;
522 }
523
524 static int
525 dri_screen_create(struct gbm_dri_device *dri)
526 {
527 char *driver_name;
528
529 driver_name = loader_get_driver_for_fd(dri->base.fd);
530 if (!driver_name)
531 return -1;
532
533 return dri_screen_create_dri2(dri, driver_name);
534 }
535
536 static int
537 dri_screen_create_sw(struct gbm_dri_device *dri)
538 {
539 char *driver_name;
540 int ret;
541
542 driver_name = strdup("kms_swrast");
543 if (!driver_name)
544 return -errno;
545
546 ret = dri_screen_create_dri2(dri, driver_name);
547 if (ret == 0)
548 return ret;
549
550 return dri_screen_create_swrast(dri);
551 }
552
553 static int
554 gbm_dri_is_format_supported(struct gbm_device *gbm,
555 uint32_t format,
556 uint32_t usage)
557 {
558 switch (format) {
559 case GBM_BO_FORMAT_XRGB8888:
560 case GBM_FORMAT_XBGR8888:
561 case GBM_FORMAT_XRGB8888:
562 break;
563 case GBM_BO_FORMAT_ARGB8888:
564 case GBM_FORMAT_ARGB8888:
565 if (usage & GBM_BO_USE_SCANOUT)
566 return 0;
567 break;
568 default:
569 return 0;
570 }
571
572 if (usage & GBM_BO_USE_CURSOR &&
573 usage & GBM_BO_USE_RENDERING)
574 return 0;
575
576 return 1;
577 }
578
579 static int
580 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
581 {
582 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
583
584 if (bo->image != NULL) {
585 errno = EINVAL;
586 return -1;
587 }
588
589 memcpy(bo->map, buf, count);
590
591 return 0;
592 }
593
594 static int
595 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
596 {
597 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
598 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
599 int fd;
600
601 if (bo->image == NULL)
602 return -1;
603
604 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd))
605 return -1;
606
607 return fd;
608 }
609
610 static int
611 get_number_planes(struct gbm_dri_device *dri, __DRIimage *image)
612 {
613 int num_planes = 0;
614
615 /* Dumb buffers are single-plane only. */
616 if (!image)
617 return 1;
618
619 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
620
621 if (num_planes <= 0)
622 num_planes = 1;
623
624 return num_planes;
625 }
626
627 static int
628 gbm_dri_bo_get_planes(struct gbm_bo *_bo)
629 {
630 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
631 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
632
633 return get_number_planes(dri, bo->image);
634 }
635
636 static union gbm_bo_handle
637 gbm_dri_bo_get_handle_for_plane(struct gbm_bo *_bo, int plane)
638 {
639 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
640 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
641 union gbm_bo_handle ret;
642 ret.s32 = -1;
643
644 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
645 errno = ENOSYS;
646 return ret;
647 }
648
649 if (plane >= get_number_planes(dri, bo->image)) {
650 errno = EINVAL;
651 return ret;
652 }
653
654 /* dumb BOs can only utilize non-planar formats */
655 if (!bo->image) {
656 assert(plane == 0);
657 ret.s32 = bo->handle;
658 return ret;
659 }
660
661 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
662 if (image) {
663 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
664 dri->image->destroyImage(image);
665 } else {
666 assert(plane == 0);
667 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
668 }
669
670 return ret;
671 }
672
673 static uint32_t
674 gbm_dri_bo_get_stride(struct gbm_bo *_bo, int plane)
675 {
676 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
677 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
678 __DRIimage *image;
679 int stride = 0;
680
681 if (!dri->image || dri->image->base.version < 11 || !dri->image->fromPlanar) {
682 /* Preserve legacy behavior if plane is 0 */
683 if (plane == 0)
684 return _bo->stride;
685
686 errno = ENOSYS;
687 return 0;
688 }
689
690 if (plane >= get_number_planes(dri, bo->image)) {
691 errno = EINVAL;
692 return 0;
693 }
694
695 if (bo->image == NULL) {
696 assert(plane == 0);
697 return _bo->stride;
698 }
699
700 image = dri->image->fromPlanar(bo->image, plane, NULL);
701 if (image) {
702 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
703 dri->image->destroyImage(image);
704 } else {
705 assert(plane == 0);
706 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
707 }
708
709 return (uint32_t)stride;
710 }
711
712 static uint32_t
713 gbm_dri_bo_get_offset(struct gbm_bo *_bo, int plane)
714 {
715 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
716 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
717 int offset = 0;
718
719 /* These error cases do not actually return an error code, as the user
720 * will also fail to obtain the handle/FD from the BO. In that case, the
721 * offset is irrelevant, as they have no buffer to offset into, so
722 * returning 0 is harmless.
723 */
724 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar)
725 return 0;
726
727 if (plane >= get_number_planes(dri, bo->image))
728 return 0;
729
730 /* Dumb images have no offset */
731 if (bo->image == NULL) {
732 assert(plane == 0);
733 return 0;
734 }
735
736 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
737 if (image) {
738 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
739 dri->image->destroyImage(image);
740 } else {
741 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
742 }
743
744 return (uint32_t)offset;
745 }
746
747 static uint64_t
748 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
749 {
750 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
751 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
752
753 if (!dri->image || dri->image->base.version < 14) {
754 errno = ENOSYS;
755 return DRM_FORMAT_MOD_INVALID;
756 }
757
758 /* Dumb buffers have no modifiers */
759 if (!bo->image)
760 return DRM_FORMAT_MOD_LINEAR;
761
762 uint64_t ret = 0;
763 int mod;
764 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
765 &mod))
766 return DRM_FORMAT_MOD_INVALID;
767
768 ret = (uint64_t)mod << 32;
769
770 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
771 &mod))
772 return DRM_FORMAT_MOD_INVALID;
773
774 ret |= (uint64_t)(mod & 0xffffffff);
775
776 return ret;
777 }
778
779 static void
780 gbm_dri_bo_destroy(struct gbm_bo *_bo)
781 {
782 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
783 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
784 struct drm_mode_destroy_dumb arg;
785
786 if (bo->image != NULL) {
787 dri->image->destroyImage(bo->image);
788 } else {
789 gbm_dri_bo_unmap_dumb(bo);
790 memset(&arg, 0, sizeof(arg));
791 arg.handle = bo->handle;
792 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
793 }
794
795 free(bo);
796 }
797
798 static uint32_t
799 gbm_dri_to_gbm_format(uint32_t dri_format)
800 {
801 uint32_t ret = 0;
802
803 switch (dri_format) {
804 case __DRI_IMAGE_FORMAT_RGB565:
805 ret = GBM_FORMAT_RGB565;
806 break;
807 case __DRI_IMAGE_FORMAT_XRGB8888:
808 ret = GBM_FORMAT_XRGB8888;
809 break;
810 case __DRI_IMAGE_FORMAT_ARGB8888:
811 ret = GBM_FORMAT_ARGB8888;
812 break;
813 case __DRI_IMAGE_FORMAT_XBGR8888:
814 ret = GBM_FORMAT_XBGR8888;
815 break;
816 case __DRI_IMAGE_FORMAT_ABGR8888:
817 ret = GBM_FORMAT_ABGR8888;
818 break;
819 default:
820 ret = 0;
821 break;
822 }
823
824 return ret;
825 }
826
827 static struct gbm_bo *
828 gbm_dri_bo_import(struct gbm_device *gbm,
829 uint32_t type, void *buffer, uint32_t usage)
830 {
831 struct gbm_dri_device *dri = gbm_dri_device(gbm);
832 struct gbm_dri_bo *bo;
833 __DRIimage *image;
834 unsigned dri_use = 0;
835 int gbm_format;
836
837 /* Required for query image WIDTH & HEIGHT */
838 if (dri->image == NULL || dri->image->base.version < 4) {
839 errno = ENOSYS;
840 return NULL;
841 }
842
843 switch (type) {
844 #if HAVE_WAYLAND_PLATFORM
845 case GBM_BO_IMPORT_WL_BUFFER:
846 {
847 struct wl_drm_buffer *wb;
848
849 if (!dri->wl_drm) {
850 errno = EINVAL;
851 return NULL;
852 }
853
854 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
855 if (!wb) {
856 errno = EINVAL;
857 return NULL;
858 }
859
860 image = dri->image->dupImage(wb->driver_buffer, NULL);
861
862 switch (wb->format) {
863 case WL_DRM_FORMAT_XRGB8888:
864 gbm_format = GBM_FORMAT_XRGB8888;
865 break;
866 case WL_DRM_FORMAT_ARGB8888:
867 gbm_format = GBM_FORMAT_ARGB8888;
868 break;
869 case WL_DRM_FORMAT_RGB565:
870 gbm_format = GBM_FORMAT_RGB565;
871 break;
872 case WL_DRM_FORMAT_YUYV:
873 gbm_format = GBM_FORMAT_YUYV;
874 break;
875 default:
876 dri->image->destroyImage(image);
877 return NULL;
878 }
879 break;
880 }
881 #endif
882
883 case GBM_BO_IMPORT_EGL_IMAGE:
884 {
885 int dri_format;
886 if (dri->lookup_image == NULL) {
887 errno = EINVAL;
888 return NULL;
889 }
890
891 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
892 image = dri->image->dupImage(image, NULL);
893 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
894 gbm_format = gbm_dri_to_gbm_format(dri_format);
895 if (gbm_format == 0) {
896 errno = EINVAL;
897 dri->image->destroyImage(image);
898 return NULL;
899 }
900 break;
901 }
902
903 case GBM_BO_IMPORT_FD:
904 {
905 struct gbm_import_fd_data *fd_data = buffer;
906 int stride = fd_data->stride, offset = 0;
907 int dri_format;
908
909 switch (fd_data->format) {
910 case GBM_BO_FORMAT_XRGB8888:
911 dri_format = GBM_FORMAT_XRGB8888;
912 break;
913 case GBM_BO_FORMAT_ARGB8888:
914 dri_format = GBM_FORMAT_ARGB8888;
915 break;
916 default:
917 dri_format = fd_data->format;
918 }
919
920 image = dri->image->createImageFromFds(dri->screen,
921 fd_data->width,
922 fd_data->height,
923 dri_format,
924 &fd_data->fd, 1,
925 &stride, &offset,
926 NULL);
927 if (image == NULL) {
928 errno = EINVAL;
929 return NULL;
930 }
931 gbm_format = fd_data->format;
932 break;
933 }
934
935 default:
936 errno = ENOSYS;
937 return NULL;
938 }
939
940
941 bo = calloc(1, sizeof *bo);
942 if (bo == NULL) {
943 dri->image->destroyImage(image);
944 return NULL;
945 }
946
947 bo->image = image;
948
949 if (usage & GBM_BO_USE_SCANOUT)
950 dri_use |= __DRI_IMAGE_USE_SCANOUT;
951 if (usage & GBM_BO_USE_CURSOR)
952 dri_use |= __DRI_IMAGE_USE_CURSOR;
953 if (dri->image->base.version >= 2 &&
954 !dri->image->validateUsage(bo->image, dri_use)) {
955 errno = EINVAL;
956 dri->image->destroyImage(bo->image);
957 free(bo);
958 return NULL;
959 }
960
961 bo->base.gbm = gbm;
962 bo->base.format = gbm_format;
963
964 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
965 (int*)&bo->base.width);
966 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
967 (int*)&bo->base.height);
968 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
969 (int*)&bo->base.stride);
970 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
971 &bo->base.handle.s32);
972
973 return &bo->base;
974 }
975
976 static bool
977 is_planar_format(uint32_t format)
978 {
979 switch (format) {
980 case GBM_FORMAT_NV12:
981 case GBM_FORMAT_NV21:
982 case GBM_FORMAT_NV16:
983 case GBM_FORMAT_NV61:
984 case GBM_FORMAT_YUV410:
985 case GBM_FORMAT_YVU410:
986 case GBM_FORMAT_YUV411:
987 case GBM_FORMAT_YVU411:
988 case GBM_FORMAT_YUV420:
989 case GBM_FORMAT_YVU420:
990 case GBM_FORMAT_YUV422:
991 case GBM_FORMAT_YVU422:
992 case GBM_FORMAT_YUV444:
993 case GBM_FORMAT_YVU444:
994 return true;
995 default:
996 return false;
997 }
998
999 }
1000
1001 static struct gbm_bo *
1002 create_dumb(struct gbm_device *gbm,
1003 uint32_t width, uint32_t height,
1004 uint32_t format, uint32_t usage)
1005 {
1006 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1007 struct drm_mode_create_dumb create_arg;
1008 struct gbm_dri_bo *bo;
1009 struct drm_mode_destroy_dumb destroy_arg;
1010 int ret;
1011 int is_cursor, is_scanout;
1012
1013 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1014 format == GBM_FORMAT_ARGB8888;
1015 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1016 format == GBM_FORMAT_XRGB8888;
1017 if (!is_cursor && !is_scanout) {
1018 errno = EINVAL;
1019 return NULL;
1020 }
1021
1022 if (is_planar_format(format)) {
1023 errno = EINVAL;
1024 return NULL;
1025 }
1026
1027 bo = calloc(1, sizeof *bo);
1028 if (bo == NULL)
1029 return NULL;
1030
1031 memset(&create_arg, 0, sizeof(create_arg));
1032 create_arg.bpp = 32;
1033 create_arg.width = width;
1034 create_arg.height = height;
1035
1036 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1037 if (ret)
1038 goto free_bo;
1039
1040 bo->base.gbm = gbm;
1041 bo->base.width = width;
1042 bo->base.height = height;
1043 bo->base.stride = create_arg.pitch;
1044 bo->base.format = format;
1045 bo->base.handle.u32 = create_arg.handle;
1046 bo->handle = create_arg.handle;
1047 bo->size = create_arg.size;
1048
1049 if (gbm_dri_bo_map_dumb(bo) == NULL)
1050 goto destroy_dumb;
1051
1052 return &bo->base;
1053
1054 destroy_dumb:
1055 memset(&destroy_arg, 0, sizeof destroy_arg);
1056 destroy_arg.handle = create_arg.handle;
1057 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1058 free_bo:
1059 free(bo);
1060
1061 return NULL;
1062 }
1063
1064 static struct gbm_bo *
1065 gbm_dri_bo_create(struct gbm_device *gbm,
1066 uint32_t width, uint32_t height,
1067 uint32_t format, uint32_t usage,
1068 const uint64_t *modifiers,
1069 const unsigned int count)
1070 {
1071 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1072 struct gbm_dri_bo *bo;
1073 int dri_format;
1074 unsigned dri_use = 0;
1075
1076 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1077 * newer modifier interface deprecates the older usage flags.
1078 */
1079 assert(!(usage && count));
1080
1081 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1082 return create_dumb(gbm, width, height, format, usage);
1083
1084 bo = calloc(1, sizeof *bo);
1085 if (bo == NULL)
1086 return NULL;
1087
1088 bo->base.gbm = gbm;
1089 bo->base.width = width;
1090 bo->base.height = height;
1091 bo->base.format = format;
1092
1093 switch (format) {
1094 case GBM_FORMAT_R8:
1095 dri_format = __DRI_IMAGE_FORMAT_R8;
1096 break;
1097 case GBM_FORMAT_GR88:
1098 dri_format = __DRI_IMAGE_FORMAT_GR88;
1099 break;
1100 case GBM_FORMAT_RGB565:
1101 dri_format = __DRI_IMAGE_FORMAT_RGB565;
1102 break;
1103 case GBM_FORMAT_XRGB8888:
1104 case GBM_BO_FORMAT_XRGB8888:
1105 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
1106 break;
1107 case GBM_FORMAT_ARGB8888:
1108 case GBM_BO_FORMAT_ARGB8888:
1109 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
1110 break;
1111 case GBM_FORMAT_ABGR8888:
1112 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
1113 break;
1114 case GBM_FORMAT_XBGR8888:
1115 dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
1116 break;
1117 case GBM_FORMAT_ARGB2101010:
1118 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
1119 break;
1120 case GBM_FORMAT_XRGB2101010:
1121 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
1122 break;
1123 default:
1124 errno = EINVAL;
1125 goto failed;
1126 }
1127
1128 if (usage & GBM_BO_USE_SCANOUT)
1129 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1130 if (usage & GBM_BO_USE_CURSOR)
1131 dri_use |= __DRI_IMAGE_USE_CURSOR;
1132 if (usage & GBM_BO_USE_LINEAR)
1133 dri_use |= __DRI_IMAGE_USE_LINEAR;
1134
1135 /* Gallium drivers requires shared in order to get the handle/stride */
1136 dri_use |= __DRI_IMAGE_USE_SHARE;
1137
1138 if (modifiers) {
1139 if (!dri->image || dri->image->base.version < 14 ||
1140 !dri->image->createImageWithModifiers) {
1141 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1142 errno = ENOSYS;
1143 goto failed;
1144 }
1145
1146 /* It's acceptable to create an image with INVALID modifier in the list,
1147 * but it cannot be on the only modifier (since it will certainly fail
1148 * later). While we could easily catch this after modifier creation, doing
1149 * the check here is a convenient debug check likely pointing at whatever
1150 * interface the client is using to build its modifier list.
1151 */
1152 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1153 fprintf(stderr, "Only invalid modifier specified\n");
1154 errno = EINVAL;
1155 goto failed;
1156 }
1157
1158 bo->image =
1159 dri->image->createImageWithModifiers(dri->screen,
1160 width, height,
1161 dri_format,
1162 modifiers, count,
1163 bo);
1164
1165 if (bo->image) {
1166 /* The client passed in a list of invalid modifiers */
1167 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1168 }
1169 } else {
1170 bo->image = dri->image->createImage(dri->screen, width, height,
1171 dri_format, dri_use, bo);
1172 }
1173
1174 if (bo->image == NULL)
1175 goto failed;
1176
1177 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1178 &bo->base.handle.s32);
1179 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1180 (int *) &bo->base.stride);
1181
1182 return &bo->base;
1183
1184 failed:
1185 free(bo);
1186 return NULL;
1187 }
1188
1189 static void *
1190 gbm_dri_bo_map(struct gbm_bo *_bo,
1191 uint32_t x, uint32_t y,
1192 uint32_t width, uint32_t height,
1193 uint32_t flags, uint32_t *stride, void **map_data)
1194 {
1195 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1196 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1197
1198 /* If it's a dumb buffer, we already have a mapping */
1199 if (bo->map) {
1200 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1201 *stride = bo->base.stride;
1202 return *map_data;
1203 }
1204
1205 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1206 errno = ENOSYS;
1207 return NULL;
1208 }
1209
1210 mtx_lock(&dri->mutex);
1211 if (!dri->context)
1212 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1213 NULL, NULL);
1214 assert(dri->context);
1215 mtx_unlock(&dri->mutex);
1216
1217 /* GBM flags and DRI flags are the same, so just pass them on */
1218 return dri->image->mapImage(dri->context, bo->image, x, y,
1219 width, height, flags, (int *)stride,
1220 map_data);
1221 }
1222
1223 static void
1224 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1225 {
1226 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1227 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1228
1229 /* Check if it's a dumb buffer and check the pointer is in range */
1230 if (bo->map) {
1231 assert(map_data >= bo->map);
1232 assert(map_data < (bo->map + bo->size));
1233 return;
1234 }
1235
1236 if (!dri->context || !dri->image ||
1237 dri->image->base.version < 12 || !dri->image->unmapImage)
1238 return;
1239
1240 dri->image->unmapImage(dri->context, bo->image, map_data);
1241
1242 /*
1243 * Not all DRI drivers use direct maps. They may queue up DMA operations
1244 * on the mapping context. Since there is no explicit gbm flush
1245 * mechanism, we need to flush here.
1246 */
1247 if (dri->flush->base.version >= 4)
1248 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1249 }
1250
1251
1252 static struct gbm_surface *
1253 gbm_dri_surface_create(struct gbm_device *gbm,
1254 uint32_t width, uint32_t height,
1255 uint32_t format, uint32_t flags,
1256 const uint64_t *modifiers, const unsigned count)
1257 {
1258 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1259 struct gbm_dri_surface *surf;
1260
1261 if (modifiers &&
1262 (!dri->image || dri->image->base.version < 14 ||
1263 !dri->image->createImageWithModifiers)) {
1264 errno = ENOSYS;
1265 return NULL;
1266 }
1267
1268 if (count)
1269 assert(modifiers);
1270
1271 /* It's acceptable to create an image with INVALID modifier in the list,
1272 * but it cannot be on the only modifier (since it will certainly fail
1273 * later). While we could easily catch this after modifier creation, doing
1274 * the check here is a convenient debug check likely pointing at whatever
1275 * interface the client is using to build its modifier list.
1276 */
1277 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1278 fprintf(stderr, "Only invalid modifier specified\n");
1279 errno = EINVAL;
1280 }
1281
1282 surf = calloc(1, sizeof *surf);
1283 if (surf == NULL) {
1284 errno = ENOMEM;
1285 return NULL;
1286 }
1287
1288 surf->base.gbm = gbm;
1289 surf->base.width = width;
1290 surf->base.height = height;
1291 surf->base.format = format;
1292 surf->base.flags = flags;
1293 if (!modifiers) {
1294 assert(!count);
1295 return &surf->base;
1296 }
1297
1298 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1299 if (count && !surf->base.modifiers) {
1300 errno = ENOMEM;
1301 free(surf);
1302 return NULL;
1303 }
1304
1305 /* TODO: We are deferring validation of modifiers until the image is actually
1306 * created. This deferred creation can fail due to a modifier-format
1307 * mismatch. The result is the client has a surface but no object to back it.
1308 */
1309 surf->base.count = count;
1310 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1311
1312 return &surf->base;
1313 }
1314
1315 static void
1316 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1317 {
1318 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1319
1320 free(surf->base.modifiers);
1321 free(surf);
1322 }
1323
1324 static void
1325 dri_destroy(struct gbm_device *gbm)
1326 {
1327 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1328 unsigned i;
1329
1330 if (dri->context)
1331 dri->core->destroyContext(dri->context);
1332
1333 dri->core->destroyScreen(dri->screen);
1334 for (i = 0; dri->driver_configs[i]; i++)
1335 free((__DRIconfig *) dri->driver_configs[i]);
1336 free(dri->driver_configs);
1337 dlclose(dri->driver);
1338 free(dri->driver_name);
1339
1340 free(dri);
1341 }
1342
1343 static struct gbm_device *
1344 dri_device_create(int fd)
1345 {
1346 struct gbm_dri_device *dri;
1347 int ret, force_sw;
1348
1349 dri = calloc(1, sizeof *dri);
1350 if (!dri)
1351 return NULL;
1352
1353 dri->base.fd = fd;
1354 dri->base.bo_create = gbm_dri_bo_create;
1355 dri->base.bo_import = gbm_dri_bo_import;
1356 dri->base.bo_map = gbm_dri_bo_map;
1357 dri->base.bo_unmap = gbm_dri_bo_unmap;
1358 dri->base.is_format_supported = gbm_dri_is_format_supported;
1359 dri->base.bo_write = gbm_dri_bo_write;
1360 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1361 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1362 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1363 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1364 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1365 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1366 dri->base.bo_destroy = gbm_dri_bo_destroy;
1367 dri->base.destroy = dri_destroy;
1368 dri->base.surface_create = gbm_dri_surface_create;
1369 dri->base.surface_destroy = gbm_dri_surface_destroy;
1370
1371 dri->base.name = "drm";
1372
1373 mtx_init(&dri->mutex, mtx_plain);
1374
1375 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1376 if (!force_sw) {
1377 ret = dri_screen_create(dri);
1378 if (ret)
1379 ret = dri_screen_create_sw(dri);
1380 } else {
1381 ret = dri_screen_create_sw(dri);
1382 }
1383
1384 if (ret)
1385 goto err_dri;
1386
1387 return &dri->base;
1388
1389 err_dri:
1390 free(dri);
1391
1392 return NULL;
1393 }
1394
1395 struct gbm_backend gbm_dri_backend = {
1396 .backend_name = "dri",
1397 .create_device = dri_device_create,
1398 };