2 * Mesa 3-D graphics library
5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
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.
26 #include "util/u_memory.h"
27 #include "util/u_inlines.h"
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "state_tracker/drm_driver.h"
37 #include "native_wayland.h"
39 /* see get_drm_screen_name */
40 #include <radeon_drm.h>
41 #include "radeon/drm/radeon_drm_public.h"
43 #include <wayland-client.h>
44 #include "wayland-egl-priv.h"
48 static struct native_event_handler
*wayland_event_handler
;
51 sync_callback(void *data
)
59 force_roundtrip(struct wl_display
*display
)
63 wl_display_sync_callback(display
, sync_callback
, &done
);
64 wl_display_iterate(display
, WL_DISPLAY_WRITABLE
);
66 wl_display_iterate(display
, WL_DISPLAY_READABLE
);
69 static const struct native_config
**
70 wayland_display_get_configs (struct native_display
*ndpy
, int *num_configs
)
72 struct wayland_display
*display
= wayland_display(ndpy
);
73 const struct native_config
**configs
;
75 if (!display
->config
) {
76 struct native_config
*nconf
;
77 enum pipe_format format
;
78 display
->config
= CALLOC(1, sizeof(*display
->config
));
81 nconf
= &display
->config
->base
;
84 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
85 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
87 format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
89 nconf
->color_format
= format
;
90 nconf
->window_bit
= TRUE
;
91 nconf
->pixmap_bit
= TRUE
;
94 configs
= MALLOC(sizeof(*configs
));
96 configs
[0] = &display
->config
->base
;
105 wayland_display_get_param(struct native_display
*ndpy
,
106 enum native_param_type param
)
111 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
112 case NATIVE_PARAM_PRESERVE_BUFFER
:
113 case NATIVE_PARAM_MAX_SWAP_INTERVAL
:
123 wayland_display_is_pixmap_supported(struct native_display
*ndpy
,
124 EGLNativePixmapType pix
,
125 const struct native_config
*nconf
)
127 /* all wl_egl_pixmaps are supported */
133 wayland_display_destroy(struct native_display
*ndpy
)
135 struct wayland_display
*display
= wayland_display(ndpy
);
138 FREE(display
->config
);
140 if (display
->base
.screen
)
141 display
->base
.screen
->destroy(display
->base
.screen
);
147 static struct wl_buffer
*
148 wayland_create_buffer(struct wayland_surface
*surface
,
149 enum native_attachment attachment
)
151 struct wayland_display
*display
= surface
->display
;
152 struct pipe_resource
*resource
;
153 struct winsys_handle wsh
;
156 resource
= resource_surface_get_single_resource(surface
->rsurf
, attachment
);
157 resource_surface_get_size(surface
->rsurf
, &width
, &height
);
159 wsh
.type
= DRM_API_HANDLE_TYPE_SHARED
;
160 display
->base
.screen
->resource_get_handle(display
->base
.screen
, resource
, &wsh
);
162 pipe_resource_reference(&resource
, NULL
);
164 return wl_drm_create_buffer(display
->dpy
->drm
, wsh
.handle
,
166 wsh
.stride
, surface
->win
->visual
);
170 wayland_pixmap_destroy(struct wl_egl_pixmap
*egl_pixmap
)
172 struct pipe_resource
*resource
= egl_pixmap
->driver_private
;
176 pipe_resource_reference(&resource
, NULL
);
178 egl_pixmap
->driver_private
= NULL
;
179 egl_pixmap
->destroy
= NULL
;
180 egl_pixmap
->name
= 0;
184 wayland_pixmap_surface_intialize(struct wayland_surface
*surface
)
186 struct native_display
*ndpy
= &surface
->display
->base
;
187 struct pipe_resource
*resource
;
188 struct winsys_handle wsh
;
189 const enum native_attachment front_natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
191 if (surface
->pix
->name
> 0)
194 resource
= resource_surface_get_single_resource(surface
->rsurf
, front_natt
);
196 wsh
.type
= DRM_API_HANDLE_TYPE_SHARED
;
197 ndpy
->screen
->resource_get_handle(ndpy
->screen
, resource
, &wsh
);
199 surface
->pix
->name
= wsh
.handle
;
200 surface
->pix
->stride
= wsh
.stride
;
201 surface
->pix
->destroy
= wayland_pixmap_destroy
;
202 surface
->pix
->driver_private
= resource
;
206 wayland_window_surface_handle_resize(struct wayland_surface
*surface
)
210 if (resource_surface_set_size(surface
->rsurf
,
211 surface
->win
->width
, surface
->win
->height
)) {
212 for (i
= 0; i
< WL_BUFFER_COUNT
; ++i
) {
213 if (surface
->buffer
[i
])
214 wl_buffer_destroy(surface
->buffer
[i
]);
215 surface
->buffer
[i
] = NULL
;
219 surface
->dx
= surface
->win
->dx
;
220 surface
->dy
= surface
->win
->dy
;
221 surface
->win
->dx
= 0;
222 surface
->win
->dy
= 0;
226 wayland_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
227 unsigned int *seq_num
, struct pipe_resource
**textures
,
228 int *width
, int *height
)
230 struct wayland_surface
*surface
= wayland_surface(nsurf
);
232 if (surface
->type
== WL_WINDOW_SURFACE
)
233 wayland_window_surface_handle_resize(surface
);
235 if (!resource_surface_add_resources(surface
->rsurf
, attachment_mask
|
236 surface
->attachment_mask
))
240 resource_surface_get_resources(surface
->rsurf
, textures
, attachment_mask
);
243 *seq_num
= surface
->sequence_number
;
245 resource_surface_get_size(surface
->rsurf
, (uint
*) width
, (uint
*) height
);
247 if (surface
->type
== WL_PIXMAP_SURFACE
)
248 wayland_pixmap_surface_intialize(surface
);
254 wayland_frame_callback(void *data
, uint32_t time
)
256 struct wayland_surface
*surface
= data
;
258 surface
->block_swap_buffers
= FALSE
;
262 wayland_buffers_swap(struct wl_buffer
**buffer
,
263 enum wayland_buffer_type buf1
,
264 enum wayland_buffer_type buf2
)
266 struct wl_buffer
*tmp
= buffer
[buf1
];
267 buffer
[buf1
] = buffer
[buf2
];
272 wayland_surface_swap_buffers(struct native_surface
*nsurf
)
274 struct wayland_surface
*surface
= wayland_surface(nsurf
);
275 struct wayland_display
*display
= surface
->display
;
277 while (surface
->block_swap_buffers
)
278 wl_display_iterate(display
->dpy
->display
, WL_DISPLAY_READABLE
);
280 surface
->block_swap_buffers
= TRUE
;
281 wl_display_frame_callback(display
->dpy
->display
, wayland_frame_callback
,
284 if (surface
->type
== WL_WINDOW_SURFACE
) {
285 resource_surface_swap_buffers(surface
->rsurf
,
286 NATIVE_ATTACHMENT_FRONT_LEFT
, NATIVE_ATTACHMENT_BACK_LEFT
, FALSE
);
288 wayland_buffers_swap(surface
->buffer
, WL_BUFFER_FRONT
, WL_BUFFER_BACK
);
290 if (surface
->buffer
[WL_BUFFER_FRONT
] == NULL
)
291 surface
->buffer
[WL_BUFFER_FRONT
] =
292 wayland_create_buffer(surface
, NATIVE_ATTACHMENT_FRONT_LEFT
);
294 wl_surface_attach(surface
->win
->surface
, surface
->buffer
[WL_BUFFER_FRONT
],
295 surface
->dx
, surface
->dy
);
297 resource_surface_get_size(surface
->rsurf
,
298 (uint
*) &surface
->win
->attached_width
,
299 (uint
*) &surface
->win
->attached_height
);
304 surface
->sequence_number
++;
305 wayland_event_handler
->invalid_surface(&display
->base
,
306 &surface
->base
, surface
->sequence_number
);
312 wayland_surface_present(struct native_surface
*nsurf
,
313 enum native_attachment natt
,
317 struct wayland_surface
*surface
= wayland_surface(nsurf
);
321 if (preserve
|| swap_interval
)
325 case NATIVE_ATTACHMENT_FRONT_LEFT
:
328 case NATIVE_ATTACHMENT_BACK_LEFT
:
329 ret
= wayland_surface_swap_buffers(nsurf
);
336 if (surface
->type
== WL_WINDOW_SURFACE
) {
337 resource_surface_get_size(surface
->rsurf
, &width
, &height
);
338 wl_surface_damage(surface
->win
->surface
, 0, 0, width
, height
);
345 wayland_surface_wait(struct native_surface
*nsurf
)
351 wayland_surface_destroy(struct native_surface
*nsurf
)
353 struct wayland_surface
*surface
= wayland_surface(nsurf
);
354 enum wayland_buffer_type buffer
;
356 for (buffer
= 0; buffer
< WL_BUFFER_COUNT
; ++buffer
) {
357 if (surface
->buffer
[buffer
])
358 wl_buffer_destroy(surface
->buffer
[buffer
]);
361 resource_surface_destroy(surface
->rsurf
);
365 static struct native_surface
*
366 wayland_create_pixmap_surface(struct native_display
*ndpy
,
367 EGLNativePixmapType pix
,
368 const struct native_config
*nconf
)
370 struct wayland_display
*display
= wayland_display(ndpy
);
371 struct wayland_config
*config
= wayland_config(nconf
);
372 struct wayland_surface
*surface
;
373 struct wl_egl_pixmap
*egl_pixmap
= (struct wl_egl_pixmap
*) pix
;
374 enum native_attachment natt
= NATIVE_ATTACHMENT_FRONT_LEFT
;
376 surface
= CALLOC_STRUCT(wayland_surface
);
380 surface
->display
= display
;
382 surface
->color_format
= config
->base
.color_format
;
384 surface
->color_format
= PIPE_FORMAT_B8G8R8X8_UNORM
;
386 surface
->type
= WL_PIXMAP_SURFACE
;
387 surface
->pix
= egl_pixmap
;
389 surface
->attachment_mask
= (1 << NATIVE_ATTACHMENT_FRONT_LEFT
);
391 surface
->rsurf
= resource_surface_create(display
->base
.screen
,
392 surface
->color_format
,
393 PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
|
394 PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
);
396 if (!surface
->rsurf
) {
401 resource_surface_set_size(surface
->rsurf
,
402 egl_pixmap
->width
, egl_pixmap
->height
);
404 /* the pixmap is already allocated, so import it */
405 if (surface
->pix
->name
> 0)
406 resource_surface_import_resource(surface
->rsurf
, natt
,
407 surface
->pix
->driver_private
);
409 surface
->base
.destroy
= wayland_surface_destroy
;
410 surface
->base
.present
= wayland_surface_present
;
411 surface
->base
.validate
= wayland_surface_validate
;
412 surface
->base
.wait
= wayland_surface_wait
;
414 return &surface
->base
;
417 static struct native_surface
*
418 wayland_create_window_surface(struct native_display
*ndpy
,
419 EGLNativeWindowType win
,
420 const struct native_config
*nconf
)
422 struct wayland_display
*display
= wayland_display(ndpy
);
423 struct wayland_config
*config
= wayland_config(nconf
);
424 struct wayland_surface
*surface
;
426 surface
= CALLOC_STRUCT(wayland_surface
);
430 surface
->display
= display
;
431 surface
->color_format
= config
->base
.color_format
;
433 surface
->win
= (struct wl_egl_window
*) win
;
435 surface
->block_swap_buffers
= FALSE
;
436 surface
->type
= WL_WINDOW_SURFACE
;
438 surface
->buffer
[WL_BUFFER_FRONT
] = NULL
;
439 surface
->buffer
[WL_BUFFER_BACK
] = NULL
;
440 surface
->attachment_mask
= (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
441 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
443 surface
->rsurf
= resource_surface_create(display
->base
.screen
,
444 surface
->color_format
,
445 PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
|
446 PIPE_BIND_DISPLAY_TARGET
| PIPE_BIND_SCANOUT
);
448 if (!surface
->rsurf
) {
453 surface
->base
.destroy
= wayland_surface_destroy
;
454 surface
->base
.present
= wayland_surface_present
;
455 surface
->base
.validate
= wayland_surface_validate
;
456 surface
->base
.wait
= wayland_surface_wait
;
458 return &surface
->base
;
462 get_drm_screen_name(int fd
, drmVersionPtr version
)
464 const char *name
= version
->name
;
466 if (name
&& !strcmp(name
, "radeon")) {
468 struct drm_radeon_info info
;
470 memset(&info
, 0, sizeof(info
));
471 info
.request
= RADEON_INFO_DEVICE_ID
;
472 info
.value
= pointer_to_intptr(&chip_id
);
473 if (drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
)) != 0)
476 name
= is_r3xx(chip_id
) ? "r300" : "r600";
483 wayland_display_init_screen(struct native_display
*ndpy
)
485 struct wayland_display
*display
= wayland_display(ndpy
);
486 drmVersionPtr version
;
487 const char *driver_name
;
489 if (display
->dpy
->fd
== -1)
490 force_roundtrip(display
->dpy
->display
);
491 if (display
->dpy
->fd
== -1)
494 if (!display
->dpy
->authenticated
)
495 force_roundtrip(display
->dpy
->display
);
496 if (!display
->dpy
->authenticated
)
499 version
= drmGetVersion(display
->dpy
->fd
);
501 _eglLog(_EGL_WARNING
, "invalid fd %d", display
->dpy
->fd
);
505 /* FIXME: share this with native_drm or egl_dri2 */
506 driver_name
= get_drm_screen_name(display
->dpy
->fd
, version
);
508 display
->base
.screen
=
509 wayland_event_handler
->new_drm_screen(&display
->base
,
510 driver_name
, display
->dpy
->fd
);
511 drmFreeVersion(version
);
513 if (!display
->base
.screen
) {
514 _eglLog(_EGL_WARNING
, "failed to create DRM screen");
523 wayland_set_event_handler(struct native_event_handler
*event_handler
)
525 wayland_event_handler
= event_handler
;
528 static struct pipe_resource
*
529 wayland_display_import_buffer(struct native_display
*ndpy
,
530 const struct pipe_resource
*templ
,
533 return ndpy
->screen
->resource_from_handle(ndpy
->screen
,
534 templ
, (struct winsys_handle
*) buf
);
538 wayland_display_export_buffer(struct native_display
*ndpy
,
539 struct pipe_resource
*res
,
542 return ndpy
->screen
->resource_get_handle(ndpy
->screen
,
543 res
, (struct winsys_handle
*) buf
);
546 static struct native_display_buffer wayland_display_buffer
= {
547 wayland_display_import_buffer
,
548 wayland_display_export_buffer
551 static struct native_display
*
552 wayland_display_create(void *dpy
, boolean use_sw
, void *user_data
)
554 struct wayland_display
*display
;
556 display
= CALLOC_STRUCT(wayland_display
);
560 display
->base
.user_data
= user_data
;
563 if (!display
->dpy
->display
) {
564 wayland_display_destroy(&display
->base
);
568 if (!wayland_display_init_screen(&display
->base
)) {
569 wayland_display_destroy(&display
->base
);
573 display
->base
.destroy
= wayland_display_destroy
;
574 display
->base
.get_param
= wayland_display_get_param
;
575 display
->base
.get_configs
= wayland_display_get_configs
;
576 display
->base
.is_pixmap_supported
= wayland_display_is_pixmap_supported
;
577 display
->base
.create_window_surface
= wayland_create_window_surface
;
578 display
->base
.create_pixmap_surface
= wayland_create_pixmap_surface
;
579 display
->base
.buffer
= &wayland_display_buffer
;
581 return &display
->base
;
584 static const struct native_platform wayland_platform
= {
585 "wayland", /* name */
586 wayland_set_event_handler
,
587 wayland_display_create
590 const struct native_platform
*
591 native_get_wayland_platform(void)
593 return &wayland_platform
;