2 * Mesa 3-D graphics library
5 * Copyright (C) 2009-2010 Chia-I Wu <olv@0xlab.org>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
28 #include <sys/types.h>
31 #include <X11/Xutil.h>
32 #include "util/u_memory.h"
33 #include "util/u_math.h"
34 #include "util/u_format.h"
35 #include "pipe/p_compiler.h"
36 #include "util/u_inlines.h"
37 #include "state_tracker/xlib_sw_winsys.h"
38 #include "target-helpers/wrap_screen.h"
39 #include "util/u_debug.h"
40 #include "softpipe/sp_public.h"
41 #include "llvmpipe/lp_public.h"
42 #include "cell/ppu/cell_public.h"
45 #include "native_x11.h"
46 #include "x11_screen.h"
48 enum ximage_surface_type {
49 XIMAGE_SURFACE_TYPE_WINDOW,
50 XIMAGE_SURFACE_TYPE_PIXMAP,
53 struct ximage_display {
54 struct native_display base;
58 struct native_event_handler *event_handler;
60 struct x11_screen *xscr;
63 struct ximage_config *configs;
67 struct ximage_buffer {
68 struct pipe_texture *texture;
69 struct xlib_drawable xdraw;
72 struct ximage_surface {
73 struct native_surface base;
75 enum ximage_surface_type type;
76 enum pipe_format color_format;
78 struct ximage_display *xdpy;
80 unsigned int server_stamp;
81 unsigned int client_stamp;
83 struct ximage_buffer buffers[NUM_NATIVE_ATTACHMENTS];
86 struct pipe_surface *draw_surface;
89 struct ximage_config {
90 struct native_config base;
91 const XVisualInfo *visual;
94 static INLINE struct ximage_display *
95 ximage_display(const struct native_display *ndpy)
97 return (struct ximage_display *) ndpy;
100 static INLINE struct ximage_surface *
101 ximage_surface(const struct native_surface *nsurf)
103 return (struct ximage_surface *) nsurf;
106 static INLINE struct ximage_config *
107 ximage_config(const struct native_config *nconf)
109 return (struct ximage_config *) nconf;
113 ximage_surface_free_buffer(struct native_surface *nsurf,
114 enum native_attachment which)
116 struct ximage_surface *xsurf = ximage_surface(nsurf);
117 struct ximage_buffer *xbuf = &xsurf->buffers[which];
119 pipe_texture_reference(&xbuf->texture, NULL);
123 ximage_surface_alloc_buffer(struct native_surface *nsurf,
124 enum native_attachment which)
126 struct ximage_surface *xsurf = ximage_surface(nsurf);
127 struct ximage_buffer *xbuf = &xsurf->buffers[which];
128 struct pipe_screen *screen = xsurf->xdpy->base.screen;
129 struct pipe_texture templ;
133 ximage_surface_free_buffer(&xsurf->base, which);
135 memset(&templ, 0, sizeof(templ));
136 templ.target = PIPE_TEXTURE_2D;
137 templ.format = xsurf->color_format;
138 templ.width0 = xsurf->width;
139 templ.height0 = xsurf->height;
141 templ.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
144 case NATIVE_ATTACHMENT_FRONT_LEFT:
145 case NATIVE_ATTACHMENT_FRONT_RIGHT:
146 templ.tex_usage |= PIPE_TEXTURE_USAGE_SCANOUT;
148 case NATIVE_ATTACHMENT_BACK_LEFT:
149 case NATIVE_ATTACHMENT_BACK_RIGHT:
150 templ.tex_usage |= PIPE_TEXTURE_USAGE_DISPLAY_TARGET;
156 xbuf->texture = screen->texture_create(screen, &templ);
158 xbuf->xdraw.visual = xsurf->visual.visual;
159 xbuf->xdraw.depth = xsurf->visual.depth;
160 xbuf->xdraw.drawable = xsurf->drawable;
163 /* clean up the buffer if allocation failed */
165 ximage_surface_free_buffer(&xsurf->base, which);
167 return (xbuf->texture != NULL);
171 * Update the geometry of the surface. Return TRUE if the geometry has changed
175 ximage_surface_update_geometry(struct native_surface *nsurf)
177 struct ximage_surface *xsurf = ximage_surface(nsurf);
181 unsigned int w, h, border, depth;
182 boolean updated = FALSE;
184 ok = XGetGeometry(xsurf->xdpy->dpy, xsurf->drawable,
185 &root, &x, &y, &w, &h, &border, &depth);
186 if (ok && (xsurf->width != w || xsurf->height != h)) {
190 xsurf->server_stamp++;
198 ximage_surface_notify_invalid(struct native_surface *nsurf)
200 struct ximage_surface *xsurf = ximage_surface(nsurf);
201 struct ximage_display *xdpy = xsurf->xdpy;
203 xdpy->event_handler->invalid_surface(&xdpy->base,
204 &xsurf->base, xsurf->server_stamp);
208 * Update the buffers of the surface. It is a slow function due to the
209 * round-trip to the server.
212 ximage_surface_update_buffers(struct native_surface *nsurf, uint buffer_mask)
214 struct ximage_surface *xsurf = ximage_surface(nsurf);
219 updated = ximage_surface_update_geometry(&xsurf->base);
221 /* all buffers become invalid */
222 xsurf->valid_mask = 0x0;
225 buffer_mask &= ~xsurf->valid_mask;
226 /* all requested buffers are valid */
228 xsurf->client_stamp = xsurf->server_stamp;
234 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
235 if (native_attachment_mask_test(buffer_mask, att)) {
236 /* reallocate the texture */
237 if (!ximage_surface_alloc_buffer(&xsurf->base, att))
240 new_valid |= (1 << att);
241 if (buffer_mask == new_valid)
246 xsurf->valid_mask |= new_valid;
247 xsurf->client_stamp = xsurf->server_stamp;
249 return (new_valid == buffer_mask);
253 ximage_surface_draw_buffer(struct native_surface *nsurf,
254 enum native_attachment which)
256 struct ximage_surface *xsurf = ximage_surface(nsurf);
257 struct ximage_buffer *xbuf = &xsurf->buffers[which];
258 struct pipe_screen *screen = xsurf->xdpy->base.screen;
259 struct pipe_surface *psurf;
261 assert(xsurf->drawable && xbuf->texture);
263 psurf = xsurf->draw_surface;
264 if (!psurf || psurf->texture != xbuf->texture) {
265 pipe_surface_reference(&xsurf->draw_surface, NULL);
267 psurf = screen->get_tex_surface(screen,
268 xbuf->texture, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ);
272 xsurf->draw_surface = psurf;
275 screen->flush_frontbuffer(screen, psurf, &xbuf->xdraw);
281 ximage_surface_flush_frontbuffer(struct native_surface *nsurf)
283 struct ximage_surface *xsurf = ximage_surface(nsurf);
286 ret = ximage_surface_draw_buffer(&xsurf->base,
287 NATIVE_ATTACHMENT_FRONT_LEFT);
288 /* force buffers to be updated in next validation call */
289 xsurf->server_stamp++;
290 ximage_surface_notify_invalid(&xsurf->base);
296 ximage_surface_swap_buffers(struct native_surface *nsurf)
298 struct ximage_surface *xsurf = ximage_surface(nsurf);
299 struct ximage_buffer *xfront, *xback, xtmp;
302 /* display the back buffer first */
303 ret = ximage_surface_draw_buffer(&xsurf->base,
304 NATIVE_ATTACHMENT_BACK_LEFT);
305 /* force buffers to be updated in next validation call */
306 xsurf->server_stamp++;
307 ximage_surface_notify_invalid(&xsurf->base);
309 xfront = &xsurf->buffers[NATIVE_ATTACHMENT_FRONT_LEFT];
310 xback = &xsurf->buffers[NATIVE_ATTACHMENT_BACK_LEFT];
312 /* skip swapping unless there is a front buffer */
313 if (xfront->texture) {
323 ximage_surface_validate(struct native_surface *nsurf, uint attachment_mask,
324 unsigned int *seq_num, struct pipe_texture **textures,
325 int *width, int *height)
327 struct ximage_surface *xsurf = ximage_surface(nsurf);
329 if (xsurf->client_stamp != xsurf->server_stamp ||
330 (xsurf->valid_mask & attachment_mask) != attachment_mask) {
331 if (!ximage_surface_update_buffers(&xsurf->base, attachment_mask))
336 *seq_num = xsurf->client_stamp;
340 for (att = 0; att < NUM_NATIVE_ATTACHMENTS; att++) {
341 if (native_attachment_mask_test(attachment_mask, att)) {
342 struct ximage_buffer *xbuf = &xsurf->buffers[att];
344 textures[att] = NULL;
345 pipe_texture_reference(&textures[att], xbuf->texture);
351 *width = xsurf->width;
353 *height = xsurf->height;
359 ximage_surface_wait(struct native_surface *nsurf)
361 struct ximage_surface *xsurf = ximage_surface(nsurf);
362 XSync(xsurf->xdpy->dpy, FALSE);
363 /* TODO XGetImage and update the front texture */
367 ximage_surface_destroy(struct native_surface *nsurf)
369 struct ximage_surface *xsurf = ximage_surface(nsurf);
372 pipe_surface_reference(&xsurf->draw_surface, NULL);
374 for (i = 0; i < NUM_NATIVE_ATTACHMENTS; i++)
375 ximage_surface_free_buffer(&xsurf->base, i);
380 static struct ximage_surface *
381 ximage_display_create_surface(struct native_display *ndpy,
382 enum ximage_surface_type type,
384 const struct native_config *nconf)
386 struct ximage_display *xdpy = ximage_display(ndpy);
387 struct ximage_config *xconf = ximage_config(nconf);
388 struct ximage_surface *xsurf;
390 xsurf = CALLOC_STRUCT(ximage_surface);
396 xsurf->color_format = xconf->base.color_format;
397 xsurf->drawable = drawable;
399 xsurf->drawable = drawable;
400 xsurf->visual = *xconf->visual;
401 /* initialize the geometry */
402 ximage_surface_update_buffers(&xsurf->base, 0x0);
404 xsurf->base.destroy = ximage_surface_destroy;
405 xsurf->base.swap_buffers = ximage_surface_swap_buffers;
406 xsurf->base.flush_frontbuffer = ximage_surface_flush_frontbuffer;
407 xsurf->base.validate = ximage_surface_validate;
408 xsurf->base.wait = ximage_surface_wait;
413 static struct native_surface *
414 ximage_display_create_window_surface(struct native_display *ndpy,
415 EGLNativeWindowType win,
416 const struct native_config *nconf)
418 struct ximage_surface *xsurf;
420 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_WINDOW,
421 (Drawable) win, nconf);
422 return (xsurf) ? &xsurf->base : NULL;
425 static struct native_surface *
426 ximage_display_create_pixmap_surface(struct native_display *ndpy,
427 EGLNativePixmapType pix,
428 const struct native_config *nconf)
430 struct ximage_surface *xsurf;
432 xsurf = ximage_display_create_surface(ndpy, XIMAGE_SURFACE_TYPE_PIXMAP,
433 (Drawable) pix, nconf);
434 return (xsurf) ? &xsurf->base : NULL;
437 static enum pipe_format
438 choose_format(const XVisualInfo *vinfo)
440 enum pipe_format fmt;
441 /* TODO elaborate the formats */
442 switch (vinfo->depth) {
444 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
447 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
450 fmt = PIPE_FORMAT_B5G6R5_UNORM;
453 fmt = PIPE_FORMAT_NONE;
460 static const struct native_config **
461 ximage_display_get_configs(struct native_display *ndpy, int *num_configs)
463 struct ximage_display *xdpy = ximage_display(ndpy);
464 const struct native_config **configs;
468 if (!xdpy->configs) {
469 const XVisualInfo *visuals;
470 int num_visuals, count, j;
472 visuals = x11_screen_get_visuals(xdpy->xscr, &num_visuals);
477 * Create two configs for each visual.
478 * One with depth/stencil buffer; one without
480 xdpy->configs = calloc(num_visuals * 2, sizeof(*xdpy->configs));
485 for (i = 0; i < num_visuals; i++) {
486 for (j = 0; j < 2; j++) {
487 struct ximage_config *xconf = &xdpy->configs[count];
488 __GLcontextModes *mode = &xconf->base.mode;
490 xconf->visual = &visuals[i];
491 xconf->base.color_format = choose_format(xconf->visual);
492 if (xconf->base.color_format == PIPE_FORMAT_NONE)
495 x11_screen_convert_visual(xdpy->xscr, xconf->visual, mode);
496 /* support double buffer mode */
497 mode->doubleBufferMode = TRUE;
499 xconf->base.depth_format = PIPE_FORMAT_NONE;
500 xconf->base.stencil_format = PIPE_FORMAT_NONE;
501 /* create the second config with depth/stencil buffer */
503 xconf->base.depth_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
504 xconf->base.stencil_format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
505 mode->depthBits = 24;
506 mode->stencilBits = 8;
507 mode->haveDepthBuffer = TRUE;
508 mode->haveStencilBuffer = TRUE;
511 mode->maxPbufferWidth = 4096;
512 mode->maxPbufferHeight = 4096;
513 mode->maxPbufferPixels = 4096 * 4096;
515 GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
516 mode->swapMethod = GLX_SWAP_EXCHANGE_OML;
519 mode->bindToTextureRgba = TRUE;
521 mode->bindToTextureRgb = TRUE;
527 xdpy->num_configs = count;
530 configs = malloc(xdpy->num_configs * sizeof(*configs));
532 for (i = 0; i < xdpy->num_configs; i++)
533 configs[i] = (const struct native_config *) &xdpy->configs[i];
535 *num_configs = xdpy->num_configs;
541 ximage_display_is_pixmap_supported(struct native_display *ndpy,
542 EGLNativePixmapType pix,
543 const struct native_config *nconf)
545 struct ximage_display *xdpy = ximage_display(ndpy);
546 enum pipe_format fmt;
549 depth = x11_drawable_get_depth(xdpy->xscr, (Drawable) pix);
552 fmt = PIPE_FORMAT_B8G8R8A8_UNORM;
555 fmt = PIPE_FORMAT_B8G8R8X8_UNORM;
558 fmt = PIPE_FORMAT_B5G6R5_UNORM;
561 fmt = PIPE_FORMAT_NONE;
565 return (fmt == nconf->color_format);
569 ximage_display_get_param(struct native_display *ndpy,
570 enum native_param_type param)
575 case NATIVE_PARAM_USE_NATIVE_BUFFER:
576 /* private buffers are allocated */
588 ximage_display_destroy(struct native_display *ndpy)
590 struct ximage_display *xdpy = ximage_display(ndpy);
595 xdpy->base.screen->destroy(xdpy->base.screen);
597 x11_screen_destroy(xdpy->xscr);
599 XCloseDisplay(xdpy->dpy);
604 /* Helper function to build a subset of a driver stack consisting of
605 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
608 * This function could be shared, but currently causes headaches for
609 * the build systems, particularly scons if we try.
611 * Long term, want to avoid having global #defines for things like
612 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
613 * those #defines, so things that are painful for it now are likely to
614 * be painful for other build systems in the future.
616 static struct pipe_screen *
617 swrast_xlib_create_screen( Display *display )
619 struct sw_winsys *winsys;
620 struct pipe_screen *screen = NULL;
622 /* Create the underlying winsys, which performs presents to Xlib
625 winsys = xlib_create_sw_winsys( display );
629 /* Create a software rasterizer on top of that winsys. Use
630 * llvmpipe if it is available.
632 #if defined(GALLIUM_LLVMPIPE)
633 if (screen == NULL &&
634 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE))
635 screen = llvmpipe_create_screen( winsys );
639 screen = softpipe_create_screen( winsys );
644 /* Inject any wrapping layers we want to here:
646 return gallium_wrap_screen( screen );
650 winsys->destroy( winsys );
657 struct native_display *
658 x11_create_ximage_display(EGLNativeDisplayType dpy,
659 struct native_event_handler *event_handler)
661 struct ximage_display *xdpy;
663 xdpy = CALLOC_STRUCT(ximage_display);
669 xdpy->dpy = XOpenDisplay(NULL);
674 xdpy->own_dpy = TRUE;
677 xdpy->event_handler = event_handler;
679 xdpy->xscr_number = DefaultScreen(xdpy->dpy);
680 xdpy->xscr = x11_screen_create(xdpy->dpy, xdpy->xscr_number);
686 xdpy->base.screen = swrast_xlib_create_screen(xdpy->dpy);
688 xdpy->base.destroy = ximage_display_destroy;
689 xdpy->base.get_param = ximage_display_get_param;
691 xdpy->base.get_configs = ximage_display_get_configs;
692 xdpy->base.is_pixmap_supported = ximage_display_is_pixmap_supported;
693 xdpy->base.create_window_surface = ximage_display_create_window_surface;
694 xdpy->base.create_pixmap_surface = ximage_display_create_pixmap_surface;