os/graphics/windowing/windowserver/nga/SERVER/openwfc/ScreenRedraw.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/windowing/windowserver/nga/SERVER/openwfc/ScreenRedraw.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1228 @@
     1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include "ScreenRedraw.h"
    1.20 +
    1.21 +#include <hal.h>
    1.22 +
    1.23 +#include "debugbar.h"
    1.24 +#include "inifile.h"
    1.25 +#include "screen.h"
    1.26 +#include "pointer.h"
    1.27 +#include "rootwin.h"
    1.28 +#include "walkwindowtree.h"
    1.29 +#include "wstop.h"
    1.30 +#include "WsMemMgr.h"
    1.31 +#include "renderstagemanager.h"
    1.32 +#include "graphics/WsRenderStageFactory.h"
    1.33 +#include "graphics/WsRenderStage.h"
    1.34 +#include "graphics/wsgraphicscontext.h"
    1.35 +#include "EVENT.H"
    1.36 +
    1.37 +#ifdef USE_DEBUG_FRAME_CAPTURE
    1.38 +#include <graphics/wsscreendevice.h>
    1.39 +#include "../debuglog/debuglog.h"
    1.40 +#endif
    1.41 +
    1.42 +GLREF_D CDebugLogBase *wsDebugLog;
    1.43 +
    1.44 +#ifdef USE_DEBUG_REGIONS
    1.45 +#	define DEBUG_REGION(col,fill,reg) DebugRegion(col,fill,reg)
    1.46 +#	define DEBUG_RECT(col,fill,rect) DebugRect(col,fill,rect)
    1.47 +#else
    1.48 +#	define DEBUG_REGION(col,fill,reg)
    1.49 +#	define DEBUG_RECT(col,fill,rect)
    1.50 +#endif
    1.51 +
    1.52 +#ifdef _DEBUG
    1.53 +# define LOG_SCREEN_REDRAW_START {if (wsDebugLog) {_LIT(KLogScreenRedrawStart, ">> CScreenRedraw::OnAnimation()"); wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, KLogScreenRedrawStart);}}
    1.54 +# define LOG_SCREEN_REDRAW_END {if (wsDebugLog) {_LIT(KLogScreenRedrawEnd, "<< CScreenRedraw::OnAnimation()"); wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, KLogScreenRedrawEnd);}}
    1.55 +#else
    1.56 +# define LOG_SCREEN_REDRAW_START
    1.57 +# define LOG_SCREEN_REDRAW_END
    1.58 +#endif
    1.59 +
    1.60 +
    1.61 +#ifdef USE_DEBUG_FRAME_CAPTURE
    1.62 +#	ifdef __WINS__
    1.63 +	_LIT(KDefaultFrameCaptureLocation, "c:\\");
    1.64 +#	else
    1.65 +	_LIT(KDefaultFrameCaptureLocation, "e:\\");
    1.66 +#	endif
    1.67 +#endif //USE_DEBUG_FRAME_CAPTURE
    1.68 +
    1.69 +CScreenRedraw::TTimedRect::TTimedRect(TAnimType aType, const TRect& aRect, const TTime& aTime, CWsWindow* aWindow)
    1.70 +	: iType(aType), iRect(aRect), iTime(aTime), iWindow(aWindow)
    1.71 +	{
    1.72 +	}
    1.73 +
    1.74 +TInt CScreenRedraw::TTimedRect::Compare(const TTimedRect& aOne,const TTimedRect& aOther)
    1.75 +	{
    1.76 +	if(aOne.iTime < aOther.iTime)
    1.77 +		return -1;
    1.78 +	else if(aOne.iTime > aOther.iTime)
    1.79 +		return 1;
    1.80 +	else
    1.81 +		return 0;
    1.82 +	}
    1.83 +
    1.84 +CScreenRedraw * CScreenRedraw::NewL(CScreen& aScreen)
    1.85 +	{
    1.86 +	CScreenRedraw * self = new (ELeave) CScreenRedraw(aScreen);
    1.87 +	CleanupStack::PushL(self);
    1.88 +	self->ConstructL();
    1.89 +	CleanupStack::Pop(self);
    1.90 +	return self;
    1.91 +	}
    1.92 +	
    1.93 +CScreenRedraw::CScreenRedraw(CScreen& aScreen): iScreen(aScreen)
    1.94 +	{
    1.95 +	}
    1.96 +
    1.97 +CScreenRedraw::~CScreenRedraw()
    1.98 +	{
    1.99 +	CRenderStageManager::Release(iRenderStages);
   1.100 +	iRenderStages = NULL;
   1.101 +	iTimedDrawRect.Close();
   1.102 +	iInvalid.Reset();
   1.103 +	iTopElement.Reset();
   1.104 +	iQuickFadeList.Reset();
   1.105 +#ifdef USE_DEBUG_FRAME_CAPTURE
   1.106 +	delete iDebugBitmap;
   1.107 +	delete iDebugBitmapDevice;
   1.108 +	delete iDebugBitmapGc;
   1.109 +#endif //USE_DEBUG_FRAME_CAPTURE
   1.110 +	}
   1.111 +
   1.112 +void CScreenRedraw::ConstructL()
   1.113 +	{
   1.114 +	
   1.115 +	LEAVE_LOG_INSTALL_C;
   1.116 +	
   1.117 +	iRenderStages = CRenderStageManager::ConnectL(
   1.118 +						iScreen.ScreenNumber(),
   1.119 +						static_cast<MWsScreen*>(&iScreen),
   1.120 +						this
   1.121 +						);
   1.122 +	
   1.123 +	LEAVE_LOG_UNINSTALL_C;
   1.124 +	
   1.125 +	WS_ASSERT_ALWAYS(iRenderStages, EWsPanicNoRenderStagePipeline);
   1.126 +	iRenderStageTextCursor = static_cast<MWsTextCursor*>(iRenderStages->ResolveObjectInterface(KMWsTextCursor));
   1.127 +	WS_ASSERT_ALWAYS(iRenderStageTextCursor, EWsPanicTextCursorInterfaceMissing);
   1.128 +
   1.129 +#ifdef USE_DEBUG_FRAME_CAPTURE
   1.130 +	_LIT(KWSERVIniFileVarFrameCapture,"FRAMECAPTURE");
   1.131 +	iFrameCapture = WsIniFile->FindVar(KWSERVIniFileVarFrameCapture);
   1.132 +
   1.133 +	// Location to save captured images
   1.134 +	if (!WsIniFile->FindVar(KWSERVIniFileVarFrameCapture, iFrameCaptureLocation) || iFrameCaptureLocation.Length() == 0)
   1.135 +		{
   1.136 +		iFrameCaptureLocation.Set(KDefaultFrameCaptureLocation);
   1.137 +		}
   1.138 +#endif //USE_DEBUG_FRAME_CAPTURE
   1.139 +	}
   1.140 +
   1.141 +MWsTextCursor* CScreenRedraw::RenderStageTextCursor() const
   1.142 +	{
   1.143 +	return iRenderStageTextCursor;
   1.144 +	}
   1.145 +
   1.146 +const TTime& CScreenRedraw::Now() const
   1.147 +	{
   1.148 +	if(!iAnimating)
   1.149 +		{
   1.150 +		iNow.UniversalTime();
   1.151 +		}
   1.152 +	return iNow;
   1.153 +	}
   1.154 +
   1.155 +void CScreenRedraw::ScheduleRender(const TTimeIntervalMicroSeconds& aFromNow)
   1.156 +	{
   1.157 +	iRenderScheduled = ETrue;
   1.158 +	TTime then(Now() + aFromNow);
   1.159 +	if ((!iScheduled) || then < iNext)
   1.160 +		iNext = then;
   1.161 +	iScheduled = ETrue;
   1.162 +	CWsTop::WindowServer()->AnimationScheduler()->ScheduleAnimation(iScreen,iNext);
   1.163 +	}
   1.164 +
   1.165 +void CScreenRedraw::ScheduleRedraw()
   1.166 +	{
   1.167 +	iNext = Now();
   1.168 +	iScheduled = ETrue;
   1.169 +		
   1.170 +	// The other scheduler also removes future animations which this one encompasses.
   1.171 +	// We choose not to do the region calculations needed to achieve that here.
   1.172 +	CWindowServer* server = CWsTop::WindowServer();
   1.173 +	ASSERT(server);
   1.174 +	MWsAnimationScheduler* sched = server->AnimationScheduler();
   1.175 +	if(sched)
   1.176 +		sched->ScheduleRedraw(iScreen,iNext);
   1.177 +	else
   1.178 +		RDebug::Printf("CWsTop::WindowServer()->RedrawScheduler() is NULL!!!");			
   1.179 +	}
   1.180 +
   1.181 +/**
   1.182 +@param aRect in screen coordinates 
   1.183 +*/
   1.184 +void CScreenRedraw::ScheduleAnimation(TAnimType aType, const TRect& aRect,const TTimeIntervalMicroSeconds& aFromNow,const TTimeIntervalMicroSeconds& /*aFreq*/,const TTimeIntervalMicroSeconds& /*aStop*/, CWsWindow* aWindow)
   1.185 +	{
   1.186 +	TRect scheduledRect(aRect);
   1.187 +	TRect test(aRect);
   1.188 +	// In changetracking mode, animation is either scheduled via a window dirty region (aWindow non-NULL)
   1.189 +	// or via a sprite manager dirty region (aWindow NULL) if scheduling a floating sprite
   1.190 +	if (iScreen.ChangeTracking())
   1.191 +		{
   1.192 +		if (aWindow)
   1.193 +			{
   1.194 +			// scheduling a window dirty rect for a window/anim/sprite
   1.195 +			test.Intersection(aWindow->Abs());
   1.196 +			scheduledRect.Move(-aWindow->Origin()); // convert to window coordinates
   1.197 +			}
   1.198 +		// else, // scheduling a sprite manager dirty rect for a floating sprite
   1.199 +		}
   1.200 +	else
   1.201 +		{
   1.202 +		// scheduling a screen dirty rect
   1.203 +		test.Intersection(iScreen.DrawableArea());
   1.204 +		aWindow = NULL; // ensure all future timed draw screen rects are checked below
   1.205 +		}
   1.206 +	if(!test.IsEmpty())
   1.207 +		{
   1.208 +		const TTime then(Now() + aFromNow);
   1.209 +		TTimedRect tRect(aType, scheduledRect, then, aWindow);
   1.210 +
   1.211 +		const TInt error = iTimedDrawRect.InsertInOrderAllowRepeats(tRect,TTimedRect::Compare);
   1.212 +		if (KErrNone == error)
   1.213 +	   		{
   1.214 +			if (iScheduled)
   1.215 +				{
   1.216 +				if (then < iNext)
   1.217 +					{
   1.218 +					iNext = then;
   1.219 +					}
   1.220 +				}
   1.221 +			else
   1.222 +				{
   1.223 +				iNext = then;
   1.224 +				iScheduled = ETrue;
   1.225 +				}
   1.226 +			// remove further futures that are completely contained
   1.227 +			TInt count = iTimedDrawRect.Count();
   1.228 +			for(TInt i=0; i<count; i++)
   1.229 +				{
   1.230 +				const TTimedRect& future = iTimedDrawRect[i];
   1.231 +				if (future.iWindow != aWindow) // only check futures for the window/floating sprite/screen we're scheduling
   1.232 +					continue;
   1.233 +				if(future.iTime.Int64() > then.Int64())
   1.234 +					{
   1.235 +					TRect rect(scheduledRect);
   1.236 +					rect.BoundingRect(future.iRect);
   1.237 +					if(rect == scheduledRect) // future is completely contained within scheduledRect
   1.238 +						{
   1.239 +						iTimedDrawRect.Remove(i);
   1.240 +						count--;
   1.241 +						i--;
   1.242 +						}
   1.243 +					}
   1.244 +				}
   1.245 +			CWsTop::WindowServer()->AnimationScheduler()->ScheduleAnimation(iScreen,iNext);
   1.246 +
   1.247 +			// Blue rectangles for scheduled animations
   1.248 +			DEBUG_RECT(TRgb(0x00, 0x00, 0xFF),TRgb(0x00, 0x00, 0xFF, 0x20),&aRect);
   1.249 +			}
   1.250 +		}
   1.251 +	}
   1.252 +
   1.253 +TBool CScreenRedraw::IsScheduled(TAnimType aType, const TRect& aRect, CWsWindow* aWindow) const
   1.254 +	{
   1.255 +	TRect rect(aRect);
   1.256 +	if(aWindow)
   1.257 +		{
   1.258 +		rect.Move(-aWindow->Origin()); //convert to window coordinates
   1.259 +		}
   1.260 +	
   1.261 +	const TInt count(iTimedDrawRect.Count());
   1.262 +	for(TInt i=0; i<count; i++)
   1.263 +		{
   1.264 +		if ((iTimedDrawRect[i].iType == aType) && 
   1.265 +				(iTimedDrawRect[i].iRect == rect) && 
   1.266 +				(iTimedDrawRect[i].iWindow == aWindow))
   1.267 +			{
   1.268 +			return ETrue;
   1.269 +			}
   1.270 +		}
   1.271 +	return EFalse;
   1.272 +	}
   1.273 +
   1.274 +// This adds a region to the stored invalid region.
   1.275 +// The invalid region is the area of the screen that needs to be redrawn in addition to any animations.
   1.276 +// The draw region is the area of the screen on which only the top window needs to be redrawn.
   1.277 +// If the top window has transparency, this can only be true when it has been made newly visible.
   1.278 +// The value of aSchedule could be determined automatically from iAnimating, but passing it this way
   1.279 +// allows us to have the assert, which is a very valuable assert.
   1.280 +void CScreenRedraw::AddRedrawRegion(const TRegion& aRegion, TBool aSchedule, TRedrawDepth aDepth)
   1.281 +	{
   1.282 +	WS_ASSERT_DEBUG(!iAnimating || !aSchedule, EWsPanicScheduledRedraw);
   1.283 +
   1.284 +	if(aRegion.CheckError())
   1.285 +		{
   1.286 +		iInvalid.ForceError();
   1.287 +
   1.288 +		if (aSchedule)
   1.289 +			ScheduleRedraw();
   1.290 +		}
   1.291 +	else if(aRegion.Count()) // often called despite window not being visible
   1.292 +		{
   1.293 +		if (aDepth == ERedrawAll)
   1.294 +			{
   1.295 +			// red lines for an invalid region which is ready to be drawn
   1.296 +			DEBUG_REGION(TRgb(0xFF, 0x00, 0x00),TRgb(0xFF, 0x00, 0x00, 0x20),&aRegion);
   1.297 +			iInvalid.Union(aRegion);
   1.298 +			
   1.299 +			if (aSchedule)
   1.300 +				ScheduleRedraw();
   1.301 +			}
   1.302 +		else
   1.303 +			{
   1.304 +			// yellow lines for a valid region which we will draw on top of
   1.305 +			DEBUG_REGION(TRgb(0xFF, 0xFF, 0x00),TRgb(0xFF, 0xFF, 0x00, 0x20),&aRegion);
   1.306 +				
   1.307 +			iTopElement.Union(aRegion);
   1.308 +			
   1.309 +			if (aSchedule)
   1.310 +				ScheduleRedraw();			
   1.311 +			}
   1.312 +		}
   1.313 +	}
   1.314 +
   1.315 +// This causes any asynchronously scheduled redraw to happen immediately
   1.316 +// It should be avoided where possible for performance reasons, but is 
   1.317 +// needed whenever the redraw store is discarded for a window which still
   1.318 +// has a redraw region pending.
   1.319 +// @note This method always attempts to DoRedrawNow(), even if currently
   1.320 +//		 animating. In this context, animation is a frame update. With NGA
   1.321 +// 		 that update completes at some point in the future. If you get a request
   1.322 +//		 to DoRedrawNow() the expectation is that all updates scheduled on or 
   1.323 +//		 before that point in time will have completed by the time the call 
   1.324 +//		 returns. An update may have been scheduled during a current (asynchronous)
   1.325 +//		 Animate call...the animation scheduler has this knowledge, so let it
   1.326 +//		 decide what to do.
   1.327 +
   1.328 +void CScreenRedraw::DoRedrawNow()
   1.329 +	{
   1.330 +	CWsTop::WindowServer()->AnimationScheduler()->DoRedrawNow(iScreen);
   1.331 +	}
   1.332 +	
   1.333 +#ifdef USE_DEBUG_REGIONS
   1.334 +void CScreenRedraw::DebugRect(TRgb aColor, TRgb aFill, const TRect* aRect)
   1.335 +	{
   1.336 +	if (aRect)
   1.337 +		{
   1.338 +		CFbsBitGc * gc = iScreen.GetBitGc();
   1.339 +		gc->SetPenColor(aColor);
   1.340 +		gc->SetPenStyle(CGraphicsContext::ESolidPen);
   1.341 +		gc->SetPenSize(TSize(2,2));
   1.342 +		gc->SetBrushColor(aFill);
   1.343 +		gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
   1.344 +		TRect smaller = *aRect;
   1.345 +		smaller.iBr.iX -= 1;
   1.346 +		smaller.iBr.iY -= 1;
   1.347 +		gc->DrawRect(smaller);
   1.348 +		iScreen.Update();
   1.349 +		}
   1.350 +	}
   1.351 +
   1.352 +void CScreenRedraw::DebugRegion(TRgb aColor, TRgb aFill, const TRegion * aRegion)
   1.353 +	{
   1.354 +	if (aRegion)
   1.355 +		{
   1.356 +		CFbsBitGc * gc = iScreen.GetBitGc();
   1.357 +		gc->SetPenColor(aColor);
   1.358 +		gc->SetPenStyle(CGraphicsContext::ESolidPen);
   1.359 +		gc->SetPenSize(TSize(2,2));
   1.360 +		gc->SetBrushColor(aFill);
   1.361 +		gc->SetBrushStyle(CGraphicsContext::ESolidBrush);
   1.362 +		for (const TRect *rect = aRegion->RectangleList(); rect - aRegion->RectangleList() < aRegion->Count(); ++rect)
   1.363 +			{
   1.364 +			TRect smaller = *rect;
   1.365 +			smaller.iBr.iX -= 1;
   1.366 +			smaller.iBr.iY -= 1;
   1.367 +			gc->DrawRect(smaller);
   1.368 +			}
   1.369 +		iScreen.Update();
   1.370 +		}
   1.371 +	}
   1.372 +#endif 
   1.373 +
   1.374 +void CScreenRedraw::OnAnimation(TRequestStatus* aFinished)
   1.375 +	{
   1.376 +	LOG_SCREEN_REDRAW_START
   1.377 +	WS_ASSERT_ALWAYS(!iAnimating,EWsPanicAnimationAlreadyAnimating);
   1.378 +	WS_ASSERT_ALWAYS(iScheduled,EWsPanicAnimationNotScheduled);
   1.379 +	iAnimating = ETrue;
   1.380 +	iScheduled = EFalse;
   1.381 +
   1.382 +	const TBool changeTracking = iScreen.ChangeTracking();
   1.383 +	
   1.384 + 	const TRegionFix<1> fallbackRegion(iScreen.RootWindow()->Abs());
   1.385 +
   1.386 +	CWsActiveScheduler::Static()->PrepareDraw();
   1.387 +	
   1.388 +	// Calculate any updates required by region changes:
   1.389 +	RegionUpdate();
   1.390 +
   1.391 +	// Use the timed rectangles to mark screen, window or floating sprite as dirty
   1.392 +	RWsRegionBuf<10> floatingSpriteDirtyRegion;
   1.393 + 	const TBool futureAnimationRequired = ScheduleTimedRects(floatingSpriteDirtyRegion);
   1.394 + 	if (floatingSpriteDirtyRegion.CheckError())
   1.395 + 		{
   1.396 + 		floatingSpriteDirtyRegion.Reset();
   1.397 + 		floatingSpriteDirtyRegion.Copy(fallbackRegion);
   1.398 + 		}
   1.399 + 	
   1.400 + 	TWalkWindowTreeSchedule* scheduler = NULL;
   1.401 + 	TWalkWindowListSchedule windowScheduler(iScheduledWindowList, iInvalid); //ChangeTracking
   1.402 +	if (!changeTracking)
   1.403 +		{
   1.404 +		// Animating rectangles could cause iInvalid to overlap iTopElement, in which case iTopElement won't work.
   1.405 +		iTopElement.SubRegion(iInvalid);
   1.406 +		iTopElement.Intersect(iScreen.RootWindow()->WindowArea());
   1.407 +		iTopElement.Tidy();
   1.408 +		// Anything in the top element is implicitly invalid
   1.409 +		iInvalid.Union(iTopElement);
   1.410 +		iInvalid.Intersect(iScreen.RootWindow()->WindowArea());
   1.411 +		iInvalid.Tidy();
   1.412 +		
   1.413 +		if(iInvalid.CheckError())
   1.414 +			{
   1.415 +			iTopElement.Reset();
   1.416 +			iInvalid.Reset();
   1.417 +			iInvalid.Copy(fallbackRegion);
   1.418 +			}
   1.419 +		}
   1.420 +	else 
   1.421 +		{
   1.422 +		// In ChangeTracking mode, iInvalid is only used to tell the render stage
   1.423 +		// which part of the screen needs updating.
   1.424 +		iInvalid.Reset();
   1.425 +		iInvalid.Copy(floatingSpriteDirtyRegion);
   1.426 +		scheduler = &windowScheduler;
   1.427 +		windowScheduler.WalkWindowList();
   1.428 +		CWsTop::TriggerRedraws(iScreen.RootWindow()); //In case WalkWindowList did queue a request for the client to provide draw commands
   1.429 +		iInvalid.Tidy();
   1.430 +		if(iInvalid.CheckError())
   1.431 +			{
   1.432 +			iInvalid.Reset();
   1.433 +			iInvalid.Copy(fallbackRegion);
   1.434 +			}
   1.435 +		}
   1.436 +		
   1.437 +	// At this point, if the DEBUG_REGION is being used:
   1.438 +	// Red represents invalid regions that need to be redrawn completely.
   1.439 +	// Yellow represents regions that only need the top window to be drawn.
   1.440 +	// Blue represents regions which are being animated server side.
   1.441 +	if (iRenderScheduled || !iInvalid.IsEmpty() || iQuickFadeList.Count() > 0)  
   1.442 +		{
   1.443 +		iRenderScheduled = EFalse;
   1.444 +		
   1.445 +		TWalkWindowTreeScheduleRegions regionScheduler(iInvalid, iTopElement); //!ChangeTeacking
   1.446 +		TWalkWindowTreeScheduleFallback fallbackScheduler(iScreen.FallbackMap());//!ChangeTeacking
   1.447 +
   1.448 +		RWsRegionBuf<20> animationRegion;
   1.449 +		
   1.450 +		if (!changeTracking)
   1.451 +			{	
   1.452 +			animationRegion.Copy(iInvalid);
   1.453 +			AddQuickFadeableRegions(animationRegion);
   1.454 +			if (animationRegion.CheckError())
   1.455 +				{
   1.456 +				animationRegion.Reset();
   1.457 +				animationRegion.Copy(fallbackRegion);
   1.458 +				}
   1.459 +			iAnimationRegion = &animationRegion; //iAnimationRegion must be reset to NULL before the call stack unwinds.
   1.460 +			
   1.461 +			scheduler = &regionScheduler;
   1.462 +			iScreen.RootWindow()->WalkWindowTree(regionScheduler,EWalkChildren);
   1.463 +			if (!regionScheduler.ScheduledRegionsOk())
   1.464 +				{
   1.465 +				// our region calculations for what to draw failed at some point.
   1.466 +				// From this point on we MUST NOT rely on allocating memory
   1.467 +				scheduler = &fallbackScheduler;
   1.468 +				iScreen.FallbackMap()->Prepare();
   1.469 +				iScreen.RootWindow()->WalkWindowTree(fallbackScheduler,EWalkChildren);
   1.470 +				iAnimationRegion = iScreen.FallbackMap()->Region();
   1.471 +				}
   1.472 +			}
   1.473 +		else
   1.474 +			{
   1.475 +			iAnimationRegion = &iInvalid;
   1.476 +			}
   1.477 +		
   1.478 +		CWsActiveScheduler::Static()->StartDraw();
   1.479 +		CWsMemoryManager::Static()->EnableReserve();
   1.480 +		
   1.481 +		// Redraw debug regions more brightly than before:
   1.482 +		DEBUG_REGION(TRgb(0xFF, 0x00, 0x00),TRgb(0xFF, 0x00, 0x00, 0x80),iAnimationRegion);
   1.483 +		DEBUG_REGION(TRgb(0xFF, 0xFF, 0x00),TRgb(0xFF, 0xFF, 0x00, 0x80),&iTopElement);
   1.484 +		
   1.485 +		// Pipe the drawing into the first render stage:
   1.486 +		iRenderStages->Begin(iAnimationRegion);
   1.487 +		
   1.488 +		RWsRegionBuf<10> accumulatedDrawing;
   1.489 +		
   1.490 +		MWsGraphicsContext * stageGc = static_cast<MWsGraphicsContext*>(iRenderStages->ResolveObjectInterface(KMWsGraphicsContext));
   1.491 +		for (CWsWindow * win = scheduler->HeadWindow(); (win!=NULL); win = win->NextScheduled())
   1.492 +			{
   1.493 +			if(!changeTracking)
   1.494 +					accumulatedDrawing.Union(scheduler->WindowRegion(*win));
   1.495 +			
   1.496 +			Render(*win, *stageGc, *scheduler);
   1.497 +			}
   1.498 +
   1.499 +		if(!changeTracking && !accumulatedDrawing.CheckError())
   1.500 +			{
   1.501 +			//Fade any region of the screen scheduled for fading which has not been redrawn,
   1.502 +			DoQuickFade(stageGc, accumulatedDrawing);
   1.503 +			}
   1.504 +		accumulatedDrawing.Reset();
   1.505 +		
   1.506 +		//We limit floating sprite drawing to regions already touched
   1.507 +		iScreen.SpriteManager()->DrawFloatingSprites(stageGc, iScreen.ChangeTracking() ? floatingSpriteDirtyRegion : *iAnimationRegion);
   1.508 +		
   1.509 +		iRenderStages->End(aFinished);
   1.510 +		stageGc = NULL; //we're not allowed to draw outside Begin()/End()
   1.511 +
   1.512 +#if defined(__WINS__) && defined(_DEBUG)
   1.513 +		MWsDebugBar * debugBar = static_cast<MWsDebugBar*>(iRenderStages->ResolveObjectInterface(KMWsDebugBar));
   1.514 +		if (debugBar) //optional for the licensees
   1.515 +			{
   1.516 +			if (CDebugBar* dbg = iScreen.DebugBar())
   1.517 +				{	
   1.518 +				RArray<TPtrC> debugText;
   1.519 +				dbg->DebugBarInfo(debugText);
   1.520 +				debugBar->DrawDebugBar(debugText.Array());
   1.521 +				debugText.Close();
   1.522 +				}
   1.523 +			}
   1.524 +#endif
   1.525 +		
   1.526 +		iScheduledWindowList = NULL;
   1.527 +		CWsMemoryManager::Static()->DisableReserve();
   1.528 +
   1.529 +#ifdef USE_DEBUG_FRAME_CAPTURE
   1.530 +		if (iFrameCapture)
   1.531 +			{
   1.532 +			CaptureFrame(iAnimationRegion);
   1.533 +			}
   1.534 +#endif
   1.535 +		
   1.536 +		// DEBUG_REGION does not work with render stages.
   1.537 +		// These comments refer to what used to happen.
   1.538 +		
   1.539 +		// At this point, if the DEBUG_REGION is being used, there should usually be only green regions
   1.540 +		// displayed.  If we see red or yellow, then something didn't get redrawn that should have been.
   1.541 +		// If we see purple then a window has disobeyed the const setting on the region to draw.
   1.542 +		// Red or yellow is valid - a window can decide it isn't ready to draw yet - purple is very bad.
   1.543 +		
   1.544 +		//iScreen.Update();
   1.545 +		
   1.546 +		// At this point, if the DEBUG_REGION is being used, there should be no regions visible
   1.547 +		// of any colour.  If we see green, then it means an area of the screen was drawn to which
   1.548 +		// wasn't invalid, or the screen update call failed.  The former is more likely.
   1.549 +		// If we still see red or yellow it is a region that is not yet ready to draw.
   1.550 +
   1.551 +		const TRect* rect = iAnimationRegion->RectangleList();
   1.552 +		TInt pixels = 0;
   1.553 +		for(TInt r = iAnimationRegion->Count(); r>0; r--)
   1.554 +			{
   1.555 +			pixels += (rect->Width()*rect->Height());
   1.556 +			rect++;
   1.557 +			}
   1.558 +		CWsActiveScheduler::Static()->StopDraw(pixels);
   1.559 +		
   1.560 +		TWindowServerEvent::NotifyScreenDrawingEvent(iAnimationRegion);
   1.561 +
   1.562 +		iAnimationRegion = NULL; //iAnimationRegion must be reset to NULL before the call stack unwinds.
   1.563 +		iInvalid.Reset();
   1.564 +		animationRegion.Reset();
   1.565 +		}
   1.566 +	else
   1.567 +		{
   1.568 +		// There was nothing to compose. Signal that composition is complete.
   1.569 +		if (aFinished) 
   1.570 +			{
   1.571 +			*aFinished = KRequestPending;
   1.572 +			User::RequestComplete(aFinished, KErrNone);
   1.573 +			}
   1.574 +		
   1.575 +		CWsActiveScheduler::Static()->CancelPrepare();
   1.576 +		}
   1.577 +	
   1.578 +	floatingSpriteDirtyRegion.Reset();
   1.579 +	iInvalid.Reset();
   1.580 +	iTopElement.Reset();
   1.581 +	iAnimating = EFalse;
   1.582 +	
   1.583 +	if (futureAnimationRequired && iTimedDrawRect.Count() > 0 && !iScheduled)
   1.584 +		{
   1.585 +		// If this flag is set then it means there were already some animations scheduled when we ran,
   1.586 +		// but they themselves didn't run.  We need to make sure we have _something_ scheduled.
   1.587 +		CWsTop::WindowServer()->AnimationScheduler()->ScheduleAnimation(iScreen, iTimedDrawRect[0].iTime);
   1.588 +		iScheduled = ETrue;
   1.589 +		}
   1.590 +	
   1.591 +	if(iObserver)
   1.592 +		{
   1.593 +		iObserver->ScreenUpdated(iScreen.ScreenNumber());
   1.594 +		iObserver=NULL; //once signalled we are never going to call it again
   1.595 +		}
   1.596 +	LOG_SCREEN_REDRAW_END
   1.597 +	}
   1.598 +
   1.599 +void CScreenRedraw::AddQuickFadeableRegions(TRegion& aRegion)
   1.600 +	{
   1.601 +	if (iQuickFadeList.Count() > 0)
   1.602 +		{
   1.603 +		for (TInt idx = iQuickFadeList.Count() - 1; idx >= 0; idx --)
   1.604 +			{
   1.605 +			CWsWindow* win = iQuickFadeList[ idx ];
   1.606 +
   1.607 +			if ( !win->IsDSAHost() )
   1.608 +				{
   1.609 +				aRegion.Union(win->QuickFadeRegion() );
   1.610 +				}
   1.611 +			}
   1.612 +		aRegion.Tidy();
   1.613 +		}
   1.614 +	}
   1.615 +
   1.616 +void CScreenRedraw::DoQuickFade(MWsGraphicsContext* aGc, TRegion& aAccumulatedDrawing)
   1.617 +	{
   1.618 +	if ( iQuickFadeList.Count() > 0 )
   1.619 +		{
   1.620 +		for ( TInt idx = iQuickFadeList.Count() - 1; idx >= 0 ; idx -- )
   1.621 +			{
   1.622 +
   1.623 +			CWsWindow* win = iQuickFadeList[ idx ];
   1.624 +			
   1.625 +			if ( !win->IsDSAHost() )
   1.626 +				{				
   1.627 +				STACK_REGION winFadeRgn;				
   1.628 +				winFadeRgn.Copy( win->QuickFadeRegion() );
   1.629 +				winFadeRgn.SubRegion( aAccumulatedDrawing );
   1.630 +				winFadeRgn.Tidy();
   1.631 +				if ( !winFadeRgn.IsEmpty() && !winFadeRgn.CheckError() )
   1.632 +					{ // now fade any region that has not been redrawn (via win->Render())
   1.633 +					win->Fade( aGc, winFadeRgn );
   1.634 +					aAccumulatedDrawing.Union( winFadeRgn );	      		
   1.635 +					}		      	
   1.636 +	  			winFadeRgn.Close();
   1.637 +				}
   1.638 +			}
   1.639 +	
   1.640 +		iQuickFadeList.Reset();
   1.641 +		}
   1.642 +	}
   1.643 +
   1.644 +/**
   1.645 +This function will iterate the timed rectangles and find any animation or sprite 
   1.646 +that are due to be scheduled for render now. Then schedule them and remove them 
   1.647 +from iTimedDrawRect.
   1.648 +@return ETrue if iTimedDrawRect still contains future animations
   1.649 +*/
   1.650 +TBool CScreenRedraw::ScheduleTimedRects(TRegion& aScheduledFloatingSpriteRegion)
   1.651 + 	{
   1.652 +	iNow.UniversalTime();
   1.653 +	TInt count(iTimedDrawRect.Count());
   1.654 +	TBool futureAnimationRequired = EFalse;
   1.655 +	while (0 < count)
   1.656 +		{
   1.657 +		if(iTimedDrawRect[0].iTime.Int64() <= iNow.Int64())
   1.658 +			{
   1.659 +			if (iScreen.ChangeTracking())
   1.660 +				{
   1.661 +				switch(iTimedDrawRect[0].iType)
   1.662 +					{
   1.663 +					case ECrpAnim:
   1.664 +						{
   1.665 +						CWsWindow* win = iTimedDrawRect[0].iWindow;
   1.666 +						WS_ASSERT_DEBUG(win,EWsPanicWindowNull);
   1.667 +						if(!win->IsTrackingVisibleRegion() || (win->IsTrackingVisibleRegion() && !win->VisibleRegion().IsEmpty()))
   1.668 +							{
   1.669 +							TRegionFix<1> region;
   1.670 +							region.AddRect(iTimedDrawRect[0].iRect);
   1.671 +							win->AddDirtyWindowRegion(region);
   1.672 +							ScheduleWindow(win);
   1.673 +							}
   1.674 +						break;
   1.675 +						}
   1.676 +					case EWindowAnim:
   1.677 +					case ESpriteAnim:
   1.678 +					case ETextCursor:
   1.679 +					case EWindowSprite:
   1.680 +						{
   1.681 +						CWsWindow* win = iTimedDrawRect[0].iWindow;
   1.682 +						WS_ASSERT_DEBUG(win,EWsPanicWindowNull);
   1.683 +						if(!win->IsTrackingVisibleRegion() || (win->IsTrackingVisibleRegion() && !win->VisibleRegion().IsEmpty()))
   1.684 +							{
   1.685 +							TRegionFix<1> region;
   1.686 +							region.AddRect(iTimedDrawRect[0].iRect);
   1.687 +							win->AddDirtySpriteRegion(region);
   1.688 +							ScheduleWindow(win);
   1.689 +							}
   1.690 +						break;
   1.691 +						}
   1.692 +					case EFloatingSprite:
   1.693 +					case EFloatingSpriteAnim:
   1.694 +						{
   1.695 +						aScheduledFloatingSpriteRegion.AddRect(iTimedDrawRect[0].iRect);
   1.696 +						break;
   1.697 +						}
   1.698 +					}
   1.699 +				
   1.700 +				TRect screenRect(iTimedDrawRect[0].iRect);
   1.701 +				if(iTimedDrawRect[0].iWindow)
   1.702 +					{
   1.703 +					screenRect.Move(iTimedDrawRect[0].iWindow->Origin()); // convert to screen coordinates
   1.704 +					}	
   1.705 +				}
   1.706 +			else
   1.707 +				{ 
   1.708 +				// schedule a screen dirty rect
   1.709 +				iInvalid.AddRect(iTimedDrawRect[0].iRect);
   1.710 +				}
   1.711 +			iTimedDrawRect.Remove(0);
   1.712 +			count--;
   1.713 +			}
   1.714 +		else
   1.715 +			{
   1.716 +			futureAnimationRequired = ETrue;	
   1.717 +			break;
   1.718 +			}
   1.719 +		}
   1.720 +	return futureAnimationRequired;
   1.721 + 	}
   1.722 +
   1.723 +void CScreenRedraw::Render(CWsWindow& aWin, MWsGraphicsContext& aGc, const TWalkWindowTreeSchedule& aScheduler)
   1.724 +	{
   1.725 +	const TRegion* windowRegion = &(aScheduler.WindowRegion(aWin));
   1.726 +	const TRegion* spriteRegion = &(aScheduler.SpriteRegion(aWin));
   1.727 +
   1.728 +	if(windowRegion->IsEmpty() && spriteRegion->IsEmpty()) 
   1.729 +	    return; //Don't call CWsWindow::Render if there is nothing that can be rendered 
   1.730 +	
   1.731 +	//Make sure we don't try to draw outside screen
   1.732 +	STACK_REGION clippedWindowRegion;
   1.733 +	if(!windowRegion->IsContainedBy(iScreen.RootWindow()->Abs()))
   1.734 +		{
   1.735 +		TRegionFix<1> screen(iScreen.RootWindow()->Abs());
   1.736 +		clippedWindowRegion.Intersection(*windowRegion, screen);
   1.737 +		windowRegion = &clippedWindowRegion;
   1.738 +		}
   1.739 +	STACK_REGION clippedSpriteRegion;
   1.740 +	if(!spriteRegion->IsContainedBy(iScreen.RootWindow()->Abs()))
   1.741 +		{
   1.742 +		TRegionFix<1> screen(iScreen.RootWindow()->Abs());
   1.743 +		clippedSpriteRegion.Intersection(*spriteRegion, screen);
   1.744 +		spriteRegion = &clippedSpriteRegion;
   1.745 +		}	
   1.746 +	
   1.747 +	if(!windowRegion->CheckError() && !spriteRegion->CheckError())
   1.748 +		{
   1.749 +		// Purple regions are about to be drawn
   1.750 +		DEBUG_REGION(TRgb(0x80, 0x00, 0x80),TRgb(0x80, 0x00, 0x80, 0x80), windowRegion);
   1.751 +		DEBUG_REGION(TRgb(0x80, 0x00, 0x80),TRgb(0x80, 0x00, 0x80, 0x80), spriteRegion);
   1.752 +		
   1.753 +		aWin.Render(aGc, *windowRegion, *spriteRegion); //When not in ChangeTracking mode, both windowRegion and spriteRegion points to the same region
   1.754 +		aWin.ClearScheduledRegion();
   1.755 +		aWin.ClearScheduledSpriteRegion();
   1.756 +		
   1.757 +		// Green regions have been drawn
   1.758 +		DEBUG_REGION(TRgb(0x00, 0xFF, 0x00),TRgb(0x00, 0xFF, 0x00, 0x80), windowRegion);
   1.759 +		DEBUG_REGION(TRgb(0x00, 0xFF, 0x00),TRgb(0x00, 0xFF, 0x00, 0x80), spriteRegion);
   1.760 +		}
   1.761 +	else
   1.762 +		{
   1.763 +		OomRender(aWin, aGc, aScheduler);		
   1.764 +		}
   1.765 +	
   1.766 +	clippedSpriteRegion.Close();
   1.767 +	clippedWindowRegion.Close();
   1.768 +	}
   1.769 +
   1.770 +void CScreenRedraw::OomRender(CWsWindow& aWin, MWsGraphicsContext& aGc, const TWalkWindowTreeSchedule& aScheduler)
   1.771 +	{	
   1.772 +	const TRegion* windowRegion = &(aScheduler.WindowRegion(aWin));
   1.773 +	const TRegion* spriteRegion = &(aScheduler.SpriteRegion(aWin));
   1.774 +
   1.775 +	WS_ASSERT_DEBUG(!(windowRegion->IsEmpty() && spriteRegion->IsEmpty()), EWsPanicRegionNull);
   1.776 +	
   1.777 +	TRect validWindow(aWin.Abs());
   1.778 +	validWindow.Intersection(iScreen.RootWindow()->Abs());
   1.779 +	
   1.780 +    TRegionFix<1> fallbackRegion(validWindow);
   1.781 +    
   1.782 +    if(windowRegion->CheckError())
   1.783 +        windowRegion = &fallbackRegion;
   1.784 +    if(spriteRegion->CheckError())
   1.785 +        spriteRegion = &fallbackRegion;
   1.786 +    
   1.787 +    // Purple regions are about to be drawn
   1.788 +    DEBUG_REGION(TRgb(0x80, 0x00, 0x80),TRgb(0x80, 0x00, 0x80, 0x80), windowRegion);
   1.789 +    DEBUG_REGION(TRgb(0x80, 0x00, 0x80),TRgb(0x80, 0x00, 0x80, 0x80), spriteRegion);
   1.790 +    
   1.791 +    aWin.Render(aGc, *windowRegion, *spriteRegion); //When not in ChangeTracking mode, both windowRegion and spriteRegion points to the same region
   1.792 +    aWin.ClearScheduledRegion();
   1.793 +    aWin.ClearScheduledSpriteRegion();
   1.794 +    
   1.795 +    // Green regions have been drawn
   1.796 +    DEBUG_REGION(TRgb(0x00, 0xFF, 0x00),TRgb(0x00, 0xFF, 0x00, 0x80), windowRegion);
   1.797 +    DEBUG_REGION(TRgb(0x00, 0xFF, 0x00),TRgb(0x00, 0xFF, 0x00, 0x80), spriteRegion);	
   1.798 +	}
   1.799 +
   1.800 +//
   1.801 +void CScreenRedraw::DiscardAllSchedules()
   1.802 +	{
   1.803 +	ASSERT(!iAnimating);
   1.804 +	
   1.805 +	iTimedDrawRect.Reset();
   1.806 +	iInvalid.Reset();
   1.807 +	}
   1.808 +
   1.809 +/**
   1.810 +Indicates that a window has moved or changed ordinal position so that the visible regions
   1.811 +of all windows needs to be recalculated
   1.812 +*/
   1.813 +void CScreenRedraw::ScheduleRegionUpdate(const TRegion* aDefinitelyDirty)
   1.814 +	{
   1.815 +	if (iScreen.ChangeTracking() && iScreen.WindowVisibilityNotifier())
   1.816 +		return;
   1.817 +
   1.818 +	iRegionUpdateScheduled = ETrue;
   1.819 +	ScheduleRedraw();
   1.820 +	if(!iScreen.ChangeTracking() && aDefinitelyDirty)
   1.821 +		{
   1.822 +		iInvalid.Union(*aDefinitelyDirty);
   1.823 +		// Cyan regions for invalidations caused by this:
   1.824 +		DEBUG_REGION(TRgb(0x00, 0xFF, 0xFF),TRgb(0x00, 0xFF, 0xFF, 0x20),aDefinitelyDirty);
   1.825 +		}
   1.826 +	}
   1.827 +
   1.828 +/**
   1.829 +Only used in CHANGETRACKING mode.
   1.830 +Windows are scheduled to:
   1.831 +- if required, ask client to validate (from lower loop)
   1.832 +- render the dirty window region
   1.833 +*/
   1.834 +void CScreenRedraw::ScheduleWindow(CWsWindow* aWindow)
   1.835 +	{
   1.836 +	// Add a scheduled window to the linked list, ignoring duplicates
   1.837 +	CWsWindow* win = iScheduledWindowList;
   1.838 +	while (win && win != aWindow)
   1.839 +		{
   1.840 +		win = win->NextScheduled();
   1.841 +		}
   1.842 +	if (!win)
   1.843 +		{
   1.844 +		aWindow->SetNextScheduled(iScheduledWindowList);
   1.845 +		iScheduledWindowList = aWindow;
   1.846 +		}
   1.847 +	ScheduleRedraw();
   1.848 +	}
   1.849 +
   1.850 +void CScreenRedraw::RemoveFromScheduledList(CWsWindow* aWindow)
   1.851 +	{
   1.852 +	// Search for the window
   1.853 +	CWsWindow* win = iScheduledWindowList;
   1.854 +	CWsWindow* previous = NULL;
   1.855 +	while (win && win != aWindow)
   1.856 +		{
   1.857 +		previous = win;
   1.858 +		win = win->NextScheduled();
   1.859 +		}
   1.860 +	if (win)
   1.861 +		{
   1.862 +		// Found it, remove from list
   1.863 +		if (win == iScheduledWindowList)
   1.864 +			{
   1.865 +			iScheduledWindowList = win->NextScheduled();
   1.866 +			}
   1.867 +		else
   1.868 +			{
   1.869 +			previous->SetNextScheduled(win->NextScheduled());
   1.870 +			}
   1.871 +		}
   1.872 +	}
   1.873 +
   1.874 +void CScreenRedraw::RemoveFromTimedDrawList(CWsWindow* aWindow)
   1.875 +	{
   1.876 +	if(aWindow)
   1.877 +		{	
   1.878 +		TInt count(iTimedDrawRect.Count());
   1.879 +		TInt index = 0;
   1.880 +		while(index < count)
   1.881 +			{
   1.882 +			if (iTimedDrawRect[index].iWindow == aWindow)
   1.883 +				{
   1.884 +				iTimedDrawRect.Remove(index);
   1.885 +				--count;
   1.886 +				}
   1.887 +			else
   1.888 +				{
   1.889 +				++index;
   1.890 +				}
   1.891 +			}
   1.892 +		}
   1.893 +	}
   1.894 +
   1.895 +/**
   1.896 +Recalculates visible regions and schedules redraws or queues redraw events in response to
   1.897 +any changes
   1.898 +*/
   1.899 +void CScreenRedraw::RegionUpdate()
   1.900 +	{
   1.901 +	TBool somethingScheduled = EFalse;
   1.902 +
   1.903 +	if (iVisibilityUpdateScheduled)
   1.904 +		{
   1.905 +		iVisibilityUpdateScheduled = EFalse;
   1.906 +		somethingScheduled = ETrue;
   1.907 +		MWsWindowVisibilityNotifier* const notifier = iScreen.WindowVisibilityNotifier();
   1.908 +		if(notifier)
   1.909 +			{
   1.910 +			notifier->SendVisibilityChanges(); // Should result in one callback to SetWindowVisibility for each window subscribing for visible region updates
   1.911 +			}
   1.912 +		}
   1.913 +
   1.914 +	if (iRegionUpdateScheduled)
   1.915 +		{
   1.916 +		iRegionUpdateScheduled = EFalse;
   1.917 +		somethingScheduled = ETrue;
   1.918 +		TWalkWindowTreeUpdateRegions updater(iScreen);
   1.919 +		updater.Walk();
   1.920 +		
   1.921 +		for ( TInt idx = iQuickFadeList.Count() - 1; idx >= 0; idx-- )
   1.922 +			{
   1.923 +			CWsWindow* win = iQuickFadeList[ idx ];
   1.924 +			const TRegion& quickFadeRegion = win->QuickFadeRegion();
   1.925 +			//If QuickFadeRegion().IsEmpty() we should remove the window from iQuickFadeList.
   1.926 +			//And if this window has not been drawn to the screen, then it is not possible to quick fade it.
   1.927 +			if (quickFadeRegion.IsEmpty() || !win->HasBeenDrawnToScreen())
   1.928 +				{
   1.929 +				iQuickFadeList.Remove( idx );
   1.930 +				}
   1.931 +			}
   1.932 +		}
   1.933 +
   1.934 +	if (somethingScheduled)
   1.935 +		{
   1.936 +		TWsPointer::ReLogPointersCurrentWindows();
   1.937 +		CWsTop::TriggerRedraws(iScreen.RootWindow());
   1.938 +		}
   1.939 +	}
   1.940 +
   1.941 +/**
   1.942 +To be called by MWsWindowVisibilityNotifier (when running in ChangeTracking mode) 
   1.943 +in response to MWsWindowVisibilityNotifier::SendVisibilityChanges()  
   1.944 +*/
   1.945 +void CScreenRedraw::SetWindowVisibility(const MWsWindow& aWindow, const TRegion& aVisibleRegion)
   1.946 +	{
   1.947 +	CWsWindow& win = static_cast<CWsWindow&>(const_cast<MWsWindow&>(aWindow));
   1.948 +	WS_ASSERT_DEBUG(win.IsTrackingVisibleRegion(), EWsPanicVisibleRegionTracking);
   1.949 +	WS_ASSERT_DEBUG(!aVisibleRegion.CheckError(), EWsPanicRegion);
   1.950 +	
   1.951 +	if(aVisibleRegion.IsEmpty() && !win.VisibleRegion().IsEmpty())
   1.952 +		{
   1.953 +		win.ClearVisibleRegion();
   1.954 +		}
   1.955 +	else if(!aVisibleRegion.IsEmpty() && !aVisibleRegion.CheckError())
   1.956 +		{
   1.957 +		// Assert that aVisibleRegion is contained by aWin
   1.958 +		TRect bounds = win.AbsRect();
   1.959 +		bounds.Resize(1,1);
   1.960 +		WS_ASSERT_DEBUG(bounds.Contains(aVisibleRegion.BoundingRect().iTl), EWsPanicRegion);
   1.961 +		WS_ASSERT_DEBUG(bounds.Contains(aVisibleRegion.BoundingRect().iBr), EWsPanicRegion);
   1.962 +		win.SetVisibleRegion(aVisibleRegion, NULL);
   1.963 +		}
   1.964 +	else if(aVisibleRegion.CheckError())
   1.965 +		{
   1.966 +		const TRegionFix<1> fallback(win.Abs());
   1.967 +		win.SetVisibleRegion(fallback, NULL);
   1.968 +		}
   1.969 +	}
   1.970 +
   1.971 +/**
   1.972 +To be called by MWsWindowVisibilityNotifier (when running in ChangeTracking mode) when the 
   1.973 +iVisibleRegion is changed for a window that subscribes for this information. The visible region 
   1.974 +of the window has not been updated yet at the time of this function call, but the new metrics will be retrieved 
   1.975 +from MWsWindowVisibilityNotifier (through a call to MWsWindowVisibilityObserver::SetWindowVisibility())
   1.976 +and set to each window next time OnAnimation is called.   
   1.977 +*/
   1.978 +void CScreenRedraw::VisibilityChanged()
   1.979 +	{
   1.980 +	iVisibilityUpdateScheduled = ETrue;
   1.981 +	ScheduleRedraw();
   1.982 +	}
   1.983 +
   1.984 +void CScreenRedraw::SetObserver(MWsScreenRedrawObserver* aObserver)
   1.985 +	{
   1.986 +	iObserver = aObserver;
   1.987 +	}
   1.988 +
   1.989 +/**
   1.990 +Returns ETrue if an update or animation is scheduled.
   1.991 +
   1.992 +Note: Now that WSERV surface and composition updates are asynchronous (with NGA)
   1.993 +it is possible for this method to return EFalse, even if the last update has
   1.994 +not been completed by the rendering pipeline. This is dependant on the configuration
   1.995 +and implementation of render stages that make up the rendering pipeline.
   1.996 +
   1.997 +If in doubt, it is best to call CScreenRedraw::DoRedrawNow(), which will not return
   1.998 +untill all updates have been signalled as being complete (note that if signalling
   1.999 +is not used, then even this cannot guarantee completion).
  1.1000 +*/ 
  1.1001 +TBool CScreenRedraw::IsUpdatePending()
  1.1002 +	{
  1.1003 +	if(iScheduled || iAnimating)
  1.1004 +		return ETrue;
  1.1005 +	else 
  1.1006 +		return EFalse;
  1.1007 +	}
  1.1008 +
  1.1009 +/**
  1.1010 + Overidding MWsObjectProvider
  1.1011 +*/
  1.1012 +TAny* CScreenRedraw::ResolveObjectInterface(TUint aTypeId)
  1.1013 +	{
  1.1014 +	TAny* interface = NULL;
  1.1015 +
  1.1016 +	switch (aTypeId)
  1.1017 +		{
  1.1018 +		case KWsScreenRedraw:
  1.1019 +			interface = static_cast<MWsScreenRedraw*>(this);
  1.1020 +			break;
  1.1021 +		}
  1.1022 +
  1.1023 +	if (!interface)
  1.1024 +		{
  1.1025 +		interface = iRenderStages->ResolveObjectInterface(aTypeId);
  1.1026 +		}
  1.1027 +	
  1.1028 +	return interface;
  1.1029 +	}
  1.1030 +
  1.1031 +const TRegion * CScreenRedraw::AnimationRegion() const
  1.1032 +	{
  1.1033 +	if (iAnimating)
  1.1034 +		return iAnimationRegion;
  1.1035 +	else
  1.1036 +		return 0;
  1.1037 +	}
  1.1038 +
  1.1039 +void CScreenRedraw::UpdateDevice()
  1.1040 +	{
  1.1041 +	//this used to call iScreen->Update, not needed anymore in NGA
  1.1042 +	}
  1.1043 +
  1.1044 +TBool CScreenRedraw::IsQuickFadeScheduled( CWsWindow* aWin ) const
  1.1045 +	{
  1.1046 +	const TInt idx = iQuickFadeList.Find( aWin );	
  1.1047 +	return (idx > KErrNotFound);
  1.1048 +	}
  1.1049 +
  1.1050 +void CScreenRedraw::ScheduleQuickFade( CWsWindow* aWin )
  1.1051 +	{
  1.1052 +	const TInt idx = iQuickFadeList.Find( aWin );	
  1.1053 +	if ( idx == KErrNotFound )
  1.1054 +		{ // not yet enlisted		
  1.1055 +		if ( KErrNone != iQuickFadeList.Append(aWin) )
  1.1056 +			{  // out of resources?
  1.1057 +			const TRegion& winReg = aWin->VisibleRegion();
  1.1058 +			ScheduleRegionUpdate( &winReg );
  1.1059 +			}		
  1.1060 +		}
  1.1061 +	}
  1.1062 +
  1.1063 +void CScreenRedraw::RemoveFromQuickFadeList( CWsWindow* aWin )
  1.1064 +	{	
  1.1065 +	const TInt idx = iQuickFadeList.Find( aWin );	
  1.1066 +	if ( idx > KErrNotFound )
  1.1067 +		{
  1.1068 +		iQuickFadeList.Remove( idx );
  1.1069 +		}
  1.1070 +	}
  1.1071 +
  1.1072 +void CScreenRedraw::AcceptFadeRequest( CWsWindow* aWin, TBool aFadeOn )
  1.1073 +	{
  1.1074 +	if (aFadeOn)
  1.1075 +		{
  1.1076 +		if (aWin->WinType() != EWinTypeGroup)
  1.1077 +			{
  1.1078 +			ScheduleQuickFade(aWin);
  1.1079 +			ScheduleRegionUpdate(NULL);
  1.1080 +			}
  1.1081 +		}
  1.1082 +	else
  1.1083 +		{ // fade off, just initiate redraw
  1.1084 +		if ( !aWin->IsDSAHost() )
  1.1085 +			{
  1.1086 +			AddRedrawRegion(aWin->VisibleRegion() );
  1.1087 +			ScheduleRegionUpdate(NULL);
  1.1088 +			}
  1.1089 +		}
  1.1090 +	}
  1.1091 +
  1.1092 +
  1.1093 +#ifdef USE_DEBUG_FRAME_CAPTURE
  1.1094 +class TTruncateOverflow : public TDesOverflow
  1.1095 +	{
  1.1096 +public: //from TDesOverflow
  1.1097 +	void Overflow(TDes&) {};
  1.1098 +	};
  1.1099 +
  1.1100 +void CScreenRedraw::CaptureFrame(const TRegion* aRegion)
  1.1101 +	{
  1.1102 +	MWsScreenDevice* screenDevice = static_cast<MWsScreen&>(iScreen).ObjectInterface<MWsScreenDevice>();
  1.1103 +	WS_ASSERT_ALWAYS(screenDevice, EWsPanicScreenDeviceMissing);
  1.1104 +	const TSize screenSize(screenDevice->SizeInPixels());
  1.1105 +	const TDisplayMode screenDisplayMode(screenDevice->DisplayMode());
  1.1106 +
  1.1107 +	//copy the whole screen
  1.1108 +	TRAPD(err, SetupFrameCaptureResourcesL(screenSize, screenDisplayMode);
  1.1109 +	           screenDevice->CopyScreenToBitmapL(iDebugBitmap, TRect(screenSize)));
  1.1110 +
  1.1111 +	if(err)
  1.1112 +		{
  1.1113 +		if(wsDebugLog)
  1.1114 +			wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, _L("CScreenRedraw::CaptureFrame(): Failed to create resources for screen capture"));
  1.1115 +		return;
  1.1116 +		}
  1.1117 +
  1.1118 +	//remove what's not part of the region
  1.1119 +	iDebugBitmapGc->Activate(iDebugBitmapDevice);
  1.1120 +	
  1.1121 +	if(aRegion && !aRegion->IsEmpty() && !aRegion->CheckError())
  1.1122 +		{
  1.1123 +		RWsRegion inverseRegion;
  1.1124 +		inverseRegion.AddRect(TRect(screenSize));
  1.1125 +		const TRect* rectList = aRegion->RectangleList();
  1.1126 +		for(TInt i = aRegion->Count() - 1; i >= 0; i--)
  1.1127 +			{
  1.1128 +			inverseRegion.SubRect(rectList[i]);
  1.1129 +			}
  1.1130 +		if(!inverseRegion.CheckError())
  1.1131 +			{
  1.1132 +			iDebugBitmapGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
  1.1133 +			iDebugBitmapGc->SetClippingRegion(inverseRegion);
  1.1134 +			iDebugBitmapGc->SetBrushColor(TRgb(55, 55, 55, 0));
  1.1135 +			iDebugBitmapGc->Clear();
  1.1136 +			iDebugBitmapGc->CancelClippingRegion();
  1.1137 +			iDebugBitmapGc->SetDrawMode(CGraphicsContext::EDrawModePEN);
  1.1138 +			}
  1.1139 +		inverseRegion.Close();
  1.1140 +		}
  1.1141 +
  1.1142 +	//save to file
  1.1143 +	const TUint timestamp = User::FastCounter();	
  1.1144 +	TFileName filename;
  1.1145 +	TTruncateOverflow overflow;
  1.1146 +	filename.AppendFormat(iFrameCaptureLocation, &overflow);
  1.1147 +	filename.AppendFormat(_L("frame_%010u.mbm"), &overflow, timestamp);
  1.1148 +	iDebugBitmap->Save(filename);
  1.1149 +
  1.1150 +	//log region
  1.1151 +	LogRegion(filename, _L(" CScreenRedraw::CaptureFrame() "), aRegion);	
  1.1152 +	}
  1.1153 +
  1.1154 +void CScreenRedraw::SetupFrameCaptureResourcesL(const TSize& aScreenSize, TDisplayMode aScreenDisplayMode)
  1.1155 +	{
  1.1156 +	//make sure the existing bitmap has the correct display mode
  1.1157 +	if(iDebugBitmap && iDebugBitmap->DisplayMode() != aScreenDisplayMode)
  1.1158 +		{
  1.1159 +		if(iDebugBitmap->SetDisplayMode(aScreenDisplayMode) != KErrNone)
  1.1160 +			{
  1.1161 +			delete iDebugBitmap;
  1.1162 +			iDebugBitmap = NULL;
  1.1163 +			delete iDebugBitmapDevice;
  1.1164 +			iDebugBitmapDevice = NULL;
  1.1165 +			}
  1.1166 +		}
  1.1167 +
  1.1168 +	//make sure the existing bitmap has the correct size
  1.1169 +	if(iDebugBitmap && iDebugBitmap->SizeInPixels() != aScreenSize)
  1.1170 +		{
  1.1171 +		if(iDebugBitmap->Resize(aScreenSize) != KErrNone)
  1.1172 +			{
  1.1173 +			delete iDebugBitmap;
  1.1174 +			iDebugBitmap = NULL;
  1.1175 +			delete iDebugBitmapDevice;
  1.1176 +			iDebugBitmapDevice = NULL;
  1.1177 +			}
  1.1178 +		}
  1.1179 +
  1.1180 +	//make sure the bitmap and bitmap device is created
  1.1181 +	if(!iDebugBitmap)
  1.1182 +		{
  1.1183 +		WS_ASSERT_ALWAYS(!iDebugBitmapDevice, EWsPanicTemp); //this should never occur, they should always be created/destroyed in tandem
  1.1184 +		CFbsBitmap* bitmap = new(ELeave) CFbsBitmap;
  1.1185 +		CleanupStack::PushL(bitmap);
  1.1186 +		User::LeaveIfError(bitmap->Create(aScreenSize, aScreenDisplayMode));
  1.1187 +
  1.1188 +		iDebugBitmapDevice = CFbsBitmapDevice::NewL(bitmap);
  1.1189 +		iDebugBitmap = bitmap;
  1.1190 +		CleanupStack::Pop(bitmap);
  1.1191 +		}
  1.1192 +	
  1.1193 +	//make sure the gc is created
  1.1194 +	if(!iDebugBitmapGc)
  1.1195 +		{
  1.1196 +		User::LeaveIfError(iDebugBitmapDevice->CreateContext(iDebugBitmapGc));
  1.1197 +		}
  1.1198 +	}
  1.1199 +
  1.1200 +void CScreenRedraw::LogRegion(const TDesC& aPrefix, const TDesC& aFunctionName, const TRegion* aRegion)
  1.1201 +	{
  1.1202 +	if(!wsDebugLog)
  1.1203 +		return;
  1.1204 +
  1.1205 +	TBuf<LogTBufSize> log;
  1.1206 +	TTruncateOverflow overflow;
  1.1207 +	TInt rectCount = (aRegion == NULL ? 0 : aRegion->Count());
  1.1208 +	log.AppendFormat(aPrefix, &overflow);
  1.1209 +	log.AppendFormat(aFunctionName, &overflow);
  1.1210 +	log.AppendFormat(_L("region [%d,"), &overflow, rectCount);
  1.1211 +	if (rectCount > 0)
  1.1212 +		{
  1.1213 +		const TRect* rectangles = aRegion->RectangleList();
  1.1214 +		TBuf<1> comma;
  1.1215 +		for (TInt ii = 0; ii < rectCount; ii++)
  1.1216 +			{
  1.1217 +			TRect current = rectangles[ii];
  1.1218 +			log.AppendFormat(_L("%S{{%d,%d},{%d,%d}}"), &overflow, &comma,
  1.1219 +                             current.iTl.iX,current.iTl.iY,current.iBr.iX,current.iBr.iY);
  1.1220 +			comma = _L(",");
  1.1221 +			}
  1.1222 +		}
  1.1223 +	else
  1.1224 +		{
  1.1225 +		log.AppendFormat(_L("NULL"), &overflow);
  1.1226 +		}
  1.1227 +	log.AppendFormat(_L("]"), &overflow);
  1.1228 +	wsDebugLog->MiscMessage(CDebugLogBase::ELogEverything, log);
  1.1229 +	}
  1.1230 +#endif //USE_DEBUG_FRAME_CAPTURE
  1.1231 +