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_resource
*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_resource_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_resource 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
.bind
= PIPE_BIND_RENDER_TARGET
;
144 case NATIVE_ATTACHMENT_FRONT_LEFT
:
145 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
146 templ
.bind
|= PIPE_BIND_SCANOUT
;
148 case NATIVE_ATTACHMENT_BACK_LEFT
:
149 case NATIVE_ATTACHMENT_BACK_RIGHT
:
150 templ
.bind
|= PIPE_BIND_DISPLAY_TARGET
;
155 xbuf
->texture
= screen
->resource_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 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
184 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
185 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
189 xsurf
->server_stamp
++;
197 ximage_surface_notify_invalid(struct native_surface
*nsurf
)
199 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
200 struct ximage_display
*xdpy
= xsurf
->xdpy
;
202 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
203 &xsurf
->base
, xsurf
->server_stamp
);
207 * Update the buffers of the surface. It is a slow function due to the
208 * round-trip to the server.
211 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
213 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
218 updated
= ximage_surface_update_geometry(&xsurf
->base
);
220 /* all buffers become invalid */
221 xsurf
->valid_mask
= 0x0;
224 buffer_mask
&= ~xsurf
->valid_mask
;
225 /* all requested buffers are valid */
227 xsurf
->client_stamp
= xsurf
->server_stamp
;
233 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
234 if (native_attachment_mask_test(buffer_mask
, att
)) {
235 /* reallocate the texture */
236 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
239 new_valid
|= (1 << att
);
240 if (buffer_mask
== new_valid
)
245 xsurf
->valid_mask
|= new_valid
;
246 xsurf
->client_stamp
= xsurf
->server_stamp
;
248 return (new_valid
== buffer_mask
);
252 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
253 enum native_attachment which
)
255 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
256 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
257 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
258 struct pipe_surface
*psurf
;
260 assert(xsurf
->drawable
&& xbuf
->texture
);
262 psurf
= xsurf
->draw_surface
;
263 if (!psurf
|| psurf
->texture
!= xbuf
->texture
) {
264 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
266 psurf
= screen
->get_tex_surface(screen
,
267 xbuf
->texture
, 0, 0, 0, PIPE_BIND_DISPLAY_TARGET
);
271 xsurf
->draw_surface
= psurf
;
274 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
280 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
282 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
285 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
286 NATIVE_ATTACHMENT_FRONT_LEFT
);
287 /* force buffers to be updated in next validation call */
288 xsurf
->server_stamp
++;
289 ximage_surface_notify_invalid(&xsurf
->base
);
295 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
297 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
298 struct ximage_buffer
*xfront
, *xback
, xtmp
;
301 /* display the back buffer first */
302 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
303 NATIVE_ATTACHMENT_BACK_LEFT
);
304 /* force buffers to be updated in next validation call */
305 xsurf
->server_stamp
++;
306 ximage_surface_notify_invalid(&xsurf
->base
);
308 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
309 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
311 /* skip swapping unless there is a front buffer */
312 if (xfront
->texture
) {
322 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
323 unsigned int *seq_num
, struct pipe_resource
**textures
,
324 int *width
, int *height
)
326 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
328 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
329 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
330 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
335 *seq_num
= xsurf
->client_stamp
;
339 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
340 if (native_attachment_mask_test(attachment_mask
, att
)) {
341 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
343 textures
[att
] = NULL
;
344 pipe_resource_reference(&textures
[att
], xbuf
->texture
);
350 *width
= xsurf
->width
;
352 *height
= xsurf
->height
;
358 ximage_surface_wait(struct native_surface
*nsurf
)
360 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
361 XSync(xsurf
->xdpy
->dpy
, FALSE
);
362 /* TODO XGetImage and update the front texture */
366 ximage_surface_destroy(struct native_surface
*nsurf
)
368 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
371 pipe_surface_reference(&xsurf
->draw_surface
, NULL
);
373 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
374 ximage_surface_free_buffer(&xsurf
->base
, i
);
379 static struct ximage_surface
*
380 ximage_display_create_surface(struct native_display
*ndpy
,
381 enum ximage_surface_type type
,
383 const struct native_config
*nconf
)
385 struct ximage_display
*xdpy
= ximage_display(ndpy
);
386 struct ximage_config
*xconf
= ximage_config(nconf
);
387 struct ximage_surface
*xsurf
;
389 xsurf
= CALLOC_STRUCT(ximage_surface
);
395 xsurf
->color_format
= xconf
->base
.color_format
;
396 xsurf
->drawable
= drawable
;
398 xsurf
->drawable
= drawable
;
399 xsurf
->visual
= *xconf
->visual
;
400 /* initialize the geometry */
401 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
403 xsurf
->base
.destroy
= ximage_surface_destroy
;
404 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
405 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
406 xsurf
->base
.validate
= ximage_surface_validate
;
407 xsurf
->base
.wait
= ximage_surface_wait
;
412 static struct native_surface
*
413 ximage_display_create_window_surface(struct native_display
*ndpy
,
414 EGLNativeWindowType win
,
415 const struct native_config
*nconf
)
417 struct ximage_surface
*xsurf
;
419 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
420 (Drawable
) win
, nconf
);
421 return (xsurf
) ? &xsurf
->base
: NULL
;
424 static struct native_surface
*
425 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
426 EGLNativePixmapType pix
,
427 const struct native_config
*nconf
)
429 struct ximage_surface
*xsurf
;
431 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
432 (Drawable
) pix
, nconf
);
433 return (xsurf
) ? &xsurf
->base
: NULL
;
436 static enum pipe_format
437 choose_format(const XVisualInfo
*vinfo
)
439 enum pipe_format fmt
;
440 /* TODO elaborate the formats */
441 switch (vinfo
->depth
) {
443 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
446 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
449 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
452 fmt
= PIPE_FORMAT_NONE
;
459 static const struct native_config
**
460 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
462 struct ximage_display
*xdpy
= ximage_display(ndpy
);
463 const struct native_config
**configs
;
467 if (!xdpy
->configs
) {
468 const XVisualInfo
*visuals
;
469 int num_visuals
, count
;
471 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
476 * Create two configs for each visual.
477 * One with depth/stencil buffer; one without
479 xdpy
->configs
= calloc(num_visuals
* 2, sizeof(*xdpy
->configs
));
484 for (i
= 0; i
< num_visuals
; i
++) {
485 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
487 xconf
->visual
= &visuals
[i
];
488 xconf
->base
.color_format
= choose_format(xconf
->visual
);
489 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
492 xconf
->base
.buffer_mask
=
493 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
494 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
496 xconf
->base
.window_bit
= TRUE
;
497 xconf
->base
.pixmap_bit
= TRUE
;
499 xconf
->base
.native_visual_id
= xconf
->visual
->visualid
;
500 #if defined(__cplusplus) || defined(c_plusplus)
501 xconf
->base
.native_visual_type
= xconf
->visual
->c_class
;
503 xconf
->base
.native_visual_type
= xconf
->visual
->class;
506 xconf
->base
.slow_config
= TRUE
;
511 xdpy
->num_configs
= count
;
514 configs
= malloc(xdpy
->num_configs
* sizeof(*configs
));
516 for (i
= 0; i
< xdpy
->num_configs
; i
++)
517 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
519 *num_configs
= xdpy
->num_configs
;
525 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
526 EGLNativePixmapType pix
,
527 const struct native_config
*nconf
)
529 struct ximage_display
*xdpy
= ximage_display(ndpy
);
530 enum pipe_format fmt
;
533 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
536 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
539 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
542 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
545 fmt
= PIPE_FORMAT_NONE
;
549 return (fmt
== nconf
->color_format
);
553 ximage_display_get_param(struct native_display
*ndpy
,
554 enum native_param_type param
)
559 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
560 /* private buffers are allocated */
572 ximage_display_destroy(struct native_display
*ndpy
)
574 struct ximage_display
*xdpy
= ximage_display(ndpy
);
579 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
581 x11_screen_destroy(xdpy
->xscr
);
583 XCloseDisplay(xdpy
->dpy
);
588 /* Helper function to build a subset of a driver stack consisting of
589 * one of the software rasterizers (cell, llvmpipe, softpipe) and the
592 * This function could be shared, but currently causes headaches for
593 * the build systems, particularly scons if we try.
595 * Long term, want to avoid having global #defines for things like
596 * GALLIUM_LLVMPIPE, GALLIUM_CELL, etc. Scons already eliminates
597 * those #defines, so things that are painful for it now are likely to
598 * be painful for other build systems in the future.
600 static struct pipe_screen
*
601 swrast_xlib_create_screen( Display
*display
)
603 struct sw_winsys
*winsys
;
604 struct pipe_screen
*screen
= NULL
;
606 /* Create the underlying winsys, which performs presents to Xlib
609 winsys
= xlib_create_sw_winsys( display
);
613 /* Create a software rasterizer on top of that winsys. Use
614 * llvmpipe if it is available.
616 #if defined(GALLIUM_LLVMPIPE)
617 if (screen
== NULL
&&
618 !debug_get_bool_option("GALLIUM_NO_LLVM", FALSE
))
619 screen
= llvmpipe_create_screen( winsys
);
623 screen
= softpipe_create_screen( winsys
);
628 /* Inject any wrapping layers we want to here:
630 return gallium_wrap_screen( screen
);
634 winsys
->destroy( winsys
);
641 struct native_display
*
642 x11_create_ximage_display(EGLNativeDisplayType dpy
,
643 struct native_event_handler
*event_handler
)
645 struct ximage_display
*xdpy
;
647 xdpy
= CALLOC_STRUCT(ximage_display
);
653 xdpy
->dpy
= XOpenDisplay(NULL
);
658 xdpy
->own_dpy
= TRUE
;
661 xdpy
->event_handler
= event_handler
;
663 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
664 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
670 xdpy
->base
.screen
= swrast_xlib_create_screen(xdpy
->dpy
);
672 xdpy
->base
.destroy
= ximage_display_destroy
;
673 xdpy
->base
.get_param
= ximage_display_get_param
;
675 xdpy
->base
.get_configs
= ximage_display_get_configs
;
676 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
677 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
678 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;