native_wayland.c revision 287278352ea26947f8d304458c10bf0388beebb5
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 const struct native_event_handler *wayland_event_handler; 39 40static const struct native_config ** 41wayland_display_get_configs (struct native_display *ndpy, int *num_configs) 42{ 43 struct wayland_display *display = wayland_display(ndpy); 44 const struct native_config **configs; 45 int i; 46 47 if (!display->config) { 48 struct native_config *nconf; 49 display->config = CALLOC(2, sizeof(*display->config)); 50 if (!display->config) 51 return NULL; 52 53 for (i = 0; i < 2; ++i) { 54 nconf = &display->config[i].base; 55 56 nconf->buffer_mask = 57 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 58 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 59 60 nconf->window_bit = TRUE; 61 nconf->pixmap_bit = TRUE; 62 } 63 64 display->config[0].base.color_format = PIPE_FORMAT_B8G8R8A8_UNORM; 65 display->config[1].base.color_format = PIPE_FORMAT_B8G8R8X8_UNORM; 66 } 67 68 configs = MALLOC(2 * sizeof(*configs)); 69 if (configs) { 70 configs[0] = &display->config[0].base; 71 configs[1] = &display->config[1].base; 72 if (num_configs) 73 *num_configs = 2; 74 } 75 76 return configs; 77} 78 79static int 80wayland_display_get_param(struct native_display *ndpy, 81 enum native_param_type param) 82{ 83 int val; 84 85 switch (param) { 86 case NATIVE_PARAM_USE_NATIVE_BUFFER: 87 case NATIVE_PARAM_PRESERVE_BUFFER: 88 case NATIVE_PARAM_MAX_SWAP_INTERVAL: 89 default: 90 val = 0; 91 break; 92 } 93 94 return val; 95} 96 97static boolean 98wayland_display_get_pixmap_format(struct native_display *ndpy, 99 EGLNativePixmapType pix, 100 enum pipe_format *format) 101{ 102 /* all wl_egl_pixmaps are supported */ 103 *format = PIPE_FORMAT_NONE; 104 105 return TRUE; 106} 107 108static void 109wayland_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap) 110{ 111 struct pipe_resource *resource = egl_pixmap->driver_private; 112 113 assert(resource); 114 115 pipe_resource_reference(&resource, NULL); 116 if (egl_pixmap->buffer) { 117 wl_buffer_destroy(egl_pixmap->buffer); 118 egl_pixmap->buffer = NULL; 119 } 120 121 egl_pixmap->driver_private = NULL; 122 egl_pixmap->destroy = NULL; 123} 124 125static void 126wayland_pixmap_surface_initialize(struct wayland_surface *surface) 127{ 128 struct wayland_display *display = wayland_display(&surface->display->base); 129 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT; 130 131 if (surface->pix->buffer != NULL) 132 return; 133 134 surface->pix->buffer = display->create_buffer(display, surface, front_natt); 135 surface->pix->destroy = wayland_pixmap_destroy; 136 surface->pix->driver_private = 137 resource_surface_get_single_resource(surface->rsurf, front_natt); 138} 139 140static void 141wayland_release_pending_resource(void *data, 142 struct wl_callback *callback, 143 uint32_t time) 144{ 145 struct wayland_surface *surface = data; 146 147 wl_callback_destroy(callback); 148 149 /* FIXME: print internal error */ 150 if (!surface->pending_resource) 151 return; 152 153 pipe_resource_reference(&surface->pending_resource, NULL); 154} 155 156static const struct wl_callback_listener release_buffer_listener = { 157 wayland_release_pending_resource 158}; 159 160static void 161wayland_window_surface_handle_resize(struct wayland_surface *surface) 162{ 163 struct wayland_display *display = surface->display; 164 struct pipe_resource *front_resource; 165 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT; 166 int i; 167 168 front_resource = resource_surface_get_single_resource(surface->rsurf, 169 front_natt); 170 if (resource_surface_set_size(surface->rsurf, 171 surface->win->width, surface->win->height)) { 172 173 if (surface->pending_resource) 174 wl_display_roundtrip(display->dpy); 175 176 if (front_resource) { 177 struct wl_callback *callback; 178 179 surface->pending_resource = front_resource; 180 front_resource = NULL; 181 182 callback = wl_display_sync(display->dpy); 183 wl_callback_add_listener(callback, &release_buffer_listener, surface); 184 } 185 186 for (i = 0; i < WL_BUFFER_COUNT; ++i) { 187 if (surface->buffer[i]) 188 wl_buffer_destroy(surface->buffer[i]); 189 surface->buffer[i] = NULL; 190 } 191 192 surface->dx = surface->win->dx; 193 surface->dy = surface->win->dy; 194 } 195 pipe_resource_reference(&front_resource, NULL); 196} 197 198static boolean 199wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask, 200 unsigned int *seq_num, struct pipe_resource **textures, 201 int *width, int *height) 202{ 203 struct wayland_surface *surface = wayland_surface(nsurf); 204 205 if (surface->type == WL_WINDOW_SURFACE) 206 wayland_window_surface_handle_resize(surface); 207 208 if (!resource_surface_add_resources(surface->rsurf, attachment_mask | 209 surface->attachment_mask)) 210 return FALSE; 211 212 if (textures) 213 resource_surface_get_resources(surface->rsurf, textures, attachment_mask); 214 215 if (seq_num) 216 *seq_num = surface->sequence_number; 217 218 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height); 219 220 if (surface->type == WL_PIXMAP_SURFACE) 221 wayland_pixmap_surface_initialize(surface); 222 223 return TRUE; 224} 225 226static void 227wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time) 228{ 229 struct wayland_surface *surface = data; 230 231 surface->block_swap_buffers = FALSE; 232 233 wl_callback_destroy(callback); 234} 235 236static const struct wl_callback_listener frame_listener = { 237 wayland_frame_callback 238}; 239 240static INLINE void 241wayland_buffers_swap(struct wl_buffer **buffer, 242 enum wayland_buffer_type buf1, 243 enum wayland_buffer_type buf2) 244{ 245 struct wl_buffer *tmp = buffer[buf1]; 246 buffer[buf1] = buffer[buf2]; 247 buffer[buf2] = tmp; 248} 249 250static boolean 251wayland_surface_swap_buffers(struct native_surface *nsurf) 252{ 253 struct wayland_surface *surface = wayland_surface(nsurf); 254 struct wayland_display *display = surface->display; 255 struct wl_callback *callback; 256 257 while (surface->block_swap_buffers) 258 wl_display_iterate(display->dpy, WL_DISPLAY_READABLE); 259 260 surface->block_swap_buffers = TRUE; 261 262 callback = wl_surface_frame(surface->win->surface); 263 wl_callback_add_listener(callback, &frame_listener, surface); 264 265 if (surface->type == WL_WINDOW_SURFACE) { 266 resource_surface_swap_buffers(surface->rsurf, 267 NATIVE_ATTACHMENT_FRONT_LEFT, 268 NATIVE_ATTACHMENT_BACK_LEFT, FALSE); 269 270 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK); 271 272 if (surface->buffer[WL_BUFFER_FRONT] == NULL) 273 surface->buffer[WL_BUFFER_FRONT] = 274 display->create_buffer(display, surface, 275 NATIVE_ATTACHMENT_FRONT_LEFT); 276 277 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT], 278 surface->dx, surface->dy); 279 280 resource_surface_get_size(surface->rsurf, 281 (uint *) &surface->win->attached_width, 282 (uint *) &surface->win->attached_height); 283 surface->dx = 0; 284 surface->dy = 0; 285 } 286 287 surface->sequence_number++; 288 wayland_event_handler->invalid_surface(&display->base, 289 &surface->base, 290 surface->sequence_number); 291 292 return TRUE; 293} 294 295static boolean 296wayland_surface_present(struct native_surface *nsurf, 297 enum native_attachment natt, 298 boolean preserve, 299 uint swap_interval) 300{ 301 struct wayland_surface *surface = wayland_surface(nsurf); 302 uint width, height; 303 boolean ret; 304 305 if (preserve || swap_interval) 306 return FALSE; 307 308 switch (natt) { 309 case NATIVE_ATTACHMENT_FRONT_LEFT: 310 ret = TRUE; 311 break; 312 case NATIVE_ATTACHMENT_BACK_LEFT: 313 ret = wayland_surface_swap_buffers(nsurf); 314 break; 315 default: 316 ret = FALSE; 317 break; 318 } 319 320 if (surface->type == WL_WINDOW_SURFACE) { 321 resource_surface_get_size(surface->rsurf, &width, &height); 322 wl_buffer_damage(surface->buffer[WL_BUFFER_FRONT], 0, 0, width, height); 323 wl_surface_damage(surface->win->surface, 0, 0, width, height); 324 } 325 326 return ret; 327} 328 329static void 330wayland_surface_wait(struct native_surface *nsurf) 331{ 332 /* no-op */ 333} 334 335static void 336wayland_surface_destroy(struct native_surface *nsurf) 337{ 338 struct wayland_surface *surface = wayland_surface(nsurf); 339 enum wayland_buffer_type buffer; 340 341 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) { 342 if (surface->buffer[buffer]) 343 wl_buffer_destroy(surface->buffer[buffer]); 344 } 345 346 resource_surface_destroy(surface->rsurf); 347 FREE(surface); 348} 349 350 351 352static struct native_surface * 353wayland_create_pixmap_surface(struct native_display *ndpy, 354 EGLNativePixmapType pix, 355 const struct native_config *nconf) 356{ 357 struct wayland_display *display = wayland_display(ndpy); 358 struct wayland_surface *surface; 359 struct wl_egl_pixmap *egl_pixmap = (struct wl_egl_pixmap *) pix; 360 enum native_attachment natt = NATIVE_ATTACHMENT_FRONT_LEFT; 361 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW | 362 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT; 363 364 surface = CALLOC_STRUCT(wayland_surface); 365 if (!surface) 366 return NULL; 367 368 surface->display = display; 369 370 surface->pending_resource = NULL; 371 surface->type = WL_PIXMAP_SURFACE; 372 surface->pix = egl_pixmap; 373 374 if (nconf) 375 surface->color_format = nconf->color_format; 376 else /* FIXME: derive format from wl_visual */ 377 surface->color_format = PIPE_FORMAT_B8G8R8A8_UNORM; 378 379 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT); 380 381 surface->rsurf = resource_surface_create(display->base.screen, 382 surface->color_format, bind); 383 384 if (!surface->rsurf) { 385 FREE(surface); 386 return NULL; 387 } 388 389 resource_surface_set_size(surface->rsurf, 390 egl_pixmap->width, egl_pixmap->height); 391 392 /* the pixmap is already allocated, so import it */ 393 if (surface->pix->buffer != NULL) 394 resource_surface_import_resource(surface->rsurf, natt, 395 surface->pix->driver_private); 396 397 surface->base.destroy = wayland_surface_destroy; 398 surface->base.present = wayland_surface_present; 399 surface->base.validate = wayland_surface_validate; 400 surface->base.wait = wayland_surface_wait; 401 402 return &surface->base; 403} 404 405 406static struct native_surface * 407wayland_create_window_surface(struct native_display *ndpy, 408 EGLNativeWindowType win, 409 const struct native_config *nconf) 410{ 411 struct wayland_display *display = wayland_display(ndpy); 412 struct wayland_config *config = wayland_config(nconf); 413 struct wayland_surface *surface; 414 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW | 415 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT; 416 417 surface = CALLOC_STRUCT(wayland_surface); 418 if (!surface) 419 return NULL; 420 421 surface->display = display; 422 surface->color_format = config->base.color_format; 423 424 surface->win = (struct wl_egl_window *) win; 425 426 surface->pending_resource = NULL; 427 surface->block_swap_buffers = FALSE; 428 surface->type = WL_WINDOW_SURFACE; 429 430 surface->buffer[WL_BUFFER_FRONT] = NULL; 431 surface->buffer[WL_BUFFER_BACK] = NULL; 432 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 433 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 434 435 surface->rsurf = resource_surface_create(display->base.screen, 436 surface->color_format, bind); 437 438 if (!surface->rsurf) { 439 FREE(surface); 440 return NULL; 441 } 442 443 surface->base.destroy = wayland_surface_destroy; 444 surface->base.present = wayland_surface_present; 445 surface->base.validate = wayland_surface_validate; 446 surface->base.wait = wayland_surface_wait; 447 448 return &surface->base; 449} 450 451static struct native_display * 452native_create_display(void *dpy, boolean use_sw) 453{ 454 struct wayland_display *display = NULL; 455 boolean own_dpy = FALSE; 456 457 use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE); 458 459 if (dpy == NULL) { 460 dpy = wl_display_connect(NULL); 461 if (dpy == NULL) 462 return NULL; 463 own_dpy = TRUE; 464 } 465 466 if (use_sw) { 467 _eglLog(_EGL_INFO, "use software fallback"); 468 display = wayland_create_shm_display((struct wl_display *) dpy, 469 wayland_event_handler); 470 } else { 471 display = wayland_create_drm_display((struct wl_display *) dpy, 472 wayland_event_handler); 473 } 474 475 if (!display) 476 return NULL; 477 478 display->base.get_param = wayland_display_get_param; 479 display->base.get_configs = wayland_display_get_configs; 480 display->base.get_pixmap_format = wayland_display_get_pixmap_format; 481 display->base.copy_to_pixmap = native_display_copy_to_pixmap; 482 display->base.create_window_surface = wayland_create_window_surface; 483 display->base.create_pixmap_surface = wayland_create_pixmap_surface; 484 485 display->own_dpy = own_dpy; 486 487 return &display->base; 488} 489 490static const struct native_platform wayland_platform = { 491 "wayland", /* name */ 492 native_create_display 493}; 494 495const struct native_platform * 496native_get_wayland_platform(const struct native_event_handler *event_handler) 497{ 498 wayland_event_handler = event_handler; 499 return &wayland_platform; 500} 501 502/* vim: set sw=3 ts=8 sts=3 expandtab: */ 503