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