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