view src/video/symbian/EKA2/dsa_old.cpp @ 4157:baf615f9f2a0 SDL-1.2

Date: Thu, 16 Oct 2008 20:27:34 +0400 From: "Ilya Kasnacheev" <ilya.kasnacheev@gmail.com> To: sdl@lists.libsdl.org Subject: [SDL] SDL for Windows CE: a few GAPI patches Hi *! I've just ported a POWDER roguelike ( http://www.zincland.com/powder/ ) to Windows CE (PDAs, Windows Mobile/Pocket PC). To do that, I had to get libsdl working. Thanks for the awesome project files, it built without a hitch. Nevertheless, I've found quite a few bugs in Windows CE (GAPI) SDL implementation, which I've solved and now present as a serie of patches. I'll try carefully annotate them. Please annotate them so I can work toward accepting them into the main source tree since without them SDL isn't really working on Windows CE (I wonder why nobody fixed them before, btw: why isn't SDL popular as a way to develop Windows CE games? Where are no ports?) These changes can't be considered flawless, but they can be considered working because I've yet to hear complains about things I fixed and POWDER build for Windows CE is now considered stable. Note: my comments start with !!, delete them before applying. diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.c SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c --- SDL-1.2.13/src/video/gapi/SDL_gapivideo.c 2007-12-31 07:48:00.000000000 +0300 +++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.c 2008-10-16 20:02:11.000000000 +0400 @@ -643,6 +643,7 @@ } gapi->userOrientation = SDL_ORIENTATION_UP; + gapi->systemOrientation = SDL_ORIENTATION_UP; video->flags = SDL_FULLSCREEN; /* Clear flags, GAPI supports fullscreen only */ /* GAPI or VGA? */ @@ -661,18 +662,21 @@ } /* detect user landscape mode */ - if( (width > height) && (GetSystemMetrics(SM_CXSCREEN) < GetSystemMetrics(SM_CYSCREEN))) + if( (width > height) && (gapi->gxProperties.cxWidth < gapi->gxProperties.cyHeight)) gapi->userOrientation = SDL_ORIENTATION_RIGHT; + if(GetSystemMetrics(SM_CYSCREEN) < GetSystemMetrics(SM_CXSCREEN)) + gapi->systemOrientation = SDL_ORIENTATION_RIGHT; + /* shall we apply hires fix? for example when we do not use hires resource */ gapi->hiresFix = 0; - if( gapi->userOrientation == SDL_ORIENTATION_RIGHT ) + if( gapi->systemOrientation == gapi->userOrientation ) { - if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height > GetSystemMetrics(SM_CXSCREEN))) + if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height > GetSystemMetrics(SM_CYSCREEN))) gapi->hiresFix = 1; } else - if( (width > GetSystemMetrics(SM_CXSCREEN)) || (height > GetSystemMetrics(SM_CYSCREEN))) - if( !((width == GetSystemMetrics(SM_CYSCREEN)) && (height == GetSystemMetrics(SM_CXSCREEN)))) // user portrait, device landscape + if( (width > GetSystemMetrics(SM_CYSCREEN)) || (height > GetSystemMetrics(SM_CXSCREEN))) +// if( !((width == gapi->gxProperties.cyHeight) && (height == gapi->gxProperties.cxWidth))) // user portrait, device landscape gapi->hiresFix = 1; switch( gapi->userOrientation ) !! It used to query system metrics which return dimensions according to screen !! orientation, which can really be portrait, left landscape or right landscape. !! This is presumably incorrect because we couldn't care less about user mode !! dimensions - all we want are the GAPI framebuffer dimensions, which only match !! user dimensions in one of possible orientations. !! There's a fair dose of cargo cult programming involved in this fix, but it !! used to work only in one orientation (portrait for PDAs, where frame-buffer !! have same orientation as user screen), and now it works on all orientations. @@ -742,21 +746,30 @@ WIN_FlushMessageQueue(); /* Open GAPI display */ - if( !gapi->useVga && this->hidden->useGXOpenDisplay ) + if( !gapi->useVga && this->hidden->useGXOpenDisplay && !this->hidden->alreadyGXOpened ) + { + this->hidden->alreadyGXOpened = 1; if( !gapi->gxFunc.GXOpenDisplay(SDL_Window, GX_FULLSCREEN) ) { SDL_SetError("Couldn't initialize GAPI"); return(NULL); } + } #if REPORT_VIDEO_INFO printf("Video properties:\n"); printf("display bpp: %d\n", gapi->gxProperties.cBPP); printf("display width: %d\n", gapi->gxProperties.cxWidth); printf("display height: %d\n", gapi->gxProperties.cyHeight); + printf("system display width: %d\n", GetSystemMetrics(SM_CXSCREEN)); + printf("system display height: %d\n", GetSystemMetrics(SM_CYSCREEN)); printf("x pitch: %d\n", gapi->gxProperties.cbxPitch); printf("y pitch: %d\n", gapi->gxProperties.cbyPitch); printf("gapi flags: 0x%x\n", gapi->gxProperties.ffFormat); + printf("user orientation: %d\n", gapi->userOrientation); + printf("system orientation: %d\n", gapi->userOrientation); + printf("gapi orientation: %d\n", gapi->gapiOrientation); + if( !gapi->useVga && this->hidden->useGXOpenDisplay && gapi->needUpdate) { !! Previous version used to call gapi->gxFunc.GXOpenDisplay each time the video !! mode would be changed. You shouldn't, because this call has a meaning "Lock the !! GAPI framebuffer, designate it as busy", so the second call will fail (it is !! already locked/busy). !! Testing might not find that because most programs set up the video mode only !! once, but POWDER does this once in a while, so it crashed when in 320x240 mode !! (640x480 mode doesn't use that code, it worked fine). diff -bru SDL-1.2.13/src/video/gapi/SDL_gapivideo.h SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h --- SDL-1.2.13/src/video/gapi/SDL_gapivideo.h 2007-12-31 07:48:00.000000000 +0300 +++ SDL-1.2.13-new/src/video/gapi/SDL_gapivideo.h 2008-10-16 20:02:11.000000000 +0400 @@ -132,12 +132,17 @@ #define NUM_MODELISTS 4 /* 8, 16, 24, and 32 bits-per-pixel */ int SDL_nummodes[NUM_MODELISTS]; SDL_Rect **SDL_modelist[NUM_MODELISTS]; + // The orientation of the video mode user wants to get + // Probably restricted to UP and RIGHT enum SDL_ScreenOrientation userOrientation; int invert; char hiresFix; // using hires mode without defining hires resource // -------------- int useGXOpenDisplay; /* use GXOpenDispplay */ + int alreadyGXOpened; int w, h; + // The orientation of GAPI framebuffer. + // Never changes on the same device. enum SDL_ScreenOrientation gapiOrientation; void *buffer; // may be 8, 16, 24, 32 bpp @@ -153,6 +158,10 @@ int startOffset; // in bytes int useVga; int suspended; // do not pu anything into video memory + // The orientation of the system, as defined by SM_CXSCREEN and SM_CYSCREEN + // User can change it by using 'screen layout' in system options + // Restricted to UP or RIGHT + enum SDL_ScreenOrientation systemOrientation; }; !! This is a flag variable, see the previous comment !! And yet another orientation: now we have to keep three of them in mind. diff -bru SDL-1.2.13/src/video/wincommon/SDL_sysevents.c SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c --- SDL-1.2.13/src/video/wincommon/SDL_sysevents.c 2007-12-31 07:48:02.000000000 +0300 +++ SDL-1.2.13-new/src/video/wincommon/SDL_sysevents.c 2008-10-16 20:02:12.000000000 +0400 @@ -160,10 +160,22 @@ #endif */ } break; + // FIXME: Older version used just SDL_VideoSurface->(w, h) + // w and h are "clipped" while x and y are "raw", which caused + // x in former and y in latter case to be clipped in a wrong direction, + // thus offsetting the coordinate on 2 x clip pixels + // (like, 128 for 640 -> 512 clipping). + // We will now try to extract and use raw values. + // The way to do that RIGHT is do (orientation-dependent) clipping before + // doing this transform, but it's hardly possible. + + // SEE SDL_mouse.c /ClipOffset to understand these calculations. case SDL_ORIENTATION_RIGHT: if (!SDL_VideoSurface) break; - rotatedX = SDL_VideoSurface->w - *y; + rotatedX = (2 * ((SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/ + SDL_VideoSurface->format->BytesPerPixel)) + + SDL_VideoSurface->w - *y; rotatedY = *x; *x = rotatedX; *y = rotatedY; @@ -172,7 +184,8 @@ if (!SDL_VideoSurface) break; rotatedX = *y; - rotatedY = SDL_VideoSurface->h - *x; + rotatedY = (2 * (SDL_VideoSurface->offset/SDL_VideoSurface->pitch)) + + SDL_VideoSurface->h - *x; *x = rotatedX; *y = rotatedY; break; !! That's the trickest part, hence the long comment. !! GAPI would really support only 320x240 or 640x480 mode, if application !! requested the different screen size (as POWDER did, wishing 256x192), then SDL !! is going to grab the first mode that fits the requested, and pad the screen !! with black bars (as they do with wide-screen films). !! It would also get, say, 240x320 mode, and to turn it into 256x192 it would !! need to rotate mouse clicks. !! It worked, but one bug slipped through: it would receive mouse clicks !! unpadded, then rotate them, and then pad the black bars. The problem is: rotate !! is done by GAPI driver while padding is done by SDL core. SDL core doesn't know !! anything about rotating, so it would pad one of dimensions incorrectly. I understand that some of my claims (or code) might seem unbacked, but you can always grab the POWDER binary, compile your own libsdl with one or more of those fixes turned off, and see how weird it would misbehave. I can even supply you with those custom builds of libsdl if you don't want to set up the build environment for windows ce, you'll just need a PDA or a smartphone with it. I plan to take care of SDL on Windows CE as long as I maintain the POWDER port. POWDER is good for that because it: Employs both padded (with centered image, black bars) and unpadded (image occupies full screen) graphics; initializes video more than once; uses both 320x240 and 640x480 video; uses both stylus and buttons. There's still a list of unresolved issues which I'm planning to fix: 1) Arrow buttons on PDA return weird scancodes compared to PC, this caused the game to misbehave before I've fixed that. You can see it on those diagrams: http://wrar.name/upload/powder-htc.png http://wrar.name/upload/powder-pda.png 2) SDL (or underlying windows) doesn't care to rotate arrow presses when we're in a low-res GAPI mode, but it will rotate them in VGA mode (because of different screen orientations, the same arrow buttons can suddently mean different directions). Solution: we should stick to GAPI user orientation (the orientation the program supposedly wants) and rotate the keys on our own. _______________________________________________ SDL mailing list SDL@lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
author Sam Lantinga <slouken@libsdl.org>
date Fri, 07 Nov 2008 04:15:36 +0000
parents e85e65aec22f
children
line wrap: on
line source

