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