src/video/x11/SDL_x11image.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 161 eb6b76a95f2d
permissions -rw-r--r--
Initial revision
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@0
    20
    slouken@devolution.com
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@0
    29
slouken@0
    30
#include "SDL_error.h"
slouken@0
    31
#include "SDL_endian.h"
slouken@0
    32
#include "SDL_x11image_c.h"
slouken@0
    33
slouken@0
    34
#if defined(__USLC__)
slouken@0
    35
#ifdef HAVE_KSTAT
slouken@0
    36
#undef HAVE_KSTAT
slouken@0
    37
#endif
slouken@0
    38
#include <unistd.h>
slouken@0
    39
#endif
slouken@0
    40
slouken@0
    41
#ifdef HAVE_KSTAT
slouken@0
    42
#include <kstat.h>
slouken@0
    43
#endif
slouken@0
    44
slouken@0
    45
#ifndef NO_SHARED_MEMORY
slouken@0
    46
slouken@0
    47
/* Shared memory information */
slouken@0
    48
extern int XShmQueryExtension(Display *dpy);	/* Not in X11 headers */
slouken@0
    49
slouken@0
    50
/* Shared memory error handler routine */
slouken@0
    51
static int shm_error;
slouken@0
    52
static int (*X_handler)(Display *, XErrorEvent *) = NULL;
slouken@0
    53
static int shm_errhandler(Display *d, XErrorEvent *e)
slouken@0
    54
{
slouken@0
    55
        if ( e->error_code == BadAccess ) {
slouken@0
    56
        	++shm_error;
slouken@0
    57
        	return(0);
slouken@0
    58
        } else
slouken@0
    59
		return(X_handler(d,e));
slouken@0
    60
}
slouken@0
    61
#endif /* ! NO_SHARED_MEMORY */
slouken@0
    62
slouken@0
    63
/* Various screen update functions available */
slouken@0
    64
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    65
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    66
slouken@0
    67
int X11_SetupImage(_THIS, SDL_Surface *screen)
slouken@0
    68
{
slouken@0
    69
#ifdef NO_SHARED_MEMORY
slouken@0
    70
	screen->pixels = malloc(screen->h*screen->pitch);
slouken@0
    71
#else
slouken@0
    72
	/* Allocate shared memory if possible */
slouken@0
    73
	if ( use_mitshm ) {
slouken@0
    74
		shminfo.shmid = shmget(IPC_PRIVATE, screen->h*screen->pitch,
slouken@0
    75
								IPC_CREAT|0777);
slouken@0
    76
		if ( shminfo.shmid >= 0 ) {
slouken@0
    77
			shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0);
slouken@0
    78
			shminfo.readOnly = False;
slouken@0
    79
			if ( shminfo.shmaddr != (char *)-1 ) {
slouken@0
    80
				shm_error = False;
slouken@0
    81
				X_handler = XSetErrorHandler(shm_errhandler);
slouken@0
    82
				XShmAttach(SDL_Display, &shminfo);
slouken@0
    83
				XSync(SDL_Display, True);
slouken@0
    84
				XSetErrorHandler(X_handler);
slouken@0
    85
				if ( shm_error == True )
slouken@0
    86
					shmdt(shminfo.shmaddr);
slouken@0
    87
			} else {
slouken@0
    88
				shm_error = True;
slouken@0
    89
			}
slouken@0
    90
			shmctl(shminfo.shmid, IPC_RMID, NULL);
slouken@0
    91
		} else {
slouken@0
    92
			shm_error = True;
slouken@0
    93
		}
slouken@0
    94
		if ( shm_error == True )
slouken@0
    95
			use_mitshm = 0;
slouken@0
    96
	}
slouken@0
    97
	if ( use_mitshm ) {
slouken@0
    98
		screen->pixels = shminfo.shmaddr;
slouken@0
    99
	} else {
slouken@0
   100
		screen->pixels = malloc(screen->h*screen->pitch);
slouken@0
   101
	}
slouken@0
   102
#endif /* NO_SHARED_MEMORY */
slouken@0
   103
	if ( screen->pixels == NULL ) {
slouken@0
   104
		SDL_OutOfMemory();
slouken@0
   105
		return(-1);
slouken@0
   106
	}
slouken@0
   107
slouken@0
   108
