2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 Chia-I Wu <olv@0xlab.org>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 #include <sys/types.h>
32 #include "util/u_memory.h"
35 #include "native_drm.h"
37 #include "gbm_gallium_drmint.h"
44 drm_display_is_format_supported(struct native_display
*ndpy
,
45 enum pipe_format fmt
, boolean is_color
)
47 return ndpy
->screen
->is_format_supported(ndpy
->screen
,
48 fmt
, PIPE_TEXTURE_2D
, 0,
49 (is_color
) ? PIPE_BIND_RENDER_TARGET
:
50 PIPE_BIND_DEPTH_STENCIL
);
53 static const struct native_config
**
54 drm_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
56 struct drm_display
*drmdpy
= drm_display(ndpy
);
57 const struct native_config
**configs
;
60 if (!drmdpy
->config
) {
61 struct native_config
*nconf
;
62 enum pipe_format format
;
64 drmdpy
->config
= CALLOC(1, sizeof(*drmdpy
->config
));
68 nconf
= &drmdpy
->config
->base
;
71 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
72 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
74 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
75 if (!drm_display_is_format_supported(&drmdpy
->base
, format
, TRUE
)) {
76 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
77 if (!drm_display_is_format_supported(&drmdpy
->base
, format
, TRUE
))
78 format
= PIPE_FORMAT_NONE
;
80 if (format
== PIPE_FORMAT_NONE
) {
82 drmdpy
->config
= NULL
;
86 nconf
->color_format
= format
;
89 if (drmdpy
->resources
)
90 nconf
->scanout_bit
= TRUE
;
93 configs
= MALLOC(sizeof(*configs
));
95 configs
[0] = &drmdpy
->config
->base
;
104 drm_display_get_param(struct native_display
*ndpy
,
105 enum native_param_type param
)
110 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
111 case NATIVE_PARAM_PRESERVE_BUFFER
:
112 case NATIVE_PARAM_MAX_SWAP_INTERVAL
:
122 drm_display_destroy(struct native_display
*ndpy
)
124 struct drm_display
*drmdpy
= drm_display(ndpy
);
127 FREE(drmdpy
->config
);
129 drm_display_fini_modeset(&drmdpy
->base
);
131 /* gbm owns screen */
135 if (drmdpy
->device_name
)
136 FREE(drmdpy
->device_name
);
138 if (drmdpy
->own_gbm
) {
139 gbm_device_destroy(&drmdpy
->gbmdrm
->base
.base
);
147 static struct native_display_buffer drm_display_buffer
= {
148 /* use the helpers */
149 drm_display_import_native_buffer
,
150 drm_display_export_native_buffer
154 drm_get_device_name(int fd
)
156 char *device_name
= NULL
;
159 struct udev_device
*device
;
164 if (fstat(fd
, &buf
) < 0) {
165 _eglLog(_EGL_WARNING
, "failed to stat fd %d", fd
);
169 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
170 if (device
== NULL
) {
171 _eglLog(_EGL_WARNING
,
172 "could not create udev device for fd %d", fd
);
176 tmp
= udev_device_get_devnode(device
);
179 device_name
= strdup(tmp
);
182 udev_device_unref(device
);
189 #ifdef HAVE_WAYLAND_BACKEND
192 drm_display_authenticate(void *user_data
, uint32_t magic
)
194 struct native_display
*ndpy
= user_data
;
195 struct drm_display
*drmdpy
= drm_display(ndpy
);
197 return drmAuthMagic(drmdpy
->fd
, magic
);
200 static struct wayland_drm_callbacks wl_drm_callbacks
= {
201 drm_display_authenticate
,
202 egl_g3d_wl_drm_helper_reference_buffer
,
203 egl_g3d_wl_drm_helper_unreference_buffer
207 drm_display_bind_wayland_display(struct native_display
*ndpy
,
208 struct wl_display
*wl_dpy
)
210 struct drm_display
*drmdpy
= drm_display(ndpy
);
212 if (drmdpy
->wl_server_drm
)
215 drmdpy
->wl_server_drm
= wayland_drm_init(wl_dpy
,
217 &wl_drm_callbacks
, ndpy
);
219 if (!drmdpy
->wl_server_drm
)
226 drm_display_unbind_wayland_display(struct native_display
*ndpy
,
227 struct wl_display
*wl_dpy
)
229 struct drm_display
*drmdpy
= drm_display(ndpy
);
231 if (!drmdpy
->wl_server_drm
)
234 wayland_drm_uninit(drmdpy
->wl_server_drm
);
235 drmdpy
->wl_server_drm
= NULL
;
240 static struct native_display_wayland_bufmgr drm_display_wayland_bufmgr
= {
241 drm_display_bind_wayland_display
,
242 drm_display_unbind_wayland_display
,
243 egl_g3d_wl_drm_common_wl_buffer_get_resource
,
244 egl_g3d_wl_drm_common_query_buffer
247 #endif /* HAVE_WAYLAND_BACKEND */
249 static struct native_surface
*
250 drm_create_pixmap_surface(struct native_display
*ndpy
,
251 EGLNativePixmapType pix
,
252 const struct native_config
*nconf
)
254 struct gbm_gallium_drm_bo
*bo
= (void *) pix
;
256 return drm_display_create_surface_from_resource(ndpy
, bo
->resource
);
260 drm_display_init_screen(struct native_display
*ndpy
)
265 static struct native_display
*
266 drm_create_display(struct gbm_gallium_drm_device
*gbmdrm
, int own_gbm
,
267 const struct native_event_handler
*event_handler
)
269 struct drm_display
*drmdpy
;
271 drmdpy
= CALLOC_STRUCT(drm_display
);
275 drmdpy
->gbmdrm
= gbmdrm
;
276 drmdpy
->own_gbm
= own_gbm
;
277 drmdpy
->fd
= gbmdrm
->base
.base
.fd
;
278 drmdpy
->device_name
= drm_get_device_name(drmdpy
->fd
);
280 gbmdrm
->lookup_egl_image
= (struct pipe_resource
*(*)(void *, void *))
281 event_handler
->lookup_egl_image
;
282 gbmdrm
->lookup_egl_image_data
= &drmdpy
->base
;
284 drmdpy
->event_handler
= event_handler
;
286 drmdpy
->base
.screen
= gbmdrm
->screen
;
288 drmdpy
->base
.init_screen
= drm_display_init_screen
;
289 drmdpy
->base
.destroy
= drm_display_destroy
;
290 drmdpy
->base
.get_param
= drm_display_get_param
;
291 drmdpy
->base
.get_configs
= drm_display_get_configs
;
293 drmdpy
->base
.create_pixmap_surface
= drm_create_pixmap_surface
;
295 drmdpy
->base
.buffer
= &drm_display_buffer
;
296 #ifdef HAVE_WAYLAND_BACKEND
297 if (drmdpy
->device_name
)
298 drmdpy
->base
.wayland_bufmgr
= &drm_display_wayland_bufmgr
;
300 drm_display_init_modeset(&drmdpy
->base
);
302 return &drmdpy
->base
;
305 static const struct native_event_handler
*drm_event_handler
;
307 static struct native_display
*
308 native_create_display(void *dpy
, boolean use_sw
)
310 struct gbm_gallium_drm_device
*gbm
;
317 const char *device_name
="/dev/dri/card0";
319 fd
= open(device_name
, O_RDWR
| O_CLOEXEC
);
320 if (fd
== -1 && errno
== EINVAL
)
323 fd
= open(device_name
, O_RDWR
);
325 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
327 /* FIXME: Use an internal constructor to create a gbm
328 * device with gallium backend directly, without setenv */
329 setenv("GBM_BACKEND", "gbm_gallium_drm.so", 1);
330 gbm
= gbm_gallium_drm_device(gbm_create_device(fd
));
337 if (strcmp(gbm_device_get_backend_name(&gbm
->base
.base
), "drm") != 0 ||
338 gbm
->base
.type
!= GBM_DRM_DRIVER_TYPE_GALLIUM
) {
340 gbm_device_destroy(&gbm
->base
.base
);
344 return drm_create_display(gbm
, own_gbm
, drm_event_handler
);
347 static const struct native_platform drm_platform
= {
349 native_create_display
352 const struct native_platform
*
353 native_get_drm_platform(const struct native_event_handler
*event_handler
)
355 drm_event_handler
= event_handler
;
356 return &drm_platform
;