src/video/x11/SDL_x11image.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:38:15 +0000
changeset 252 e8157fcb3114
parent 166 39877400bd1e
child 270 37fa1484f71b
permissions -rw-r--r--
Updated the source with the correct e-mail address
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@166
    29
#include <unistd.h>
slouken@0
    30
slouken@0
    31
#include "SDL_error.h"
slouken@0
    32
#include "SDL_endian.h"
slouken@161
    33
#include "SDL_events_c.h"
slouken@0
    34
#include "SDL_x11image_c.h"
slouken@0
    35
slouken@0
    36
#ifndef NO_SHARED_MEMORY
slouken@0
    37
slouken@0
    38
/* Shared memory information */
slouken@0
    39
extern int XShmQueryExtension(Display *dpy);	/* Not in X11 headers */
slouken@0
    40
slouken@0
    41
/* Shared memory error handler routine */
slouken@0
    42
static int shm_error;
slouken@0
    43
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
slouken@0
    44
static int shm_errhandler(Display *d, XErrorEvent *e)
slouken@0
    45
{
slouken@0
    46
        if ( e->error_code == BadAccess ) {
slouken@0
    47
        	++shm_error;
slouken@0
    48
        	return(0);
slouken@0
    49
        } else
slouken@0
    50
		return(X_handler(d,e));
slouken@0
    51
}
slouken@0
    52
#endif /* ! NO_SHARED_MEMORY */
slouken@0
    53
slouken@0
    54
/* Various screen update functions available */
slouken@0
    55
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    56
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    57
slouken@0
    58
int X11_SetupImage(_THIS, SDL_Surface *screen)
slouken@0
    59
{
slouken@0
    60
#ifdef NO_SHARED_MEMORY
slouken@0
    61
	screen->pixels = malloc(screen->h*screen->pitch);
slouken@0
    62
#else
slouken@0
    63
	/* Allocate shared memory if possible */
slouken@0
    64
	if ( use_mitshm ) {
slouken@0
    65
		shminfo.shmid = shmget(IPC_PRIVATE, screen->h*screen->pitch,
slouken@0
    66
								IPC_CREAT|0777);
slouken@0
    67
		if ( shminfo.shmid >= 0 ) {
slouken@0
    68
			shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0);
slouken@0
    69
			shminfo.readOnly = False;
slouken@0
    70
			if ( shminfo.shmaddr != (char *)-1 ) {
slouken@0
    71
				shm_error = False;
slouken@0
    72
				X_handler = XSetErrorHandler(shm_errhandler);
slouken@0
    73
				XShmAttach(SDL_Display, &shminfo);
slouken@0
    74
				XSync(SDL_Display, True);
slouken@0
    75
				XSetErrorHandler(X_handler);
slouken@0
    76
				if ( shm_error == True )
slouken@0
    77
					shmdt(shminfo.shmaddr);
slouken@0
    78
			} else {
slouken@0
    79
				shm_error = True;
slouken@0
    80
			}
slouken@0
    81
			shmctl(shminfo.shmid, IPC_RMID, NULL);
slouken@0
    82
		} else {
slouken@0
    83
			shm_error = True;
slouken@0
    84
		}
slouken@0
    85
		if ( shm_error == True )
slouken@0
    86
			use_mitshm = 0;
slouken@0
    87
	}
slouken@0
    88
	if ( use_mitshm ) {
slouken@0
    89
		screen->pixels = shminfo.shmaddr;
slouken@0
    90
	} else {
slouken@0
    91
		screen->pixels = malloc(screen->h*screen->pitch);
slouken@0
    92
	}
slouken@0
    93
#endif /* NO_SHARED_MEMORY */
slouken@0
    94
	if ( screen->pixels == NULL ) {
slouken@0
    95
		SDL_OutOfMemory();
slouken@0
    96
		return(-1);
slouken@0
    97
	}
slouken@0
    98
slouken@0
    99
