c930a1d1963dc93118aafbdbc6c504a13093ad2a
[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
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"
47
48 #ifdef GALLIUM_CELL
49 #include "cell/ppu/cell_context.h"
50 #include "cell/ppu/cell_screen.h"
51 #include "cell/ppu/cell_winsys.h"
52 #else
53 #define TILE_SIZE 32 /* avoid compilation errors */
54 #endif
55
56 #include "xm_winsys_aub.h"
57
58
59 /**
60 * Low-level OS/window system memory buffer
61 */
62 struct xm_buffer
63 {
64 struct pipe_buffer base;
65 boolean userBuffer; /** Is this a user-space buffer? */
66 void *data;
67 void *mapped;
68
69 XImage *tempImage;
70 int shm;
71 #if defined(USE_XSHM) && !defined(XFree86Server)
72 XShmSegmentInfo shminfo;
73 #endif
74 };
75
76 #if defined(USE_XSHM) && !defined(XFree86Server)
77 # define XSHM_ENABLED(b) ((b)->shm)
78 #else
79 # define XSHM_ENABLED(b) 0
80 #endif
81
82 struct xmesa_surface
83 {
84 struct pipe_surface surface;
85
86 int tileSize;
87 };
88
89
90 /**
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.
94 */
95 struct xmesa_softpipe_winsys
96 {
97 struct softpipe_winsys spws;
98 enum pipe_format pixelformat;
99 };
100
101
102 struct xmesa_pipe_winsys
103 {
104 struct pipe_winsys base;
105 struct xmesa_visual *xm_visual;
106 int shm;
107 };
108
109
110 static void alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
111 unsigned width, unsigned height);
112
113 /** Cast wrapper */
114 static INLINE struct xmesa_surface *
115 xmesa_surface(struct pipe_surface *ps)
116 {
117 // assert(0);
118 return (struct xmesa_surface *) ps;
119 }
120
121 /** cast wrapper */
122 static INLINE struct xmesa_softpipe_winsys *
123 xmesa_softpipe_winsys(struct softpipe_winsys *spws)
124 {
125 return (struct xmesa_softpipe_winsys *) spws;
126 }
127
128 /**
129 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
130 * buffer pointer...
131 */
132 static INLINE struct xm_buffer *
133 xm_buffer( struct pipe_buffer *buf )
134 {
135 return (struct xm_buffer *)buf;
136 }
137
138
139
140 /* Most callbacks map direcly onto dri_bufmgr operations:
141 */
142 static void *
143 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
144 unsigned flags)
145 {
146 struct xm_buffer *xm_buf = xm_buffer(buf);
147 xm_buf->mapped = xm_buf->data;
148 return xm_buf->mapped;
149 }
150
151 static void
152 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
153 {
154 struct xm_buffer *xm_buf = xm_buffer(buf);
155 xm_buf->mapped = NULL;
156 }
157
158 static void
159 xm_buffer_destroy(struct pipe_winsys *pws,
160 struct pipe_buffer *buf)
161 {
162 struct xm_buffer *oldBuf = xm_buffer(buf);
163
164 if (oldBuf->data) {
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);
169
170 oldBuf->shminfo.shmid = -1;
171 oldBuf->shminfo.shmaddr = (char *) -1;
172 }
173 else
174 #endif
175 {
176 if (!oldBuf->userBuffer) {
177 align_free(oldBuf->data);
178 }
179 }
180
181 oldBuf->data = NULL;
182 }
183
184 free(oldBuf);
185 }
186
187
188 /**
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.
191 */
192 static void
193 xmesa_display_surface_tiled(XMesaBuffer b, const struct pipe_surface *surf)
194 {
195 XImage *ximage;
196 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
197 const uint tilesPerRow = (surf->width + TILE_SIZE - 1) / TILE_SIZE;
198 uint x, y;
199
200 /* check that the XImage has been previously initialized */
201 assert(ximage->format);
202 assert(ximage->bitmap_unit);
203
204 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
205 alloc_shm_ximage(xm_buf, b, TILE_SIZE, TILE_SIZE);
206 }
207
208 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
209
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;
215 }
216
217 for (y = 0; y < surf->height; y += TILE_SIZE) {
218 for (x = 0; x < surf->width; x += TILE_SIZE) {
219 int dx = x;
220 int dy = y;
221 int tx = x / TILE_SIZE;
222 int ty = y / TILE_SIZE;
223 int offset = ty * tilesPerRow + tx;
224
225 offset *= 4 * TILE_SIZE * TILE_SIZE;
226
227 ximage->data = (char *) xm_buf->data + offset;
228
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);
233 #endif
234 } else {
235 XPutImage(b->xm_visual->display, b->drawable, b->gc,
236 ximage, 0, 0, dx, dy, TILE_SIZE, TILE_SIZE);
237 }
238 }
239 }
240 }
241
242
243 /**
244 * Display/copy the image in the surface into the X window specified
245 * by the XMesaBuffer.
246 */
247 void
248 xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
249 {
250 XImage *ximage;
251 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
252 const struct xmesa_surface *xm_surf
253 = xmesa_surface((struct pipe_surface *) surf);
254
255 if (xm_surf->tileSize) {
256 xmesa_display_surface_tiled(b, surf);
257 return;
258 }
259
260
261 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
262 alloc_shm_ximage(xm_buf, b, surf->pitch, surf->height);
263 }
264
265 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
266 ximage->data = xm_buf->data;
267
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);
273 #endif
274 } else {
275 /* check that the XImage has been previously initialized */
276 assert(ximage->format);
277 assert(ximage->bitmap_unit);
278
279 /* update XImage's fields */
280 ximage->width = surf->width;
281 ximage->height = surf->height;
282 ximage->bytes_per_line = surf->pitch * surf->cpp;
283
284 XPutImage(b->xm_visual->display, b->drawable, b->gc,
285 ximage, 0, 0, 0, 0, surf->width, surf->height);
286 }
287 }
288
289
290 static void
291 xm_flush_frontbuffer(struct pipe_winsys *pws,
292 struct pipe_surface *surf,
293 void *context_private)
294 {
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.
299 */
300 XMesaContext xmctx = (XMesaContext) context_private;
301 xmesa_display_surface(xmctx->xm_buffer, surf);
302 }
303
304
305
306 static void
307 xm_printf(struct pipe_winsys *pws, const char *fmtString, ...)
308 {
309 va_list args;
310 va_start( args, fmtString );
311 vfprintf(stderr, fmtString, args);
312 va_end( args );
313 }
314
315
316 static const char *
317 xm_get_name(struct pipe_winsys *pws)
318 {
319 return "Xlib";
320 }
321
322
323 #if defined(USE_XSHM) && !defined(XFree86Server)
324 static volatile int mesaXErrorFlag = 0;
325
326 /**
327 * Catches potential Xlib errors.
328 */
329 static int
330 mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
331 {
332 (void) dpy;
333 (void) event;
334 mesaXErrorFlag = 1;
335 return 0;
336 }
337
338
339 static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
340 {
341 XShmSegmentInfo *const shminfo = & buf->shminfo;
342
343 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
344 if (shminfo->shmid < 0) {
345 return GL_FALSE;
346 }
347
348 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
349 if (shminfo->shmaddr == (char *) -1) {
350 shmctl(shminfo->shmid, IPC_RMID, 0);
351 return GL_FALSE;
352 }
353
354 shminfo->readOnly = False;
355 return GL_TRUE;
356 }
357
358
359 /**
360 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
361 */
362 static void
363 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
364 unsigned width, unsigned height)
365 {
366 /*
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...
371 */
372 #if 0
373 GC gc;
374 #endif
375 int (*old_handler)(XMesaDisplay *, XErrorEvent *);
376
377 b->tempImage = XShmCreateImage(xmb->xm_visual->display,
378 xmb->xm_visual->visinfo->visual,
379 xmb->xm_visual->visinfo->depth,
380 ZPixmap,
381 NULL,
382 &b->shminfo,
383 width, height);
384 if (b->tempImage == NULL) {
385 b->shm = 0;
386 return;
387 }
388
389
390 mesaXErrorFlag = 0;
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);
395
396 if (mesaXErrorFlag) {
397 /* we are on a remote display, this error is normal, don't print it */
398 XFlush(xmb->xm_visual->display);
399 mesaXErrorFlag = 0;
400 XDestroyImage(b->tempImage);
401 b->tempImage = NULL;
402 b->shm = 0;
403 (void) XSetErrorHandler(old_handler);
404 return;
405 }
406
407
408 /* Finally, try an XShmPutImage to be really sure the extension works */
409 #if 0
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);
418 mesaXErrorFlag = 0;
419 XDestroyImage(b->tempImage);
420 b->tempImage = NULL;
421 b->shm = 0;
422 return;
423 }
424 #endif
425 }
426 #else
427 static void
428 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
429 unsigned width, unsigned height)
430 {
431 b->shm = 0;
432 }
433 #endif
434
435
436 static struct pipe_buffer *
437 xm_buffer_create(struct pipe_winsys *pws,
438 unsigned alignment,
439 unsigned usage,
440 unsigned size)
441 {
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;
445 #endif
446
447 buffer->base.refcount = 1;
448 buffer->base.alignment = alignment;
449 buffer->base.usage = usage;
450 buffer->base.size = size;
451
452
453 #if defined(USE_XSHM) && !defined(XFree86Server)
454 buffer->shminfo.shmid = -1;
455 buffer->shminfo.shmaddr = (char *) -1;
456
457 if (xpws->shm && (usage & PIPE_BUFFER_USAGE_PIXEL) != 0) {
458 buffer->shm = xpws->shm;
459
460 if (alloc_shm(buffer, size)) {
461 buffer->data = buffer->shminfo.shmaddr;
462 }
463 }
464 #endif
465
466 if (buffer->data == NULL) {
467 buffer->shm = 0;
468
469 /* align to 16-byte multiple for Cell */
470 buffer->data = align_malloc(size, max(alignment, 16));
471 }
472
473 return &buffer->base;
474 }
475
476
477 /**
478 * Create buffer which wraps user-space data.
479 */
480 static struct pipe_buffer *
481 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
482 {
483 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
484 buffer->base.refcount = 1;
485 buffer->base.size = bytes;
486 buffer->userBuffer = TRUE;
487 buffer->data = ptr;
488 buffer->shm = 0;
489
490 return &buffer->base;
491 }
492
493
494
495 /**
496 * Round n up to next multiple.
497 */
498 static INLINE unsigned
499 round_up(unsigned n, unsigned multiple)
500 {
501 return (n + multiple - 1) & ~(multiple - 1);
502 }
503
504 static int
505 xm_surface_alloc_storage(struct pipe_winsys *winsys,
506 struct pipe_surface *surf,
507 unsigned width, unsigned height,
508 enum pipe_format format,
509 unsigned flags)
510 {
511 const unsigned alignment = 64;
512
513 surf->width = width;
514 surf->height = height;
515 surf->format = format;
516 surf->cpp = pf_get_size(format);
517 surf->pitch = round_up(width, alignment / surf->cpp);
518
519 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
520 height = round_up(height, TILE_SIZE);
521 #endif
522
523 assert(!surf->buffer);
524 surf->buffer = winsys->buffer_create(winsys, alignment,
525 PIPE_BUFFER_USAGE_PIXEL,
526 surf->pitch * surf->cpp * height);
527 if(!surf->buffer)
528 return -1;
529
530 return 0;
531 }
532
533
534 /**
535 * Called via pipe->surface_alloc() to create new surfaces (textures,
536 * renderbuffers, etc.
537 */
538 static struct pipe_surface *
539 xm_surface_alloc(struct pipe_winsys *ws)
540 {
541 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
542
543 assert(ws);
544
545 xms->surface.refcount = 1;
546 xms->surface.winsys = ws;
547
548 #ifdef GALLIUM_CELL
549 if (!getenv("GALLIUM_NOCELL")) {
550 xms->tileSize = 32; /** probably temporary */
551 }
552 #endif
553
554 return &xms->surface;
555 }
556
557
558
559 static void
560 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
561 {
562 struct pipe_surface *surf = *s;
563 surf->refcount--;
564 if (surf->refcount == 0) {
565 if (surf->buffer)
566 pipe_buffer_reference(winsys, &surf->buffer, NULL);
567 free(surf);
568 }
569 *s = NULL;
570 }
571
572
573 /*
574 * Fence functions - basically nothing to do, as we don't create any actual
575 * fence objects.
576 */
577
578 static void
579 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
580 struct pipe_fence_handle *fence)
581 {
582 }
583
584
585 static int
586 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
587 unsigned flag)
588 {
589 return 0;
590 }
591
592
593 static int
594 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
595 unsigned flag)
596 {
597 return 0;
598 }
599
600
601 /**
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.
605 */
606 struct pipe_winsys *
607 xmesa_get_pipe_winsys_aub(struct xmesa_visual *xm_vis)
608 {
609 static struct xmesa_pipe_winsys *ws = NULL;
610
611 if (!ws && getenv("XM_AUB")) {
612 ws = (struct xmesa_pipe_winsys *) xmesa_create_pipe_winsys_aub();
613 }
614 else if (!ws) {
615 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
616
617 ws->xm_visual = xm_vis;
618 ws->shm = xmesa_check_for_xshm(xm_vis->display);
619
620 /* Fill in this struct with callbacks that pipe will need to
621 * communicate with the window system, buffer manager, etc.
622 */
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;
628
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;
632
633 ws->fence_reference = xm_fence_reference;
634 ws->fence_signalled = xm_fence_signalled;
635 ws->fence_finish = xm_fence_finish;
636
637 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
638 ws->base.printf = xm_printf;
639 ws->base.get_name = xm_get_name;
640 }
641
642 return &ws->base;
643 }
644
645
646 /**
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.
651 */
652 static boolean
653 xmesa_is_format_supported(struct softpipe_winsys *sws,
654 enum pipe_format format)
655 {
656 struct xmesa_softpipe_winsys *xmws = xmesa_softpipe_winsys(sws);
657 return (format == xmws->pixelformat);
658 }
659
660
661 /**
662 * Return pointer to a softpipe_winsys object.
663 */
664 static struct softpipe_winsys *
665 xmesa_get_softpipe_winsys(uint pixelformat)
666 {
667 struct xmesa_softpipe_winsys *xmws
668 = CALLOC_STRUCT(xmesa_softpipe_winsys);
669 if (!xmws)
670 return NULL;
671
672 xmws->spws.is_format_supported = xmesa_is_format_supported;
673 xmws->pixelformat = pixelformat;
674
675 return &xmws->spws;
676 }
677
678
679 struct pipe_context *
680 xmesa_create_pipe_context(XMesaContext xmesa, uint pixelformat)
681 {
682 struct pipe_winsys *pws = xmesa_get_pipe_winsys_aub(xmesa->xm_visual);
683 struct pipe_context *pipe;
684
685 #ifdef GALLIUM_CELL
686 if (!getenv("GALLIUM_NOCELL")) {
687 struct cell_winsys *cws = cell_get_winsys(pixelformat);
688 struct pipe_screen *screen = cell_create_screen(pws);
689
690 pipe = cell_create_context(screen, cws);
691 }
692 else
693 #endif
694 {
695 struct softpipe_winsys *spws = xmesa_get_softpipe_winsys(pixelformat);
696 struct pipe_screen *screen = softpipe_create_screen(pws);
697
698 pipe = softpipe_create(screen, pws, spws);
699 }
700
701 if (pipe)
702 pipe->priv = xmesa;
703
704 return pipe;
705 }