merge from master
[mesa.git] / src / mesa / drivers / x11 / xm_buffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file xm_buffer.h
28 * Framebuffer and renderbuffer-related functions.
29 */
30
31
32 #include "glxheader.h"
33 #include "GL/xmesa.h"
34 #include "xmesaP.h"
35 #include "imports.h"
36 #include "framebuffer.h"
37 #include "renderbuffer.h"
38
39
40 #ifndef XFree86Server
41 static volatile int mesaXErrorFlag = 0;
42
43 /**
44 * Catches potential Xlib errors.
45 */
46 static int
47 mesaHandleXError(XMesaDisplay *dpy, XErrorEvent *event)
48 {
49 (void) dpy;
50 (void) event;
51 mesaXErrorFlag = 1;
52 return 0;
53 }
54 #endif
55
56
57 /**
58 * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
59 * Return: GL_TRUE if success, GL_FALSE if error
60 */
61 #ifndef XFree86Server
62 static GLboolean
63 alloc_back_shm_ximage(XMesaBuffer b, GLuint width, GLuint height)
64 {
65 #ifdef USE_XSHM
66 /*
67 * We have to do a _lot_ of error checking here to be sure we can
68 * really use the XSHM extension. It seems different servers trigger
69 * errors at different points if the extension won't work. Therefore
70 * we have to be very careful...
71 */
72 GC gc;
73 int (*old_handler)(XMesaDisplay *, XErrorEvent *);
74
75 if (width == 0 || height == 0) {
76 /* this will be true the first time we're called on 'b' */
77 return GL_FALSE;
78 }
79
80 b->backxrb->ximage = XShmCreateImage(b->xm_visual->display,
81 b->xm_visual->visinfo->visual,
82 b->xm_visual->visinfo->depth,
83 ZPixmap, NULL, &b->shminfo,
84 width, height);
85 if (b->backxrb->ximage == NULL) {
86 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.\n");
87 b->shm = 0;
88 return GL_FALSE;
89 }
90
91 b->shminfo.shmid = shmget(IPC_PRIVATE, b->backxrb->ximage->bytes_per_line
92 * b->backxrb->ximage->height, IPC_CREAT|0777);
93 if (b->shminfo.shmid < 0) {
94 _mesa_warning(NULL, "shmget failed while allocating back buffer.\n");
95 XDestroyImage(b->backxrb->ximage);
96 b->backxrb->ximage = NULL;
97 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmget), disabling.\n");
98 b->shm = 0;
99 return GL_FALSE;
100 }
101
102 b->shminfo.shmaddr = b->backxrb->ximage->data
103 = (char*)shmat(b->shminfo.shmid, 0, 0);
104 if (b->shminfo.shmaddr == (char *) -1) {
105 _mesa_warning(NULL, "shmat() failed while allocating back buffer.\n");
106 XDestroyImage(b->backxrb->ximage);
107 shmctl(b->shminfo.shmid, IPC_RMID, 0);
108 b->backxrb->ximage = NULL;
109 _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (shmat), disabling.\n");
110 b->shm = 0;
111 return GL_FALSE;
112 }
113
114 b->shminfo.readOnly = False;
115 mesaXErrorFlag = 0;
116 old_handler = XSetErrorHandler(mesaHandleXError);
117 /* This may trigger the X protocol error we're ready to catch: */
118 XShmAttach(b->xm_visual->display, &b->shminfo);
119 XSync(b->xm_visual->display, False);
120
121 if (mesaXErrorFlag) {
122 /* we are on a remote display, this error is normal, don't print it */
123 XFlush(b->xm_visual->display);
124 mesaXErrorFlag = 0;
125 XDestroyImage(b->backxrb->ximage);
126 shmdt(b->shminfo.shmaddr);
127 shmctl(b->shminfo.shmid, IPC_RMID, 0);
128 b->backxrb->ximage = NULL;
129 b->shm = 0;
130 (void) XSetErrorHandler(old_handler);
131 return GL_FALSE;
132 }
133
134 shmctl(b->shminfo.shmid, IPC_RMID, 0); /* nobody else needs it */
135
136 /* Finally, try an XShmPutImage to be really sure the extension works */
137 gc = XCreateGC(b->xm_visual->display, b->frontxrb->drawable, 0, NULL);
138 XShmPutImage(b->xm_visual->display, b->frontxrb->drawable, gc,
139 b->backxrb->ximage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False);
140 XSync(b->xm_visual->display, False);
141 XFreeGC(b->xm_visual->display, gc);
142 (void) XSetErrorHandler(old_handler);
143 if (mesaXErrorFlag) {
144 XFlush(b->xm_visual->display);
145 mesaXErrorFlag = 0;
146 XDestroyImage(b->backxrb->ximage);
147 shmdt(b->shminfo.shmaddr);
148 shmctl(b->shminfo.shmid, IPC_RMID, 0);
149 b->backxrb->ximage = NULL;
150 b->shm = 0;
151 return GL_FALSE;
152 }
153
154 return GL_TRUE;
155 #else
156 /* Can't compile XSHM support */
157 return GL_FALSE;
158 #endif
159 }
160 #endif
161
162
163
164 /**
165 * Setup an off-screen pixmap or Ximage to use as the back buffer.
166 * Input: b - the X/Mesa buffer
167 */
168 static void
169 alloc_back_buffer(XMesaBuffer b, GLuint width, GLuint height)
170 {
171 if (b->db_mode == BACK_XIMAGE) {
172 /* Deallocate the old backxrb->ximage, if any */
173 if (b->backxrb->ximage) {
174 #if defined(USE_XSHM) && !defined(XFree86Server)
175 if (b->shm) {
176 XShmDetach(b->xm_visual->display, &b->shminfo);
177 XDestroyImage(b->backxrb->ximage);
178 shmdt(b->shminfo.shmaddr);
179 }
180 else
181 #endif
182 XMesaDestroyImage(b->backxrb->ximage);
183 b->backxrb->ximage = NULL;
184 }
185
186 if (width == 0 || height == 0)
187 return;
188
189 /* Allocate new back buffer */
190 #ifdef XFree86Server
191 /* Allocate a regular XImage for the back buffer. */
192 b->backxrb->ximage = XMesaCreateImage(b->xm_visual->BitsPerPixel,
193 width, height, NULL);
194 {
195 #else
196 if (b->shm == 0 || !alloc_back_shm_ximage(b, width, height)) {
197 /* Allocate a regular XImage for the back buffer. */
198 b->backxrb->ximage = XCreateImage(b->xm_visual->display,
199 b->xm_visual->visinfo->visual,
200 GET_VISUAL_DEPTH(b->xm_visual),
201 ZPixmap, 0, /* format, offset */
202 NULL,
203 width, height,
204 8, 0); /* pad, bytes_per_line */
205 #endif
206 if (!b->backxrb->ximage) {
207 _mesa_warning(NULL, "alloc_back_buffer: XCreateImage failed.\n");
208 return;
209 }
210 b->backxrb->ximage->data = (char *) MALLOC(b->backxrb->ximage->height
211 * b->backxrb->ximage->bytes_per_line);
212 if (!b->backxrb->ximage->data) {
213 _mesa_warning(NULL, "alloc_back_buffer: MALLOC failed.\n");
214 XMesaDestroyImage(b->backxrb->ximage);
215 b->backxrb->ximage = NULL;
216 }
217 }
218 b->backxrb->pixmap = None;
219 }
220 else if (b->db_mode == BACK_PIXMAP) {
221 /* Free the old back pixmap */
222 if (b->backxrb->pixmap) {
223 XMesaFreePixmap(b->xm_visual->display, b->backxrb->pixmap);
224 b->backxrb->pixmap = 0;
225 }
226
227 if (width > 0 && height > 0) {
228 /* Allocate new back pixmap */
229 b->backxrb->pixmap = XMesaCreatePixmap(b->xm_visual->display,
230 b->frontxrb->drawable,
231 width, height,
232 GET_VISUAL_DEPTH(b->xm_visual));
233 }
234
235 b->backxrb->ximage = NULL;
236 }
237 }
238
239
240 static void
241 xmesa_delete_renderbuffer(struct gl_renderbuffer *rb)
242 {
243 /* XXX Note: the ximage or Pixmap attached to this renderbuffer
244 * should probably get freed here, but that's currently done in
245 * XMesaDestroyBuffer().
246 */
247 _mesa_free(rb);
248 }
249
250
251 /**
252 * Reallocate renderbuffer storage for front color buffer.
253 * Called via gl_renderbuffer::AllocStorage()
254 */
255 static GLboolean
256 xmesa_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
257 GLenum internalFormat, GLuint width, GLuint height)
258 {
259 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
260
261 /* just clear these to be sure we don't accidentally use them */
262 xrb->origin1 = NULL;
263 xrb->origin2 = NULL;
264 xrb->origin3 = NULL;
265 xrb->origin4 = NULL;
266
267 /* for the FLIP macro: */
268 xrb->bottom = height - 1;
269
270 rb->Width = width;
271 rb->Height = height;
272 rb->InternalFormat = internalFormat;
273
274 return GL_TRUE;
275 }
276
277
278 /**
279 * Reallocate renderbuffer storage for back color buffer.
280 * Called via gl_renderbuffer::AllocStorage()
281 */
282 static GLboolean
283 xmesa_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
284 GLenum internalFormat, GLuint width, GLuint height)
285 {
286 struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
287
288 /* reallocate the back buffer XImage or Pixmap */
289 assert(xrb->Parent);
290 alloc_back_buffer(xrb->Parent, width, height);
291
292 /* same as front buffer */
293 /* XXX why is this here? */
294 (void) xmesa_alloc_front_storage(ctx, rb, internalFormat, width, height);
295
296 /* plus... */
297 if (xrb->ximage) {
298 /* Needed by PIXELADDR1 macro */
299 xrb->width1 = xrb->ximage->bytes_per_line;
300 xrb->origin1 = (GLubyte *) xrb->ximage->data + xrb->width1 * (height - 1);
301
302 /* Needed by PIXELADDR2 macro */
303 xrb->width2 = xrb->ximage->bytes_per_line / 2;
304 xrb->origin2 = (GLushort *) xrb->ximage->data + xrb->width2 * (height - 1);
305
306 /* Needed by PIXELADDR3 macro */
307 xrb->width3 = xrb->ximage->bytes_per_line;
308 xrb->origin3 = (GLubyte *) xrb->ximage->data + xrb->width3 * (height - 1);
309
310 /* Needed by PIXELADDR4 macro */
311 xrb->width4 = xrb->ximage->width;
312 xrb->origin4 = (GLuint *) xrb->ximage->data + xrb->width4 * (height - 1);
313 }
314 else {
315 /* out of memory or buffer size is 0 x 0 */
316 xrb->width1 = xrb->width2 = xrb->width3 = xrb->width4 = 0;
317 xrb->origin1 = NULL;
318 xrb->origin2 = NULL;
319 xrb->origin3 = NULL;
320 xrb->origin4 = NULL;
321 }
322
323 return GL_TRUE;
324 }
325
326
327 struct xmesa_renderbuffer *
328 xmesa_new_renderbuffer(GLcontext *ctx, GLuint name, const GLvisual *visual,
329 GLboolean backBuffer)
330 {
331 struct xmesa_renderbuffer *xrb = CALLOC_STRUCT(xmesa_renderbuffer);
332 if (xrb) {
333 GLuint name = 0;
334 _mesa_init_renderbuffer(&xrb->Base, name);
335
336 xrb->Base.Delete = xmesa_delete_renderbuffer;
337 if (backBuffer)
338 xrb->Base.AllocStorage = xmesa_alloc_back_storage;
339 else
340 xrb->Base.AllocStorage = xmesa_alloc_front_storage;
341
342 if (visual->rgbMode) {
343 xrb->Base.InternalFormat = GL_RGBA;
344 xrb->Base._BaseFormat = GL_RGBA;
345 xrb->Base.DataType = GL_UNSIGNED_BYTE;
346 xrb->Base.RedBits = visual->redBits;
347 xrb->Base.GreenBits = visual->greenBits;
348 xrb->Base.BlueBits = visual->blueBits;
349 xrb->Base.AlphaBits = visual->alphaBits;
350 }
351 else {
352 xrb->Base.InternalFormat = GL_COLOR_INDEX;
353 xrb->Base._BaseFormat = GL_COLOR_INDEX;
354 xrb->Base.DataType = GL_UNSIGNED_INT;
355 xrb->Base.IndexBits = visual->indexBits;
356 }
357 /* only need to set Red/Green/EtcBits fields for user-created RBs */
358 }
359 return xrb;
360 }
361
362
363 /**
364 * Called via gl_framebuffer::Delete() method when this buffer
365 * is _really_ being deleted.
366 */
367 void
368 xmesa_delete_framebuffer(struct gl_framebuffer *fb)
369 {
370 XMesaBuffer b = XMESA_BUFFER(fb);
371
372 if (b->num_alloced > 0) {
373 /* If no other buffer uses this X colormap then free the colors. */
374 if (!xmesa_find_buffer(b->display, b->cmap, b)) {
375 #ifdef XFree86Server
376 int client = 0;
377 if (b->frontxrb->drawable)
378 client = CLIENT_ID(b->frontxrb->drawable->id);
379 (void)FreeColors(b->cmap, client,
380 b->num_alloced, b->alloced_colors, 0);
381 #else
382 XFreeColors(b->display, b->cmap,
383 b->alloced_colors, b->num_alloced, 0);
384 #endif
385 }
386 }
387
388 if (b->gc)
389 XMesaFreeGC(b->xm_visual->display, b->gc);
390 if (b->cleargc)
391 XMesaFreeGC(b->xm_visual->display, b->cleargc);
392 if (b->swapgc)
393 XMesaFreeGC(b->xm_visual->display, b->swapgc);
394
395 if (b->xm_visual->mesa_visual.doubleBufferMode) {
396 /* free back ximage/pixmap/shmregion */
397 if (b->backxrb->ximage) {
398 #if defined(USE_XSHM) && !defined(XFree86Server)
399 if (b->shm) {
400 XShmDetach( b->xm_visual->display, &b->shminfo );
401 XDestroyImage( b->backxrb->ximage );
402 shmdt( b->shminfo.shmaddr );
403 }
404 else
405 #endif
406 XMesaDestroyImage( b->backxrb->ximage );
407 b->backxrb->ximage = NULL;
408 }
409 if (b->backxrb->pixmap) {
410 XMesaFreePixmap( b->xm_visual->display, b->backxrb->pixmap );
411 if (b->xm_visual->hpcr_clear_flag) {
412 XMesaFreePixmap( b->xm_visual->display,
413 b->xm_visual->hpcr_clear_pixmap );
414 XMesaDestroyImage( b->xm_visual->hpcr_clear_ximage );
415 }
416 }
417 }
418
419 if (b->rowimage) {
420 _mesa_free( b->rowimage->data );
421 b->rowimage->data = NULL;
422 XMesaDestroyImage( b->rowimage );
423 }
424
425 /* Note that XMesaBuffer renderbuffers normally have a refcount of 2
426 * (creation + binding) so we need to explicitly delete/unbind them here.
427 */
428 if (b->frontxrb) {
429 _mesa_unreference_renderbuffer((struct gl_renderbuffer **) &b->frontxrb);
430 ASSERT(b->frontxrb == NULL);
431 }
432 if (b->backxrb) {
433 _mesa_unreference_renderbuffer((struct gl_renderbuffer **) &b->backxrb);
434 ASSERT(b->backxrb == NULL);
435 }
436
437 _mesa_free_framebuffer_data(fb);
438 _mesa_free(fb);
439 }