windowing/windowserver/stdgraphic/TESTGRAPHICDRAWER.CPP
changeset 0 5d03bc08d59c
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 //
       
    15 
       
    16 #include "wsgraphicdrawercontext.h"
       
    17 #include "stdgraphictestdrawer.h"
       
    18 
       
    19 CWsGraphicDrawerTestFrameRate* CWsGraphicDrawerTestFrameRate::CreateL()
       
    20 	{
       
    21 	return new(ELeave) CWsGraphicDrawerTestFrameRate;
       
    22 	}
       
    23 
       
    24 CWsGraphicDrawerTestFrameRate::CWsGraphicDrawerTestFrameRate()
       
    25 	{
       
    26 	}
       
    27 
       
    28 CWsGraphicDrawerTestFrameRate::~CWsGraphicDrawerTestFrameRate()
       
    29 	{
       
    30 	if (iContext)
       
    31 		{
       
    32 		iContext->Destroy();
       
    33 		iContext = NULL;
       
    34 		}
       
    35 	}
       
    36 
       
    37 void CWsGraphicDrawerTestFrameRate::ConstructL(MWsGraphicDrawerEnvironment& aEnv,const TGraphicDrawerId& aId,MWsClient& aOwner,const TDesC8& /*aData*/)
       
    38 	{
       
    39 	BaseConstructL(aEnv,aId,aOwner);
       
    40 	if (!(aEnv.Screen(0)->ResolveObjectInterface(KMWsCompositionContext) || aEnv.Screen(0)->ResolveObjectInterface(KMWsScene)))
       
    41 		{
       
    42 		iContext = CWsGraphicDrawerNonNgaContext::NewL();
       
    43 		}
       
    44 	else
       
    45 		{
       
    46 		iContext = CWsGraphicDrawerNgaContext::NewL();
       
    47 		}
       
    48 	}
       
    49 
       
    50 void CWsGraphicDrawerTestFrameRate::DoDraw(MWsGc& aGc,const TRect& aRect,const TDesC8& /*aData*/) const
       
    51 	{
       
    52 	if (KErrNone != iContext->Push(aGc))
       
    53 		{
       
    54 		return;
       
    55 		}
       
    56 	//Simplification: treats all screens the same
       
    57 	iContext->DrawFrameRate(aGc, aRect, iFps);
       
    58 	//Count this draw		
       
    59 	iFps.Sample();
       
    60 	//Schedule a redraw
       
    61 	iContext->ScheduleAnimation(aGc,aRect,0); // as fast as possible
       
    62 	iContext->Pop(aGc);
       
    63 	}
       
    64 
       
    65 void CWsGraphicDrawerTestFrameRate::HandleMessage(const TDesC8& /*aData*/)
       
    66 	{
       
    67 	}
       
    68 	
       
    69 TInt CFrameRate::Fps() const
       
    70 	{
       
    71 	if(iNumSamples > 1)
       
    72 		{
       
    73 		const TUint count = Min(iNumSamples,KMaxSamples);		
       
    74 		TInt64 earliest = iSamples[0];
       
    75 		TInt64 latest = earliest;
       
    76 		for(TInt i=1; i<count; i++)
       
    77 			{
       
    78 			const TInt64 sample = iSamples[i];
       
    79 			earliest = Min(earliest,sample);
       
    80 			latest = Max(latest,sample);
       
    81 			}
       
    82 		const TInt64 duration = latest - earliest;
       
    83 		const TInt microspf = (duration / count);
       
    84 		if(microspf)
       
    85 			{
       
    86 			const TInt millispf = (microspf / 1000);
       
    87 			if(millispf)
       
    88 				{
       
    89 				const TInt fps = (1000 / millispf);
       
    90 				return fps;
       
    91 				}
       
    92 			}
       
    93 		}
       
    94 	return 0;
       
    95 	}
       
    96 	
       
    97 TInt CFrameRate::CountSamples() const
       
    98 	{
       
    99 	return iNumSamples;
       
   100 	}
       
   101 
       
   102 void CFrameRate::Sample() const
       
   103 	{
       
   104 	//Add the current tick to the stats for next time
       
   105 	TTime now;
       
   106 	now.UniversalTime();
       
   107 	iSamples[iNumSamples % KMaxSamples] = now.Int64();
       
   108 	iNumSamples++;
       
   109 	}