src/video/x11/SDL_x11image.c
changeset 0 74212992fb08
child 161 eb6b76a95f2d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/x11/SDL_x11image.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,456 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include <stdlib.h>
    1.32 +
    1.33 +#include "SDL_error.h"
    1.34 +#include "SDL_endian.h"
    1.35 +#include "SDL_x11image_c.h"
    1.36 +
    1.37 +#if defined(__USLC__)
    1.38 +#ifdef HAVE_KSTAT
    1.39 +#undef HAVE_KSTAT
    1.40 +#endif
    1.41 +#include <unistd.h>
    1.42 +#endif
    1.43 +
    1.44 +#ifdef HAVE_KSTAT
    1.45 +#include <kstat.h>
    1.46 +#endif
    1.47 +
    1.48 +#ifndef NO_SHARED_MEMORY
    1.49 +
    1.50 +/* Shared memory information */
    1.51 +extern int XShmQueryExtension(Display *dpy);	/* Not in X11 headers */
    1.52 +
    1.53 +/* Shared memory error handler routine */
    1.54 +static int shm_error;
    1.55 +static int (*X_handler)(Display *, XErrorEvent *) = NULL;
    1.56 +static int shm_errhandler(Display *d, XErrorEvent *e)
    1.57 +{
    1.58 +        if ( e->error_code == BadAccess ) {
    1.59 +        	++shm_error;
    1.60 +        	return(0);
    1.61 +        } else
    1.62 +		return(X_handler(d,e));
    1.63 +}
    1.64 +#endif /* ! NO_SHARED_MEMORY */
    1.65 +
    1.66 +/* Various screen update functions available */
    1.67 +static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
    1.68 +static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects);
    1.69 +
    1.70 +int X11_SetupImage(_THIS, SDL_Surface *screen)
    1.71 +{
    1.72 +#ifdef NO_SHARED_MEMORY
    1.73 +	screen->pixels = malloc(screen->h*screen->pitch);
    1.74 +#else
    1.75 +	/* Allocate shared memory if possible */
    1.76 +	if ( use_mitshm ) {
    1.77 +		shminfo.shmid = shmget(IPC_PRIVATE, screen->h*screen->pitch,
    1.78 +								IPC_CREAT|0777);
    1.79 +		if ( shminfo.shmid >= 0 ) {
    1.80 +			shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0);
    1.81 +			shminfo.readOnly = False;
    1.82 +			if ( shminfo.shmaddr != (char *)-1 ) {
    1.83 +				shm_error = False;
    1.84 +				X_handler = XSetErrorHandler(shm_errhandler);
    1.85 +				XShmAttach(SDL_Display, &shminfo);
    1.86 +				XSync(SDL_Display, True);
    1.87 +				XSetErrorHandler(X_handler);
    1.88 +				if ( shm_error == True )
    1.89 +					shmdt(shminfo.shmaddr);
    1.90 +			} else {
    1.91 +				shm_error = True;
    1.92 +			}
    1.93 +			shmctl(shminfo.shmid, IPC_RMID, NULL);
    1.94 +		} else {
    1.95 +			shm_error = True;
    1.96 +		}
    1.97 +		if ( shm_error == True )
    1.98 +			use_mitshm = 0;
    1.99 +	}
   1.100 +	if ( use_mitshm ) {
   1.101 +		screen->pixels = shminfo.shmaddr;
   1.102 +	} else {
   1.103 +		screen->pixels = malloc(screen->h*screen->pitch);
   1.104 +	}
   1.105 +#endif /* NO_SHARED_MEMORY */
   1.106 +	if ( screen->pixels == NULL ) {
   1.107 +		SDL_OutOfMemory();
   1.108 +		return(-1);
   1.109 +	}
   1.110 +
   1.111 +#ifdef NO_SHARED_MEMORY
   1.112 +	{
   1.113 + 	        int bpp = screen->format->BytesPerPixel;
   1.114 +		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
   1.115 +					  this->hidden->depth, ZPixmap, 0,
   1.116 +					  (char *)screen->pixels, 
   1.117 +					  screen->w, screen->h,
   1.118 +					  (bpp == 3) ? 32 : bpp * 8,
   1.119 +					  0);
   1.120 +	}
   1.121 +#else
   1.122 +	if ( use_mitshm ) {
   1.123 +		SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual,
   1.124 +					     this->hidden->depth, ZPixmap,
   1.125 +					     shminfo.shmaddr, &shminfo, 
   1.126 +					     screen->w, screen->h);
   1.127 +	} else {
   1.128 + 	        int bpp = screen->format->BytesPerPixel;
   1.129 +		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
   1.130 +					  this->hidden->depth, ZPixmap, 0,
   1.131 +					  (char *)screen->pixels, 
   1.132 +					  screen->w, screen->h,
   1.133 +					  (bpp == 3) ? 32 : bpp * 8,
   1.134 +					  0);
   1.135 +	}
   1.136 +#endif /* NO_SHARED_MEMORY */
   1.137 +	if ( SDL_Ximage == NULL ) {
   1.138 +		SDL_SetError("Couldn't create XImage");
   1.139 +#ifndef NO_SHARED_MEMORY
   1.140 +		if ( use_mitshm ) {
   1.141 +			XShmDetach(SDL_Display, &shminfo);
   1.142 +			XSync(SDL_Display, False);
   1.143 +			shmdt(shminfo.shmaddr);
   1.144 +			screen->pixels = NULL;
   1.145 +		}
   1.146 +#endif /* ! NO_SHARED_MEMORY */
   1.147 +		return(-1);
   1.148 +	}
   1.149 +	screen->pitch = SDL_Ximage->bytes_per_line;
   1.150 +
   1.151 +	/* Determine what blit function to use */
   1.152 +#ifdef NO_SHARED_MEMORY
   1.153 +	this->UpdateRects = X11_NormalUpdate;
   1.154 +#else
   1.155 +	if ( use_mitshm ) {
   1.156 +		this->UpdateRects = X11_MITSHMUpdate;
   1.157 +	} else {
   1.158 +		this->UpdateRects = X11_NormalUpdate;
   1.159 +	}
   1.160 +#endif
   1.161 +	return(0);
   1.162 +}
   1.163 +
   1.164 +void X11_DestroyImage(_THIS, SDL_Surface *screen)
   1.165 +{
   1.166 +	if ( SDL_Ximage ) {
   1.167 +		XDestroyImage(SDL_Ximage);
   1.168 +#ifndef NO_SHARED_MEMORY
   1.169 +		if ( use_mitshm ) {
   1.170 +			XShmDetach(SDL_Display, &shminfo);
   1.171 +			XSync(SDL_Display, False);
   1.172 +			shmdt(shminfo.shmaddr);
   1.173 +		}
   1.174 +#endif /* ! NO_SHARED_MEMORY */
   1.175 +		SDL_Ximage = NULL;
   1.176 +	}
   1.177 +	if ( screen ) {
   1.178 +		screen->pixels = NULL;
   1.179 +	}
   1.180 +}
   1.181 +
   1.182 +/* This is a hack to see whether this system has more than 1 CPU */
   1.183 +static int num_CPU(void)
   1.184 +{
   1.185 +       static int num_cpus = 0;
   1.186 +
   1.187 +       if(!num_cpus) {
   1.188 +#ifdef linux
   1.189 +           char line[BUFSIZ];
   1.190 +           FILE *pstat = fopen("/proc/stat", "r");
   1.191 +           if ( pstat ) {
   1.192 +               while ( fgets(line, sizeof(line), pstat) ) {
   1.193 +                   if (memcmp(line, "cpu", 3) == 0 && line[3] != ' ') {
   1.194 +                       ++num_cpus;
   1.195 +                   }
   1.196 +               }
   1.197 +               fclose(pstat);
   1.198 +           }
   1.199 +#elif defined(HAVE_KSTAT)
   1.200 +           kstat_ctl_t *kc = kstat_open();
   1.201 +           kstat_t *ks;
   1.202 +           kstat_named_t *kn;
   1.203 +           if(kc) {
   1.204 +               if((ks = kstat_lookup(kc, "unix", -1, "system_misc"))
   1.205 +                  && kstat_read(kc, ks, NULL) != -1
   1.206 +                  && (kn = kstat_data_lookup(ks, "ncpus")))
   1.207 +#ifdef KSTAT_DATA_UINT32
   1.208 +                   num_cpus = kn->value.ui32;
   1.209 +#else
   1.210 +                   num_cpus = kn->value.ul; /* needed in solaris <2.6 */
   1.211 +#endif
   1.212 +               kstat_close(kc);
   1.213 +           }
   1.214 +#elif defined(__USLC__)
   1.215 +           num_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
   1.216 +#endif
   1.217 +           if ( num_cpus <= 0 ) {
   1.218 +               num_cpus = 1;
   1.219 +           }
   1.220 +       }
   1.221 +       return num_cpus;
   1.222 +}
   1.223 +
   1.224 +int X11_ResizeImage(_THIS, SDL_Surface *screen, Uint32 flags)
   1.225 +{
   1.226 +	int retval;
   1.227 +
   1.228 +	X11_DestroyImage(this, screen);
   1.229 +        if ( flags & SDL_OPENGL ) {  /* No image when using GL */
   1.230 +        	retval = 0;
   1.231 +        } else {
   1.232 +		retval = X11_SetupImage(this, screen);
   1.233 +		/* We support asynchronous blitting on the display */
   1.234 +		if ( flags & SDL_ASYNCBLIT ) {
   1.235 +			/* This is actually slower on single-CPU systems,
   1.236 +			   probably because of CPU contention between the
   1.237 +			   X server and the application.
   1.238 +			   Note: Is this still true with XFree86 4.0?
   1.239 +			*/
   1.240 +			if ( num_CPU() > 1 ) {
   1.241 +				screen->flags |= SDL_ASYNCBLIT;
   1.242 +			}
   1.243 +		}
   1.244 +	}
   1.245 +	return(retval);
   1.246 +}
   1.247 +
   1.248 +/* We don't actually allow hardware surfaces other than the main one */
   1.249 +int X11_AllocHWSurface(_THIS, SDL_Surface *surface)
   1.250 +{
   1.251 +	return(-1);
   1.252 +}
   1.253 +void X11_FreeHWSurface(_THIS, SDL_Surface *surface)
   1.254 +{
   1.255 +	return;
   1.256 +}
   1.257 +
   1.258 +int X11_LockHWSurface(_THIS, SDL_Surface *surface)
   1.259 +{
   1.260 +	if ( (surface == SDL_VideoSurface) && blit_queued ) {
   1.261 +		XSync(GFX_Display, False);
   1.262 +		blit_queued = 0;
   1.263 +	}
   1.264 +	return(0);
   1.265 +}
   1.266 +void X11_UnlockHWSurface(_THIS, SDL_Surface *surface)
   1.267 +{
   1.268 +	return;
   1.269 +}
   1.270 +
   1.271 +int X11_FlipHWSurface(_THIS, SDL_Surface *surface)
   1.272 +{
   1.273 +	return(0);
   1.274 +}
   1.275 +
   1.276 +/* Byte-swap the pixels in the display image */
   1.277 +static void X11_SwapAllPixels(SDL_Surface *screen)
   1.278 +{
   1.279 +	int x, y;
   1.280 +
   1.281 +	switch (screen->format->BytesPerPixel) {
   1.282 +	    case 2: {
   1.283 +		Uint16 *spot;
   1.284 +		for ( y=0; y<screen->h; ++y ) {
   1.285 +			spot = (Uint16 *) ((Uint8 *)screen->pixels +
   1.286 +						y * screen->pitch);
   1.287 +			for ( x=0; x<screen->w; ++x, ++spot ) {
   1.288 +				*spot = SDL_Swap16(*spot);
   1.289 +			}
   1.290 +		}
   1.291 +	    }
   1.292 +	    break;
   1.293 +
   1.294 +	    case 4: {
   1.295 +		Uint32 *spot;
   1.296 +		for ( y=0; y<screen->h; ++y ) {
   1.297 +			spot = (Uint32 *) ((Uint8 *)screen->pixels +
   1.298 +						y * screen->pitch);
   1.299 +			for ( x=0; x<screen->w; ++x, ++spot ) {
   1.300 +				*spot = SDL_Swap32(*spot);
   1.301 +			}
   1.302 +		}
   1.303 +	    }
   1.304 +	    break;
   1.305 +
   1.306 +	    default:
   1.307 +		/* should never get here */
   1.308 +		break;
   1.309 +	}
   1.310 +}
   1.311 +static void X11_SwapPixels(SDL_Surface *screen, SDL_Rect *rect)
   1.312 +{
   1.313 +	int x, minx, maxx;
   1.314 +	int y, miny, maxy;
   1.315 +
   1.316 +	switch (screen->format->BytesPerPixel) {
   1.317 +	    case 2: {
   1.318 +		Uint16 *spot;
   1.319 +		minx = rect->x;
   1.320 +		maxx = rect->x + rect->w;
   1.321 +		miny = rect->y;
   1.322 +		maxy = rect->y + rect->h;
   1.323 +		for ( y=miny; y<maxy; ++y ) {
   1.324 +		    spot = (Uint16 *) ((Uint8 *)screen->pixels +
   1.325 +				       y * screen->pitch + minx * 2);
   1.326 +		    for ( x=minx; x<maxx; ++x, ++spot ) {
   1.327 +			*spot = SDL_Swap16(*spot);
   1.328 +		    }
   1.329 +		}
   1.330 +	    }
   1.331 +	    break;
   1.332 +
   1.333 +	    case 4: {
   1.334 +		Uint32 *spot;
   1.335 +		minx = rect->x;
   1.336 +		maxx = rect->x + rect->w;
   1.337 +		miny = rect->y;
   1.338 +		maxy = rect->y + rect->h;
   1.339 +		for ( y=miny; y<maxy; ++y ) {
   1.340 +		    spot = (Uint32 *) ((Uint8 *)screen->pixels +
   1.341 +				       y * screen->pitch + minx * 4);
   1.342 +		    for ( x=minx; x<maxx; ++x, ++spot ) {
   1.343 +			*spot = SDL_Swap32(*spot);
   1.344 +		    }
   1.345 +		}
   1.346 +	    }
   1.347 +	    break;
   1.348 +
   1.349 +	    default:
   1.350 +		/* should never get here */
   1.351 +		break;
   1.352 +	}
   1.353 +}
   1.354 +
   1.355 +static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
   1.356 +{
   1.357 +	int i;
   1.358 +
   1.359 +	/* Check for endian-swapped X server, swap if necessary (VERY slow!) */
   1.360 +	if ( swap_pixels &&
   1.361 +	     ((this->screen->format->BytesPerPixel%2) == 0) ) {
   1.362 +		for ( i=0; i<numrects; ++i ) {
   1.363 +			if ( ! rects[i].w ) { /* Clipped? */
   1.364 +				continue;
   1.365 +			}
   1.366 +		        X11_SwapPixels(this->screen, rects + i);
   1.367 +			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.368 +				rects[i].x, rects[i].y,
   1.369 +				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
   1.370 +			X11_SwapPixels(this->screen, rects + i);
   1.371 +		}
   1.372 +	} else {
   1.373 +		for ( i=0; i<numrects; ++i ) {
   1.374 +			if ( ! rects[i].w ) { /* Clipped? */
   1.375 +				continue;
   1.376 +			}
   1.377 +			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.378 +				rects[i].x, rects[i].y,
   1.379 +				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
   1.380 +		}
   1.381 +	}
   1.382 +	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
   1.383 +		XFlush(GFX_Display);
   1.384 +		++blit_queued;
   1.385 +	} else {
   1.386 +		XSync(GFX_Display, False);
   1.387 +	}
   1.388 +}
   1.389 +
   1.390 +static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects)
   1.391 +{
   1.392 +#ifndef NO_SHARED_MEMORY
   1.393 +	int i;
   1.394 +
   1.395 +	for ( i=0; i<numrects; ++i ) {
   1.396 +		if ( ! rects[i].w ) { /* Clipped? */
   1.397 +			continue;
   1.398 +		}
   1.399 +		XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.400 +				rects[i].x, rects[i].y,
   1.401 +				rects[i].x, rects[i].y, rects[i].w, rects[i].h,
   1.402 +									False);
   1.403 +	}
   1.404 +	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
   1.405 +		XFlush(GFX_Display);
   1.406 +		++blit_queued;
   1.407 +	} else {
   1.408 +		XSync(GFX_Display, False);
   1.409 +	}
   1.410 +#endif /* ! NO_SHARED_MEMORY */
   1.411 +}
   1.412 +
   1.413 +/* There's a problem with the automatic refreshing of the display.
   1.414 +   Even though the XVideo code uses the GFX_Display to update the
   1.415 +   video memory, it appears that updating the window asynchronously
   1.416 +   from a different thread will cause "blackouts" of the window.
   1.417 +   This is a sort of a hacked workaround for the problem.
   1.418 +*/
   1.419 +static int enable_autorefresh = 1;
   1.420 +
   1.421 +void X11_DisableAutoRefresh(_THIS)
   1.422 +{
   1.423 +	--enable_autorefresh;
   1.424 +}
   1.425 +
   1.426 +void X11_EnableAutoRefresh(_THIS)
   1.427 +{
   1.428 +	++enable_autorefresh;
   1.429 +}
   1.430 +
   1.431 +void X11_RefreshDisplay(_THIS)
   1.432 +{
   1.433 +	/* Don't refresh a display that doesn't have an image (like GL) */
   1.434 +	if ( ! SDL_Ximage || (enable_autorefresh <= 0) ) {
   1.435 +		return;
   1.436 +	}
   1.437 +#ifndef NO_SHARED_MEMORY
   1.438 +	if ( this->UpdateRects == X11_MITSHMUpdate ) {
   1.439 +		XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.440 +				0, 0, 0, 0, this->screen->w, this->screen->h,
   1.441 +				False);
   1.442 +	} else {
   1.443 +#else
   1.444 +	{
   1.445 +#endif /* ! NO_SHARED_MEMORY */
   1.446 +		/* Check for endian-swapped X server, swap if necessary */
   1.447 +		if ( swap_pixels &&
   1.448 +		     ((this->screen->format->BytesPerPixel%2) == 0) ) {
   1.449 +			X11_SwapAllPixels(this->screen);
   1.450 +			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.451 +				0, 0, 0, 0, this->screen->w, this->screen->h);
   1.452 +			X11_SwapAllPixels(this->screen);
   1.453 +		} else {
   1.454 +			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
   1.455 +				0, 0, 0, 0, this->screen->w, this->screen->h);
   1.456 +		}
   1.457 +	}
   1.458 +	XSync(SDL_Display, False);
   1.459 +}