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