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 "util/u_debug.h"
37 #include "common/native_helper.h"
38 #include "native_x11.h"
39 #include "x11_screen.h"
41 enum ximage_surface_type
{
42 XIMAGE_SURFACE_TYPE_WINDOW
,
43 XIMAGE_SURFACE_TYPE_PIXMAP
,
46 struct ximage_display
{
47 struct native_display base
;
51 struct native_event_handler
*event_handler
;
53 struct x11_screen
*xscr
;
56 struct ximage_config
*configs
;
60 struct ximage_surface
{
61 struct native_surface base
;
63 enum ximage_surface_type type
;
64 enum pipe_format color_format
;
66 struct ximage_display
*xdpy
;
68 unsigned int server_stamp
;
69 unsigned int client_stamp
;
71 struct resource_surface
*rsurf
;
72 struct xlib_drawable xdraw
;
75 struct ximage_config
{
76 struct native_config base
;
77 const XVisualInfo
*visual
;
80 static INLINE
struct ximage_display
*
81 ximage_display(const struct native_display
*ndpy
)
83 return (struct ximage_display
*) ndpy
;
86 static INLINE
struct ximage_surface
*
87 ximage_surface(const struct native_surface
*nsurf
)
89 return (struct ximage_surface
*) nsurf
;
92 static INLINE
struct ximage_config
*
93 ximage_config(const struct native_config
*nconf
)
95 return (struct ximage_config
*) nconf
;
99 * Update the geometry of the surface. This is a slow functions.
102 ximage_surface_update_geometry(struct native_surface
*nsurf
)
104 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
108 unsigned int w
, h
, border
, depth
;
110 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
111 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
112 if (ok
&& resource_surface_set_size(xsurf
->rsurf
, w
, h
))
113 xsurf
->server_stamp
++;
117 * Update the buffers of the surface.
120 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
122 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
124 if (xsurf
->client_stamp
!= xsurf
->server_stamp
) {
125 ximage_surface_update_geometry(&xsurf
->base
);
126 xsurf
->client_stamp
= xsurf
->server_stamp
;
129 return resource_surface_add_resources(xsurf
->rsurf
, buffer_mask
);
133 * Emulate an invalidate event.
136 ximage_surface_invalidate(struct native_surface
*nsurf
)
138 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
139 struct ximage_display
*xdpy
= xsurf
->xdpy
;
141 xsurf
->server_stamp
++;
142 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
143 &xsurf
->base
, xsurf
->server_stamp
);
147 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
149 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
152 ret
= resource_surface_present(xsurf
->rsurf
,
153 NATIVE_ATTACHMENT_FRONT_LEFT
, (void *) &xsurf
->xdraw
);
154 /* force buffers to be updated in next validation call */
155 ximage_surface_invalidate(&xsurf
->base
);
161 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
163 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
166 ret
= resource_surface_present(xsurf
->rsurf
,
167 NATIVE_ATTACHMENT_BACK_LEFT
, (void *) &xsurf
->xdraw
);
169 resource_surface_swap_buffers(xsurf
->rsurf
,
170 NATIVE_ATTACHMENT_FRONT_LEFT
, NATIVE_ATTACHMENT_BACK_LEFT
, TRUE
);
171 /* the front/back buffers have been swapped */
172 ximage_surface_invalidate(&xsurf
->base
);
178 ximage_surface_present(struct native_surface
*nsurf
,
179 enum native_attachment natt
,
185 if (preserve
|| swap_interval
)
189 case NATIVE_ATTACHMENT_FRONT_LEFT
:
190 ret
= ximage_surface_flush_frontbuffer(nsurf
);
192 case NATIVE_ATTACHMENT_BACK_LEFT
:
193 ret
= ximage_surface_swap_buffers(nsurf
);
204 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
205 unsigned int *seq_num
, struct pipe_resource
**textures
,
206 int *width
, int *height
)
208 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
211 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
215 *seq_num
= xsurf
->client_stamp
;
218 resource_surface_get_resources(xsurf
->rsurf
, textures
, attachment_mask
);
220 resource_surface_get_size(xsurf
->rsurf
, &w
, &h
);
230 ximage_surface_wait(struct native_surface
*nsurf
)
232 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
233 XSync(xsurf
->xdpy
->dpy
, FALSE
);
234 /* TODO XGetImage and update the front texture */
238 ximage_surface_destroy(struct native_surface
*nsurf
)
240 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
242 resource_surface_destroy(xsurf
->rsurf
);
246 static struct ximage_surface
*
247 ximage_display_create_surface(struct native_display
*ndpy
,
248 enum ximage_surface_type type
,
250 const struct native_config
*nconf
)
252 struct ximage_display
*xdpy
= ximage_display(ndpy
);
253 struct ximage_config
*xconf
= ximage_config(nconf
);
254 struct ximage_surface
*xsurf
;
256 xsurf
= CALLOC_STRUCT(ximage_surface
);
262 xsurf
->color_format
= xconf
->base
.color_format
;
263 xsurf
->drawable
= drawable
;
265 xsurf
->rsurf
= resource_surface_create(xdpy
->base
.screen
,
267 PIPE_BIND_RENDER_TARGET
|
268 PIPE_BIND_SAMPLER_VIEW
|
269 PIPE_BIND_DISPLAY_TARGET
|
276 xsurf
->drawable
= drawable
;
277 xsurf
->visual
= *xconf
->visual
;
278 /* initialize the geometry */
279 ximage_surface_update_geometry(&xsurf
->base
);
281 xsurf
->xdraw
.visual
= xsurf
->visual
.visual
;
282 xsurf
->xdraw
.depth
= xsurf
->visual
.depth
;
283 xsurf
->xdraw
.drawable
= xsurf
->drawable
;
285 xsurf
->base
.destroy
= ximage_surface_destroy
;
286 xsurf
->base
.present
= ximage_surface_present
;
287 xsurf
->base
.validate
= ximage_surface_validate
;
288 xsurf
->base
.wait
= ximage_surface_wait
;
293 static struct native_surface
*
294 ximage_display_create_window_surface(struct native_display
*ndpy
,
295 EGLNativeWindowType win
,
296 const struct native_config
*nconf
)
298 struct ximage_surface
*xsurf
;
300 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
301 (Drawable
) win
, nconf
);
302 return (xsurf
) ? &xsurf
->base
: NULL
;
305 static struct native_surface
*
306 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
307 EGLNativePixmapType pix
,
308 const struct native_config
*nconf
)
310 struct ximage_surface
*xsurf
;
312 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
313 (Drawable
) pix
, nconf
);
314 return (xsurf
) ? &xsurf
->base
: NULL
;
317 static enum pipe_format
318 choose_format(const XVisualInfo
*vinfo
)
320 enum pipe_format fmt
;
321 /* TODO elaborate the formats */
322 switch (vinfo
->depth
) {
324 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
327 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
330 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
333 fmt
= PIPE_FORMAT_NONE
;
340 static const struct native_config
**
341 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
343 struct ximage_display
*xdpy
= ximage_display(ndpy
);
344 const struct native_config
**configs
;
348 if (!xdpy
->configs
) {
349 const XVisualInfo
*visuals
;
350 int num_visuals
, count
;
352 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
357 * Create two configs for each visual.
358 * One with depth/stencil buffer; one without
360 xdpy
->configs
= CALLOC(num_visuals
* 2, sizeof(*xdpy
->configs
));
365 for (i
= 0; i
< num_visuals
; i
++) {
366 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
368 xconf
->visual
= &visuals
[i
];
369 xconf
->base
.color_format
= choose_format(xconf
->visual
);
370 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
373 xconf
->base
.buffer_mask
=
374 (1 << NATIVE_ATTACHMENT_FRONT_LEFT
) |
375 (1 << NATIVE_ATTACHMENT_BACK_LEFT
);
377 xconf
->base
.window_bit
= TRUE
;
378 xconf
->base
.pixmap_bit
= TRUE
;
380 xconf
->base
.native_visual_id
= xconf
->visual
->visualid
;
381 #if defined(__cplusplus) || defined(c_plusplus)
382 xconf
->base
.native_visual_type
= xconf
->visual
->c_class
;
384 xconf
->base
.native_visual_type
= xconf
->visual
->class;
387 xconf
->base
.slow_config
= TRUE
;
392 xdpy
->num_configs
= count
;
395 configs
= MALLOC(xdpy
->num_configs
* sizeof(*configs
));
397 for (i
= 0; i
< xdpy
->num_configs
; i
++)
398 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
400 *num_configs
= xdpy
->num_configs
;
406 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
407 EGLNativePixmapType pix
,
408 const struct native_config
*nconf
)
410 struct ximage_display
*xdpy
= ximage_display(ndpy
);
411 enum pipe_format fmt
;
414 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
417 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
420 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
423 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
426 fmt
= PIPE_FORMAT_NONE
;
430 return (fmt
== nconf
->color_format
);
434 ximage_display_get_param(struct native_display
*ndpy
,
435 enum native_param_type param
)
440 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
441 /* private buffers are allocated */
444 case NATIVE_PARAM_PRESERVE_BUFFER
:
445 case NATIVE_PARAM_MAX_SWAP_INTERVAL
:
455 ximage_display_destroy(struct native_display
*ndpy
)
457 struct ximage_display
*xdpy
= ximage_display(ndpy
);
462 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
464 x11_screen_destroy(xdpy
->xscr
);
466 XCloseDisplay(xdpy
->dpy
);
470 struct native_display
*
471 x11_create_ximage_display(Display
*dpy
,
472 struct native_event_handler
*event_handler
,
475 struct ximage_display
*xdpy
;
476 struct sw_winsys
*winsys
= NULL
;
478 xdpy
= CALLOC_STRUCT(ximage_display
);
484 xdpy
->dpy
= XOpenDisplay(NULL
);
489 xdpy
->own_dpy
= TRUE
;
492 xdpy
->event_handler
= event_handler
;
493 xdpy
->base
.user_data
= user_data
;
495 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
496 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
500 winsys
= xlib_create_sw_winsys(xdpy
->dpy
);
505 xdpy
->event_handler
->new_sw_screen(&xdpy
->base
, winsys
);
506 if (!xdpy
->base
.screen
)
509 xdpy
->base
.destroy
= ximage_display_destroy
;
510 xdpy
->base
.get_param
= ximage_display_get_param
;
512 xdpy
->base
.get_configs
= ximage_display_get_configs
;
513 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
514 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
515 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
520 if (winsys
&& winsys
->destroy
)
521 winsys
->destroy(winsys
);
523 x11_screen_destroy(xdpy
->xscr
);
524 if (xdpy
->dpy
&& xdpy
->own_dpy
)
525 XCloseDisplay(xdpy
->dpy
);