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