#include "dsa.h"
#include "sdlepocapi.h"
#include <cdsb.h>

LOCAL_C TInt BytesPerPixel(TDisplayMode aMode)
	{
	return ((TDisplayModeUtils::NumDisplayModeBitsPerPixel(aMode) - 1) >> 3) + 1; 
	}


////////////////////////////////////////////////////////////////////////////////////////////////
  
NONSHARABLE_CLASS(CDsaA) : public CDsa
	{
	public:
		CDsaA(RWsSession& aSession);
	private:
		~CDsaA();
		TUint8* LockSurface();
		void UnlockHWSurfaceRequestComplete();
		void UnlockHwSurface();
		void CreateSurfaceL();
		void Wipe(TInt aLength);
		void RecreateL();
		void Free();
		TInt ExternalUpdate() {return 0;}
	private:
		CFbsBitmap* iBmp;	
	};
	
	
CDsaA::CDsaA(RWsSession& aSession) : CDsa(aSession)
	{
	}
	
void CDsaA::Free()
	{
	delete iBmp;
	iBmp = NULL;
	}

CDsaA::~CDsaA()
	{
	__ASSERT_DEBUG(iBmp == NULL, PANIC(KErrNotReady));
	}
	
TUint8* CDsaA::LockSurface()
	{
	iBmp->LockHeap();
	return reinterpret_cast<TUint8*>(iBmp->DataAddress());
	}

