84b4dd88530ae910ed04c424fd39b1ea9b657b41
[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, 1, 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 int64_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 if (!dri->image || dri->image->base.version < 13 || !dri->image->fromPlanar) {
721 errno = ENOSYS;
722 return -1;
723 }
724
725 if (plane >= get_number_planes(dri, bo->image)) {
726 errno = EINVAL;
727 return -2;
728 }
729
730 /* Dumb images have no offset */
731 if (bo->image == NULL) {
732 assert(plane == 0);
733 return 0;
734 }
735
736 __DRIimage *image = dri->image->fromPlanar(bo->image, plane, NULL);
737 if (image) {
738 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
739 dri->image->destroyImage(image);
740 } else {
741 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
742 }
743
744 return (uint32_t)offset;
745 }
746
747 static uint64_t
748 gbm_dri_bo_get_modifier(struct gbm_bo *_bo)
749 {
750 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
751 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
752
753 if (!dri->image || dri->image->base.version < 14) {
754 errno = ENOSYS;
755 return DRM_FORMAT_MOD_INVALID;
756 }
757
758 /* Dumb buffers have no modifiers */
759 if (!bo->image)
760 return DRM_FORMAT_MOD_LINEAR;
761
762 uint64_t ret = 0;
763 int mod;
764 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
765 &mod))
766 return DRM_FORMAT_MOD_INVALID;
767
768 ret = (uint64_t)mod << 32;
769
770 if (!dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
771 &mod))
772 return DRM_FORMAT_MOD_INVALID;
773
774 ret |= mod;
775
776 return ret;
777 }
778
779 static void
780 gbm_dri_bo_destroy(struct gbm_bo *_bo)
781 {
782 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
783 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
784 struct drm_mode_destroy_dumb arg;
785
786 if (bo->image != NULL) {
787 dri->image->destroyImage(bo->image);
788 } else {
789 gbm_dri_bo_unmap_dumb(bo);
790 memset(&arg, 0, sizeof(arg));
791 arg.handle = bo->handle;
792 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
793 }
794
795 free(bo);
796 }
797
798 static uint32_t
799 gbm_dri_to_gbm_format(uint32_t dri_format)
800 {
801 uint32_t ret = 0;
802
803 switch (dri_format) {
804 case __DRI_IMAGE_FORMAT_RGB565:
805 ret = GBM_FORMAT_RGB565;
806 break;
807 case __DRI_IMAGE_FORMAT_XRGB8888:
808 ret = GBM_FORMAT_XRGB8888;
809 break;
810 case __DRI_IMAGE_FORMAT_ARGB8888:
811 ret = GBM_FORMAT_ARGB8888;
812 break;
813 case __DRI_IMAGE_FORMAT_XBGR8888:
814 ret = GBM_FORMAT_XBGR8888;
815 break;
816 case __DRI_IMAGE_FORMAT_ABGR8888:
817 ret = GBM_FORMAT_ABGR8888;
818 break;
819 default:
820 ret = 0;
821 break;
822 }
823
824 return ret;
825 }
826
827 static struct gbm_bo *
828 gbm_dri_bo_import(struct gbm_device *gbm,
829 uint32_t type, void *buffer, uint32_t usage)
830 {
831 struct gbm_dri_device *dri = gbm_dri_device(gbm);
832 struct gbm_dri_bo *bo;
833 __DRIimage *image;
834 unsigned dri_use = 0;
835 int gbm_format;
836
837 /* Required for query image WIDTH & HEIGHT */
838 if (dri->image == NULL || dri->image->base.version < 4) {
839 errno = ENOSYS;
840 return NULL;
841 }
842
843 switch (type) {
844 #if HAVE_WAYLAND_PLATFORM
845 case GBM_BO_IMPORT_WL_BUFFER:
846 {
847 struct wl_drm_buffer *wb;
848
849 if (!dri->wl_drm) {
850 errno = EINVAL;
851 return NULL;
852 }
853
854 wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
855 if (!wb) {
856 errno = EINVAL;
857 return NULL;
858 }
859
860 image = dri->image->dupImage(wb->driver_buffer, NULL);
861
862 switch (wb->format) {
863 case WL_DRM_FORMAT_XRGB8888:
864 gbm_format = GBM_FORMAT_XRGB8888;
865 break;
866 case WL_DRM_FORMAT_ARGB8888:
867 gbm_format = GBM_FORMAT_ARGB8888;
868 break;
869 case WL_DRM_FORMAT_RGB565:
870 gbm_format = GBM_FORMAT_RGB565;
871 break;
872 case WL_DRM_FORMAT_YUYV:
873 gbm_format = GBM_FORMAT_YUYV;
874 break;
875 default:
876 dri->image->destroyImage(image);
877 return NULL;
878 }
879 break;
880 }
881 #endif
882
883 case GBM_BO_IMPORT_EGL_IMAGE:
884 {
885 int dri_format;
886 if (dri->lookup_image == NULL) {
887 errno = EINVAL;
888 return NULL;
889 }
890
891 image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
892 image = dri->image->dupImage(image, NULL);
893 dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
894 gbm_format = gbm_dri_to_gbm_format(dri_format);
895 if (gbm_format == 0) {
896 errno = EINVAL;
897 dri->image->destroyImage(image);
898 return NULL;
899 }
900 break;
901 }
902
903 case GBM_BO_IMPORT_FD:
904 {
905 struct gbm_import_fd_data *fd_data = buffer;
906 int stride = fd_data->stride, offset = 0;
907 int dri_format;
908
909 switch (fd_data->format) {
910 case GBM_BO_FORMAT_XRGB8888:
911 dri_format = GBM_FORMAT_XRGB8888;
912 break;
913 case GBM_BO_FORMAT_ARGB8888:
914 dri_format = GBM_FORMAT_ARGB8888;
915 break;
916 default:
917 dri_format = fd_data->format;
918 }
919
920 image = dri->image->createImageFromFds(dri->screen,
921 fd_data->width,
922 fd_data->height,
923 dri_format,
924 &fd_data->fd, 1,
925 &stride, &offset,
926 NULL);
927 if (image == NULL) {
928 errno = EINVAL;
929 return NULL;
930 }
931 gbm_format = fd_data->format;
932 break;
933 }
934
935 default:
936 errno = ENOSYS;
937 return NULL;
938 }
939
940
941 bo = calloc(1, sizeof *bo);
942 if (bo == NULL) {
943 dri->image->destroyImage(image);
944 return NULL;
945 }
946
947 bo->image = image;
948
949 if (usage & GBM_BO_USE_SCANOUT)
950 dri_use |= __DRI_IMAGE_USE_SCANOUT;
951 if (usage & GBM_BO_USE_CURSOR)
952 dri_use |= __DRI_IMAGE_USE_CURSOR;
953 if (dri->image->base.version >= 2 &&
954 !dri->image->validateUsage(bo->image, dri_use)) {
955 errno = EINVAL;
956 dri->image->destroyImage(bo->image);
957 free(bo);
958 return NULL;
959 }
960
961 bo->base.base.gbm = gbm;
962 bo->base.base.format = gbm_format;
963
964 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_WIDTH,
965 (int*)&bo->base.base.width);
966 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HEIGHT,
967 (int*)&bo->base.base.height);
968 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
969 (int*)&bo->base.base.stride);
970 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
971 &bo->base.base.handle.s32);
972
973 return &bo->base.base;
974 }
975
976 static bool
977 is_planar_format(uint32_t format)
978 {
979 switch (format) {
980 case GBM_FORMAT_NV12:
981 case GBM_FORMAT_NV21:
982 case GBM_FORMAT_NV16:
983 case GBM_FORMAT_NV61:
984 case GBM_FORMAT_YUV410:
985 case GBM_FORMAT_YVU410:
986 case GBM_FORMAT_YUV411:
987 case GBM_FORMAT_YVU411:
988 case GBM_FORMAT_YUV420:
989 case GBM_FORMAT_YVU420:
990 case GBM_FORMAT_YUV422:
991 case GBM_FORMAT_YVU422:
992 case GBM_FORMAT_YUV444:
993 case GBM_FORMAT_YVU444:
994 return true;
995 default:
996 return false;
997 }
998
999 }
1000
1001 static struct gbm_bo *
1002 create_dumb(struct gbm_device *gbm,
1003 uint32_t width, uint32_t height,
1004 uint32_t format, uint32_t usage)
1005 {
1006 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1007 struct drm_mode_create_dumb create_arg;
1008 struct gbm_dri_bo *bo;
1009 struct drm_mode_destroy_dumb destroy_arg;
1010 int ret;
1011 int is_cursor, is_scanout;
1012
1013 is_cursor = (usage & GBM_BO_USE_CURSOR) != 0 &&
1014 format == GBM_FORMAT_ARGB8888;
1015 is_scanout = (usage & GBM_BO_USE_SCANOUT) != 0 &&
1016 format == GBM_FORMAT_XRGB8888;
1017 if (!is_cursor && !is_scanout) {
1018 errno = EINVAL;
1019 return NULL;
1020 }
1021
1022 if (is_planar_format(format)) {
1023 errno = EINVAL;
1024 return NULL;
1025 }
1026
1027 bo = calloc(1, sizeof *bo);
1028 if (bo == NULL)
1029 return NULL;
1030
1031 memset(&create_arg, 0, sizeof(create_arg));
1032 create_arg.bpp = 32;
1033 create_arg.width = width;
1034 create_arg.height = height;
1035
1036 ret = drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
1037 if (ret)
1038 goto free_bo;
1039
1040 bo->base.base.gbm = gbm;
1041 bo->base.base.width = width;
1042 bo->base.base.height = height;
1043 bo->base.base.stride = create_arg.pitch;
1044 bo->base.base.format = format;
1045 bo->base.base.handle.u32 = create_arg.handle;
1046 bo->handle = create_arg.handle;
1047 bo->size = create_arg.size;
1048
1049 if (gbm_dri_bo_map_dumb(bo) == NULL)
1050 goto destroy_dumb;
1051
1052 return &bo->base.base;
1053
1054 destroy_dumb:
1055 memset(&destroy_arg, 0, sizeof destroy_arg);
1056 destroy_arg.handle = create_arg.handle;
1057 drmIoctl(dri->base.base.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
1058 free_bo:
1059 free(bo);
1060
1061 return NULL;
1062 }
1063
1064 static struct gbm_bo *
1065 gbm_dri_bo_create(struct gbm_device *gbm,
1066 uint32_t width, uint32_t height,
1067 uint32_t format, uint32_t usage,
1068 const uint64_t *modifiers,
1069 const unsigned int count)
1070 {
1071 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1072 struct gbm_dri_bo *bo;
1073 int dri_format;
1074 unsigned dri_use = 0;
1075
1076 /* Callers of this may specify a modifier, or a dri usage, but not both. The
1077 * newer modifier interface deprecates the older usage flags.
1078 */
1079 assert(!(usage && count));
1080
1081 if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
1082 return create_dumb(gbm, width, height, format, usage);
1083
1084 bo = calloc(1, sizeof *bo);
1085 if (bo == NULL)
1086 return NULL;
1087
1088 bo->base.base.gbm = gbm;
1089 bo->base.base.width = width;
1090 bo->base.base.height = height;
1091 bo->base.base.format = format;
1092
1093 switch (format) {
1094 case GBM_FORMAT_R8:
1095 dri_format = __DRI_IMAGE_FORMAT_R8;
1096 break;
1097 case GBM_FORMAT_GR88:
1098 dri_format = __DRI_IMAGE_FORMAT_GR88;
1099 break;
1100 case GBM_FORMAT_RGB565:
1101 dri_format = __DRI_IMAGE_FORMAT_RGB565;
1102 break;
1103 case GBM_FORMAT_XRGB8888:
1104 case GBM_BO_FORMAT_XRGB8888:
1105 dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
1106 break;
1107 case GBM_FORMAT_ARGB8888:
1108 case GBM_BO_FORMAT_ARGB8888:
1109 dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
1110 break;
1111 case GBM_FORMAT_ABGR8888:
1112 dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
1113 break;
1114 case GBM_FORMAT_XBGR8888:
1115 dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
1116 break;
1117 case GBM_FORMAT_ARGB2101010:
1118 dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
1119 break;
1120 case GBM_FORMAT_XRGB2101010:
1121 dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
1122 break;
1123 default:
1124 errno = EINVAL;
1125 goto failed;
1126 }
1127
1128 if (usage & GBM_BO_USE_SCANOUT)
1129 dri_use |= __DRI_IMAGE_USE_SCANOUT;
1130 if (usage & GBM_BO_USE_CURSOR)
1131 dri_use |= __DRI_IMAGE_USE_CURSOR;
1132 if (usage & GBM_BO_USE_LINEAR)
1133 dri_use |= __DRI_IMAGE_USE_LINEAR;
1134
1135 /* Gallium drivers requires shared in order to get the handle/stride */
1136 dri_use |= __DRI_IMAGE_USE_SHARE;
1137
1138 if (modifiers) {
1139 if (!dri->image || dri->image->base.version < 14 ||
1140 !dri->image->createImageWithModifiers) {
1141 fprintf(stderr, "Modifiers specified, but DRI is too old\n");
1142 errno = ENOSYS;
1143 goto failed;
1144 }
1145
1146 /* It's acceptable to create an image with INVALID modifier in the list,
1147 * but it cannot be on the only modifier (since it will certainly fail
1148 * later). While we could easily catch this after modifier creation, doing
1149 * the check here is a convenient debug check likely pointing at whatever
1150 * interface the client is using to build its modifier list.
1151 */
1152 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1153 fprintf(stderr, "Only invalid modifier specified\n");
1154 errno = EINVAL;
1155 goto failed;
1156 }
1157
1158 bo->image =
1159 dri->image->createImageWithModifiers(dri->screen,
1160 width, height,
1161 dri_format,
1162 modifiers, count,
1163 bo);
1164
1165 if (bo->image) {
1166 /* The client passed in a list of invalid modifiers */
1167 assert(gbm_dri_bo_get_modifier(&bo->base.base) != DRM_FORMAT_MOD_INVALID);
1168 }
1169 } else {
1170 bo->image = dri->image->createImage(dri->screen, width, height,
1171 dri_format, dri_use, bo);
1172 }
1173
1174 if (bo->image == NULL)
1175 goto failed;
1176
1177 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_HANDLE,
1178 &bo->base.base.handle.s32);
1179 dri->image->queryImage(bo->image, __DRI_IMAGE_ATTRIB_STRIDE,
1180 (int *) &bo->base.base.stride);
1181
1182 return &bo->base.base;
1183
1184 failed:
1185 free(bo);
1186 return NULL;
1187 }
1188
1189 static void *
1190 gbm_dri_bo_map(struct gbm_bo *_bo,
1191 uint32_t x, uint32_t y,
1192 uint32_t width, uint32_t height,
1193 uint32_t flags, uint32_t *stride, void **map_data)
1194 {
1195 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1196 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1197
1198 /* If it's a dumb buffer, we already have a mapping */
1199 if (bo->map) {
1200 *map_data = (char *)bo->map + (bo->base.base.stride * y) + (x * 4);
1201 *stride = bo->base.base.stride;
1202 return *map_data;
1203 }
1204
1205 if (!dri->image || dri->image->base.version < 12 || !dri->image->mapImage) {
1206 errno = ENOSYS;
1207 return NULL;
1208 }
1209
1210 mtx_lock(&dri->mutex);
1211 if (!dri->context)
1212 dri->context = dri->dri2->createNewContext(dri->screen, NULL,
1213 NULL, NULL);
1214 assert(dri->context);
1215 mtx_unlock(&dri->mutex);
1216
1217 /* GBM flags and DRI flags are the same, so just pass them on */
1218 return dri->image->mapImage(dri->context, bo->image, x, y,
1219 width, height, flags, (int *)stride,
1220 map_data);
1221 }
1222
1223 static void
1224 gbm_dri_bo_unmap(struct gbm_bo *_bo, void *map_data)
1225 {
1226 struct gbm_dri_device *dri = gbm_dri_device(_bo->gbm);
1227 struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
1228
1229 /* Check if it's a dumb buffer and check the pointer is in range */
1230 if (bo->map) {
1231 assert(map_data >= bo->map);
1232 assert(map_data < (bo->map + bo->size));
1233 return;
1234 }
1235
1236 if (!dri->context || !dri->image ||
1237 dri->image->base.version < 12 || !dri->image->unmapImage)
1238 return;
1239
1240 dri->image->unmapImage(dri->context, bo->image, map_data);
1241 }
1242
1243
1244 static struct gbm_surface *
1245 gbm_dri_surface_create(struct gbm_device *gbm,
1246 uint32_t width, uint32_t height,
1247 uint32_t format, uint32_t flags,
1248 const uint64_t *modifiers, const unsigned count)
1249 {
1250 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1251 struct gbm_dri_surface *surf;
1252
1253 if (modifiers &&
1254 (!dri->image || dri->image->base.version < 14 ||
1255 !dri->image->createImageWithModifiers)) {
1256 errno = ENOSYS;
1257 return NULL;
1258 }
1259
1260 /* It's acceptable to create an image with INVALID modifier in the list,
1261 * but it cannot be on the only modifier (since it will certainly fail
1262 * later). While we could easily catch this after modifier creation, doing
1263 * the check here is a convenient debug check likely pointing at whatever
1264 * interface the client is using to build its modifier list.
1265 */
1266 if (count == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
1267 fprintf(stderr, "Only invalid modifier specified\n");
1268 errno = EINVAL;
1269 }
1270
1271 surf = calloc(1, sizeof *surf);
1272 if (surf == NULL) {
1273 errno = ENOMEM;
1274 return NULL;
1275 }
1276
1277 surf->base.gbm = gbm;
1278 surf->base.width = width;
1279 surf->base.height = height;
1280 surf->base.format = format;
1281 surf->base.flags = flags;
1282 if (!modifiers) {
1283 assert(!count);
1284 return &surf->base;
1285 }
1286
1287 surf->base.modifiers = calloc(count, sizeof(*modifiers));
1288 if (count && !surf->base.modifiers) {
1289 errno = ENOMEM;
1290 free(surf);
1291 return NULL;
1292 }
1293
1294 /* TODO: We are deferring validation of modifiers until the image is actually
1295 * created. This deferred creation can fail due to a modifier-format
1296 * mismatch. The result is the client has a surface but no object to back it.
1297 */
1298 surf->base.count = count;
1299 memcpy(surf->base.modifiers, modifiers, count * sizeof(*modifiers));
1300
1301 return &surf->base;
1302 }
1303
1304 static void
1305 gbm_dri_surface_destroy(struct gbm_surface *_surf)
1306 {
1307 struct gbm_dri_surface *surf = gbm_dri_surface(_surf);
1308
1309 free(surf->base.modifiers);
1310 free(surf);
1311 }
1312
1313 static void
1314 dri_destroy(struct gbm_device *gbm)
1315 {
1316 struct gbm_dri_device *dri = gbm_dri_device(gbm);
1317 unsigned i;
1318
1319 if (dri->context)
1320 dri->core->destroyContext(dri->context);
1321
1322 dri->core->destroyScreen(dri->screen);
1323 for (i = 0; dri->driver_configs[i]; i++)
1324 free((__DRIconfig *) dri->driver_configs[i]);
1325 free(dri->driver_configs);
1326 dlclose(dri->driver);
1327 free(dri->base.driver_name);
1328
1329 free(dri);
1330 }
1331
1332 static struct gbm_device *
1333 dri_device_create(int fd)
1334 {
1335 struct gbm_dri_device *dri;
1336 int ret, force_sw;
1337
1338 dri = calloc(1, sizeof *dri);
1339 if (!dri)
1340 return NULL;
1341
1342 dri->base.base.fd = fd;
1343 dri->base.base.bo_create = gbm_dri_bo_create;
1344 dri->base.base.bo_import = gbm_dri_bo_import;
1345 dri->base.base.bo_map = gbm_dri_bo_map;
1346 dri->base.base.bo_unmap = gbm_dri_bo_unmap;
1347 dri->base.base.is_format_supported = gbm_dri_is_format_supported;
1348 dri->base.base.bo_write = gbm_dri_bo_write;
1349 dri->base.base.bo_get_fd = gbm_dri_bo_get_fd;
1350 dri->base.base.bo_get_planes = gbm_dri_bo_get_planes;
1351 dri->base.base.bo_get_handle = gbm_dri_bo_get_handle_for_plane;
1352 dri->base.base.bo_get_stride = gbm_dri_bo_get_stride;
1353 dri->base.base.bo_get_offset = gbm_dri_bo_get_offset;
1354 dri->base.base.bo_get_modifier = gbm_dri_bo_get_modifier;
1355 dri->base.base.bo_destroy = gbm_dri_bo_destroy;
1356 dri->base.base.destroy = dri_destroy;
1357 dri->base.base.surface_create = gbm_dri_surface_create;
1358 dri->base.base.surface_destroy = gbm_dri_surface_destroy;
1359
1360 dri->base.type = GBM_DRM_DRIVER_TYPE_DRI;
1361 dri->base.base.name = "drm";
1362
1363 mtx_init(&dri->mutex, mtx_plain);
1364
1365 force_sw = getenv("GBM_ALWAYS_SOFTWARE") != NULL;
1366 if (!force_sw) {
1367 ret = dri_screen_create(dri);
1368 if (ret)
1369 ret = dri_screen_create_sw(dri);
1370 } else {
1371 ret = dri_screen_create_sw(dri);
1372 }
1373
1374 if (ret)
1375 goto err_dri;
1376
1377 return &dri->base.base;
1378
1379 err_dri:
1380 free(dri);
1381
1382 return NULL;
1383 }
1384
1385 struct gbm_backend gbm_dri_backend = {
1386 .backend_name = "dri",
1387 .create_device = dri_device_create,
1388 };