-/*
- * Return number of bits set in n.
- */
-static int bitcount( unsigned long n )
-{
- int bits;
- for (bits=0; n>0; n=n>>1) {
- if (n&1) {
- bits++;
- }
- }
- return bits;
-}
-
-
-
-/*
- * Allocate a shared memory XImage back buffer for the given XMesaBuffer.
- * Return: GL_TRUE if success, GL_FALSE if error
- */
-#ifndef XFree86Server
-static GLboolean alloc_shm_back_buffer( XMesaBuffer b )
-{
-#ifdef USE_XSHM
- /*
- * We have to do a _lot_ of error checking here to be sure we can
- * really use the XSHM extension. It seems different servers trigger
- * errors at different points if the extension won't work. Therefore
- * we have to be very careful...
- */
- GC gc;
- int (*old_handler)( XMesaDisplay *, XErrorEvent * );
-
- b->backimage = XShmCreateImage( b->xm_visual->display,
- b->xm_visual->visinfo->visual,
- b->xm_visual->visinfo->depth,
- ZPixmap, NULL, &b->shminfo,
- b->width, b->height );
- if (b->backimage == NULL) {
- error("alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
- b->shm = 0;
- return GL_FALSE;
- }
-
- b->shminfo.shmid = shmget( IPC_PRIVATE, b->backimage->bytes_per_line
- * b->backimage->height, IPC_CREAT|0777 );
- if (b->shminfo.shmid < 0) {
- if (getenv("MESA_DEBUG"))
- perror("alloc_back_buffer");
- XDestroyImage( b->backimage );
- b->backimage = NULL;
- error("alloc_back_buffer: Shared memory error (shmget), disabling.");
- b->shm = 0;
- return GL_FALSE;
- }
-
- b->shminfo.shmaddr = b->backimage->data
- = (char*)shmat( b->shminfo.shmid, 0, 0 );
- if (b->shminfo.shmaddr == (char *) -1) {
- if (getenv("MESA_DEBUG"))
- perror("alloc_back_buffer");
- XDestroyImage( b->backimage );
- shmctl( b->shminfo.shmid, IPC_RMID, 0 );
- b->backimage = NULL;
- error("alloc_back_buffer: Shared memory error (shmat), disabling.");
- b->shm = 0;
- return GL_FALSE;
- }
-
- b->shminfo.readOnly = False;
- mesaXErrorFlag = 0;
- old_handler = XSetErrorHandler( mesaHandleXError );
- /* This may trigger the X protocol error we're ready to catch: */
- XShmAttach( b->xm_visual->display, &b->shminfo );
- XSync( b->xm_visual->display, False );
-
- if (mesaXErrorFlag) {
- /* we are on a remote display, this error is normal, don't print it */
- XFlush( b->xm_visual->display );
- mesaXErrorFlag = 0;
- XDestroyImage( b->backimage );
- shmdt( b->shminfo.shmaddr );
- shmctl( b->shminfo.shmid, IPC_RMID, 0 );
- b->backimage = NULL;
- b->shm = 0;
- (void) XSetErrorHandler( old_handler );
- return GL_FALSE;
- }
-
- shmctl( b->shminfo.shmid, IPC_RMID, 0 ); /* nobody else needs it */
-
- /* Finally, try an XShmPutImage to be really sure the extension works */
- gc = XCreateGC( b->xm_visual->display, b->frontbuffer, 0, NULL );
- XShmPutImage( b->xm_visual->display, b->frontbuffer, gc,
- b->backimage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False );
- XSync( b->xm_visual->display, False );
- XFreeGC( b->xm_visual->display, gc );
- (void) XSetErrorHandler( old_handler );
- if (mesaXErrorFlag) {
- XFlush( b->xm_visual->display );
- mesaXErrorFlag = 0;
- XDestroyImage( b->backimage );
- shmdt( b->shminfo.shmaddr );
- shmctl( b->shminfo.shmid, IPC_RMID, 0 );
- b->backimage = NULL;
- b->shm = 0;
- return GL_FALSE;
- }
-
- if (b->backimage) {
- int height = b->backimage->height;
- /* Needed by PIXELADDR1 macro */
- b->ximage_width1 = b->backimage->bytes_per_line;
- b->ximage_origin1 = (GLubyte *) b->backimage->data
- + b->ximage_width1 * (height-1);
- /* Needed by PIXELADDR2 macro */
- b->ximage_width2 = b->backimage->bytes_per_line / 2;
- b->ximage_origin2 = (GLushort *) b->backimage->data
- + b->ximage_width2 * (height-1);
- /* Needed by PIXELADDR3 macro */
- b->ximage_width3 = b->backimage->bytes_per_line;
- b->ximage_origin3 = (GLubyte *) b->backimage->data
- + b->ximage_width3 * (height-1);
- /* Needed by PIXELADDR4 macro */
- b->ximage_width4 = b->backimage->width;
- b->ximage_origin4 = (GLuint *) b->backimage->data
- + b->ximage_width4 * (height-1);
- }
-
- return GL_TRUE;
-#else
- /* Can't compile XSHM support */
- return GL_FALSE;
-#endif
-}
-#endif
-
-
-
-/*
- * Setup an off-screen pixmap or Ximage to use as the back buffer.
- * Input: b - the X/Mesa buffer
- */
-void xmesa_alloc_back_buffer( XMesaBuffer b )
-{
- if (b->db_state==BACK_XIMAGE) {
- /* Deallocate the old backimage, if any */
- if (b->backimage) {
-#if defined(USE_XSHM) && !defined(XFree86Server)
- if (b->shm) {
- XShmDetach( b->xm_visual->display, &b->shminfo );
- XDestroyImage( b->backimage );
- shmdt( b->shminfo.shmaddr );
- }
- else
-#endif
- XMesaDestroyImage( b->backimage );
- b->backimage = NULL;
- }
-
- /* Allocate new back buffer */
-#ifdef XFree86Server
- {
- /* Allocate a regular XImage for the back buffer. */
- b->backimage = XMesaCreateImage(b->xm_visual->BitsPerPixel,
- b->width, b->height, NULL);
-#else
- if (b->shm==0 || alloc_shm_back_buffer(b)==GL_FALSE) {
- /* Allocate a regular XImage for the back buffer. */
- b->backimage = XCreateImage( b->xm_visual->display,
- b->xm_visual->visinfo->visual,
- GET_VISUAL_DEPTH(b->xm_visual),
- ZPixmap, 0, /* format, offset */
- NULL, b->width, b->height,
- 8, 0 ); /* pad, bytes_per_line */
-#endif
- if (!b->backimage) {
- error("alloc_back_buffer: XCreateImage failed.");
- }
- b->backimage->data = (char *) MALLOC( b->backimage->height
- * b->backimage->bytes_per_line );
- if (!b->backimage->data) {
- error("alloc_back_buffer: MALLOC failed.");
- XMesaDestroyImage( b->backimage );
- b->backimage = NULL;
- }
- }
- b->backpixmap = None;
- }
- else if (b->db_state==BACK_PIXMAP) {
- XMesaPixmap old_pixmap = b->backpixmap;
- /* Free the old back pixmap */
- if (b->backpixmap) {
- XMesaFreePixmap( b->xm_visual->display, b->backpixmap );
- }
- /* Allocate new back pixmap */
- b->backpixmap = XMesaCreatePixmap( b->xm_visual->display, b->frontbuffer,
- b->width, b->height,
- GET_VISUAL_DEPTH(b->xm_visual) );
- b->backimage = NULL;
- /* update other references to backpixmap */
- if (b->buffer==(XMesaDrawable)old_pixmap) {
- b->buffer = (XMesaDrawable)b->backpixmap;
- }
- }
-}
-
-
-
-/*