#ifdef NO_SHARED_MEMORY
slouken@0
   109
	{
slouken@0
   110
 	        int bpp = screen->format->BytesPerPixel;
slouken@0
   111
		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
slouken@0
   112
					  this->hidden->depth, ZPixmap, 0,
slouken@0
   113
					  (char *)screen->pixels, 
slouken@0
   114
					  screen->w, screen->h,
slouken@0
   115
					  (bpp == 3) ? 32 : bpp * 8,
slouken@0
   116
					  0);
slouken@0
   117
	}
slouken@0
   118
#else
slouken@0
   119
	if ( use_mitshm ) {
slouken@0
   120
		SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual,
slouken@0
   121
					     this->hidden->depth, ZPixmap,
slouken@0
   122
					     shminfo.shmaddr, &shminfo, 
slouken@0
   123
					     screen->w, screen->h);
slouken@0
   124
	} else {
slouken@0
   125
 	        int bpp = screen->format->BytesPerPixel;
slouken@0
   126
		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
slouken@0
   127
					  this->hidden->depth, ZPixmap, 0,
slouken@0
   128
					  (char *)screen->pixels, 
slouken@0
   129
					  screen->w, screen->h,
slouken@0
   130
					  (bpp == 3) ? 32 : bpp * 8,
slouken@0
   131
					  0);
slouken@0
   132
	}
slouken@0
   133
#endif /* NO_SHARED_MEMORY */
slouken@0
   134
	if ( SDL_Ximage == NULL ) {
slouken@0
   135
		SDL_SetError("Couldn't create XImage");
slouken@0
   136
#ifndef NO_SHARED_MEMORY
slouken@0
   137
		if ( use_mitshm ) {
slouken@0
   138
			XShmDetach(SDL_Display, &shminfo);
slouken@0
   139
			XSync(SDL_Display, False);
slouken@0
   140
			shmdt(shminfo.shmaddr);
slouken@0
   141
			screen->pixels = NULL;
slouken@0
   142
		}
slouken@0
   143
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   144
		return(-1);
slouken@0
   145
	}
slouken@0
   146
	screen->pitch = SDL_Ximage->bytes_per_line;
slouken@0
   147
slouken@0
   148
	/* Determine what blit function to use */
slouken@0
   149
#ifdef NO_SHARED_MEMORY
slouken@0
   150
	this->UpdateRects = X11_NormalUpdate;
slouken@0
   151
#else
slouken@0
   152
	if ( use_mitshm ) {
slouken@0
   153
		this->UpdateRects = X11_MITSHMUpdate;
slouken@0
   154
	} else {
slouken@0
   155
		this->UpdateRects = X11_NormalUpdate;
slouken@0
   156
	}
slouken@0
   157
#endif
slouken@0
   158
	return(0);
slouken@0
   159
}
slouken@0
   160
slouken@0
   161