#ifdef NO_SHARED_MEMORY
slouken@0
   100
	{
slouken@0
   101
 	        int bpp = screen->format->BytesPerPixel;
slouken@0
   102
		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
slouken@0
   103
					  this->hidden->depth, ZPixmap, 0,
slouken@0
   104
					  (char *)screen->pixels, 
slouken@0
   105
					  screen->w, screen->h,
slouken@0
   106
					  (bpp == 3) ? 32 : bpp * 8,
slouken@0
   107
					  0);
slouken@0
   108
	}
slouken@0
   109
#else
slouken@0
   110
	if ( use_mitshm ) {
slouken@0
   111
		SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual,
slouken@0
   112
					     this->hidden->depth, ZPixmap,
slouken@0
   113
					     shminfo.shmaddr, &shminfo, 
slouken@0
   114
					     screen->w, screen->h);
slouken@0
   115
	} else {
slouken@0
   116
 	        int bpp = screen->format->BytesPerPixel;
slouken@0
   117
		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
slouken@0
   118
					  this->hidden->depth, ZPixmap, 0,
slouken@0
   119
					  (char *)screen->pixels, 
slouken@0
   120
					  screen->w, screen->h,
slouken@0
   121
					  (bpp == 3) ? 32 : bpp * 8,
slouken@0
   122
					  0);
slouken@0
   123
	}
slouken@0
   124
#endif /* NO_SHARED_MEMORY */
slouken@0
   125
	if ( SDL_Ximage == NULL ) {
slouken@0
   126
		SDL_SetError("Couldn't create XImage");
slouken@0
   127
#ifndef NO_SHARED_MEMORY
slouken@0
   128
		if ( use_mitshm ) {
slouken@0
   129
			XShmDetach(SDL_Display, &shminfo);
slouken@0
   130
			XSync(SDL_Display, False);
slouken@0
   131
			shmdt(shminfo.shmaddr);
slouken@0
   132
			screen->pixels = NULL;
slouken@0
   133
		}
slouken@0
   134
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   135
		return(-1);
slouken@0
   136
	}
slouken@0
   137
	screen->pitch = SDL_Ximage->bytes_per_line;
slouken@0
   138
slouken@0
   139
	/* Determine what blit function to use */
slouken@0
   140
#ifdef NO_SHARED_MEMORY
slouken@0
   141
	this->UpdateRects = X11_NormalUpdate;
slouken@0
   142
#else
slouken@0
   143
	if ( use_mitshm ) {
slouken@0
   144
		this->UpdateRects = X11_MITSHMUpdate;
slouken@0
   145
	} else {
slouken@0
   146
		this->UpdateRects = X11_NormalUpdate;
slouken@0
   147
	}
slouken@0
   148
#endif
slouken@0
   149
	return(0);
slouken@0
   150
}
slouken@0
   151
slouken@0
   152
void X11_DestroyImage(_THIS, SDL_Surface *screen)
slouken@0
   153
{
slouken@0
   154
	if ( SDL_Ximage ) {
slouken@0
   155
		XDestroyImage(SDL_Ximage);
slouken@0
   156
#ifndef NO_SHARED_MEMORY
slouken@0
   157
		if ( use_mitshm ) {
slouken@0
   158
			XShmDetach(SDL_Display, &shminfo);
slouken@0
   159
			XSync(SDL_Display, False);
slouken@0
   160
			shmdt(shminfo.shmaddr);
slouken@0
   161
		}
slouken@0
   162
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   163
		SDL_Ximage = NULL;
slouken@0
   164
	}
slouken@0
   165
	if ( screen ) {
slouken@0
   166
		screen->pixels = NULL;
slouken@0
   167
	}
slouken@0
   168
}
slouken@0
   169
slouken@166
   170
/* Determine the number of CPUs in the system */
slouken@0
   171
