windowing/windowserver/test/t_stress/src/animatedwin.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 "animatedwin.h"
       
    23 #include "utils.h"
       
    24 
       
    25 #include "stressanimcmd.h"
       
    26 
       
    27 #include <hal.h>
       
    28 
       
    29 
       
    30 //static configuration data, definitions and default assignments
       
    31 TBool CAnimatedWin::iEnabled = ETrue;
       
    32 TBool CAnimatedWin::iTransparent = ETrue;
       
    33 TBool CAnimatedWin::iTransparentForegroundWindow = EFalse;
       
    34 
       
    35 const TInt sInvisibleFrame = 24;	//how much bigger the invisible window is
       
    36 
       
    37 CAnimatedWin* CAnimatedWin::NewLC(RWsSession& aWs, RWindowGroup* aGroup, CCompWin* aParent, CWindowGc& aGc)
       
    38 	{
       
    39 	CAnimatedWin* self = new (ELeave) CAnimatedWin( aWs, aGroup, aParent, aGc );
       
    40 	CleanupStack::PushL( self );
       
    41 	self->ConstructL();
       
    42 	
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 void CAnimatedWin::LoadConfiguration(const MTestStepConfigurationContext* aContext)
       
    47 	{
       
    48 	aContext->GetBool(KT_WservStressParamEnabled, iEnabled);
       
    49 	aContext->GetBool(KT_WservStressParamTransparent, iTransparent);
       
    50 	aContext->GetBool(KT_WservStressParamTransparentForegroundWindow, iTransparentForegroundWindow);
       
    51 	}
       
    52 
       
    53 CAnimatedWin::~CAnimatedWin()
       
    54 	{ 
       
    55 	if (iForeWin)
       
    56 		{
       
    57 		iForeWin->Close();
       
    58 		delete iForeWin;
       
    59 		}
       
    60 	
       
    61 	if ( iAnim )
       
    62 		{
       
    63 		iAnim->Destroy();
       
    64 		}
       
    65 	iAnimDll.Close();
       
    66 	
       
    67 	TInt idx;
       
    68 	for ( idx = 0; idx < ENofFrames; idx++)
       
    69 		{
       
    70 		delete iAnimCntDevice [idx];
       
    71 		delete iAnimContent [idx]; 
       
    72 		}
       
    73 	
       
    74 	delete iAnimMskDevice;
       
    75 	delete iAnimMask;
       
    76 	}
       
    77 
       
    78 	
       
    79 void CAnimatedWin::Redraw(const TRect& aRect)
       
    80 	{
       
    81 	iWsGc.Activate(*iWindow);
       
    82 	
       
    83 	iRedrawWindow->BeginRedraw( aRect );
       
    84 	
       
    85 	iWsGc.Reset();
       
    86 
       
    87 	iWsGc.SetPenStyle( CGraphicsContext::ESolidPen );
       
    88 	iWsGc.SetBrushStyle( CGraphicsContext::EDiamondCrossHatchBrush );
       
    89 	iWsGc.SetBrushColor( iBgColor );
       
    90 	iWsGc.SetPenColor( iBgColor );
       
    91 	
       
    92 	TRect drawRect( TPoint(0,0), iSize );
       
    93 	
       
    94 	iWsGc.DrawRect( drawRect );
       
    95 	
       
    96 	iRedrawWindow->EndRedraw();
       
    97 	
       
    98 	iWsGc.Deactivate();	
       
    99 
       
   100 	}
       
   101 
       
   102 void CAnimatedWin::AppendTime(TDes& aTimeText,const TTime& aTime) const
       
   103 {
       
   104 	_LIT(TimeFormat,"%:0%H%:1%T%:2%S");
       
   105 	TRAPD(err,aTime.FormatL(aTimeText,TimeFormat));
       
   106 	if (err!=KErrNone)
       
   107 		{
       
   108 		_LIT(DummyTime,"######");
       
   109 		aTimeText.Append(DummyTime);
       
   110 		}
       
   111 }
       
   112 
       
   113 
       
   114 void CAnimatedWin::DrawBitmap(CFbsBitGc* aGc, TRect& aClip, TPoint& aOrigin)
       
   115 	{
       
   116 	aGc->Reset();
       
   117 	TPoint origin = iPos + aOrigin;
       
   118 	aGc->SetOrigin(origin);
       
   119 	TRect clip(origin, iSize);
       
   120 	clip.Intersection(aClip);
       
   121 	clip.Move(-origin);
       
   122 	aGc->SetClippingRect(clip);
       
   123 
       
   124 	// draw win background
       
   125 	aGc->SetPenStyle(CGraphicsContext::ESolidPen);
       
   126 	aGc->SetBrushStyle(CGraphicsContext::EDiamondCrossHatchBrush);
       
   127 	aGc->SetBrushColor( iBgColor );
       
   128 	aGc->SetPenColor( iBgColor );
       
   129 	aGc->DrawRect(TRect(TPoint(0,0), iSize));
       
   130 	
       
   131 	// draw text
       
   132 	TBuf<128> timebuf;
       
   133 	TUint64 elapsedMs = iAnimLastKnownAction * iKernelTicksPeriod;
       
   134 	TTime restoredTime(elapsedMs);
       
   135 	AppendTime( timebuf , restoredTime );
       
   136 		
       
   137 	TInt textWidth = iAnimFont->MeasureText( timebuf );
       
   138 	TInt textHalf = textWidth / 2;
       
   139 	
       
   140 	TInt centerX = iSize.iWidth / 2;
       
   141 	TInt centerY = iSize.iHeight / 2;
       
   142 	
       
   143 	TRect rect(centerX - textHalf, centerY - iAnimFont->AscentInPixels(),
       
   144 			   centerX + textHalf, centerY - iAnimFont->AscentInPixels() + iAnimFont->HeightInPixels());
       
   145 
       
   146 	aGc->Reset();
       
   147 
       
   148 	origin = iPos + aOrigin;
       
   149 	aGc->SetOrigin( origin );
       
   150 	clip = TRect( origin, iSize );
       
   151 	clip.Intersection( aClip );
       
   152 	clip.Move(-origin);
       
   153 	aGc->SetClippingRect(clip);
       
   154 	
       
   155 	aGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   156 	
       
   157 	aGc->UseFont(iAnimFont);
       
   158 	
       
   159 	aGc->DrawText(timebuf, rect, iAnimFont->AscentInPixels());
       
   160 
       
   161 	rect.Move(0, iAnimFont->HeightInPixels());
       
   162 	aGc->DrawText(timebuf, rect, iAnimFont->AscentInPixels());
       
   163 
       
   164 	// inherited
       
   165 	CCompWin::DrawBitmap(aGc, aClip, aOrigin);	
       
   166 	}
       
   167 
       
   168 void CAnimatedWin::SetSize(const TSize & aSize)
       
   169 	{
       
   170 	CCompWin::SetSize( aSize );
       
   171 	if (iForeWin)
       
   172 		{
       
   173 		iForeWin->SetExtent(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame), 
       
   174 				            TSize(iSize.iWidth+2*sInvisibleFrame, iSize.iHeight+2*sInvisibleFrame));
       
   175 		}
       
   176 	}
       
   177 
       
   178 void CAnimatedWin::SetPos(const TPoint & aPos)
       
   179 	{
       
   180 	CCompWin::SetPos( aPos );
       
   181 	if (iForeWin)
       
   182 		{
       
   183 		iForeWin->SetPosition(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame));
       
   184 		}
       
   185 	}
       
   186 
       
   187 CAnimatedWin::CAnimatedWin(RWsSession& aWs, RWindowGroup* aGroup, CCompWin* aParent, CWindowGc& aGc):
       
   188 	CCompWin( aWs, aGroup, aParent, aGc ), iAnimDll( aWs ), iForeWin(NULL)
       
   189 	{
       
   190 	iBgColor 	= TRnd::rnd(); 
       
   191 	
       
   192 	iFrameDuration = (TRnd::rnd( EFrameDurMaxTenthSec ) + 1) * EFrameDurMult;
       
   193 
       
   194 	HAL::Get( HAL::ENanoTickPeriod, iKernelTicksPeriod );	
       
   195 	
       
   196 	}
       
   197 
       
   198 void CAnimatedWin::ConstructL()
       
   199 	{
       
   200 	// prepare surface
       
   201 	CCompWin::PreConstructL(iTransparent);
       
   202 	
       
   203 	TInt idx, err = KErrNone;
       
   204 	TRect rect;
       
   205 
       
   206 	TPoint animPos( iSize.iWidth / 2 - EFrameSzXHalf, 
       
   207 			iSize.iHeight / 2 - EFrameSzYHalf );
       
   208 	
       
   209 	// create anim frames & masks
       
   210 	for ( idx = 0; idx < ENofFrames; idx++)
       
   211 		{
       
   212 		iAnimContent[idx]	= new (ELeave) CFbsBitmap();
       
   213 
       
   214 		err = iAnimContent[idx]->Create( TSize( EFrameSzX , EFrameSzY), EColor64K );
       
   215 		User::LeaveIfError( err );
       
   216 		
       
   217 		iAnimCntDevice [idx] = CFbsBitmapDevice::NewL (iAnimContent[idx]);
       
   218 		}
       
   219 
       
   220 	iAnimMask	= new (ELeave) CFbsBitmap();
       
   221 
       
   222 	err = iAnimMask->Create( TSize( EFrameSzX , EFrameSzY), EColor64K );
       
   223 	User::LeaveIfError( err );
       
   224 	
       
   225 	iAnimMskDevice = CFbsBitmapDevice::NewL ( iAnimMask );
       
   226 	
       
   227 
       
   228 	CGraphicsContext* gCtxAni = NULL;
       
   229 	CGraphicsContext* gCtxMsk = NULL;
       
   230 	// draw frame 1 and mask
       
   231 	err = iAnimCntDevice [ EFrame1 ]->CreateContext( gCtxAni );
       
   232 	User::LeaveIfError( err );
       
   233 	CleanupStack::PushL( gCtxAni );
       
   234 
       
   235 	err = iAnimMskDevice->CreateContext( gCtxMsk );
       
   236 	User::LeaveIfError( err );
       
   237 	CleanupStack::PushL( gCtxMsk );
       
   238 	
       
   239 	
       
   240 	gCtxAni->SetBrushColor(KRgbWhite);
       
   241 	gCtxAni->SetPenColor(KRgbWhite);
       
   242 	gCtxAni->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   243 
       
   244 	gCtxMsk->SetBrushColor(KRgbWhite);
       
   245 	gCtxMsk->SetPenColor(KRgbWhite);
       
   246 	gCtxMsk->SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   247 	
       
   248 	rect.SetRect( EFrameSzXHalf - EFrameSzXEgt, 0, EFrameSzXHalf + EFrameSzXEgt, EFrameSzYHalf ); //lint !e656 Arithmetic operation uses (compatible) enum's 
       
   249 	gCtxAni->DrawRect( rect );
       
   250 	gCtxMsk->DrawRect( rect );
       
   251 
       
   252 	rect.SetRect( EFrameSzXHalf - EFrameSzXQtr, EFrameSzYHalf, EFrameSzXHalf + EFrameSzXQtr, EFrameSzY ); //lint !e656 Arithmetic operation uses (compatible) enum's
       
   253 	gCtxAni->DrawRect( rect );
       
   254 	gCtxMsk->DrawRect( rect );
       
   255 
       
   256 	gCtxAni->SetPenColor(KRgbBlack);
       
   257 	gCtxAni->DrawLine( TPoint(EFrameSzXHalf,0), TPoint (EFrameSzXHalf,EFrameSzY) );
       
   258 	
       
   259 	
       
   260 	CleanupStack::PopAndDestroy( gCtxMsk ); 
       
   261 	CleanupStack::PopAndDestroy( gCtxAni ); 
       
   262 
       
   263 	// draw frame 2
       
   264 	err = iAnimCntDevice [ EFrame2 ]->CreateContext( gCtxAni );
       
   265 	User::LeaveIfError( err );
       
   266 	CleanupStack::PushL( gCtxAni );
       
   267 
       
   268 	gCtxAni->SetPenColor(KRgbWhite);
       
   269 	gCtxAni->DrawLine( TPoint(EFrameSzXHalf,0), TPoint (EFrameSzXHalf,EFrameSzY) );
       
   270 
       
   271 	CleanupStack::PopAndDestroy( gCtxAni ); 
       
   272 	
       
   273 	// init font
       
   274 	err	= iAnimMskDevice->GetNearestFontInPixels( iAnimFont, TFontSpec( _L("Roman"), 16 ) );
       
   275 	User::LeaveIfError( err );
       
   276 
       
   277 	// load anim DLL
       
   278 	err = iAnimDll.Load( KAnimDllName );
       
   279 	User::LeaveIfError( err );
       
   280 	
       
   281 	// construct server side anim
       
   282 	TPckgBuf<TStressTestAnimParams> animParams;
       
   283 	animParams().pos 		= animPos;
       
   284 	animParams().interval = iFrameDuration.Int();
       
   285 	animParams().bit1		= iAnimContent[ EFrame1 ]->Handle();
       
   286 	animParams().bit2		= iAnimContent[ EFrame2 ]->Handle();
       
   287 	animParams().mask		= iAnimMask->Handle();
       
   288 	animParams().font		= iAnimFont->Handle();
       
   289 
       
   290 	iAnim = new (ELeave) CAnimatedWin::RStressAnim ( iAnimDll );
       
   291 	
       
   292 	err = iAnim->Construct( *iWindow, animParams );
       
   293 	User::LeaveIfError( err );
       
   294 
       
   295 	// finally
       
   296 	CCompWin::PostConstructL();	
       
   297 
       
   298 	// got time shot when we started
       
   299 	iAnimStartup = User::NTickCount();
       
   300 
       
   301 	//Create a transparent foreground window, moving and resizing with the animation window
       
   302 	if (iTransparentForegroundWindow)
       
   303 		{
       
   304 		iForeWin = new(ELeave) RBlankWindow(iWs);
       
   305 		iForeWin->Construct(*iGroup,reinterpret_cast<TUint32>(iForeWin));
       
   306 		iForeWin->SetColor(TRgb(0, 0, 0, 0));	//a transparent window
       
   307 		iForeWin->SetExtent(TPoint(iPos.iX-sInvisibleFrame, iPos.iY-sInvisibleFrame), 
       
   308 				            TSize(iSize.iWidth+2*sInvisibleFrame, iSize.iHeight+2*sInvisibleFrame));
       
   309 		iForeWin->SetOrdinalPosition(0);
       
   310 		iForeWin->Activate();
       
   311 		}
       
   312 	
       
   313 	iConstructed = ETrue;
       
   314 	}
       
   315 
       
   316 void CAnimatedWin::SetAnimPos(const TPoint& aPos)
       
   317 	{
       
   318 	TPckgBuf<TStressTestAnimParams> animParams;
       
   319 	animParams().pos = aPos;
       
   320 	iAnim->RequestAnimThis( EADllTextPos, animParams );
       
   321 	}
       
   322 
       
   323 TBool CAnimatedWin::QueryReadyForVerification()
       
   324 	{
       
   325 
       
   326 	TBool res;
       
   327 	
       
   328 	TInt32 curTicks = User::NTickCount();
       
   329 
       
   330 	iAnimLastKnownAction = (TUint32) iAnim->RequestAnimThis( EADllQuerySync ); 
       
   331 	
       
   332 	TUint64 elapsed = TTickUtils::CalcTickDelta( curTicks, iAnimLastKnownAction );
       
   333 
       
   334 	TUint64 elapsedMs = elapsed * iKernelTicksPeriod;
       
   335 	
       
   336 	res = ( elapsedMs < EWatchMatchGap );
       
   337 	
       
   338 	res = res && CCompWin::QueryReadyForVerification();
       
   339 		
       
   340 	return res;
       
   341 	}
       
   342 
       
   343 CAnimatedWin::RStressAnim::RStressAnim (RAnimDll& aAnimDll):
       
   344 	RAnim( aAnimDll )
       
   345 	{ } // empty
       
   346 
       
   347 TInt CAnimatedWin::RStressAnim::Construct(const RWindowBase &aDevice, const TDesC8 &aParams)
       
   348 	{
       
   349 	return RAnim::Construct( aDevice, 0, aParams );
       
   350 	}
       
   351 
       
   352 TInt CAnimatedWin::RStressAnim::RequestAnimThis(TInt aOpcode)
       
   353 	{
       
   354 	TInt res;
       
   355 	
       
   356 	TPckgC<TBool> params(ETrue);
       
   357 	
       
   358 	res = RequestAnimThis( aOpcode, params );
       
   359 	
       
   360 	return res;
       
   361 	}
       
   362 
       
   363 TInt CAnimatedWin::RStressAnim::RequestAnimThis(TInt aOpcode, const TDesC8 &aParams)
       
   364 	{
       
   365 	TInt res;
       
   366 	
       
   367 	res = CommandReply( aOpcode, aParams );
       
   368 	
       
   369 	return res;	
       
   370 	}
       
   371 
       
   372 void CAnimatedWin::DumpDetails(RFile& aFile, TInt aDepth)
       
   373 	{
       
   374 	TBuf8<256> buf;
       
   375 	buf.SetLength(0);
       
   376 	for (TInt d = 0; d < aDepth; ++d)
       
   377 		{
       
   378 		buf.Append(_L8("  "));
       
   379 		}
       
   380 	buf.Append(_L8("Transparent = ["));
       
   381 	buf.AppendNum((TInt64)iTransparent);
       
   382 	buf.Append(_L8("] transparent_foreground_window = ["));
       
   383 	buf.AppendNum((TInt64)iTransparentForegroundWindow);
       
   384 	buf.Append(_L8("] BgColor = ["));
       
   385 	buf.AppendNum(iBgColor.Value());
       
   386 	buf.Append(_L8("]\r\n"));
       
   387 	aFile.Write(buf);
       
   388 	}