gbm: Pull out FourCC <-> DRIimage format table
[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
604 static int
605 gbm_dri_is_format_supported(struct gbm_device *gbm,
606 uint32_t format,
607 uint32_t usage)
608 {
609 switch (format) {
610 case GBM_BO_FORMAT_XRGB8888:
611 case GBM_FORMAT_XBGR8888:
612 case GBM_FORMAT_XRGB8888:
613 break;
614 case GBM_BO_FORMAT_ARGB8888:
615 case GBM_FORMAT_ARGB8888:
616 if (usage & GBM_BO_USE_SCANOUT)
617 return 0;
618 break;
619 default:
620 return 0;
621 }
622
623 if (usage & GBM_BO_USE_CURSOR &&
624 usage & GBM_BO_USE_RENDERING)
625 return 0;
626
627 return 1;
628 }
629
630 static int
631 gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
632 {
633 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
634
635 if (bo->image != NULL) {
636 errno = EINVAL;
637 return -1;
638 }
639
640 memcpy(bo->map, buf, count);
641
642 return 0;
643 }
644
645 static int
646 gbm_dri_bo_get_fd(struct gbm_bo *_bo)
647 {
648 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
649 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
650 int fd;
651
652 if (bo->image == NULL)
653 return -1;
654
655 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_FD, &fd))
656 return -1;
657
658 return fd;
659 }
660
661 static int
662 get_number_planes(struct gbm_dri_device *dri, __DRIimage *image)
663 {
664 int num_planes = 0;
665
666 /* Dumb buffers are single-plane only. */
667 if (!image)
668 return 1;
669
670 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES, &num_planes);
671
672 if (num_planes <= 0)
673 num_planes = 1;
674
675 return num_planes;
676 }
677
678 static int
679 gbm_dri_bo_get_planes(struct gbm_bo *_bo)
680 {
681 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
682 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
683
684 return get_number_planes(dri, bo->image);
685 }
686
687 static union gbm_bo_handle
688 gbm_dri_bo_get_handle_for_plane(struct gbm_bo *_bo, int plane)
689 {
690 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
691 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
692 union gbm_bo_handle ret;
693 ret.s32 = -1;
694
695 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
696 errno = ENOSYS;
697 return ret;
698 }
699
700 if (plane >= get_number_planes(dri, bo->image)) {
701 errno = EINVAL;
702 return ret;
703 }
704
705 /* dumb BOs can only utilize non-planar formats */
706 if (!bo->image) {
707 assert(plane == 0);
708 ret.s32 = bo->handle;
709 return ret;
710 }
711
712 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
713 if (image) {
714 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
715 dri->image->destroyImage(image);
716 } else {
717 assert(plane == 0);
718 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE, &ret.s32);
719 }
720
721 return ret;
722 }
723
724 static uint32_t
725 gbm_dri_bo_get_stride(struct gbm_bo *_bo, int plane)
726 {
727 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
728 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
729 __DRIimage *image;
730 int stride = 0;
731
732 if (!dri->image || dri->image->base.version < 11 || !dri->image->fromPlanar) {
733 /* Preserve legacy behavior if plane is 0 */
734 if (plane == 0)
735 return _bo->stride;
736
737 errno = ENOSYS;
738 return 0;
739 }
740
741 if (plane >= get_number_planes(dri, bo->image)) {
742 errno = EINVAL;
743 return 0;
744 }
745
746 if (bo->image == NULL) {
747 assert(plane == 0);
748 return _bo->stride;
749 }
750
751 image = dri->image->fromPlanar(bo->image, plane, NULL);
752 if (image) {
753 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
754 dri->image->destroyImage(image);
755 } else {
756 assert(plane == 0);
757 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
758 }
759
760 return (uint32_t)stride;
761 }
762
763 static uint32_t
764 gbm_dri_bo_get_offset(struct gbm_bo *_bo, int plane)
765 {
766 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
767 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
768 int offset = 0;
769
770 /* These error cases do not actually return an error code, as the user
771 * will also fail to obtain the handle/FD from the BO. In that case, the
772 * offset is irrelevant, as they have no buffer to offset into, so
773 * returning 0 is harmless.
774 */
775 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar)
776 return 0;
777
778 if (plane >= get_number_planes(dri, bo->image))
779 return 0;
780
781 /* Dumb images have no offset */
782 if (bo->image == NULL) {
783 assert(plane == 0);
784 return 0;
785 }
786
787 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
788 if (image) {
789 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
790 dri->image->destroyImage(image);
791 } else {
792 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
793 }
794
795 return (uint32_t)offset;
796 }
797
798 static uint64_t
799 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
800 {
801 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
802 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
803
804 if (!dri->image || dri->image->base.version < 14) {
805 errno = ENOSYS;
806 return DRM_FORMAT_MOD_INVALID;
807 }
808
809 /* Dumb buffers have no modifiers */
810 if (!bo->image)
811 return DRM_FORMAT_MOD_LINEAR;
812
813 uint64_t ret = 0;
814 int mod;
815 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
816 &mod))
817 return DRM_FORMAT_MOD_INVALID;
818
819 ret = (uint64_t)mod << 32;
820
821 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
822 &mod))
823 return DRM_FORMAT_MOD_INVALID;
824
825 ret |= (uint64_t)(mod & 0xffffffff);
826
827 return ret;
828 }
829
830 static void
831 gbm_dri_bo_destroy(struct gbm_bo *_bo)
832 {
833 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
834 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
835 struct drm_mode_destroy_dumb arg;
836
837 if (bo->image != NULL) {
838 dri->image->destroyImage(bo->image);
839 } else {
840 gbm_dri_bo_unmap_dumb(bo);
841 memset(&arg, 0, sizeof(arg));
842 arg.handle = bo->handle;
843 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
844 }
845
846 free(bo);
847 }
848
849 static struct gbm_bo *
850 gbm_dri_bo_import(struct gbm_device *gbm,
851 uint32_t type, void *buffer, uint32_t usage)
852 {
853 struct gbm_dri_device *dri = gbm_dri_device(gbm);
854 struct gbm_dri_bo *bo;
855 __DRIimage *image;
856 unsigned dri_use = 0;
857 int gbm_format;
858
859 /* Required for query image WIDTH & HEIGHT */
860 if (dri->image == NULL || dri->image->base.version < 4) {
861 errno = ENOSYS;
862 return NULL;
863 }
864
865 switch (type) {
866 #if HAVE_WAYLAND_PLATFORM
867 case GBM_BO_IMPORT_WL_BUFFER:
868 {
869 struct wl_drm_buffer *wb;
870
871 if (!dri->wl_drm) {
872 errno = EINVAL;
873 return NULL;
874 }
875
876 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
877 if (!wb) {
878 errno = EINVAL;
879 return NULL;
880 }
881
882 image = dri->image->dupImage(wb->driver_buffer, NULL);
883
884 /* GBM_FORMAT_* is identical to WL_DRM_FORMAT_*, so no conversion
885 * required. */
886 gbm_format = wb->format;
887 break;
888 }
889 #endif
890
891 case GBM_BO_IMPORT_EGL_IMAGE:
892 {
893 int dri_format;
894 if (dri->lookup_image == NULL) {
895 errno = EINVAL;
896 return NULL;
897 }
898
899 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
900 image = dri->image->dupImage(image, NULL);
901 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
902 gbm_format = gbm_dri_to_gbm_format(dri_format);
903 if (gbm_format == 0) {
904 errno = EINVAL;
905 dri->image->destroyImage(image);
906 return NULL;
907 }
908 break;
909 }
910
911 case GBM_BO_IMPORT_FD:
912 {
913 struct gbm_import_fd_data *fd_data = buffer;
914 int stride = fd_data->stride, offset = 0;
915 int fourcc;
916
917 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
918 * tokens accepted by createImageFromFds, except for not supporting
919 * the sARGB format. */
920 fourcc = gbm_format_canonicalize(fd_data->format);
921
922 image = dri->image->createImageFromFds(dri->screen,
923 fd_data->width,
924 fd_data->height,
925 fourcc,
926 &fd_data->fd, 1,
927 &stride, &offset,
928 NULL);
929 if (image == NULL) {
930 errno = EINVAL;
931 return NULL;
932 }
933 gbm_format = fd_data->format;
934 break;
935 }
936
937 case GBM_BO_IMPORT_FD_MODIFIER:
938 {
939 struct gbm_import_fd_modifier_data *fd_data = buffer;
940 unsigned int error;
941 int fourcc;
942
943 /* Import with modifier requires createImageFromDmaBufs2 */
944 if (dri->image == NULL || dri->image->base.version < 15 ||
945 dri->image->createImageFromDmaBufs2 == NULL) {
946 errno = ENOSYS;
947 return NULL;
948 }
949
950 /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
951 * tokens accepted by createImageFromDmaBufs2, except for not supporting
952 * the sARGB format. */
953 fourcc = gbm_format_canonicalize(fd_data->format);
954
955 image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
956 fd_data->height, fourcc,
957 fd_data->modifier,
958 fd_data->fds,
959 fd_data->num_fds,
960 fd_data->strides,
961 fd_data->offsets,
962 0, 0, 0, 0,
963 &error, NULL);
964 if (image == NULL) {
965 errno = ENOSYS;
966 return NULL;
967 }
968
969 gbm_format = fourcc;
970 break;
971 }
972
973 default:
974 errno = ENOSYS;
975 return NULL;
976 }
977
978
979 bo = calloc(1, sizeof *bo);
980 if (bo == NULL) {
981 dri->image->destroyImage(image);
982 return NULL;
983 }
984
985 bo->image = image;
986
987 if (usage & GBM_BO_USE_SCANOUT)
988 dri_use |= __DRI_IMAGE_USE_SCANOUT;
989 if (usage & GBM_BO_USE_CURSOR)
990 dri_use |= __DRI_IMAGE_USE_CURSOR;
991 if (dri->image->base.version >= 2 &&
992 !dri->image->validateUsage(bo->image, dri_use)) {
993 errno = EINVAL;
994 dri->image->destroyImage(bo->image);
995 free(bo);
996 return NULL;
997 }
998
999 bo->base.gbm = gbm;
1000 bo->base.format = gbm_format;
1001
1002 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
1003 (int*)&bo->base.width);
1004 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
1005 (int*)&bo->base.height);
1006 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1007 (int*)&bo->base.stride);
1008 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1009 &bo->base.handle.s32);
1010
1011 return &bo->base;
1012 }
1013
1014 static bool
1015 is_planar_format(uint32_t format)
1016 {
1017 switch (format) {
1018 case GBM_FORMAT_NV12:
1019 case GBM_FORMAT_NV21:
1020 case GBM_FORMAT_NV16:
1021 case GBM_FORMAT_NV61:
1022 case GBM_FORMAT_YUV410:
1023 case GBM_FORMAT_YVU410:
1024 case GBM_FORMAT_YUV411:
1025 case GBM_FORMAT_YVU411:
1026 case GBM_FORMAT_YUV420:
1027 case GBM_FORMAT_YVU420:
1028 case GBM_FORMAT_YUV422:
1029 case GBM_FORMAT_YVU422:
1030 case GBM_FORMAT_YUV444:
1031 case GBM_FORMAT_YVU444:
1032 return true;
1033 default:
1034 return false;
1035 }
1036
1037 }
1038
1039 static struct gbm_bo *
1040 create_dumb(struct gbm_device *gbm,
1041 uint32_t width, uint32_t height,
1042 uint32_t format, uint32_t usage)
1043 {
1044 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1045 struct drm_mode_create_dumb create_arg;
1046 struct gbm_dri_bo *bo;
1047 struct drm_mode_destroy_dumb destroy_arg;
1048 int ret;
1049 int is_cursor, is_scanout;
1050
1051 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1052 format == GBM_FORMAT_ARGB8888;
1053 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1054 (format == GBM_FORMAT_XRGB8888 || format == GBM_FORMAT_XBGR8888);
1055 if (!is_cursor && !is_scanout) {
1056 errno = EINVAL;
1057 return NULL;
1058 }
1059
1060 if (is_planar_format(format)) {
1061 errno = EINVAL;
1062 return NULL;
1063 }
1064
1065 bo = calloc(1, sizeof *bo);
1066 if (bo == NULL)
1067 return NULL;
1068
1069 memset(&create_arg, 0, sizeof(create_arg));
1070 create_arg.bpp = 32;
1071 create_arg.width = width;
1072 create_arg.height = height;
1073
1074 ret = drmIoctl(dri->base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1075 if (ret)
1076 goto free_bo;
1077
1078 bo->base.gbm = gbm;
1079 bo->base.width = width;
1080 bo->base.height = height;
1081 bo->base.stride = create_arg.pitch;
1082 bo->base.format = format;
1083 bo->base.handle.u32 = create_arg.handle;
1084 bo->handle = create_arg.handle;
1085 bo->size = create_arg.size;
1086
1087 if (gbm_dri_bo_map_dumb(bo) == NULL)
1088 goto destroy_dumb;
1089
1090 return &bo->base;
1091
1092 destroy_dumb:
1093 memset(&destroy_arg, 0, sizeof destroy_arg);
1094 destroy_arg.handle = create_arg.handle;
1095 drmIoctl(dri->base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1096 free_bo:
1097 free(bo);
1098
1099 return NULL;
1100 }
1101
1102 static struct gbm_bo *
1103 gbm_dri_bo_create(struct gbm_device *gbm,
1104 uint32_t width, uint32_t height,
1105 uint32_t format, uint32_t usage,
1106 const uint64_t *modifiers,
1107 const unsigned int count)
1108 {
1109 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1110 struct gbm_dri_bo *bo;
1111 int dri_format;
1112 unsigned dri_use = 0;
1113
1114 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1115 * newer modifier interface deprecates the older usage flags.
1116 */
1117 assert(!(usage && count));
1118
1119 format = gbm_format_canonicalize(format);
1120
1121 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1122 return create_dumb(gbm, width, height, format, usage);
1123
1124 bo = calloc(1, sizeof *bo);
1125 if (bo == NULL)
1126 return NULL;
1127
1128 bo->base.gbm = gbm;
1129 bo->base.width = width;
1130 bo->base.height = height;
1131 bo->base.format = format;
1132
1133 dri_format = gbm_format_to_dri_format(format);
1134 if (dri_format == 0) {
1135 errno = EINVAL;
1136 goto failed;
1137 }
1138
1139 if (usage & GBM_BO_USE_SCANOUT)
1140 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1141 if (usage & GBM_BO_USE_CURSOR)
1142 dri_use |= __DRI_IMAGE_USE_CURSOR;
1143 if (usage & GBM_BO_USE_LINEAR)
1144 dri_use |= __DRI_IMAGE_USE_LINEAR;
1145
1146 /* Gallium drivers requires shared in order to get the handle/stride */
1147 dri_use |= __DRI_IMAGE_USE_SHARE;
1148
1149 if (modifiers) {
1150 if (!dri->image || dri->image->base.version < 14 ||
1151 !dri->image->createImageWithModifiers) {
1152 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1153 errno = ENOSYS;
1154 goto failed;
1155 }
1156
1157 /* It's acceptable to create an image with INVALID modifier in the list,
1158 * but it cannot be on the only modifier (since it will certainly fail
1159 * later). While we could easily catch this after modifier creation, doing
1160 * the check here is a convenient debug check likely pointing at whatever
1161 * interface the client is using to build its modifier list.
1162 */
1163 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1164 fprintf(stderr, "Only invalid modifier specified\n");
1165 errno = EINVAL;
1166 goto failed;
1167 }
1168
1169 bo->image =
1170 dri->image->createImageWithModifiers(dri->screen,
1171 width, height,
1172 dri_format,
1173 modifiers, count,
1174 bo);
1175
1176 if (bo->image) {
1177 /* The client passed in a list of invalid modifiers */
1178 assert(gbm_dri_bo_get_modifier(&bo->base) != DRM_FORMAT_MOD_INVALID);
1179 }
1180 } else {
1181 bo->image = dri->image->createImage(dri->screen, width, height,
1182 dri_format, dri_use, bo);
1183 }
1184
1185 if (bo->image == NULL)
1186 goto failed;
1187
1188 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1189 &bo->base.handle.s32);
1190 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1191 (int *) &bo->base.stride);
1192
1193 return &bo->base;
1194
1195 failed:
1196 free(bo);
1197 return NULL;
1198 }
1199
1200 static void *
1201 gbm_dri_bo_map(struct gbm_bo *_bo,
1202 uint32_t x, uint32_t y,
1203 uint32_t width, uint32_t height,
1204 uint32_t flags, uint32_t *stride, void **map_data)
1205 {
1206 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1207 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1208
1209 /* If it's a dumb buffer, we already have a mapping */
1210 if (bo->map) {
1211 *map_data = (char *)bo->map + (bo->base.stride * y) + (x * 4);
1212 *stride = bo->base.stride;
1213 return *map_data;
1214 }
1215
1216 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1217 errno = ENOSYS;
1218 return NULL;
1219 }
1220
1221 mtx_lock(&dri->mutex);
1222 if (!dri->context)
1223 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1224 NULL, NULL);
1225 assert(dri->context);
1226 mtx_unlock(&dri->mutex);
1227
1228 /* GBM flags and DRI flags are the same, so just pass them on */
1229 return dri->image->mapImage(dri->context, bo->image, x, y,
1230 width, height, flags, (int *)stride,
1231 map_data);
1232 }
1233
1234 static void
1235 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1236 {
1237 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1238 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1239
1240 /* Check if it's a dumb buffer and check the pointer is in range */
1241 if (bo->map) {
1242 assert(map_data >= bo->map);
1243 assert(map_data < (bo->map + bo->size));
1244 return;
1245 }
1246
1247 if (!dri->context || !dri->image ||
1248 dri->image->base.version < 12 || !dri->image->unmapImage)
1249 return;
1250
1251 dri->image->unmapImage(dri->context, bo->image, map_data);
1252
1253 /*
1254 * Not all DRI drivers use direct maps. They may queue up DMA operations
1255 * on the mapping context. Since there is no explicit gbm flush
1256 * mechanism, we need to flush here.
1257 */
1258 if (dri->flush->base.version >= 4)
1259 dri->flush->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
1260 }
1261
1262
1263 static struct gbm_surface *
1264 gbm_dri_surface_create(struct gbm_device *gbm,
1265 uint32_t width, uint32_t height,
1266 uint32_t format, uint32_t flags,
1267 const uint64_t *modifiers, const unsigned count)
1268 {
1269 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1270 struct gbm_dri_surface *surf;
1271
1272 if (modifiers &&
1273 (!dri->image || dri->image->base.version < 14 ||
1274 !dri->image->createImageWithModifiers)) {
1275 errno = ENOSYS;
1276 return NULL;
1277 }
1278
1279 if (count)
1280 assert(modifiers);
1281
1282 /* It's acceptable to create an image with INVALID modifier in the list,
1283 * but it cannot be on the only modifier (since it will certainly fail
1284 * later). While we could easily catch this after modifier creation, doing
1285 * the check here is a convenient debug check likely pointing at whatever
1286 * interface the client is using to build its modifier list.
1287 */
1288 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1289 fprintf(stderr, "Only invalid modifier specified\n");
1290 errno = EINVAL;
1291 }
1292
1293 surf = calloc(1, sizeof *surf);
1294 if (surf == NULL) {
1295 errno = ENOMEM;
1296 return NULL;
1297 }
1298
1299 surf->base.gbm = gbm;
1300 surf->base.width = width;
1301 surf->base.height = height;
1302 surf->base.format = gbm_format_canonicalize(format);
1303 surf->base.flags = flags;
1304 if (!modifiers) {
1305 assert(!count);
1306 return &surf->base;
1307 }
1308
1309 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1310 if (count && !surf->base.modifiers) {
1311 errno = ENOMEM;
1312 free(surf);
1313 return NULL;
1314 }
1315
1316 /* TODO: We are deferring validation of modifiers until the image is actually
1317 * created. This deferred creation can fail due to a modifier-format
1318 * mismatch. The result is the client has a surface but no object to back it.
1319 */
1320 surf->base.count = count;
1321 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1322
1323 return &surf->base;
1324 }
1325
1326 static void
1327 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1328 {
1329 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1330
1331 free(surf->base.modifiers);
1332 free(surf);
1333 }
1334
1335 static void
1336 dri_destroy(struct gbm_device *gbm)
1337 {
1338 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1339 unsigned i;
1340
1341 if (dri->context)
1342 dri->core->destroyContext(dri->context);
1343
1344 dri->core->destroyScreen(dri->screen);
1345 for (i = 0; dri->driver_configs[i]; i++)
1346 free((__DRIconfig *) dri->driver_configs[i]);
1347 free(dri->driver_configs);
1348 dlclose(dri->driver);
1349 free(dri->driver_name);
1350
1351 free(dri);
1352 }
1353
1354 static struct gbm_device *
1355 dri_device_create(int fd)
1356 {
1357 struct gbm_dri_device *dri;
1358 int ret, force_sw;
1359
1360 dri = calloc(1, sizeof *dri);
1361 if (!dri)
1362 return NULL;
1363
1364 dri->base.fd = fd;
1365 dri->base.bo_create = gbm_dri_bo_create;
1366 dri->base.bo_import = gbm_dri_bo_import;
1367 dri->base.bo_map = gbm_dri_bo_map;
1368 dri->base.bo_unmap = gbm_dri_bo_unmap;
1369 dri->base.is_format_supported = gbm_dri_is_format_supported;
1370 dri->base.bo_write = gbm_dri_bo_write;
1371 dri->base.bo_get_fd = gbm_dri_bo_get_fd;
1372 dri->base.bo_get_planes = gbm_dri_bo_get_planes;
1373 dri->base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1374 dri->base.bo_get_stride = gbm_dri_bo_get_stride;
1375 dri->base.bo_get_offset = gbm_dri_bo_get_offset;
1376 dri->base.bo_get_modifier = gbm_dri_bo_get_modifier;
1377 dri->base.bo_destroy = gbm_dri_bo_destroy;
1378 dri->base.destroy = dri_destroy;
1379 dri->base.surface_create = gbm_dri_surface_create;
1380 dri->base.surface_destroy = gbm_dri_surface_destroy;
1381
1382 dri->base.name = "drm";
1383
1384 mtx_init(&dri->mutex, mtx_plain);
1385
1386 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1387 if (!force_sw) {
1388 ret = dri_screen_create(dri);
1389 if (ret)
1390 ret = dri_screen_create_sw(dri);
1391 } else {
1392 ret = dri_screen_create_sw(dri);
1393 }
1394
1395 if (ret)
1396 goto err_dri;
1397
1398 return &dri->base;
1399
1400 err_dri:
1401 free(dri);
1402
1403 return NULL;
1404 }
1405
1406 struct gbm_backend gbm_dri_backend = {
1407 .backend_name = "dri",
1408 .create_device = dri_device_create,
1409 };