static int num_CPU(void)
slouken@0
   172
{
slouken@0
   173
       static int num_cpus = 0;
slouken@0
   174
slouken@0
   175
       if(!num_cpus) {
slouken@166
   176
#if defined(__linux)
slouken@0
   177
           char line[BUFSIZ];
slouken@0
   178
           FILE *pstat = fopen("/proc/stat", "r");
slouken@0
   179
           if ( pstat ) {
slouken@0
   180
               while ( fgets(line, sizeof(line), pstat) ) {
slouken@0
   181
                   if (memcmp(line, "cpu", 3) == 0 && line[3] != ' ') {
slouken@0
   182
                       ++num_cpus;
slouken@0
   183
                   }
slouken@0
   184
               }
slouken@0
   185
               fclose(pstat);
slouken@0
   186
           }
slouken@166
   187
#elif defined(_SC_NPROCESSORS_ONLN)
slouken@166
   188
	   /* number of processors online (SVR4.0MP compliant machines) */
slouken@166
   189
           num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
slouken@166
   190
#elif defined(_SC_NPROCESSORS_CONF)
slouken@166
   191
	   /* number of processors configured (SVR4.0MP compliant machines) */
slouken@166
   192
           num_cpus = sysconf(_SC_NPROCESSORS_CONF);
slouken@0
   193
#endif
slouken@0
   194
           if ( num_cpus <= 0 ) {
slouken@0
   195
               num_cpus = 1;
slouken@0
   196
           }
slouken@0
   197
       }
slouken@0
   198
       return num_cpus;
slouken@0
   199
}
slouken@0
   200
slouken@0
   201
int X11_ResizeImage(_THIS, SDL_Surface *screen, Uint32 flags)
slouken@0
   202
{
slouken@0
   203
	int retval;
slouken@0
   204
slouken@0
   205
	X11_DestroyImage(this, screen);
slouken@0
   206
        if ( flags & SDL_OPENGL ) {  /* No image when using GL */
slouken@0
   207
        	retval = 0;
slouken@0
   208
        } else {
slouken@0
   209
		retval = X11_SetupImage(this, screen);
slouken@0
   210
		/* We support asynchronous blitting on the display */
slouken@0
   211
		if ( flags & SDL_ASYNCBLIT ) {
slouken@0
   212
			/* This is actually slower on single-CPU systems,
slouken@0
   213
			   probably because of CPU contention between the
slouken@0
   214
			   X server and the application.
slouken@0
   215
			   Note: Is this still true with XFree86 4.0?
slouken@0
   216
			*/
slouken@0
   217
			if ( num_CPU() > 1 ) {
slouken@0
   218
				screen->flags |= SDL_ASYNCBLIT;
slouken@0
   219
			}
slouken@0
   220
		}
slouken@0
   221
	}
slouken@0
   222
	return(retval);
slouken@0
   223
}
slouken@0
   224
slouken@0
   225
/* We don't actually allow hardware surfaces other than the main one */
slouken@0
   226
int X11_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   227
{
slouken@0
   228
	return(-1);
slouken@0
   229
}
slouken@0
   230
void X11_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   231
{
slouken@0
   232
	return;
slouken@0
   233
}
slouken@0
   234
slouken@0
   235
int X11_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   236
{
slouken@0
   237
	if ( (surface == SDL_VideoSurface) && blit_queued ) {
slouken@0
   238
		XSync(GFX_Display, False);
slouken@0
   239
		blit_queued = 0;
slouken@0
   240
	}
slouken@0
   241
	return(0);
slouken@0
   242
}
slouken@0
   243