void CDsaA::UnlockHWSurfaceRequestComplete()
	{
	PANIC(KErrNotSupported);
	}

void CDsaA::UnlockHwSurface()
	{
	iBmp->UnlockHeap();
	SetUpdating(EFalse);
	Dsa().Gc()->BitBlt(HwRect().iTl, iBmp);
	Dsa().ScreenDevice()->Update();
	}

void CDsaA::CreateSurfaceL()
	{
	delete iBmp;
	iBmp = NULL;
	iBmp  = new (ELeave) CFbsBitmap();
	User::LeaveIfError(iBmp->Create(HwRect().Size(), DisplayMode()));
	}

void CDsaA::Wipe(TInt aLength) //dont call in drawing
	{
	iBmp->LockHeap();
	Mem::FillZ(iBmp->DataAddress(), aLength);
	iBmp->UnlockHeap();
	}
	
void CDsaA::RecreateL()
	{
	}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

NONSHARABLE_CLASS(MDsbObs)
	{
	public:
		virtual void SurfaceReady() = 0;
		virtual CDirectScreenBitmap& Dsb() = 0;
	};
	
NONSHARABLE_CLASS(CDsbSurface) : public CActive
	{
	public:
		CDsbSurface(MDsbObs& aDsb);
		TUint8* Address();
		void Complete();
		~CDsbSurface();
	private:
		void RunL();
		void DoCancel();
	private:
		MDsbObs& iDsb; 
		TUint8* iAddress;
	};	

CDsbSurface::CDsbSurface(MDsbObs& aDsb) : CActive(CActive::EPriorityHigh) , iDsb(aDsb)
	{
	CActiveScheduler::Add(this);
	}

CDsbSurface::~CDsbSurface()
	{
	Cancel();
	}
	
void CDsbSurface::Complete()
	{
	if(iAddress != NULL && !IsActive())
		{
		iAddress = NULL;
		SetActive();
		iDsb.Dsb().EndUpdate(iStatus);
		}
	}
	
TUint8* CDsbSurface::Address()
	{
	if(iAddress == NULL && !IsActive())
		{
		TAcceleratedBitmapInfo info;
		if(KErrNone == iDsb.Dsb().BeginUpdate(info))
			iAddress = info.iAddress;
		}
	return iAddress;
	}
	
void CDsbSurface::RunL()
	{
	iDsb.SurfaceReady();
	}

void CDsbSurface::DoCancel()
	{
	//empty
	}
		
NONSHARABLE_CLASS(CDsaB) : public CDsa, public MDsbObs
	{
	public:
		CDsaB(RWsSession& aSession);
	private:
		~CDsaB();
		TUint8* LockSurface();
		void UnlockHWSurfaceRequestComplete();
		void UnlockHwSurface();
		void CreateSurfaceL();
		void Wipe(TInt aLength);
		void RecreateL();
		void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
		void Free();
		CDirectScreenBitmap& Dsb();
		void SurfaceReady();
		TInt ExternalUpdate() {return 0;}
	private:
		CDsbSurface* iSurface1;
		CDsbSurface* iSurface2;
		CDirectScreenBitmap* iDsb;
	};

