src/video/x11/SDL_x11image.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 270 37fa1484f71b
child 551 b0c5a13a4243
permissions -rw-r--r--
Updated copyright information for 2002
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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@270
    47
        	shm_error = 1;
slouken@0
    48
        	return(0);
slouken@0
    49
        } else
slouken@0
    50
		return(X_handler(d,e));
slouken@0
    51
}
slouken@270
    52
slouken@270
    53
static void try_mitshm(_THIS, SDL_Surface *screen)
slouken@270
    54
{
slouken@270
    55
	if(!use_mitshm)
slouken@270
    56
		return;
slouken@270
    57
	shminfo.shmid = shmget(IPC_PRIVATE, screen->h*screen->pitch,
slouken@270
    58
			       IPC_CREAT | 0777);
slouken@270
    59
	if ( shminfo.shmid >= 0 ) {
slouken@270
    60
		shminfo.shmaddr = (char *)shmat(shminfo.shmid, 0, 0);
slouken@270
    61
		shminfo.readOnly = False;
slouken@270
    62
		if ( shminfo.shmaddr != (char *)-1 ) {
slouken@270
    63
			shm_error = False;
slouken@270
    64
			X_handler = XSetErrorHandler(shm_errhandler);
slouken@270
    65
			XShmAttach(SDL_Display, &shminfo);
slouken@270
    66
			XSync(SDL_Display, True);
slouken@270
    67
			XSetErrorHandler(X_handler);
slouken@270
    68
			if (shm_error)
slouken@270
    69
				shmdt(shminfo.shmaddr);
slouken@270
    70
		} else {
slouken@270
    71
			shm_error = True;
slouken@270
    72
		}
slouken@270
    73
		shmctl(shminfo.shmid, IPC_RMID, NULL);
slouken@270
    74
	} else {
slouken@270
    75
		shm_error = True;
slouken@270
    76
	}
slouken@270
    77
	if ( shm_error )
slouken@270
    78
		use_mitshm = 0;
slouken@270
    79
	if ( use_mitshm )
slouken@270
    80
		screen->pixels = shminfo.shmaddr;
slouken@270
    81
}
slouken@0
    82
#endif /* ! NO_SHARED_MEMORY */
slouken@0
    83
slouken@0
    84
/* Various screen update functions available */
slouken@0
    85
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    86
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
    87
slouken@0
    88
int X11_SetupImage(_THIS, SDL_Surface *screen)
slouken@0
    89
{
slouken@270
    90
#ifndef NO_SHARED_MEMORY
slouken@270
    91
	try_mitshm(this, screen);
slouken@270
    92
	if(use_mitshm) {
slouken@270
    93
		SDL_Ximage = XShmCreateImage(SDL_Display, SDL_Visual,
slouken@270
    94
					     this->hidden->depth, ZPixmap,
slouken@270
    95
					     shminfo.shmaddr, &shminfo, 
slouken@270
    96
					     screen->w, screen->h);
slouken@270
    97
		if(!SDL_Ximage) {
slouken@270
    98
			XShmDetach(SDL_Display, &shminfo);
slouken@270
    99
			XSync(SDL_Display, False);
slouken@270
   100
			shmdt(shminfo.shmaddr);
slouken@270
   101
			screen->pixels = NULL;
slouken@270
   102
			goto error;
slouken@0
   103
		}
slouken@270
   104
		this->UpdateRects = X11_MITSHMUpdate;
slouken@0
   105
	}
slouken@270
   106
#endif /* not NO_SHARED_MEMORY */
slouken@270
   107
	if(!use_mitshm) {
slouken@270
   108
		int bpp;
slouken@0
   109
		screen->pixels = malloc(screen->h*screen->pitch);
slouken@270
   110
		if ( screen->pixels == NULL ) {
slouken@270
   111
			SDL_OutOfMemory();
slouken@270
   112
			return -1;
slouken@270
   113
		}
slouken@270
   114
 	        bpp = screen->format->BytesPerPixel;
slouken@0
   115
		SDL_Ximage = XCreateImage(SDL_Display, SDL_Visual,
slouken@0
   116
					  this->hidden->depth, ZPixmap, 0,
slouken@0
   117
					  (char *)screen->pixels, 
slouken@0
   118
					  screen->w, screen->h,
slouken@270
   119
					  32, 0);
slouken@270
   120
		if ( SDL_Ximage == NULL )
slouken@270
   121
			goto error;
slouken@270
   122
		/* XPutImage will convert byte sex automatically */
slouken@270
   123
		SDL_Ximage->byte_order = (SDL_BYTEORDER == SDL_BIG_ENDIAN)
slouken@270
   124
			                 ? MSBFirst : LSBFirst;
slouken@270
   125
		this->UpdateRects = X11_NormalUpdate;
slouken@0
   126
	}
slouken@0
   127
	screen->pitch = SDL_Ximage->bytes_per_line;
slouken@270
   128
	return(0);
slouken@0
   129
slouken@270
   130
error:
slouken@270
   131
	SDL_SetError("Couldn't create XImage");
slouken@270
   132
	return 1;
slouken@0
   133
}
slouken@0
   134
