native_wayland.c revision 87c3bb65bbd4fdf868d57c2c58f3330efa36b75b
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.11 4 * 5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com> 6 * 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: 13 * 14 * The above copyright notice and this permission notice shall be included 15 * in all copies or substantial portions of the Software. 16 * 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. 24 */ 25 26#include "util/u_memory.h" 27#include "util/u_inlines.h" 28 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" 34#include "egllog.h" 35 36#include "native_wayland.h" 37 38static struct native_event_handler *wayland_event_handler; 39 40static void 41sync_callback(void *data) 42{ 43 int *done = data; 44 45 *done = 1; 46} 47 48static void 49force_roundtrip(struct wl_display *display) 50{ 51 int done = 0; 52 53 wl_display_sync_callback(display, sync_callback, &done); 54 wl_display_iterate(display, WL_DISPLAY_WRITABLE); 55 while (!done) 56 wl_display_iterate(display, WL_DISPLAY_READABLE); 57} 58 59static const struct native_config ** 60wayland_display_get_configs (struct native_display *ndpy, int *num_configs) 61{ 62 struct wayland_display *display = wayland_display(ndpy); 63 const struct native_config **configs; 64 int i; 65 66 if (!display->config) { 67 struct native_config *nconf; 68 display->config = CALLOC(2, sizeof(*display->config)); 69 if (!display->config) 70 return NULL; 71 72 for (i = 0; i < 2; ++i) { 73 nconf = &display->config[i].base; 74 75 nconf->buffer_mask = 76 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 77 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 78 79 nconf->window_bit = TRUE; 80 nconf->pixmap_bit = TRUE; 81 } 82 83 display->config[0].base.color_format = PIPE_FORMAT_B8G8R8A8_UNORM; 84 display->config[1].base.color_format = PIPE_FORMAT_B8G8R8X8_UNORM; 85 } 86 87 configs = MALLOC(2 * sizeof(*configs)); 88 if (configs) { 89 configs[0] = &display->config[0].base; 90 configs[1] = &display->config[1].base; 91 if (num_configs) 92 *num_configs = 2; 93 } 94 95 return configs; 96} 97 98static int 99wayland_display_get_param(struct native_display *ndpy, 100 enum native_param_type param) 101{ 102 int val; 103 104 switch (param) { 105 case NATIVE_PARAM_USE_NATIVE_BUFFER: 106 case NATIVE_PARAM_PRESERVE_BUFFER: 107 case NATIVE_PARAM_MAX_SWAP_INTERVAL: 108 default: 109 val = 0; 110 break; 111 } 112 113 return val; 114} 115 116static boolean 117wayland_display_is_pixmap_supported(struct native_display *ndpy, 118 EGLNativePixmapType pix, 119 const struct native_config *nconf) 120{ 121 /* all wl_egl_pixmaps are supported */ 122 123 return TRUE; 124} 125 126static void 127wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap) 128{ 129 struct pipe_resource *resource = egl_pixmap->driver_private; 130 131 assert(resource); 132 133 pipe_resource_reference(&resource, NULL); 134 if (egl_pixmap->buffer) { 135 wl_buffer_destroy(egl_pixmap->buffer); 136 egl_pixmap->buffer = NULL; 137 } 138 139 egl_pixmap->driver_private = NULL; 140 egl_pixmap->destroy = NULL; 141} 142 143static void 144wayland_pixmap_surface_initialize(struct wayland_surface *surface) 145{ 146 struct wayland_display *display = wayland_display(&surface->display->base); 147 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT; 148 149 if (surface->pix->buffer != NULL) 150 return; 151 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); 156} 157 158static void 159wayland_release_pending_resource(void *data) 160{ 161 struct wayland_surface *surface = data; 162 163 /* FIXME: print internal error */ 164 if (!surface->pending_resource) 165 return; 166 167 pipe_resource_reference(&surface->pending_resource, NULL); 168} 169 170static void 171wayland_window_surface_handle_resize(struct wayland_surface *surface) 172{ 173 struct wayland_display *display = surface->display; 174 struct pipe_resource *front_resource; 175 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT; 176 int i; 177 178 front_resource = resource_surface_get_single_resource(surface->rsurf, 179 front_natt); 180 if (resource_surface_set_size(surface->rsurf, 181 surface->win->width, surface->win->height)) { 182 183 if (surface->pending_resource) 184 force_roundtrip(display->dpy); 185 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); 191 } 192 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; 197 } 198 199 surface->dx = surface->win->dx; 200 surface->dy = surface->win->dy; 201 } 202 pipe_resource_reference(&front_resource, NULL); 203} 204 205static boolean 206wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask, 207 unsigned int *seq_num, struct pipe_resource **textures, 208 int *width, int *height) 209{ 210 struct wayland_surface *surface = wayland_surface(nsurf); 211 212 if (surface->type == WL_WINDOW_SURFACE) 213 wayland_window_surface_handle_resize(surface); 214 215 if (!resource_surface_add_resources(surface->rsurf, attachment_mask | 216 surface->attachment_mask)) 217 return FALSE; 218 219 if (textures) 220 resource_surface_get_resources(surface->rsurf, textures, attachment_mask); 221 222 if (seq_num) 223 *seq_num = surface->sequence_number; 224 225 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height); 226 227 if (surface->type == WL_PIXMAP_SURFACE) 228 wayland_pixmap_surface_initialize(surface); 229 230 return TRUE; 231} 232 233static void 234wayland_frame_callback(struct wl_surface *surf, void *data, uint32_t time) 235{ 236 struct wayland_surface *surface = data; 237 238 surface->block_swap_buffers = FALSE; 239} 240 241static INLINE void 242wayland_buffers_swap(struct wl_buffer **buffer, 243 enum wayland_buffer_type buf1, 244 enum wayland_buffer_type buf2) 245{ 246 struct wl_buffer *tmp = buffer[buf1]; 247 buffer[buf1] = buffer[buf2]; 248 buffer[buf2] = tmp; 249} 250 251static boolean 252wayland_surface_swap_buffers(struct native_surface *nsurf) 253{ 254 struct wayland_surface *surface = wayland_surface(nsurf); 255 struct wayland_display *display = surface->display; 256 257 while (surface->block_swap_buffers) 258 wl_display_iterate(display->dpy, WL_DISPLAY_READABLE); 259 260 surface->block_swap_buffers = TRUE; 261 wl_display_frame_callback(display->dpy, surface->win->surface, 262 wayland_frame_callback, surface); 263 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); 268 269 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK); 270 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); 275 276 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT], 277 surface->dx, surface->dy); 278 279 resource_surface_get_size(surface->rsurf, 280 (uint *) &surface->win->attached_width, 281 (uint *) &surface->win->attached_height); 282 surface->dx = 0; 283 surface->dy = 0; 284 } 285 286 surface->sequence_number++; 287 wayland_event_handler->invalid_surface(&display->base, 288 &surface->base, 289 surface->sequence_number); 290 291 return TRUE; 292} 293 294static boolean 295wayland_surface_present(struct native_surface *nsurf, 296 enum native_attachment natt, 297 boolean preserve, 298 uint swap_interval) 299{ 300 struct wayland_surface *surface = wayland_surface(nsurf); 301 uint width, height; 302 boolean ret; 303 304 if (preserve || swap_interval) 305 return FALSE; 306 307 switch (natt) { 308 case NATIVE_ATTACHMENT_FRONT_LEFT: 309 ret = TRUE; 310 break; 311 case NATIVE_ATTACHMENT_BACK_LEFT: 312 ret = wayland_surface_swap_buffers(nsurf); 313 break; 314 default: 315 ret = FALSE; 316 break; 317 } 318 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); 323 } 324 325 return ret; 326} 327 328static void 329wayland_surface_wait(struct native_surface *nsurf) 330{ 331 /* no-op */ 332} 333 334static void 335wayland_surface_destroy(struct native_surface *nsurf) 336{ 337 struct wayland_surface *surface = wayland_surface(nsurf); 338 enum wayland_buffer_type buffer; 339 340 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) { 341 if (surface->buffer[buffer]) 342 wl_buffer_destroy(surface->buffer[buffer]); 343 } 344 345 resource_surface_destroy(surface->rsurf); 346 FREE(surface); 347} 348 349 350 351static struct native_surface * 352wayland_create_pixmap_surface(struct native_display *ndpy, 353 EGLNativePixmapType pix, 354 const struct native_config *nconf) 355{ 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; 362 363 surface = CALLOC_STRUCT(wayland_surface); 364 if (!surface) 365 return NULL; 366 367 surface->display = display; 368 369 surface->pending_resource = NULL; 370 surface->type = WL_PIXMAP_SURFACE; 371 surface->pix = egl_pixmap; 372 373 if (nconf) 374 surface->color_format = nconf->color_format; 375 else /* FIXME: derive format from wl_visual */ 376 surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM; 377 378 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT); 379 380 surface->rsurf = resource_surface_create(display->base.screen, 381 surface->color_format, bind); 382 383 if (!surface->rsurf) { 384 FREE(surface); 385 return NULL; 386 } 387 388 resource_surface_set_size(surface->rsurf, 389 egl_pixmap->width, egl_pixmap->height); 390 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); 395 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; 400 401 return &surface->base; 402} 403 404 405static struct native_surface * 406wayland_create_window_surface(struct native_display *ndpy, 407 EGLNativeWindowType win, 408 const struct native_config *nconf) 409{ 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; 415 416 surface = CALLOC_STRUCT(wayland_surface); 417 if (!surface) 418 return NULL; 419 420 surface->display = display; 421 surface->color_format = config->base.color_format; 422 423 surface->win = (struct wl_egl_window *) win; 424 425 surface->pending_resource = NULL; 426 surface->block_swap_buffers = FALSE; 427 surface->type = WL_WINDOW_SURFACE; 428 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); 433 434 surface->rsurf = resource_surface_create(display->base.screen, 435 surface->color_format, bind); 436 437 if (!surface->rsurf) { 438 FREE(surface); 439 return NULL; 440 } 441 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; 446 447 return &surface->base; 448} 449 450static void 451native_set_event_handler(struct native_event_handler *event_handler) 452{ 453 wayland_event_handler = event_handler; 454} 455 456static struct native_display * 457native_create_display(void *dpy, boolean use_sw, void *user_data) 458{ 459 struct wayland_display *display = NULL; 460 boolean own_dpy = FALSE; 461 462 use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE); 463 464 if (dpy == NULL) { 465 dpy = wl_display_connect(NULL); 466 if (dpy == NULL) 467 return NULL; 468 own_dpy = TRUE; 469 } 470 471 if (use_sw) { 472 _eglLog(_EGL_INFO, "use software fallback"); 473 display = wayland_create_shm_display((struct wl_display *) dpy, 474 wayland_event_handler, 475 user_data); 476 } else { 477 display = wayland_create_drm_display((struct wl_display *) dpy, 478 wayland_event_handler, 479 user_data); 480 } 481 482 if (!display) 483 return NULL; 484 485 display->base.get_param = wayland_display_get_param; 486 display->base.get_configs = wayland_display_get_configs; 487 display->base.is_pixmap_supported = wayland_display_is_pixmap_supported; 488 display->base.create_window_surface = wayland_create_window_surface; 489 display->base.create_pixmap_surface = wayland_create_pixmap_surface; 490 491 display->own_dpy = own_dpy; 492 493 return &display->base; 494} 495 496static const struct native_platform wayland_platform = { 497 "wayland", /* name */ 498 native_set_event_handler, 499 native_create_display 500}; 501 502const struct native_platform * 503native_get_wayland_platform(void) 504{ 505 return &wayland_platform; 506} 507 508/* vim: set sw=3 ts=8 sts=3 expandtab: */ 509