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
];
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
;
143 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
145 case NATIVE_ATTACHMENT_FRONT_LEFT
:
146 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
147 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_SCANOUT
;
149 case NATIVE_ATTACHMENT_BACK_LEFT
:
150 case NATIVE_ATTACHMENT_BACK_RIGHT
:
151 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
157 xbuf
->texture
= screen
->texture_create(screen
, &templ
);
159 xbuf
->xdraw
.visual
= xsurf
->visual
.visual
;
160 xbuf
->xdraw
.depth
= xsurf
->visual
.depth
;
161 xbuf
->xdraw
.drawable
= xsurf
->drawable
;
164 /* clean up the buffer if allocation failed */
166 ximage_surface_free_buffer(&xsurf
->base
, which
);
168 return (xbuf
->texture
!= NULL
);
172 * Update the geometry of the surface. Return TRUE if the geometry has changed
176 ximage_surface_update_geometry(struct native_surface
*nsurf
)
178 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
182 unsigned int w
, h
, border
, depth
;
183 boolean updated
= FALSE
;
185 /* pbuffer has fixed geometry */
186 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
189 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
190 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
191 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
195 xsurf
->server_stamp
++;
203 ximage_surface_notify_invalid(struct native_surface
*nsurf
)
205 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
206 struct ximage_display
*xdpy
= xsurf
->xdpy
;
208 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
209 &xsurf
->base
, xsurf
->server_stamp
);
213 * Update the buffers of the surface. It is a slow function due to the
214 * round-trip to the server.
217 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
219 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
224 updated
= ximage_surface_update_geometry(&xsurf
->base
);
226 /* all buffers become invalid */
227 xsurf
->valid_mask
= 0x0;
230 buffer_mask
&= ~xsurf
->valid_mask
;
231 /* all requested buffers are valid */
233 xsurf
->client_stamp
= xsurf
->server_stamp
;
239 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
240 if (native_attachment_mask_test(buffer_mask
, att
)) {
241 /* reallocate the texture */
242 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
245 new_valid
|= (1 << att
);
246 if (buffer_mask
== new_valid
)
251 xsurf
->valid_mask
|= new_valid
;
252 xsurf
->client_stamp
= xsurf
->server_stamp
;
254 return (new_valid
== buffer_mask
);
258 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
259 enum native_attachment which
)
261 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
262 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
263 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
264 struct pipe_surface
*psurf
;
266 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
269 assert(xsurf
->drawable
&& xbuf
->texture
);
271 psurf
= xsurf
->draw_surface
;
272 if (!psurf
|| psurf
->texture
!= xbuf
->texture
) {
273 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
275 psurf
= screen
->get_tex_surface(screen
,
276 xbuf
->texture
, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ
);
280 xsurf
->draw_surface
= psurf
;
283 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
289 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
291 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
294 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
295 NATIVE_ATTACHMENT_FRONT_LEFT
);
296 /* force buffers to be updated in next validation call */
297 xsurf
->server_stamp
++;
298 ximage_surface_notify_invalid(&xsurf
->base
);
304 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
306 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
307 struct ximage_buffer
*xfront
, *xback
, xtmp
;
310 /* display the back buffer first */
311 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
312 NATIVE_ATTACHMENT_BACK_LEFT
);
313 /* force buffers to be updated in next validation call */
314 xsurf
->server_stamp
++;
315 ximage_surface_notify_invalid(&xsurf
->base
);
317 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
318 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
320 /* skip swapping unless there is a front buffer */
321 if (xfront
->texture
) {
331 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
332 unsigned int *seq_num
, struct pipe_texture
**textures
,
333 int *width
, int *height
)
335 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
337 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
338 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
339 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
344 *seq_num
= xsurf
->client_stamp
;
348 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
349 if (native_attachment_mask_test(attachment_mask
, att
)) {
350 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
352 textures
[att
] = NULL
;
353 pipe_texture_reference(&textures
[att
], xbuf
->texture
);
359 *width
= xsurf
->width
;
361 *height
= xsurf
->height
;
367 ximage_surface_wait(struct native_surface
*nsurf
)
369 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
370 XSync(xsurf
->xdpy
->dpy
, FALSE
);
371 /* TODO XGetImage and update the front texture */
375 ximage_surface_destroy(struct native_surface
*nsurf
)
377 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
380 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
382 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
383 ximage_surface_free_buffer(&xsurf
->base
, i
);
388 static struct ximage_surface
*
389 ximage_display_create_surface(struct native_display
*ndpy
,
390 enum ximage_surface_type type
,
392 const struct native_config
*nconf
)
394 struct ximage_display
*xdpy
= ximage_display(ndpy
);
395 struct ximage_config
*xconf
= ximage_config(nconf
);
396 struct ximage_surface
*xsurf
;
398 xsurf
= CALLOC_STRUCT(ximage_surface
);
404 xsurf
->color_format
= xconf
->base
.color_format
;
405 xsurf
->drawable
= drawable
;
407 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
408 xsurf
->drawable
= drawable
;
409 xsurf
->visual
= *xconf
->visual
;
410 /* initialize the geometry */
411 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
414 xsurf
->base
.destroy
= ximage_surface_destroy
;
415 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
416 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
417 xsurf
->base
.validate
= ximage_surface_validate
;
418 xsurf
->base
.wait
= ximage_surface_wait
;
423 static struct native_surface
*
424 ximage_display_create_window_surface(struct native_display
*ndpy
,
425 EGLNativeWindowType win
,
426 const struct native_config
*nconf
)
428 struct ximage_surface
*xsurf
;
430 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
431 (Drawable
) win
, nconf
);
432 return (xsurf
) ? &xsurf
->base
: NULL
;
435 static struct native_surface
*
436 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
437 EGLNativePixmapType pix
,
438 const struct native_config
*nconf
)
440 struct ximage_surface
*xsurf
;
442 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
443 (Drawable
) pix
, nconf
);
444 return (xsurf
) ? &xsurf
->base
: NULL
;
447 static struct native_surface
*
448 ximage_display_create_pbuffer_surface(struct native_display
*ndpy
,
449 const struct native_config
*nconf
,
450 uint width
, uint height
)
452 struct ximage_surface
*xsurf
;
454 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PBUFFER
,
455 (Drawable
) None
, nconf
);
457 xsurf
->width
= width
;
458 xsurf
->height
= height
;
460 return (xsurf
) ? &xsurf
->base
: NULL
;
463 static enum pipe_format
464 choose_format(const XVisualInfo
*vinfo
)
466 enum pipe_format fmt
;
467 /* TODO elaborate the formats */
468 switch (vinfo
->depth
) {
470 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
473 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
476 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
479 fmt
= PIPE_FORMAT_NONE
;
486 static const struct native_config
**
487 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
489 struct ximage_display
*xdpy
= ximage_display(ndpy
);
490 const struct native_config
**configs
;
494 if (!xdpy
->configs
) {
495 const XVisualInfo
*visuals
;
496 int num_visuals
, count
, j
;
498 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
503 * Create two configs for each visual.
504 * One with depth/stencil buffer; one without
506 xdpy
->configs
= calloc(num_visuals
* 2, sizeof(*xdpy
->configs
));
511 for (i
= 0; i
< num_visuals
; i
++) {
512 for (j
= 0; j
< 2; j
++) {
513 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
514 __GLcontextModes
*mode
= &xconf
->base
.mode
;
516 xconf
->visual
= &visuals
[i
];
517 xconf
->base
.color_format
= choose_format(xconf
->visual
);
518 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
521 x11_screen_convert_visual(xdpy
->xscr
, xconf
->visual
, mode
);
522 /* support double buffer mode */
523 mode
->doubleBufferMode
= TRUE
;
525 xconf
->base
.depth_format
= PIPE_FORMAT_NONE
;
526 xconf
->base
.stencil_format
= PIPE_FORMAT_NONE
;
527 /* create the second config with depth/stencil buffer */
529 xconf
->base
.depth_format
= PIPE_FORMAT_Z24_UNORM_S8_USCALED
;
530 xconf
->base
.stencil_format
= PIPE_FORMAT_Z24_UNORM_S8_USCALED
;
531 mode
->depthBits
= 24;
532 mode
->stencilBits
= 8;
533 mode
->haveDepthBuffer
= TRUE
;
534 mode
->haveStencilBuffer
= TRUE
;
537 mode
->maxPbufferWidth
= 4096;
538 mode
->maxPbufferHeight
= 4096;
539 mode
->maxPbufferPixels
= 4096 * 4096;
541 GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
542 mode
->swapMethod
= GLX_SWAP_EXCHANGE_OML
;
545 mode
->bindToTextureRgba
= TRUE
;
547 mode
->bindToTextureRgb
= TRUE
;
553 xdpy
->num_configs
= count
;
556 configs
= malloc(xdpy
->num_configs
* sizeof(*configs
));
558 for (i
= 0; i
< xdpy
->num_configs
; i
++)
559 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
561 *num_configs
= xdpy
->num_configs
;
567 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
568 EGLNativePixmapType pix
,
569 const struct native_config
*nconf
)
571 struct ximage_display
*xdpy
= ximage_display(ndpy
);
572 enum pipe_format fmt
;
575 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
578 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
581 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
584 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
587 fmt
= PIPE_FORMAT_NONE
;
591 return (fmt
== nconf
->color_format
);
595 ximage_display_get_param(struct native_display
*ndpy
,
596 enum native_param_type param
)
601 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
602 /* private buffers are allocated */
614 ximage_display_destroy(struct native_display
*ndpy
)
616 struct ximage_display
*xdpy
= ximage_display(ndpy
);
621 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
623 x11_screen_destroy(xdpy
->xscr
);
625 XCloseDisplay(xdpy
->dpy
);
630 /* Helper function to build a subset of a driver stack consisting of
631 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
634 * This function could be shared, but currently causes headaches for
635 * the build systems, particularly scons if we try.
637 * Long term, want to avoid having global #defines for things like
638 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
639 * those #defines, so things that are painful for it now are likely to
640 * be painful for other build systems in the future.
642 static struct pipe_screen
*
643 swrast_xlib_create_screen( Display
*display
)
645 struct sw_winsys
*winsys
;
646 struct pipe_screen
*screen
= NULL
;
648 /* Create the underlying winsys, which performs presents to Xlib
651 winsys
= xlib_create_sw_winsys( display
);
655 /* Create a software rasterizer on top of that winsys. Use
656 * llvmpipe if it is available.
658 #if defined(GALLIUM_LLVMPIPE)
659 if (screen
== NULL
&&
660 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE
))
661 screen
= llvmpipe_create_screen( winsys
);
665 screen
= softpipe_create_screen( winsys
);
670 /* Inject any wrapping layers we want to here:
672 return gallium_wrap_screen( screen
);
676 winsys
->destroy( winsys
);
683 struct native_display
*
684 x11_create_ximage_display(EGLNativeDisplayType dpy
,
685 struct native_event_handler
*event_handler
)
687 struct ximage_display
*xdpy
;
689 xdpy
= CALLOC_STRUCT(ximage_display
);
695 xdpy
->dpy
= XOpenDisplay(NULL
);
700 xdpy
->own_dpy
= TRUE
;
703 xdpy
->event_handler
= event_handler
;
705 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
706 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
712 xdpy
->base
.screen
= swrast_xlib_create_screen(xdpy
->dpy
);
714 xdpy
->base
.destroy
= ximage_display_destroy
;
715 xdpy
->base
.get_param
= ximage_display_get_param
;
717 xdpy
->base
.get_configs
= ximage_display_get_configs
;
718 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
719 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
720 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
721 xdpy
->base
.create_pbuffer_surface
= ximage_display_create_pbuffer_surface
;