CDsaB::CDsaB(RWsSession& aSession) : CDsa(aSession)
	{
	}

void CDsaB::Free()
	{
	}
	
void CDsaB::UnlockHWSurfaceRequestComplete()
	{
	iSurface1->Complete();
	iSurface2->Complete();
	}	

void CDsaB::CreateSurfaceL()
	{
	}
	
void CDsaB::Wipe(TInt aLength) //dont call in drawing
	{
	TUint8* addr = LockSurface();
	if(addr != NULL) 
		{
		Mem::FillZ(addr, aLength);
		UnlockHwSurface();
		}
	}

void CDsaB::UnlockHwSurface()
	{
	EpocSdlEnv::Request(CDsa::ERequestUpdate);
	}
	
TUint8* CDsaB::LockSurface()
	{
	TUint8* addr =  iSurface1->Address();
	if(addr == NULL)
		addr =  iSurface2->Address();
	SetUpdating(addr == NULL);
	return addr;
	}
	
void CDsaB::SurfaceReady()	
	{
	SetUpdating(EFalse);
	}

CDirectScreenBitmap& CDsaB::Dsb()
	{
	return *iDsb;
	}
	
void CDsaB::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
	{
	if(iDsb == NULL)
		iDsb = CDirectScreenBitmap::NewL();	
	CDsa::ConstructL(aWindow, aDevice);
	iSurface1 = new (ELeave) CDsbSurface(*this);
	iSurface2 = new (ELeave) CDsbSurface(*this);
	}
	
CDsaB::~CDsaB()
	{
	delete iSurface1;
	delete iSurface2;
	delete iDsb;
	}	

void CDsaB::RecreateL()
	{
    iDsb->Close();
    iDsb->Create(HwRect(), CDirectScreenBitmap::EDoubleBuffer);
	}

/////////////////////////////////////////////////////////////////////////////////////////////////////	


TSize CDsa::WindowSize() const
	{
	TSize size = HwRect().Size();
	if(iStateFlags & EOrientation90)
		{
		const TInt tmp = size.iWidth;
		size.iWidth = size.iHeight;
		size.iHeight = tmp;
		}
	return size;
	}
	
void CDsa::SetSuspend()
	{
	iStateFlags |= ESdlThreadSuspend;
	}

void CDsa::ReleaseStop()
	{
	iStateFlags &= ~ESdlThreadExplicitStop;
	}


TBool CDsa::Stopped() const
	{
	return (iStateFlags & ESdlThreadExplicitStop);
	}

void CDsa::SetOrientation(CSDL::TOrientationMode aOrientation)
	{
	TInt flags = 0;
	switch(aOrientation)
		{
		case CSDL::EOrientation90:
			flags = EOrientation90;
			break;
		case CSDL::EOrientation180:
			flags = EOrientation180;
			break;
		case CSDL::EOrientation270:
			flags = EOrientation90 | EOrientation180;
			break;
		case CSDL::EOrientation0:
			flags = 0;
			break;
		}
	if(flags != (iStateFlags & EOrientationFlags))
		{
		iStateFlags |= EOrientationChanged;
		iNewFlags = flags; //cannot be set during drawing...
		}
	}

CDsa::~CDsa()
    {
    if(iDsa != NULL)
        {
        iDsa->Cancel();
        }
    delete iDsa;
    User::Free(iLut256);
    }
         
void CDsa::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
    {
    if(iDsa != NULL)
    	{
    	iDsa->Cancel();
    	delete iDsa;
    	iDsa = NULL;
    	}
    	
    	
    iDsa = CDirectScreenAccess::NewL(
    				iSession,
					aDevice,
					aWindow,
					*this);				

	if(iLut256 == NULL)
		iLut256 = (TUint32*) User::AllocL(256 * sizeof(TUint32));
	iTargetMode = aWindow.DisplayMode();
	iTargetBpp = BytesPerPixel(DisplayMode());
	iTargetRect = TRect(aWindow.Position(), aWindow.Size());
    RestartL();
    }

void CDsa::LockPalette(TBool aLock)
	{
	if(aLock)
		iStateFlags |= EPaletteLocked;
	else
		iStateFlags &= ~EPaletteLocked;
	}
TInt CDsa::SetPalette(TInt aFirst, TInt aCount, TUint32* aPalette)
	{
	if(iLut256 == NULL)
		return KErrNotFound;
	const TInt count = aCount - aFirst;
	if(count > 256)
		return KErrArgument;
	if(iStateFlags & EPaletteLocked)
		return KErrNone;
	for(TInt i = aFirst; i < count; i++) //not so busy here:-)
		{
		iLut256[i] = aPalette[i];
		}
	return KErrNone;
	}
	
	

	