void X11_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   244
{
slouken@0
   245
	return;
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
int X11_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   249
{
slouken@0
   250
	return(0);
slouken@0
   251
}
slouken@0
   252
slouken@0
   253
/* Byte-swap the pixels in the display image */
slouken@0
   254
static void X11_SwapAllPixels(SDL_Surface *screen)
slouken@0
   255
{
slouken@0
   256
	int x, y;
slouken@0
   257
slouken@0
   258
	switch (screen->format->BytesPerPixel) {
slouken@0
   259
	    case 2: {
slouken@0
   260
		Uint16 *spot;
slouken@0
   261
		for ( y=0; y<screen->h; ++y ) {
slouken@0
   262
			spot = (Uint16 *) ((Uint8 *)screen->pixels +
slouken@0
   263
						y * screen->pitch);
slouken@0
   264
			for ( x=0; x<screen->w; ++x, ++spot ) {
slouken@0
   265
				*spot = SDL_Swap16(*spot);
slouken@0
   266
			}
slouken@0
   267
		}
slouken@0
   268
	    }
slouken@0
   269
	    break;
slouken@0
   270
slouken@0
   271
	    case 4: {
slouken@0
   272
		Uint32 *spot;
slouken@0
   273
		for ( y=0; y<screen->h; ++y ) {
slouken@0
   274
			spot = (Uint32 *) ((Uint8 *)screen->pixels +
slouken@0
   275
						y * screen->pitch);
slouken@0
   276
			for ( x=0; x<screen->w; ++x, ++spot ) {
slouken@0
   277
				*spot = SDL_Swap32(*spot);
slouken@0
   278
			}
slouken@0
   279
		}
slouken@0
   280
	    }
slouken@0
   281
	    break;
slouken@0
   282
slouken@0
   283
	    default:
slouken@0
   284
		/* should never get here */
slouken@0
   285
		break;
slouken@0
   286
	}
slouken@0
   287
}
slouken@0
   288
static void X11_SwapPixels(SDL_Surface *screen, SDL_Rect *rect)
slouken@0
   289
{
slouken@0
   290
	int x, minx, maxx;
slouken@0
   291
	int y, miny, maxy;
slouken@0
   292
slouken@0
   293
	switch (screen->format->BytesPerPixel) {
slouken@0
   294
	    case 2: {
slouken@0
   295
		Uint16 *spot;
slouken@0
   296
		minx = rect->x;
slouken@0
   297
		maxx = rect->x + rect->w;
slouken@0
   298
		miny = rect->y;
slouken@0
   299
		maxy = rect->y + rect->h;
slouken@0
   300
		for ( y=miny; y<maxy; ++y ) {
slouken@0
   301
		    spot = (Uint16 *) ((Uint8 *)screen->pixels +
slouken@0
   302
				       y * screen->pitch + minx * 2);
slouken@0
   303
		    for ( x=minx; x<maxx; ++x, ++spot ) {
slouken@0
   304
			*spot = SDL_Swap16(*spot);
slouken@0
   305
		    }
slouken@0
   306
		}
slouken@0
   307
	    }
slouken@0
   308
	    break;
slouken@0
   309
slouken@0
   310
	    case 4: {
slouken@0
   311
		Uint32 *spot;
slouken@0
   312
		minx = rect->x;
slouken@0
   313
		maxx = rect->x + rect->w;
slouken@0
   314
		miny = rect->y;
slouken@0
   315
		maxy = rect->y + rect->h;
slouken@0
   316
		for ( y=miny; y<maxy; ++y ) {
slouken@0
   317
		    spot = (Uint32 *) ((Uint8 *)screen->pixels +
slouken@0
   318
				       y * screen->pitch + minx * 4);
slouken@0
   319
		    for ( x=minx; x<maxx; ++x, ++spot ) {
slouken@0
   320
			*spot = SDL_Swap32(*spot);
slouken@0
   321
		    }
slouken@0
   322
		}
slouken@0
   323
	    }
slouken@0
   324
	    break;
slouken@0
   325
slouken@0
   326
	    default:
slouken@0
   327
		/* should never get here */
slouken@0
   328
		break;
slouken@0
   329
	}
slouken@0
   330
}
slouken@0
   331
slouken@0
   332
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   333
{
slouken@0
   334
	int i;
slouken@0
   335
slouken@0
   336
	/* Check for endian-swapped X server, swap if necessary (VERY slow!) */
slouken@0
   337
	if ( swap_pixels &&
slouken@0
   338
	     ((this->screen->format->BytesPerPixel%2) == 0) ) {
slouken@0
   339
		for ( i=0; i<numrects; ++i ) {
slouken@0
   340
			if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   341
				continue;
slouken@0
   342
			}
slouken@0
   343
		        X11_SwapPixels(this->screen, rects + i);
slouken@0
   344
			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   345
				rects[i].x, rects[i].y,
slouken@0
   346
				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
slouken@0
   347
			X11_SwapPixels(this->screen, rects + i);
slouken@0
   348
		}
slouken@0
   349
	} else {
slouken@0
   350
		for ( i=0; i<numrects; ++i ) {
slouken@0
   351
			if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   352
				continue;
slouken@0
   353
			}
slouken@0
   354
			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   355
				rects[i].x, rects[i].y,
slouken@0
   356
				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
slouken@0
   357
		}
slouken@0
   358
	}
