gbm: wire up fence extension
[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 <errno.h>
34 #include <limits.h>
35 #include <assert.h>
36
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <dlfcn.h>
40 #include <xf86drm.h>
41
42 #include <GL/gl.h> /* dri_interface needs GL types */
43 #include <GL/internal/dri_interface.h>
44
45 #include "gbm_driint.h"
46
47 #include "gbmint.h"
48 #include "loader.h"
49
50 /* For importing wl_buffer */
51 #if HAVE_WAYLAND_PLATFORM
52 #include "../../../egl/wayland/wayland-drm/wayland-drm.h"
53 #endif
54
55 static __DRIimage *
56 dri_lookup_egl_image(__DRIscreen *screen, void *image, void *data)
57 {
58 struct gbm_dri_device *dri = data;
59
60 if (dri->lookup_image == NULL)
61 return NULL;
62
63 return dri->lookup_image(screen, image, dri->lookup_user_data);
64 }
65
66 static __DRIbuffer *
67 dri_get_buffers(__DRIdrawable * driDrawable,
68 int *width, int *height,
69 unsigned int *attachments, int count,
70 int *out_count, void *data)
71 {
72 struct gbm_dri_surface *surf = data;
73 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
74
75 if (dri->get_buffers == NULL)
76 return NULL;
77
78 return dri->get_buffers(driDrawable, width, height, attachments,
79 count, out_count, surf->dri_private);
80 }
81
82 static void
83 dri_flush_front_buffer(__DRIdrawable * driDrawable, void *data)
84 {
85 struct gbm_dri_surface *surf = data;
86 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
87
88 if (dri->flush_front_buffer != NULL)
89 dri->flush_front_buffer(driDrawable, surf->dri_private);
90 }
91
92 static __DRIbuffer *
93 dri_get_buffers_with_format(__DRIdrawable * driDrawable,
94 int *width, int *height,
95 unsigned int *attachments, int count,
96 int *out_count, void *data)
97 {
98 struct gbm_dri_surface *surf = data;
99 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
100
101 if (dri->get_buffers_with_format == NULL)
102 return NULL;
103
104 return
105 dri->get_buffers_with_format(driDrawable, width, height, attachments,
106 count, out_count, surf->dri_private);
107 }
108
109 static int
110 image_get_buffers(__DRIdrawable *driDrawable,
111 unsigned int format,
112 uint32_t *stamp,
113 void *loaderPrivate,
114 uint32_t buffer_mask,
115 struct __DRIimageList *buffers)
116 {
117 struct gbm_dri_surface *surf = loaderPrivate;
118 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
119
120 if (dri->image_get_buffers == NULL)
121 return 0;
122
123 return dri->image_get_buffers(driDrawable, format, stamp,
124 surf->dri_private, buffer_mask, buffers);
125 }
126
127 static void
128 swrast_get_drawable_info(__DRIdrawable *driDrawable,
129 int *x,
130 int *y,
131 int *width,
132 int *height,
133 void *loaderPrivate)
134 {
135 struct gbm_dri_surface *surf = loaderPrivate;
136
137 *x = 0;
138 *y = 0;
139 *width = surf->base.width;
140 *height = surf->base.height;
141 }
142
143 static void
144 swrast_put_image2(__DRIdrawable *driDrawable,
145 int op,
146 int x,
147 int y,
148 int width,
149 int height,
150 int stride,
151 char *data,
152 void *loaderPrivate)
153 {
154 struct gbm_dri_surface *surf = loaderPrivate;
155 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
156
157 dri->swrast_put_image2(driDrawable,
158 op, x, y,
159 width, height, stride,
160 data, surf->dri_private);
161 }
162
163 static void
164 swrast_put_image(__DRIdrawable *driDrawable,
165 int op,
166 int x,
167 int y,
168 int width,
169 int height,
170 char *data,
171 void *loaderPrivate)
172 {
173 return swrast_put_image2(driDrawable, op, x, y, width, height,
174 width * 4, data, loaderPrivate);
175 }
176
177 static void
178 swrast_get_image(__DRIdrawable *driDrawable,
179 int x,
180 int y,
181 int width,
182 int height,
183 char *data,
184 void *loaderPrivate)
185 {
186 struct gbm_dri_surface *surf = loaderPrivate;
187 struct gbm_dri_device *dri = gbm_dri_device(surf->base.gbm);
188
189 dri->swrast_get_image(driDrawable,
190 x, y,
191 width, height,
192 data, surf->dri_private);
193 }
194
195 static const __DRIuseInvalidateExtension use_invalidate = {
196 .base = { __DRI_USE_INVALIDATE, 1 }
197 };
198
199 static const __DRIimageLookupExtension image_lookup_extension = {
200 .base = { __DRI_IMAGE_LOOKUP, 1 },
201
202 .lookupEGLImage = dri_lookup_egl_image
203 };
204
205 static const __DRIdri2LoaderExtension dri2_loader_extension = {
206 .base = { __DRI_DRI2_LOADER, 3 },
207
208 .getBuffers = dri_get_buffers,
209 .flushFrontBuffer = dri_flush_front_buffer,
210 .getBuffersWithFormat = dri_get_buffers_with_format,
211 };
212
213 static const __DRIimageLoaderExtension image_loader_extension = {
214 .base = { __DRI_IMAGE_LOADER, 1 },
215
216 .getBuffers = image_get_buffers,
217 .flushFrontBuffer = dri_flush_front_buffer,
218 };
219
220 static const __DRIswrastLoaderExtension swrast_loader_extension = {
221 .base = { __DRI_SWRAST_LOADER, 2 },
222
223 .getDrawableInfo = swrast_get_drawable_info,
224 .putImage = swrast_put_image,
225 .getImage = swrast_get_image,
226 .putImage2 = swrast_put_image2
227 };
228
229 static const __DRIextension *gbm_dri_screen_extensions[] = {
230 &image_lookup_extension.base,
231 &use_invalidate.base,
232 &dri2_loader_extension.base,
233 &image_loader_extension.base,
234 &swrast_loader_extension.base,
235 NULL,
236 };
237
238 struct dri_extension_match {
239 const char *name;
240 int version;
241 int offset;
242 int optional;
243 };
244
245 static struct dri_extension_match dri_core_extensions[] = {
246 { __DRI2_FLUSH, 1, offsetof(struct gbm_dri_device, flush) },
247 { __DRI_IMAGE, 1, offsetof(struct gbm_dri_device, image) },
248 { __DRI2_FENCE, 2, offsetof(struct gbm_dri_device, fence), 1 },
249 { NULL, 0, 0 }
250 };
251
252 static struct dri_extension_match gbm_dri_device_extensions[] = {
253 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core) },
254 { __DRI_DRI2, 1, offsetof(struct gbm_dri_device, dri2) },
255 { NULL, 0, 0 }
256 };
257
258 static struct dri_extension_match gbm_swrast_device_extensions[] = {
259 { __DRI_CORE, 1, offsetof(struct gbm_dri_device, core), },
260 { __DRI_SWRAST, 1, offsetof(struct gbm_dri_device, swrast) },
261 { NULL, 0, 0 }
262 };
263
264 static int
265 dri_bind_extensions(struct gbm_dri_device *dri,
266 struct dri_extension_match *matches,
267 const __DRIextension **extensions)
268 {
269 int i, j, ret = 0;
270 void *field;
271
272 for (i = 0; extensions[i]; i++) {
273 for (j = 0; matches[j].name; j++) {
274 if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
275 extensions[i]->version >= matches[j].version) {
276 field = ((char *) dri + matches[j].offset);
277 *(const __DRIextension **) field = extensions[i];
278 }
279 }
280 }
281
282 for (j = 0; matches[j].name; j++) {
283 field = ((char *) dri + matches[j].offset);
284 if ((*(const __DRIextension **) field == NULL) && !matches[j].optional) {
285 ret = -1;
286 }
287 }
288
289 return ret;
290 }
291
292 static const __DRIextension **
293 dri_open_driver(struct gbm_dri_device *dri)
294 {
295 const __DRIextension **extensions = NULL;
296 char path[PATH_MAX], *search_paths, *p, *next, *end;
297 char *get_extensions_name;
298
299 search_paths = NULL;
300 /* don't allow setuid apps to use LIBGL_DRIVERS_PATH or GBM_DRIVERS_PATH */
301 if (geteuid() == getuid()) {
302 /* Read GBM_DRIVERS_PATH first for compatibility, but LIBGL_DRIVERS_PATH
303 * is recommended over GBM_DRIVERS_PATH.
304 */
305 search_paths = getenv("GBM_DRIVERS_PATH");
306
307 /* Read LIBGL_DRIVERS_PATH if GBM_DRIVERS_PATH was not set.
308 * LIBGL_DRIVERS_PATH is recommended over GBM_DRIVERS_PATH.
309 */
310 if (search_paths == NULL) {
311 search_paths = getenv("LIBGL_DRIVERS_PATH");
312 }
313 }
314 if (search_paths == NULL)
315 search_paths = DEFAULT_DRIVER_DIR;
316
317 /* Temporarily work around dri driver libs that need symbols in libglapi
318 * but don't automatically link it in.
319 */
320 /* XXX: Library name differs on per platforms basis. Update this as
321 * osx/cygwin/windows/bsd gets support for GBM..
322 */
323 dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
324
325 dri->driver = NULL;
326 end = search_paths + strlen(search_paths);
327 for (p = search_paths; p < end && dri->driver == NULL; p = next + 1) {
328 int len;
329 next = strchr(p, ':');
330 if (next == NULL)
331 next = end;
332
333 len = next - p;
334 #if GLX_USE_TLS
335 snprintf(path, sizeof path,
336 "%.*s/tls/%s_dri.so", len, p, dri->base.driver_name);
337 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
338 #endif
339 if (dri->driver == NULL) {
340 snprintf(path, sizeof path,
341 "%.*s/%s_dri.so", len, p, dri->base.driver_name);
342 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
343 }
344 /* not need continue to loop all paths once the driver is found */
345 if (dri->driver != NULL)
346 break;
347
348 #ifdef ANDROID
349 snprintf(path, sizeof path, "%.*s/gallium_dri.so", len, p);
350 dri->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
351 if (dri->driver == NULL)
352 sprintf("failed to open %s: %s\n", path, dlerror());
353 else
354 break;
355 #endif
356 }
357
358 if (dri->driver == NULL) {
359 fprintf(stderr, "gbm: failed to open any driver (search paths %s)\n",
360 search_paths);
361 fprintf(stderr, "gbm: Last dlopen error: %s\n", dlerror());
362 return NULL;
363 }
364
365 if (asprintf(&get_extensions_name, "%s_%s",
366 __DRI_DRIVER_GET_EXTENSIONS, dri->base.driver_name) != -1) {
367 const __DRIextension **(*get_extensions)(void);
368
369 get_extensions = dlsym(dri->driver, get_extensions_name);
370 free(get_extensions_name);
371
372 if (get_extensions)
373 extensions = get_extensions();
374 }
375
376 if (!extensions)
377 extensions = dlsym(dri->driver, __DRI_DRIVER_EXTENSIONS);
378 if (extensions == NULL) {
379 fprintf(stderr, "gbm: driver exports no extensions (%s)", dlerror());
380 dlclose(dri->driver);
381 }
382
383 return extensions;
384 }
385
386 static int
387 dri_load_driver(struct gbm_dri_device *dri)
388 {
389 const __DRIextension **extensions;
390
391 extensions = dri_open_driver(dri);
392 if (!extensions)
393 return -1;
394
395 if (dri_bind_extensions(dri, gbm_dri_device_extensions, extensions) < 0) {
396 dlclose(dri->driver);
397 fprintf(stderr, "failed to bind extensions\n");
398 return -1;
399 }
400
401 dri->driver_extensions = extensions;
402
403 return 0;
404 }
405
406 static int
407 dri_load_driver_swrast(struct gbm_dri_device *dri)
408 {
409 const __DRIextension **extensions;
410
411 extensions = dri_open_driver(dri);
412 if (!extensions)
413 return -1;
414
415 if (dri_bind_extensions(dri, gbm_swrast_device_extensions, extensions) < 0) {
416 dlclose(dri->driver);
417 fprintf(stderr, "failed to bind extensions\n");
418 return -1;
419 }
420
421 dri->driver_extensions = extensions;
422
423 return 0;
424 }
425
426 static int
427 dri_screen_create_dri2(struct gbm_dri_device *dri, char *driver_name)
428 {
429 const __DRIextension **extensions;
430 int ret = 0;
431
432 dri->base.driver_name = driver_name;
433 if (dri->base.driver_name == NULL)
434 return -1;
435
436 ret = dri_load_driver(dri);
437 if (ret) {
438 fprintf(stderr, "failed to load driver: %s\n", dri->base.driver_name);
439 return ret;
440 };
441
442 dri->extensions = gbm_dri_screen_extensions;
443
444 if (dri->dri2 == NULL)
445 return -1;
446
447 if (dri->dri2->base.version >= 4) {
448 dri->screen = dri->dri2->createNewScreen2(0, dri->base.base.fd,
449 dri->extensions,
450 dri->driver_extensions,
451 &dri->driver_configs, dri);
452 } else {
453 dri->screen = dri->dri2->createNewScreen(0, dri->base.base.fd,
454 dri->extensions,
455 &dri->driver_configs, dri);
456 }
457 if (dri->screen == NULL)
458 return -1;
459
460 extensions = dri->core->getExtensions(dri->screen);
461 if (dri_bind_extensions(dri, dri_core_extensions, extensions) < 0) {
462 ret = -1;
463 goto free_screen;
464 }
465
466 dri->lookup_image = NULL;
467 dri->lookup_user_data = NULL;
468
469 return 0;
470
471 free_screen:
472 dri->core->destroyScreen(dri->screen);
473
474 return ret;
475 }
476
477 static int
478 dri_screen_create_swrast(struct gbm_dri_device *dri)
479 {
480 int ret;
481
482 dri->base.driver_name = strdup("swrast");
483 if (dri->base.driver_name == NULL)
484 return -1;
485
486 ret = dri_load_driver_swrast(dri);
487 if (ret) {
488 fprintf(stderr, "failed to load swrast driver\n");
489 return ret;
490 }
491
492 dri->extensions = gbm_dri_screen_extensions;
493
494 if (dri->swrast == NULL)
495 return -1;
496
497 if (dri->swrast->base.version >= 4) {
498 dri->screen = dri->swrast->createNewScreen2(0, dri->extensions,
499 dri->driver_extensions,
500 &dri->driver_configs, dri);
501 } else {
502 dri->screen = dri->swrast->createNewScreen(0, dri->extensions,
503 &dri->driver_configs, dri);
504 }
505 if (dri->screen == NULL)
506 return -1;
507
508 dri->lookup_image = NULL;
509 dri->lookup_user_data = NULL;
510
511 return 0;
512 }
513
514 static int
515 dri_screen_create(struct gbm_dri_device *dri)
516 {
517 char *driver_name;
518
519 driver_name = loader_get_driver_for_fd(dri->base.base.fd, 0);
520 if (!driver_name)
521 return -1;
522
523 return dri_screen_create_dri2(dri, driver_name);
524 }
525
526 static int
527 dri_screen_create_sw(struct gbm_dri_device *dri)
528 {
529 char *driver_name;
530 int ret;
531
532 driver_name = strdup("kms_swrast");
533 if (!driver_name)
534 return -errno;
535
536 ret = dri_screen_create_dri2(dri, driver_name);
537 if (ret == 0)
538 return ret;
539
540 return dri_screen_create_swrast(dri);
541 }
542
543 static int
544 gbm_dri_is_format_supported(struct gbm_device *gbm,
545 uint32_t format,
546 uint32_t usage)
547 {
548 switch (format) {
549 case GBM_BO_FORMAT_XRGB8888:
550 case GBM_FORMAT_XBGR8888:
551 case GBM_FORMAT_XRGB8888:
552 break;
553 case GBM_BO_FORMAT_ARGB8888:
554 case GBM_FORMAT_ARGB8888:
555 if (usage & GBM_BO_USE_SCANOUT)
556 return 0;
557 break;
558 default:
559 return 0;
560 }
561
562 if (usage & GBM_BO_USE_CURSOR &&
563 usage & GBM_BO_USE_RENDERING)
564 return 0;
565
566 return 1;
567 }
568
569 static int
570 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
571 {
572 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
573
574 if (bo->image != NULL) {
575 errno = EINVAL;
576 return -1;
577 }
578
579 memcpy(bo->map, buf, count);
580
581 return 0;
582 }
583
584 static int
585 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
586 {
587 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
588 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
589 int fd;
590
591 if (bo->image == NULL)
592 return -1;
593
594 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd);
595
596 return fd;
597 }
598
599 static void
600 gbm_dri_bo_destroy(struct gbm_bo *_bo)
601 {
602 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
603 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
604 struct drm_mode_destroy_dumb arg;
605
606 if (bo->image != NULL) {
607 dri->image->destroyImage(bo->image);
608 } else {
609 gbm_dri_bo_unmap_dumb(bo);
610 memset(&arg, 0, sizeof(arg));
611 arg.handle = bo->handle;
612 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
613 }
614
615 free(bo);
616 }
617
618 static uint32_t
619 gbm_dri_to_gbm_format(uint32_t dri_format)
620 {
621 uint32_t ret = 0;
622
623 switch (dri_format) {
624 case __DRI_IMAGE_FORMAT_RGB565:
625 ret = GBM_FORMAT_RGB565;
626 break;
627 case __DRI_IMAGE_FORMAT_XRGB8888:
628 ret = GBM_FORMAT_XRGB8888;
629 break;
630 case __DRI_IMAGE_FORMAT_ARGB8888:
631 ret = GBM_FORMAT_ARGB8888;
632 break;
633 case __DRI_IMAGE_FORMAT_XBGR8888:
634 ret = GBM_FORMAT_XBGR8888;
635 break;
636 case __DRI_IMAGE_FORMAT_ABGR8888:
637 ret = GBM_FORMAT_ABGR8888;
638 break;
639 default:
640 ret = 0;
641 break;
642 }
643
644 return ret;
645 }
646
647 static struct gbm_bo *
648 gbm_dri_bo_import(struct gbm_device *gbm,
649 uint32_t type, void *buffer, uint32_t usage)
650 {
651 struct gbm_dri_device *dri = gbm_dri_device(gbm);
652 struct gbm_dri_bo *bo;
653 __DRIimage *image;
654 unsigned dri_use = 0;
655 int gbm_format;
656
657 /* Required for query image WIDTH & HEIGHT */
658 if (dri->image == NULL || dri->image->base.version < 4) {
659 errno = ENOSYS;
660 return NULL;
661 }
662
663 switch (type) {
664 #if HAVE_WAYLAND_PLATFORM
665 case GBM_BO_IMPORT_WL_BUFFER:
666 {
667 struct wl_drm_buffer *wb;
668
669 if (!dri->wl_drm) {
670 errno = EINVAL;
671 return NULL;
672 }
673
674 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
675 if (!wb) {
676 errno = EINVAL;
677 return NULL;
678 }
679
680 image = dri->image->dupImage(wb->driver_buffer, NULL);
681
682 switch (wb->format) {
683 case WL_DRM_FORMAT_XRGB8888:
684 gbm_format = GBM_FORMAT_XRGB8888;
685 break;
686 case WL_DRM_FORMAT_ARGB8888:
687 gbm_format = GBM_FORMAT_ARGB8888;
688 break;
689 case WL_DRM_FORMAT_RGB565:
690 gbm_format = GBM_FORMAT_RGB565;
691 break;
692 case WL_DRM_FORMAT_YUYV:
693 gbm_format = GBM_FORMAT_YUYV;
694 break;
695 default:
696 return NULL;
697 }
698 break;
699 }
700 #endif
701
702 case GBM_BO_IMPORT_EGL_IMAGE:
703 {
704 int dri_format;
705 if (dri->lookup_image == NULL) {
706 errno = EINVAL;
707 return NULL;
708 }
709
710 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
711 image = dri->image->dupImage(image, NULL);
712 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
713 gbm_format = gbm_dri_to_gbm_format(dri_format);
714 if (gbm_format == 0) {
715 errno = EINVAL;
716 return NULL;
717 }
718 break;
719 }
720
721 case GBM_BO_IMPORT_FD:
722 {
723 struct gbm_import_fd_data *fd_data = buffer;
724 int stride = fd_data->stride, offset = 0;
725 int dri_format;
726
727 switch (fd_data->format) {
728 case GBM_BO_FORMAT_XRGB8888:
729 dri_format = GBM_FORMAT_XRGB8888;
730 break;
731 case GBM_BO_FORMAT_ARGB8888:
732 dri_format = GBM_FORMAT_ARGB8888;
733 break;
734 default:
735 dri_format = fd_data->format;
736 }
737
738 image = dri->image->createImageFromFds(dri->screen,
739 fd_data->width,
740 fd_data->height,
741 dri_format,
742 &fd_data->fd, 1,
743 &stride, &offset,
744 NULL);
745 if (image == NULL) {
746 errno = EINVAL;
747 return NULL;
748 }
749 gbm_format = fd_data->format;
750 break;
751 }
752
753 default:
754 errno = ENOSYS;
755 return NULL;
756 }
757
758
759 bo = calloc(1, sizeof *bo);
760 if (bo == NULL)
761 return NULL;
762
763 bo->image = image;
764
765 if (usage & GBM_BO_USE_SCANOUT)
766 dri_use |= __DRI_IMAGE_USE_SCANOUT;
767 if (usage & GBM_BO_USE_CURSOR)
768 dri_use |= __DRI_IMAGE_USE_CURSOR;
769 if (dri->image->base.version >= 2 &&
770 !dri->image->validateUsage(bo->image, dri_use)) {
771 errno = EINVAL;
772 free(bo);
773 return NULL;
774 }
775
776 bo->base.base.gbm = gbm;
777 bo->base.base.format = gbm_format;
778
779 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
780 (int*)&bo->base.base.width);
781 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
782 (int*)&bo->base.base.height);
783 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
784 (int*)&bo->base.base.stride);
785 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
786 &bo->base.base.handle.s32);
787
788 return &bo->base.base;
789 }
790
791 static struct gbm_bo *
792 create_dumb(struct gbm_device *gbm,
793 uint32_t width, uint32_t height,
794 uint32_t format, uint32_t usage)
795 {
796 struct gbm_dri_device *dri = gbm_dri_device(gbm);
797 struct drm_mode_create_dumb create_arg;
798 struct gbm_dri_bo *bo;
799 struct drm_mode_destroy_dumb destroy_arg;
800 int ret;
801 int is_cursor, is_scanout;
802
803 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
804 format == GBM_FORMAT_ARGB8888;
805 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
806 format == GBM_FORMAT_XRGB8888;
807 if (!is_cursor && !is_scanout) {
808 errno = EINVAL;
809 return NULL;
810 }
811
812 bo = calloc(1, sizeof *bo);
813 if (bo == NULL)
814 return NULL;
815
816 memset(&create_arg, 0, sizeof(create_arg));
817 create_arg.bpp = 32;
818 create_arg.width = width;
819 create_arg.height = height;
820
821 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
822 if (ret)
823 goto free_bo;
824
825 bo->base.base.gbm = gbm;
826 bo->base.base.width = width;
827 bo->base.base.height = height;
828 bo->base.base.stride = create_arg.pitch;
829 bo->base.base.format = format;
830 bo->base.base.handle.u32 = create_arg.handle;
831 bo->handle = create_arg.handle;
832 bo->size = create_arg.size;
833
834 if (gbm_dri_bo_map_dumb(bo) == NULL)
835 goto destroy_dumb;
836
837 return &bo->base.base;
838
839 destroy_dumb:
840 memset(&destroy_arg, 0, sizeof destroy_arg);
841 destroy_arg.handle = create_arg.handle;
842 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
843 free_bo:
844 free(bo);
845
846 return NULL;
847 }
848
849 static struct gbm_bo *
850 gbm_dri_bo_create(struct gbm_device *gbm,
851 uint32_t width, uint32_t height,
852 uint32_t format, uint32_t usage)
853 {
854 struct gbm_dri_device *dri = gbm_dri_device(gbm);
855 struct gbm_dri_bo *bo;
856 int dri_format;
857 unsigned dri_use = 0;
858
859 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
860 return create_dumb(gbm, width, height, format, usage);
861
862 bo = calloc(1, sizeof *bo);
863 if (bo == NULL)
864 return NULL;
865
866 bo->base.base.gbm = gbm;
867 bo->base.base.width = width;
868 bo->base.base.height = height;
869 bo->base.base.format = format;
870
871 switch (format) {
872 case GBM_FORMAT_R8:
873 dri_format = __DRI_IMAGE_FORMAT_R8;
874 break;
875 case GBM_FORMAT_GR88:
876 dri_format = __DRI_IMAGE_FORMAT_GR88;
877 break;
878 case GBM_FORMAT_RGB565:
879 dri_format = __DRI_IMAGE_FORMAT_RGB565;
880 break;
881 case GBM_FORMAT_XRGB8888:
882 case GBM_BO_FORMAT_XRGB8888:
883 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
884 break;
885 case GBM_FORMAT_ARGB8888:
886 case GBM_BO_FORMAT_ARGB8888:
887 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
888 break;
889 case GBM_FORMAT_ABGR8888:
890 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
891 break;
892 case GBM_FORMAT_XBGR8888:
893 dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
894 break;
895 case GBM_FORMAT_ARGB2101010:
896 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
897 break;
898 case GBM_FORMAT_XRGB2101010:
899 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
900 break;
901 default:
902 errno = EINVAL;
903 goto failed;
904 }
905
906 if (usage & GBM_BO_USE_SCANOUT)
907 dri_use |= __DRI_IMAGE_USE_SCANOUT;
908 if (usage & GBM_BO_USE_CURSOR)
909 dri_use |= __DRI_IMAGE_USE_CURSOR;
910 if (usage & GBM_BO_USE_LINEAR)
911 dri_use |= __DRI_IMAGE_USE_LINEAR;
912
913 /* Gallium drivers requires shared in order to get the handle/stride */
914 dri_use |= __DRI_IMAGE_USE_SHARE;
915
916 bo->image =
917 dri->image->createImage(dri->screen,
918 width, height,
919 dri_format, dri_use,
920 bo);
921 if (bo->image == NULL)
922 goto failed;
923
924 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
925 &bo->base.base.handle.s32);
926 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
927 (int *) &bo->base.base.stride);
928
929 return &bo->base.base;
930
931 failed:
932 free(bo);
933 return NULL;
934 }
935
936 static void *
937 gbm_dri_bo_map(struct gbm_bo *_bo,
938 uint32_t x, uint32_t y,
939 uint32_t width, uint32_t height,
940 uint32_t flags, uint32_t *stride, void **map_data)
941 {
942 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
943 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
944
945 /* If it's a dumb buffer, we already have a mapping */
946 if (bo->map) {
947 *map_data = (char *)bo->map + (bo->base.base.stride * y) + (x * 4);
948 *stride = bo->base.base.stride;
949 return *map_data;
950 }
951
952 if (!dri->image || dri->image->base.version < 12) {
953 errno = ENOSYS;
954 return NULL;
955 }
956
957 mtx_lock(&dri->mutex);
958 if (!dri->context)
959 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
960 NULL, NULL);
961 assert(dri->context);
962 mtx_unlock(&dri->mutex);
963
964 /* GBM flags and DRI flags are the same, so just pass them on */
965 return dri->image->mapImage(dri->context, bo->image, x, y,
966 width, height, flags, (int *)stride,
967 map_data);
968 }
969
970 static void
971 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
972 {
973 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
974 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
975
976 /* Check if it's a dumb buffer and check the pointer is in range */
977 if (bo->map) {
978 assert(map_data >= bo->map);
979 assert(map_data < (bo->map + bo->size));
980 return;
981 }
982
983 if (!dri->context || !dri->image || dri->image->base.version < 12)
984 return;
985
986 dri->image->unmapImage(dri->context, bo->image, map_data);
987 }
988
989
990 static struct gbm_surface *
991 gbm_dri_surface_create(struct gbm_device *gbm,
992 uint32_t width, uint32_t height,
993 uint32_t format, uint32_t flags)
994 {
995 struct gbm_dri_surface *surf;
996
997 surf = calloc(1, sizeof *surf);
998 if (surf == NULL)
999 return NULL;
1000
1001 surf->base.gbm = gbm;
1002 surf->base.width = width;
1003 surf->base.height = height;
1004 surf->base.format = format;
1005 surf->base.flags = flags;
1006
1007 return &surf->base;
1008 }
1009
1010 static void
1011 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1012 {
1013 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1014
1015 free(surf);
1016 }
1017
1018 static void
1019 dri_destroy(struct gbm_device *gbm)
1020 {
1021 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1022 unsigned i;
1023
1024 if (dri->context)
1025 dri->core->destroyContext(dri->context);
1026
1027 dri->core->destroyScreen(dri->screen);
1028 for (i = 0; dri->driver_configs[i]; i++)
1029 free((__DRIconfig *) dri->driver_configs[i]);
1030 free(dri->driver_configs);
1031 dlclose(dri->driver);
1032 free(dri->base.driver_name);
1033
1034 free(dri);
1035 }
1036
1037 static struct gbm_device *
1038 dri_device_create(int fd)
1039 {
1040 struct gbm_dri_device *dri;
1041 int ret, force_sw;
1042
1043 dri = calloc(1, sizeof *dri);
1044 if (!dri)
1045 return NULL;
1046
1047 dri->base.base.fd = fd;
1048 dri->base.base.bo_create = gbm_dri_bo_create;
1049 dri->base.base.bo_import = gbm_dri_bo_import;
1050 dri->base.base.bo_map = gbm_dri_bo_map;
1051 dri->base.base.bo_unmap = gbm_dri_bo_unmap;
1052 dri->base.base.is_format_supported = gbm_dri_is_format_supported;
1053 dri->base.base.bo_write = gbm_dri_bo_write;
1054 dri->base.base.bo_get_fd = gbm_dri_bo_get_fd;
1055 dri->base.base.bo_destroy = gbm_dri_bo_destroy;
1056 dri->base.base.destroy = dri_destroy;
1057 dri->base.base.surface_create = gbm_dri_surface_create;
1058 dri->base.base.surface_destroy = gbm_dri_surface_destroy;
1059
1060 dri->base.type = GBM_DRM_DRIVER_TYPE_DRI;
1061 dri->base.base.name = "drm";
1062
1063 mtx_init(&dri->mutex, mtx_plain);
1064
1065 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1066 if (!force_sw) {
1067 ret = dri_screen_create(dri);
1068 if (ret)
1069 ret = dri_screen_create_sw(dri);
1070 } else {
1071 ret = dri_screen_create_sw(dri);
1072 }
1073
1074 if (ret)
1075 goto err_dri;
1076
1077 return &dri->base.base;
1078
1079 err_dri:
1080 free(dri);
1081
1082 return NULL;
1083 }
1084
1085 struct gbm_backend gbm_dri_backend = {
1086 .backend_name = "dri",
1087 .create_device = dri_device_create,
1088 };