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
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <sys/types.h>
30 #include <X11/Xutil.h>
31 #include "util/u_memory.h"
32 #include "util/u_math.h"
33 #include "util/u_format.h"
34 #include "pipe/p_compiler.h"
35 #include "util/u_inlines.h"
36 #include "state_tracker/xlib_sw_winsys.h"
37 #include "target-helpers/wrap_screen.h"
38 #include "util/u_debug.h"
39 #include "softpipe/sp_public.h"
40 #include "llvmpipe/lp_public.h"
41 #include "cell/ppu/cell_public.h"
44 #include "native_x11.h"
45 #include "x11_screen.h"
47 enum ximage_surface_type
{
48 XIMAGE_SURFACE_TYPE_WINDOW
,
49 XIMAGE_SURFACE_TYPE_PIXMAP
,
50 XIMAGE_SURFACE_TYPE_PBUFFER
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
];
87 struct ximage_config
{
88 struct native_config base
;
89 const XVisualInfo
*visual
;
92 static INLINE
struct ximage_display
*
93 ximage_display(const struct native_display
*ndpy
)
95 return (struct ximage_display
*) ndpy
;
98 static INLINE
struct ximage_surface
*
99 ximage_surface(const struct native_surface
*nsurf
)
101 return (struct ximage_surface
*) nsurf
;
104 static INLINE
struct ximage_config
*
105 ximage_config(const struct native_config
*nconf
)
107 return (struct ximage_config
*) nconf
;
111 ximage_surface_free_buffer(struct native_surface
*nsurf
,
112 enum native_attachment which
)
114 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
115 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
117 pipe_texture_reference(&xbuf
->texture
, NULL
);
121 ximage_surface_alloc_buffer(struct native_surface
*nsurf
,
122 enum native_attachment which
)
124 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
125 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
126 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
127 struct pipe_texture templ
;
131 ximage_surface_free_buffer(&xsurf
->base
, which
);
133 memset(&templ
, 0, sizeof(templ
));
134 templ
.target
= PIPE_TEXTURE_2D
;
135 templ
.format
= xsurf
->color_format
;
136 templ
.width0
= xsurf
->width
;
137 templ
.height0
= xsurf
->height
;
139 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
141 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
143 case NATIVE_ATTACHMENT_FRONT_LEFT
:
144 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
145 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_SCANOUT
;
147 case NATIVE_ATTACHMENT_BACK_LEFT
:
148 case NATIVE_ATTACHMENT_BACK_RIGHT
:
149 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
155 xbuf
->texture
= screen
->texture_create(screen
, &templ
);
157 xbuf
->xdraw
.visual
= xsurf
->visual
.visual
;
158 xbuf
->xdraw
.depth
= xsurf
->visual
.depth
;
159 xbuf
->xdraw
.drawable
= xsurf
->drawable
;
162 /* clean up the buffer if allocation failed */
164 ximage_surface_free_buffer(&xsurf
->base
, which
);
166 return (xbuf
->texture
!= NULL
);
170 * Update the geometry of the surface. Return TRUE if the geometry has changed
174 ximage_surface_update_geometry(struct native_surface
*nsurf
)
176 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
180 unsigned int w
, h
, border
, depth
;
181 boolean updated
= FALSE
;
183 /* pbuffer has fixed geometry */
184 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
187 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
188 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
189 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
193 xsurf
->server_stamp
++;
201 ximage_surface_notify_invalid(struct native_surface
*nsurf
)
203 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
204 struct ximage_display
*xdpy
= xsurf
->xdpy
;
206 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
207 &xsurf
->base
, xsurf
->server_stamp
);
211 * Update the buffers of the surface. It is a slow function due to the
212 * round-trip to the server.
215 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
217 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
222 updated
= ximage_surface_update_geometry(&xsurf
->base
);
224 /* all buffers become invalid */
225 xsurf
->valid_mask
= 0x0;
228 buffer_mask
&= ~xsurf
->valid_mask
;
229 /* all requested buffers are valid */
231 xsurf
->client_stamp
= xsurf
->server_stamp
;
237 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
238 if (native_attachment_mask_test(buffer_mask
, att
)) {
239 /* reallocate the texture */
240 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
243 new_valid
|= (1 << att
);
244 if (buffer_mask
== new_valid
)
249 xsurf
->valid_mask
|= new_valid
;
250 xsurf
->client_stamp
= xsurf
->server_stamp
;
252 return (new_valid
== buffer_mask
);
256 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
257 enum native_attachment which
)
259 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
260 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
261 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
262 struct pipe_surface
*psurf
;
264 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
267 assert(xsurf
->drawable
&& xbuf
->texture
);
269 /* what's the cost of surface creation? */
270 psurf
= screen
->get_tex_surface(screen
,
271 xbuf
->texture
, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ
);
275 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
277 pipe_surface_reference(&psurf
, NULL
);
283 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
285 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
288 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
289 NATIVE_ATTACHMENT_FRONT_LEFT
);
290 /* force buffers to be updated in next validation call */
291 xsurf
->server_stamp
++;
292 ximage_surface_notify_invalid(&xsurf
->base
);
298 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
300 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
301 struct ximage_buffer
*xfront
, *xback
, xtmp
;
304 /* display the back buffer first */
305 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
306 NATIVE_ATTACHMENT_BACK_LEFT
);
307 /* force buffers to be updated in next validation call */
308 xsurf
->server_stamp
++;
309 ximage_surface_notify_invalid(&xsurf
->base
);
311 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
312 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
314 /* skip swapping unless there is a front buffer */
315 if (xfront
->texture
) {
325 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
326 unsigned int *seq_num
, struct pipe_texture
**textures
,
327 int *width
, int *height
)
329 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
331 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
332 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
333 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
338 *seq_num
= xsurf
->client_stamp
;
342 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
343 if (native_attachment_mask_test(attachment_mask
, att
)) {
344 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
346 textures
[att
] = NULL
;
347 pipe_texture_reference(&textures
[att
], xbuf
->texture
);
353 *width
= xsurf
->width
;
355 *height
= xsurf
->height
;
361 ximage_surface_wait(struct native_surface
*nsurf
)
363 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
364 XSync(xsurf
->xdpy
->dpy
, FALSE
);
365 /* TODO XGetImage and update the front texture */
369 ximage_surface_destroy(struct native_surface
*nsurf
)
371 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
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 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
400 xsurf
->drawable
= drawable
;
401 xsurf
->visual
= *xconf
->visual
;
402 /* initialize the geometry */
403 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
406 xsurf
->base
.destroy
= ximage_surface_destroy
;
407 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
408 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
409 xsurf
->base
.validate
= ximage_surface_validate
;
410 xsurf
->base
.wait
= ximage_surface_wait
;
415 static struct native_surface
*
416 ximage_display_create_window_surface(struct native_display
*ndpy
,
417 EGLNativeWindowType win
,
418 const struct native_config
*nconf
)
420 struct ximage_surface
*xsurf
;
422 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
423 (Drawable
) win
, nconf
);
424 return (xsurf
) ? &xsurf
->base
: NULL
;
427 static struct native_surface
*
428 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
429 EGLNativePixmapType pix
,
430 const struct native_config
*nconf
)
432 struct ximage_surface
*xsurf
;
434 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
435 (Drawable
) pix
, nconf
);
436 return (xsurf
) ? &xsurf
->base
: NULL
;
439 static struct native_surface
*
440 ximage_display_create_pbuffer_surface(struct native_display
*ndpy
,
441 const struct native_config
*nconf
,
442 uint width
, uint height
)
444 struct ximage_surface
*xsurf
;
446 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PBUFFER
,
447 (Drawable
) None
, nconf
);
449 xsurf
->width
= width
;
450 xsurf
->height
= height
;
452 return (xsurf
) ? &xsurf
->base
: NULL
;
455 static enum pipe_format
456 choose_format(const XVisualInfo
*vinfo
)
458 enum pipe_format fmt
;
459 /* TODO elaborate the formats */
460 switch (vinfo
->depth
) {
462 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
465 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
468 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
471 fmt
= PIPE_FORMAT_NONE
;
478 static const struct native_config
**
479 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
481 struct ximage_display
*xdpy
= ximage_display(ndpy
);
482 const struct native_config
**configs
;
486 if (!xdpy
->configs
) {
487 const XVisualInfo
*visuals
;
488 int num_visuals
, count
, j
;
490 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
495 * Create two configs for each visual.
496 * One with depth/stencil buffer; one without
498 xdpy
->configs
= calloc(num_visuals
* 2, sizeof(*xdpy
->configs
));
503 for (i
= 0; i
< num_visuals
; i
++) {
504 for (j
= 0; j
< 2; j
++) {
505 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
506 __GLcontextModes
*mode
= &xconf
->base
.mode
;
508 xconf
->visual
= &visuals
[i
];
509 xconf
->base
.color_format
= choose_format(xconf
->visual
);
510 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
513 x11_screen_convert_visual(xdpy
->xscr
, xconf
->visual
, mode
);
514 /* support double buffer mode */
515 mode
->doubleBufferMode
= TRUE
;
517 xconf
->base
.depth_format
= PIPE_FORMAT_NONE
;
518 xconf
->base
.stencil_format
= PIPE_FORMAT_NONE
;
519 /* create the second config with depth/stencil buffer */
521 xconf
->base
.depth_format
= PIPE_FORMAT_Z24S8_UNORM
;
522 xconf
->base
.stencil_format
= PIPE_FORMAT_Z24S8_UNORM
;
523 mode
->depthBits
= 24;
524 mode
->stencilBits
= 8;
525 mode
->haveDepthBuffer
= TRUE
;
526 mode
->haveStencilBuffer
= TRUE
;
529 mode
->maxPbufferWidth
= 4096;
530 mode
->maxPbufferHeight
= 4096;
531 mode
->maxPbufferPixels
= 4096 * 4096;
533 GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
534 mode
->swapMethod
= GLX_SWAP_EXCHANGE_OML
;
537 mode
->bindToTextureRgba
= TRUE
;
539 mode
->bindToTextureRgb
= TRUE
;
545 xdpy
->num_configs
= count
;
548 configs
= malloc(xdpy
->num_configs
* sizeof(*configs
));
550 for (i
= 0; i
< xdpy
->num_configs
; i
++)
551 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
553 *num_configs
= xdpy
->num_configs
;
559 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
560 EGLNativePixmapType pix
,
561 const struct native_config
*nconf
)
563 struct ximage_display
*xdpy
= ximage_display(ndpy
);
564 enum pipe_format fmt
;
567 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
570 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
573 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
576 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
579 fmt
= PIPE_FORMAT_NONE
;
583 return (fmt
== nconf
->color_format
);
587 ximage_display_get_param(struct native_display
*ndpy
,
588 enum native_param_type param
)
593 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
594 /* private buffers are allocated */
606 ximage_display_destroy(struct native_display
*ndpy
)
608 struct ximage_display
*xdpy
= ximage_display(ndpy
);
613 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
615 x11_screen_destroy(xdpy
->xscr
);
617 XCloseDisplay(xdpy
->dpy
);
622 /* Helper function to build a subset of a driver stack consisting of
623 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
626 * This function could be shared, but currently causes headaches for
627 * the build systems, particularly scons if we try.
629 * Long term, want to avoid having global #defines for things like
630 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
631 * those #defines, so things that are painful for it now are likely to
632 * be painful for other build systems in the future.
634 static struct pipe_screen
*
635 swrast_xlib_create_screen( Display
*display
)
637 struct sw_winsys
*winsys
;
638 struct pipe_screen
*screen
= NULL
;
640 /* Create the underlying winsys, which performs presents to Xlib
643 winsys
= xlib_create_sw_winsys( display
);
647 /* Create a software rasterizer on top of that winsys. Use
648 * llvmpipe if it is available.
650 #if defined(GALLIUM_LLVMPIPE)
651 if (screen
== NULL
&&
652 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE
))
653 screen
= llvmpipe_create_screen( winsys
);
657 screen
= softpipe_create_screen( winsys
);
662 /* Inject any wrapping layers we want to here:
664 return gallium_wrap_screen( screen
);
668 winsys
->destroy( winsys
);
675 struct native_display
*
676 x11_create_ximage_display(EGLNativeDisplayType dpy
,
677 struct native_event_handler
*event_handler
)
679 struct ximage_display
*xdpy
;
681 xdpy
= CALLOC_STRUCT(ximage_display
);
687 xdpy
->dpy
= XOpenDisplay(NULL
);
692 xdpy
->own_dpy
= TRUE
;
695 xdpy
->event_handler
= event_handler
;
697 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
698 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
704 xdpy
->base
.screen
= swrast_xlib_create_screen(xdpy
->dpy
);
706 xdpy
->base
.destroy
= ximage_display_destroy
;
707 xdpy
->base
.get_param
= ximage_display_get_param
;
709 xdpy
->base
.get_configs
= ximage_display_get_configs
;
710 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
711 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
712 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
713 xdpy
->base
.create_pbuffer_surface
= ximage_display_create_pbuffer_surface
;