native_ximage.c revision a27690931be9479353dd8ae0d52186f5c72f4380
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.8 4 * 5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org> 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 <X11/Xlib.h> 27#include <X11/Xutil.h> 28#include "util/u_memory.h" 29#include "util/u_math.h" 30#include "util/u_format.h" 31#include "pipe/p_compiler.h" 32#include "util/u_inlines.h" 33#include "state_tracker/xlib_sw_winsys.h" 34#include "target-helpers/wrap_screen.h" 35#include "util/u_debug.h" 36#include "softpipe/sp_public.h" 37#include "llvmpipe/lp_public.h" 38#include "egllog.h" 39 40#include "common/native_helper.h" 41#include "native_x11.h" 42#include "x11_screen.h" 43 44enum ximage_surface_type { 45 XIMAGE_SURFACE_TYPE_WINDOW, 46 XIMAGE_SURFACE_TYPE_PIXMAP, 47}; 48 49struct ximage_display { 50 struct native_display base; 51 Display *dpy; 52 boolean own_dpy; 53 54 struct native_event_handler *event_handler; 55 56 struct x11_screen *xscr; 57 int xscr_number; 58 59 struct ximage_config *configs; 60 int num_configs; 61}; 62 63struct ximage_surface { 64 struct native_surface base; 65 Drawable drawable; 66 enum ximage_surface_type type; 67 enum pipe_format color_format; 68 XVisualInfo visual; 69 struct ximage_display *xdpy; 70 71 unsigned int server_stamp; 72 unsigned int client_stamp; 73 74 struct resource_surface *rsurf; 75 struct xlib_drawable xdraw; 76}; 77 78struct ximage_config { 79 struct native_config base; 80 const XVisualInfo *visual; 81}; 82 83static INLINE struct ximage_display * 84ximage_display(const struct native_display *ndpy) 85{ 86 return (struct ximage_display *) ndpy; 87} 88 89static INLINE struct ximage_surface * 90ximage_surface(const struct native_surface *nsurf) 91{ 92 return (struct ximage_surface *) nsurf; 93} 94 95static INLINE struct ximage_config * 96ximage_config(const struct native_config *nconf) 97{ 98 return (struct ximage_config *) nconf; 99} 100 101/** 102 * Update the geometry of the surface. This is a slow functions. 103 */ 104static void 105ximage_surface_update_geometry(struct native_surface *nsurf) 106{ 107 struct ximage_surface *xsurf = ximage_surface(nsurf); 108 Status ok; 109 Window root; 110 int x, y; 111 unsigned int w, h, border, depth; 112 113 ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable, 114 &root, &x, &y, &w, &h, &border, &depth); 115 if (ok && resource_surface_set_size(xsurf->rsurf, w, h)) 116 xsurf->server_stamp++; 117} 118 119/** 120 * Update the buffers of the surface. 121 */ 122static boolean 123ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask) 124{ 125 struct ximage_surface *xsurf = ximage_surface(nsurf); 126 127 if (xsurf->client_stamp != xsurf->server_stamp) { 128 ximage_surface_update_geometry(&xsurf->base); 129 xsurf->client_stamp = xsurf->server_stamp; 130 } 131 132 return resource_surface_add_resources(xsurf->rsurf, buffer_mask); 133} 134 135/** 136 * Emulate an invalidate event. 137 */ 138static void 139ximage_surface_invalidate(struct native_surface *nsurf) 140{ 141 struct ximage_surface *xsurf = ximage_surface(nsurf); 142 struct ximage_display *xdpy = xsurf->xdpy; 143 144 xsurf->server_stamp++; 145 xdpy->event_handler->invalid_surface(&xdpy->base, 146 &xsurf->base, xsurf->server_stamp); 147} 148 149static boolean 150ximage_surface_flush_frontbuffer(struct native_surface *nsurf) 151{ 152 struct ximage_surface *xsurf = ximage_surface(nsurf); 153 boolean ret; 154 155 ret = resource_surface_present(xsurf->rsurf, 156 NATIVE_ATTACHMENT_FRONT_LEFT, (void *) &xsurf->xdraw); 157 /* force buffers to be updated in next validation call */ 158 ximage_surface_invalidate(&xsurf->base); 159 160 return ret; 161} 162 163static boolean 164ximage_surface_swap_buffers(struct native_surface *nsurf) 165{ 166 struct ximage_surface *xsurf = ximage_surface(nsurf); 167 boolean ret; 168 169 ret = resource_surface_present(xsurf->rsurf, 170 NATIVE_ATTACHMENT_BACK_LEFT, (void *) &xsurf->xdraw); 171 172 resource_surface_swap_buffers(xsurf->rsurf, 173 NATIVE_ATTACHMENT_FRONT_LEFT, NATIVE_ATTACHMENT_BACK_LEFT, TRUE); 174 /* the front/back buffers have been swapped */ 175 ximage_surface_invalidate(&xsurf->base); 176 177 return ret; 178} 179 180static boolean 181ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask, 182 unsigned int *seq_num, struct pipe_resource **textures, 183 int *width, int *height) 184{ 185 struct ximage_surface *xsurf = ximage_surface(nsurf); 186 uint w, h; 187 188 if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask)) 189 return FALSE; 190 191 if (seq_num) 192 *seq_num = xsurf->client_stamp; 193 194 if (textures) 195 resource_surface_get_resources(xsurf->rsurf, textures, attachment_mask); 196 197 resource_surface_get_size(xsurf->rsurf, &w, &h); 198 if (width) 199 *width = w; 200 if (height) 201 *height = h; 202 203 return TRUE; 204} 205 206static void 207ximage_surface_wait(struct native_surface *nsurf) 208{ 209 struct ximage_surface *xsurf = ximage_surface(nsurf); 210 XSync(xsurf->xdpy->dpy, FALSE); 211 /* TODO XGetImage and update the front texture */ 212} 213 214static void 215ximage_surface_destroy(struct native_surface *nsurf) 216{ 217 struct ximage_surface *xsurf = ximage_surface(nsurf); 218 219 resource_surface_destroy(xsurf->rsurf); 220 FREE(xsurf); 221} 222 223static struct ximage_surface * 224ximage_display_create_surface(struct native_display *ndpy, 225 enum ximage_surface_type type, 226 Drawable drawable, 227 const struct native_config *nconf) 228{ 229 struct ximage_display *xdpy = ximage_display(ndpy); 230 struct ximage_config *xconf = ximage_config(nconf); 231 struct ximage_surface *xsurf; 232 233 xsurf = CALLOC_STRUCT(ximage_surface); 234 if (!xsurf) 235 return NULL; 236 237 xsurf->xdpy = xdpy; 238 xsurf->type = type; 239 xsurf->color_format = xconf->base.color_format; 240 xsurf->drawable = drawable; 241 242 xsurf->rsurf = resource_surface_create(xdpy->base.screen, 243 xsurf->color_format, 244 PIPE_BIND_RENDER_TARGET | 245 PIPE_BIND_SAMPLER_VIEW | 246 PIPE_BIND_DISPLAY_TARGET | 247 PIPE_BIND_SCANOUT); 248 if (!xsurf->rsurf) { 249 FREE(xsurf); 250 return NULL; 251 } 252 253 xsurf->drawable = drawable; 254 xsurf->visual = *xconf->visual; 255 /* initialize the geometry */ 256 ximage_surface_update_geometry(&xsurf->base); 257 258 xsurf->xdraw.visual = xsurf->visual.visual; 259 xsurf->xdraw.depth = xsurf->visual.depth; 260 xsurf->xdraw.drawable = xsurf->drawable; 261 262 xsurf->base.destroy = ximage_surface_destroy; 263 xsurf->base.swap_buffers = ximage_surface_swap_buffers; 264 xsurf->base.flush_frontbuffer = ximage_surface_flush_frontbuffer; 265 xsurf->base.validate = ximage_surface_validate; 266 xsurf->base.wait = ximage_surface_wait; 267 268 return xsurf; 269} 270 271static struct native_surface * 272ximage_display_create_window_surface(struct native_display *ndpy, 273 EGLNativeWindowType win, 274 const struct native_config *nconf) 275{ 276 struct ximage_surface *xsurf; 277 278 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_WINDOW, 279 (Drawable) win, nconf); 280 return (xsurf) ? &xsurf->base : NULL; 281} 282 283static struct native_surface * 284ximage_display_create_pixmap_surface(struct native_display *ndpy, 285 EGLNativePixmapType pix, 286 const struct native_config *nconf) 287{ 288 struct ximage_surface *xsurf; 289 290 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_PIXMAP, 291 (Drawable) pix, nconf); 292 return (xsurf) ? &xsurf->base : NULL; 293} 294 295static enum pipe_format 296choose_format(const XVisualInfo *vinfo) 297{ 298 enum pipe_format fmt; 299 /* TODO elaborate the formats */ 300 switch (vinfo->depth) { 301 case 32: 302 fmt = PIPE_FORMAT_B8G8R8A8_UNORM; 303 break; 304 case 24: 305 fmt = PIPE_FORMAT_B8G8R8X8_UNORM; 306 break; 307 case 16: 308 fmt = PIPE_FORMAT_B5G6R5_UNORM; 309 break; 310 default: 311 fmt = PIPE_FORMAT_NONE; 312 break; 313 } 314 315 return fmt; 316} 317 318static const struct native_config ** 319ximage_display_get_configs(struct native_display *ndpy, int *num_configs) 320{ 321 struct ximage_display *xdpy = ximage_display(ndpy); 322 const struct native_config **configs; 323 int i; 324 325 /* first time */ 326 if (!xdpy->configs) { 327 const XVisualInfo *visuals; 328 int num_visuals, count; 329 330 visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals); 331 if (!visuals) 332 return NULL; 333 334 /* 335 * Create two configs for each visual. 336 * One with depth/stencil buffer; one without 337 */ 338 xdpy->configs = CALLOC(num_visuals * 2, sizeof(*xdpy->configs)); 339 if (!xdpy->configs) 340 return NULL; 341 342 count = 0; 343 for (i = 0; i < num_visuals; i++) { 344 struct ximage_config *xconf = &xdpy->configs[count]; 345 346 xconf->visual = &visuals[i]; 347 xconf->base.color_format = choose_format(xconf->visual); 348 if (xconf->base.color_format == PIPE_FORMAT_NONE) 349 continue; 350 351 xconf->base.buffer_mask = 352 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) | 353 (1 << NATIVE_ATTACHMENT_BACK_LEFT); 354 355 xconf->base.window_bit = TRUE; 356 xconf->base.pixmap_bit = TRUE; 357 358 xconf->base.native_visual_id = xconf->visual->visualid; 359#if defined(__cplusplus) || defined(c_plusplus) 360 xconf->base.native_visual_type = xconf->visual->c_class; 361#else 362 xconf->base.native_visual_type = xconf->visual->class; 363#endif 364 365 xconf->base.slow_config = TRUE; 366 367 count++; 368 } 369 370 xdpy->num_configs = count; 371 } 372 373 configs = MALLOC(xdpy->num_configs * sizeof(*configs)); 374 if (configs) { 375 for (i = 0; i < xdpy->num_configs; i++) 376 configs[i] = (const struct native_config *) &xdpy->configs[i]; 377 if (num_configs) 378 *num_configs = xdpy->num_configs; 379 } 380 return configs; 381} 382 383static boolean 384ximage_display_is_pixmap_supported(struct native_display *ndpy, 385 EGLNativePixmapType pix, 386 const struct native_config *nconf) 387{ 388 struct ximage_display *xdpy = ximage_display(ndpy); 389 enum pipe_format fmt; 390 uint depth; 391 392 depth = x11_drawable_get_depth(xdpy->xscr, (Drawable) pix); 393 switch (depth) { 394 case 32: 395 fmt = PIPE_FORMAT_B8G8R8A8_UNORM; 396 break; 397 case 24: 398 fmt = PIPE_FORMAT_B8G8R8X8_UNORM; 399 break; 400 case 16: 401 fmt = PIPE_FORMAT_B5G6R5_UNORM; 402 break; 403 default: 404 fmt = PIPE_FORMAT_NONE; 405 break; 406 } 407 408 return (fmt == nconf->color_format); 409} 410 411static int 412ximage_display_get_param(struct native_display *ndpy, 413 enum native_param_type param) 414{ 415 int val; 416 417 switch (param) { 418 case NATIVE_PARAM_USE_NATIVE_BUFFER: 419 /* private buffers are allocated */ 420 val = FALSE; 421 break; 422 default: 423 val = 0; 424 break; 425 } 426 427 return val; 428} 429 430static void 431ximage_display_destroy(struct native_display *ndpy) 432{ 433 struct ximage_display *xdpy = ximage_display(ndpy); 434 435 if (xdpy->configs) 436 FREE(xdpy->configs); 437 438 xdpy->base.screen->destroy(xdpy->base.screen); 439 440 x11_screen_destroy(xdpy->xscr); 441 if (xdpy->own_dpy) 442 XCloseDisplay(xdpy->dpy); 443 FREE(xdpy); 444} 445 446 447/* Helper function to build a subset of a driver stack consisting of 448 * one of the software rasterizers (cell, llvmpipe, softpipe) and the 449 * xlib winsys. 450 * 451 * This function could be shared, but currently causes headaches for 452 * the build systems, particularly scons if we try. 453 * 454 * Long term, want to avoid having global #defines for things like 455 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates 456 * those #defines, so things that are painful for it now are likely to 457 * be painful for other build systems in the future. 458 */ 459static struct pipe_screen * 460swrast_xlib_create_screen( Display *display ) 461{ 462 struct sw_winsys *winsys; 463 struct pipe_screen *screen = NULL; 464 465 /* Create the underlying winsys, which performs presents to Xlib 466 * drawables: 467 */ 468 winsys = xlib_create_sw_winsys( display ); 469 if (winsys == NULL) 470 return NULL; 471 472 /* Create a software rasterizer on top of that winsys. Use 473 * llvmpipe if it is available. 474 */ 475#if defined(GALLIUM_LLVMPIPE) 476 if (screen == NULL && 477 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE)) 478 screen = llvmpipe_create_screen( winsys ); 479#endif 480 481 if (screen == NULL) 482 screen = softpipe_create_screen( winsys ); 483 484 if (screen == NULL) 485 goto fail; 486 487 /* Inject any wrapping layers we want to here: 488 */ 489 return gallium_wrap_screen( screen ); 490 491fail: 492 if (winsys) 493 winsys->destroy( winsys ); 494 495 return NULL; 496} 497 498 499 500struct native_display * 501x11_create_ximage_display(EGLNativeDisplayType dpy, 502 struct native_event_handler *event_handler) 503{ 504 struct ximage_display *xdpy; 505 506 xdpy = CALLOC_STRUCT(ximage_display); 507 if (!xdpy) 508 return NULL; 509 510 xdpy->dpy = dpy; 511 if (!xdpy->dpy) { 512 xdpy->dpy = XOpenDisplay(NULL); 513 if (!xdpy->dpy) { 514 FREE(xdpy); 515 return NULL; 516 } 517 xdpy->own_dpy = TRUE; 518 } 519 520 xdpy->event_handler = event_handler; 521 522 xdpy->xscr_number = DefaultScreen(xdpy->dpy); 523 xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number); 524 if (!xdpy->xscr) { 525 FREE(xdpy); 526 return NULL; 527 } 528 529 xdpy->base.screen = swrast_xlib_create_screen(xdpy->dpy); 530 531 xdpy->base.destroy = ximage_display_destroy; 532 xdpy->base.get_param = ximage_display_get_param; 533 534 xdpy->base.get_configs = ximage_display_get_configs; 535 xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported; 536 xdpy->base.create_window_surface = ximage_display_create_window_surface; 537 xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface; 538 539 return &xdpy->base; 540} 541