gbm: Remove is_planar_format dead code
[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 struct gbm_bo *
1026 create_dumb(struct gbm_device *gbm,
1027 uint32_t width, uint32_t height,
1028 uint32_t format, uint32_t usage)
1029 {
1030 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1031 struct drm_mode_create_dumb create_arg;
1032 struct gbm_dri_bo *bo;
1033 struct drm_mode_destroy_dumb destroy_arg;
1034 int ret;
1035 int is_cursor, is_scanout;
1036
1037 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1038 format == GBM_FORMAT_ARGB8888;
1039 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1040 (format == GBM_FORMAT_XRGB8888 || format == GBM_FORMAT_XBGR8888);
1041 if (!is_cursor && !is_scanout) {
1042 errno = EINVAL;
1043 return NULL;
1044 }
1045
1046 bo = calloc(1, sizeof *bo);
1047 if (bo == NULL)
1048 return NULL;
1049
1050 memset(&create_arg, 0, sizeof(create_arg));
1051 create_arg.bpp = 32;
1052 create_arg.width = width;
1053 create_arg.height = height;
1054
1055 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1056 if (ret)
1057 goto free_bo;
1058
1059 bo->base.gbm = gbm;
1060 bo->base.width = width;
1061 bo->base.height = height;
1062 bo->base.stride = create_arg.pitch;
1063 bo->base.format = format;
1064 bo->base.handle.u32 = create_arg.handle;
1065 bo->handle = create_arg.handle;
1066 bo->size = create_arg.size;
1067
1068 if (gbm_dri_bo_map_dumb(bo) == NULL)
1069 goto destroy_dumb;
1070
1071 return &bo->base;
1072
1073 destroy_dumb:
1074 memset(&destroy_arg, 0, sizeof destroy_arg);
1075 destroy_arg.handle = create_arg.handle;
1076 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1077 free_bo:
1078 free(bo);
1079
1080 return NULL;
1081 }
1082
1083 static struct gbm_bo *
1084 gbm_dri_bo_create(struct gbm_device *gbm,
1085 uint32_t width, uint32_t height,
1086 uint32_t format, uint32_t usage,
1087 const uint64_t *modifiers,
1088 const unsigned int count)
1089 {
1090 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1091 struct gbm_dri_bo *bo;
1092 int dri_format;
1093 unsigned dri_use = 0;
1094
1095 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1096 * newer modifier interface deprecates the older usage flags.
1097 */
1098 assert(!(usage && count));
1099
1100 format = gbm_format_canonicalize(format);
1101
1102 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1103 return create_dumb(gbm, width, height, format, usage);
1104
1105 bo = calloc(1, sizeof *bo);
1106 if (bo == NULL)
1107 return NULL;
1108
1109 bo->base.gbm = gbm;
1110 bo->base.width = width;
1111 bo->base.height = height;
1112 bo->base.format = format;
1113
1114 dri_format = gbm_format_to_dri_format(format);
1115 if (dri_format == 0) {
1116 errno = EINVAL;
1117 goto failed;
1118 }
1119
1120 if (usage & GBM_BO_USE_SCANOUT)
1121 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1122 if (usage & GBM_BO_USE_CURSOR)
1123 dri_use |= __DRI_IMAGE_USE_CURSOR;
1124 if (usage & GBM_BO_USE_LINEAR)
1125 dri_use |= __DRI_IMAGE_USE_LINEAR;
1126
1127 /* Gallium drivers requires shared in order to get the handle/stride */
1128 dri_use |= __DRI_IMAGE_USE_SHARE;
1129
1130 if (modifiers) {
1131 if (!dri->image || dri->image->base.version < 14 ||
1132 !dri->image->createImageWithModifiers) {
1133 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1134 errno = ENOSYS;
1135 goto failed;
1136 }
1137
1138 /* It's acceptable to create an image with INVALID modifier in the list,
1139 * but it cannot be on the only modifier (since it will certainly fail
1140 * later). While we could easily catch this after modifier creation, doing
1141 * the check here is a convenient debug check likely pointing at whatever
1142 * interface the client is using to build its modifier list.
1143 */
1144 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1145 fprintf(stderr, "Only invalid modifier specified\n");
1146 errno = EINVAL;
1147 goto failed;
1148 }
1149
1150 bo->image =
1151 dri->image->createImageWithModifiers(dri->screen,
1152 width, height,
1153 dri_format,
1154 modifiers, count,
1155 bo);
1156
1157 if (bo->image) {
1158 /* The client passed in a list of invalid modifiers */
1159 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1160 }
1161 } else {
1162 bo->image = dri->image->createImage(dri->screen, width, height,
1163 dri_format, dri_use, bo);
1164 }
1165
1166 if (bo->image == NULL)
1167 goto failed;
1168
1169 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1170 &bo->base.handle.s32);
1171 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1172 (int *) &bo->base.stride);
1173
1174 return &bo->base;
1175
1176 failed:
1177 free(bo);
1178 return NULL;
1179 }
1180
1181 static void *
1182 gbm_dri_bo_map(struct gbm_bo *_bo,
1183 uint32_t x, uint32_t y,
1184 uint32_t width, uint32_t height,
1185 uint32_t flags, uint32_t *stride, void **map_data)
1186 {
1187 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1188 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1189
1190 /* If it's a dumb buffer, we already have a mapping */
1191 if (bo->map) {
1192 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1193 *stride = bo->base.stride;
1194 return *map_data;
1195 }
1196
1197 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1198 errno = ENOSYS;
1199 return NULL;
1200 }
1201
1202 mtx_lock(&dri->mutex);
1203 if (!dri->context)
1204 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1205 NULL, NULL);
1206 assert(dri->context);
1207 mtx_unlock(&dri->mutex);
1208
1209 /* GBM flags and DRI flags are the same, so just pass them on */
1210 return dri->image->mapImage(dri->context, bo->image, x, y,
1211 width, height, flags, (int *)stride,
1212 map_data);
1213 }
1214
1215 static void
1216 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1217 {
1218 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1219 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1220
1221 /* Check if it's a dumb buffer and check the pointer is in range */
1222 if (bo->map) {
1223 assert(map_data >= bo->map);
1224 assert(map_data < (bo->map + bo->size));
1225 return;
1226 }
1227
1228 if (!dri->context || !dri->image ||
1229 dri->image->base.version < 12 || !dri->image->unmapImage)
1230 return;
1231
1232 dri->image->unmapImage(dri->context, bo->image, map_data);
1233
1234 /*
1235 * Not all DRI drivers use direct maps. They may queue up DMA operations
1236 * on the mapping context. Since there is no explicit gbm flush
1237 * mechanism, we need to flush here.
1238 */
1239 if (dri->flush->base.version >= 4)
1240 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1241 }
1242
1243
1244 static struct gbm_surface *
1245 gbm_dri_surface_create(struct gbm_device *gbm,
1246 uint32_t width, uint32_t height,
1247 uint32_t format, uint32_t flags,
1248 const uint64_t *modifiers, const unsigned count)
1249 {
1250 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1251 struct gbm_dri_surface *surf;
1252
1253 if (modifiers &&
1254 (!dri->image || dri->image->base.version < 14 ||
1255 !dri->image->createImageWithModifiers)) {
1256 errno = ENOSYS;
1257 return NULL;
1258 }
1259
1260 if (count)
1261 assert(modifiers);
1262
1263 /* It's acceptable to create an image with INVALID modifier in the list,
1264 * but it cannot be on the only modifier (since it will certainly fail
1265 * later). While we could easily catch this after modifier creation, doing
1266 * the check here is a convenient debug check likely pointing at whatever
1267 * interface the client is using to build its modifier list.
1268 */
1269 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1270 fprintf(stderr, "Only invalid modifier specified\n");
1271 errno = EINVAL;
1272 }
1273
1274 surf = calloc(1, sizeof *surf);
1275 if (surf == NULL) {
1276 errno = ENOMEM;
1277 return NULL;
1278 }
1279
1280 surf->base.gbm = gbm;
1281 surf->base.width = width;
1282 surf->base.height = height;
1283 surf->base.format = gbm_format_canonicalize(format);
1284 surf->base.flags = flags;
1285 if (!modifiers) {
1286 assert(!count);
1287 return &surf->base;
1288 }
1289
1290 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1291 if (count && !surf->base.modifiers) {
1292 errno = ENOMEM;
1293 free(surf);
1294 return NULL;
1295 }
1296
1297 /* TODO: We are deferring validation of modifiers until the image is actually
1298 * created. This deferred creation can fail due to a modifier-format
1299 * mismatch. The result is the client has a surface but no object to back it.
1300 */
1301 surf->base.count = count;
1302 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1303
1304 return &surf->base;
1305 }
1306
1307 static void
1308 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1309 {
1310 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1311
1312 free(surf->base.modifiers);
1313 free(surf);
1314 }
1315
1316 static void
1317 dri_destroy(struct gbm_device *gbm)
1318 {
1319 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1320 unsigned i;
1321
1322 if (dri->context)
1323 dri->core->destroyContext(dri->context);
1324
1325 dri->core->destroyScreen(dri->screen);
1326 for (i = 0; dri->driver_configs[i]; i++)
1327 free((__DRIconfig *) dri->driver_configs[i]);
1328 free(dri->driver_configs);
1329 dlclose(dri->driver);
1330 free(dri->driver_name);
1331
1332 free(dri);
1333 }
1334
1335 static struct gbm_device *
1336 dri_device_create(int fd)
1337 {
1338 struct gbm_dri_device *dri;
1339 int ret, force_sw;
1340
1341 dri = calloc(1, sizeof *dri);
1342 if (!dri)
1343 return NULL;
1344
1345 dri->base.fd = fd;
1346 dri->base.bo_create = gbm_dri_bo_create;
1347 dri->base.bo_import = gbm_dri_bo_import;
1348 dri->base.bo_map = gbm_dri_bo_map;
1349 dri->base.bo_unmap = gbm_dri_bo_unmap;
1350 dri->base.is_format_supported = gbm_dri_is_format_supported;
1351 dri->base.bo_write = gbm_dri_bo_write;
1352 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1353 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1354 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1355 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1356 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1357 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1358 dri->base.bo_destroy = gbm_dri_bo_destroy;
1359 dri->base.destroy = dri_destroy;
1360 dri->base.surface_create = gbm_dri_surface_create;
1361 dri->base.surface_destroy = gbm_dri_surface_destroy;
1362
1363 dri->base.name = "drm";
1364
1365 mtx_init(&dri->mutex, mtx_plain);
1366
1367 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1368 if (!force_sw) {
1369 ret = dri_screen_create(dri);
1370 if (ret)
1371 ret = dri_screen_create_sw(dri);
1372 } else {
1373 ret = dri_screen_create_sw(dri);
1374 }
1375
1376 if (ret)
1377 goto err_dri;
1378
1379 return &dri->base;
1380
1381 err_dri:
1382 free(dri);
1383
1384 return NULL;
1385 }
1386
1387 struct gbm_backend gbm_dri_backend = {
1388 .backend_name = "dri",
1389 .create_device = dri_device_create,
1390 };