void X11_DestroyImage(_THIS, SDL_Surface *screen)
slouken@0
   162
{
slouken@0
   163
	if ( SDL_Ximage ) {
slouken@0
   164
		XDestroyImage(SDL_Ximage);
slouken@0
   165
#ifndef NO_SHARED_MEMORY
slouken@0
   166
		if ( use_mitshm ) {
slouken@0
   167
			XShmDetach(SDL_Display, &shminfo);
slouken@0
   168
			XSync(SDL_Display, False);
slouken@0
   169
			shmdt(shminfo.shmaddr);
slouken@0
   170
		}
slouken@0
   171
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   172
		SDL_Ximage = NULL;
slouken@0
   173
	}
slouken@0
   174
	if ( screen ) {
slouken@0
   175
		screen->pixels = NULL;
slouken@0
   176
	}
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
/* This is a hack to see whether this system has more than 1 CPU */
slouken@0
   180
static int num_CPU(void)
slouken@0
   181
{
slouken@0
   182
       static int num_cpus = 0;
slouken@0
   183
slouken@0
   184
       if(!num_cpus) {
slouken@0
   185
#ifdef linux
slouken@0
   186
           char line[BUFSIZ];
slouken@0
   187
           FILE *pstat = fopen("/proc/stat", "r");
slouken@0
   188
           if ( pstat ) {
slouken@0
   189
               while ( fgets(line, sizeof(line), pstat) ) {
slouken@0
   190
                   if (memcmp(line, "cpu", 3) == 0 && line[3] != ' ') {
slouken@0
   191
                       ++num_cpus;
slouken@0
   192
                   }
slouken@0
   193
               }
slouken@0
   194
               fclose(pstat);
slouken@0
   195
           }
slouken@0
   196
#elif defined(HAVE_KSTAT)
slouken@0
   197
           kstat_ctl_t *kc = kstat_open();
slouken@0
   198
           kstat_t *ks;
slouken@0
   199
           kstat_named_t *kn;
slouken@0
   200
           if(kc) {
slouken@0
   201
               if((ks = kstat_lookup(kc, "unix", -1, "system_misc"))
slouken@0
   202
                  && kstat_read(kc, ks, NULL) != -1
slouken@0
   203
                  && (kn = kstat_data_lookup(ks, "ncpus")))
slouken@0
   204
#ifdef KSTAT_DATA_UINT32
slouken@0
   205
                   num_cpus = kn->value.ui32;
slouken@0
   206
#else
slouken@0
   207
                   num_cpus = kn->value.ul; /* needed in solaris <2.6 */
slouken@0
   208
#endif
slouken@0
   209
               kstat_close(kc);
slouken@0
   210
           }
slouken@0
   211
#elif defined(__USLC__)
slouken@0
   212
           num_cpus = (int)sysconf(_SC_NPROCESSORS_CONF);
slouken@0
   213
#endif
slouken@0
   214
           if ( num_cpus <= 0 ) {
slouken@0
   215
               num_cpus = 1;
slouken@0
   216
           }
slouken@0
   217
       }
slouken@0
   218
       return num_cpus;
slouken@0
   219
}
slouken@0
   220
slouken@0
   221
int X11_ResizeImage(_THIS, SDL_Surface *screen, Uint32 flags)
slouken@0
   222
{
slouken@0
   223
	int retval;
slouken@0
   224
slouken@0
   225
	X11_DestroyImage(this, screen);
slouken@0
   226
        if ( flags & SDL_OPENGL ) {  /* No image when using GL */
slouken@0
   227
        	retval = 0;
slouken@0
   228
        } else {
slouken@0
   229
		retval = X11_SetupImage(this, screen);
slouken@0
   230
		/* We support asynchronous blitting on the display */
slouken@0
   231
		if ( flags & SDL_ASYNCBLIT ) {
slouken@0
   232
			/* This is actually slower on single-CPU systems,
slouken@0
   233
			   probably because of CPU contention between the
slouken@0
   234
			   X server and the application.
slouken@0
   235
			   Note: Is this still true with XFree86 4.0?
slouken@0
   236
			*/
slouken@0
   237
			if ( num_CPU() > 1 ) {
slouken@0
   238
				screen->flags |= SDL_ASYNCBLIT;
slouken@0
   239
			}
slouken@0
   240
		}
slouken@0
   241
	}
slouken@0
   242
	return(retval);
slouken@0
   243
}
slouken@0
   244
slouken@0
   245
/* We don't actually allow hardware surfaces other than the main one */
slouken@0
   246
int X11_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   247
{
slouken@0
   248
	return(-1);
slouken@0
   249
}
slouken@0
   250
void X11_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   251
{
slouken@0
   252
	return;
slouken@0
   253
}
slouken@0
   254
slouken@0
   255
int X11_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   256
{
slouken@0
   257
	if ( (surface == SDL_VideoSurface) && blit_queued ) {
slouken@0
   258
		XSync(GFX_Display, False);
slouken@0
   259
		blit_queued = 0;
slouken@0
   260
	}
slouken@0
   261
	return(0);
slouken@0
   262
}
slouken@0
   263
void X11_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   264
{
slouken@0
   265
	return;
slouken@0
   266
}
slouken@0
   267
slouken@0
   268
int X11_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   269
{
slouken@0
   270
	return(0);
slouken@0
   271
}
slouken@0
   272
slouken@0
   273
/* Byte-swap the pixels in the display image */
slouken@0
   274
static void X11_SwapAllPixels(SDL_Surface *screen)
slouken@0
   275
{
slouken@0
   276
	int x, y;
slouken@0
   277
slouken@0
   278
	switch (screen->format->BytesPerPixel) {
slouken@0
   279
	    case 2: {
slouken@0
   280
		Uint16 *spot;
slouken@0
   281
		for ( y=0; y<screen->h; ++y ) {
slouken@0
   282
			spot = (Uint16 *) ((Uint8 *)screen->pixels +
slouken@0
   283
						y * screen->pitch);
slouken@0
   284
			for ( x=0; x<screen->w; ++x, ++spot ) {
slouken@0
   285
				*spot = SDL_Swap16(*spot);
slouken@0
   286
			}
slouken@0
   287
		}
slouken@0
   288
	    }
slouken@0
   289
	    break;
slouken@0
   290
slouken@0
   291
	    case 4: {
slouken@0
   292
		Uint32 *spot;
slouken@0
   293
		for ( y=0; y<screen->h; ++y ) {
slouken@0
   294
			spot = (Uint32 *) ((Uint8 *)screen->pixels +
slouken@0
   295
						y * screen->pitch);
slouken@0
   296
			for ( x=0; x<screen->w; ++x, ++spot ) {
slouken@0
   297
				*spot = SDL_Swap32(*spot);
slouken@0
   298
			}
slouken@0
   299
		}
slouken@0
   300
	    }
slouken@0
   301
	    break;
slouken@0
   302
slouken@0
   303
	    default:
slouken@0
   304
		/* should never get here */
slouken@0
   305
		break;
slouken@0
   306
	}
slouken@0
   307
}
slouken@0
   308
static void X11_SwapPixels(SDL_Surface *screen, SDL_Rect *rect)
slouken@0
   309
{
slouken@0
   310
	int x, minx, maxx;
slouken@0
   311
	int y, miny, maxy;
slouken@0
   312
slouken@0
   313
	switch (screen->format->BytesPerPixel) {
slouken@0
   314
	    case 2: {
slouken@0
   315
		Uint16 *spot;
slouken@0
   316
		minx = rect->x;
slouken@0
   317
		maxx = rect->x + rect->w;
slouken@0
   318
		miny = rect->y;
slouken@0
   319
		maxy = rect->y + rect->h;
slouken@0
   320
		for ( y=miny; y<maxy; ++y ) {
slouken@0
   321
		    spot = (Uint16 *) ((Uint8 *)screen->pixels +
slouken@0
   322
				       y * screen->pitch + minx * 2);
slouken@0
   323
		    for ( x=minx; x<maxx; ++x, ++spot ) {
slouken@0
   324
			*spot = SDL_Swap16(*spot);
slouken@0
   325
		    }
slouken@0
   326
		}
slouken@0
   327
	    }
slouken@0
   328
	    break;
slouken@0
   329
slouken@0
   330
	    case 4: {
slouken@0
   331
		Uint32 *spot;
slouken@0
   332
		minx = rect->x;
slouken@0
   333
		maxx = rect->x + rect->w;
slouken@0
   334
		miny = rect->y;
slouken@0
   335
		maxy = rect->y + rect->h;
slouken@0
   336
		for ( y=miny; y<maxy; ++y ) {
slouken@0
   337
		    spot = (Uint32 *) ((Uint8 *)screen->pixels +
slouken@0
   338
				       y * screen->pitch + minx * 4);
slouken@0
   339
		    for ( x=minx; x<maxx; ++x, ++spot ) {
slouken@0
   340
			*spot = SDL_Swap32(*spot);
slouken@0
   341
		    }
slouken@0
   342
		}
slouken@0
   343
	    }
slouken@0
   344
	    break;
slouken@0
   345
slouken@0
   346
	    default:
slouken@0
   347
		/* should never get here */
slouken@0
   348
		break;
slouken@0
   349
	}
slouken@0
   350
}
slouken@0
   351
slouken@0
   352
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   353
{
slouken@0
   354
	int i;
slouken@0
   355
slouken@0
   356
	/* Check for endian-swapped X server, swap if necessary (VERY slow!) */
slouken@0
   357
	if ( swap_pixels &&
slouken@0
   358
	     ((this->screen->format->BytesPerPixel%2) == 0) ) {
slouken@0
   359
		for ( i=0; i<numrects; ++i ) {
slouken@0
   360
			if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   361
				continue;
slouken@0
   362
			}
slouken@0
   363
		        X11_SwapPixels(this->screen, rects + i);
slouken@0
   364
			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   365
				rects[i].x, rects[i].y,
slouken@0
   366
				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
slouken@0
   367
			X11_SwapPixels(this->screen, rects + i);
slouken@0
   368
		}
slouken@0
   369
	} else {
slouken@0
   370
		for ( i=0; i<numrects; ++i ) {
slouken@0
   371
			if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   372
				continue;
slouken@0
   373
			}
slouken@0
   374
			XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   375
				rects[i].x, rects[i].y,
slouken@0
   376
				rects[i].x, rects[i].y, rects[i].w, rects[i].h);
slouken@0
   377
		}