void CDsa::RestartL()
    {
    //const TBool active = iDsa->IsActive();
    
    //if(!active)
    iDsa->StartL();
    
    RRegion* r = iDsa->DrawingRegion();
    iDsa->Gc()->SetClippingRegion(r);
    TRect rect = r->BoundingRect();
   
    if(rect.IsEmpty())
    	{
    	return;
    	}
    	
    iScreenRect = rect; //to ensure properly set, albeit may not(?) match to value SDL has - therefore may has to clip
	
	RecreateL();

    iStateFlags |= ERunning;
//    iScanLineWidth = iTargetBpp * HwRect().Width();
    ReleaseStop();
    if(iStateFlags & ESdlThreadSuspend)
    	{
    	EpocSdlEnv::Resume();
    	iStateFlags &= ~ ESdlThreadSuspend;
    	}
    }
    
CDsa::CDsa(RWsSession& aSession) : 
 	iSession(aSession),
  	iStateFlags(0)
	{
//	CActiveScheduler::Add(this);
	iCFTable[0] = CopyMem;
	iCFTable[1] = CopyMemFlipReversed;
	iCFTable[2] = CopyMemReversed;
	iCFTable[3] = CopyMemFlip;	
	
	iCFTable[4] = Copy256;
	iCFTable[5] = Copy256FlipReversed;
	iCFTable[6] = Copy256Reversed;
	iCFTable[7] = Copy256Flip;	
	
	
	iCFTable[8] = CopySlow;
	iCFTable[9] = CopySlowFlipReversed;
	iCFTable[10] = CopySlowReversed;
	iCFTable[11] = CopySlowFlip;	
	}
	
RWsSession& CDsa::Session()
	{
	return iSession;
	}



TUint8* CDsa::LockHwSurface()
	{
	if((iStateFlags & EUpdating) == 0) //else frame is skipped
		{
		return LockSurface();
		}
	return NULL; 
	}

/*	
void CDsa::RunL()
	{
	iStateFlags &= ~EUpdating;
	}
		
	
void CDsa::DoCancel()
	{
	iStateFlags &= ~EUpdating;
	//nothing can do, just wait?
	}
*/	
	
TInt CDsa::AllocSurface(TBool aHwSurface, const TSize& aSize, TDisplayMode aMode)
	{
	if(aHwSurface && aMode != DisplayMode())
		return KErrArgument;
	
	iSourceMode = aMode;
	
	iSourceBpp = BytesPerPixel(aMode);
	
	const TSize size = WindowSize();
	if(aSize.iWidth > size.iWidth)
		return KErrTooBig;
	if(aSize.iHeight > size.iHeight)
		return KErrTooBig;
	
	TRAPD(err, CreateSurfaceL());
	if(err != KErrNone)
		return err;


	SetCopyFunction();
	
	EpocSdlEnv::ObserverEvent(MSDLObserver::EEventWindowReserved);
	
	return KErrNone;
	}
	

/*
void SaveBmp(const TDesC& aName, const TAny* aData, TInt aLength, const TSize& aSz, TDisplayMode aMode)
	{
	CFbsBitmap* s = new CFbsBitmap();
	s->Create(aSz, aMode);
	s->LockHeap();
	TUint32* addr = s->DataAddress();
	Mem::Copy(addr, aData, aLength);
	s->UnlockHeap();
	s->Save(aName);
	s->Reset();
	delete s;
	}
	
void SaveBmp(const TDesC& aName, const TUint32* aData, const TSize& aSz)
	{
	CFbsBitmap* s = new CFbsBitmap();
	s->Create(aSz, EColor64K);
	TBitmapUtil bmp(s);
	bmp.Begin(TPoint(0, 0));
	for(TInt j = 0; j < aSz.iHeight; j++)
		{
		bmp.SetPos(TPoint(0, j));
		for(TInt i = 0; i < aSz.iWidth; i++)
			{
			bmp.SetPixel(*aData);
			aData++;
			bmp.IncXPos();
			}
		}
	bmp.End();
	s->Save(aName);
	s->Reset();
	delete s;
	}	
	
TBuf<16> FooName(TInt aFoo)
	{
	TBuf<16> b;
	b.Format(_L("C:\\pic%d.mbm"), aFoo);
	return b;
	}
*/
void CDsa::ClipCopy(TUint8* aTarget, const TUint8* aSource, const TRect& aRect, const TRect& aTargetPos) const
	{
	TUint8* target = aTarget;
	const TUint8* source = aSource;
	const TInt lineWidth = aRect.Width();
	source += iSourceBpp * (aRect.iTl.iY * lineWidth); 
	TInt sourceStartOffset = iSourceBpp *  aRect.iTl.iX;
	source += sourceStartOffset;
	target += iTargetBpp * ((aTargetPos.iTl.iY + aRect.iTl.iY ) * lineWidth); 
	TInt targetStartOffset = iTargetBpp * (aRect.iTl.iX + aTargetPos.iTl.iX);
	target += targetStartOffset;
	TUint32* targetPtr = reinterpret_cast<TUint32*>(target);
	const TInt targetWidth = HwRect().Size().iWidth;
	const TInt height = aRect.Height(); 
	
	TInt lineMove = iStateFlags & EOrientation90 ? 1 : lineWidth;
	
	if(iStateFlags & EOrientation180)
		{
		
		targetPtr += targetWidth *  (height - 1);
	
		for(TInt i = 0; i < height; i++) //source is always smaller
			{
			iCopyFunction(*this, targetPtr, source, lineWidth, height);
			source += lineMove;
			targetPtr -= targetWidth;
			}
		}
	else
		{
		
		
		for(TInt i = 0; i < height; i++) //source is always smaller
			{
			iCopyFunction(*this, targetPtr, source, lineWidth, height);
			source += lineMove;
			targetPtr += targetWidth;
			}
		}

	}
	
	


