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