slouken@0
   359
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   360
		XFlush(GFX_Display);
slouken@0
   361
		++blit_queued;
slouken@0
   362
	} else {
slouken@0
   363
		XSync(GFX_Display, False);
slouken@0
   364
	}
slouken@0
   365
}
slouken@0
   366
slouken@0
   367
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   368
{
slouken@0
   369
#ifndef NO_SHARED_MEMORY
slouken@0
   370
	int i;
slouken@0
   371
slouken@0
   372
	for ( i=0; i<numrects; ++i ) {
slouken@0
   373
		if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   374
			continue;
slouken@0
   375
		}
slouken@0
   376
		XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   377
				rects[i].x, rects[i].y,
slouken@0
   378
				rects[i].x, rects[i].y, rects[i].w, rects[i].h,
slouken@0
   379
									False);
slouken@0
   380
	}
slouken@0
   381
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   382
		XFlush(GFX_Display);
slouken@0
   383
		++blit_queued;
slouken@0
   384
	} else {
slouken@0
   385
		XSync(GFX_Display, False);
slouken@0
   386
	}
slouken@0
   387
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   388
}
slouken@0
   389
slouken@0
   390
/* There's a problem with the automatic refreshing of the display.
slouken@0
   391
   Even though the XVideo code uses the GFX_Display to update the
slouken@0
   392
   video memory, it appears that updating the window asynchronously
slouken@0
   393
   from a different thread will cause "blackouts" of the window.
slouken@0
   394
   This is a sort of a hacked workaround for the problem.
slouken@0
   395
*/
slouken@0
   396
static int enable_autorefresh = 1;
slouken@0
   397
slouken@0
   398
void X11_DisableAutoRefresh(_THIS)
slouken@0
   399
{
slouken@0
   400
	--enable_autorefresh;
slouken@0
   401
}
slouken@0
   402
slouken@0
   403
void X11_EnableAutoRefresh(_THIS)
slouken@0
   404
{
slouken@0
   405
	++enable_autorefresh;
slouken@0
   406
}
slouken@0
   407
slouken@0
   408
void X11_RefreshDisplay(_THIS)
slouken@0
   409
{
slouken@161
   410
	/* Don't refresh a display that doesn't have an image (like GL)
slouken@161
   411
	   Instead, post an expose event so the application can refresh.
slouken@161
   412
	 */
slouken@0
   413
	if ( ! SDL_Ximage || (enable_autorefresh <= 0) ) {
slouken@161
   414
		SDL_PrivateExpose();
slouken@0
   415
		return;
slouken@0
   416
	}
slouken@0
   417
#ifndef NO_SHARED_MEMORY
slouken@0
   418
	if ( this->UpdateRects == X11_MITSHMUpdate ) {
slouken@0
   419
		XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   420
				0, 0, 0, 0, this->screen->w, this->screen->h,
slouken@0
   421
				False);
slouken@0
   422
	} else {
slouken@0
   423
#else
slouken@0
   424
	{
slouken@0
   425
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   426
		/* Check for endian-swapped X server, swap if necessary */
slouken@0
   427
		if ( swap_pixels &&
slouken@0
   428
		     ((this->screen->format->BytesPerPixel%2) == 0) ) {
slouken@0
   429
			X11_SwapAllPixels(this->screen);
slouken@0
   430
			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   431
				0, 0, 0, 0, this->screen->w, this->screen->h);
slouken@0
   432
			X11_SwapAllPixels(this->screen);
slouken@0
   433
		} else {
slouken@0
   434
			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   435
				0, 0, 0, 0, this->screen->w, this->screen->h);
slouken@0
   436
		}
slouken@0
   437
	}
slouken@0
   438
	XSync(SDL_Display, False);
slouken@0
   439
}