void CDsa::Wipe() //dont call in drawing
	{
	if(IsDsaAvailable())
		Wipe(iTargetBpp * iScreenRect.Width() * iScreenRect.Height());
	}
	
void CDsa::SetCopyFunction()
	{
	//calculate offset to correct function in iCFTable according to given parameters
	TInt function = 0;
	const TInt KCopyFunctions = 4;
	const TInt KOffsetToNative = 0;
	const TInt KOffsetTo256 = KOffsetToNative + KCopyFunctions;
	const TInt KOffsetToOtherModes = KOffsetTo256 + KCopyFunctions;
	const TInt KOffsetTo90Functions = 1;
	const TInt KOffsetTo180Functions = 2;
	
	if(iSourceMode == DisplayMode())
		function = KOffsetToNative; 		//0
	else if(iSourceMode == EColor256)
		function = KOffsetTo256;			//4
	else
		function = KOffsetToOtherModes; 	//8
	
	if(iStateFlags & EOrientation90)
		function += KOffsetTo90Functions; 	// + 1
	if(iStateFlags & EOrientation180)
		function += KOffsetTo180Functions; 	//+ 2
	
	iCopyFunction = iCFTable[function];
	
	Wipe();
	}
	
inline void Rotate(TRect& aRect)
	{
	const TInt dx = aRect.iBr.iX - aRect.iTl.iX;
	const TInt dy = aRect.iBr.iY - aRect.iTl.iY;

	aRect.iBr.iX = aRect.iTl.iX + dy;
	aRect.iBr.iY = aRect.iTl.iY + dx;
	
	const TInt tmp = aRect.iTl.iX;
	aRect.iTl.iX = aRect.iTl.iY;
	aRect.iTl.iY = tmp;
	}
	
/*	
int bar = 0;
*/	
TBool CDsa::AddUpdateRect(const TUint8* aBits, const TRect& aUpdateRect, const TRect& aRect)
	{

	if(iStateFlags & EOrientationChanged)
		{
		iStateFlags &= ~EOrientationFlags;
		iStateFlags |= iNewFlags;
		SetCopyFunction();
		iStateFlags &= ~EOrientationChanged;
	    EpocSdlEnv::WaitDeviceChange();
	    return EFalse; //skip this frame as data is may be changed
		}

	if(iTargetAddr == NULL)
		{
		iTargetAddr = LockHwSurface();
		}
	TUint8* target = iTargetAddr;
	if(target == NULL)
		return EFalse;
	
	
	TRect targetRect = HwRect();
	TRect sourceRect = aRect;
	TRect updateRect = aUpdateRect;
	
	if(iStateFlags & EOrientation90)
		{
		Rotate(sourceRect);
		Rotate(updateRect);
		}
		
	if(iSourceMode != DisplayMode() ||  targetRect != sourceRect || targetRect != updateRect || ((iStateFlags & EOrientationFlags) != 0))
		{
		sourceRect.Intersection(targetRect); //so source always smaller or equal than target
		updateRect.Intersection(targetRect);
		ClipCopy(target, aBits, updateRect, sourceRect);
		}
	else
		{
		const TInt byteCount = aRect.Width() * aRect.Height() * iSourceBpp; //this could be stored
		Mem::Copy(target, aBits, byteCount);
		}

	return ETrue;
	}
	
CDsa* CDsa::CreateL(RWsSession& aSession)
	{
	if(EpocSdlEnv::Flags(CSDL::EDrawModeDSB))
		{
		TInt flags = CDirectScreenBitmap::ENone;
		if(EpocSdlEnv::Flags(CSDL::EDrawModeDSBDoubleBuffer))
			flags |= CDirectScreenBitmap::EDoubleBuffer;
		if(EpocSdlEnv::Flags(CSDL::EDrawModeDSBIncrentalUpdate))
			flags |= CDirectScreenBitmap::EIncrementalUpdate;
		return new (ELeave) CDsaB(aSession);
		}
    else
        return new (ELeave) CDsaA(aSession);
	} 	

void CDsa::CreateZoomerL(const TSize& aSize)
	{
	iSwSize = aSize;
	iStateFlags |= EResizeRequest;
	CreateSurfaceL();
	SetTargetRect();
	}
	
TPoint CDsa::WindowCoordinates(const TPoint& aPoint) const	
	{
	TPoint pos = aPoint - iScreenRect.iTl;
	const TSize asz = iScreenRect.Size();
	if(iStateFlags & EOrientation180)
		{
		pos.iX = asz.iWidth - pos.iX;
		pos.iY = asz.iHeight - pos.iY;	
		}	
	if(iStateFlags & EOrientation90)
		{
		pos.iX = aPoint.iY;
		pos.iY = aPoint.iX;	
		}
	pos.iX <<= 16;
	pos.iY <<= 16;
	pos.iX /= asz.iWidth; 
	pos.iY /= asz.iHeight;
	pos.iX *= iSwSize.iWidth;
	pos.iY *= iSwSize.iHeight;
	pos.iX >>= 16;
	pos.iY >>= 16;
	return pos; 	
	}
	
