windowing/windowserver/nga/SERVER/openwfc/wnredraw.cpp
changeset 0 5d03bc08d59c
child 19 ac96196b945c
child 36 01a6848ebfd7
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Window redraw code, three sorts of redrawing are supported
       
    15 // Sending a redraw message to the client (see redrawmsgwindow.cpp)
       
    16 // Drawing from backup bitmap
       
    17 // Simply clearing the window
       
    18 // 
       
    19 //
       
    20 
       
    21 #include "wnredraw.h"
       
    22 #include "server.h"
       
    23 #include "playbackgc.h"
       
    24 #include "wstop.h"
       
    25 #include "ANIM.H"
       
    26 #include "EVQUEUE.H"
       
    27 #include <s32mem.h>
       
    28 #include <gdi.h>
       
    29 #include "panics.h"
       
    30 #include "inifile.h"
       
    31 #include "rootwin.h"
       
    32 #include "EVENT.H"
       
    33 #include "wstypes.h"
       
    34 #include <graphics/surface.h>
       
    35 #include <graphics/wselement.h>
       
    36 #include <graphics/wsscreendevice.h>
       
    37 #include "windowelementset.h"
       
    38 
       
    39 struct TFadingParams
       
    40 	{
       
    41 	TUint8 blackMap;
       
    42 	TUint8 whiteMap;
       
    43 	};
       
    44 
       
    45 CWsWindowRedraw::CWsWindowRedraw(CWsWindow *aWin) : iWsWin(aWin)
       
    46 	{
       
    47 	}
       
    48 
       
    49 CWsWindowRedraw::~CWsWindowRedraw()
       
    50 	{
       
    51 	if (iWsWin->WsOwner())
       
    52 		{
       
    53 		iWsWin->WsOwner()->RedrawQueue()->RemoveInvalid(this);
       
    54 		}
       
    55 	if (HasElement())
       
    56 		{
       
    57 		iWsWin->Screen()->WindowElements().ReleaseAllElements(*CliWin());
       
    58 		}
       
    59 	}
       
    60 
       
    61 void CWsWindowRedraw::ConstructL()
       
    62 	{
       
    63 	}
       
    64 
       
    65 const TRegion& CWsWindowRedraw::InvalidArea() const
       
    66 	{
       
    67 	return(nullRegion);
       
    68 	}
       
    69 
       
    70 const TRegion &CWsWindowRedraw::BaseDrawRegion() const
       
    71 	{
       
    72 	return(iWsWin->VisibleRegion());
       
    73 	}
       
    74 
       
    75 void CWsWindowRedraw::ClipInvalidRegion(const TRect &)
       
    76 	{
       
    77 	}
       
    78 
       
    79 void CWsWindowRedraw::Resize(const TSize &, const TSize &)
       
    80 	{
       
    81 	}
       
    82 
       
    83 void CWsWindowRedraw::SetReply(TInt aReply)
       
    84 	{
       
    85 	iWsWin->WsOwner()->SetReply(aReply);
       
    86 	}
       
    87 
       
    88 void CWsWindowRedraw::OwnerPanic(TClientPanic aPanic)
       
    89 	{
       
    90 	iWsWin->OwnerPanic(aPanic);
       
    91 	}
       
    92 
       
    93 CWsBackedUpWindow *CWsWindowRedraw::Backup() const
       
    94 	{
       
    95 	return(NULL);
       
    96 	}
       
    97 
       
    98 void CWsWindowRedraw::Scroll(const TRect &, const TPoint &,const TRect &)
       
    99 	{
       
   100 	}
       
   101 
       
   102 void CWsWindowRedraw::UpdateAnimArea()
       
   103 	{
       
   104 	}
       
   105 
       
   106 void CWsWindowRedraw::PrepareForResizeL(const TSize& /* aNewSize */, TSize& /* aOldSize */)
       
   107 	{
       
   108 	}
       
   109 
       
   110 TBool CWsWindowRedraw::DrawCommand(CWsGc*,const TAny*)
       
   111 	{
       
   112 	return ETrue;
       
   113 	}
       
   114 
       
   115 void CWsWindowRedraw::GcAttributeChange(CWsGc*,const TAny*)
       
   116 	{
       
   117 	}
       
   118 
       
   119 void CWsWindowRedraw::GcDeactivate(CWsGc*)
       
   120 	{
       
   121 	}
       
   122 
       
   123 CFbsDevice* CWsWindowRedraw::OutputDevice() const
       
   124 	{
       
   125 	return NULL;
       
   126 	}
       
   127 
       
   128 void CWsWindowRedraw::ClientExposing()
       
   129 	{
       
   130 	}
       
   131 
       
   132 void CWsWindowRedraw::ClearRedrawStore(TBool)
       
   133 	{}
       
   134 
       
   135 void CWsWindowRedraw::PreDrawWindow(MWsGraphicsContext* aGc, const TRegion& aWindowRegion)
       
   136 	{
       
   137 	WS_ASSERT_DEBUG(iRedrawRegion == NULL, EWsPanicScheduledRedraw);
       
   138 	iRedrawRegion = &aWindowRegion;
       
   139 	CPlaybackGc::Instance()->SetTargetRegion(iRedrawRegion);
       
   140 	CWsClient::iCurrentCommand.iOpcode=0;
       
   141 	CPlaybackGc::Instance()->Activate(CliWin(), aGc, iRedrawRegion);
       
   142 	}
       
   143 
       
   144 void CWsWindowRedraw::PostDrawWindow(MWsGraphicsContext* aGc, const TRegion& aWindowChildNodeRegion)
       
   145 	{
       
   146 	WS_ASSERT_DEBUG(iRedrawRegion, EWsPanicScheduledRedraw);
       
   147 	CPlaybackGc::Instance()->Deactivate();
       
   148 	CPlaybackGc::Instance()->SetTargetRegion(NULL);
       
   149 	
       
   150 	if(!Screen()->ChangeTracking())
       
   151 		{
       
   152 		DoFade(*iRedrawRegion);
       
   153 		}
       
   154 
       
   155 	AnnotateWindowRedrawEnd(*iWsWin);	
       
   156 	
       
   157 	DrawWindowAnims(aGc, aWindowChildNodeRegion);
       
   158 	DrawCursorAndSprites(aGc, aWindowChildNodeRegion);
       
   159 	iRedrawRegion = 0;
       
   160 	}
       
   161 
       
   162 void CWsWindowRedraw::Fade(MWsGraphicsContext * aGc, const TRegion& aRegion)
       
   163 	{
       
   164 	LOG_WINDOW_FADE_START(WsWin());	
       
   165 	AnnotateWindowRedrawStart(*iWsWin, aRegion);
       
   166 	
       
   167 	aGc->Reset();
       
   168 	DoFade(aRegion);
       
   169 	
       
   170 	AnnotateWindowRedrawEnd(*iWsWin);		
       
   171 	LOG_WINDOW_FADE_END(WsWin());	
       
   172 	}
       
   173 
       
   174 void CWsWindowRedraw::DoFade(const TRegion& aRegion)
       
   175 	{
       
   176 	if( CWsTop::IsFadeEnabled() && iWsWin && iWsWin->FadeCount()>0 && !(iWsWin->IsNonFading()) && !(iWsWin->FadableRegion().IsEmpty()) && !(iWsWin->IsDSAHost()) )
       
   177 		{
       
   178 		MWsFader* fader = static_cast<MWsFader*>(iWsWin->Screen()->ResolveObjectInterface(KMWsFader));
       
   179 		if(fader)
       
   180 			{
       
   181 		  	TFadingParams parameters;
       
   182 	  		iWsWin->GetFadingParams(parameters.blackMap,parameters.whiteMap);
       
   183 	      	TPckgBuf<TFadingParams> buf(parameters);
       
   184 	      	fader->SetFadingParameters(buf);
       
   185 		  	// Only fade the region that hasn't been faded before
       
   186 	  		STACK_REGION fdRgn;
       
   187 	  		fdRgn.Copy( aRegion );
       
   188 			fdRgn.Intersect( iWsWin->FadableRegion() );
       
   189 			if(!fdRgn.CheckError())
       
   190 				{
       
   191 				fader->FadeArea( fdRgn );
       
   192 				LOG_WINDOW_FADE_REGION(&fdRgn);
       
   193 				}
       
   194 	  		fdRgn.Close();
       
   195 	      	}
       
   196 		}
       
   197 	}
       
   198 
       
   199 void CWsWindowRedraw::DrawWindowAnims(MWsGraphicsContext * aGc, const TRegion& aRegion)
       
   200 	{
       
   201 	if (iWsWin->iAnimList)
       
   202 		{
       
   203 		// If an anim panics, it will leave and set the panic flag on the client
       
   204 		// The client itself won't actually panic yet, and we don't want to leave from here.
       
   205 		TRAP_IGNORE(DrawWindowAnimsL(aGc, aRegion));
       
   206 		}
       
   207 	}
       
   208 
       
   209 void CWsWindowRedraw::DrawWindowAnimsL(MWsGraphicsContext * aGc, const TRegion& aRegion)
       
   210 	{
       
   211 	for (CWsAnim * anim = iWsWin->iAnimList; anim; anim = anim->Next())
       
   212 		{
       
   213 		AnnotateWindowAnimRedrawStart(*iWsWin, *anim, aRegion);
       
   214 		
       
   215 		//Animate and redraw
       
   216 		TRAPD(err,anim->RedrawWindowAnimL(Screen()->Now(), aGc, &aRegion));
       
   217 		if(err!=KErrNone)
       
   218 			{
       
   219 			AnnotateWindowAnimRedrawEnd(*iWsWin, *anim);
       
   220 			anim->Panic(EWservPanicAnimLeave);
       
   221 			return;
       
   222 			}
       
   223 		
       
   224 		AnnotateWindowAnimRedrawEnd(*iWsWin, *anim);
       
   225 		}
       
   226 	}
       
   227 
       
   228 void CWsWindowRedraw::DrawCursorAndSprites(MWsGraphicsContext * aGc, const TRegion& aRegion)
       
   229 	{	
       
   230 	// Draw standard text cursor if required
       
   231 	RWsTextCursor* const cursor = CWsTop::CurrentTextCursor();
       
   232 	if (!iWsWin->Screen()->ChangeTracking() && cursor && cursor->Win() == iWsWin && cursor->IsStandardCursorActive())
       
   233 		{
       
   234 		// Standard text cursor is active on this window
       
   235 		const TBool flashing = cursor->IsFlashing();
       
   236 		TFlashState flashState = EFlashOn;
       
   237 		if (flashing)
       
   238 			{
       
   239 			flashState = cursor->CurrentCursorFlashState();
       
   240 			}
       
   241 		if (flashState == EFlashOn)
       
   242 			{
       
   243 			// Cursor should be visible, so draw it
       
   244 			cursor->Draw(aRegion);
       
   245 			}
       
   246 		if (flashing)
       
   247 			{
       
   248 			// Reschedule to flash the standard cursor on or off
       
   249 			Screen()->ScheduleAnimation(ETextCursor, cursor->RectRelativeToScreen(), Screen()->SpriteManager()->NextCursorFlashStateChange(), 0, 0, iWsWin);
       
   250 			}
       
   251 		}
       
   252 
       
   253 	for (CWsSpriteBase * sprite = iWsWin->iSpriteList; sprite; sprite = sprite->Next())
       
   254 		{
       
   255 		STACK_REGION redrawRegion;
       
   256 		sprite->CalcRedrawRegion(aRegion, redrawRegion);
       
   257 		if(redrawRegion.CheckError() || !redrawRegion.IsEmpty())
       
   258 			{
       
   259 			if (sprite->IsFlashingEnabled() || sprite->IsDirty() || sprite->HasAnimation())
       
   260 				{
       
   261 				AnnotateSpriteRedrawStart(*iWsWin, *sprite, redrawRegion);
       
   262 				
       
   263 				if(sprite->HasAnimation())
       
   264 					{
       
   265 					CWsAnim* anim = static_cast<CWsSprite*>(sprite)->iAnim;
       
   266 					WS_ASSERT_DEBUG(anim,EWsPanicAnim);
       
   267 					
       
   268 					//Animate and...
       
   269 					TRAPD(err, anim->AnimateSpriteAnimL(Screen()->Now()));
       
   270 					if(err!=KErrNone)
       
   271 						{
       
   272 						AnnotateSpriteRedrawEnd(*iWsWin, *sprite);
       
   273 						anim->Panic(EWservPanicAnimLeave);
       
   274 						return;
       
   275 						}
       
   276 					}
       
   277 				
       
   278 				//...call Redraw on the sprite
       
   279 				aGc->Reset();
       
   280 				sprite->Redraw(aGc, redrawRegion);
       
   281 				
       
   282 				AnnotateSpriteRedrawEnd(*iWsWin, *sprite);
       
   283 				}
       
   284 			}
       
   285 		redrawRegion.Close();
       
   286 		}
       
   287 	}
       
   288 
       
   289 TBool CWsWindowRedraw::Contains(const TArray<TGraphicDrawerId>& /*aDrawers*/,const TRegion& aRegion) const
       
   290 	{
       
   291 	// if in doubt, assume we do
       
   292 	return !aRegion.IsEmpty();
       
   293 	}
       
   294 
       
   295 TInt CWsWindowRedraw::DrawBackgroundColor(const TRegion& aRegion, TBool	aDoFillColor)
       
   296 	{
       
   297 	if (BackColor().Alpha() == 0 && !HasElement())
       
   298 		return KErrNone;
       
   299 	
       
   300 	if(aRegion.IsEmpty())
       
   301 		return KErrNone;
       
   302 	
       
   303 	TRect winAbs(CliWin()->AbsRect());	//fill size for background color fill
       
   304 	TRect surfaceAbs(0,0,0,0);			//fill size for background surface fill - initially disabled
       
   305 	
       
   306 	if (HasElement())
       
   307 		{
       
   308 		TBackgroundAttributes* backgroundAttributes = CliWin()->Screen()->WindowElements().FindBackgroundElement(*CliWin());
       
   309 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   310 		
       
   311 		if (backgroundAttributes->iElement)
       
   312 			{
       
   313 			if (backgroundAttributes->ExplicitExtent())
       
   314 				{
       
   315 				backgroundAttributes->iElement->GetDestinationRectangle(surfaceAbs);
       
   316 				surfaceAbs.Intersection(winAbs);
       
   317 				if (surfaceAbs==winAbs)
       
   318 					{
       
   319 					winAbs.iBr.iX=winAbs.iTl.iX;	//disable background color fill
       
   320 					}
       
   321 				}
       
   322 			else
       
   323 				{
       
   324 				surfaceAbs=winAbs;
       
   325 				winAbs.iBr.iX=winAbs.iTl.iX;	//disable background color fill
       
   326 				}
       
   327 			}
       
   328 		if (!aDoFillColor)
       
   329 			{
       
   330 			winAbs.iBr.iX=winAbs.iTl.iX;	//disable background color fill
       
   331 			}
       
   332 		}
       
   333 	
       
   334 	CPlaybackGc* playback = CPlaybackGc::Instance();
       
   335 	MWsGraphicsContext* gc = static_cast<MWsGraphicsContext*>(playback->ResolveObjectInterface(KMWsGraphicsContext));
       
   336 	gc->SetClippingRegion(aRegion);
       
   337 	gc->SetBrushStyle(MWsGraphicsContext::ESolidBrush);
       
   338 	gc->SetPenStyle(MWsGraphicsContext::ENullPen);
       
   339 	TInt err = KErrNone;
       
   340 	if (!winAbs.IsEmpty())
       
   341 		{
       
   342 		gc->SetBrushColor(BackColor());
       
   343 		gc->DrawRect(winAbs);
       
   344 		}
       
   345 	if (!surfaceAbs.IsEmpty())
       
   346 		{
       
   347 		gc->SetDrawMode(MWsGraphicsContext::EDrawModeWriteAlpha);
       
   348 		gc->SetBrushColor(TRgb(0,0,0,0));
       
   349 		gc->DrawRect(surfaceAbs);
       
   350 		gc->SetBrushColor(BackColor());		//leave in a sensible state
       
   351 		gc->SetDrawMode(MWsGraphicsContext::EDrawModePEN);
       
   352 		}
       
   353 	gc->ResetClippingRegion();
       
   354 	return err;
       
   355 	}
       
   356 
       
   357 TBool CWsWindowRedraw::ReleaseMemory(MWsMemoryRelease::TMemoryReleaseLevel)
       
   358 	{
       
   359 	return EFalse;
       
   360 	}
       
   361 
       
   362 void CWsWindowRedraw::VisibleRegionChange()
       
   363 	{
       
   364 	}
       
   365 
       
   366 TBool CWsWindowRedraw::ReadyToDraw() const
       
   367 	{
       
   368 	return ETrue;
       
   369 	}
       
   370 
       
   371 TBool CWsWindowRedraw::RedrawingInProgress() const
       
   372 	{
       
   373 	return EFalse;	
       
   374 	}
       
   375 
       
   376 void CWsWindowRedraw::WindowClosing()
       
   377 	{
       
   378 	ReleaseBackgroundElement();
       
   379 	}
       
   380 
       
   381 TBool CWsWindowRedraw::HasDsaElement() const
       
   382 	{
       
   383 	TBool hasDsaElement = EFalse;
       
   384 
       
   385 	if (HasElement())
       
   386 		{
       
   387 		CWsClientWindow* cliWin = CliWin();
       
   388 		CWindowElementSet& set = cliWin->Screen()->WindowElements();
       
   389 		TBackgroundAttributes* backgroundAttributes = set.FindBackgroundElement(*cliWin);
       
   390 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   391 
       
   392 		if (backgroundAttributes->iElement)
       
   393 			{
       
   394 			MWsElement& element = *(backgroundAttributes->iElement);
       
   395 			hasDsaElement = (element.ConnectedSurface() == cliWin->Screen()->DsaSurface());
       
   396 			}
       
   397 		}
       
   398 
       
   399 	return hasDsaElement;
       
   400 	}
       
   401 
       
   402 void CWsWindowRedraw::SetDsaElementL()
       
   403 	{
       
   404 	TRect extent(TPoint(0,0), WsWin()->Screen()->DSASizeInPixels());
       
   405 	MWsDisplayMapping *dispMap = WsWin()->Screen()->DisplayMapping();
       
   406 	TRect extentOut;
       
   407 	TRect extentInDSA;
       
   408 	if(dispMap)
       
   409 		{
       
   410 		dispMap->MapCoordinates(EDirectScreenAccessSpace,extent,EApplicationSpace,extentOut);
       
   411 		//DSA extent in application space intersects window extent in application space
       
   412 		extentOut.Intersection(WsWin()->FullRect());		
       
   413 		if(extentOut.IsEmpty())
       
   414 			{
       
   415 			extentOut.SetRect(0,0,0,0);
       
   416 			}
       
   417 		//use DSA coordinates to determine the viewport
       
   418 		dispMap->MapCoordinates(EApplicationSpace, extentOut, EDirectScreenAccessSpace, extentInDSA);
       
   419 		}
       
   420 	else
       
   421 		{
       
   422 		extentOut = extent;
       
   423 		extentInDSA = extent;
       
   424 		extentOut.Intersection(WsWin()->FullRect());
       
   425 		}
       
   426 	if (!HasDsaElement())
       
   427 		{
       
   428 		WsWin()->Screen()->ClearDsaSurface(extent, BackColor());
       
   429 		}
       
   430 
       
   431 	TSurfaceConfiguration sc;
       
   432 	sc.SetSurfaceId(WsWin()->Screen()->DsaSurface());
       
   433 	sc.SetExtent(extentOut.Size());
       
   434 	sc.SetViewport(extentInDSA);
       
   435 
       
   436 	SetBackgroundSurfaceL(sc, ETrue, ETrue);
       
   437 	}
       
   438 
       
   439 TBackgroundAttributes& CWsWindowRedraw::AcquireBackgroundElementL()
       
   440 	{
       
   441 	// Only client windows can have elements set
       
   442 	WS_ASSERT_DEBUG(iWsWin->WinType() == EWinTypeClient,EWsPanicWindowType);
       
   443 	CWsClientWindow* cliWin = static_cast<CWsClientWindow*>(iWsWin);
       
   444 	CScreen* screen = cliWin->Screen();
       
   445 	WS_ASSERT_DEBUG(screen,EWsPanicNoScreen);
       
   446 
       
   447 	CWindowElementSet& set = screen->WindowElements();
       
   448 	SetHasElement(EFalse);
       
   449 	TBackgroundAttributes& backgroundAttributes = set.AcquireBackgroundElementL(*cliWin);
       
   450 	MWsElement& element = *(backgroundAttributes.iElement);
       
   451 	element.SetGlobalAlpha(cliWin->IsVisible() ? 0xFF : 0);
       
   452 	SetHasElement(ETrue);
       
   453 	screen->ElementAdded();
       
   454 	
       
   455 	return backgroundAttributes;
       
   456 	}
       
   457 
       
   458 void CWsWindowRedraw::SetBackgroundSurfaceL(const TSurfaceId& aSurface)
       
   459 	{
       
   460 	if (aSurface.Type() == TSurfaceId::EScreenSurface || aSurface.IsNull())
       
   461 		{
       
   462 		OwnerPanic(EWservPanicInvalidSurface);
       
   463 		}
       
   464 
       
   465 	CWsClientWindow* cliWin = CliWin();
       
   466 	CScreen* screen = cliWin->Screen();
       
   467 	CWindowElementSet& set = screen->WindowElements();
       
   468 	TBackgroundAttributes& backgroundAttributes = AcquireBackgroundElementL();
       
   469 	MWsElement& element = *(backgroundAttributes.iElement);
       
   470     TInt err = set.RegisterSurface(aSurface);
       
   471     if (err != KErrNone)
       
   472         {
       
   473         ReleaseBackgroundElement();
       
   474         User::Leave(err);
       
   475         }
       
   476 	err = element.ConnectSurface(aSurface); 
       
   477 	if (err != KErrNone)
       
   478 		{
       
   479 		set.UnregisterSurface(aSurface);
       
   480 		ReleaseBackgroundElement();
       
   481 		User::Leave(err);
       
   482 		}
       
   483 
       
   484 	TRect winExtent = cliWin->FullRect();
       
   485 	element.SetDestinationRectangle(winExtent);
       
   486 	
       
   487 	// By default Element's source rectangle is set to its surface rectangle
       
   488 	TRect srcRect;
       
   489 	element.GetSourceRectangle(srcRect);
       
   490 	cliWin->SetOriginalSrcElementRect(srcRect);
       
   491 	cliWin->SetOriginalDestElementRect(winExtent);
       
   492 
       
   493 	SetMayContainElementFlags();
       
   494 	
       
   495 	MWsWindowTreeObserver* windowTreeObserver = Screen()->WindowTreeObserver();
       
   496 	if (windowTreeObserver)
       
   497 		{
       
   498 		windowTreeObserver->ElementAdded(*iWsWin, element);
       
   499 		}
       
   500 	}
       
   501 
       
   502 void CWsWindowRedraw::SetBackgroundSurfaceL(const TSurfaceConfiguration& aConfiguration, TBool aTriggerRedraw, TBool aAllowScreenSurface)
       
   503 	{
       
   504 	if (aConfiguration.Size() < sizeof(TSurfaceConfiguration))
       
   505 		{
       
   506 		__ASSERT_COMPILE(sizeof(TSurfaceConfiguration2)==sizeof(TSurfaceConfiguration));
       
   507 		if (aConfiguration.Size() != sizeof(TSurfaceConfiguration1))
       
   508 			{
       
   509 			OwnerPanic(EWservPanicInvalidSurfaceConfiguration);
       
   510 			}
       
   511 		}
       
   512 
       
   513 	TSurfaceId surfaceId;
       
   514 	aConfiguration.GetSurfaceId(surfaceId);
       
   515 	if ((surfaceId.Type() == TSurfaceId::EScreenSurface && !aAllowScreenSurface) || surfaceId.IsNull())
       
   516 		{
       
   517 		OwnerPanic(EWservPanicInvalidSurface);
       
   518 		}
       
   519 
       
   520 	CFbsBitGc::TGraphicsOrientation tempOrientation = aConfiguration.Orientation();
       
   521 	__ASSERT_COMPILE(CFbsBitGc::EGraphicsOrientationNormal==0 && 
       
   522 			CFbsBitGc::EGraphicsOrientationRotated270 == 3);
       
   523 	if(tempOrientation < CFbsBitGc::EGraphicsOrientationNormal || 
       
   524 			tempOrientation > CFbsBitGc::EGraphicsOrientationRotated270)
       
   525 		{
       
   526 		OwnerPanic(EWservPanicInvalidSurfaceConfiguration);
       
   527 		}
       
   528 
       
   529 	CWsClientWindow* cliWin = CliWin();
       
   530 	CScreen* screen = cliWin->Screen();
       
   531 	__ASSERT_DEBUG(screen, Panic(EWsPanicNoScreen));
       
   532 	
       
   533 	CWindowElementSet& set = screen->WindowElements();
       
   534 	TBool mustRegister = ETrue;
       
   535 	TRect oldExtent(0,0,0,0);
       
   536 	MWsElement::TElementRotation oldRotation = MWsElement::EElementAntiClockwise0;
       
   537 	TBool oldFlip = EFalse;
       
   538 	TRect oldViewport(0,0,0,0);
       
   539 	TSurfaceId oldSurfaceId = TSurfaceId::CreateNullId();
       
   540 
       
   541 	// If a element has already been set
       
   542 	if (HasElement())
       
   543 		{
       
   544 		TBackgroundAttributes* backgroundAttributes = set.FindBackgroundElement(*cliWin);
       
   545 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   546 		
       
   547 		if (backgroundAttributes->iElement)
       
   548 			{
       
   549 			MWsElement& element = *(backgroundAttributes->iElement);
       
   550 			element.GetDestinationRectangle(oldExtent);
       
   551 			element.GetSourceRectangle(oldViewport);
       
   552 			oldRotation = element.SourceRotation();
       
   553 			oldFlip = element.SourceFlipping();
       
   554 			oldSurfaceId = element.ConnectedSurface();
       
   555 			mustRegister = EFalse;
       
   556 			// If it is a different surface, flag to register the new surface
       
   557 			if (element.ConnectedSurface() != surfaceId)
       
   558 				{
       
   559 				mustRegister = ETrue;
       
   560 				}
       
   561 			}
       
   562 		}
       
   563 	
       
   564 	//the call to AcquireBackgroundElementL() will remove any existing background element
       
   565 	TBackgroundAttributes& backgroundAttributes = mustRegister ? 
       
   566 			AcquireBackgroundElementL() : *(set.FindBackgroundElement(*cliWin));
       
   567 	MWsElement& element = *(backgroundAttributes.iElement);
       
   568 	TInt err = KErrNone;
       
   569 	if (mustRegister)
       
   570 		{
       
   571         err = set.RegisterSurface(surfaceId);
       
   572         switch(err)
       
   573             {
       
   574         case KErrBadHandle:
       
   575             // Invalid surface IDs have to return KErrArgument
       
   576             err = KErrArgument;
       
   577             // drop through
       
   578         case KErrNoMemory:
       
   579         case KErrArgument:
       
   580             ReleaseBackgroundElement();
       
   581             User::Leave(err);
       
   582         case KErrNone:
       
   583             break;
       
   584         default:
       
   585             // No need to release layer here since session closure will do it
       
   586             // automatically when the client thread is panicked.
       
   587             OwnerPanic(EWservPanicInvalidSurface);
       
   588             }
       
   589 
       
   590 		err = element.ConnectSurface(surfaceId);
       
   591 		if (err != KErrNone)
       
   592 			{
       
   593 			set.UnregisterSurface(surfaceId);
       
   594 			ReleaseBackgroundElement();	//Releasing new empty element
       
   595 			User::Leave(err);
       
   596 			}
       
   597 
       
   598 		if (screen->DsaSurface() == surfaceId)
       
   599 			{
       
   600 	         TUint32 flags; 
       
   601 	         element.GetRenderStageFlags(flags);
       
   602 	         flags |= MWsElement::EElementIsDirectlyRenderedUserInterface;
       
   603 	         element.SetRenderStageFlags(flags);
       
   604 			}
       
   605 		}
       
   606 
       
   607 	SetHasElement(ETrue);		//set element flag
       
   608 	SetMayContainElementFlags();
       
   609 	
       
   610 	err = CWindowElement::SetElement(element,aConfiguration,ETrue);	//set viewport and orientation
       
   611 	if (err == KErrArgument)
       
   612 		{
       
   613 		OwnerPanic(EWservPanicInvalidSurfaceConfiguration);
       
   614 		}
       
   615     TRect srcRect;
       
   616     aConfiguration.GetViewport(srcRect);
       
   617     if (!srcRect.IsEmpty())
       
   618         backgroundAttributes.SetExplicitViewPort();
       
   619     element.GetSourceRectangle(srcRect);
       
   620     cliWin->SetOriginalSrcElementRect(srcRect);
       
   621 
       
   622 	//Set Extent
       
   623 	TRect newExtent;
       
   624 	aConfiguration.GetExtent(newExtent);
       
   625 	SetElementExtentL(newExtent, backgroundAttributes);
       
   626 	cliWin->SetOriginalDestElementRect(newExtent);
       
   627 	
       
   628 	MWsWindowTreeObserver* windowTreeObserver = Screen()->WindowTreeObserver();
       
   629 	if (windowTreeObserver && mustRegister)
       
   630 		{
       
   631 		windowTreeObserver->ElementAdded(*iWsWin, element);
       
   632 		}
       
   633 	
       
   634 	//If set, redraw
       
   635 	if (aTriggerRedraw)
       
   636 		{
       
   637 		TRect newViewport;
       
   638 		aConfiguration.GetViewport(newViewport);
       
   639 		CFbsBitGc::TGraphicsOrientation orientation = aConfiguration.Orientation();
       
   640 		MWsElement::TElementRotation newRotation = GcToElementRotation(orientation);
       
   641 		TBool newFlip = aConfiguration.Flip();
       
   642 		
       
   643 		//The following parameter guarantees that update will be scheduled. 
       
   644 		//This will trigger the composition.
       
   645 		TBool alwaysScheduleUpdate = (oldSurfaceId != surfaceId) || 
       
   646 									(oldExtent != newExtent) ||
       
   647 									(oldViewport != newViewport) ||
       
   648 									(oldRotation != newRotation)||
       
   649 									(oldFlip != newFlip);
       
   650 									
       
   651 		ElementRedraw(oldExtent,newExtent,alwaysScheduleUpdate);
       
   652 		}
       
   653 	}
       
   654 
       
   655 /**
       
   656 Sets the EMayContainElement flag for parent window.
       
   657 Sets the flag for all ancestor windows.
       
   658 **/
       
   659 void CWsWindowRedraw::SetMayContainElementFlags()
       
   660 	{
       
   661 	CWsWindowBase* parent = CliWin()->BaseParent();
       
   662 	TInt type = parent->WinType();
       
   663 	while(type ==EWinTypeClient)
       
   664 			{
       
   665 			CWsClientWindow* win = static_cast<CWsClientWindow*>(parent);
       
   666 			win->Redraw()->iStateFlags |= EMayContainElement;
       
   667 			parent=parent->BaseParent();
       
   668 			type = parent->WinType();
       
   669 			}
       
   670 	}
       
   671 
       
   672 void CWsWindowRedraw::SetElementExtentL(TRect& aNewExtent, TBackgroundAttributes& aAttributes)
       
   673 	{
       
   674 	CWsClientWindow* cliWin = CliWin();
       
   675 	MWsElement& element = *(aAttributes.iElement);
       
   676 	if (aNewExtent.IsEmpty())
       
   677 		{
       
   678 		aNewExtent = cliWin->FullRect();
       
   679 		aAttributes.SetExplicitExtent(EFalse);
       
   680 		}
       
   681 	else
       
   682 		{
       
   683 		TRect tempWindowPosition = cliWin->FullRect();		//get window absolute coordinates
       
   684 		aNewExtent.Move(tempWindowPosition.iTl);				//shift user defined extent to absolute coordinates
       
   685 		aAttributes.SetExplicitExtent(ETrue);
       
   686 		}
       
   687 	element.SetDestinationRectangle(aNewExtent);
       
   688 	}
       
   689 
       
   690 void CWsWindowRedraw::ElementRedraw(const TRect& aOldExtent, const TRect& aNewExtent, TBool aAlwaysScheduleUpdate)
       
   691 	{
       
   692 	if (!aOldExtent.IsEmpty())
       
   693 		{
       
   694 		//If the previous extent was different
       
   695 		if (aOldExtent != aNewExtent)
       
   696 			{
       
   697 			STACK_REGION tempRegion;
       
   698 			tempRegion.AddRect(aOldExtent);
       
   699 			tempRegion.AddRect(aNewExtent);
       
   700 
       
   701 			//Calculate the difference between
       
   702 			TRect tempRect = aOldExtent;
       
   703 			tempRect.Intersection(aNewExtent);			//intersect both regions
       
   704 			tempRegion.SubRect(tempRect);		//cut unaltered region
       
   705 			Screen()->ScheduleRegionUpdate(&tempRegion);
       
   706 
       
   707 			tempRegion.Close();
       
   708 			}
       
   709 		else
       
   710 			{
       
   711 			if(aAlwaysScheduleUpdate)
       
   712 				{
       
   713 				TTimeIntervalMicroSeconds interval(0);
       
   714 				Screen()->ScheduleRender(interval);
       
   715 				}
       
   716 			}
       
   717 		}
       
   718 	else
       
   719 		{
       
   720 		TRegionFix<1> region(aNewExtent);
       
   721 		Screen()->ScheduleRegionUpdate(&region);
       
   722 		}
       
   723 	}
       
   724 
       
   725 void CWsWindowRedraw::RemoveBackgroundSurface(TBool aTriggerRedraw)
       
   726 	{
       
   727 	if (HasElement())
       
   728 		{
       
   729 		TBackgroundAttributes* backgroundAttributes = CliWin()->Screen()->
       
   730 				WindowElements().FindBackgroundElement(*CliWin());
       
   731 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   732 		if (backgroundAttributes->iElement)
       
   733 			{
       
   734 			RemoveBackgroundElement(aTriggerRedraw);
       
   735 			}
       
   736 		}
       
   737 	}
       
   738 
       
   739 void CWsWindowRedraw::RemoveBackgroundElement(TBool aTriggerRedraw)
       
   740 	{
       
   741 	CWsClientWindow* cliWin = CliWin();
       
   742 	CScreen* screen = cliWin->Screen();
       
   743 	TRect tempRect;
       
   744 	if (aTriggerRedraw)
       
   745 		{
       
   746 		TBackgroundAttributes* backgroundAttributes = screen->WindowElements().FindBackgroundElement(*CliWin());
       
   747 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   748 		if (backgroundAttributes->ExplicitExtent())
       
   749 			{
       
   750 			backgroundAttributes->iElement->GetDestinationRectangle(tempRect);
       
   751 			backgroundAttributes->SetExplicitExtent(EFalse);
       
   752 			}
       
   753 		else
       
   754 			{
       
   755 			tempRect = cliWin->FullRect();
       
   756 			}
       
   757 		}
       
   758 	ReleaseBackgroundElement();
       
   759     if (aTriggerRedraw)
       
   760         {
       
   761         if (screen->ChangeTracking())
       
   762             {
       
   763             TTimeIntervalMicroSeconds interval(0);
       
   764             screen->ScheduleRender(interval);
       
   765             }
       
   766         else
       
   767             {
       
   768             TRegionFix<1> region(tempRect);
       
   769             screen->ScheduleRegionUpdate(&region);
       
   770             }
       
   771         }
       
   772 	}
       
   773 
       
   774 void CWsWindowRedraw::GetBackgroundSurfaceL(TSurfaceConfiguration& aConfiguration)
       
   775 	{
       
   776 	if (aConfiguration.Size() < sizeof(TSurfaceConfiguration))
       
   777 		{
       
   778 		__ASSERT_COMPILE(sizeof(TSurfaceConfiguration2)==sizeof(TSurfaceConfiguration));
       
   779 		if (aConfiguration.Size() != sizeof(TSurfaceConfiguration1))
       
   780 			{
       
   781 			OwnerPanic(EWservPanicInvalidSurfaceConfiguration);
       
   782 			}
       
   783 		}
       
   784 	
       
   785 	CWsClientWindow* cliWin = CliWin();
       
   786 	TBackgroundAttributes* backgroundAttributes = NULL;
       
   787 
       
   788 	if (HasElement())
       
   789 		{
       
   790 		backgroundAttributes = cliWin->Screen()->WindowElements().FindBackgroundElement(*cliWin);
       
   791 		WS_ASSERT_DEBUG(backgroundAttributes,EWsPanicNoWindowElement);
       
   792 		if (!backgroundAttributes->iElement)
       
   793 			{
       
   794 			User::Leave(KErrNotFound);
       
   795 			}
       
   796 		}
       
   797 	else
       
   798 		{
       
   799 		User::Leave(KErrNotFound);
       
   800 		}
       
   801 
       
   802 	MWsElement& element = *(backgroundAttributes->iElement);
       
   803 
       
   804 	TInt errCode=CWindowElementSet::GetConfiguration(aConfiguration,element);
       
   805 
       
   806 	// Get source rect
       
   807 	if (errCode>=KErrNone)
       
   808 	    {
       
   809 	    if (!backgroundAttributes->ExplicitViewPort())
       
   810 	        {
       
   811 	        aConfiguration.SetViewport(TRect());
       
   812 	        }
       
   813 	    else
       
   814 	        {
       
   815 	        TRect tempExtent = cliWin->GetOriginalSrcElementRect();
       
   816 	        aConfiguration.SetViewport(tempExtent);
       
   817 	        }
       
   818 	    }
       
   819 
       
   820 	//Convert and copy extent
       
   821 	if (errCode>=KErrNone)
       
   822 		{
       
   823 		if (!backgroundAttributes->ExplicitExtent())
       
   824 			{
       
   825 			aConfiguration.SetExtent(TRect());
       
   826 			}
       
   827 		else	//translate to window coordinates
       
   828 			{
       
   829 			TRect tempExtent = cliWin->GetOriginalDestElementRect();
       
   830 			tempExtent.Move(-cliWin->Origin());
       
   831 			aConfiguration.SetExtent(tempExtent);
       
   832 			}
       
   833 		}
       
   834 	}
       
   835 
       
   836 void CWsWindowRedraw::ReleaseBackgroundElement()
       
   837 	{
       
   838 	if (HasElement())
       
   839 		{
       
   840 		CWsClientWindow* cliWin = CliWin();
       
   841 		CScreen* screen = cliWin->Screen();
       
   842 		screen->WindowElements().ReleaseBackgroundElement(*cliWin, ETrue);
       
   843 		screen->ElementRemoved();
       
   844 		}
       
   845 	}
       
   846 
       
   847 //
       
   848 // Blank window //
       
   849 //
       
   850 
       
   851 CWsBlankWindow::CWsBlankWindow(CWsWindow *aWin) : CWsWindowRedraw(aWin), iColor(iWsWin->RootWindow()->DefaultBackgroundColor()), iNoColor(EFalse)
       
   852 	{
       
   853 	}
       
   854 
       
   855 CWsBlankWindow::~CWsBlankWindow()
       
   856 	{
       
   857 	}
       
   858 
       
   859 void CWsBlankWindow::ConstructL()
       
   860 	{
       
   861 	CWsWindowRedraw::ConstructL();
       
   862 	if (Screen()->ChangeTracking())
       
   863 		{
       
   864 		STACK_REGION dirtyRegion;
       
   865 		dirtyRegion.Copy(iWsWin->WindowArea());
       
   866 		dirtyRegion.Offset(-iWsWin->Origin());
       
   867 		iWsWin->AddDirtyWindowRegion(dirtyRegion);
       
   868 		dirtyRegion.Close();
       
   869 		}
       
   870 	}
       
   871 
       
   872 void CWsBlankWindow::SetColor(TRgb aColor)
       
   873 	{
       
   874 	iColor=aColor;
       
   875 	iNoColor=EFalse;
       
   876 	if (Screen()->ChangeTracking())
       
   877 		{
       
   878 		STACK_REGION dirtyRegion;
       
   879 		dirtyRegion.Copy(iWsWin->WindowArea());
       
   880 		dirtyRegion.Offset(-iWsWin->Origin());
       
   881 		iWsWin->AddDirtyWindowRegion(dirtyRegion);
       
   882 		dirtyRegion.Close();
       
   883 
       
   884 		if (iWsWin->IsActive() && iWsWin->IsVisible())
       
   885 			{
       
   886 			Screen()->ScheduleWindow(iWsWin);
       
   887 			}
       
   888 		}
       
   889 	else
       
   890 		{
       
   891 		Screen()->AddRedrawRegion(iWsWin->VisibleRegion());
       
   892 		}
       
   893 	}
       
   894 
       
   895 TBool CWsBlankWindow::CommandL(TInt aOpcode, TWsWinCmdUnion &aCmd)
       
   896 	{
       
   897 	switch(aOpcode)
       
   898 		{
       
   899 		case EWsWinOpSetBackgroundSurface:
       
   900 			SetBackgroundSurfaceL(*aCmd.Surface);
       
   901 			break;
       
   902 		case EWsWinOpSetBackgroundSurfaceConfig:
       
   903 			SetBackgroundSurfaceL(aCmd.SurfaceConfigurationAndTrigger->surfaceConfig, aCmd.SurfaceConfigurationAndTrigger->triggerRedraw, EFalse);
       
   904 			break;
       
   905 		case EWsWinOpRemoveBackgroundSurface:
       
   906 			RemoveBackgroundSurface(*aCmd.Bool);
       
   907 			break;
       
   908 		case EWsWinOpGetBackgroundSurfaceConfig:
       
   909 			{
       
   910 			TSurfaceConfiguration tempConfiguration = *aCmd.SurfaceConfiguration;
       
   911 			GetBackgroundSurfaceL(tempConfiguration);
       
   912 			TInt tempSize = aCmd.SurfaceConfiguration->Size();
       
   913 			if (sizeof(TSurfaceConfiguration)<tempSize)
       
   914 				tempSize = sizeof(TSurfaceConfiguration);
       
   915 			CWsClient::ReplyBuf(&tempConfiguration,tempSize);
       
   916 			}
       
   917 			break;
       
   918 		case EWsWinOpSetColor:
       
   919 			SetColor(*aCmd.rgb);
       
   920 			break;
       
   921 		case EWsWinOpSetNoBackgroundColor:
       
   922 			SetBackgroundClear();
       
   923 			break;
       
   924 		default:
       
   925 			return(EFalse);
       
   926 		}
       
   927 	return(ETrue);
       
   928 	}
       
   929 
       
   930 TRgb CWsBlankWindow::BackColor() const
       
   931 	{
       
   932 	return(iColor);
       
   933 	}
       
   934 
       
   935 TBool CWsBlankWindow::GetRedrawRect(TRect &) const
       
   936 	{
       
   937 	if (!iNoColor || iWsWin->iAnimList)
       
   938 		iWsWin->Screen()->AddRedrawRegion(iWsWin->VisibleRegion());
       
   939 	return(EFalse);
       
   940 	}
       
   941 
       
   942 TBool CWsBlankWindow::NeedsRedraw() const
       
   943 	{
       
   944 	return(EFalse);
       
   945 	}
       
   946 
       
   947 void CWsBlankWindow::DrawWindow()
       
   948 	{
       
   949 	if ((!iNoColor)||HasElement())
       
   950 		{
       
   951 		DrawBackgroundColor(*iRedrawRegion,!iNoColor);
       
   952 		}
       
   953 	}