2 * Copyright © 2011 Intel Corporation
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:
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
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.
25 * Benjamin Franzke <benjaminfranzke@googlemail.com>
35 #include <sys/types.h>
41 #include <GL/gl.h> /* dri_interface needs GL types */
42 #include <GL/internal/dri_interface.h>
44 #include "gbm_driint.h"
49 /* For importing wl_buffer */
50 #if HAVE_WAYLAND_PLATFORM
51 #include "../../../egl/wayland/wayland-drm/wayland-drm.h"
55 dri_lookup_egl_image(__DRIscreen
*screen
, void *image
, void *data
)
57 struct gbm_dri_device
*dri
= data
;
59 if (dri
->lookup_image
== NULL
)
62 return dri
->lookup_image(screen
, image
, dri
->lookup_user_data
);
66 dri_get_buffers(__DRIdrawable
* driDrawable
,
67 int *width
, int *height
,
68 unsigned int *attachments
, int count
,
69 int *out_count
, void *data
)
71 struct gbm_dri_surface
*surf
= data
;
72 struct gbm_dri_device
*dri
= gbm_dri_device(surf
->base
.gbm
);
74 if (dri
->get_buffers
== NULL
)
77 return dri
->get_buffers(driDrawable
, width
, height
, attachments
,
78 count
, out_count
, surf
->dri_private
);
82 dri_flush_front_buffer(__DRIdrawable
* driDrawable
, void *data
)
84 struct gbm_dri_surface
*surf
= data
;
85 struct gbm_dri_device
*dri
= gbm_dri_device(surf
->base
.gbm
);
87 if (dri
->flush_front_buffer
!= NULL
)
88 dri
->flush_front_buffer(driDrawable
, surf
->dri_private
);
92 dri_get_buffers_with_format(__DRIdrawable
* driDrawable
,
93 int *width
, int *height
,
94 unsigned int *attachments
, int count
,
95 int *out_count
, void *data
)
97 struct gbm_dri_surface
*surf
= data
;
98 struct gbm_dri_device
*dri
= gbm_dri_device(surf
->base
.gbm
);
100 if (dri
->get_buffers_with_format
== NULL
)
104 dri
->get_buffers_with_format(driDrawable
, width
, height
, attachments
,
105 count
, out_count
, surf
->dri_private
);
109 image_get_buffers(__DRIdrawable
*driDrawable
,
113 uint32_t buffer_mask
,
114 struct __DRIimageList
*buffers
)
116 struct gbm_dri_surface
*surf
= loaderPrivate
;
117 struct gbm_dri_device
*dri
= gbm_dri_device(surf
->base
.gbm
);
119 if (dri
->image_get_buffers
== NULL
)
122 return dri
->image_get_buffers(driDrawable
, format
, stamp
,
123 surf
->dri_private
, buffer_mask
, buffers
);
126 static const __DRIuseInvalidateExtension use_invalidate
= {
127 { __DRI_USE_INVALIDATE
, 1 }
130 static const __DRIimageLookupExtension image_lookup_extension
= {
131 { __DRI_IMAGE_LOOKUP
, 1 },
135 static const __DRIdri2LoaderExtension dri2_loader_extension
= {
136 .base
= { __DRI_DRI2_LOADER
, 3 },
138 .getBuffers
= dri_get_buffers
,
139 .flushFrontBuffer
= dri_flush_front_buffer
,
140 .getBuffersWithFormat
= dri_get_buffers_with_format
,
143 static const __DRIimageLoaderExtension image_loader_extension
= {
144 .base
= { __DRI_IMAGE_LOADER
, 1 },
146 .getBuffers
= image_get_buffers
,
147 .flushFrontBuffer
= dri_flush_front_buffer
,
151 struct dri_extension_match
{
157 static struct dri_extension_match dri_core_extensions
[] = {
158 { __DRI2_FLUSH
, 1, offsetof(struct gbm_dri_device
, flush
) },
159 { __DRI_IMAGE
, 1, offsetof(struct gbm_dri_device
, image
) },
163 static struct dri_extension_match gbm_dri_device_extensions
[] = {
164 { __DRI_CORE
, 1, offsetof(struct gbm_dri_device
, core
) },
165 { __DRI_DRI2
, 1, offsetof(struct gbm_dri_device
, dri2
) },
170 dri_bind_extensions(struct gbm_dri_device
*dri
,
171 struct dri_extension_match
*matches
,
172 const __DRIextension
**extensions
)
177 for (i
= 0; extensions
[i
]; i
++) {
178 for (j
= 0; matches
[j
].name
; j
++) {
179 if (strcmp(extensions
[i
]->name
, matches
[j
].name
) == 0 &&
180 extensions
[i
]->version
>= matches
[j
].version
) {
181 field
= ((char *) dri
+ matches
[j
].offset
);
182 *(const __DRIextension
**) field
= extensions
[i
];
187 for (j
= 0; matches
[j
].name
; j
++) {
188 field
= ((char *) dri
+ matches
[j
].offset
);
189 if (*(const __DRIextension
**) field
== NULL
) {
198 dri_load_driver(struct gbm_dri_device
*dri
)
200 const __DRIextension
**extensions
= NULL
;
201 char path
[PATH_MAX
], *search_paths
, *p
, *next
, *end
;
202 char *get_extensions_name
;
205 if (geteuid() == getuid()) {
206 /* don't allow setuid apps to use GBM_DRIVERS_PATH */
207 search_paths
= getenv("GBM_DRIVERS_PATH");
209 if (search_paths
== NULL
)
210 search_paths
= DEFAULT_DRIVER_DIR
;
213 end
= search_paths
+ strlen(search_paths
);
214 for (p
= search_paths
; p
< end
&& dri
->driver
== NULL
; p
= next
+ 1) {
216 next
= strchr(p
, ':');
222 snprintf(path
, sizeof path
,
223 "%.*s/tls/%s_dri.so", len
, p
, dri
->base
.driver_name
);
224 dri
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
226 if (dri
->driver
== NULL
) {
227 snprintf(path
, sizeof path
,
228 "%.*s/%s_dri.so", len
, p
, dri
->base
.driver_name
);
229 dri
->driver
= dlopen(path
, RTLD_NOW
| RTLD_GLOBAL
);
231 /* not need continue to loop all paths once the driver is found */
232 if (dri
->driver
!= NULL
)
236 if (dri
->driver
== NULL
) {
237 fprintf(stderr
, "gbm: failed to open any driver (search paths %s)",
242 if (asprintf(&get_extensions_name
, "%s_%s",
243 __DRI_DRIVER_GET_EXTENSIONS
, dri
->base
.driver_name
) != -1) {
244 const __DRIextension
**(*get_extensions
)(void);
246 get_extensions
= dlsym(dri
->driver
, get_extensions_name
);
247 free(get_extensions_name
);
250 extensions
= get_extensions();
254 extensions
= dlsym(dri
->driver
, __DRI_DRIVER_EXTENSIONS
);
255 if (extensions
== NULL
) {
256 fprintf(stderr
, "gbm: driver exports no extensions (%s)", dlerror());
257 dlclose(dri
->driver
);
260 dri
->driver_extensions
= extensions
;
262 if (dri_bind_extensions(dri
, gbm_dri_device_extensions
, extensions
) < 0) {
263 dlclose(dri
->driver
);
264 fprintf(stderr
, "failed to bind extensions\n");
272 dri_screen_create(struct gbm_dri_device
*dri
)
274 const __DRIextension
**extensions
;
277 dri
->base
.driver_name
= loader_get_driver_for_fd(dri
->base
.base
.fd
, 0);
278 if (dri
->base
.driver_name
== NULL
)
281 ret
= dri_load_driver(dri
);
283 fprintf(stderr
, "failed to load driver: %s\n", dri
->base
.driver_name
);
287 dri
->extensions
[0] = &image_lookup_extension
.base
;
288 dri
->extensions
[1] = &use_invalidate
.base
;
289 dri
->extensions
[2] = &dri2_loader_extension
.base
;
290 dri
->extensions
[3] = &image_loader_extension
.base
;
291 dri
->extensions
[4] = NULL
;
293 if (dri
->dri2
== NULL
)
296 if (dri
->dri2
->base
.version
>= 4) {
297 dri
->screen
= dri
->dri2
->createNewScreen2(0, dri
->base
.base
.fd
,
299 dri
->driver_extensions
,
300 &dri
->driver_configs
, dri
);
302 dri
->screen
= dri
->dri2
->createNewScreen(0, dri
->base
.base
.fd
,
304 &dri
->driver_configs
, dri
);
306 if (dri
->screen
== NULL
)
309 extensions
= dri
->core
->getExtensions(dri
->screen
);
310 if (dri_bind_extensions(dri
, dri_core_extensions
, extensions
) < 0) {
315 dri
->lookup_image
= NULL
;
316 dri
->lookup_user_data
= NULL
;
321 dri
->core
->destroyScreen(dri
->screen
);
327 gbm_dri_is_format_supported(struct gbm_device
*gbm
,
332 case GBM_BO_FORMAT_XRGB8888
:
333 case GBM_FORMAT_XRGB8888
:
335 case GBM_BO_FORMAT_ARGB8888
:
336 case GBM_FORMAT_ARGB8888
:
337 if (usage
& GBM_BO_USE_SCANOUT
)
344 if (usage
& GBM_BO_USE_CURSOR_64X64
&&
345 usage
& GBM_BO_USE_RENDERING
)
352 gbm_dri_bo_write(struct gbm_bo
*_bo
, const void *buf
, size_t count
)
354 struct gbm_dri_bo
*bo
= gbm_dri_bo(_bo
);
356 if (bo
->image
!= NULL
)
359 memcpy(bo
->map
, buf
, count
);
365 gbm_dri_bo_get_fd(struct gbm_bo
*_bo
)
367 struct gbm_dri_device
*dri
= gbm_dri_device(_bo
->gbm
);
368 struct gbm_dri_bo
*bo
= gbm_dri_bo(_bo
);
371 if (bo
->image
== NULL
)
374 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_FD
, &fd
);
380 gbm_dri_bo_destroy(struct gbm_bo
*_bo
)
382 struct gbm_dri_device
*dri
= gbm_dri_device(_bo
->gbm
);
383 struct gbm_dri_bo
*bo
= gbm_dri_bo(_bo
);
384 struct drm_mode_destroy_dumb arg
;
386 if (bo
->image
!= NULL
) {
387 dri
->image
->destroyImage(bo
->image
);
389 munmap(bo
->map
, bo
->size
);
390 memset(&arg
, 0, sizeof(arg
));
391 arg
.handle
= bo
->handle
;
392 drmIoctl(dri
->base
.base
.fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &arg
);
399 gbm_dri_to_gbm_format(uint32_t dri_format
)
403 switch (dri_format
) {
404 case __DRI_IMAGE_FORMAT_RGB565
:
405 ret
= GBM_FORMAT_RGB565
;
407 case __DRI_IMAGE_FORMAT_XRGB8888
:
408 ret
= GBM_FORMAT_XRGB8888
;
410 case __DRI_IMAGE_FORMAT_ARGB8888
:
411 ret
= GBM_FORMAT_ARGB8888
;
413 case __DRI_IMAGE_FORMAT_ABGR8888
:
414 ret
= GBM_FORMAT_ABGR8888
;
424 static struct gbm_bo
*
425 gbm_dri_bo_import(struct gbm_device
*gbm
,
426 uint32_t type
, void *buffer
, uint32_t usage
)
428 struct gbm_dri_device
*dri
= gbm_dri_device(gbm
);
429 struct gbm_dri_bo
*bo
;
431 unsigned dri_use
= 0;
434 /* Required for query image WIDTH & HEIGHT */
435 if (dri
->image
->base
.version
< 4)
439 #if HAVE_WAYLAND_PLATFORM
440 case GBM_BO_IMPORT_WL_BUFFER
:
442 struct wl_drm_buffer
*wb
;
447 wb
= wayland_drm_buffer_get(dri
->wl_drm
, (struct wl_resource
*) buffer
);
451 image
= wb
->driver_buffer
;
453 switch (wb
->format
) {
454 case WL_DRM_FORMAT_XRGB8888
:
455 gbm_format
= GBM_FORMAT_XRGB8888
;
457 case WL_DRM_FORMAT_ARGB8888
:
458 gbm_format
= GBM_FORMAT_ARGB8888
;
460 case WL_DRM_FORMAT_RGB565
:
461 gbm_format
= GBM_FORMAT_RGB565
;
463 case WL_DRM_FORMAT_YUYV
:
464 gbm_format
= GBM_FORMAT_YUYV
;
473 case GBM_BO_IMPORT_EGL_IMAGE
:
476 if (dri
->lookup_image
== NULL
)
479 image
= dri
->lookup_image(dri
->screen
, buffer
, dri
->lookup_user_data
);
480 dri
->image
->queryImage(image
, __DRI_IMAGE_ATTRIB_FORMAT
, &dri_format
);
481 gbm_format
= gbm_dri_to_gbm_format(dri_format
);
492 bo
= calloc(1, sizeof *bo
);
496 bo
->image
= dri
->image
->dupImage(image
, NULL
);
498 if (usage
& GBM_BO_USE_SCANOUT
)
499 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
500 if (usage
& GBM_BO_USE_CURSOR_64X64
)
501 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
502 if (dri
->image
->base
.version
>= 2 &&
503 !dri
->image
->validateUsage(bo
->image
, dri_use
)) {
508 bo
->base
.base
.gbm
= gbm
;
509 bo
->base
.base
.format
= gbm_format
;
511 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_WIDTH
,
512 (int*)&bo
->base
.base
.width
);
513 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_HEIGHT
,
514 (int*)&bo
->base
.base
.height
);
515 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_STRIDE
,
516 (int*)&bo
->base
.base
.stride
);
517 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_HANDLE
,
518 &bo
->base
.base
.handle
.s32
);
520 return &bo
->base
.base
;
523 static struct gbm_bo
*
524 create_dumb(struct gbm_device
*gbm
,
525 uint32_t width
, uint32_t height
,
526 uint32_t format
, uint32_t usage
)
528 struct gbm_dri_device
*dri
= gbm_dri_device(gbm
);
529 struct drm_mode_create_dumb create_arg
;
530 struct drm_mode_map_dumb map_arg
;
531 struct gbm_dri_bo
*bo
;
532 struct drm_mode_destroy_dumb destroy_arg
;
535 if (!(usage
& GBM_BO_USE_CURSOR_64X64
))
537 if (format
!= GBM_FORMAT_ARGB8888
)
540 bo
= calloc(1, sizeof *bo
);
545 create_arg
.width
= width
;
546 create_arg
.height
= height
;
548 ret
= drmIoctl(dri
->base
.base
.fd
, DRM_IOCTL_MODE_CREATE_DUMB
, &create_arg
);
552 bo
->base
.base
.gbm
= gbm
;
553 bo
->base
.base
.width
= width
;
554 bo
->base
.base
.height
= height
;
555 bo
->base
.base
.stride
= create_arg
.pitch
;
556 bo
->base
.base
.format
= format
;
557 bo
->base
.base
.handle
.u32
= create_arg
.handle
;
558 bo
->handle
= create_arg
.handle
;
559 bo
->size
= create_arg
.size
;
561 memset(&map_arg
, 0, sizeof(map_arg
));
562 map_arg
.handle
= bo
->handle
;
564 ret
= drmIoctl(dri
->base
.base
.fd
, DRM_IOCTL_MODE_MAP_DUMB
, &map_arg
);
568 bo
->map
= mmap(0, bo
->size
, PROT_WRITE
,
569 MAP_SHARED
, dri
->base
.base
.fd
, map_arg
.offset
);
570 if (bo
->map
== MAP_FAILED
)
573 return &bo
->base
.base
;
576 memset(&destroy_arg
, 0, sizeof destroy_arg
);
577 destroy_arg
.handle
= create_arg
.handle
;
578 drmIoctl(dri
->base
.base
.fd
, DRM_IOCTL_MODE_DESTROY_DUMB
, &destroy_arg
);
585 static struct gbm_bo
*
586 gbm_dri_bo_create(struct gbm_device
*gbm
,
587 uint32_t width
, uint32_t height
,
588 uint32_t format
, uint32_t usage
)
590 struct gbm_dri_device
*dri
= gbm_dri_device(gbm
);
591 struct gbm_dri_bo
*bo
;
593 unsigned dri_use
= 0;
595 if (usage
& GBM_BO_USE_WRITE
)
596 return create_dumb(gbm
, width
, height
, format
, usage
);
598 bo
= calloc(1, sizeof *bo
);
602 bo
->base
.base
.gbm
= gbm
;
603 bo
->base
.base
.width
= width
;
604 bo
->base
.base
.height
= height
;
605 bo
->base
.base
.format
= format
;
608 case GBM_FORMAT_RGB565
:
609 dri_format
=__DRI_IMAGE_FORMAT_RGB565
;
611 case GBM_FORMAT_XRGB8888
:
612 case GBM_BO_FORMAT_XRGB8888
:
613 dri_format
= __DRI_IMAGE_FORMAT_XRGB8888
;
615 case GBM_FORMAT_ARGB8888
:
616 case GBM_BO_FORMAT_ARGB8888
:
617 dri_format
= __DRI_IMAGE_FORMAT_ARGB8888
;
619 case GBM_FORMAT_ABGR8888
:
620 dri_format
= __DRI_IMAGE_FORMAT_ABGR8888
;
622 case GBM_FORMAT_ARGB2101010
:
623 dri_format
= __DRI_IMAGE_FORMAT_ARGB2101010
;
625 case GBM_FORMAT_XRGB2101010
:
626 dri_format
= __DRI_IMAGE_FORMAT_XRGB2101010
;
632 if (usage
& GBM_BO_USE_SCANOUT
)
633 dri_use
|= __DRI_IMAGE_USE_SCANOUT
;
634 if (usage
& GBM_BO_USE_CURSOR_64X64
)
635 dri_use
|= __DRI_IMAGE_USE_CURSOR
;
637 /* Gallium drivers requires shared in order to get the handle/stride */
638 dri_use
|= __DRI_IMAGE_USE_SHARE
;
641 dri
->image
->createImage(dri
->screen
,
645 if (bo
->image
== NULL
)
648 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_HANDLE
,
649 &bo
->base
.base
.handle
.s32
);
650 dri
->image
->queryImage(bo
->image
, __DRI_IMAGE_ATTRIB_STRIDE
,
651 (int *) &bo
->base
.base
.stride
);
653 return &bo
->base
.base
;
660 static struct gbm_surface
*
661 gbm_dri_surface_create(struct gbm_device
*gbm
,
662 uint32_t width
, uint32_t height
,
663 uint32_t format
, uint32_t flags
)
665 struct gbm_dri_surface
*surf
;
667 surf
= calloc(1, sizeof *surf
);
671 surf
->base
.gbm
= gbm
;
672 surf
->base
.width
= width
;
673 surf
->base
.height
= height
;
674 surf
->base
.format
= format
;
675 surf
->base
.flags
= flags
;
681 gbm_dri_surface_destroy(struct gbm_surface
*_surf
)
683 struct gbm_dri_surface
*surf
= gbm_dri_surface(_surf
);
689 dri_destroy(struct gbm_device
*gbm
)
691 struct gbm_dri_device
*dri
= gbm_dri_device(gbm
);
693 dri
->core
->destroyScreen(dri
->screen
);
694 free(dri
->driver_configs
);
695 dlclose(dri
->driver
);
696 free(dri
->base
.driver_name
);
701 static struct gbm_device
*
702 dri_device_create(int fd
)
704 struct gbm_dri_device
*dri
;
707 dri
= calloc(1, sizeof *dri
);
709 dri
->base
.base
.fd
= fd
;
710 dri
->base
.base
.bo_create
= gbm_dri_bo_create
;
711 dri
->base
.base
.bo_import
= gbm_dri_bo_import
;
712 dri
->base
.base
.is_format_supported
= gbm_dri_is_format_supported
;
713 dri
->base
.base
.bo_write
= gbm_dri_bo_write
;
714 dri
->base
.base
.bo_get_fd
= gbm_dri_bo_get_fd
;
715 dri
->base
.base
.bo_destroy
= gbm_dri_bo_destroy
;
716 dri
->base
.base
.destroy
= dri_destroy
;
717 dri
->base
.base
.surface_create
= gbm_dri_surface_create
;
718 dri
->base
.base
.surface_destroy
= gbm_dri_surface_destroy
;
720 dri
->base
.type
= GBM_DRM_DRIVER_TYPE_DRI
;
721 dri
->base
.base
.name
= "drm";
723 ret
= dri_screen_create(dri
);
727 return &dri
->base
.base
;
735 struct gbm_backend gbm_dri_backend
= {
736 .backend_name
= "dri",
737 .create_device
= dri_device_create
,