slouken@0
   135
void X11_DestroyImage(_THIS, SDL_Surface *screen)
slouken@0
   136
{
slouken@0
   137
	if ( SDL_Ximage ) {
slouken@0
   138
		XDestroyImage(SDL_Ximage);
slouken@0
   139
#ifndef NO_SHARED_MEMORY
slouken@0
   140
		if ( use_mitshm ) {
slouken@0
   141
			XShmDetach(SDL_Display, &shminfo);
slouken@0
   142
			XSync(SDL_Display, False);
slouken@0
   143
			shmdt(shminfo.shmaddr);
slouken@0
   144
		}
slouken@0
   145
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   146
		SDL_Ximage = NULL;
slouken@0
   147
	}
slouken@0
   148
	if ( screen ) {
slouken@0
   149
		screen->pixels = NULL;
slouken@0
   150
	}
slouken@0
   151
}
slouken@0
   152
slouken@166
   153
/* Determine the number of CPUs in the system */
slouken@0
   154
static int num_CPU(void)
slouken@0
   155
{
slouken@0
   156
       static int num_cpus = 0;
slouken@0
   157
slouken@0
   158
       if(!num_cpus) {
slouken@166
   159
#if defined(__linux)
slouken@0
   160
           char line[BUFSIZ];
slouken@0
   161
           FILE *pstat = fopen("/proc/stat", "r");
slouken@0
   162
           if ( pstat ) {
slouken@0
   163
               while ( fgets(line, sizeof(line), pstat) ) {
slouken@0
   164
                   if (memcmp(line, "cpu", 3) == 0 && line[3] != ' ') {
slouken@0
   165
                       ++num_cpus;
slouken@0
   166
                   }
slouken@0
   167
               }
slouken@0
   168
               fclose(pstat);
slouken@0
   169
           }
slouken@166
   170
#elif defined(_SC_NPROCESSORS_ONLN)
slouken@166
   171
	   /* number of processors online (SVR4.0MP compliant machines) */
slouken@166
   172
           num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
slouken@166
   173
#elif defined(_SC_NPROCESSORS_CONF)
slouken@166
   174
	   /* number of processors configured (SVR4.0MP compliant machines) */
slouken@166
   175
           num_cpus = sysconf(_SC_NPROCESSORS_CONF);
slouken@0
   176
#endif
slouken@0
   177
           if ( num_cpus <= 0 ) {
slouken@0
   178
               num_cpus = 1;
slouken@0
   179
           }
slouken@0
   180
       }
slouken@0
   181
       return num_cpus;
slouken@0
   182
}
slouken@0
   183
slouken@0
   184
int X11_ResizeImage(_THIS, SDL_Surface *screen, Uint32 flags)
slouken@0
   185
{
slouken@0
   186
	int retval;
slouken@0
   187
slouken@0
   188
	X11_DestroyImage(this, screen);
slouken@0
   189
        if ( flags & SDL_OPENGL ) {  /* No image when using GL */
slouken@0
   190
        	retval = 0;
slouken@0
   191
        } else {
slouken@0
   192
		retval = X11_SetupImage(this, screen);
slouken@0
   193
		/* We support asynchronous blitting on the display */
slouken@0
   194
		if ( flags & SDL_ASYNCBLIT ) {
slouken@0
   195
			/* This is actually slower on single-CPU systems,
slouken@0
   196
			   probably because of CPU contention between the
slouken@0
   197
			   X server and the application.
slouken@0
   198
			   Note: Is this still true with XFree86 4.0?
slouken@0
   199
			*/
slouken@0
   200
			if ( num_CPU() > 1 ) {
slouken@0
   201
				screen->flags |= SDL_ASYNCBLIT;
slouken@0
   202
			}
slouken@0
   203
		}
slouken@0
   204
	}
slouken@0
   205
	return(retval);
slouken@0
   206
}
slouken@0
   207
slouken@0
   208
/* We don't actually allow hardware surfaces other than the main one */
slouken@0
   209
int X11_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   210
{
slouken@0
   211
	return(-1);
slouken@0
   212
}
slouken@0
   213
void X11_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   214
{
slouken@0
   215
	return;
slouken@0
   216
}
slouken@0
   217
slouken@0
   218
