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.
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"
40 #include "native_x11.h"
41 #include "x11_screen.h"
43 enum ximage_surface_type
{
44 XIMAGE_SURFACE_TYPE_WINDOW
,
45 XIMAGE_SURFACE_TYPE_PIXMAP
,
48 struct ximage_display
{
49 struct native_display base
;
53 struct native_event_handler
*event_handler
;
55 struct x11_screen
*xscr
;
58 struct ximage_config
*configs
;
62 struct ximage_buffer
{
63 struct pipe_resource
*texture
;
64 struct xlib_drawable xdraw
;
67 struct ximage_surface
{
68 struct native_surface base
;
70 enum ximage_surface_type type
;
71 enum pipe_format color_format
;
73 struct ximage_display
*xdpy
;
75 unsigned int server_stamp
;
76 unsigned int client_stamp
;
78 struct ximage_buffer buffers
[NUM_NATIVE_ATTACHMENTS
];
81 struct pipe_surface
*draw_surface
;
84 struct ximage_config
{
85 struct native_config base
;
86 const XVisualInfo
*visual
;
89 static INLINE
struct ximage_display
*
90 ximage_display(const struct native_display
*ndpy
)
92 return (struct ximage_display
*) ndpy
;
95 static INLINE
struct ximage_surface
*
96 ximage_surface(const struct native_surface
*nsurf
)
98 return (struct ximage_surface
*) nsurf
;
101 static INLINE
struct ximage_config
*
102 ximage_config(const struct native_config
*nconf
)
104 return (struct ximage_config
*) nconf
;
108 ximage_surface_free_buffer(struct native_surface
*nsurf
,
109 enum native_attachment which
)
111 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
112 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
114 pipe_resource_reference(&xbuf
->texture
, NULL
);
118 ximage_surface_alloc_buffer(struct native_surface
*nsurf
,
119 enum native_attachment which
)
121 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
122 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
123 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
124 struct pipe_resource templ
;
128 ximage_surface_free_buffer(&xsurf
->base
, which
);
130 memset(&templ
, 0, sizeof(templ
));
131 templ
.target
= PIPE_TEXTURE_2D
;
132 templ
.format
= xsurf
->color_format
;
133 templ
.width0
= xsurf
->width
;
134 templ
.height0
= xsurf
->height
;
136 templ
.bind
= PIPE_BIND_RENDER_TARGET
;
139 case NATIVE_ATTACHMENT_FRONT_LEFT
:
140 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
141 templ
.bind
|= PIPE_BIND_SCANOUT
;
143 case NATIVE_ATTACHMENT_BACK_LEFT
:
144 case NATIVE_ATTACHMENT_BACK_RIGHT
:
145 templ
.bind
|= PIPE_BIND_DISPLAY_TARGET
;
150 xbuf
->texture
= screen
->resource_create(screen
, &templ
);
152 xbuf
->xdraw
.visual
= xsurf
->visual
.visual
;
153 xbuf
->xdraw
.depth
= xsurf
->visual
.depth
;
154 xbuf
->xdraw
.drawable
= xsurf
->drawable
;
157 /* clean up the buffer if allocation failed */
159 ximage_surface_free_buffer(&xsurf
->base
, which
);
161 return (xbuf
->texture
!= NULL
);
165 * Update the geometry of the surface. Return TRUE if the geometry has changed
169 ximage_surface_update_geometry(struct native_surface
*nsurf
)
171 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
175 unsigned int w
, h
, border
, depth
;
176 boolean updated
= FALSE
;
178 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
179 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
180 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
184 xsurf
->server_stamp
++;
192 ximage_surface_notify_invalid(struct native_surface
*nsurf
)
194 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
195 struct ximage_display
*xdpy
= xsurf
->xdpy
;
197 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
198 &xsurf
->base
, xsurf
->server_stamp
);
202 * Update the buffers of the surface. It is a slow function due to the
203 * round-trip to the server.
206 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
208 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
213 updated
= ximage_surface_update_geometry(&xsurf
->base
);
215 /* all buffers become invalid */
216 xsurf
->valid_mask
= 0x0;
219 buffer_mask
&= ~xsurf
->valid_mask
;
220 /* all requested buffers are valid */
222 xsurf
->client_stamp
= xsurf
->server_stamp
;
228 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
229 if (native_attachment_mask_test(buffer_mask
, att
)) {
230 /* reallocate the texture */
231 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
234 new_valid
|= (1 << att
);
235 if (buffer_mask
== new_valid
)
240 xsurf
->valid_mask
|= new_valid
;
241 xsurf
->client_stamp
= xsurf
->server_stamp
;
243 return (new_valid
== buffer_mask
);
247 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
248 enum native_attachment which
)
250 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
251 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
252 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
253 struct pipe_surface
*psurf
;
255 assert(xsurf
->drawable
&& xbuf
->texture
);
257 psurf
= xsurf
->draw_surface
;
258 if (!psurf
|| psurf
->texture
!= xbuf
->texture
) {
259 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
261 psurf
= screen
->get_tex_surface(screen
,
262 xbuf
->texture
, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET
);
266 xsurf
->draw_surface
= psurf
;
269 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
275 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
277 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
280 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
281 NATIVE_ATTACHMENT_FRONT_LEFT
);
282 /* force buffers to be updated in next validation call */
283 xsurf
->server_stamp
++;
284 ximage_surface_notify_invalid(&xsurf
->base
);
290 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
292 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
293 struct ximage_buffer
*xfront
, *xback
, xtmp
;
296 /* display the back buffer first */
297 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
298 NATIVE_ATTACHMENT_BACK_LEFT
);
299 /* force buffers to be updated in next validation call */
300 xsurf
->server_stamp
++;
301 ximage_surface_notify_invalid(&xsurf
->base
);
303 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
304 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
306 /* skip swapping unless there is a front buffer */
307 if (xfront
->texture
) {
317 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
318 unsigned int *seq_num
, struct pipe_resource
**textures
,
319 int *width
, int *height
)
321 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
323 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
324 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
325 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
330 *seq_num
= xsurf
->client_stamp
;
334 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
335 if (native_attachment_mask_test(attachment_mask
, att
)) {
336 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
338 textures
[att
] = NULL
;
339 pipe_resource_reference(&textures
[att
], xbuf
->texture
);
345 *width
= xsurf
->width
;
347 *height
= xsurf
->height
;
353 ximage_surface_wait(struct native_surface
*nsurf
)
355 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
356 XSync(xsurf
->xdpy
->dpy
, FALSE
);
357 /* TODO XGetImage and update the front texture */
361 ximage_surface_destroy(struct native_surface
*nsurf
)
363 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
366 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
368 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
369 ximage_surface_free_buffer(&xsurf
->base
, i
);
374 static struct ximage_surface
*
375 ximage_display_create_surface(struct native_display
*ndpy
,
376 enum ximage_surface_type type
,
378 const struct native_config
*nconf
)
380 struct ximage_display
*xdpy
= ximage_display(ndpy
);
381 struct ximage_config
*xconf
= ximage_config(nconf
);
382 struct ximage_surface
*xsurf
;
384 xsurf
= CALLOC_STRUCT(ximage_surface
);
390 xsurf
->color_format
= xconf
->base
.color_format
;
391 xsurf
->drawable
= drawable
;
393 xsurf
->drawable
= drawable
;
394 xsurf
->visual
= *xconf
->visual
;
395 /* initialize the geometry */
396 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
398 xsurf
->base
.destroy
= ximage_surface_destroy
;
399 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
400 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
401 xsurf
->base
.validate
= ximage_surface_validate
;
402 xsurf
->base
.wait
= ximage_surface_wait
;
407 static struct native_surface
*
408 ximage_display_create_window_surface(struct native_display
*ndpy
,
409 EGLNativeWindowType win
,
410 const struct native_config
*nconf
)
412 struct ximage_surface
*xsurf
;
414 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
415 (Drawable
) win
, nconf
);
416 return (xsurf
) ? &xsurf
->base
: NULL
;
419 static struct native_surface
*
420 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
421 EGLNativePixmapType pix
,
422 const struct native_config
*nconf
)
424 struct ximage_surface
*xsurf
;
426 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
427 (Drawable
) pix
, nconf
);
428 return (xsurf
) ? &xsurf
->base
: NULL
;
431 static enum pipe_format
432 choose_format(const XVisualInfo
*vinfo
)
434 enum pipe_format fmt
;
435 /* TODO elaborate the formats */
436 switch (vinfo
->depth
) {
438 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
441 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
444 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
447 fmt
= PIPE_FORMAT_NONE
;
454 static const struct native_config
**
455 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
457 struct ximage_display
*xdpy
= ximage_display(ndpy
);
458 const struct native_config
**configs
;
462 if (!xdpy
->configs
) {
463 const XVisualInfo
*visuals
;
464 int num_visuals
, count
;
466 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
471 * Create two configs for each visual.
472 * One with depth/stencil buffer; one without
474 xdpy
->configs
= CALLOC(num_visuals
* 2, sizeof(*xdpy
->configs
));
479 for (i
= 0; i
< num_visuals
; i
++) {
480 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
482 xconf
->visual
= &visuals
[i
];
483 xconf
->base
.color_format
= choose_format(xconf
->visual
);
484 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
487 xconf
->base
.buffer_mask
=
488 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
489 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
491 xconf
->base
.window_bit
= TRUE
;
492 xconf
->base
.pixmap_bit
= TRUE
;
494 xconf
->base
.native_visual_id
= xconf
->visual
->visualid
;
495 #if defined(__cplusplus) || defined(c_plusplus)
496 xconf
->base
.native_visual_type
= xconf
->visual
->c_class
;
498 xconf
->base
.native_visual_type
= xconf
->visual
->class;
501 xconf
->base
.slow_config
= TRUE
;
506 xdpy
->num_configs
= count
;
509 configs
= MALLOC(xdpy
->num_configs
* sizeof(*configs
));
511 for (i
= 0; i
< xdpy
->num_configs
; i
++)
512 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
514 *num_configs
= xdpy
->num_configs
;
520 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
521 EGLNativePixmapType pix
,
522 const struct native_config
*nconf
)
524 struct ximage_display
*xdpy
= ximage_display(ndpy
);
525 enum pipe_format fmt
;
528 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
531 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
534 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
537 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
540 fmt
= PIPE_FORMAT_NONE
;
544 return (fmt
== nconf
->color_format
);
548 ximage_display_get_param(struct native_display
*ndpy
,
549 enum native_param_type param
)
554 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
555 /* private buffers are allocated */
567 ximage_display_destroy(struct native_display
*ndpy
)
569 struct ximage_display
*xdpy
= ximage_display(ndpy
);
574 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
576 x11_screen_destroy(xdpy
->xscr
);
578 XCloseDisplay(xdpy
->dpy
);
583 /* Helper function to build a subset of a driver stack consisting of
584 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
587 * This function could be shared, but currently causes headaches for
588 * the build systems, particularly scons if we try.
590 * Long term, want to avoid having global #defines for things like
591 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
592 * those #defines, so things that are painful for it now are likely to
593 * be painful for other build systems in the future.
595 static struct pipe_screen
*
596 swrast_xlib_create_screen( Display
*display
)
598 struct sw_winsys
*winsys
;
599 struct pipe_screen
*screen
= NULL
;
601 /* Create the underlying winsys, which performs presents to Xlib
604 winsys
= xlib_create_sw_winsys( display
);
608 /* Create a software rasterizer on top of that winsys. Use
609 * llvmpipe if it is available.
611 #if defined(GALLIUM_LLVMPIPE)
612 if (screen
== NULL
&&
613 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE
))
614 screen
= llvmpipe_create_screen( winsys
);
618 screen
= softpipe_create_screen( winsys
);
623 /* Inject any wrapping layers we want to here:
625 return gallium_wrap_screen( screen
);
629 winsys
->destroy( winsys
);
636 struct native_display
*
637 x11_create_ximage_display(EGLNativeDisplayType dpy
,
638 struct native_event_handler
*event_handler
)
640 struct ximage_display
*xdpy
;
642 xdpy
= CALLOC_STRUCT(ximage_display
);
648 xdpy
->dpy
= XOpenDisplay(NULL
);
653 xdpy
->own_dpy
= TRUE
;
656 xdpy
->event_handler
= event_handler
;
658 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
659 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
665 xdpy
->base
.screen
= swrast_xlib_create_screen(xdpy
->dpy
);
667 xdpy
->base
.destroy
= ximage_display_destroy
;
668 xdpy
->base
.get_param
= ximage_display_get_param
;
670 xdpy
->base
.get_configs
= ximage_display_get_configs
;
671 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
672 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
673 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;