[Scummvm-cvs-logs] CVS: scummvm/backends/sdl sdl-common.cpp,1.43,1.44 sdl-common.h,1.19,1.20 sdl.cpp,1.27,1.28

Max Horn fingolfin at users.sourceforge.net
Wed Apr 30 12:12:15 CEST 2003


Update of /cvsroot/scummvm/scummvm/backends/sdl
In directory sc8-pr-cvs1:/tmp/cvs-serv31403

Modified Files:
	sdl-common.cpp sdl-common.h sdl.cpp 
Log Message:
moved screen mutex from smush into SDL backend (other backends have to make sure they are thread safe by themselves)

Index: sdl-common.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/sdl/sdl-common.cpp,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- sdl-common.cpp	30 Apr 2003 18:34:29 -0000	1.43
+++ sdl-common.cpp	30 Apr 2003 19:11:32 -0000	1.44
@@ -104,6 +104,8 @@
 
 	// reset mouse state
 	memset(&km, 0, sizeof(km));
+	
+	_mutex = SDL_CreateMutex();
 }
 
 OSystem_SDL_Common::~OSystem_SDL_Common() {
@@ -111,6 +113,7 @@
 		free(_dirty_checksums);
 	free(_currentPalette);
 	free(_mouseBackup);
+	SDL_DestroyMutex(_mutex);
 }
 
 void OSystem_SDL_Common::init_size(uint w, uint h) {
@@ -140,6 +143,8 @@
 	if (_screen == NULL)
 		return;
 
+	StackLock lock(_mutex);	// Lock the mutex until this function ends
+	
 	if (((uint32)buf & 3) == 0 && pitch == _screenWidth && x==0 && y==0 &&
 			w==_screenWidth && h==_screenHeight && _mode_flags&DF_WANT_RECT_OPTIM) {
 		/* Special, optimized case for full screen updates.
@@ -208,8 +213,10 @@
 	if (_mouseDrawn)
 		undraw_mouse();
 
-	// FIXME - calling copy rect repeatedly is horribly inefficient, as it (un)locks the surface repeatedly
+	// FIXME - calling copy_rect repeatedly is horribly inefficient, as it (un)locks the surface repeatedly
 	// and it performs unneeded clipping checks etc.
+	// Furthermore, this code is not correct, techincally: the pixels members of an SDLSource may be 0
+	// while it is not locked (e.g. for HW surfaces which are stored in the graphic card's VRAM).
 	
 	// vertical movement
 	if (dy > 0) {
@@ -218,7 +225,7 @@
 			copy_rect((byte *)_screen->pixels + _screenWidth * (y - dy), _screenWidth, 0, y, _screenWidth, 1);
 	} else if (dy < 0) {
 		// move up - copy from top to bottom
-		for (y = 0; y < height + dx; y++)
+		for (y = dy; y < height; y++)
 			copy_rect((byte *)_screen->pixels + _screenWidth * (y - dy), _screenWidth, 0, y, _screenWidth, 1);
 	}
 
@@ -229,7 +236,7 @@
 			copy_rect((byte *)_screen->pixels + x - dx, _screenWidth, x, 0, 1, height);
 	} else if (dx < 0)  {
 		// move left - copy from left to right
-		for (x = 0; x < _screenWidth; x++)
+		for (x = dx; x < _screenWidth; x++)
 			copy_rect((byte *)_screen->pixels + x - dx, _screenWidth, x, 0, 1, height);
 	}
 }
@@ -1149,6 +1156,8 @@
 void OSystem_SDL_Common::clear_overlay() {
 	if (!_overlayVisible)
 		return;
+	
+	StackLock lock(_mutex);	// Lock the mutex until this function ends
 	
 	// hide the mouse
 	undraw_mouse();

Index: sdl-common.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/sdl/sdl-common.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- sdl-common.h	25 Apr 2003 20:03:00 -0000	1.19
+++ sdl-common.h	30 Apr 2003 19:11:33 -0000	1.20
@@ -200,6 +200,10 @@
 	// Palette data
 	SDL_Color *_currentPalette;
 	uint _paletteDirtyStart, _paletteDirtyEnd;
+	
+	// Mutex that prevents multiple threads interferring with each other
+	// when accessing the screen.
+	SDL_mutex *_mutex;
 
 
 	void add_dirty_rgn_auto(const byte *buf);
@@ -220,6 +224,16 @@
 	void init_joystick() { _joystick = SDL_JoystickOpen(0); }
 
 	static OSystem_SDL_Common *create();
+};
+
+// Auxillary class to (un)lock a mutex on the stack
+class StackLock {
+	SDL_mutex *_mutex;
+public:
+	StackLock(SDL_mutex *mutex) : _mutex(mutex) { lock(); }
+	~StackLock() { unlock(); }
+	void lock() { SDL_mutexP(_mutex); }
+	void unlock() { SDL_mutexV(_mutex); }
 };
 
 

Index: sdl.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/backends/sdl/sdl.cpp,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- sdl.cpp	30 Apr 2003 18:07:17 -0000	1.27
+++ sdl.cpp	30 Apr 2003 19:11:33 -0000	1.28
@@ -230,6 +230,8 @@
 void OSystem_SDL::update_screen() {
 	assert(_hwscreen != NULL);
 
+	StackLock lock(_mutex);	// Lock the mutex until this function ends
+
 	// If the shake position changed, fill the dirty area with blackness
 	if (_currentShakePos != _newShakePos) {
 		SDL_Rect blackrect = {0, 0, _screenWidth * _scaleFactor, _newShakePos * _scaleFactor};
@@ -277,12 +279,12 @@
 		if (!_overlayVisible) {
 			for(r = _dirty_rect_list; r != last_rect; ++r) {
 				dst = *r;
+				dst.x++;	// Shift rect by one since 2xSai needs to acces the data around
+				dst.y++;	// any pixel to scale it, and we want to avoid mem access crashes.
 				if (_scaler_proc == Normal1x) {
 					if (SDL_BlitSurface(_screen, r, _hwscreen, &dst) != 0)
 						error("SDL_BlitSurface failed: %s", SDL_GetError());
 				} else {
-					dst.x++;	// Shift rect by one since 2xSai needs to acces the data around
-					dst.y++;	// any pixel to scale it, and we want to avoid mem access crashes.
 					if (SDL_BlitSurface(_screen, r, _tmpscreen, &dst) != 0)
 						error("SDL_BlitSurface failed: %s", SDL_GetError());
 				}





More information about the Scummvm-git-logs mailing list