void CDsa::SetTargetRect()
	{
	iTargetRect = iScreenRect;
	if(iStateFlags & EResizeRequest && EpocSdlEnv::Flags(CSDL::EAllowImageResizeKeepRatio))
		{
		const TSize asz = iScreenRect.Size();
		const TSize sz = iSwSize;
		
		TRect rect;
		
		const TInt dh = (sz.iHeight << 16) / sz.iWidth;

		if((asz.iWidth * dh ) >> 16 <= asz.iHeight)
			{
			rect.SetRect(TPoint(0, 0), TSize(asz.iWidth, (asz.iWidth * dh) >> 16));
			}
		else
			{
			const TInt dw = (sz.iWidth << 16) / sz.iHeight;
	    	rect.SetRect(TPoint(0, 0), TSize((asz.iHeight * dw) >> 16, asz.iHeight));
			}
		rect.Move((asz.iWidth - rect.Size().iWidth) >> 1, (asz.iHeight - rect.Size().iHeight) >> 1);  
		
		iTargetRect = rect;
		iTargetRect.Move(iScreenRect.iTl);

		} 
	if(!(iStateFlags & EResizeRequest))
		iSwSize = iScreenRect.Size();
//	iScanLineWidth = /*iTargetBpp **/ SwSize().iWidth;
	}
		
void CDsa::RecreateL()
	{
	}

void CDsa::Free()
	{
	}
		
void CDsa::UpdateSwSurface()
	{
	iTargetAddr = NULL;
	UnlockHwSurface();	//could be faster if does not use AO, but only check status before redraw, then no context switch needed
	}

void CDsa::SetBlitter(MBlitter* aBlitter)
	{
	iBlitter = aBlitter;
	}
	
void CDsa::DrawOverlays()
	{
	const TInt last = iOverlays.Count() - 1;
	for(TInt i = last; i >= 0 ; i--)
		iOverlays[i].iOverlay->Draw(*iDsa->Gc(), HwRect(), SwSize());
	}

TInt CDsa::AppendOverlay(MOverlay& aOverlay, TInt aPriority)
	{
	TInt i;
	for(i = 0; i < iOverlays.Count() && iOverlays[i].iPriority < aPriority; i++)
		{}
	const TOverlay overlay = {&aOverlay, aPriority};
	return iOverlays.Insert(overlay, i);
	}
	
TInt CDsa::RemoveOverlay(MOverlay& aOverlay)
	{
	for(TInt i = 0; i < iOverlays.Count(); i++)
		{
		if(iOverlays[i].iOverlay == &aOverlay)
			{
			iOverlays.Remove(i);
			return KErrNone;
			}
		}
	return KErrNotFound;
	}
		
TInt CDsa::RedrawRequest()
	{
	if(!(iStateFlags & (EUpdating) && (iStateFlags & ERunning)))
		{
		return ExternalUpdate();
		}
	return KErrNotReady;
	}


void CDsa::Resume()	
	{
	if(Stopped())
		Restart(RDirectScreenAccess::ETerminateRegion);
	}
	
void CDsa::DoStop()
	{
	if(IsDsaAvailable())
		iStateFlags |= ESdlThreadExplicitStop;
	Stop();
	}
	
void CDsa::Stop()
	{
	iStateFlags &= ~ERunning;
//	Cancel(); //can be called only from main!
	iDsa->Cancel();
	}
	
void CDsa::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
	{
//	iStateFlags |= EChangeNotify;
	Stop();
	}
	
void CDsa::Restart(RDirectScreenAccess::TTerminationReasons aReason)
	{
	if(aReason == RDirectScreenAccess::ETerminateRegion) //auto restart
		{												
		TRAPD(err, RestartL());
		PANIC_IF_ERROR(err);
		}
	}
/*)
TBool CDsa::ChangeTrigger()
	{
	const TBool change = iStateFlags & EChangeNotify;
	iStateFlags &= ~EChangeNotify;
	return change;
	}
*/	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void CDsa::Copy256(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	TUint32* target = aTarget;
	const TUint32* endt = target + aBytes; 
	const TUint8* source = aSource;
	while(target < endt)
		{
		*target++ = aDsa.iLut256[*source++]; 
		}
	}
	
void CDsa::Copy256Reversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	const TUint32* target = aTarget;
	TUint32* endt = aTarget + aBytes; 
	const TUint8* source = aSource;
	while(target < endt)
		{
		*(--endt) = aDsa.iLut256[*source++]; 
		}
	}	
	
void CDsa::Copy256Flip(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	TUint32* target = aTarget;
	const TUint32* endt = target + aBytes; 
	const TUint8* column = aSource;

	while(target < endt)
		{
		*target++ = aDsa.iLut256[*column];
		column += aLineLen;
		}
	}
	
void CDsa::Copy256FlipReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	const TUint32* target = aTarget;
	TUint32* endt = aTarget + aBytes; 
	const TUint8* column = aSource;

	while(target < endt)
		{
		*(--endt) = aDsa.iLut256[*column];
		column += aLineLen;
		}
	}		

