35ec3a1c3a2a1911cf4043229cf7aa7a26f0c091
[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 gbm_dri_visual gbm_dri_visuals_table[] = {
547 {
548 GBM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8,
549 { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 },
550 },
551 {
552 GBM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88,
553 { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 },
554 },
555 {
556 GBM_FORMAT_ARGB1555, __DRI_IMAGE_FORMAT_ARGB1555,
557 { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 },
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, 0xff000000 },
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, 0xff000000 },
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 GBM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010,
589 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0x00000000 },
590 },
591 {
592 GBM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010,
593 { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 },
594 },
595 };
596
597 /* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
598 * formats of the same name. We want to accept them whenever someone
599 * has a GBM format, but never return them to the user. */
600 static int
601 gbm_format_canonicalize(uint32_t gbm_format)
602 {
603 switch (gbm_format) {
604 case GBM_BO_FORMAT_XRGB8888:
605 return GBM_FORMAT_XRGB8888;
606 case GBM_BO_FORMAT_ARGB8888:
607 return GBM_FORMAT_ARGB8888;
608 default:
609 return gbm_format;
610 }
611 }
612
613 static int
614 gbm_format_to_dri_format(uint32_t gbm_format)
615 {
616 int i;
617
618 gbm_format = gbm_format_canonicalize(gbm_format);
619 for (i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
620 if (gbm_dri_visuals_table[i].gbm_format == gbm_format)
621 return gbm_dri_visuals_table[i].dri_image_format;
622 }
623
624 return 0;
625 }
626
627 static uint32_t
628 gbm_dri_to_gbm_format(int dri_format)
629 {
630 int i;
631
632 for (i = 0; i < ARRAY_SIZE(gbm_dri_visuals_table); i++) {
633 if (gbm_dri_visuals_table[i].dri_image_format == dri_format)
634 return gbm_dri_visuals_table[i].gbm_format;
635 }
636
637 return 0;
638 }
639
640 static int
641 gbm_dri_is_format_supported(struct gbm_device *gbm,
642 uint32_t format,
643 uint32_t usage)
644 {
645 struct gbm_dri_device *dri = gbm_dri_device(gbm);
646 int count;
647
648 if ((usage & GBM_BO_USE_CURSOR) && (usage & GBM_BO_USE_RENDERING))
649 return 0;
650
651 format = gbm_format_canonicalize(format);
652 if (gbm_format_to_dri_format(format) == 0)
653 return 0;
654
655 /* If there is no query, fall back to the small table which was originally
656 * here. */
657 if (dri->image->base.version <= 15 || !dri->image->queryDmaBufModifiers) {
658 switch (format) {
659 case GBM_FORMAT_XRGB8888:
660 case GBM_FORMAT_ARGB8888:
661 case GBM_FORMAT_XBGR8888:
662 return 1;
663 default:
664 return 0;
665 }
666 }
667
668 /* Check if the driver returns any modifiers for this format; since linear
669 * is counted as a modifier, we will have at least one modifier for any
670 * supported format. */
671 if (!dri->image->queryDmaBufModifiers(dri->screen, format, 0, NULL, NULL,
672 &count))
673 return 0;
674
675 return (count > 0);
676 }
677
678 static int
679 gbm_dri_get_format_modifier_plane_count(struct gbm_device *gbm,
680 uint32_t format,
681 uint64_t modifier)
682 {
683 struct gbm_dri_device *dri = gbm_dri_device(gbm);
684 uint64_t plane_count;
685
686 if (dri->image->base.version < 16 ||
687 !dri->image->queryDmaBufFormatModifierAttribs)
688 return -1;
689
690 format = gbm_format_canonicalize(format);
691 if (gbm_format_to_dri_format(format) == 0)
692 return -1;
693
694 if (!dri->image->queryDmaBufFormatModifierAttribs(
695 dri->screen, format, modifier,
696 __DRI_IMAGE_FORMAT_MODIFIER_ATTRIB_PLANE_COUNT, &plane_count))
697 return -1;
698
699 return plane_count;
700 }
701
702 static int
703 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
704 {
705 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
706
707 if (bo->image != NULL) {
708 errno = EINVAL;
709 return -1;
710 }
711
712 memcpy(bo->map, buf, count);
713
714 return 0;
715 }
716
717 static int
718 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
719 {
720 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
721 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
722 int fd;
723
724 if (bo->image == NULL)
725 return -1;
726
727 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd))
728 return -1;
729
730 return fd;
731 }
732
733 static int
734 get_number_planes(struct gbm_dri_device *dri, __DRIimage *image)
735 {
736 int num_planes = 0;
737
738 /* Dumb buffers are single-plane only. */
739 if (!image)
740 return 1;
741
742 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
743
744 if (num_planes <= 0)
745 num_planes = 1;
746
747 return num_planes;
748 }
749
750 static int
751 gbm_dri_bo_get_planes(struct gbm_bo *_bo)
752 {
753 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
754 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
755
756 return get_number_planes(dri, bo->image);
757 }
758
759 static union gbm_bo_handle
760 gbm_dri_bo_get_handle_for_plane(struct gbm_bo *_bo, int plane)
761 {
762 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
763 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
764 union gbm_bo_handle ret;
765 ret.s32 = -1;
766
767 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
768 errno = ENOSYS;
769 return ret;
770 }
771
772 if (plane >= get_number_planes(dri, bo->image)) {
773 errno = EINVAL;
774 return ret;
775 }
776
777 /* dumb BOs can only utilize non-planar formats */
778 if (!bo->image) {
779 assert(plane == 0);
780 ret.s32 = bo->handle;
781 return ret;
782 }
783
784 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
785 if (image) {
786 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
787 dri->image->destroyImage(image);
788 } else {
789 assert(plane == 0);
790 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
791 }
792
793 return ret;
794 }
795
796 static uint32_t
797 gbm_dri_bo_get_stride(struct gbm_bo *_bo, int plane)
798 {
799 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
800 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
801 __DRIimage *image;
802 int stride = 0;
803
804 if (!dri->image || dri->image->base.version < 11 || !dri->image->fromPlanar) {
805 /* Preserve legacy behavior if plane is 0 */
806 if (plane == 0)
807 return _bo->stride;
808
809 errno = ENOSYS;
810 return 0;
811 }
812
813 if (plane >= get_number_planes(dri, bo->image)) {
814 errno = EINVAL;
815 return 0;
816 }
817
818 if (bo->image == NULL) {
819 assert(plane == 0);
820 return _bo->stride;
821 }
822
823 image = dri->image->fromPlanar(bo->image, plane, NULL);
824 if (image) {
825 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
826 dri->image->destroyImage(image);
827 } else {
828 assert(plane == 0);
829 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
830 }
831
832 return (uint32_t)stride;
833 }
834
835 static uint32_t
836 gbm_dri_bo_get_offset(struct gbm_bo *_bo, int plane)
837 {
838 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
839 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
840 int offset = 0;
841
842 /* These error cases do not actually return an error code, as the user
843 * will also fail to obtain the handle/FD from the BO. In that case, the
844 * offset is irrelevant, as they have no buffer to offset into, so
845 * returning 0 is harmless.
846 */
847 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar)
848 return 0;
849
850 if (plane >= get_number_planes(dri, bo->image))
851 return 0;
852
853 /* Dumb images have no offset */
854 if (bo->image == NULL) {
855 assert(plane == 0);
856 return 0;
857 }
858
859 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
860 if (image) {
861 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
862 dri->image->destroyImage(image);
863 } else {
864 assert(plane == 0);
865 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
866 }
867
868 return (uint32_t)offset;
869 }
870
871 static uint64_t
872 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
873 {
874 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
875 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
876
877 if (!dri->image || dri->image->base.version < 14) {
878 errno = ENOSYS;
879 return DRM_FORMAT_MOD_INVALID;
880 }
881
882 /* Dumb buffers have no modifiers */
883 if (!bo->image)
884 return DRM_FORMAT_MOD_LINEAR;
885
886 uint64_t ret = 0;
887 int mod;
888 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
889 &mod))
890 return DRM_FORMAT_MOD_INVALID;
891
892 ret = (uint64_t)mod << 32;
893
894 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
895 &mod))
896 return DRM_FORMAT_MOD_INVALID;
897
898 ret |= (uint64_t)(mod & 0xffffffff);
899
900 return ret;
901 }
902
903 static void
904 gbm_dri_bo_destroy(struct gbm_bo *_bo)
905 {
906 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
907 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
908 struct drm_mode_destroy_dumb arg;
909
910 if (bo->image != NULL) {
911 dri->image->destroyImage(bo->image);
912 } else {
913 gbm_dri_bo_unmap_dumb(bo);
914 memset(&arg, 0, sizeof(arg));
915 arg.handle = bo->handle;
916 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
917 }
918
919 free(bo);
920 }
921
922 static struct gbm_bo *
923 gbm_dri_bo_import(struct gbm_device *gbm,
924 uint32_t type, void *buffer, uint32_t usage)
925 {
926 struct gbm_dri_device *dri = gbm_dri_device(gbm);
927 struct gbm_dri_bo *bo;
928 __DRIimage *image;
929 unsigned dri_use = 0;
930 int gbm_format;
931
932 /* Required for query image WIDTH & HEIGHT */
933 if (dri->image == NULL || dri->image->base.version < 4) {
934 errno = ENOSYS;
935 return NULL;
936 }
937
938 switch (type) {
939 #if HAVE_WAYLAND_PLATFORM
940 case GBM_BO_IMPORT_WL_BUFFER:
941 {
942 struct wl_drm_buffer *wb;
943
944 if (!dri->wl_drm) {
945 errno = EINVAL;
946 return NULL;
947 }
948
949 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
950 if (!wb) {
951 errno = EINVAL;
952 return NULL;
953 }
954
955 image = dri->image->dupImage(wb->driver_buffer, NULL);
956
957 /* GBM_FORMAT_* is identical to WL_DRM_FORMAT_*, so no conversion
958 * required. */
959 gbm_format = wb->format;
960 break;
961 }
962 #endif
963
964 case GBM_BO_IMPORT_EGL_IMAGE:
965 {
966 int dri_format;
967 if (dri->lookup_image == NULL) {
968 errno = EINVAL;
969 return NULL;
970 }
971
972 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
973 image = dri->image->dupImage(image, NULL);
974 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
975 gbm_format = gbm_dri_to_gbm_format(dri_format);
976 if (gbm_format == 0) {
977 errno = EINVAL;
978 dri->image->destroyImage(image);
979 return NULL;
980 }
981 break;
982 }
983
984 case GBM_BO_IMPORT_FD:
985 {
986 struct gbm_import_fd_data *fd_data = buffer;
987 int stride = fd_data->stride, offset = 0;
988 int fourcc;
989
990 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
991 * tokens accepted by createImageFromFds, except for not supporting
992 * the sARGB format. */
993 fourcc = gbm_format_canonicalize(fd_data->format);
994
995 image = dri->image->createImageFromFds(dri->screen,
996 fd_data->width,
997 fd_data->height,
998 fourcc,
999 &fd_data->fd, 1,
1000 &stride, &offset,
1001 NULL);
1002 if (image == NULL) {
1003 errno = EINVAL;
1004 return NULL;
1005 }
1006 gbm_format = fd_data->format;
1007 break;
1008 }
1009
1010 case GBM_BO_IMPORT_FD_MODIFIER:
1011 {
1012 struct gbm_import_fd_modifier_data *fd_data = buffer;
1013 unsigned int error;
1014 int fourcc;
1015
1016 /* Import with modifier requires createImageFromDmaBufs2 */
1017 if (dri->image == NULL || dri->image->base.version < 15 ||
1018 dri->image->createImageFromDmaBufs2 == NULL) {
1019 errno = ENOSYS;
1020 return NULL;
1021 }
1022
1023 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
1024 * tokens accepted by createImageFromDmaBufs2, except for not supporting
1025 * the sARGB format. */
1026 fourcc = gbm_format_canonicalize(fd_data->format);
1027
1028 image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
1029 fd_data->height, fourcc,
1030 fd_data->modifier,
1031 fd_data->fds,
1032 fd_data->num_fds,
1033 fd_data->strides,
1034 fd_data->offsets,
1035 0, 0, 0, 0,
1036 &error, NULL);
1037 if (image == NULL) {
1038 errno = ENOSYS;
1039 return NULL;
1040 }
1041
1042 gbm_format = fourcc;
1043 break;
1044 }
1045
1046 default:
1047 errno = ENOSYS;
1048 return NULL;
1049 }
1050
1051
1052 bo = calloc(1, sizeof *bo);
1053 if (bo == NULL) {
1054 dri->image->destroyImage(image);
1055 return NULL;
1056 }
1057
1058 bo->image = image;
1059
1060 if (usage & GBM_BO_USE_SCANOUT)
1061 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1062 if (usage & GBM_BO_USE_CURSOR)
1063 dri_use |= __DRI_IMAGE_USE_CURSOR;
1064 if (dri->image->base.version >= 2 &&
1065 !dri->image->validateUsage(bo->image, dri_use)) {
1066 errno = EINVAL;
1067 dri->image->destroyImage(bo->image);
1068 free(bo);
1069 return NULL;
1070 }
1071
1072 bo->base.gbm = gbm;
1073 bo->base.format = gbm_format;
1074
1075 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
1076 (int*)&bo->base.width);
1077 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
1078 (int*)&bo->base.height);
1079 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1080 (int*)&bo->base.stride);
1081 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1082 &bo->base.handle.s32);
1083
1084 return &bo->base;
1085 }
1086
1087 static struct gbm_bo *
1088 create_dumb(struct gbm_device *gbm,
1089 uint32_t width, uint32_t height,
1090 uint32_t format, uint32_t usage)
1091 {
1092 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1093 struct drm_mode_create_dumb create_arg;
1094 struct gbm_dri_bo *bo;
1095 struct drm_mode_destroy_dumb destroy_arg;
1096 int ret;
1097 int is_cursor, is_scanout;
1098
1099 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1100 format == GBM_FORMAT_ARGB8888;
1101 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1102 (format == GBM_FORMAT_XRGB8888 || format == GBM_FORMAT_XBGR8888);
1103 if (!is_cursor && !is_scanout) {
1104 errno = EINVAL;
1105 return NULL;
1106 }
1107
1108 bo = calloc(1, sizeof *bo);
1109 if (bo == NULL)
1110 return NULL;
1111
1112 memset(&create_arg, 0, sizeof(create_arg));
1113 create_arg.bpp = 32;
1114 create_arg.width = width;
1115 create_arg.height = height;
1116
1117 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1118 if (ret)
1119 goto free_bo;
1120
1121 bo->base.gbm = gbm;
1122 bo->base.width = width;
1123 bo->base.height = height;
1124 bo->base.stride = create_arg.pitch;
1125 bo->base.format = format;
1126 bo->base.handle.u32 = create_arg.handle;
1127 bo->handle = create_arg.handle;
1128 bo->size = create_arg.size;
1129
1130 if (gbm_dri_bo_map_dumb(bo) == NULL)
1131 goto destroy_dumb;
1132
1133 return &bo->base;
1134
1135 destroy_dumb:
1136 memset(&destroy_arg, 0, sizeof destroy_arg);
1137 destroy_arg.handle = create_arg.handle;
1138 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1139 free_bo:
1140 free(bo);
1141
1142 return NULL;
1143 }
1144
1145 static struct gbm_bo *
1146 gbm_dri_bo_create(struct gbm_device *gbm,
1147 uint32_t width, uint32_t height,
1148 uint32_t format, uint32_t usage,
1149 const uint64_t *modifiers,
1150 const unsigned int count)
1151 {
1152 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1153 struct gbm_dri_bo *bo;
1154 int dri_format;
1155 unsigned dri_use = 0;
1156
1157 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1158 * newer modifier interface deprecates the older usage flags.
1159 */
1160 assert(!(usage && count));
1161
1162 format = gbm_format_canonicalize(format);
1163
1164 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1165 return create_dumb(gbm, width, height, format, usage);
1166
1167 bo = calloc(1, sizeof *bo);
1168 if (bo == NULL)
1169 return NULL;
1170
1171 bo->base.gbm = gbm;
1172 bo->base.width = width;
1173 bo->base.height = height;
1174 bo->base.format = format;
1175
1176 dri_format = gbm_format_to_dri_format(format);
1177 if (dri_format == 0) {
1178 errno = EINVAL;
1179 goto failed;
1180 }
1181
1182 if (usage & GBM_BO_USE_SCANOUT)
1183 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1184 if (usage & GBM_BO_USE_CURSOR)
1185 dri_use |= __DRI_IMAGE_USE_CURSOR;
1186 if (usage & GBM_BO_USE_LINEAR)
1187 dri_use |= __DRI_IMAGE_USE_LINEAR;
1188
1189 /* Gallium drivers requires shared in order to get the handle/stride */
1190 dri_use |= __DRI_IMAGE_USE_SHARE;
1191
1192 if (modifiers) {
1193 if (!dri->image || dri->image->base.version < 14 ||
1194 !dri->image->createImageWithModifiers) {
1195 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1196 errno = ENOSYS;
1197 goto failed;
1198 }
1199
1200 /* It's acceptable to create an image with INVALID modifier in the list,
1201 * but it cannot be on the only modifier (since it will certainly fail
1202 * later). While we could easily catch this after modifier creation, doing
1203 * the check here is a convenient debug check likely pointing at whatever
1204 * interface the client is using to build its modifier list.
1205 */
1206 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1207 fprintf(stderr, "Only invalid modifier specified\n");
1208 errno = EINVAL;
1209 goto failed;
1210 }
1211
1212 bo->image =
1213 dri->image->createImageWithModifiers(dri->screen,
1214 width, height,
1215 dri_format,
1216 modifiers, count,
1217 bo);
1218
1219 if (bo->image) {
1220 /* The client passed in a list of invalid modifiers */
1221 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1222 }
1223 } else {
1224 bo->image = dri->image->createImage(dri->screen, width, height,
1225 dri_format, dri_use, bo);
1226 }
1227
1228 if (bo->image == NULL)
1229 goto failed;
1230
1231 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1232 &bo->base.handle.s32);
1233 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1234 (int *) &bo->base.stride);
1235
1236 return &bo->base;
1237
1238 failed:
1239 free(bo);
1240 return NULL;
1241 }
1242
1243 static void *
1244 gbm_dri_bo_map(struct gbm_bo *_bo,
1245 uint32_t x, uint32_t y,
1246 uint32_t width, uint32_t height,
1247 uint32_t flags, uint32_t *stride, void **map_data)
1248 {
1249 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1250 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1251
1252 /* If it's a dumb buffer, we already have a mapping */
1253 if (bo->map) {
1254 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1255 *stride = bo->base.stride;
1256 return *map_data;
1257 }
1258
1259 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1260 errno = ENOSYS;
1261 return NULL;
1262 }
1263
1264 mtx_lock(&dri->mutex);
1265 if (!dri->context)
1266 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1267 NULL, NULL);
1268 assert(dri->context);
1269 mtx_unlock(&dri->mutex);
1270
1271 /* GBM flags and DRI flags are the same, so just pass them on */
1272 return dri->image->mapImage(dri->context, bo->image, x, y,
1273 width, height, flags, (int *)stride,
1274 map_data);
1275 }
1276
1277 static void
1278 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1279 {
1280 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1281 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1282
1283 /* Check if it's a dumb buffer and check the pointer is in range */
1284 if (bo->map) {
1285 assert(map_data >= bo->map);
1286 assert(map_data < (bo->map + bo->size));
1287 return;
1288 }
1289
1290 if (!dri->context || !dri->image ||
1291 dri->image->base.version < 12 || !dri->image->unmapImage)
1292 return;
1293
1294 dri->image->unmapImage(dri->context, bo->image, map_data);
1295
1296 /*
1297 * Not all DRI drivers use direct maps. They may queue up DMA operations
1298 * on the mapping context. Since there is no explicit gbm flush
1299 * mechanism, we need to flush here.
1300 */
1301 if (dri->flush->base.version >= 4)
1302 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1303 }
1304
1305
1306 static struct gbm_surface *
1307 gbm_dri_surface_create(struct gbm_device *gbm,
1308 uint32_t width, uint32_t height,
1309 uint32_t format, uint32_t flags,
1310 const uint64_t *modifiers, const unsigned count)
1311 {
1312 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1313 struct gbm_dri_surface *surf;
1314
1315 if (modifiers &&
1316 (!dri->image || dri->image->base.version < 14 ||
1317 !dri->image->createImageWithModifiers)) {
1318 errno = ENOSYS;
1319 return NULL;
1320 }
1321
1322 if (count)
1323 assert(modifiers);
1324
1325 /* It's acceptable to create an image with INVALID modifier in the list,
1326 * but it cannot be on the only modifier (since it will certainly fail
1327 * later). While we could easily catch this after modifier creation, doing
1328 * the check here is a convenient debug check likely pointing at whatever
1329 * interface the client is using to build its modifier list.
1330 */
1331 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1332 fprintf(stderr, "Only invalid modifier specified\n");
1333 errno = EINVAL;
1334 }
1335
1336 surf = calloc(1, sizeof *surf);
1337 if (surf == NULL) {
1338 errno = ENOMEM;
1339 return NULL;
1340 }
1341
1342 surf->base.gbm = gbm;
1343 surf->base.width = width;
1344 surf->base.height = height;
1345 surf->base.format = gbm_format_canonicalize(format);
1346 surf->base.flags = flags;
1347 if (!modifiers) {
1348 assert(!count);
1349 return &surf->base;
1350 }
1351
1352 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1353 if (count && !surf->base.modifiers) {
1354 errno = ENOMEM;
1355 free(surf);
1356 return NULL;
1357 }
1358
1359 /* TODO: We are deferring validation of modifiers until the image is actually
1360 * created. This deferred creation can fail due to a modifier-format
1361 * mismatch. The result is the client has a surface but no object to back it.
1362 */
1363 surf->base.count = count;
1364 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1365
1366 return &surf->base;
1367 }
1368
1369 static void
1370 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1371 {
1372 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1373
1374 free(surf->base.modifiers);
1375 free(surf);
1376 }
1377
1378 static void
1379 dri_destroy(struct gbm_device *gbm)
1380 {
1381 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1382 unsigned i;
1383
1384 if (dri->context)
1385 dri->core->destroyContext(dri->context);
1386
1387 dri->core->destroyScreen(dri->screen);
1388 for (i = 0; dri->driver_configs[i]; i++)
1389 free((__DRIconfig *) dri->driver_configs[i]);
1390 free(dri->driver_configs);
1391 dlclose(dri->driver);
1392 free(dri->driver_name);
1393
1394 free(dri);
1395 }
1396
1397 static struct gbm_device *
1398 dri_device_create(int fd)
1399 {
1400 struct gbm_dri_device *dri;
1401 int ret;
1402 bool force_sw;
1403
1404 dri = calloc(1, sizeof *dri);
1405 if (!dri)
1406 return NULL;
1407
1408 dri->base.fd = fd;
1409 dri->base.bo_create = gbm_dri_bo_create;
1410 dri->base.bo_import = gbm_dri_bo_import;
1411 dri->base.bo_map = gbm_dri_bo_map;
1412 dri->base.bo_unmap = gbm_dri_bo_unmap;
1413 dri->base.is_format_supported = gbm_dri_is_format_supported;
1414 dri->base.get_format_modifier_plane_count =
1415 gbm_dri_get_format_modifier_plane_count;
1416 dri->base.bo_write = gbm_dri_bo_write;
1417 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1418 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1419 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1420 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1421 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1422 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1423 dri->base.bo_destroy = gbm_dri_bo_destroy;
1424 dri->base.destroy = dri_destroy;
1425 dri->base.surface_create = gbm_dri_surface_create;
1426 dri->base.surface_destroy = gbm_dri_surface_destroy;
1427
1428 dri->base.name = "drm";
1429
1430 dri->visual_table = gbm_dri_visuals_table;
1431 dri->num_visuals = ARRAY_SIZE(gbm_dri_visuals_table);
1432
1433 mtx_init(&dri->mutex, mtx_plain);
1434
1435 force_sw = env_var_as_boolean("GBM_ALWAYS_SOFTWARE", false);
1436 if (!force_sw) {
1437 ret = dri_screen_create(dri);
1438 if (ret)
1439 ret = dri_screen_create_sw(dri);
1440 } else {
1441 ret = dri_screen_create_sw(dri);
1442 }
1443
1444 if (ret)
1445 goto err_dri;
1446
1447 return &dri->base;
1448
1449 err_dri:
1450 free(dri);
1451
1452 return NULL;
1453 }
1454
1455 struct gbm_backend gbm_dri_backend = {
1456 .backend_name = "dri",
1457 .create_device = dri_device_create,
1458 };