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
;
78 unsigned int server_stamp
;
79 unsigned int client_stamp
;
81 struct ximage_buffer buffers
[NUM_NATIVE_ATTACHMENTS
];
85 struct ximage_config
{
86 struct native_config base
;
87 const XVisualInfo
*visual
;
90 static INLINE
struct ximage_display
*
91 ximage_display(const struct native_display
*ndpy
)
93 return (struct ximage_display
*) ndpy
;
96 static INLINE
struct ximage_surface
*
97 ximage_surface(const struct native_surface
*nsurf
)
99 return (struct ximage_surface
*) nsurf
;
102 static INLINE
struct ximage_config
*
103 ximage_config(const struct native_config
*nconf
)
105 return (struct ximage_config
*) nconf
;
109 ximage_surface_free_buffer(struct native_surface
*nsurf
,
110 enum native_attachment which
)
112 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
113 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
115 pipe_texture_reference(&xbuf
->texture
, NULL
);
119 ximage_surface_alloc_buffer(struct native_surface
*nsurf
,
120 enum native_attachment which
)
122 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
123 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
124 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
125 struct pipe_texture templ
;
129 ximage_surface_free_buffer(&xsurf
->base
, which
);
131 memset(&templ
, 0, sizeof(templ
));
132 templ
.target
= PIPE_TEXTURE_2D
;
133 templ
.format
= xsurf
->color_format
;
134 templ
.width0
= xsurf
->width
;
135 templ
.height0
= xsurf
->height
;
137 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
139 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
141 case NATIVE_ATTACHMENT_FRONT_LEFT
:
142 case NATIVE_ATTACHMENT_FRONT_RIGHT
:
143 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_PRIMARY
;
145 case NATIVE_ATTACHMENT_BACK_LEFT
:
146 case NATIVE_ATTACHMENT_BACK_RIGHT
:
147 templ
.tex_usage
|= PIPE_TEXTURE_USAGE_DISPLAY_TARGET
;
153 xbuf
->texture
= screen
->texture_create(screen
, &templ
);
155 xbuf
->xdraw
.visual
= xsurf
->visual
.visual
;
156 xbuf
->xdraw
.depth
= xsurf
->visual
.depth
;
157 xbuf
->xdraw
.drawable
= xsurf
->drawable
;
158 xbuf
->xdraw
.gc
= xsurf
->gc
;
161 /* clean up the buffer if allocation failed */
163 ximage_surface_free_buffer(&xsurf
->base
, which
);
165 return (xbuf
->texture
!= NULL
);
169 * Update the geometry of the surface. Return TRUE if the geometry has changed
173 ximage_surface_update_geometry(struct native_surface
*nsurf
)
175 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
179 unsigned int w
, h
, border
, depth
;
180 boolean updated
= FALSE
;
182 /* pbuffer has fixed geometry */
183 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
186 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
187 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
188 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
192 xsurf
->server_stamp
++;
200 ximage_surface_notify_invalid(struct native_surface
*nsurf
)
202 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
203 struct ximage_display
*xdpy
= xsurf
->xdpy
;
205 xdpy
->event_handler
->invalid_surface(&xdpy
->base
,
206 &xsurf
->base
, xsurf
->server_stamp
);
210 * Update the buffers of the surface. It is a slow function due to the
211 * round-trip to the server.
214 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
216 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
221 updated
= ximage_surface_update_geometry(&xsurf
->base
);
223 /* all buffers become invalid */
224 xsurf
->valid_mask
= 0x0;
227 buffer_mask
&= ~xsurf
->valid_mask
;
228 /* all requested buffers are valid */
230 xsurf
->client_stamp
= xsurf
->server_stamp
;
236 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
237 if (native_attachment_mask_test(buffer_mask
, att
)) {
238 /* reallocate the texture */
239 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
242 new_valid
|= (1 << att
);
243 if (buffer_mask
== new_valid
)
248 xsurf
->valid_mask
|= new_valid
;
249 xsurf
->client_stamp
= xsurf
->server_stamp
;
251 return (new_valid
== buffer_mask
);
255 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
256 enum native_attachment which
)
258 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
259 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
260 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
261 struct pipe_surface
*psurf
;
263 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
266 assert(xsurf
->drawable
&& xbuf
->texture
);
268 /* what's the cost of surface creation? */
269 psurf
= screen
->get_tex_surface(screen
,
270 xbuf
->texture
, 0, 0, 0, PIPE_BUFFER_USAGE_CPU_READ
);
274 screen
->flush_frontbuffer(screen
, psurf
, &xbuf
->xdraw
);
276 pipe_surface_reference(&psurf
, NULL
);
282 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
284 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
287 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
288 NATIVE_ATTACHMENT_FRONT_LEFT
);
289 /* force buffers to be updated in next validation call */
290 xsurf
->server_stamp
++;
291 ximage_surface_notify_invalid(&xsurf
->base
);
297 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
299 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
300 struct ximage_buffer
*xfront
, *xback
, xtmp
;
303 /* display the back buffer first */
304 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
305 NATIVE_ATTACHMENT_BACK_LEFT
);
306 /* force buffers to be updated in next validation call */
307 xsurf
->server_stamp
++;
308 ximage_surface_notify_invalid(&xsurf
->base
);
310 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
311 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
313 /* skip swapping unless there is a front buffer */
314 if (xfront
->texture
) {
324 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
325 unsigned int *seq_num
, struct pipe_texture
**textures
,
326 int *width
, int *height
)
328 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
330 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
331 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
332 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
337 *seq_num
= xsurf
->client_stamp
;
341 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
342 if (native_attachment_mask_test(attachment_mask
, att
)) {
343 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
345 textures
[att
] = NULL
;
346 pipe_texture_reference(&textures
[att
], xbuf
->texture
);
352 *width
= xsurf
->width
;
354 *height
= xsurf
->height
;
360 ximage_surface_wait(struct native_surface
*nsurf
)
362 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
363 XSync(xsurf
->xdpy
->dpy
, FALSE
);
364 /* TODO XGetImage and update the front texture */
368 ximage_surface_destroy(struct native_surface
*nsurf
)
370 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
373 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++)
374 ximage_surface_free_buffer(&xsurf
->base
, i
);
376 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
)
377 XFreeGC(xsurf
->xdpy
->dpy
, xsurf
->gc
);
381 static struct ximage_surface
*
382 ximage_display_create_surface(struct native_display
*ndpy
,
383 enum ximage_surface_type type
,
385 const struct native_config
*nconf
)
387 struct ximage_display
*xdpy
= ximage_display(ndpy
);
388 struct ximage_config
*xconf
= ximage_config(nconf
);
389 struct ximage_surface
*xsurf
;
391 xsurf
= CALLOC_STRUCT(ximage_surface
);
397 xsurf
->color_format
= xconf
->base
.color_format
;
398 xsurf
->drawable
= drawable
;
400 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
401 xsurf
->drawable
= drawable
;
402 xsurf
->visual
= *xconf
->visual
;
404 xsurf
->gc
= XCreateGC(xdpy
->dpy
, xsurf
->drawable
, 0, NULL
);
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_Z24S8_UNORM
;
530 xconf
->base
.stencil_format
= PIPE_FORMAT_Z24S8_UNORM
;
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
);
629 struct native_display
*
630 x11_create_ximage_display(EGLNativeDisplayType dpy
)
632 struct ximage_display
*xdpy
;
634 xdpy
= CALLOC_STRUCT(ximage_display
);
640 xdpy
->dpy
= XOpenDisplay(NULL
);
645 xdpy
->own_dpy
= TRUE
;
648 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
649 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
655 xdpy
->driver
= xlib_sw_winsys_init();
656 xdpy
->base
.screen
= xdpy
->driver
->create_pipe_screen(xdpy
->dpy
);
658 xdpy
->base
.destroy
= ximage_display_destroy
;
659 xdpy
->base
.get_param
= ximage_display_get_param
;
661 xdpy
->base
.get_configs
= ximage_display_get_configs
;
662 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
663 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
664 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
665 xdpy
->base
.create_pbuffer_surface
= ximage_display_create_pbuffer_surface
;