int X11_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   219
{
slouken@0
   220
	if ( (surface == SDL_VideoSurface) && blit_queued ) {
slouken@0
   221
		XSync(GFX_Display, False);
slouken@0
   222
		blit_queued = 0;
slouken@0
   223
	}
slouken@0
   224
	return(0);
slouken@0
   225
}
slouken@0
   226
void X11_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   227
{
slouken@0
   228
	return;
slouken@0
   229
}
slouken@0
   230
slouken@0
   231
int X11_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   232
{
slouken@0
   233
	return(0);
slouken@0
   234
}
slouken@0
   235
slouken@0
   236
static void X11_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   237
{
slouken@0
   238
	int i;
slouken@270
   239
	
slouken@270
   240
	for (i = 0; i < numrects; ++i) {
slouken@270
   241
		if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */
slouken@270
   242
			continue;
slouken@0
   243
		}
slouken@270
   244
		XPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@270
   245
			  rects[i].x, rects[i].y,
slouken@270
   246
			  rects[i].x, rects[i].y, rects[i].w, rects[i].h);
slouken@0
   247
	}
slouken@0
   248
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   249
		XFlush(GFX_Display);
slouken@270
   250
		blit_queued = 1;
slouken@0
   251
	} else {
slouken@0
   252
		XSync(GFX_Display, False);
slouken@0
   253
	}
slouken@0
   254
}
slouken@0
   255
slouken@0
   256
static void X11_MITSHMUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   257
{
slouken@0
   258
#ifndef NO_SHARED_MEMORY
slouken@0
   259
	int i;
slouken@0
   260
slouken@0
   261
	for ( i=0; i<numrects; ++i ) {
slouken@270
   262
		if ( rects[i].w == 0 || rects[i].h == 0 ) { /* Clipped? */
slouken@0
   263
			continue;
slouken@0
   264
		}
slouken@0
   265
		XShmPutImage(GFX_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   266
				rects[i].x, rects[i].y,
slouken@0
   267
				rects[i].x, rects[i].y, rects[i].w, rects[i].h,
slouken@0
   268
									False);
slouken@0
   269
	}
slouken@0
   270
	if ( SDL_VideoSurface->flags & SDL_ASYNCBLIT ) {
slouken@0
   271
		XFlush(GFX_Display);
slouken@270
   272
		blit_queued = 1;
slouken@0
   273
	} else {
slouken@0
   274
		XSync(GFX_Display, False);
slouken@0
   275
	}
slouken@0
   276
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   277
}
slouken@0
   278
slouken@0
   279
/* There's a problem with the automatic refreshing of the display.
slouken@0
   280
   Even though the XVideo code uses the GFX_Display to update the
slouken@0
   281
   video memory, it appears that updating the window asynchronously
slouken@0
   282
   from a different thread will cause "blackouts" of the window.
slouken@0
   283
   This is a sort of a hacked workaround for the problem.
slouken@0
   284
*/
slouken@0
   285
static int enable_autorefresh = 1;
slouken@0
   286
slouken@0
   287
void X11_DisableAutoRefresh(_THIS)
slouken@0
   288
{
slouken@0
   289
	--enable_autorefresh;
slouken@0
   290
}
slouken@0
   291
slouken@0
   292
void X11_EnableAutoRefresh(_THIS)
slouken@0
   293
{
slouken@0
   294
	++enable_autorefresh;
slouken@0
   295
}
slouken@0
   296
slouken@0
   297
void X11_RefreshDisplay(_THIS)
slouken@0
   298
{
slouken@161
   299
	/* Don't refresh a display that doesn't have an image (like GL)
slouken@161
   300
	   Instead, post an expose event so the application can refresh.
slouken@161
   301
	 */
slouken@0
   302
	if ( ! SDL_Ximage || (enable_autorefresh <= 0) ) {
slouken@161
   303
		SDL_PrivateExpose();
slouken@0
   304
		return;
slouken@0
   305
	}
slouken@0
   306
#ifndef NO_SHARED_MEMORY
slouken@0
   307
	if ( this->UpdateRects == X11_MITSHMUpdate ) {
slouken@0
   308
		XShmPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@0
   309
				0, 0, 0, 0, this->screen->w, this->screen->h,
slouken@0
   310
				False);
slouken@270
   311
	} else
slouken@270
   312
#endif /* ! NO_SHARED_MEMORY */
slouken@0
   313
	{
slouken@270
   314
		XPutImage(SDL_Display, SDL_Window, SDL_GC, SDL_Ximage,
slouken@270
   315
			  0, 0, 0, 0, this->screen->w, this->screen->h);
slouken@0
   316
	}
slouken@0
   317
	XSync(SDL_Display, False);
slouken@0
   318
}