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