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