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