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