gbm: Add gbm_bo_get_fd()
[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 <string.h>
33 #include <limits.h>
34
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <unistd.h>
38 #include <dlfcn.h>
39 #include <xf86drm.h>
40
41 #include <GL/gl.h> /* dri_interface needs GL types */
42 #include <GL/internal/dri_interface.h>
43
44 #include "gbm_driint.h"
45
46 #include "gbmint.h"
47 #include "loader.h"
48
49 /* For importing wl_buffer */
50 #if HAVE_WAYLAND_PLATFORM
51 #include "../../../egl/wayland/wayland-drm/wayland-drm.h"
52 #endif
53
54 static __DRIimage *
55 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
56 {
57 struct gbm_dri_device *dri = data;
58
59 if (dri->lookup_image == NULL)
60 return NULL;
61
62 return dri->lookup_image(screen, image, dri->lookup_user_data);
63 }
64
65 static __DRIbuffer *
66 dri_get_buffers(__DRIdrawable * driDrawable,
67 int *width, int *height,
68 unsigned int *attachments, int count,
69 int *out_count, void *data)
70 {
71 struct gbm_dri_surface *surf = data;
72 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
73
74 if (dri->get_buffers == NULL)
75 return NULL;
76
77 return dri->get_buffers(driDrawable, width, height, attachments,
78 count, out_count, surf->dri_private);
79 }
80
81 static void
82 dri_flush_front_buffer(__DRIdrawable * driDrawable, 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->flush_front_buffer != NULL)
88 dri->flush_front_buffer(driDrawable, surf->dri_private);
89 }
90
91 static __DRIbuffer *
92 dri_get_buffers_with_format(__DRIdrawable * driDrawable,
93 int *width, int *height,
94 unsigned int *attachments, int count,
95 int *out_count, 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->get_buffers_with_format == NULL)
101 return NULL;
102
103 return
104 dri->get_buffers_with_format(driDrawable, width, height, attachments,
105 count, out_count, surf->dri_private);
106 }
107
108 static int
109 image_get_buffers(__DRIdrawable *driDrawable,
110 unsigned int format,
111 uint32_t *stamp,
112 void *loaderPrivate,
113 uint32_t buffer_mask,
114 struct __DRIimageList *buffers)
115 {
116 struct gbm_dri_surface *surf = loaderPrivate;
117 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
118
119 if (dri->image_get_buffers == NULL)
120 return 0;
121
122 return dri->image_get_buffers(driDrawable, format, stamp,
123 surf->dri_private, buffer_mask, buffers);
124 }
125
126 static const __DRIuseInvalidateExtension use_invalidate = {
127 { __DRI_USE_INVALIDATE, 1 }
128 };
129
130 static const __DRIimageLookupExtension image_lookup_extension = {
131 { __DRI_IMAGE_LOOKUP, 1 },
132 dri_lookup_egl_image
133 };
134
135 static const __DRIdri2LoaderExtension dri2_loader_extension = {
136 .base = { __DRI_DRI2_LOADER, 3 },
137
138 .getBuffers = dri_get_buffers,
139 .flushFrontBuffer = dri_flush_front_buffer,
140 .getBuffersWithFormat = dri_get_buffers_with_format,
141 };
142
143 static const __DRIimageLoaderExtension image_loader_extension = {
144 .base = { __DRI_IMAGE_LOADER, 1 },
145
146 .getBuffers = image_get_buffers,
147 .flushFrontBuffer = dri_flush_front_buffer,
148 };
149
150
151 struct dri_extension_match {
152 const char *name;
153 int version;
154 int offset;
155 };
156
157 static struct dri_extension_match dri_core_extensions[] = {
158 { __DRI2_FLUSH, 1, offsetof(struct gbm_dri_device, flush) },
159 { __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image) },
160 { NULL, 0, 0 }
161 };
162
163 static struct dri_extension_match gbm_dri_device_extensions[] = {
164 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core) },
165 { __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2) },
166 { NULL, 0, 0 }
167 };
168
169 static int
170 dri_bind_extensions(struct gbm_dri_device *dri,
171 struct dri_extension_match *matches,
172 const __DRIextension **extensions)
173 {
174 int i, j, ret = 0;
175 void *field;
176
177 for (i = 0; extensions[i]; i++) {
178 for (j = 0; matches[j].name; j++) {
179 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
180 extensions[i]->version >= matches[j].version) {
181 field = ((char *) dri + matches[j].offset);
182 *(const __DRIextension **) field = extensions[i];
183 }
184 }
185 }
186
187 for (j = 0; matches[j].name; j++) {
188 field = ((char *) dri + matches[j].offset);
189 if (*(const __DRIextension **) field == NULL) {
190 ret = -1;
191 }
192 }
193
194 return ret;
195 }
196
197 static int
198 dri_load_driver(struct gbm_dri_device *dri)
199 {
200 const __DRIextension **extensions = NULL;
201 char path[PATH_MAX], *search_paths, *p, *next, *end;
202 char *get_extensions_name;
203
204 search_paths = NULL;
205 if (geteuid() == getuid()) {
206 /* don't allow setuid apps to use GBM_DRIVERS_PATH */
207 search_paths = getenv("GBM_DRIVERS_PATH");
208 }
209 if (search_paths == NULL)
210 search_paths = DEFAULT_DRIVER_DIR;
211
212 dri->driver = NULL;
213 end = search_paths + strlen(search_paths);
214 for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
215 int len;
216 next = strchr(p, ':');
217 if (next == NULL)
218 next = end;
219
220 len = next - p;
221 #if GLX_USE_TLS
222 snprintf(path, sizeof path,
223 "%.*s/tls/%s_dri.so", len, p, dri->base.driver_name);
224 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
225 #endif
226 if (dri->driver == NULL) {
227 snprintf(path, sizeof path,
228 "%.*s/%s_dri.so", len, p, dri->base.driver_name);
229 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
230 }
231 /* not need continue to loop all paths once the driver is found */
232 if (dri->driver != NULL)
233 break;
234 }
235
236 if (dri->driver == NULL) {
237 fprintf(stderr, "gbm: failed to open any driver (search paths %s)",
238 search_paths);
239 return -1;
240 }
241
242 if (asprintf(&get_extensions_name, "%s_%s",
243 __DRI_DRIVER_GET_EXTENSIONS, dri->base.driver_name) != -1) {
244 const __DRIextension **(*get_extensions)(void);
245
246 get_extensions = dlsym(dri->driver, get_extensions_name);
247 free(get_extensions_name);
248
249 if (get_extensions)
250 extensions = get_extensions();
251 }
252
253 if (!extensions)
254 extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
255 if (extensions == NULL) {
256 fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
257 dlclose(dri->driver);
258 return -1;
259 }
260 dri->driver_extensions = extensions;
261
262 if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
263 dlclose(dri->driver);
264 fprintf(stderr, "failed to bind extensions\n");
265 return -1;
266 }
267
268 return 0;
269 }
270
271 static int
272 dri_screen_create(struct gbm_dri_device *dri)
273 {
274 const __DRIextension **extensions;
275 int ret = 0;
276
277 dri->base.driver_name = loader_get_driver_for_fd(dri->base.base.fd, 0);
278 if (dri->base.driver_name == NULL)
279 return -1;
280
281 ret = dri_load_driver(dri);
282 if (ret) {
283 fprintf(stderr, "failed to load driver: %s\n", dri->base.driver_name);
284 return ret;
285 };
286
287 dri->extensions[0] = &image_lookup_extension.base;
288 dri->extensions[1] = &use_invalidate.base;
289 dri->extensions[2] = &dri2_loader_extension.base;
290 dri->extensions[3] = &image_loader_extension.base;
291 dri->extensions[4] = NULL;
292
293 if (dri->dri2 == NULL)
294 return -1;
295
296 if (dri->dri2->base.version >= 4) {
297 dri->screen = dri->dri2->createNewScreen2(0, dri->base.base.fd,
298 dri->extensions,
299 dri->driver_extensions,
300 &dri->driver_configs, dri);
301 } else {
302 dri->screen = dri->dri2->createNewScreen(0, dri->base.base.fd,
303 dri->extensions,
304 &dri->driver_configs, dri);
305 }
306 if (dri->screen == NULL)
307 return -1;
308
309 extensions = dri->core->getExtensions(dri->screen);
310 if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
311 ret = -1;
312 goto free_screen;
313 }
314
315 dri->lookup_image = NULL;
316 dri->lookup_user_data = NULL;
317
318 return 0;
319
320 free_screen:
321 dri->core->destroyScreen(dri->screen);
322
323 return ret;
324 }
325
326 static int
327 gbm_dri_is_format_supported(struct gbm_device *gbm,
328 uint32_t format,
329 uint32_t usage)
330 {
331 switch (format) {
332 case GBM_BO_FORMAT_XRGB8888:
333 case GBM_FORMAT_XRGB8888:
334 break;
335 case GBM_BO_FORMAT_ARGB8888:
336 case GBM_FORMAT_ARGB8888:
337 if (usage & GBM_BO_USE_SCANOUT)
338 return 0;
339 break;
340 default:
341 return 0;
342 }
343
344 if (usage & GBM_BO_USE_CURSOR_64X64 &&
345 usage & GBM_BO_USE_RENDERING)
346 return 0;
347
348 return 1;
349 }
350
351 static int
352 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
353 {
354 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
355
356 if (bo->image != NULL)
357 return -1;
358
359 memcpy(bo->map, buf, count);
360
361 return 0;
362 }
363
364 static int
365 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
366 {
367 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
368 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
369 int fd;
370
371 if (bo->image == NULL)
372 return -1;
373
374 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd);
375
376 return fd;
377 }
378
379 static void
380 gbm_dri_bo_destroy(struct gbm_bo *_bo)
381 {
382 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
383 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
384 struct drm_mode_destroy_dumb arg;
385
386 if (bo->image != NULL) {
387 dri->image->destroyImage(bo->image);
388 } else {
389 munmap(bo->map, bo->size);
390 memset(&arg, 0, sizeof(arg));
391 arg.handle = bo->handle;
392 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
393 }
394
395 free(bo);
396 }
397
398 static uint32_t
399 gbm_dri_to_gbm_format(uint32_t dri_format)
400 {
401 uint32_t ret = 0;
402
403 switch (dri_format) {
404 case __DRI_IMAGE_FORMAT_RGB565:
405 ret = GBM_FORMAT_RGB565;
406 break;
407 case __DRI_IMAGE_FORMAT_XRGB8888:
408 ret = GBM_FORMAT_XRGB8888;
409 break;
410 case __DRI_IMAGE_FORMAT_ARGB8888:
411 ret = GBM_FORMAT_ARGB8888;
412 break;
413 case __DRI_IMAGE_FORMAT_ABGR8888:
414 ret = GBM_FORMAT_ABGR8888;
415 break;
416 default:
417 ret = 0;
418 break;
419 }
420
421 return ret;
422 }
423
424 static struct gbm_bo *
425 gbm_dri_bo_import(struct gbm_device *gbm,
426 uint32_t type, void *buffer, uint32_t usage)
427 {
428 struct gbm_dri_device *dri = gbm_dri_device(gbm);
429 struct gbm_dri_bo *bo;
430 __DRIimage *image;
431 unsigned dri_use = 0;
432 int gbm_format;
433
434 /* Required for query image WIDTH & HEIGHT */
435 if (dri->image->base.version < 4)
436 return NULL;
437
438 switch (type) {
439 #if HAVE_WAYLAND_PLATFORM
440 case GBM_BO_IMPORT_WL_BUFFER:
441 {
442 struct wl_drm_buffer *wb;
443
444 if (!dri->wl_drm)
445 return NULL;
446
447 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
448 if (!wb)
449 return NULL;
450
451 image = wb->driver_buffer;
452
453 switch (wb->format) {
454 case WL_DRM_FORMAT_XRGB8888:
455 gbm_format = GBM_FORMAT_XRGB8888;
456 break;
457 case WL_DRM_FORMAT_ARGB8888:
458 gbm_format = GBM_FORMAT_ARGB8888;
459 break;
460 case WL_DRM_FORMAT_RGB565:
461 gbm_format = GBM_FORMAT_RGB565;
462 break;
463 case WL_DRM_FORMAT_YUYV:
464 gbm_format = GBM_FORMAT_YUYV;
465 break;
466 default:
467 return NULL;
468 }
469 break;
470 }
471 #endif
472
473 case GBM_BO_IMPORT_EGL_IMAGE:
474 {
475 int dri_format;
476 if (dri->lookup_image == NULL)
477 return NULL;
478
479 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
480 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
481 gbm_format = gbm_dri_to_gbm_format(dri_format);
482 if (gbm_format == 0)
483 return NULL;
484 break;
485 }
486
487 default:
488 return NULL;
489 }
490
491
492 bo = calloc(1, sizeof *bo);
493 if (bo == NULL)
494 return NULL;
495
496 bo->image = dri->image->dupImage(image, NULL);
497
498 if (usage & GBM_BO_USE_SCANOUT)
499 dri_use |= __DRI_IMAGE_USE_SCANOUT;
500 if (usage & GBM_BO_USE_CURSOR_64X64)
501 dri_use |= __DRI_IMAGE_USE_CURSOR;
502 if (dri->image->base.version >= 2 &&
503 !dri->image->validateUsage(bo->image, dri_use)) {
504 free(bo);
505 return NULL;
506 }
507
508 bo->base.base.gbm = gbm;
509 bo->base.base.format = gbm_format;
510
511 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
512 (int*)&bo->base.base.width);
513 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
514 (int*)&bo->base.base.height);
515 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
516 (int*)&bo->base.base.stride);
517 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
518 &bo->base.base.handle.s32);
519
520 return &bo->base.base;
521 }
522
523 static struct gbm_bo *
524 create_dumb(struct gbm_device *gbm,
525 uint32_t width, uint32_t height,
526 uint32_t format, uint32_t usage)
527 {
528 struct gbm_dri_device *dri = gbm_dri_device(gbm);
529 struct drm_mode_create_dumb create_arg;
530 struct drm_mode_map_dumb map_arg;
531 struct gbm_dri_bo *bo;
532 struct drm_mode_destroy_dumb destroy_arg;
533 int ret;
534
535 if (!(usage & GBM_BO_USE_CURSOR_64X64))
536 return NULL;
537 if (format != GBM_FORMAT_ARGB8888)
538 return NULL;
539
540 bo = calloc(1, sizeof *bo);
541 if (bo == NULL)
542 return NULL;
543
544 create_arg.bpp = 32;
545 create_arg.width = width;
546 create_arg.height = height;
547
548 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
549 if (ret)
550 goto free_bo;
551
552 bo->base.base.gbm = gbm;
553 bo->base.base.width = width;
554 bo->base.base.height = height;
555 bo->base.base.stride = create_arg.pitch;
556 bo->base.base.format = format;
557 bo->base.base.handle.u32 = create_arg.handle;
558 bo->handle = create_arg.handle;
559 bo->size = create_arg.size;
560
561 memset(&map_arg, 0, sizeof(map_arg));
562 map_arg.handle = bo->handle;
563
564 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
565 if (ret)
566 goto destroy_dumb;
567
568 bo->map = mmap(0, bo->size, PROT_WRITE,
569 MAP_SHARED, dri->base.base.fd, map_arg.offset);
570 if (bo->map == MAP_FAILED)
571 goto destroy_dumb;
572
573 return &bo->base.base;
574
575 destroy_dumb:
576 memset(&destroy_arg, 0, sizeof destroy_arg);
577 destroy_arg.handle = create_arg.handle;
578 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
579 free_bo:
580 free(bo);
581
582 return NULL;
583 }
584
585 static struct gbm_bo *
586 gbm_dri_bo_create(struct gbm_device *gbm,
587 uint32_t width, uint32_t height,
588 uint32_t format, uint32_t usage)
589 {
590 struct gbm_dri_device *dri = gbm_dri_device(gbm);
591 struct gbm_dri_bo *bo;
592 int dri_format;
593 unsigned dri_use = 0;
594
595 if (usage & GBM_BO_USE_WRITE)
596 return create_dumb(gbm, width, height, format, usage);
597
598 bo = calloc(1, sizeof *bo);
599 if (bo == NULL)
600 return NULL;
601
602 bo->base.base.gbm = gbm;
603 bo->base.base.width = width;
604 bo->base.base.height = height;
605 bo->base.base.format = format;
606
607 switch (format) {
608 case GBM_FORMAT_RGB565:
609 dri_format =__DRI_IMAGE_FORMAT_RGB565;
610 break;
611 case GBM_FORMAT_XRGB8888:
612 case GBM_BO_FORMAT_XRGB8888:
613 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
614 break;
615 case GBM_FORMAT_ARGB8888:
616 case GBM_BO_FORMAT_ARGB8888:
617 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
618 break;
619 case GBM_FORMAT_ABGR8888:
620 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
621 break;
622 case GBM_FORMAT_ARGB2101010:
623 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
624 break;
625 case GBM_FORMAT_XRGB2101010:
626 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
627 break;
628 default:
629 goto failed;
630 }
631
632 if (usage & GBM_BO_USE_SCANOUT)
633 dri_use |= __DRI_IMAGE_USE_SCANOUT;
634 if (usage & GBM_BO_USE_CURSOR_64X64)
635 dri_use |= __DRI_IMAGE_USE_CURSOR;
636
637 /* Gallium drivers requires shared in order to get the handle/stride */
638 dri_use |= __DRI_IMAGE_USE_SHARE;
639
640 bo->image =
641 dri->image->createImage(dri->screen,
642 width, height,
643 dri_format, dri_use,
644 bo);
645 if (bo->image == NULL)
646 goto failed;
647
648 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
649 &bo->base.base.handle.s32);
650 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
651 (int *) &bo->base.base.stride);
652
653 return &bo->base.base;
654
655 failed:
656 free(bo);
657 return NULL;
658 }
659
660 static struct gbm_surface *
661 gbm_dri_surface_create(struct gbm_device *gbm,
662 uint32_t width, uint32_t height,
663 uint32_t format, uint32_t flags)
664 {
665 struct gbm_dri_surface *surf;
666
667 surf = calloc(1, sizeof *surf);
668 if (surf == NULL)
669 return NULL;
670
671 surf->base.gbm = gbm;
672 surf->base.width = width;
673 surf->base.height = height;
674 surf->base.format = format;
675 surf->base.flags = flags;
676
677 return &surf->base;
678 }
679
680 static void
681 gbm_dri_surface_destroy(struct gbm_surface *_surf)
682 {
683 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
684
685 free(surf);
686 }
687
688 static void
689 dri_destroy(struct gbm_device *gbm)
690 {
691 struct gbm_dri_device *dri = gbm_dri_device(gbm);
692
693 dri->core->destroyScreen(dri->screen);
694 free(dri->driver_configs);
695 dlclose(dri->driver);
696 free(dri->base.driver_name);
697
698 free(dri);
699 }
700
701 static struct gbm_device *
702 dri_device_create(int fd)
703 {
704 struct gbm_dri_device *dri;
705 int ret;
706
707 dri = calloc(1, sizeof *dri);
708
709 dri->base.base.fd = fd;
710 dri->base.base.bo_create = gbm_dri_bo_create;
711 dri->base.base.bo_import = gbm_dri_bo_import;
712 dri->base.base.is_format_supported = gbm_dri_is_format_supported;
713 dri->base.base.bo_write = gbm_dri_bo_write;
714 dri->base.base.bo_get_fd = gbm_dri_bo_get_fd;
715 dri->base.base.bo_destroy = gbm_dri_bo_destroy;
716 dri->base.base.destroy = dri_destroy;
717 dri->base.base.surface_create = gbm_dri_surface_create;
718 dri->base.base.surface_destroy = gbm_dri_surface_destroy;
719
720 dri->base.type = GBM_DRM_DRIVER_TYPE_DRI;
721 dri->base.base.name = "drm";
722
723 ret = dri_screen_create(dri);
724 if (ret)
725 goto err_dri;
726
727 return &dri->base.base;
728
729 err_dri:
730 free(dri);
731
732 return NULL;
733 }
734
735 struct gbm_backend gbm_dri_backend = {
736 .backend_name = "dri",
737 .create_device = dri_device_create,
738 };