Merge commit 'origin/gallium-0.1' into gallium-tex-surfaces
[mesa.git] / src / gallium / winsys / xlib / xm_winsys.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /*
30 * Authors:
31 * Keith Whitwell
32 * Brian Paul
33 */
34
35
36 #include "glxheader.h"
37 #include "xmesaP.h"
38
39 #undef ASSERT
40 #undef Elements
41
42 #include "pipe/p_winsys.h"
43 #include "pipe/p_format.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_util.h"
46 #include "pipe/p_inlines.h"
47 #include "softpipe/sp_winsys.h"
48
49 #ifdef GALLIUM_CELL
50 #include "cell/ppu/cell_context.h"
51 #include "cell/ppu/cell_screen.h"
52 #include "cell/ppu/cell_winsys.h"
53 #else
54 #define TILE_SIZE 32 /* avoid compilation errors */
55 #endif
56
57 #include "xm_winsys_aub.h"
58
59
60 /**
61 * Subclass of pipe_buffer for Xlib winsys.
62 * Low-level OS/window system memory buffer
63 */
64 struct xm_buffer
65 {
66 struct pipe_buffer base;
67 boolean userBuffer; /** Is this a user-space buffer? */
68 void *data;
69 void *mapped;
70
71 XImage *tempImage;
72 int shm;
73 #if defined(USE_XSHM) && !defined(XFree86Server)
74 XShmSegmentInfo shminfo;
75 #endif
76 };
77
78
79 /**
80 * Subclass of pipe_surface for Xlib winsys
81 */
82 struct xmesa_surface
83 {
84 struct pipe_surface surface;
85
86 int tileSize;
87 boolean no_swap;
88 };
89
90
91 /**
92 * Subclass of pipe_winsys for Xlib winsys
93 */
94 struct xmesa_pipe_winsys
95 {
96 struct pipe_winsys base;
97 struct xmesa_visual *xm_visual;
98 int shm;
99 };
100
101
102
103 /** Cast wrapper */
104 static INLINE struct xmesa_surface *
105 xmesa_surface(struct pipe_surface *ps)
106 {
107 return (struct xmesa_surface *) ps;
108 }
109
110
111 /** Cast wrapper */
112 static INLINE struct xm_buffer *
113 xm_buffer( struct pipe_buffer *buf )
114 {
115 return (struct xm_buffer *)buf;
116 }
117
118
119 /**
120 * X Shared Memory Image extension code
121 */
122 #if defined(USE_XSHM) && !defined(XFree86Server)
123
124 #define XSHM_ENABLED(b) ((b)->shm)
125
126 static volatile int mesaXErrorFlag = 0;
127
128 /**
129 * Catches potential Xlib errors.
130 */
131 static int
132 mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
133 {
134 (void) dpy;
135 (void) event;
136 mesaXErrorFlag = 1;
137 return 0;
138 }
139
140
141 static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
142 {
143 XShmSegmentInfo *const shminfo = & buf->shminfo;
144
145 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
146 if (shminfo->shmid < 0) {
147 return GL_FALSE;
148 }
149
150 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
151 if (shminfo->shmaddr == (char *) -1) {
152 shmctl(shminfo->shmid, IPC_RMID, 0);
153 return GL_FALSE;
154 }
155
156 shminfo->readOnly = False;
157 return GL_TRUE;
158 }
159
160
161 /**
162 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
163 */
164 static void
165 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
166 unsigned width, unsigned height)
167 {
168 /*
169 * We have to do a _lot_ of error checking here to be sure we can
170 * really use the XSHM extension. It seems different servers trigger
171 * errors at different points if the extension won't work. Therefore
172 * we have to be very careful...
173 */
174 #if 0
175 GC gc;
176 #endif
177 int (*old_handler)(XMesaDisplay *, XErrorEvent *);
178
179 b->tempImage = XShmCreateImage(xmb->xm_visual->display,
180 xmb->xm_visual->visinfo->visual,
181 xmb->xm_visual->visinfo->depth,
182 ZPixmap,
183 NULL,
184 &b->shminfo,
185 width, height);
186 if (b->tempImage == NULL) {
187 b->shm = 0;
188 return;
189 }
190
191
192 mesaXErrorFlag = 0;
193 old_handler = XSetErrorHandler(mesaHandleXError);
194 /* This may trigger the X protocol error we're ready to catch: */
195 XShmAttach(xmb->xm_visual->display, &b->shminfo);
196 XSync(xmb->xm_visual->display, False);
197
198 if (mesaXErrorFlag) {
199 /* we are on a remote display, this error is normal, don't print it */
200 XFlush(xmb->xm_visual->display);
201 mesaXErrorFlag = 0;
202 XDestroyImage(b->tempImage);
203 b->tempImage = NULL;
204 b->shm = 0;
205 (void) XSetErrorHandler(old_handler);
206 return;
207 }
208
209
210 /* Finally, try an XShmPutImage to be really sure the extension works */
211 #if 0
212 gc = XCreateGC(xmb->xm_visual->display, xmb->drawable, 0, NULL);
213 XShmPutImage(xmb->xm_visual->display, xmb->drawable, gc,
214 b->tempImage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
215 XSync(xmb->xm_visual->display, False);
216 XFreeGC(xmb->xm_visual->display, gc);
217 (void) XSetErrorHandler(old_handler);
218 if (mesaXErrorFlag) {
219 XFlush(xmb->xm_visual->display);
220 mesaXErrorFlag = 0;
221 XDestroyImage(b->tempImage);
222 b->tempImage = NULL;
223 b->shm = 0;
224 return;
225 }
226 #endif
227 }
228
229 #else
230
231 #define XSHM_ENABLED(b) 0
232
233 static void
234 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
235 unsigned width, unsigned height)
236 {
237 b->shm = 0;
238 }
239 #endif /* USE_XSHM */
240
241
242
243
244 /* Most callbacks map direcly onto dri_bufmgr operations:
245 */
246 static void *
247 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
248 unsigned flags)
249 {
250 struct xm_buffer *xm_buf = xm_buffer(buf);
251 xm_buf->mapped = xm_buf->data;
252 return xm_buf->mapped;
253 }
254
255 static void
256 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
257 {
258 struct xm_buffer *xm_buf = xm_buffer(buf);
259 xm_buf->mapped = NULL;
260 }
261
262 static void
263 xm_buffer_destroy(struct pipe_winsys *pws,
264 struct pipe_buffer *buf)
265 {
266 struct xm_buffer *oldBuf = xm_buffer(buf);
267
268 if (oldBuf->data) {
269 #if defined(USE_XSHM) && !defined(XFree86Server)
270 if (oldBuf->shminfo.shmid >= 0) {
271 shmdt(oldBuf->shminfo.shmaddr);
272 shmctl(oldBuf->shminfo.shmid, IPC_RMID, 0);
273
274 oldBuf->shminfo.shmid = -1;
275 oldBuf->shminfo.shmaddr = (char *) -1;
276 }
277 else
278 #endif
279 {
280 if (!oldBuf->userBuffer) {
281 align_free(oldBuf->data);
282 }
283 }
284
285 oldBuf->data = NULL;
286 }
287
288 free(oldBuf);
289 }
290
291
292 /**
293 * Display a surface that's in a tiled configuration. That is, all the
294 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
295 */
296 static void
297 xmesa_display_surface_tiled(XMesaBuffer b, const struct pipe_surface *surf)
298 {
299 XImage *ximage;
300 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
301 const uint tilesPerRow = (surf->width + TILE_SIZE - 1) / TILE_SIZE;
302 uint x, y;
303
304 /* check that the XImage has been previously initialized */
305 assert(ximage->format);
306 assert(ximage->bitmap_unit);
307
308 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
309 alloc_shm_ximage(xm_buf, b, TILE_SIZE, TILE_SIZE);
310 }
311
312 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
313
314 if (!XSHM_ENABLED(xm_buf)) {
315 /* update XImage's fields */
316 ximage->width = TILE_SIZE;
317 ximage->height = TILE_SIZE;
318 ximage->bytes_per_line = TILE_SIZE * 4;
319 }
320
321 for (y = 0; y < surf->height; y += TILE_SIZE) {
322 for (x = 0; x < surf->width; x += TILE_SIZE) {
323 int dx = x;
324 int dy = y;
325 int tx = x / TILE_SIZE;
326 int ty = y / TILE_SIZE;
327 int offset = ty * tilesPerRow + tx;
328
329 offset *= 4 * TILE_SIZE * TILE_SIZE;
330
331 ximage->data = (char *) xm_buf->data + offset;
332
333 if (XSHM_ENABLED(xm_buf)) {
334 #if defined(USE_XSHM) && !defined(XFree86Server)
335 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
336 ximage, 0, 0, x, y, TILE_SIZE, TILE_SIZE, False);
337 #endif
338 } else {
339 XPutImage(b->xm_visual->display, b->drawable, b->gc,
340 ximage, 0, 0, dx, dy, TILE_SIZE, TILE_SIZE);
341 }
342 }
343 }
344 }
345
346
347 /**
348 * Display/copy the image in the surface into the X window specified
349 * by the XMesaBuffer.
350 */
351 void
352 xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
353 {
354 XImage *ximage;
355 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
356 const struct xmesa_surface *xm_surf
357 = xmesa_surface((struct pipe_surface *) surf);
358
359 if (xm_surf->no_swap)
360 return;
361
362 if (xm_surf->tileSize) {
363 xmesa_display_surface_tiled(b, surf);
364 return;
365 }
366
367
368 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
369 alloc_shm_ximage(xm_buf, b, surf->pitch, surf->height);
370 }
371
372 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
373 ximage->data = xm_buf->data;
374
375 /* display image in Window */
376 if (XSHM_ENABLED(xm_buf)) {
377 #if defined(USE_XSHM) && !defined(XFree86Server)
378 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
379 ximage, 0, 0, 0, 0, surf->width, surf->height, False);
380 #endif
381 } else {
382 /* check that the XImage has been previously initialized */
383 assert(ximage->format);
384 assert(ximage->bitmap_unit);
385
386 /* update XImage's fields */
387 ximage->width = surf->width;
388 ximage->height = surf->height;
389 ximage->bytes_per_line = surf->pitch * surf->cpp;
390
391 XPutImage(b->xm_visual->display, b->drawable, b->gc,
392 ximage, 0, 0, 0, 0, surf->width, surf->height);
393 }
394 }
395
396
397 static void
398 xm_flush_frontbuffer(struct pipe_winsys *pws,
399 struct pipe_surface *surf,
400 void *context_private)
401 {
402 /*
403 * The front color buffer is actually just another XImage buffer.
404 * This function copies that XImage to the actual X Window.
405 */
406 XMesaContext xmctx = (XMesaContext) context_private;
407 xmesa_display_surface(xmctx->xm_buffer, surf);
408 }
409
410
411
412 static const char *
413 xm_get_name(struct pipe_winsys *pws)
414 {
415 return "Xlib";
416 }
417
418
419 static struct pipe_buffer *
420 xm_buffer_create(struct pipe_winsys *pws,
421 unsigned alignment,
422 unsigned usage,
423 unsigned size)
424 {
425 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
426 #if defined(USE_XSHM) && !defined(XFree86Server)
427 struct xmesa_pipe_winsys *xpws = (struct xmesa_pipe_winsys *) pws;
428 #endif
429
430 buffer->base.refcount = 1;
431 buffer->base.alignment = alignment;
432 buffer->base.usage = usage;
433 buffer->base.size = size;
434
435
436 #if defined(USE_XSHM) && !defined(XFree86Server)
437 buffer->shminfo.shmid = -1;
438 buffer->shminfo.shmaddr = (char *) -1;
439
440 if (xpws->shm && (usage & PIPE_BUFFER_USAGE_PIXEL) != 0) {
441 buffer->shm = xpws->shm;
442
443 if (alloc_shm(buffer, size)) {
444 buffer->data = buffer->shminfo.shmaddr;
445 }
446 }
447 #endif
448
449 if (buffer->data == NULL) {
450 buffer->shm = 0;
451
452 /* align to 16-byte multiple for Cell */
453 buffer->data = align_malloc(size, max(alignment, 16));
454 }
455
456 return &buffer->base;
457 }
458
459
460 /**
461 * Create buffer which wraps user-space data.
462 */
463 static struct pipe_buffer *
464 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
465 {
466 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
467 buffer->base.refcount = 1;
468 buffer->base.size = bytes;
469 buffer->userBuffer = TRUE;
470 buffer->data = ptr;
471 buffer->shm = 0;
472
473 return &buffer->base;
474 }
475
476
477
478 /**
479 * Round n up to next multiple.
480 */
481 static INLINE unsigned
482 round_up(unsigned n, unsigned multiple)
483 {
484 return (n + multiple - 1) & ~(multiple - 1);
485 }
486
487 static int
488 xm_surface_alloc_storage(struct pipe_winsys *winsys,
489 struct pipe_surface *surf,
490 unsigned width, unsigned height,
491 enum pipe_format format,
492 unsigned flags,
493 unsigned tex_usage)
494 {
495 const unsigned alignment = 64;
496
497 surf->width = width;
498 surf->height = height;
499 surf->format = format;
500 surf->cpp = pf_get_size(format);
501 surf->pitch = round_up(width, alignment / surf->cpp);
502 surf->usage = flags;
503
504 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
505 height = round_up(height, TILE_SIZE);
506 #endif
507
508 assert(!surf->buffer);
509 surf->buffer = winsys->buffer_create(winsys, alignment,
510 PIPE_BUFFER_USAGE_PIXEL,
511 surf->pitch * surf->cpp * height);
512 if(!surf->buffer)
513 return -1;
514
515 return 0;
516 }
517
518
519 /**
520 * Called via winsys->surface_alloc() to create new surfaces.
521 */
522 static struct pipe_surface *
523 xm_surface_alloc(struct pipe_winsys *ws)
524 {
525 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
526 static boolean no_swap = 0;
527 static boolean firsttime = 1;
528
529 if (firsttime) {
530 no_swap = getenv("SP_NO_RAST") != NULL;
531 firsttime = 0;
532 }
533
534 assert(ws);
535
536 xms->surface.refcount = 1;
537 xms->surface.winsys = ws;
538
539 #ifdef GALLIUM_CELL
540 if (!getenv("GALLIUM_NOCELL")) {
541 xms->tileSize = 32; /** probably temporary */
542 }
543 #endif
544
545 xms->no_swap = no_swap;
546
547 return &xms->surface;
548 }
549
550
551
552 static void
553 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
554 {
555 struct pipe_surface *surf = *s;
556 assert(!surf->texture);
557 surf->refcount--;
558 if (surf->refcount == 0) {
559 if (surf->buffer)
560 pipe_buffer_reference(winsys, &surf->buffer, NULL);
561 free(surf);
562 }
563 *s = NULL;
564 }
565
566
567 /*
568 * Fence functions - basically nothing to do, as we don't create any actual
569 * fence objects.
570 */
571
572 static void
573 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
574 struct pipe_fence_handle *fence)
575 {
576 }
577
578
579 static int
580 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
581 unsigned flag)
582 {
583 return 0;
584 }
585
586
587 static int
588 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
589 unsigned flag)
590 {
591 return 0;
592 }
593
594
595 /**
596 * Return pointer to a pipe_winsys object.
597 * For Xlib, this is a singleton object.
598 * Nothing special for the Xlib driver so no subclassing or anything.
599 */
600 struct pipe_winsys *
601 xmesa_get_pipe_winsys_aub(struct xmesa_visual *xm_vis)
602 {
603 static struct xmesa_pipe_winsys *ws = NULL;
604
605 if (!ws) {
606 ws = (struct xmesa_pipe_winsys *) xmesa_create_pipe_winsys_aub();
607 }
608 return &ws->base;
609 }
610
611
612 static struct pipe_winsys *
613 xmesa_get_pipe_winsys(struct xmesa_visual *xm_vis)
614 {
615 static struct xmesa_pipe_winsys *ws = NULL;
616
617 if (!ws) {
618 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
619
620 ws->xm_visual = xm_vis;
621 ws->shm = xmesa_check_for_xshm(xm_vis->display);
622
623 /* Fill in this struct with callbacks that pipe will need to
624 * communicate with the window system, buffer manager, etc.
625 */
626 ws->base.buffer_create = xm_buffer_create;
627 ws->base.user_buffer_create = xm_user_buffer_create;
628 ws->base.buffer_map = xm_buffer_map;
629 ws->base.buffer_unmap = xm_buffer_unmap;
630 ws->base.buffer_destroy = xm_buffer_destroy;
631
632 ws->base.surface_alloc = xm_surface_alloc;
633 ws->base.surface_alloc_storage = xm_surface_alloc_storage;
634 ws->base.surface_release = xm_surface_release;
635
636 ws->base.fence_reference = xm_fence_reference;
637 ws->base.fence_signalled = xm_fence_signalled;
638 ws->base.fence_finish = xm_fence_finish;
639
640 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
641 ws->base.get_name = xm_get_name;
642 }
643
644 return &ws->base;
645 }
646
647
648 struct pipe_context *
649 xmesa_create_pipe_context(XMesaContext xmesa, uint pixelformat)
650 {
651 struct pipe_winsys *pws;
652 struct pipe_context *pipe;
653
654 if (getenv("XM_AUB")) {
655 pws = xmesa_get_pipe_winsys_aub(xmesa->xm_visual);
656 }
657 else {
658 pws = xmesa_get_pipe_winsys(xmesa->xm_visual);
659 }
660
661 #ifdef GALLIUM_CELL
662 if (!getenv("GALLIUM_NOCELL")) {
663 struct cell_winsys *cws = cell_get_winsys(pixelformat);
664 struct pipe_screen *screen = cell_create_screen(pws);
665
666 pipe = cell_create_context(screen, cws);
667 }
668 else
669 #endif
670 {
671 struct pipe_screen *screen = softpipe_create_screen(pws);
672
673 pipe = softpipe_create(screen, pws, NULL);
674 }
675
676 if (pipe)
677 pipe->priv = xmesa;
678
679 return pipe;
680 }