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