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"
36 #include "native_wayland.h"
38 static const struct native_event_handler *wayland_event_handler;
41 sync_callback(void *data)
49 force_roundtrip(struct wl_display *display)
53 wl_display_sync_callback(display, sync_callback, &done);
54 wl_display_iterate(display, WL_DISPLAY_WRITABLE);
56 wl_display_iterate(display, WL_DISPLAY_READABLE);
59 static const struct native_config **
60 wayland_display_get_configs (struct native_display *ndpy, int *num_configs)
62 struct wayland_display *display = wayland_display(ndpy);
63 const struct native_config **configs;
66 if (!display->config) {
67 struct native_config *nconf;
68 display->config = CALLOC(2, sizeof(*display->config));
72 for (i = 0; i < 2; ++i) {
73 nconf = &display->config[i].base;
76 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
77 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
79 nconf->window_bit = TRUE;
80 nconf->pixmap_bit = TRUE;
83 display->config[0].base.color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
84 display->config[1].base.color_format = PIPE_FORMAT_B8G8R8X8_UNORM;
87 configs = MALLOC(2 * sizeof(*configs));
89 configs[0] = &display->config[0].base;
90 configs[1] = &display->config[1].base;
99 wayland_display_get_param(struct native_display *ndpy,
100 enum native_param_type param)
105 case NATIVE_PARAM_USE_NATIVE_BUFFER:
106 case NATIVE_PARAM_PRESERVE_BUFFER:
107 case NATIVE_PARAM_MAX_SWAP_INTERVAL:
117 wayland_display_is_pixmap_supported(struct native_display *ndpy,
118 EGLNativePixmapType pix,
119 const struct native_config *nconf)
121 /* all wl_egl_pixmaps are supported */
127 wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap)
129 struct pipe_resource *resource = egl_pixmap->driver_private;
133 pipe_resource_reference(&resource, NULL);
134 if (egl_pixmap->buffer) {
135 wl_buffer_destroy(egl_pixmap->buffer);
136 egl_pixmap->buffer = NULL;
139 egl_pixmap->driver_private = NULL;
140 egl_pixmap->destroy = NULL;
144 wayland_pixmap_surface_initialize(struct wayland_surface *surface)
146 struct wayland_display *display = wayland_display(&surface->display->base);
147 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
149 if (surface->pix->buffer != NULL)
152 surface->pix->buffer = display->create_buffer(display, surface, front_natt);
153 surface->pix->destroy = wayland_pixmap_destroy;
154 surface->pix->driver_private =
155 resource_surface_get_single_resource(surface->rsurf, front_natt);
159 wayland_release_pending_resource(void *data)
161 struct wayland_surface *surface = data;
163 /* FIXME: print internal error */
164 if (!surface->pending_resource)
167 pipe_resource_reference(&surface->pending_resource, NULL);
171 wayland_window_surface_handle_resize(struct wayland_surface *surface)
173 struct wayland_display *display = surface->display;
174 struct pipe_resource *front_resource;
175 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
178 front_resource = resource_surface_get_single_resource(surface->rsurf,
180 if (resource_surface_set_size(surface->rsurf,
181 surface->win->width, surface->win->height)) {
183 if (surface->pending_resource)
184 force_roundtrip(display->dpy);
186 if (front_resource) {
187 surface->pending_resource = front_resource;
188 front_resource = NULL;
189 wl_display_sync_callback(display->dpy,
190 wayland_release_pending_resource, surface);
193 for (i = 0; i < WL_BUFFER_COUNT; ++i) {
194 if (surface->buffer[i])
195 wl_buffer_destroy(surface->buffer[i]);
196 surface->buffer[i] = NULL;
199 surface->dx = surface->win->dx;
200 surface->dy = surface->win->dy;
202 pipe_resource_reference(&front_resource, NULL);
206 wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask,
207 unsigned int *seq_num, struct pipe_resource **textures,
208 int *width, int *height)
210 struct wayland_surface *surface = wayland_surface(nsurf);
212 if (surface->type == WL_WINDOW_SURFACE)
213 wayland_window_surface_handle_resize(surface);
215 if (!resource_surface_add_resources(surface->rsurf, attachment_mask |
216 surface->attachment_mask))
220 resource_surface_get_resources(surface->rsurf, textures, attachment_mask);
223 *seq_num = surface->sequence_number;
225 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height);
227 if (surface->type == WL_PIXMAP_SURFACE)
228 wayland_pixmap_surface_initialize(surface);
234 wayland_frame_callback(struct wl_surface *surf, void *data, uint32_t time)
236 struct wayland_surface *surface = data;
238 surface->block_swap_buffers = FALSE;
242 wayland_buffers_swap(struct wl_buffer **buffer,
243 enum wayland_buffer_type buf1,
244 enum wayland_buffer_type buf2)
246 struct wl_buffer *tmp = buffer[buf1];
247 buffer[buf1] = buffer[buf2];
252 wayland_surface_swap_buffers(struct native_surface *nsurf)
254 struct wayland_surface *surface = wayland_surface(nsurf);
255 struct wayland_display *display = surface->display;
257 while (surface->block_swap_buffers)
258 wl_display_iterate(display->dpy, WL_DISPLAY_READABLE);
260 surface->block_swap_buffers = TRUE;
261 wl_display_frame_callback(display->dpy, surface->win->surface,
262 wayland_frame_callback, surface);
264 if (surface->type == WL_WINDOW_SURFACE) {
265 resource_surface_swap_buffers(surface->rsurf,
266 NATIVE_ATTACHMENT_FRONT_LEFT,
267 NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
269 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK);
271 if (surface->buffer[WL_BUFFER_FRONT] == NULL)
272 surface->buffer[WL_BUFFER_FRONT] =
273 display->create_buffer(display, surface,
274 NATIVE_ATTACHMENT_FRONT_LEFT);
276 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT],
277 surface->dx, surface->dy);
279 resource_surface_get_size(surface->rsurf,
280 (uint *) &surface->win->attached_width,
281 (uint *) &surface->win->attached_height);
286 surface->sequence_number++;
287 wayland_event_handler->invalid_surface(&display->base,
289 surface->sequence_number);
295 wayland_surface_present(struct native_surface *nsurf,
296 enum native_attachment natt,
300 struct wayland_surface *surface = wayland_surface(nsurf);
304 if (preserve || swap_interval)
308 case NATIVE_ATTACHMENT_FRONT_LEFT:
311 case NATIVE_ATTACHMENT_BACK_LEFT:
312 ret = wayland_surface_swap_buffers(nsurf);
319 if (surface->type == WL_WINDOW_SURFACE) {
320 resource_surface_get_size(surface->rsurf, &width, &height);
321 wl_buffer_damage(surface->buffer[WL_BUFFER_FRONT], 0, 0, width, height);
322 wl_surface_damage(surface->win->surface, 0, 0, width, height);
329 wayland_surface_wait(struct native_surface *nsurf)
335 wayland_surface_destroy(struct native_surface *nsurf)
337 struct wayland_surface *surface = wayland_surface(nsurf);
338 enum wayland_buffer_type buffer;
340 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
341 if (surface->buffer[buffer])
342 wl_buffer_destroy(surface->buffer[buffer]);
345 resource_surface_destroy(surface->rsurf);
351 static struct native_surface *
352 wayland_create_pixmap_surface(struct native_display *ndpy,
353 EGLNativePixmapType pix,
354 const struct native_config *nconf)
356 struct wayland_display *display = wayland_display(ndpy);
357 struct wayland_surface *surface;
358 struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix;
359 enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT;
360 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
361 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
363 surface = CALLOC_STRUCT(wayland_surface);
367 surface->display = display;
369 surface->pending_resource = NULL;
370 surface->type = WL_PIXMAP_SURFACE;
371 surface->pix = egl_pixmap;
374 surface->color_format = nconf->color_format;
375 else /* FIXME: derive format from wl_visual */
376 surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM;
378 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT);
380 surface->rsurf = resource_surface_create(display->base.screen,
381 surface->color_format, bind);
383 if (!surface->rsurf) {
388 resource_surface_set_size(surface->rsurf,
389 egl_pixmap->width, egl_pixmap->height);
391 /* the pixmap is already allocated, so import it */
392 if (surface->pix->buffer != NULL)
393 resource_surface_import_resource(surface->rsurf, natt,
394 surface->pix->driver_private);
396 surface->base.destroy = wayland_surface_destroy;
397 surface->base.present = wayland_surface_present;
398 surface->base.validate = wayland_surface_validate;
399 surface->base.wait = wayland_surface_wait;
401 return &surface->base;
405 static struct native_surface *
406 wayland_create_window_surface(struct native_display *ndpy,
407 EGLNativeWindowType win,
408 const struct native_config *nconf)
410 struct wayland_display *display = wayland_display(ndpy);
411 struct wayland_config *config = wayland_config(nconf);
412 struct wayland_surface *surface;
413 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
414 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
416 surface = CALLOC_STRUCT(wayland_surface);
420 surface->display = display;
421 surface->color_format = config->base.color_format;
423 surface->win = (struct wl_egl_window *) win;
425 surface->pending_resource = NULL;
426 surface->block_swap_buffers = FALSE;
427 surface->type = WL_WINDOW_SURFACE;
429 surface->buffer[WL_BUFFER_FRONT] = NULL;
430 surface->buffer[WL_BUFFER_BACK] = NULL;
431 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
432 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
434 surface->rsurf = resource_surface_create(display->base.screen,
435 surface->color_format, bind);
437 if (!surface->rsurf) {
442 surface->base.destroy = wayland_surface_destroy;
443 surface->base.present = wayland_surface_present;
444 surface->base.validate = wayland_surface_validate;
445 surface->base.wait = wayland_surface_wait;
447 return &surface->base;
450 static struct native_display *
451 native_create_display(void *dpy, boolean use_sw)
453 struct wayland_display *display = NULL;
454 boolean own_dpy = FALSE;
456 use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE);
459 dpy = wl_display_connect(NULL);
466 _eglLog(_EGL_INFO, "use software fallback");
467 display = wayland_create_shm_display((struct wl_display *) dpy,
468 wayland_event_handler);
470 display = wayland_create_drm_display((struct wl_display *) dpy,
471 wayland_event_handler);
477 display->base.get_param = wayland_display_get_param;
478 display->base.get_configs = wayland_display_get_configs;
479 display->base.is_pixmap_supported = wayland_display_is_pixmap_supported;
480 display->base.create_window_surface = wayland_create_window_surface;
481 display->base.create_pixmap_surface = wayland_create_pixmap_surface;
483 display->own_dpy = own_dpy;
485 return &display->base;
488 static const struct native_platform wayland_platform = {
489 "wayland", /* name */
490 native_create_display
493 const struct native_platform *
494 native_get_wayland_platform(const struct native_event_handler *event_handler)
496 wayland_event_handler = event_handler;
497 return &wayland_platform;
500 /* vim: set sw=3 ts=8 sts=3 expandtab: */