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_simple_screen.h"
36 #include "util/u_inlines.h"
37 #include "state_tracker/xlib_sw_winsys.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
,
46 XIMAGE_SURFACE_TYPE_PBUFFER
49 struct ximage_display
{
50 struct native_display base
;
54 struct x11_screen
*xscr
;
57 struct xm_driver
*driver
;
59 struct ximage_config
*configs
;
63 struct ximage_buffer
{
64 struct pipe_texture
*texture
;
65 struct xlib_drawable xdraw
;
68 struct ximage_surface
{
69 struct native_surface base
;
71 enum ximage_surface_type type
;
72 enum pipe_format color_format
;
74 struct ximage_display
*xdpy
;
76 unsigned int server_stamp
;
77 unsigned int client_stamp
;
79 struct ximage_buffer buffers
[NUM_NATIVE_ATTACHMENTS
];
83 struct ximage_config
{
84 struct native_config base
;
85 const XVisualInfo
*visual
;
88 static INLINE
struct ximage_display
*
89 ximage_display(const struct native_display
*ndpy
)
91 return (struct ximage_display
*) ndpy
;
94 static INLINE
struct ximage_surface
*
95 ximage_surface(const struct native_surface
*nsurf
)
97 return (struct ximage_surface
*) nsurf
;
100 static INLINE
struct ximage_config
*
101 ximage_config(const struct native_config
*nconf
)
103 return (struct ximage_config
*) nconf
;
107 ximage_surface_free_buffer(struct native_surface
*nsurf
,
108 enum native_attachment which
)
110 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
111 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
113 pipe_texture_reference(&xbuf
->texture
, NULL
);
117 ximage_surface_alloc_buffer(struct native_surface
*nsurf
,
118 enum native_attachment which
)
120 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
121 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
122 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
123 struct pipe_texture templ
;
127 ximage_surface_free_buffer(&xsurf
->base
, which
);
129 memset(&templ
, 0, sizeof(templ
));
130 templ
.target
= PIPE_TEXTURE_2D
;
131 templ
.format
= xsurf
->color_format
;
132 templ
.width0
= xsurf
->width
;
133 templ
.height0
= xsurf
->height
;
135 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
137 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
139 case NATIVE_ATTACHMENT_FRONT_LEFT
:
140 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
141 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_PRIMARY
;
143 case NATIVE_ATTACHMENT_BACK_LEFT
:
144 case NATIVE_ATTACHMENT_BACK_RIGHT
:
145 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
151 xbuf
->texture
= screen
->texture_create(screen
, &templ
);
153 xbuf
->xdraw
.visual
= xsurf
->visual
.visual
;
154 xbuf
->xdraw
.depth
= xsurf
->visual
.depth
;
155 xbuf
->xdraw
.drawable
= xsurf
->drawable
;
158 /* clean up the buffer if allocation failed */
160 ximage_surface_free_buffer(&xsurf
->base
, which
);
162 return (xbuf
->texture
!= NULL
);
166 * Update the geometry of the surface. Return TRUE if the geometry has changed
170 ximage_surface_update_geometry(struct native_surface
*nsurf
)
172 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
176 unsigned int w
, h
, border
, depth
;
177 boolean updated
= FALSE
;
179 /* pbuffer has fixed geometry */
180 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
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 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
263 assert(xsurf
->drawable
&& xbuf
->texture
);
265 /* what's the cost of surface creation? */
266 psurf
= screen
->get_tex_surface(screen
,
267 xbuf
->texture
, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ
);
271 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
273 pipe_surface_reference(&psurf
, NULL
);
279 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
281 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
284 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
285 NATIVE_ATTACHMENT_FRONT_LEFT
);
286 /* force buffers to be updated in next validation call */
287 xsurf
->server_stamp
++;
288 ximage_surface_notify_invalid(&xsurf
->base
);
294 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
296 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
297 struct ximage_buffer
*xfront
, *xback
, xtmp
;
300 /* display the back buffer first */
301 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
302 NATIVE_ATTACHMENT_BACK_LEFT
);
303 /* force buffers to be updated in next validation call */
304 xsurf
->server_stamp
++;
305 ximage_surface_notify_invalid(&xsurf
->base
);
307 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
308 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
310 /* skip swapping unless there is a front buffer */
311 if (xfront
->texture
) {
321 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
322 unsigned int *seq_num
, struct pipe_texture
**textures
,
323 int *width
, int *height
)
325 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
327 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
328 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
329 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
334 *seq_num
= xsurf
->client_stamp
;
338 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
339 if (native_attachment_mask_test(attachment_mask
, att
)) {
340 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
342 textures
[att
] = NULL
;
343 pipe_texture_reference(&textures
[att
], xbuf
->texture
);
349 *width
= xsurf
->width
;
351 *height
= xsurf
->height
;
357 ximage_surface_wait(struct native_surface
*nsurf
)
359 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
360 XSync(xsurf
->xdpy
->dpy
, FALSE
);
361 /* TODO XGetImage and update the front texture */
365 ximage_surface_destroy(struct native_surface
*nsurf
)
367 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
370 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
371 ximage_surface_free_buffer(&xsurf
->base
, i
);
376 static struct ximage_surface
*
377 ximage_display_create_surface(struct native_display
*ndpy
,
378 enum ximage_surface_type type
,
380 const struct native_config
*nconf
)
382 struct ximage_display
*xdpy
= ximage_display(ndpy
);
383 struct ximage_config
*xconf
= ximage_config(nconf
);
384 struct ximage_surface
*xsurf
;
386 xsurf
= CALLOC_STRUCT(ximage_surface
);
392 xsurf
->color_format
= xconf
->base
.color_format
;
393 xsurf
->drawable
= drawable
;
395 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
396 xsurf
->drawable
= drawable
;
397 xsurf
->visual
= *xconf
->visual
;
398 /* initialize the geometry */
399 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
402 xsurf
->base
.destroy
= ximage_surface_destroy
;
403 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
404 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
405 xsurf
->base
.validate
= ximage_surface_validate
;
406 xsurf
->base
.wait
= ximage_surface_wait
;
411 static struct native_surface
*
412 ximage_display_create_window_surface(struct native_display
*ndpy
,
413 EGLNativeWindowType win
,
414 const struct native_config
*nconf
)
416 struct ximage_surface
*xsurf
;
418 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
419 (Drawable
) win
, nconf
);
420 return (xsurf
) ? &xsurf
->base
: NULL
;
423 static struct native_surface
*
424 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
425 EGLNativePixmapType pix
,
426 const struct native_config
*nconf
)
428 struct ximage_surface
*xsurf
;
430 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
431 (Drawable
) pix
, nconf
);
432 return (xsurf
) ? &xsurf
->base
: NULL
;
435 static struct native_surface
*
436 ximage_display_create_pbuffer_surface(struct native_display
*ndpy
,
437 const struct native_config
*nconf
,
438 uint width
, uint height
)
440 struct ximage_surface
*xsurf
;
442 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PBUFFER
,
443 (Drawable
) None
, nconf
);
445 xsurf
->width
= width
;
446 xsurf
->height
= height
;
448 return (xsurf
) ? &xsurf
->base
: NULL
;
451 static enum pipe_format
452 choose_format(const XVisualInfo
*vinfo
)
454 enum pipe_format fmt
;
455 /* TODO elaborate the formats */
456 switch (vinfo
->depth
) {
458 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
461 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
464 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
467 fmt
= PIPE_FORMAT_NONE
;
474 static const struct native_config
**
475 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
477 struct ximage_display
*xdpy
= ximage_display(ndpy
);
478 const struct native_config
**configs
;
482 if (!xdpy
->configs
) {
483 const XVisualInfo
*visuals
;
484 int num_visuals
, count
, j
;
486 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
491 * Create two configs for each visual.
492 * One with depth/stencil buffer; one without
494 xdpy
->configs
= calloc(num_visuals
* 2, sizeof(*xdpy
->configs
));
499 for (i
= 0; i
< num_visuals
; i
++) {
500 for (j
= 0; j
< 2; j
++) {
501 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
502 __GLcontextModes
*mode
= &xconf
->base
.mode
;
504 xconf
->visual
= &visuals
[i
];
505 xconf
->base
.color_format
= choose_format(xconf
->visual
);
506 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
509 x11_screen_convert_visual(xdpy
->xscr
, xconf
->visual
, mode
);
510 /* support double buffer mode */
511 mode
->doubleBufferMode
= TRUE
;
513 xconf
->base
.depth_format
= PIPE_FORMAT_NONE
;
514 xconf
->base
.stencil_format
= PIPE_FORMAT_NONE
;
515 /* create the second config with depth/stencil buffer */
517 xconf
->base
.depth_format
= PIPE_FORMAT_Z24S8_UNORM
;
518 xconf
->base
.stencil_format
= PIPE_FORMAT_Z24S8_UNORM
;
519 mode
->depthBits
= 24;
520 mode
->stencilBits
= 8;
521 mode
->haveDepthBuffer
= TRUE
;
522 mode
->haveStencilBuffer
= TRUE
;
525 mode
->maxPbufferWidth
= 4096;
526 mode
->maxPbufferHeight
= 4096;
527 mode
->maxPbufferPixels
= 4096 * 4096;
529 GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
530 mode
->swapMethod
= GLX_SWAP_EXCHANGE_OML
;
533 mode
->bindToTextureRgba
= TRUE
;
535 mode
->bindToTextureRgb
= TRUE
;
541 xdpy
->num_configs
= count
;
544 configs
= malloc(xdpy
->num_configs
* sizeof(*configs
));
546 for (i
= 0; i
< xdpy
->num_configs
; i
++)
547 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
549 *num_configs
= xdpy
->num_configs
;
555 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
556 EGLNativePixmapType pix
,
557 const struct native_config
*nconf
)
559 struct ximage_display
*xdpy
= ximage_display(ndpy
);
560 enum pipe_format fmt
;
563 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
566 fmt
= PIPE_FORMAT_B8G8R8A8_UNORM
;
569 fmt
= PIPE_FORMAT_B8G8R8X8_UNORM
;
572 fmt
= PIPE_FORMAT_B5G6R5_UNORM
;
575 fmt
= PIPE_FORMAT_NONE
;
579 return (fmt
== nconf
->color_format
);
583 ximage_display_get_param(struct native_display
*ndpy
,
584 enum native_param_type param
)
589 case NATIVE_PARAM_USE_NATIVE_BUFFER
:
590 /* private buffers are allocated */
602 ximage_display_destroy(struct native_display
*ndpy
)
604 struct ximage_display
*xdpy
= ximage_display(ndpy
);
609 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
611 x11_screen_destroy(xdpy
->xscr
);
613 XCloseDisplay(xdpy
->dpy
);
617 struct native_display
*
618 x11_create_ximage_display(EGLNativeDisplayType dpy
)
620 struct ximage_display
*xdpy
;
622 xdpy
= CALLOC_STRUCT(ximage_display
);
628 xdpy
->dpy
= XOpenDisplay(NULL
);
633 xdpy
->own_dpy
= TRUE
;
636 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
637 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
643 xdpy
->driver
= xlib_sw_winsys_init();
644 xdpy
->base
.screen
= xdpy
->driver
->create_pipe_screen(xdpy
->dpy
);
646 xdpy
->base
.destroy
= ximage_display_destroy
;
647 xdpy
->base
.get_param
= ximage_display_get_param
;
649 xdpy
->base
.get_configs
= ximage_display_get_configs
;
650 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
651 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
652 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
653 xdpy
->base
.create_pbuffer_surface
= ximage_display_create_pbuffer_surface
;