slouken@0
   378
	}
slouken@0
   379
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   380
		XFlush(GFX_Display);
slouken@0
   381
		++blit_queued;
slouken@0
   382
	} else {
slouken@0
   383
		XSync(GFX_Display, False);
slouken@0
   384
	}
slouken@0
   385
}
slouken@0
   386
slouken@0
   387
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   388
{
slouken@0
   389
#ifndef NO_SHARED_MEMORY
slouken@0
   390
	int i;
slouken@0
   391
slouken@0
   392
	for ( i=0; i<numrects; ++i ) {
slouken@0
   393
		if ( ! rects[i].w ) { /* Clipped? */
slouken@0
   394
			continue;
slouken@0
   395
		}
slouken@0
   396
		XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   397
				rects[i].x, rects[i].y,
slouken@0
   398
				rects[i].x, rects[i].y, rects[i].w, rects[i].h,
slouken@0
   399
									False);
slouken@0
   400
	}
slouken@0
   401
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   402
		XFlush(GFX_Display);
slouken@0
   403
		++blit_queued;
slouken@0
   404
	} else {
slouken@0
   405
		XSync(GFX_Display, False);
slouken@0
   406
	}
slouken@0
   407
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   408
}
slouken@0
   409
slouken@0
   410
/* There's a problem with the automatic refreshing of the display.
slouken@0
   411
   Even though the XVideo code uses the GFX_Display to update the
slouken@0
   412
   video memory, it appears that updating the window asynchronously
slouken@0
   413
   from a different thread will cause "blackouts" of the window.
slouken@0
   414
   This is a sort of a hacked workaround for the problem.
slouken@0
   415
*/
slouken@0
   416
