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 <X11/extensions/XShm.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_simple_screen.h"
37 #include "util/u_inlines.h"
38 #include "softpipe/sp_winsys.h"
41 #include "sw_winsys.h"
42 #include "native_x11.h"
43 #include "x11_screen.h"
45 enum ximage_surface_type
{
46 XIMAGE_SURFACE_TYPE_WINDOW
,
47 XIMAGE_SURFACE_TYPE_PIXMAP
,
48 XIMAGE_SURFACE_TYPE_PBUFFER
51 struct ximage_display
{
52 struct native_display base
;
56 struct x11_screen
*xscr
;
61 struct pipe_winsys
*winsys
;
62 struct ximage_config
*configs
;
66 struct ximage_buffer
{
69 struct pipe_texture
*texture
;
70 XShmSegmentInfo
*shm_info
;
71 boolean xshm_attached
;
74 struct ximage_surface
{
75 struct native_surface base
;
77 enum ximage_surface_type type
;
78 enum pipe_format color_format
;
80 struct ximage_display
*xdpy
;
84 unsigned int server_stamp
;
85 unsigned int client_stamp
;
87 struct ximage_buffer buffers
[NUM_NATIVE_ATTACHMENTS
];
91 struct ximage_config
{
92 struct native_config base
;
93 const XVisualInfo
*visual
;
96 static INLINE
struct ximage_display
*
97 ximage_display(const struct native_display
*ndpy
)
99 return (struct ximage_display
*) ndpy
;
102 static INLINE
struct ximage_surface
*
103 ximage_surface(const struct native_surface
*nsurf
)
105 return (struct ximage_surface
*) nsurf
;
108 static INLINE
struct ximage_config
*
109 ximage_config(const struct native_config
*nconf
)
111 return (struct ximage_config
*) nconf
;
115 ximage_surface_free_buffer(struct native_surface
*nsurf
,
116 enum native_attachment which
)
118 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
119 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
121 pipe_texture_reference(&xbuf
->texture
, NULL
);
123 if (xbuf
->shm_info
) {
124 if (xbuf
->xshm_attached
)
125 XShmDetach(xsurf
->xdpy
->dpy
, xbuf
->shm_info
);
126 if (xbuf
->shm_info
->shmaddr
!= (void *) -1)
127 shmdt(xbuf
->shm_info
->shmaddr
);
128 if (xbuf
->shm_info
->shmid
!= -1)
129 shmctl(xbuf
->shm_info
->shmid
, IPC_RMID
, 0);
131 xbuf
->shm_info
->shmaddr
= (void *) -1;
132 xbuf
->shm_info
->shmid
= -1;
137 ximage_surface_alloc_buffer(struct native_surface
*nsurf
,
138 enum native_attachment which
)
140 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
141 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
142 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
143 struct pipe_texture templ
;
147 ximage_surface_free_buffer(&xsurf
->base
, which
);
149 memset(&templ
, 0, sizeof(templ
));
150 templ
.target
= PIPE_TEXTURE_2D
;
151 templ
.format
= xsurf
->color_format
;
152 templ
.width0
= xsurf
->width
;
153 templ
.height0
= xsurf
->height
;
155 templ
.tex_usage
= PIPE_TEXTURE_USAGE_RENDER_TARGET
;
157 if (xbuf
->shm_info
) {
158 struct pipe_buffer
*pbuf
;
159 unsigned stride
, size
;
162 stride
= util_format_get_stride(xsurf
->color_format
, xsurf
->width
);
163 /* alignment should depend on visual? */
164 stride
= align(stride
, 4);
165 size
= stride
* xsurf
->height
;
167 /* create and attach shm object */
168 xbuf
->shm_info
->shmid
= shmget(IPC_PRIVATE
, size
, 0755);
169 if (xbuf
->shm_info
->shmid
!= -1) {
170 xbuf
->shm_info
->shmaddr
=
171 shmat(xbuf
->shm_info
->shmid
, NULL
, 0);
172 if (xbuf
->shm_info
->shmaddr
!= (void *) -1) {
173 if (XShmAttach(xsurf
->xdpy
->dpy
, xbuf
->shm_info
)) {
174 addr
= xbuf
->shm_info
->shmaddr
;
175 xbuf
->xshm_attached
= TRUE
;
181 pbuf
= screen
->user_buffer_create(screen
, addr
, size
);
184 screen
->texture_blanket(screen
, &templ
, &stride
, pbuf
);
185 pipe_buffer_reference(&pbuf
, NULL
);
190 xbuf
->texture
= screen
->texture_create(screen
, &templ
);
193 /* clean up the buffer if allocation failed */
195 ximage_surface_free_buffer(&xsurf
->base
, which
);
197 return (xbuf
->texture
!= NULL
);
201 * Update the geometry of the surface. Return TRUE if the geometry has changed
205 ximage_surface_update_geometry(struct native_surface
*nsurf
)
207 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
211 unsigned int w
, h
, border
, depth
;
212 boolean updated
= FALSE
;
214 /* pbuffer has fixed geometry */
215 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
218 ok
= XGetGeometry(xsurf
->xdpy
->dpy
, xsurf
->drawable
,
219 &root
, &x
, &y
, &w
, &h
, &border
, &depth
);
220 if (ok
&& (xsurf
->width
!= w
|| xsurf
->height
!= h
)) {
224 xsurf
->server_stamp
++;
232 * Update the buffers of the surface. It is a slow function due to the
233 * round-trip to the server.
236 ximage_surface_update_buffers(struct native_surface
*nsurf
, uint buffer_mask
)
238 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
243 updated
= ximage_surface_update_geometry(&xsurf
->base
);
245 /* all buffers become invalid */
246 xsurf
->valid_mask
= 0x0;
249 buffer_mask
&= ~xsurf
->valid_mask
;
250 /* all requested buffers are valid */
252 xsurf
->client_stamp
= xsurf
->server_stamp
;
258 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
259 if (native_attachment_mask_test(buffer_mask
, att
)) {
260 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
262 /* reallocate the texture */
263 if (!ximage_surface_alloc_buffer(&xsurf
->base
, att
))
268 xbuf
->ximage
->width
= xsurf
->width
;
269 xbuf
->ximage
->height
= xsurf
->height
;
272 new_valid
|= (1 << att
);
273 if (buffer_mask
== new_valid
)
278 xsurf
->valid_mask
|= new_valid
;
279 xsurf
->client_stamp
= xsurf
->server_stamp
;
281 return (new_valid
== buffer_mask
);
285 ximage_surface_draw_buffer(struct native_surface
*nsurf
,
286 enum native_attachment which
)
288 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
289 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[which
];
290 struct pipe_screen
*screen
= xsurf
->xdpy
->base
.screen
;
291 struct pipe_transfer
*transfer
;
293 if (xsurf
->type
== XIMAGE_SURFACE_TYPE_PBUFFER
)
296 assert(xsurf
->drawable
&& xbuf
->ximage
&& xbuf
->texture
);
298 transfer
= screen
->get_tex_transfer(screen
, xbuf
->texture
,
299 0, 0, 0, PIPE_TRANSFER_READ
, 0, 0, xsurf
->width
, xsurf
->height
);
303 xbuf
->ximage
->bytes_per_line
= transfer
->stride
;
304 xbuf
->ximage
->data
= screen
->transfer_map(screen
, transfer
);
305 if (!xbuf
->ximage
->data
) {
306 screen
->tex_transfer_destroy(transfer
);
312 XShmPutImage(xsurf
->xdpy
->dpy
, xsurf
->drawable
, xsurf
->gc
,
313 xbuf
->ximage
, 0, 0, 0, 0, xsurf
->width
, xsurf
->height
, False
);
315 XPutImage(xsurf
->xdpy
->dpy
, xsurf
->drawable
, xsurf
->gc
,
316 xbuf
->ximage
, 0, 0, 0, 0, xsurf
->width
, xsurf
->height
);
318 xbuf
->ximage
->data
= NULL
;
319 screen
->transfer_unmap(screen
, transfer
);
322 * softpipe allows the pipe transfer to be re-used, but we don't want to
323 * rely on that behavior.
325 screen
->tex_transfer_destroy(transfer
);
327 XSync(xsurf
->xdpy
->dpy
, FALSE
);
333 ximage_surface_flush_frontbuffer(struct native_surface
*nsurf
)
335 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
338 ret
= ximage_surface_draw_buffer(&xsurf
->base
,
339 NATIVE_ATTACHMENT_FRONT_LEFT
);
340 /* force buffers to be updated in next validation call */
341 xsurf
->server_stamp
++;
347 ximage_surface_swap_buffers(struct native_surface
*nsurf
)
349 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
350 struct ximage_buffer
*xfront
, *xback
, xtmp
;
353 /* display the back buffer first */
354 ret
= ximage_surface_draw_buffer(nsurf
, NATIVE_ATTACHMENT_BACK_LEFT
);
355 /* force buffers to be updated in next validation call */
356 xsurf
->server_stamp
++;
358 xfront
= &xsurf
->buffers
[NATIVE_ATTACHMENT_FRONT_LEFT
];
359 xback
= &xsurf
->buffers
[NATIVE_ATTACHMENT_BACK_LEFT
];
361 /* skip swapping so that the front buffer is allocated only when needed */
362 if (!xfront
->texture
)
373 ximage_surface_validate(struct native_surface
*nsurf
, uint attachment_mask
,
374 unsigned int *seq_num
, struct pipe_texture
**textures
,
375 int *width
, int *height
)
377 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
379 if (xsurf
->client_stamp
!= xsurf
->server_stamp
||
380 (xsurf
->valid_mask
& attachment_mask
) != attachment_mask
) {
381 if (!ximage_surface_update_buffers(&xsurf
->base
, attachment_mask
))
386 *seq_num
= xsurf
->client_stamp
;
390 for (att
= 0; att
< NUM_NATIVE_ATTACHMENTS
; att
++) {
391 if (native_attachment_mask_test(attachment_mask
, att
)) {
392 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[att
];
394 textures
[att
] = NULL
;
395 pipe_texture_reference(&textures
[att
], xbuf
->texture
);
401 *width
= xsurf
->width
;
403 *height
= xsurf
->height
;
409 ximage_surface_wait(struct native_surface
*nsurf
)
411 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
412 XSync(xsurf
->xdpy
->dpy
, FALSE
);
413 /* TODO XGetImage and update the front texture */
417 ximage_surface_destroy(struct native_surface
*nsurf
)
419 struct ximage_surface
*xsurf
= ximage_surface(nsurf
);
422 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
423 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[i
];
424 ximage_surface_free_buffer(&xsurf
->base
, i
);
425 /* xbuf->shm_info is owned by xbuf->ximage? */
427 XDestroyImage(xbuf
->ximage
);
432 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
)
433 XFreeGC(xsurf
->xdpy
->dpy
, xsurf
->gc
);
437 static struct ximage_surface
*
438 ximage_display_create_surface(struct native_display
*ndpy
,
439 enum ximage_surface_type type
,
441 const struct native_config
*nconf
)
443 struct ximage_display
*xdpy
= ximage_display(ndpy
);
444 struct ximage_config
*xconf
= ximage_config(nconf
);
445 struct ximage_surface
*xsurf
;
448 xsurf
= CALLOC_STRUCT(ximage_surface
);
454 xsurf
->color_format
= xconf
->base
.color_format
;
455 xsurf
->drawable
= drawable
;
457 if (xsurf
->type
!= XIMAGE_SURFACE_TYPE_PBUFFER
) {
458 xsurf
->drawable
= drawable
;
459 xsurf
->visual
= *xconf
->visual
;
461 xsurf
->gc
= XCreateGC(xdpy
->dpy
, xsurf
->drawable
, 0, NULL
);
467 /* initialize the geometry */
468 ximage_surface_update_buffers(&xsurf
->base
, 0x0);
470 for (i
= 0; i
< NUM_NATIVE_ATTACHMENTS
; i
++) {
471 struct ximage_buffer
*xbuf
= &xsurf
->buffers
[i
];
473 if (xdpy
->use_xshm
) {
474 xbuf
->shm_info
= calloc(1, sizeof(*xbuf
->shm_info
));
475 if (xbuf
->shm_info
) {
476 /* initialize shm info */
477 xbuf
->shm_info
->shmid
= -1;
478 xbuf
->shm_info
->shmaddr
= (void *) -1;
479 xbuf
->shm_info
->readOnly
= TRUE
;
481 xbuf
->ximage
= XShmCreateImage(xsurf
->xdpy
->dpy
,
482 xsurf
->visual
.visual
,
490 xbuf
->ximage
= XCreateImage(xsurf
->xdpy
->dpy
,
491 xsurf
->visual
.visual
,
493 ZPixmap
, 0, /* format, offset */
497 0); /* bytes_per_line */
501 XFreeGC(xdpy
->dpy
, xsurf
->gc
);
508 xsurf
->base
.destroy
= ximage_surface_destroy
;
509 xsurf
->base
.swap_buffers
= ximage_surface_swap_buffers
;
510 xsurf
->base
.flush_frontbuffer
= ximage_surface_flush_frontbuffer
;
511 xsurf
->base
.validate
= ximage_surface_validate
;
512 xsurf
->base
.wait
= ximage_surface_wait
;
517 static struct native_surface
*
518 ximage_display_create_window_surface(struct native_display
*ndpy
,
519 EGLNativeWindowType win
,
520 const struct native_config
*nconf
)
522 struct ximage_surface
*xsurf
;
524 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_WINDOW
,
525 (Drawable
) win
, nconf
);
526 return (xsurf
) ? &xsurf
->base
: NULL
;
529 static struct native_surface
*
530 ximage_display_create_pixmap_surface(struct native_display
*ndpy
,
531 EGLNativePixmapType pix
,
532 const struct native_config
*nconf
)
534 struct ximage_surface
*xsurf
;
536 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PIXMAP
,
537 (Drawable
) pix
, nconf
);
538 return (xsurf
) ? &xsurf
->base
: NULL
;
541 static struct native_surface
*
542 ximage_display_create_pbuffer_surface(struct native_display
*ndpy
,
543 const struct native_config
*nconf
,
544 uint width
, uint height
)
546 struct ximage_surface
*xsurf
;
548 xsurf
= ximage_display_create_surface(ndpy
, XIMAGE_SURFACE_TYPE_PBUFFER
,
549 (Drawable
) None
, nconf
);
551 xsurf
->width
= width
;
552 xsurf
->height
= height
;
554 return (xsurf
) ? &xsurf
->base
: NULL
;
557 static enum pipe_format
558 choose_format(const XVisualInfo
*vinfo
)
560 enum pipe_format fmt
;
561 /* TODO elaborate the formats */
562 switch (vinfo
->depth
) {
564 fmt
= PIPE_FORMAT_A8R8G8B8_UNORM
;
567 fmt
= PIPE_FORMAT_X8R8G8B8_UNORM
;
570 fmt
= PIPE_FORMAT_R5G6B5_UNORM
;
573 fmt
= PIPE_FORMAT_NONE
;
580 static const struct native_config
**
581 ximage_display_get_configs(struct native_display
*ndpy
, int *num_configs
)
583 struct ximage_display
*xdpy
= ximage_display(ndpy
);
584 const struct native_config
**configs
;
588 if (!xdpy
->configs
) {
589 const XVisualInfo
*visuals
;
590 int num_visuals
, count
, j
;
592 visuals
= x11_screen_get_visuals(xdpy
->xscr
, &num_visuals
);
597 * Create two configs for each visual.
598 * One with depth/stencil buffer; one without
600 xdpy
->configs
= calloc(num_visuals
* 2, sizeof(*xdpy
->configs
));
605 for (i
= 0; i
< num_visuals
; i
++) {
606 for (j
= 0; j
< 2; j
++) {
607 struct ximage_config
*xconf
= &xdpy
->configs
[count
];
608 __GLcontextModes
*mode
= &xconf
->base
.mode
;
610 xconf
->visual
= &visuals
[i
];
611 xconf
->base
.color_format
= choose_format(xconf
->visual
);
612 if (xconf
->base
.color_format
== PIPE_FORMAT_NONE
)
615 x11_screen_convert_visual(xdpy
->xscr
, xconf
->visual
, mode
);
616 /* support double buffer mode */
617 mode
->doubleBufferMode
= TRUE
;
619 xconf
->base
.depth_format
= PIPE_FORMAT_NONE
;
620 xconf
->base
.stencil_format
= PIPE_FORMAT_NONE
;
621 /* create the second config with depth/stencil buffer */
623 xconf
->base
.depth_format
= PIPE_FORMAT_S8Z24_UNORM
;
624 xconf
->base
.stencil_format
= PIPE_FORMAT_S8Z24_UNORM
;
625 mode
->depthBits
= 24;
626 mode
->stencilBits
= 8;
627 mode
->haveDepthBuffer
= TRUE
;
628 mode
->haveStencilBuffer
= TRUE
;
631 mode
->maxPbufferWidth
= 4096;
632 mode
->maxPbufferHeight
= 4096;
633 mode
->maxPbufferPixels
= 4096 * 4096;
635 GLX_WINDOW_BIT
| GLX_PIXMAP_BIT
| GLX_PBUFFER_BIT
;
636 mode
->swapMethod
= GLX_SWAP_EXCHANGE_OML
;
639 mode
->bindToTextureRgba
= TRUE
;
641 mode
->bindToTextureRgb
= TRUE
;
647 xdpy
->num_configs
= count
;
650 configs
= malloc(xdpy
->num_configs
* sizeof(*configs
));
652 for (i
= 0; i
< xdpy
->num_configs
; i
++)
653 configs
[i
] = (const struct native_config
*) &xdpy
->configs
[i
];
655 *num_configs
= xdpy
->num_configs
;
661 ximage_display_is_pixmap_supported(struct native_display
*ndpy
,
662 EGLNativePixmapType pix
,
663 const struct native_config
*nconf
)
665 struct ximage_display
*xdpy
= ximage_display(ndpy
);
666 enum pipe_format fmt
;
669 depth
= x11_drawable_get_depth(xdpy
->xscr
, (Drawable
) pix
);
672 fmt
= PIPE_FORMAT_A8R8G8B8_UNORM
;
675 fmt
= PIPE_FORMAT_X8R8G8B8_UNORM
;
678 fmt
= PIPE_FORMAT_R5G6B5_UNORM
;
681 fmt
= PIPE_FORMAT_NONE
;
685 return (fmt
== nconf
->color_format
);
689 ximage_display_destroy(struct native_display
*ndpy
)
691 struct ximage_display
*xdpy
= ximage_display(ndpy
);
696 xdpy
->base
.screen
->destroy(xdpy
->base
.screen
);
699 x11_screen_destroy(xdpy
->xscr
);
701 XCloseDisplay(xdpy
->dpy
);
705 struct native_display
*
706 x11_create_ximage_display(EGLNativeDisplayType dpy
, boolean use_xshm
)
708 struct ximage_display
*xdpy
;
710 xdpy
= CALLOC_STRUCT(ximage_display
);
716 xdpy
->dpy
= XOpenDisplay(NULL
);
721 xdpy
->own_dpy
= TRUE
;
724 xdpy
->xscr_number
= DefaultScreen(xdpy
->dpy
);
725 xdpy
->xscr
= x11_screen_create(xdpy
->dpy
, xdpy
->xscr_number
);
732 (use_xshm
&& x11_screen_support(xdpy
->xscr
, X11_SCREEN_EXTENSION_XSHM
));
734 xdpy
->winsys
= create_sw_winsys();
735 xdpy
->base
.screen
= softpipe_create_screen(xdpy
->winsys
);
737 xdpy
->base
.destroy
= ximage_display_destroy
;
739 xdpy
->base
.get_configs
= ximage_display_get_configs
;
740 xdpy
->base
.is_pixmap_supported
= ximage_display_is_pixmap_supported
;
741 xdpy
->base
.create_window_surface
= ximage_display_create_window_surface
;
742 xdpy
->base
.create_pixmap_surface
= ximage_display_create_pixmap_surface
;
743 xdpy
->base
.create_pbuffer_surface
= ximage_display_create_pbuffer_surface
;