void CDsa::CopyMem(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	Mem::Copy(aTarget, aSource, aBytes);
	}
	
void CDsa::CopyMemFlip(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	TUint32* target = aTarget;
	const TUint32* endt = target + aBytes; 
	const TUint32* column = reinterpret_cast<const TUint32*>(aSource);

	while(target < endt)
		{
		*target++ = *column;
		column += aLineLen;
		}
	}
	
void CDsa::CopyMemReversed(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	const TUint32* target = aTarget;
	TUint32* endt = aTarget + aBytes; 
	const TUint32* source = reinterpret_cast<const TUint32*>(aSource);
	while(target < endt)
		{
		*(--endt) = *source++; 
		}
	}	
	
	
void CDsa::CopyMemFlipReversed(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	const TUint32* target = aTarget;
	TUint32* endt = aTarget + aBytes; 
	const TUint32* column = reinterpret_cast<const TUint32*>(aSource);

	while(target < endt)
		{
		*(--endt) = *column;
		column += aLineLen;
		}
	}
			

typedef TRgb (*TRgbFunc) (TInt aValue);

LOCAL_C TRgb rgb16MA(TInt aValue)
	{
	return TRgb::Color16MA(aValue);
	}
	
NONSHARABLE_CLASS(MRgbCopy)
	{
	public:
	virtual void Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed) = 0;
	virtual void FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed) = 0;
	};
template <class T>
NONSHARABLE_CLASS(TRgbCopy) : public MRgbCopy
	{
	public:
	TRgbCopy(TDisplayMode aMode);
	void* operator new(TUint aBytes, TAny* aMem);
	void Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed);
	void FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed);
	private:
		TRgbFunc iFunc;
	};
		
template <class T>		
void* TRgbCopy<T>::operator new(TUint /*aBytes*/, TAny* aMem)
	{
	return aMem;
	}
		
template <class T>
TRgbCopy<T>::TRgbCopy(TDisplayMode aMode)
	{
	switch(aMode)
	{
	case EGray256 : iFunc = TRgb::Gray256; break;
	case EColor256 : iFunc = TRgb::Color256; break;
	case EColor4K : iFunc = TRgb::Color4K; break;
	case EColor64K : iFunc = TRgb::Color64K; break;
	case EColor16M : iFunc = TRgb::Color16M; break;
	case EColor16MU : iFunc = TRgb::Color16MU; break;
	case EColor16MA : iFunc = rgb16MA; break;
	default:
		PANIC(KErrNotSupported);
	}
	}
	
template <class T>
void TRgbCopy<T>::Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed)
	{
	const T* source = reinterpret_cast<const T*>(aSource);
	TUint32* target = aTarget;
	TUint32* endt = target + aBytes;
	
	if(aReversed)
		{
		while(target < endt)
			{
			TUint32 value = *source++;
			*(--endt) = iFunc(value).Value();
			}
		}
	else
		{
		while(target < endt)
			{
			TUint32 value = *source++;
			*target++ = iFunc(value).Value();
			}
		}
	}
	
template <class T>
void TRgbCopy<T>::FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed)
	{
	const T* column = reinterpret_cast<const T*>(aSource);
	TUint32* target = aTarget;
	TUint32* endt = target + aBytes;
	
	if(aReversed)
		{
		while(target < endt)
			{
			*(--endt) = iFunc(*column).Value();
			column += aLineLen;
			}
		}
	else
		{
		while(target < endt)
			{
			*target++ = iFunc(*column).Value();
			column += aLineLen;
			}
		}
	}	
	

typedef TUint64 TStackMem;

LOCAL_C MRgbCopy* GetCopy(TAny* mem, TDisplayMode aMode)
	{
	if(aMode == EColor256 || aMode == EGray256)
		{
		return new (mem) TRgbCopy<TUint8>(aMode);
		}
	if(aMode == EColor4K || aMode == EColor64K)
		{
		return new (mem) TRgbCopy<TUint16>(aMode);
		}
	if(aMode == EColor16M || aMode == EColor16MU || aMode == EColor16MA)
		{
		return new (mem) TRgbCopy<TUint32>(aMode);
		}
	PANIC(KErrNotSupported);
	return NULL;
	}
	

void CDsa::CopySlowFlipReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	TStackMem mem = 0;
	GetCopy(&mem, aDsa.iSourceMode)->FlipCopy(aTarget, aSource, aBytes, aLineLen, ETrue);	
	}
	
void CDsa::CopySlowFlip(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
	{
	TStackMem mem = 0;
	GetCopy(&mem, aDsa.iSourceMode)->FlipCopy(aTarget, aSource, aBytes, aLineLen, EFalse);
	}
	
void CDsa::CopySlow(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	TStackMem mem = 0;
	GetCopy(&mem, aDsa.iSourceMode)->Copy(aTarget, aSource, aBytes, EFalse);	
	}	

void CDsa::CopySlowReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
	{
	TStackMem mem = 0;
	GetCopy(&mem, aDsa.iSourceMode)->Copy(aTarget, aSource, aBytes, ETrue);	
	}