1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
27 **************************************************************************/
36 #include "glxheader.h"
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_util.h"
45 #include "pipe/p_inlines.h"
46 #include "softpipe/sp_winsys.h"
49 #include "cell/ppu/cell_context.h"
50 #include "cell/ppu/cell_screen.h"
51 #include "cell/ppu/cell_winsys.h"
53 #define TILE_SIZE 32 /* avoid compilation errors */
56 #include "xm_winsys_aub.h"
60 * Low-level OS/window system memory buffer
64 struct pipe_buffer base
;
65 boolean userBuffer
; /** Is this a user-space buffer? */
71 #if defined(USE_XSHM) && !defined(XFree86Server)
72 XShmSegmentInfo shminfo
;
76 #if defined(USE_XSHM) && !defined(XFree86Server)
77 # define XSHM_ENABLED(b) ((b)->shm)
79 # define XSHM_ENABLED(b) 0
84 struct pipe_surface surface
;
91 * Derived from softpipe_winsys.
92 * We just need one extra field which indicates the pixel format to use for
93 * drawing surfaces so that we're compatible with the XVisual/window format.
95 struct xmesa_softpipe_winsys
97 struct softpipe_winsys spws
;
98 enum pipe_format pixelformat
;
102 struct xmesa_pipe_winsys
104 struct pipe_winsys base
;
105 struct xmesa_visual
*xm_visual
;
110 static void alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
111 unsigned width
, unsigned height
);
114 static INLINE
struct xmesa_surface
*
115 xmesa_surface(struct pipe_surface
*ps
)
118 return (struct xmesa_surface
*) ps
;
122 static INLINE
struct xmesa_softpipe_winsys
*
123 xmesa_softpipe_winsys(struct softpipe_winsys
*spws
)
125 return (struct xmesa_softpipe_winsys
*) spws
;
129 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
132 static INLINE
struct xm_buffer
*
133 xm_buffer( struct pipe_buffer
*buf
)
135 return (struct xm_buffer
*)buf
;
140 /* Most callbacks map direcly onto dri_bufmgr operations:
143 xm_buffer_map(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
,
146 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
147 xm_buf
->mapped
= xm_buf
->data
;
148 return xm_buf
->mapped
;
152 xm_buffer_unmap(struct pipe_winsys
*pws
, struct pipe_buffer
*buf
)
154 struct xm_buffer
*xm_buf
= xm_buffer(buf
);
155 xm_buf
->mapped
= NULL
;
159 xm_buffer_destroy(struct pipe_winsys
*pws
,
160 struct pipe_buffer
*buf
)
162 struct xm_buffer
*oldBuf
= xm_buffer(buf
);
165 #if defined(USE_XSHM) && !defined(XFree86Server)
166 if (oldBuf
->shminfo
.shmid
>= 0) {
167 shmdt(oldBuf
->shminfo
.shmaddr
);
168 shmctl(oldBuf
->shminfo
.shmid
, IPC_RMID
, 0);
170 oldBuf
->shminfo
.shmid
= -1;
171 oldBuf
->shminfo
.shmaddr
= (char *) -1;
176 if (!oldBuf
->userBuffer
) {
177 align_free(oldBuf
->data
);
189 * Display a surface that's in a tiled configuration. That is, all the
190 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
193 xmesa_display_surface_tiled(XMesaBuffer b
, const struct pipe_surface
*surf
)
196 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
197 const uint tilesPerRow
= (surf
->width
+ TILE_SIZE
- 1) / TILE_SIZE
;
200 /* check that the XImage has been previously initialized */
201 assert(ximage
->format
);
202 assert(ximage
->bitmap_unit
);
204 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
205 alloc_shm_ximage(xm_buf
, b
, TILE_SIZE
, TILE_SIZE
);
208 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
210 if (!XSHM_ENABLED(xm_buf
)) {
211 /* update XImage's fields */
212 ximage
->width
= TILE_SIZE
;
213 ximage
->height
= TILE_SIZE
;
214 ximage
->bytes_per_line
= TILE_SIZE
* 4;
217 for (y
= 0; y
< surf
->height
; y
+= TILE_SIZE
) {
218 for (x
= 0; x
< surf
->width
; x
+= TILE_SIZE
) {
221 int tx
= x
/ TILE_SIZE
;
222 int ty
= y
/ TILE_SIZE
;
223 int offset
= ty
* tilesPerRow
+ tx
;
225 offset
*= 4 * TILE_SIZE
* TILE_SIZE
;
227 ximage
->data
= (char *) xm_buf
->data
+ offset
;
229 if (XSHM_ENABLED(xm_buf
)) {
230 #if defined(USE_XSHM) && !defined(XFree86Server)
231 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
232 ximage
, 0, 0, x
, y
, TILE_SIZE
, TILE_SIZE
, False
);
235 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
236 ximage
, 0, 0, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
244 * Display/copy the image in the surface into the X window specified
245 * by the XMesaBuffer.
248 xmesa_display_surface(XMesaBuffer b
, const struct pipe_surface
*surf
)
251 struct xm_buffer
*xm_buf
= xm_buffer(surf
->buffer
);
252 const struct xmesa_surface
*xm_surf
253 = xmesa_surface((struct pipe_surface
*) surf
);
255 if (xm_surf
->tileSize
) {
256 xmesa_display_surface_tiled(b
, surf
);
261 if (XSHM_ENABLED(xm_buf
) && (xm_buf
->tempImage
== NULL
)) {
262 alloc_shm_ximage(xm_buf
, b
, surf
->pitch
, surf
->height
);
265 ximage
= (XSHM_ENABLED(xm_buf
)) ? xm_buf
->tempImage
: b
->tempImage
;
266 ximage
->data
= xm_buf
->data
;
268 /* display image in Window */
269 if (XSHM_ENABLED(xm_buf
)) {
270 #if defined(USE_XSHM) && !defined(XFree86Server)
271 XShmPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
272 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
, False
);
275 /* check that the XImage has been previously initialized */
276 assert(ximage
->format
);
277 assert(ximage
->bitmap_unit
);
279 /* update XImage's fields */
280 ximage
->width
= surf
->width
;
281 ximage
->height
= surf
->height
;
282 ximage
->bytes_per_line
= surf
->pitch
* surf
->cpp
;
284 XPutImage(b
->xm_visual
->display
, b
->drawable
, b
->gc
,
285 ximage
, 0, 0, 0, 0, surf
->width
, surf
->height
);
291 xm_flush_frontbuffer(struct pipe_winsys
*pws
,
292 struct pipe_surface
*surf
,
293 void *context_private
)
295 /* The Xlib driver's front color surfaces are actually X Windows so
296 * this flush is a no-op.
297 * If we instead did front buffer rendering to a temporary XImage,
298 * this would be the place to copy the Ximage to the on-screen Window.
300 XMesaContext xmctx
= (XMesaContext
) context_private
;
301 xmesa_display_surface(xmctx
->xm_buffer
, surf
);
307 xm_printf(struct pipe_winsys
*pws
, const char *fmtString
, ...)
310 va_start( args
, fmtString
);
311 vfprintf(stderr
, fmtString
, args
);
317 xm_get_name(struct pipe_winsys
*pws
)
323 #if defined(USE_XSHM) && !defined(XFree86Server)
324 static volatile int mesaXErrorFlag
= 0;
327 * Catches potential Xlib errors.
330 mesaHandleXError(XMesaDisplay
*dpy
, XErrorEvent
*event
)
339 static GLboolean
alloc_shm(struct xm_buffer
*buf
, unsigned size
)
341 XShmSegmentInfo
*const shminfo
= & buf
->shminfo
;
343 shminfo
->shmid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
|0777);
344 if (shminfo
->shmid
< 0) {
348 shminfo
->shmaddr
= (char *) shmat(shminfo
->shmid
, 0, 0);
349 if (shminfo
->shmaddr
== (char *) -1) {
350 shmctl(shminfo
->shmid
, IPC_RMID
, 0);
354 shminfo
->readOnly
= False
;
360 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
363 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
364 unsigned width
, unsigned height
)
367 * We have to do a _lot_ of error checking here to be sure we can
368 * really use the XSHM extension. It seems different servers trigger
369 * errors at different points if the extension won't work. Therefore
370 * we have to be very careful...
375 int (*old_handler
)(XMesaDisplay
*, XErrorEvent
*);
377 b
->tempImage
= XShmCreateImage(xmb
->xm_visual
->display
,
378 xmb
->xm_visual
->visinfo
->visual
,
379 xmb
->xm_visual
->visinfo
->depth
,
384 if (b
->tempImage
== NULL
) {
391 old_handler
= XSetErrorHandler(mesaHandleXError
);
392 /* This may trigger the X protocol error we're ready to catch: */
393 XShmAttach(xmb
->xm_visual
->display
, &b
->shminfo
);
394 XSync(xmb
->xm_visual
->display
, False
);
396 if (mesaXErrorFlag
) {
397 /* we are on a remote display, this error is normal, don't print it */
398 XFlush(xmb
->xm_visual
->display
);
400 XDestroyImage(b
->tempImage
);
403 (void) XSetErrorHandler(old_handler
);
408 /* Finally, try an XShmPutImage to be really sure the extension works */
410 gc
= XCreateGC(xmb
->xm_visual
->display
, xmb
->drawable
, 0, NULL
);
411 XShmPutImage(xmb
->xm_visual
->display
, xmb
->drawable
, gc
,
412 b
->tempImage
, 0, 0, 0, 0, 1, 1 /*one pixel*/, False
);
413 XSync(xmb
->xm_visual
->display
, False
);
414 XFreeGC(xmb
->xm_visual
->display
, gc
);
415 (void) XSetErrorHandler(old_handler
);
416 if (mesaXErrorFlag
) {
417 XFlush(xmb
->xm_visual
->display
);
419 XDestroyImage(b
->tempImage
);
428 alloc_shm_ximage(struct xm_buffer
*b
, struct xmesa_buffer
*xmb
,
429 unsigned width
, unsigned height
)
436 static struct pipe_buffer
*
437 xm_buffer_create(struct pipe_winsys
*pws
,
442 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
443 #if defined(USE_XSHM) && !defined(XFree86Server)
444 struct xmesa_pipe_winsys
*xpws
= (struct xmesa_pipe_winsys
*) pws
;
447 buffer
->base
.refcount
= 1;
448 buffer
->base
.alignment
= alignment
;
449 buffer
->base
.usage
= usage
;
450 buffer
->base
.size
= size
;
453 #if defined(USE_XSHM) && !defined(XFree86Server)
454 buffer
->shminfo
.shmid
= -1;
455 buffer
->shminfo
.shmaddr
= (char *) -1;
457 if (xpws
->shm
&& (usage
& PIPE_BUFFER_USAGE_PIXEL
) != 0) {
458 buffer
->shm
= xpws
->shm
;
460 if (alloc_shm(buffer
, size
)) {
461 buffer
->data
= buffer
->shminfo
.shmaddr
;
466 if (buffer
->data
== NULL
) {
469 /* align to 16-byte multiple for Cell */
470 buffer
->data
= align_malloc(size
, max(alignment
, 16));
473 return &buffer
->base
;
478 * Create buffer which wraps user-space data.
480 static struct pipe_buffer
*
481 xm_user_buffer_create(struct pipe_winsys
*pws
, void *ptr
, unsigned bytes
)
483 struct xm_buffer
*buffer
= CALLOC_STRUCT(xm_buffer
);
484 buffer
->base
.refcount
= 1;
485 buffer
->base
.size
= bytes
;
486 buffer
->userBuffer
= TRUE
;
490 return &buffer
->base
;
496 * Round n up to next multiple.
498 static INLINE
unsigned
499 round_up(unsigned n
, unsigned multiple
)
501 return (n
+ multiple
- 1) & ~(multiple
- 1);
505 xm_surface_alloc_storage(struct pipe_winsys
*winsys
,
506 struct pipe_surface
*surf
,
507 unsigned width
, unsigned height
,
508 enum pipe_format format
,
511 const unsigned alignment
= 64;
514 surf
->height
= height
;
515 surf
->format
= format
;
516 surf
->cpp
= pf_get_size(format
);
517 surf
->pitch
= round_up(width
, alignment
/ surf
->cpp
);
519 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
520 height
= round_up(height
, TILE_SIZE
);
523 assert(!surf
->buffer
);
524 surf
->buffer
= winsys
->buffer_create(winsys
, alignment
,
525 PIPE_BUFFER_USAGE_PIXEL
,
526 surf
->pitch
* surf
->cpp
* height
);
535 * Called via pipe->surface_alloc() to create new surfaces (textures,
536 * renderbuffers, etc.
538 static struct pipe_surface
*
539 xm_surface_alloc(struct pipe_winsys
*ws
)
541 struct xmesa_surface
*xms
= CALLOC_STRUCT(xmesa_surface
);
545 xms
->surface
.refcount
= 1;
546 xms
->surface
.winsys
= ws
;
549 if (!getenv("GALLIUM_NOCELL")) {
550 xms
->tileSize
= 32; /** probably temporary */
554 return &xms
->surface
;
560 xm_surface_release(struct pipe_winsys
*winsys
, struct pipe_surface
**s
)
562 struct pipe_surface
*surf
= *s
;
564 if (surf
->refcount
== 0) {
566 pipe_buffer_reference(winsys
, &surf
->buffer
, NULL
);
574 * Fence functions - basically nothing to do, as we don't create any actual
579 xm_fence_reference(struct pipe_winsys
*sws
, struct pipe_fence_handle
**ptr
,
580 struct pipe_fence_handle
*fence
)
586 xm_fence_signalled(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
594 xm_fence_finish(struct pipe_winsys
*sws
, struct pipe_fence_handle
*fence
,
602 * Return pointer to a pipe_winsys object.
603 * For Xlib, this is a singleton object.
604 * Nothing special for the Xlib driver so no subclassing or anything.
607 xmesa_get_pipe_winsys_aub(struct xmesa_visual
*xm_vis
)
609 static struct xmesa_pipe_winsys
*ws
= NULL
;
611 if (!ws
&& getenv("XM_AUB")) {
612 ws
= (struct xmesa_pipe_winsys
*) xmesa_create_pipe_winsys_aub();
615 ws
= CALLOC_STRUCT(xmesa_pipe_winsys
);
617 ws
->xm_visual
= xm_vis
;
618 ws
->shm
= xmesa_check_for_xshm(xm_vis
->display
);
620 /* Fill in this struct with callbacks that pipe will need to
621 * communicate with the window system, buffer manager, etc.
623 ws
->base
.buffer_create
= xm_buffer_create
;
624 ws
->base
.user_buffer_create
= xm_user_buffer_create
;
625 ws
->base
.buffer_map
= xm_buffer_map
;
626 ws
->base
.buffer_unmap
= xm_buffer_unmap
;
627 ws
->base
.buffer_destroy
= xm_buffer_destroy
;
629 ws
->base
.surface_alloc
= xm_surface_alloc
;
630 ws
->base
.surface_alloc_storage
= xm_surface_alloc_storage
;
631 ws
->base
.surface_release
= xm_surface_release
;
633 ws
->fence_reference
= xm_fence_reference
;
634 ws
->fence_signalled
= xm_fence_signalled
;
635 ws
->fence_finish
= xm_fence_finish
;
637 ws
->base
.flush_frontbuffer
= xm_flush_frontbuffer
;
638 ws
->base
.printf
= xm_printf
;
639 ws
->base
.get_name
= xm_get_name
;
647 * Called via softpipe_winsys->is_format_supported().
648 * This function is only called to test formats for front/back color surfaces.
649 * The winsys being queried will have been created at glXCreateContext
650 * time, with a pixel format corresponding to the context's visual.
653 xmesa_is_format_supported(struct softpipe_winsys
*sws
,
654 enum pipe_format format
)
656 struct xmesa_softpipe_winsys
*xmws
= xmesa_softpipe_winsys(sws
);
657 return (format
== xmws
->pixelformat
);
662 * Return pointer to a softpipe_winsys object.
664 static struct softpipe_winsys
*
665 xmesa_get_softpipe_winsys(uint pixelformat
)
667 struct xmesa_softpipe_winsys
*xmws
668 = CALLOC_STRUCT(xmesa_softpipe_winsys
);
672 xmws
->spws
.is_format_supported
= xmesa_is_format_supported
;
673 xmws
->pixelformat
= pixelformat
;
679 struct pipe_context
*
680 xmesa_create_pipe_context(XMesaContext xmesa
, uint pixelformat
)
682 struct pipe_winsys
*pws
= xmesa_get_pipe_winsys_aub(xmesa
->xm_visual
);
683 struct pipe_context
*pipe
;
686 if (!getenv("GALLIUM_NOCELL")) {
687 struct cell_winsys
*cws
= cell_get_winsys(pixelformat
);
688 struct pipe_screen
*screen
= cell_create_screen(pws
);
690 pipe
= cell_create_context(screen
, cws
);
695 struct softpipe_winsys
*spws
= xmesa_get_softpipe_winsys(pixelformat
);
696 struct pipe_screen
*screen
= softpipe_create_screen(pws
);
698 pipe
= softpipe_create(screen
, pws
, spws
);