windowing/windowserver/test/t_stress/src/spritewin.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2008-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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent
       
    20 */
       
    21 
       
    22 #include "spritewin.h"
       
    23 #include "utils.h"
       
    24 
       
    25 #include <hal.h>
       
    26 
       
    27 
       
    28 //common configuration parameter names
       
    29 _LIT(KT_WservStressParamRandomizeDrawMode, "randomize_draw_mode");
       
    30 _LIT(KT_WservStressParamMaxRandomStepX, "max_random_step_x");
       
    31 _LIT(KT_WservStressParamMaxRandomStepY, "max_random_step_y");
       
    32 _LIT(KT_WservStressParamMaxRandomOffsetX, "max_random_offset_x");
       
    33 _LIT(KT_WservStressParamMaxRandomOffsetY, "max_random_offset_y");
       
    34 
       
    35 //static configuration data, definitions and default assignments
       
    36 TBool CSpritedWin::iEnabled = ETrue;
       
    37 TBool CSpritedWin::iTransparent = ETrue;
       
    38 TBool CSpritedWin::iRandomizePenStyle = EFalse;
       
    39 TBool CSpritedWin::iRandomizeBrushStyle = EFalse;
       
    40 TBool CSpritedWin::iRandomizeDrawMode = EFalse;
       
    41 TBool CSpritedWin::iTransparentForegroundWindow = EFalse;
       
    42 TInt CSpritedWin::iMaxRandomStepX = 0;
       
    43 TInt CSpritedWin::iMaxRandomStepY = 0;
       
    44 TInt CSpritedWin::iMaxRandomOffsetX = 0;
       
    45 TInt CSpritedWin::iMaxRandomOffsetY = 0;
       
    46 
       
    47 const TInt sInvisibleFrame = 24;	//how much bigger the invisible window is
       
    48 
       
    49 
       
    50 CSpritedWin* CSpritedWin::NewLC(RWsSession& aWs, RWindowGroup* aGroup, CCompWin* aParent, CWindowGc& aGc)
       
    51 	{
       
    52 	CSpritedWin* self = new (ELeave) CSpritedWin( aWs, aGroup, aParent, aGc );
       
    53 	CleanupStack::PushL( self );
       
    54 	self->ConstructL();
       
    55 	
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 void CSpritedWin::LoadConfiguration(const MTestStepConfigurationContext* aContext)
       
    60 	{
       
    61 	aContext->GetBool(KT_WservStressParamEnabled, iEnabled);
       
    62 	aContext->GetBool(KT_WservStressParamTransparent, iTransparent);
       
    63 	aContext->GetBool(KT_WservStressParamRandomizePenStyle, iRandomizePenStyle);
       
    64 	aContext->GetBool(KT_WservStressParamRandomizeBrushStyle, iRandomizeBrushStyle);
       
    65 	aContext->GetBool(KT_WservStressParamRandomizeDrawMode, iRandomizeDrawMode);
       
    66 	aContext->GetBool(KT_WservStressParamTransparentForegroundWindow, iTransparentForegroundWindow);
       
    67 
       
    68 	aContext->GetInt(KT_WservStressParamMaxRandomStepX, iMaxRandomStepX);
       
    69 	aContext->GetInt(KT_WservStressParamMaxRandomStepY, iMaxRandomStepY);
       
    70 	aContext->GetInt(KT_WservStressParamMaxRandomOffsetX, iMaxRandomOffsetX);
       
    71 	aContext->GetInt(KT_WservStressParamMaxRandomOffsetY, iMaxRandomOffsetY);
       
    72 	}
       
    73 
       
    74 CSpritedWin::~CSpritedWin()
       
    75 	{ 
       
    76 	if (iForeWin)
       
    77 		{
       
    78 		iForeWin->Close();
       
    79 		delete iForeWin;
       
    80 		}
       
    81 
       
    82 	iSprite.Close();
       
    83 	
       
    84 	TInt idx;
       
    85 	for ( idx = 0; idx < ENofSlides; idx++)
       
    86 		{
       
    87 		delete iSpriteCntDevice [idx];
       
    88 		delete iSpriteMskDevice [idx]; 
       
    89 		
       
    90 		delete iSpriteContent[idx];
       
    91 		delete iSpriteMask[idx];
       
    92 		}
       
    93 	}
       
    94 	
       
    95 void CSpritedWin::Redraw(const TRect& aRect)
       
    96 	{
       
    97 	if (iSpriteStartup == 0)
       
    98 		{
       
    99 		//first time getting here should be very close to the sprite activation time
       
   100 		iSpriteStartup = User::NTickCount();
       
   101 		}
       
   102 	iWsGc.Activate(*iWindow);
       
   103 
       
   104 	iRedrawWindow->BeginRedraw( aRect );
       
   105 
       
   106 	iWsGc.Reset();
       
   107 
       
   108 	//draw rectangle filling window area
       
   109 	iWsGc.SetPenStyle( iPenStyle );
       
   110 	iWsGc.SetBrushStyle( iBrushStyle );
       
   111 	iWsGc.SetBrushColor( iBgColor );
       
   112 	iWsGc.SetPenColor( iBgColor );
       
   113 	iWsGc.SetDrawMode(iDrawMode);
       
   114 	
       
   115 	TRect drawRect( TPoint(0,0), iSize );
       
   116 	
       
   117 	iWsGc.DrawRect( drawRect );
       
   118 	
       
   119 	iRedrawWindow->EndRedraw();
       
   120 	
       
   121 	iWsGc.Deactivate();	
       
   122 	}
       
   123 
       
   124 void CSpritedWin::DrawBitmap(CFbsBitGc* aGc, TRect& aClip, TPoint& aOrigin)
       
   125 	{
       
   126 	if (iSpriteStartup == 0)
       
   127 		{
       
   128 		//although bitmap drawing requested, sprite was not yet rendered - skip
       
   129 		return;
       
   130 		}
       
   131 	aGc->Reset();
       
   132 	TPoint origin = iPos + aOrigin;
       
   133 	aGc->SetOrigin(origin);
       
   134 	TRect clip(origin, iSize);
       
   135 	clip.Intersection(aClip);
       
   136 	clip.Move(-origin);
       
   137 	aGc->SetClippingRect(clip);
       
   138 
       
   139 	// draw win
       
   140 	aGc->SetPenStyle(iPenStyle);
       
   141 	aGc->SetBrushStyle(iBrushStyle);
       
   142 	aGc->SetBrushColor( iBgColor );
       
   143 	aGc->SetPenColor( iBgColor );
       
   144 	aGc->SetDrawMode(iDrawMode);
       
   145 	aGc->DrawRect(TRect(TPoint(0,0), iSize));
       
   146 	
       
   147 	// emulate sprite, cur frame
       
   148 	// time (in ticks) passed since sprite activation
       
   149 	TUint64 delta = TTickUtils::CalcTickDelta( iVerifyTick, iSpriteStartup );
       
   150 	// time for a single slide (in microseconds)
       
   151 	TUint slideDur  = iSlideDuration.Int();
       
   152 	
       
   153 	TInt slideNo = TUint32( ( (delta * iKernelTicksPeriod ) / slideDur ) % ENofSlides );
       
   154 	
       
   155 	TPoint spritePos( iSize.iWidth / 2 - ESpriteSzXHalf, 
       
   156 			iSize.iHeight / 2 - ESpriteSzYHalf );
       
   157 	
       
   158 	TSize spriteSize(ESpriteSzX, ESpriteSzY);
       
   159 	
       
   160 	TRect	spriteRect ( TPoint(0,0), spriteSize );
       
   161 	
       
   162 	TRect   destRect( spritePos, spriteSize );
       
   163 	destRect.Move(iSpriteDef[slideNo].iOffset);
       
   164 	
       
   165 	aGc->DrawBitmapMasked( destRect, 
       
   166 						   iSpriteContent[slideNo], 
       
   167 						   spriteRect, 
       
   168 						   iSpriteMask[slideNo],
       
   169 						   EFalse);
       
   170 	
       
   171 	// inherited
       
   172 	CCompWin::DrawBitmap(aGc, aClip, aOrigin);	
       
   173 	}
       
   174 
       
   175 void CSpritedWin::ClearBitmapBackground(CFbsBitGc* aGc, TRect& aClip, TPoint& aOrigin)
       
   176 	{
       
   177 	if (iSpriteStartup == 0)
       
   178 		{
       
   179 		//although bitmap drawing requested, sprite was not yet rendered - skip
       
   180 		return;
       
   181 		}
       
   182 	CCompWin::ClearBitmapBackground(aGc, aClip, aOrigin);
       
   183 	}
       
   184 
       
   185 void CSpritedWin::SetSize(const TSize & aSize)
       
   186 	{
       
   187 	CCompWin::SetSize( aSize );
       
   188 
       
   189 	if ( iConstructed )
       
   190 		{		
       
   191 		TPoint spritePos( iSize.iWidth / 2 - ESpriteSzXHalf, 
       
   192 				iSize.iHeight / 2 - ESpriteSzYHalf );
       
   193 		
       
   194 		iSprite.SetPosition( spritePos );
       
   195 		}
       
   196 	if (iForeWin)
       
   197 		{
       
   198 		iForeWin->SetExtent(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame), 
       
   199 	            TSize(iSize.iWidth+2*sInvisibleFrame, iSize.iHeight+2*sInvisibleFrame));
       
   200 		}
       
   201 	}
       
   202 
       
   203 void CSpritedWin::SetPos(const TPoint & aPos)
       
   204 	{
       
   205 	CCompWin::SetPos( aPos );
       
   206 	if (iForeWin)
       
   207 		{
       
   208 		iForeWin->SetPosition(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame));
       
   209 		}
       
   210 	}
       
   211 
       
   212 CSpritedWin::CSpritedWin(RWsSession& aWs, RWindowGroup* aGroup, CCompWin* aParent, CWindowGc& aGc):
       
   213 	CCompWin( aWs, aGroup, aParent, aGc ), iSprite ( aWs ), iForeWin(NULL)
       
   214 	{
       
   215 	iBgColor 	= TRnd::rnd();
       
   216 	if (!iTransparent)
       
   217 		{
       
   218 		iBgColor.SetAlpha(255);
       
   219 		}
       
   220 	iSlideDuration = (TRnd::rnd( ESlideDurMaxTenthSec ) + 1) * ESlideDurMult;
       
   221 
       
   222 	HAL::Get( HAL::ENanoTickPeriod, iKernelTicksPeriod );	
       
   223 	
       
   224 	}
       
   225 
       
   226 void CSpritedWin::ConstructL()
       
   227 	{
       
   228 	// prepare surface
       
   229 	CCompWin::PreConstructL(iTransparent);
       
   230 	
       
   231 	TInt idx, err = KErrNone;
       
   232 	TRect rect;
       
   233 	CGraphicsContext* gCtxSpr = NULL;
       
   234 	CGraphicsContext* gCtxMsk = NULL;
       
   235 
       
   236 	// init sprite handles
       
   237 	TPoint spritePos( iSize.iWidth / 2 - ESpriteSzXHalf, 
       
   238 			iSize.iHeight / 2 - ESpriteSzYHalf );
       
   239 	err = iSprite.Construct( *iWindow, spritePos, ESpriteNoChildClip  );
       
   240 	User::LeaveIfError( err );
       
   241 	
       
   242 	iPenStyle = iRandomizePenStyle ? GetRandomPenStyle() : CGraphicsContext::ESolidPen;
       
   243 	iBrushStyle = iRandomizeBrushStyle ? GetRandomBrushStyle() : CGraphicsContext::ESolidBrush;
       
   244 	iDrawMode = iRandomizeDrawMode ? GetRandomDrawMode() : CGraphicsContext::EDrawModePEN;
       
   245 	
       
   246 	TInt stepx = iMaxRandomStepX > 0 ? TRnd::rnd(2*iMaxRandomStepX) - iMaxRandomStepX : 0;
       
   247 	TInt stepy = iMaxRandomStepY > 0 ? TRnd::rnd(2*iMaxRandomStepY) - iMaxRandomStepY : 0;
       
   248 	TInt offsetx = iMaxRandomOffsetX > 0 ? TRnd::rnd(2*iMaxRandomOffsetX) - iMaxRandomOffsetX : 0;
       
   249 	TInt offsety = iMaxRandomOffsetY > 0 ? TRnd::rnd(2*iMaxRandomOffsetY) - iMaxRandomOffsetY : 0;
       
   250 	
       
   251 	// create sprites & masks
       
   252 	for ( idx = 0; idx < ENofSlides; idx++)
       
   253 		{
       
   254 		iSpriteContent[idx] = new (ELeave) CFbsBitmap();
       
   255 		iSpriteMask[idx]	= new (ELeave) CFbsBitmap();;
       
   256 
       
   257 		err = iSpriteContent[idx]->Create(TSize(ESpriteSzX,ESpriteSzY), EColor64K);
       
   258 		User::LeaveIfError( err );
       
   259 		
       
   260 		err = iSpriteMask[idx]->Create(TSize(ESpriteSzX,ESpriteSzY), EColor64K);
       
   261 		User::LeaveIfError( err );
       
   262 		
       
   263 		iSpriteCntDevice [idx] = CFbsBitmapDevice::NewL (iSpriteContent[idx]);
       
   264 		iSpriteMskDevice [idx] = CFbsBitmapDevice::NewL (iSpriteMask[idx]);
       
   265 		
       
   266 	
       
   267 		// draw sprite content
       
   268 		err = iSpriteCntDevice[idx]->CreateContext( gCtxSpr );
       
   269 		User::LeaveIfError( err );
       
   270 		CleanupStack::PushL( gCtxSpr );
       
   271 
       
   272 		err = iSpriteMskDevice[idx]->CreateContext( gCtxMsk );
       
   273 		User::LeaveIfError( err );
       
   274 		CleanupStack::PushL( gCtxMsk );
       
   275 
       
   276 		gCtxSpr->SetPenStyle(CGraphicsContext::ESolidPen);
       
   277 		gCtxSpr->SetPenColor(KRgbWhite);
       
   278 		gCtxSpr->SetBrushColor(KRgbWhite);
       
   279 		gCtxSpr->SetBrushStyle( iBrushStyle );
       
   280 
       
   281 		gCtxMsk->SetPenStyle(CGraphicsContext::ESolidPen);
       
   282 		gCtxMsk->SetPenColor(KRgbBlack);
       
   283 		gCtxMsk->SetBrushColor(KRgbBlack);
       
   284 		gCtxMsk->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   285 		
       
   286 		rect.SetRect( 0, 0, ESpriteSzX, ESpriteSzY );
       
   287 		gCtxSpr->DrawRect( rect );
       
   288 		gCtxMsk->DrawRect( rect );
       
   289 
       
   290 		// cross mask
       
   291 		gCtxMsk->SetBrushColor(KRgbWhite);
       
   292 		gCtxMsk->SetPenColor(KRgbWhite);
       
   293 		rect.SetRect( ESpriteSzXHalf - EStepWidth * (idx + 1), 0, 
       
   294 					  ESpriteSzXHalf + EStepWidth * (idx + 1), ESpriteSzY );
       
   295 		gCtxMsk->DrawRect( rect );
       
   296 		rect.SetRect( 0, ESpriteSzYHalf - EStepHeight * (idx + 1),
       
   297 				      ESpriteSzX, ESpriteSzYHalf + EStepHeight * (idx + 1) );
       
   298 		gCtxMsk->DrawRect( rect );
       
   299 
       
   300 		CleanupStack::PopAndDestroy( gCtxMsk );		
       
   301 		CleanupStack::PopAndDestroy( gCtxSpr );		
       
   302 		
       
   303 		// make sprite
       
   304 		iSpriteDef[idx].iBitmap 		= iSpriteContent[idx];
       
   305 		iSpriteDef[idx].iMaskBitmap	= iSpriteMask[idx];
       
   306 		iSpriteDef[idx].iOffset		= TPoint( offsetx+idx*stepx, offsety+idx*stepy );
       
   307 		iSpriteDef[idx].iDrawMode		= CGraphicsContext::EDrawModeAND;
       
   308 		iSpriteDef[idx].iInvertMask	= EFalse;
       
   309 		iSpriteDef[idx].iInterval		= iSlideDuration;
       
   310 		
       
   311 		err = iSprite.AppendMember( iSpriteDef[idx] );
       
   312 		User::LeaveIfError( err );
       
   313 		}
       
   314 
       
   315 	//Create a transparent foreground window, moving and resizing with the sprite window
       
   316 	if (iTransparentForegroundWindow)
       
   317 		{
       
   318 		iForeWin = new(ELeave) RBlankWindow(iWs);
       
   319 		iForeWin->Construct(*iGroup,reinterpret_cast<TUint32>(iForeWin));
       
   320 		iForeWin->SetColor(TRgb(0, 0, 0, 0));	//a transparent window
       
   321 		iForeWin->SetExtent(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame), 
       
   322 				            TSize(iSize.iWidth+2*sInvisibleFrame, iSize.iHeight+2*sInvisibleFrame));
       
   323 		iForeWin->SetOrdinalPosition(0);
       
   324 		iForeWin->Activate();
       
   325 		}
       
   326 	
       
   327 	// finally
       
   328 	CCompWin::PostConstructL();	
       
   329 
       
   330 	iSpriteStartup = 0;
       
   331 	// show up the sprite
       
   332 	err = iSprite.Activate();
       
   333 	User::LeaveIfError( err );
       
   334 	
       
   335 	iConstructed = ETrue;
       
   336 	}
       
   337 
       
   338 TBool CSpritedWin::QueryReadyForVerification()
       
   339 	{
       
   340 
       
   341 	TBool res;
       
   342 
       
   343 	// time (in ticks) passed since sprite activation
       
   344 	TUint64 delta = TTickUtils::CalcTickDelta( iVerifyTick, iSpriteStartup );
       
   345 	// time for a single slide (in microseconds)
       
   346 	TUint slideDur  = iSlideDuration.Int();
       
   347 	// time for all sprite slides (in microseconds)
       
   348 	TUint spriteDur = slideDur * ENofSlides;
       
   349 	// time passed since begining of sprite sequence (in microseconds)
       
   350 	TUint64 sense = (delta * iKernelTicksPeriod ) % spriteDur; // microsec
       
   351 	
       
   352 	res = (sense >= EFrameBorderSenseMs);
       
   353 	
       
   354 	res = res && CCompWin::QueryReadyForVerification();
       
   355 		
       
   356 	return res;
       
   357 	}
       
   358 
       
   359 void CSpritedWin::DumpDetails(RFile& aFile, TInt aDepth)
       
   360 	{
       
   361 	TBuf8<256> buf;
       
   362 	buf.SetLength(0);
       
   363 	for (TInt d = 0; d < aDepth; ++d)
       
   364 		{
       
   365 		buf.Append(_L8("  "));
       
   366 		}
       
   367 	buf.Append(_L8("Transparent = ["));
       
   368 	buf.AppendNum((TInt64)iTransparent);
       
   369 	buf.Append(_L8("] pen_style = ["));
       
   370 	buf.AppendNum((TInt64)iPenStyle);
       
   371 	buf.Append(_L8("] brush_style = ["));
       
   372 	buf.AppendNum((TInt64)iBrushStyle);
       
   373 	buf.Append(_L8("] draw_mode = ["));
       
   374 	buf.AppendNum((TInt64)iDrawMode);
       
   375 	buf.Append(_L8("] transparent_foreground_window = ["));
       
   376 	buf.AppendNum((TInt64)iTransparentForegroundWindow);
       
   377 	buf.Append(_L8("] max_random_step_x = ["));
       
   378 	buf.AppendNum((TInt64)iMaxRandomStepX);
       
   379 	buf.Append(_L8("] max_random_step_y = ["));
       
   380 	buf.AppendNum((TInt64)iMaxRandomStepY);
       
   381 	buf.Append(_L8("] max_random_offset_x = ["));
       
   382 	buf.AppendNum((TInt64)iMaxRandomOffsetX);
       
   383 	buf.Append(_L8("] max_random_offset_y = ["));
       
   384 	buf.AppendNum((TInt64)iMaxRandomOffsetY);
       
   385 	buf.Append(_L8("]\r\n"));
       
   386 	aFile.Write(buf);
       
   387 	}