Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / winsys / xlib / xlib_softpipe.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 "xm_api.h"
37
38 #undef ASSERT
39 #undef Elements
40
41 #include "pipe/internal/p_winsys_screen.h"
42 #include "pipe/p_format.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_inlines.h"
45 #include "util/u_format.h"
46 #include "util/u_math.h"
47 #include "util/u_memory.h"
48 #include "softpipe/sp_winsys.h"
49 #include "softpipe/sp_texture.h"
50
51 #include "xlib.h"
52
53 /**
54 * Subclass of pipe_buffer for Xlib winsys.
55 * Low-level OS/window system memory buffer
56 */
57 struct xm_buffer
58 {
59 struct pipe_buffer base;
60 boolean userBuffer; /** Is this a user-space buffer? */
61 void *data;
62 void *mapped;
63
64 XImage *tempImage;
65 #ifdef USE_XSHM
66 int shm;
67 XShmSegmentInfo shminfo;
68 #endif
69 };
70
71
72 /**
73 * Subclass of pipe_winsys for Xlib winsys
74 */
75 struct xmesa_pipe_winsys
76 {
77 struct pipe_winsys base;
78 /* struct xmesa_visual *xm_visual; */
79 };
80
81
82
83 /** Cast wrapper */
84 static INLINE struct xm_buffer *
85 xm_buffer( struct pipe_buffer *buf )
86 {
87 return (struct xm_buffer *)buf;
88 }
89
90
91 /**
92 * X Shared Memory Image extension code
93 */
94
95 #ifdef USE_XSHM
96
97 static volatile int mesaXErrorFlag = 0;
98
99 /**
100 * Catches potential Xlib errors.
101 */
102 static int
103 mesaHandleXError(Display *dpy, XErrorEvent *event)
104 {
105 (void) dpy;
106 (void) event;
107 mesaXErrorFlag = 1;
108 return 0;
109 }
110
111
112 static char *alloc_shm(struct xm_buffer *buf, unsigned size)
113 {
114 XShmSegmentInfo *const shminfo = & buf->shminfo;
115
116 shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT|0777);
117 if (shminfo->shmid < 0) {
118 return NULL;
119 }
120
121 shminfo->shmaddr = (char *) shmat(shminfo->shmid, 0, 0);
122 if (shminfo->shmaddr == (char *) -1) {
123 shmctl(shminfo->shmid, IPC_RMID, 0);
124 return NULL;
125 }
126
127 shminfo->readOnly = False;
128 return shminfo->shmaddr;
129 }
130
131
132 /**
133 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
134 */
135 static void
136 alloc_shm_ximage(struct xm_buffer *b, struct xmesa_buffer *xmb,
137 unsigned width, unsigned height)
138 {
139 /*
140 * We have to do a _lot_ of error checking here to be sure we can
141 * really use the XSHM extension. It seems different servers trigger
142 * errors at different points if the extension won't work. Therefore
143 * we have to be very careful...
144 */
145 int (*old_handler)(Display *, XErrorEvent *);
146
147 b->tempImage = XShmCreateImage(xmb->xm_visual->display,
148 xmb->xm_visual->visinfo->visual,
149 xmb->xm_visual->visinfo->depth,
150 ZPixmap,
151 NULL,
152 &b->shminfo,
153 width, height);
154 if (b->tempImage == NULL) {
155 b->shm = 0;
156 return;
157 }
158
159
160 mesaXErrorFlag = 0;
161 old_handler = XSetErrorHandler(mesaHandleXError);
162 /* This may trigger the X protocol error we're ready to catch: */
163 XShmAttach(xmb->xm_visual->display, &b->shminfo);
164 XSync(xmb->xm_visual->display, False);
165
166 if (mesaXErrorFlag) {
167 /* we are on a remote display, this error is normal, don't print it */
168 XFlush(xmb->xm_visual->display);
169 mesaXErrorFlag = 0;
170 XDestroyImage(b->tempImage);
171 b->tempImage = NULL;
172 b->shm = 0;
173 (void) XSetErrorHandler(old_handler);
174 return;
175 }
176
177 b->shm = 1;
178 }
179
180 #endif /* USE_XSHM */
181
182
183
184 /* Most callbacks map direcly onto dri_bufmgr operations:
185 */
186 static void *
187 xm_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf,
188 unsigned flags)
189 {
190 struct xm_buffer *xm_buf = xm_buffer(buf);
191 xm_buf->mapped = xm_buf->data;
192 return xm_buf->mapped;
193 }
194
195 static void
196 xm_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
197 {
198 struct xm_buffer *xm_buf = xm_buffer(buf);
199 xm_buf->mapped = NULL;
200 }
201
202 static void
203 xm_buffer_destroy(struct pipe_buffer *buf)
204 {
205 struct xm_buffer *oldBuf = xm_buffer(buf);
206
207 if (oldBuf->data) {
208 #ifdef USE_XSHM
209 if (oldBuf->shminfo.shmid >= 0) {
210 shmdt(oldBuf->shminfo.shmaddr);
211 shmctl(oldBuf->shminfo.shmid, IPC_RMID, 0);
212
213 oldBuf->shminfo.shmid = -1;
214 oldBuf->shminfo.shmaddr = (char *) -1;
215 }
216 else
217 #endif
218 {
219 if (!oldBuf->userBuffer) {
220 align_free(oldBuf->data);
221 }
222 }
223
224 oldBuf->data = NULL;
225 }
226
227 free(oldBuf);
228 }
229
230
231 /**
232 * Display/copy the image in the surface into the X window specified
233 * by the XMesaBuffer.
234 */
235 static void
236 xlib_softpipe_display_surface(struct xmesa_buffer *b,
237 struct pipe_surface *surf)
238 {
239 XImage *ximage;
240 struct softpipe_texture *spt = softpipe_texture(surf->texture);
241 struct xm_buffer *xm_buf = xm_buffer(spt->buffer);
242 static boolean no_swap = 0;
243 static boolean firsttime = 1;
244
245 if (firsttime) {
246 no_swap = getenv("SP_NO_RAST") != NULL;
247 firsttime = 0;
248 }
249
250 if (no_swap)
251 return;
252
253 #ifdef USE_XSHM
254 if (xm_buf->shm)
255 {
256 if (xm_buf->tempImage == NULL)
257 {
258 assert(util_format_get_blockwidth(surf->texture->format) == 1);
259 assert(util_format_get_blockheight(surf->texture->format) == 1);
260 alloc_shm_ximage(xm_buf, b, spt->stride[surf->level] /
261 util_format_get_blocksize(surf->texture->format), surf->height);
262 }
263
264 ximage = xm_buf->tempImage;
265 ximage->data = xm_buf->data;
266
267 /* _debug_printf("XSHM\n"); */
268 XShmPutImage(b->xm_visual->display, b->drawable, b->gc,
269 ximage, 0, 0, 0, 0, surf->width, surf->height, False);
270 }
271 else
272 #endif
273 {
274 /* display image in Window */
275 ximage = b->tempImage;
276 ximage->data = xm_buf->data;
277
278 /* check that the XImage has been previously initialized */
279 assert(ximage->format);
280 assert(ximage->bitmap_unit);
281
282 /* update XImage's fields */
283 ximage->width = surf->width;
284 ximage->height = surf->height;
285 ximage->bytes_per_line = spt->stride[surf->level];
286
287 /* _debug_printf("XPUT\n"); */
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 /*
300 * The front color buffer is actually just another XImage buffer.
301 * This function copies that XImage to the actual X Window.
302 */
303 XMesaContext xmctx = (XMesaContext) context_private;
304 xlib_softpipe_display_surface(xmctx->xm_buffer, surf);
305 xmesa_check_and_update_buffer_size(xmctx, xmctx->xm_buffer);
306 }
307
308
309
310 static const char *
311 xm_get_name(struct pipe_winsys *pws)
312 {
313 return "Xlib";
314 }
315
316
317 static struct pipe_buffer *
318 xm_buffer_create(struct pipe_winsys *pws,
319 unsigned alignment,
320 unsigned usage,
321 unsigned size)
322 {
323 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
324
325 pipe_reference_init(&buffer->base.reference, 1);
326 buffer->base.alignment = alignment;
327 buffer->base.usage = usage;
328 buffer->base.size = size;
329
330 if (buffer->data == NULL) {
331 /* align to 16-byte multiple for Cell */
332 buffer->data = align_malloc(size, max(alignment, 16));
333 }
334
335 return &buffer->base;
336 }
337
338
339 /**
340 * Create buffer which wraps user-space data.
341 */
342 static struct pipe_buffer *
343 xm_user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
344 {
345 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
346 pipe_reference_init(&buffer->base.reference, 1);
347 buffer->base.size = bytes;
348 buffer->userBuffer = TRUE;
349 buffer->data = ptr;
350
351 return &buffer->base;
352 }
353
354
355 static struct pipe_buffer *
356 xm_surface_buffer_create(struct pipe_winsys *winsys,
357 unsigned width, unsigned height,
358 enum pipe_format format,
359 unsigned usage,
360 unsigned tex_usage,
361 unsigned *stride)
362 {
363 const unsigned alignment = 64;
364 unsigned nblocksy, size;
365
366 nblocksy = util_format_get_nblocksy(format, height);
367 *stride = align(util_format_get_stride(format, width), alignment);
368 size = *stride * nblocksy;
369
370 #ifdef USE_XSHM
371 if (!debug_get_bool_option("XLIB_NO_SHM", FALSE))
372 {
373 struct xm_buffer *buffer = CALLOC_STRUCT(xm_buffer);
374
375 pipe_reference_init(&buffer->base.reference, 1);
376 buffer->base.alignment = alignment;
377 buffer->base.usage = usage;
378 buffer->base.size = size;
379 buffer->userBuffer = FALSE;
380 buffer->shminfo.shmid = -1;
381 buffer->shminfo.shmaddr = (char *) -1;
382 buffer->shm = TRUE;
383
384 buffer->data = alloc_shm(buffer, size);
385 if (!buffer->data)
386 goto out;
387
388 return &buffer->base;
389
390 out:
391 if (buffer)
392 FREE(buffer);
393 }
394 #endif
395
396
397 return winsys->buffer_create(winsys, alignment,
398 usage,
399 size);
400 }
401
402
403 /*
404 * Fence functions - basically nothing to do, as we don't create any actual
405 * fence objects.
406 */
407
408 static void
409 xm_fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
410 struct pipe_fence_handle *fence)
411 {
412 }
413
414
415 static int
416 xm_fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
417 unsigned flag)
418 {
419 return 0;
420 }
421
422
423 static int
424 xm_fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
425 unsigned flag)
426 {
427 return 0;
428 }
429
430
431
432 static struct pipe_winsys *
433 xlib_create_softpipe_winsys( void )
434 {
435 static struct xmesa_pipe_winsys *ws = NULL;
436
437 if (!ws) {
438 ws = CALLOC_STRUCT(xmesa_pipe_winsys);
439
440 /* Fill in this struct with callbacks that pipe will need to
441 * communicate with the window system, buffer manager, etc.
442 */
443 ws->base.buffer_create = xm_buffer_create;
444 ws->base.user_buffer_create = xm_user_buffer_create;
445 ws->base.buffer_map = xm_buffer_map;
446 ws->base.buffer_unmap = xm_buffer_unmap;
447 ws->base.buffer_destroy = xm_buffer_destroy;
448
449 ws->base.surface_buffer_create = xm_surface_buffer_create;
450
451 ws->base.fence_reference = xm_fence_reference;
452 ws->base.fence_signalled = xm_fence_signalled;
453 ws->base.fence_finish = xm_fence_finish;
454
455 ws->base.flush_frontbuffer = xm_flush_frontbuffer;
456 ws->base.get_name = xm_get_name;
457 }
458
459 return &ws->base;
460 }
461
462
463 static struct pipe_screen *
464 xlib_create_softpipe_screen( void )
465 {
466 struct pipe_winsys *winsys;
467 struct pipe_screen *screen;
468
469 winsys = xlib_create_softpipe_winsys();
470 if (winsys == NULL)
471 return NULL;
472
473 screen = softpipe_create_screen(winsys);
474 if (screen == NULL)
475 goto fail;
476
477 return screen;
478
479 fail:
480 if (winsys)
481 winsys->destroy( winsys );
482
483 return NULL;
484 }
485
486
487 static struct pipe_context *
488 xlib_create_softpipe_context( struct pipe_screen *screen,
489 void *context_private )
490 {
491 struct pipe_context *pipe;
492
493 pipe = softpipe_create(screen);
494 if (pipe == NULL)
495 goto fail;
496
497 pipe->priv = context_private;
498 return pipe;
499
500 fail:
501 /* Free stuff here */
502 return NULL;
503 }
504
505 struct xm_driver xlib_softpipe_driver =
506 {
507 .create_pipe_screen = xlib_create_softpipe_screen,
508 .create_pipe_context = xlib_create_softpipe_context,
509 .display_surface = xlib_softpipe_display_surface
510 };
511
512
513