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