5a424d0ac7f79873034b9d0cf00f8d8ed753bec6
[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 boolean no_swap;
88 };
89
90
91 /**
92 * Derived from softpipe_winsys.
93 * We just need one extra field which indicates the pixel format to use for
94 * drawing surfaces so that we're compatible with the XVisual/window format.
95 */
96 struct xmesa_softpipe_winsys
97 {
98 struct softpipe_winsys spws;
99 enum pipe_format pixelformat;
100 };
101
102
103 struct xmesa_pipe_winsys
104 {
105 struct pipe_winsys base;
106 struct xmesa_visual *xm_visual;
107 int shm;
108 };
109
110
111 static void alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
112 unsigned width, unsigned height);
113
114 /** Cast wrapper */
115 static INLINE struct xmesa_surface *
116 xmesa_surface(struct pipe_surface *ps)
117 {
118 // assert(0);
119 return (struct xmesa_surface *) ps;
120 }
121
122 /** cast wrapper */
123 static INLINE struct xmesa_softpipe_winsys *
124 xmesa_softpipe_winsys(struct softpipe_winsys *spws)
125 {
126 return (struct xmesa_softpipe_winsys *) spws;
127 }
128
129 /**
130 * Turn the softpipe opaque buffer pointer into a dri_bufmgr opaque
131 * buffer pointer...
132 */
133 static INLINE struct xm_buffer *
134 xm_buffer( struct pipe_buffer *buf )
135 {
136 return (struct xm_buffer *)buf;
137 }
138
139
140
141 /* Most callbacks map direcly onto dri_bufmgr operations:
142 */
143 static void *
144 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
145 unsigned flags)
146 {
147 struct xm_buffer *xm_buf = xm_buffer(buf);
148 xm_buf->mapped = xm_buf->data;
149 return xm_buf->mapped;
150 }
151
152 static void
153 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
154 {
155 struct xm_buffer *xm_buf = xm_buffer(buf);
156 xm_buf->mapped = NULL;
157 }
158
159 static void
160 xm_buffer_destroy(struct pipe_winsys *pws,
161 struct pipe_buffer *buf)
162 {
163 struct xm_buffer *oldBuf = xm_buffer(buf);
164
165 if (oldBuf->data) {
166 #if defined(USE_XSHM) && !defined(XFree86Server)
167 if (oldBuf->shminfo.shmid >= 0) {
168 shmdt(oldBuf->shminfo.shmaddr);
169 shmctl(oldBuf->shminfo.shmid, IPC_RMID, 0);
170
171 oldBuf->shminfo.shmid = -1;
172 oldBuf->shminfo.shmaddr = (char *) -1;
173 }
174 else
175 #endif
176 {
177 if (!oldBuf->userBuffer) {
178 align_free(oldBuf->data);
179 }
180 }
181
182 oldBuf->data = NULL;
183 }
184
185 free(oldBuf);
186 }
187
188
189 /**
190 * Display a surface that's in a tiled configuration. That is, all the
191 * pixels for a TILE_SIZExTILE_SIZE block are contiguous in memory.
192 */
193 static void
194 xmesa_display_surface_tiled(XMesaBuffer b, const struct pipe_surface *surf)
195 {
196 XImage *ximage;
197 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
198 const uint tilesPerRow = (surf->width + TILE_SIZE - 1) / TILE_SIZE;
199 uint x, y;
200
201 /* check that the XImage has been previously initialized */
202 assert(ximage->format);
203 assert(ximage->bitmap_unit);
204
205 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
206 alloc_shm_ximage(xm_buf, b, TILE_SIZE, TILE_SIZE);
207 }
208
209 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
210
211 if (!XSHM_ENABLED(xm_buf)) {
212 /* update XImage's fields */
213 ximage->width = TILE_SIZE;
214 ximage->height = TILE_SIZE;
215 ximage->bytes_per_line = TILE_SIZE * 4;
216 }
217
218 for (y = 0; y < surf->height; y += TILE_SIZE) {
219 for (x = 0; x < surf->width; x += TILE_SIZE) {
220 int dx = x;
221 int dy = y;
222 int tx = x / TILE_SIZE;
223 int ty = y / TILE_SIZE;
224 int offset = ty * tilesPerRow + tx;
225
226 offset *= 4 * TILE_SIZE * TILE_SIZE;
227
228 ximage->data = (char *) xm_buf->data + offset;
229
230 if (XSHM_ENABLED(xm_buf)) {
231 #if defined(USE_XSHM) && !defined(XFree86Server)
232 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
233 ximage, 0, 0, x, y, TILE_SIZE, TILE_SIZE, False);
234 #endif
235 } else {
236 XPutImage(b->xm_visual->display, b->drawable, b->gc,
237 ximage, 0, 0, dx, dy, TILE_SIZE, TILE_SIZE);
238 }
239 }
240 }
241 }
242
243
244 /**
245 * Display/copy the image in the surface into the X window specified
246 * by the XMesaBuffer.
247 */
248 void
249 xmesa_display_surface(XMesaBuffer b, const struct pipe_surface *surf)
250 {
251 XImage *ximage;
252 struct xm_buffer *xm_buf = xm_buffer(surf->buffer);
253 const struct xmesa_surface *xm_surf
254 = xmesa_surface((struct pipe_surface *) surf);
255
256 if (xm_surf->no_swap)
257 return;
258
259 if (xm_surf->tileSize) {
260 xmesa_display_surface_tiled(b, surf);
261 return;
262 }
263
264
265 if (XSHM_ENABLED(xm_buf) && (xm_buf->tempImage == NULL)) {
266 alloc_shm_ximage(xm_buf, b, surf->pitch, surf->height);
267 }
268
269 ximage = (XSHM_ENABLED(xm_buf)) ? xm_buf->tempImage : b->tempImage;
270 ximage->data = xm_buf->data;
271
272 /* display image in Window */
273 if (XSHM_ENABLED(xm_buf)) {
274 #if defined(USE_XSHM) && !defined(XFree86Server)
275 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
276 ximage, 0, 0, 0, 0, surf->width, surf->height, False);
277 #endif
278 } else {
279 /* check that the XImage has been previously initialized */
280 assert(ximage->format);
281 assert(ximage->bitmap_unit);
282
283 /* update XImage's fields */
284 ximage->width = surf->width;
285 ximage->height = surf->height;
286 ximage->bytes_per_line = surf->pitch * surf->cpp;
287
288 XPutImage(b->xm_visual->display, b->drawable, b->gc,
289 ximage, 0, 0, 0, 0, surf->width, surf->height);
290 }
291 }
292
293
294 static void
295 xm_flush_frontbuffer(struct pipe_winsys *pws,
296 struct pipe_surface *surf,
297 void *context_private)
298 {
299 /* The Xlib driver's front color surfaces are actually X Windows so
300 * this flush is a no-op.
301 * If we instead did front buffer rendering to a temporary XImage,
302 * this would be the place to copy the Ximage to the on-screen Window.
303 */
304 XMesaContext xmctx = (XMesaContext) context_private;
305 xmesa_display_surface(xmctx->xm_buffer, surf);
306 }
307
308
309
310 static const char *
311 xm_get_name(struct pipe_winsys *pws)
312 {
313 return "Xlib";
314 }
315
316
317 #if defined(USE_XSHM) && !defined(XFree86Server)
318 static volatile int mesaXErrorFlag = 0;
319
320 /**
321 * Catches potential Xlib errors.
322 */
323 static int
324 mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
325 {
326 (void) dpy;
327 (void) event;
328 mesaXErrorFlag = 1;
329 return 0;
330 }
331
332
333 static GLboolean alloc_shm(struct xm_buffer *buf, unsigned size)
334 {
335 XShmSegmentInfo *const shminfo = & buf->shminfo;
336
337 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
338 if (shminfo->shmid < 0) {
339 return GL_FALSE;
340 }
341
342 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
343 if (shminfo->shmaddr == (char *) -1) {
344 shmctl(shminfo->shmid, IPC_RMID, 0);
345 return GL_FALSE;
346 }
347
348 shminfo->readOnly = False;
349 return GL_TRUE;
350 }
351
352
353 /**
354 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
355 */
356 static void
357 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
358 unsigned width, unsigned height)
359 {
360 /*
361 * We have to do a _lot_ of error checking here to be sure we can
362 * really use the XSHM extension. It seems different servers trigger
363 * errors at different points if the extension won't work. Therefore
364 * we have to be very careful...
365 */
366 #if 0
367 GC gc;
368 #endif
369 int (*old_handler)(XMesaDisplay *, XErrorEvent *);
370
371 b->tempImage = XShmCreateImage(xmb->xm_visual->display,
372 xmb->xm_visual->visinfo->visual,
373 xmb->xm_visual->visinfo->depth,
374 ZPixmap,
375 NULL,
376 &b->shminfo,
377 width, height);
378 if (b->tempImage == NULL) {
379 b->shm = 0;
380 return;
381 }
382
383
384 mesaXErrorFlag = 0;
385 old_handler = XSetErrorHandler(mesaHandleXError);
386 /* This may trigger the X protocol error we're ready to catch: */
387 XShmAttach(xmb->xm_visual->display, &b->shminfo);
388 XSync(xmb->xm_visual->display, False);
389
390 if (mesaXErrorFlag) {
391 /* we are on a remote display, this error is normal, don't print it */
392 XFlush(xmb->xm_visual->display);
393 mesaXErrorFlag = 0;
394 XDestroyImage(b->tempImage);
395 b->tempImage = NULL;
396 b->shm = 0;
397 (void) XSetErrorHandler(old_handler);
398 return;
399 }
400
401
402 /* Finally, try an XShmPutImage to be really sure the extension works */
403 #if 0
404 gc = XCreateGC(xmb->xm_visual->display, xmb->drawable, 0, NULL);
405 XShmPutImage(xmb->xm_visual->display, xmb->drawable, gc,
406 b->tempImage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
407 XSync(xmb->xm_visual->display, False);
408 XFreeGC(xmb->xm_visual->display, gc);
409 (void) XSetErrorHandler(old_handler);
410 if (mesaXErrorFlag) {
411 XFlush(xmb->xm_visual->display);
412 mesaXErrorFlag = 0;
413 XDestroyImage(b->tempImage);
414 b->tempImage = NULL;
415 b->shm = 0;
416 return;
417 }
418 #endif
419 }
420 #else
421 static void
422 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
423 unsigned width, unsigned height)
424 {
425 b->shm = 0;
426 }
427 #endif
428
429
430 static struct pipe_buffer *
431 xm_buffer_create(struct pipe_winsys *pws,
432 unsigned alignment,
433 unsigned usage,
434 unsigned size)
435 {
436 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
437 #if defined(USE_XSHM) && !defined(XFree86Server)
438 struct xmesa_pipe_winsys *xpws = (struct xmesa_pipe_winsys *) pws;
439 #endif
440
441 buffer->base.refcount = 1;
442 buffer->base.alignment = alignment;
443 buffer->base.usage = usage;
444 buffer->base.size = size;
445
446
447 #if defined(USE_XSHM) && !defined(XFree86Server)
448 buffer->shminfo.shmid = -1;
449 buffer->shminfo.shmaddr = (char *) -1;
450
451 if (xpws->shm && (usage & PIPE_BUFFER_USAGE_PIXEL) != 0) {
452 buffer->shm = xpws->shm;
453
454 if (alloc_shm(buffer, size)) {
455 buffer->data = buffer->shminfo.shmaddr;
456 }
457 }
458 #endif
459
460 if (buffer->data == NULL) {
461 buffer->shm = 0;
462
463 /* align to 16-byte multiple for Cell */
464 buffer->data = align_malloc(size, max(alignment, 16));
465 }
466
467 return &buffer->base;
468 }
469
470
471 /**
472 * Create buffer which wraps user-space data.
473 */
474 static struct pipe_buffer *
475 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
476 {
477 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
478 buffer->base.refcount = 1;
479 buffer->base.size = bytes;
480 buffer->userBuffer = TRUE;
481 buffer->data = ptr;
482 buffer->shm = 0;
483
484 return &buffer->base;
485 }
486
487
488
489 /**
490 * Round n up to next multiple.
491 */
492 static INLINE unsigned
493 round_up(unsigned n, unsigned multiple)
494 {
495 return (n + multiple - 1) & ~(multiple - 1);
496 }
497
498 static int
499 xm_surface_alloc_storage(struct pipe_winsys *winsys,
500 struct pipe_surface *surf,
501 unsigned width, unsigned height,
502 enum pipe_format format,
503 unsigned flags)
504 {
505 const unsigned alignment = 64;
506
507 surf->width = width;
508 surf->height = height;
509 surf->format = format;
510 surf->cpp = pf_get_size(format);
511 surf->pitch = round_up(width, alignment / surf->cpp);
512
513 #ifdef GALLIUM_CELL /* XXX a bit of a hack */
514 height = round_up(height, TILE_SIZE);
515 #endif
516
517 assert(!surf->buffer);
518 surf->buffer = winsys->buffer_create(winsys, alignment,
519 PIPE_BUFFER_USAGE_PIXEL,
520 surf->pitch * surf->cpp * height);
521 if(!surf->buffer)
522 return -1;
523
524 return 0;
525 }
526
527
528 /**
529 * Called via pipe->surface_alloc() to create new surfaces (textures,
530 * renderbuffers, etc.
531 */
532 static struct pipe_surface *
533 xm_surface_alloc(struct pipe_winsys *ws)
534 {
535 struct xmesa_surface *xms = CALLOC_STRUCT(xmesa_surface);
536 static boolean no_swap = 0;
537 static boolean firsttime = 1;
538
539 if (firsttime) {
540 no_swap = getenv("SP_NO_RAST") != NULL;
541 firsttime = 0;
542 }
543
544 assert(ws);
545
546 xms->surface.refcount = 1;
547 xms->surface.winsys = ws;
548
549 #ifdef GALLIUM_CELL
550 if (!getenv("GALLIUM_NOCELL")) {
551 xms->tileSize = 32; /** probably temporary */
552 }
553 #endif
554
555 xms->no_swap = no_swap;
556
557 return &xms->surface;
558 }
559
560
561
562 static void
563 xm_surface_release(struct pipe_winsys *winsys, struct pipe_surface **s)
564 {
565 struct pipe_surface *surf = *s;
566 surf->refcount--;
567 if (surf->refcount == 0) {
568 if (surf->buffer)
569 pipe_buffer_reference(winsys, &surf->buffer, NULL);
570 free(surf);
571 }
572 *s = NULL;
573 }
574
575
576 /*
577 * Fence functions - basically nothing to do, as we don't create any actual
578 * fence objects.
579 */
580
581 static void
582 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
583 struct pipe_fence_handle *fence)
584 {
585 }
586
587
588 static int
589 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
590 unsigned flag)
591 {
592 return 0;
593 }
594
595
596 static int
597 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
598 unsigned flag)
599 {
600 return 0;
601 }
602
603
604 /**
605 * Return pointer to a pipe_winsys object.
606 * For Xlib, this is a singleton object.
607 * Nothing special for the Xlib driver so no subclassing or anything.
608 */
609 struct pipe_winsys *
610 xmesa_get_pipe_winsys_aub(struct xmesa_visual *xm_vis)
611 {
612 static struct xmesa_pipe_winsys *ws = NULL;
613
614 if (!ws && getenv("XM_AUB")) {
615 ws = (struct xmesa_pipe_winsys *) xmesa_create_pipe_winsys_aub();
616 }
617 else 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 /**
649 * Called via softpipe_winsys->is_format_supported().
650 * This function is only called to test formats for front/back color surfaces.
651 * The winsys being queried will have been created at glXCreateContext
652 * time, with a pixel format corresponding to the context's visual.
653 */
654 static boolean
655 xmesa_is_format_supported(struct softpipe_winsys *sws,
656 enum pipe_format format)
657 {
658 struct xmesa_softpipe_winsys *xmws = xmesa_softpipe_winsys(sws);
659 return (format == xmws->pixelformat);
660 }
661
662
663 /**
664 * Return pointer to a softpipe_winsys object.
665 */
666 static struct softpipe_winsys *
667 xmesa_get_softpipe_winsys(uint pixelformat)
668 {
669 struct xmesa_softpipe_winsys *xmws
670 = CALLOC_STRUCT(xmesa_softpipe_winsys);
671 if (!xmws)
672 return NULL;
673
674 xmws->spws.is_format_supported = xmesa_is_format_supported;
675 xmws->pixelformat = pixelformat;
676
677 return &xmws->spws;
678 }
679
680
681 struct pipe_context *
682 xmesa_create_pipe_context(XMesaContext xmesa, uint pixelformat)
683 {
684 struct pipe_winsys *pws = xmesa_get_pipe_winsys_aub(xmesa->xm_visual);
685 struct pipe_context *pipe;
686
687 #ifdef GALLIUM_CELL
688 if (!getenv("GALLIUM_NOCELL")) {
689 struct cell_winsys *cws = cell_get_winsys(pixelformat);
690 struct pipe_screen *screen = cell_create_screen(pws);
691
692 pipe = cell_create_context(screen, cws);
693 }
694 else
695 #endif
696 {
697 struct softpipe_winsys *spws = xmesa_get_softpipe_winsys(pixelformat);
698 struct pipe_screen *screen = softpipe_create_screen(pws);
699
700 pipe = softpipe_create(screen, pws, spws);
701 }
702
703 if (pipe)
704 pipe->priv = xmesa;
705
706 return pipe;
707 }