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>
31 #include "util/u_memory.h"
34 #include "native_drm.h"
36 #include "gbm_gallium_drmint.h"
43 drm_display_is_format_supported(struct native_display
*ndpy
,
44 enum pipe_format fmt
, boolean is_color
)
46 return ndpy
->screen
->is_format_supported(ndpy
->screen
,
47 fmt
, PIPE_TEXTURE_2D
, 0,
48 (is_color
) ? PIPE_BIND_RENDER_TARGET
:
49 PIPE_BIND_DEPTH_STENCIL
);
52 static const struct native_config
**
53 drm_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
55 struct drm_display
*drmdpy
= drm_display(ndpy
);
56 const struct native_config
**configs
;
59 if (!drmdpy
->config
) {
60 struct native_config
*nconf
;
61 enum pipe_format format
;
63 drmdpy
->config
= CALLOC(1, sizeof(*drmdpy
->config
));
67 nconf
= &drmdpy
->config
->base
;
70 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
71 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
73 format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
74 if (!drm_display_is_format_supported(&drmdpy
->base
, format
, TRUE
)) {
75 format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
76 if (!drm_display_is_format_supported(&drmdpy
->base
, format
, TRUE
))
77 format
= PIPE_FORMAT_NONE
;
79 if (format
== PIPE_FORMAT_NONE
) {
81 drmdpy
->config
= NULL
;
85 nconf
->color_format
= format
;
88 if (drmdpy
->resources
)
89 nconf
->scanout_bit
= TRUE
;
92 configs
= MALLOC(sizeof(*configs
));
94 configs
[0] = &drmdpy
->config
->base
;
103 drm_display_get_param(struct native_display
*ndpy
,
104 enum native_param_type param
)
109 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
110 case NATIVE_PARAM_PRESERVE_BUFFER
:
111 case NATIVE_PARAM_MAX_SWAP_INTERVAL
:
121 drm_display_destroy(struct native_display
*ndpy
)
123 struct drm_display
*drmdpy
= drm_display(ndpy
);
126 FREE(drmdpy
->config
);
128 drm_display_fini_modeset(&drmdpy
->base
);
132 if (drmdpy
->device_name
)
133 FREE(drmdpy
->device_name
);
141 static struct native_display_buffer drm_display_buffer
= {
142 /* use the helpers */
143 drm_display_import_native_buffer
,
144 drm_display_export_native_buffer
148 drm_display_authenticate(void *user_data
, uint32_t magic
)
150 struct native_display
*ndpy
= user_data
;
151 struct drm_display
*drmdpy
= drm_display(ndpy
);
153 return drmAuthMagic(drmdpy
->fd
, magic
);
157 drm_get_device_name(int fd
)
159 char *device_name
= NULL
;
162 struct udev_device
*device
;
167 if (fstat(fd
, &buf
) < 0) {
168 _eglLog(_EGL_WARNING
, "failed to stat fd %d", fd
);
172 device
= udev_device_new_from_devnum(udev
, 'c', buf
.st_rdev
);
173 if (device
== NULL
) {
174 _eglLog(_EGL_WARNING
,
175 "could not create udev device for fd %d", fd
);
179 tmp
= udev_device_get_devnode(device
);
182 device_name
= strdup(tmp
);
185 udev_device_unref(device
);
192 #ifdef HAVE_WAYLAND_BACKEND
194 static struct wayland_drm_callbacks wl_drm_callbacks
= {
195 drm_display_authenticate
,
196 egl_g3d_wl_drm_helper_reference_buffer
,
197 egl_g3d_wl_drm_helper_unreference_buffer
201 drm_display_bind_wayland_display(struct native_display
*ndpy
,
202 struct wl_display
*wl_dpy
)
204 struct drm_display
*drmdpy
= drm_display(ndpy
);
206 if (drmdpy
->wl_server_drm
)
209 drmdpy
->wl_server_drm
= wayland_drm_init(wl_dpy
,
211 &wl_drm_callbacks
, ndpy
);
213 if (!drmdpy
->wl_server_drm
)
220 drm_display_unbind_wayland_display(struct native_display
*ndpy
,
221 struct wl_display
*wl_dpy
)
223 struct drm_display
*drmdpy
= drm_display(ndpy
);
225 if (!drmdpy
->wl_server_drm
)
228 wayland_drm_uninit(drmdpy
->wl_server_drm
);
229 drmdpy
->wl_server_drm
= NULL
;
234 static struct native_display_wayland_bufmgr drm_display_wayland_bufmgr
= {
235 drm_display_bind_wayland_display
,
236 drm_display_unbind_wayland_display
,
237 egl_g3d_wl_drm_common_wl_buffer_get_resource
240 #endif /* HAVE_WAYLAND_BACKEND */
242 static struct native_surface
*
243 drm_create_pixmap_surface(struct native_display
*ndpy
,
244 EGLNativePixmapType pix
,
245 const struct native_config
*nconf
)
247 struct gbm_gallium_drm_bo
*bo
= (void *) pix
;
249 return drm_display_create_surface_from_resource(ndpy
, bo
->resource
);
253 drm_display_init_screen(struct native_display
*ndpy
)
258 static struct native_display
*
259 drm_create_display(struct gbm_gallium_drm_device
*gbmdrm
,
260 const struct native_event_handler
*event_handler
)
262 struct drm_display
*drmdpy
;
264 drmdpy
= CALLOC_STRUCT(drm_display
);
268 drmdpy
->fd
= gbmdrm
->base
.base
.fd
;
269 drmdpy
->device_name
= drm_get_device_name(drmdpy
->fd
);
271 gbmdrm
->lookup_egl_image
= (struct pipe_resource
*(*)(void *, void *))
272 event_handler
->lookup_egl_image
;
273 gbmdrm
->lookup_egl_image_data
= &drmdpy
->base
;
275 drmdpy
->event_handler
= event_handler
;
277 drmdpy
->base
.screen
= gbmdrm
->screen
;
279 drmdpy
->base
.init_screen
= drm_display_init_screen
;
280 drmdpy
->base
.destroy
= drm_display_destroy
;
281 drmdpy
->base
.get_param
= drm_display_get_param
;
282 drmdpy
->base
.get_configs
= drm_display_get_configs
;
284 drmdpy
->base
.create_pixmap_surface
= drm_create_pixmap_surface
;
286 drmdpy
->base
.buffer
= &drm_display_buffer
;
287 #ifdef HAVE_WAYLAND_BACKEND
288 if (drmdpy
->device_name
)
289 drmdpy
->base
.wayland_bufmgr
= &drm_display_wayland_bufmgr
;
291 drm_display_init_modeset(&drmdpy
->base
);
293 return &drmdpy
->base
;
296 static const struct native_event_handler
*drm_event_handler
;
298 static struct native_display
*
299 native_create_display(void *dpy
, boolean use_sw
)
301 struct gbm_gallium_drm_device
*gbm
;
307 fd
= open("/dev/dri/card0", O_RDWR
);
308 gbm
= gbm_gallium_drm_device(gbm_create_device(fd
));
314 if (strcmp(gbm_device_get_backend_name(&gbm
->base
.base
), "drm") != 0 ||
315 gbm
->base
.type
!= GBM_DRM_DRIVER_TYPE_GALLIUM
)
318 return drm_create_display(gbm
, drm_event_handler
);
321 static const struct native_platform drm_platform
= {
323 native_create_display
326 const struct native_platform
*
327 native_get_drm_platform(const struct native_event_handler
*event_handler
)
329 drm_event_handler
= event_handler
;
330 return &drm_platform
;