static int enable_autorefresh = 1;
slouken@0
   417
slouken@0
   418
void X11_DisableAutoRefresh(_THIS)
slouken@0
   419
{
slouken@0
   420
	--enable_autorefresh;
slouken@0
   421
}
slouken@0
   422
slouken@0
   423
void X11_EnableAutoRefresh(_THIS)
slouken@0
   424
{
slouken@0
   425
	++enable_autorefresh;
slouken@0
   426
}
slouken@0
   427
slouken@0
   428
void X11_RefreshDisplay(_THIS)
slouken@0
   429
{
slouken@0
   430
	/* Don't refresh a display that doesn't have an image (like GL) */
slouken@0
   431
	if ( ! SDL_Ximage || (enable_autorefresh <= 0) ) {
slouken@0
   432
		return;
slouken@0
   433
	}
slouken@0
   434
#ifndef NO_SHARED_MEMORY
slouken@0
   435
	if ( this->UpdateRects == X11_MITSHMUpdate ) {
slouken@0
   436
		XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   437
				0, 0, 0, 0, this->screen->w, this->screen->h,
slouken@0
   438
				False);
slouken@0
   439
	} else {
slouken@0
   440
#else
slouken@0
   441
	{
slouken@0
   442
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   443
		/* Check for endian-swapped X server, swap if necessary */
slouken@0
   444
		if ( swap_pixels &&
slouken@0
   445
		     ((this->screen->format->BytesPerPixel%2) == 0) ) {
slouken@0
   446
			X11_SwapAllPixels(this->screen);
slouken@0
   447
			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   448
				0, 0, 0, 0, this->screen->w, this->screen->h);
slouken@0
   449
			X11_SwapAllPixels(this->screen);
slouken@0
   450
		} else {
slouken@0
   451
			XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   452
				0, 0, 0, 0, this->screen->w, this->screen->h);
slouken@0
   453
		}
slouken@0
   454
	}
slouken@0
   455
	XSync(SDL_Display, False);
slouken@0
   456
}