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