native_wayland.c revision ca79036fe8170a87fe8118fb812320c2536b6d46
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 pipe_resource_reference(&front_resource, NULL); 200 201 surface->dx = surface->win->dx; 202 surface->dy = surface->win->dy; 203 surface->win->dx = 0; 204 surface->win->dy = 0; 205} 206 207static boolean 208wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask, 209 unsigned int *seq_num, struct pipe_resource **textures, 210 int *width, int *height) 211{ 212 struct wayland_surface *surface = wayland_surface(nsurf); 213 214 if (surface->type == WL_WINDOW_SURFACE) 215 wayland_window_surface_handle_resize(surface); 216 217 if (!resource_surface_add_resources(surface->rsurf, attachment_mask | 218 surface->attachment_mask)) 219 return FALSE; 220 221 if (textures) 222 resource_surface_get_resources(surface->rsurf, textures, attachment_mask); 223 224 if (seq_num) 225 *seq_num = surface->sequence_number; 226 227 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height); 228 229 if (surface->type == WL_PIXMAP_SURFACE) 230 wayland_pixmap_surface_initialize(surface); 231 232 return TRUE; 233} 234 235static void 236wayland_frame_callback(struct wl_surface *surf, void *data, uint32_t time) 237{ 238 struct wayland_surface *surface = data; 239 240 surface->block_swap_buffers = FALSE; 241} 242 243static INLINE void 244wayland_buffers_swap(struct wl_buffer **buffer, 245 enum wayland_buffer_type buf1, 246 enum wayland_buffer_type buf2) 247{ 248 struct wl_buffer *tmp = buffer[buf1]; 249 buffer[buf1] = buffer[buf2]; 250 buffer[buf2] = tmp; 251} 252 253static boolean 254wayland_surface_swap_buffers(struct native_surface *nsurf) 255{ 256 struct wayland_surface *surface = wayland_surface(nsurf); 257 struct wayland_display *display = surface->display; 258 259 while (surface->block_swap_buffers) 260 wl_display_iterate(display->dpy, WL_DISPLAY_READABLE); 261 262 surface->block_swap_buffers = TRUE; 263 wl_display_frame_callback(display->dpy, surface->win->surface, 264 wayland_frame_callback, surface); 265 266 if (surface->type == WL_WINDOW_SURFACE) { 267 resource_surface_swap_buffers(surface->rsurf, 268 NATIVE_ATTACHMENT_FRONT_LEFT, 269 NATIVE_ATTACHMENT_BACK_LEFT, FALSE); 270 271 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK); 272 273 if (surface->buffer[WL_BUFFER_FRONT] == NULL) 274 surface->buffer[WL_BUFFER_FRONT] = 275 display->create_buffer(display, surface, 276 NATIVE_ATTACHMENT_FRONT_LEFT); 277 278 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT], 279 surface->dx, surface->dy); 280 281 resource_surface_get_size(surface->rsurf, 282 (uint *) &surface->win->attached_width, 283 (uint *) &surface->win->attached_height); 284 surface->dx = 0; 285 surface->dy = 0; 286 } 287 288 surface->sequence_number++; 289 wayland_event_handler->invalid_surface(&display->base, 290 &surface->base, 291 surface->sequence_number); 292 293 return TRUE; 294} 295 296static boolean 297wayland_surface_present(struct native_surface *nsurf, 298 enum native_attachment natt, 299 boolean preserve, 300 uint swap_interval) 301{ 302 struct wayland_surface *surface = wayland_surface(nsurf); 303 uint width, height; 304 boolean ret; 305 306 if (preserve || swap_interval) 307 return FALSE; 308 309 switch (natt) { 310 case NATIVE_ATTACHMENT_FRONT_LEFT: 311 ret = TRUE; 312 break; 313 case NATIVE_ATTACHMENT_BACK_LEFT: 314 ret = wayland_surface_swap_buffers(nsurf); 315 break; 316 default: 317 ret = FALSE; 318 break; 319 } 320 321 if (surface->type == WL_WINDOW_SURFACE) { 322 resource_surface_get_size(surface->rsurf, &width, &height); 323 wl_buffer_damage(surface->buffer[WL_BUFFER_FRONT], 0, 0, width, height); 324 wl_surface_damage(surface->win->surface, 0, 0, width, height); 325 } 326 327 return ret; 328} 329 330static void 331wayland_surface_wait(struct native_surface *nsurf) 332{ 333 /* no-op */ 334} 335 336static void 337wayland_surface_destroy(struct native_surface *nsurf) 338{ 339 struct wayland_surface *surface = wayland_surface(nsurf); 340 enum wayland_buffer_type buffer; 341 342 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) { 343 if (surface->buffer[buffer]) 344 wl_buffer_destroy(surface->buffer[buffer]); 345 } 346 347 resource_surface_destroy(surface->rsurf); 348 FREE(surface); 349} 350 351 352 353static struct native_surface * 354wayland_create_pixmap_surface(struct native_display *ndpy, 355 EGLNativePixmapType pix, 356 const struct native_config *nconf) 357{ 358 struct wayland_display *display = wayland_display(ndpy); 359 struct wayland_surface *surface; 360 struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix; 361 enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT; 362 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW | 363 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT; 364 365 surface = CALLOC_STRUCT(wayland_surface); 366 if (!surface) 367 return NULL; 368 369 surface->display = display; 370 371 surface->pending_resource = NULL; 372 surface->type = WL_PIXMAP_SURFACE; 373 surface->pix = egl_pixmap; 374 375 if (nconf) 376 surface->color_format = nconf->color_format; 377 else /* FIXME: derive format from wl_visual */ 378 surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM; 379 380 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT); 381 382 surface->rsurf = resource_surface_create(display->base.screen, 383 surface->color_format, bind); 384 385 if (!surface->rsurf) { 386 FREE(surface); 387 return NULL; 388 } 389 390 resource_surface_set_size(surface->rsurf, 391 egl_pixmap->width, egl_pixmap->height); 392 393 /* the pixmap is already allocated, so import it */ 394 if (surface->pix->buffer != NULL) 395 resource_surface_import_resource(surface->rsurf, natt, 396 surface->pix->driver_private); 397 398 surface->base.destroy = wayland_surface_destroy; 399 surface->base.present = wayland_surface_present; 400 surface->base.validate = wayland_surface_validate; 401 surface->base.wait = wayland_surface_wait; 402 403 return &surface->base; 404} 405 406 407static struct native_surface * 408wayland_create_window_surface(struct native_display *ndpy, 409 EGLNativeWindowType win, 410 const struct native_config *nconf) 411{ 412 struct wayland_display *display = wayland_display(ndpy); 413 struct wayland_config *config = wayland_config(nconf); 414 struct wayland_surface *surface; 415 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW | 416 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT; 417 418 surface = CALLOC_STRUCT(wayland_surface); 419 if (!surface) 420 return NULL; 421 422 surface->display = display; 423 surface->color_format = config->base.color_format; 424 425 surface->win = (struct wl_egl_window *) win; 426 427 surface->pending_resource = NULL; 428 surface->block_swap_buffers = FALSE; 429 surface->type = WL_WINDOW_SURFACE; 430 431 surface->buffer[WL_BUFFER_FRONT] = NULL; 432 surface->buffer[WL_BUFFER_BACK] = NULL; 433 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 434 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 435 436 surface->rsurf = resource_surface_create(display->base.screen, 437 surface->color_format, bind); 438 439 if (!surface->rsurf) { 440 FREE(surface); 441 return NULL; 442 } 443 444 surface->base.destroy = wayland_surface_destroy; 445 surface->base.present = wayland_surface_present; 446 surface->base.validate = wayland_surface_validate; 447 surface->base.wait = wayland_surface_wait; 448 449 return &surface->base; 450} 451 452static void 453native_set_event_handler(struct native_event_handler *event_handler) 454{ 455 wayland_event_handler = event_handler; 456} 457 458static struct native_display * 459native_create_display(void *dpy, boolean use_sw, void *user_data) 460{ 461 struct wayland_display *display = NULL; 462 463 use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE); 464 465 if (use_sw) { 466 _eglLog(_EGL_INFO, "use software fallback"); 467 display = wayland_create_shm_display((struct wl_display *) dpy, 468 wayland_event_handler, 469 user_data); 470 } else { 471 display = wayland_create_drm_display((struct wl_display *) dpy, 472 wayland_event_handler, 473 user_data); 474 } 475 476 if (!display) 477 return NULL; 478 479 display->base.get_param = wayland_display_get_param; 480 display->base.get_configs = wayland_display_get_configs; 481 display->base.is_pixmap_supported = wayland_display_is_pixmap_supported; 482 display->base.create_window_surface = wayland_create_window_surface; 483 display->base.create_pixmap_surface = wayland_create_pixmap_surface; 484 485 return &display->base; 486} 487 488static const struct native_platform wayland_platform = { 489 "wayland", /* name */ 490 native_set_event_handler, 491 native_create_display 492}; 493 494const struct native_platform * 495native_get_wayland_platform(void) 496{ 497 return &wayland_platform; 498} 499 500/* vim: set sw=3 ts=8 sts=3 expandtab: */ 501