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