os/graphics/graphicscomposition/surfaceupdate/tsrc/tcompositionbackend.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicscomposition/surfaceupdate/tsrc/tcompositionbackend.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,397 @@
     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 +/**
    1.20 + @file
    1.21 + @test
    1.22 + @internalComponent - Internal Symbian test code 
    1.23 +*/
    1.24 +
    1.25 +#include <e32std.h>
    1.26 +#include "tcompositionbackend.h"
    1.27 +#include <graphics/suerror.h>
    1.28 +
    1.29 +const TInt KNotificationsAtTime = 10; //how many notifications could be processed at a time, varies from 1...
    1.30 +
    1.31 +CTContentUpdateReceiver::CTContentUpdateReceiver(TInt aScreen) :
    1.32 +	iScreen(aScreen), iVisible(ETrue)
    1.33 +	{
    1.34 +	RThread thread;
    1.35 +	iReceiverThreadId = thread.Id();
    1.36 +	}
    1.37 +
    1.38 +CTContentUpdateReceiver::~CTContentUpdateReceiver()
    1.39 +	{
    1.40 +	if(iPeriodic)
    1.41 +		iPeriodic->Cancel();
    1.42 +	delete iPeriodic;
    1.43 +	iLock.Close();
    1.44 +	iPriorityLock.Close();
    1.45 +	}
    1.46 +
    1.47 +void CTContentUpdateReceiver::ConstructL()
    1.48 +	{
    1.49 +	TCallBack callback(CallBack);
    1.50 +	callback.iPtr = this;
    1.51 +	User::LeaveIfError(iLock.CreateLocal());
    1.52 +	User::LeaveIfError(iPriorityLock.CreateLocal(0));
    1.53 +    iPeriodic= CPeriodic::NewL(CPeriodic::EPriorityStandard);
    1.54 +    iPeriodic->Start(TTimeIntervalMicroSeconds32(0),TTimeIntervalMicroSeconds32(KCompositionInterval), callback);
    1.55 +	}
    1.56 +
    1.57 +CTContentUpdateReceiver* CTContentUpdateReceiver::NewL(TInt aScreen)
    1.58 +	{
    1.59 +	CTContentUpdateReceiver* receiver = new (ELeave) CTContentUpdateReceiver(aScreen);
    1.60 +	CleanupStack::PushL(receiver);
    1.61 +	receiver->ConstructL();
    1.62 +	CleanupStack::Pop();
    1.63 +	return receiver;  
    1.64 +	}
    1.65 +
    1.66 +TInt    CTContentUpdateReceiver::Extension_(TUint aExtensionId, TAny*& aRetIface, TAny* a1)
    1.67 +    {
    1.68 +    switch (aExtensionId)
    1.69 +        {
    1.70 +        case MCompositionSurfaceUpdate::ETypeId:
    1.71 +            aRetIface= static_cast<MCompositionSurfaceUpdate*>(this);
    1.72 +            return KErrNone;
    1.73 +
    1.74 +        default:    ;
    1.75 +        }
    1.76 +    return CExtensionContainer::Extension_(aExtensionId,aRetIface,a1);
    1.77 +    }
    1.78 +
    1.79 +TInt CTContentUpdateReceiver::CallBack(TAny *aAny)
    1.80 +	{
    1.81 +	return (static_cast <CTContentUpdateReceiver*> (aAny))->CheckNewNotifications();
    1.82 +	}
    1.83 +
    1.84 +void CTContentUpdateReceiver::Stop()
    1.85 +	{
    1.86 +    iLock.Wait();
    1.87 +	iStop = ETrue;
    1.88 +    iLock.Signal();
    1.89 +	}
    1.90 +
    1.91 +EXPORT_C void CTContentUpdateReceiver::SetVisible(TBool aVisible)
    1.92 +    {
    1.93 +    iLock.Wait();
    1.94 +    iVisible = aVisible;
    1.95 +    iLock.Signal();
    1.96 +    }
    1.97 +
    1.98 +TInt CTContentUpdateReceiver::CheckNewNotifications()
    1.99 +	{
   1.100 +	iLock.Wait();
   1.101 + 	if(iStop && (iNumberElements <= 0))
   1.102 +		{
   1.103 +	    iLock.Signal();
   1.104 +		CActiveScheduler::Stop();
   1.105 +		return 0;//the return value is irrelevant for CPeriodic function
   1.106 +		}
   1.107 +	if(iSetInternalPriority)
   1.108 +		{
   1.109 +		TRAPD(res, DoSetInternalPriorityL());
   1.110 +	    iLock.Signal();
   1.111 +		__ASSERT_ALWAYS(res ==KErrNone, User::Panic(_L("CheckNewNotifications"), KErrGeneral));
   1.112 +		return 0;//the return value is irrelevant for CPeriodic function
   1.113 +		}	
   1.114 +	TInt index = 0;	
   1.115 +	RThread thread;
   1.116 +	TInt res = thread.Open(iThreadId);
   1.117 +	__ASSERT_ALWAYS(res ==KErrNone, User::Panic(_L("CheckNewNotifications"), KErrGeneral));
   1.118 +
   1.119 +		//we will check only one limited amount of requests at the time
   1.120 +	for(TInt iteration = 0; (iNumberElements > index) && (iteration < KNotificationsAtTime); iteration++)
   1.121 +		{
   1.122 +		if(iArray[index].iType == EReqDisplayed)
   1.123 +			{
   1.124 +			*(iArray[index].iTimeStamp) = User::FastCounter();
   1.125 +			if(iCompositionOrder)
   1.126 +				{
   1.127 +				iCompositionOrder->SetOrder(EOrderComposition);
   1.128 +				}
   1.129 +			}
   1.130 +		else if(iArray[index].iType == EReqDisplayedXTimes)
   1.131 +			{
   1.132 +			iArray[index].iDisplayedXTimes--;
   1.133 +			if(iArray[index].iDisplayedXTimes > 0)
   1.134 +				{
   1.135 +				index++;
   1.136 +				continue;
   1.137 +				}
   1.138 +			}
   1.139 +		TRequestStatus* status = iArray[index].iStatus;
   1.140 +		res = iVisible ? KErrNone : KErrNotVisible;
   1.141 +		Remove(index);
   1.142 +		thread.RequestComplete(status, res);
   1.143 +		}
   1.144 +	thread.Close();	
   1.145 +    iLock.Signal();
   1.146 +	return 0;//the return value is irrelevant for CPeriodic function
   1.147 +	}
   1.148 +
   1.149 +void CTContentUpdateReceiver::DoSetInternalPriorityL()
   1.150 +	{
   1.151 +	RThread thread;
   1.152 +	User::LeaveIfError(thread.Open(iReceiverThreadId));
   1.153 +	thread.SetPriority(iInternalPriority);
   1.154 +	thread.Close();
   1.155 +	TInt compositionInterval = KCompositionInterval;
   1.156 +	CPeriodic::TPriority priority = CPeriodic::EPriorityStandard;
   1.157 +	if(iInternalPriority < EPriorityNormal)
   1.158 +		{
   1.159 +		priority = CPeriodic::EPriorityIdle;
   1.160 +		compositionInterval = KCompositionIntervalLong;
   1.161 +		}
   1.162 +	else if (iInternalPriority > EPriorityNormal)
   1.163 +		{
   1.164 +		priority = CPeriodic::EPriorityHigh;
   1.165 +		compositionInterval = KCompositionIntervalShort;
   1.166 +		}
   1.167 +
   1.168 +	TCallBack callback(CallBack);
   1.169 +	callback.iPtr = this;
   1.170 +	iPeriodic->Cancel();
   1.171 +	delete iPeriodic;
   1.172 +	iPeriodic= CPeriodic::NewL(priority);
   1.173 +	iPeriodic->Start(TTimeIntervalMicroSeconds32(compositionInterval),TTimeIntervalMicroSeconds32(compositionInterval), callback);
   1.174 +	iSetInternalPriority = EFalse;
   1.175 +	iPriorityLock.Signal();
   1.176 +	}
   1.177 +
   1.178 +EXPORT_C TInt CTContentUpdateReceiver::SetInternalPriority(TThreadPriority aInternalPriority)
   1.179 +	{
   1.180 +	iLock.Wait();
   1.181 +	iInternalPriority = aInternalPriority;
   1.182 +	iSetInternalPriority = ETrue;
   1.183 +    iLock.Signal();
   1.184 +	
   1.185 +    //wait for the priority changes takes place
   1.186 +    iPriorityLock.Wait();
   1.187 +	return KErrNone;
   1.188 +	}
   1.189 +
   1.190 +void CTContentUpdateReceiver::ContentUpdated(const TSurfaceId& aId, 
   1.191 +				TInt aBuffer, 
   1.192 +				const TRegion* aRegion, 
   1.193 +				TRequestStatus* aStatusAvailable, 
   1.194 +				TRequestStatus* aStatusDisplayed, TUint32* aTimeStamp, 
   1.195 +				TRequestStatus* aStatusDisplayedXTimes, TInt* aDisplayedXTimes)
   1.196 +	{
   1.197 +	(TAny)&aId;
   1.198 +	(TAny)aBuffer;
   1.199 +	(TAny)aRegion;
   1.200 +	
   1.201 +	iLock.Wait();
   1.202 +	if(iStop)
   1.203 +		{
   1.204 +		if(aStatusAvailable)
   1.205 +			{
   1.206 +			User::RequestComplete(aStatusAvailable, KErrDied);
   1.207 +			}
   1.208 +		if(aStatusDisplayed)
   1.209 +			{
   1.210 +			User::RequestComplete(aStatusDisplayed, KErrDied);
   1.211 +			}
   1.212 +		if(aStatusDisplayedXTimes)
   1.213 +			{
   1.214 +			User::RequestComplete(aStatusDisplayedXTimes, KErrDied);
   1.215 +			}
   1.216 +	    iLock.Signal();
   1.217 +		return;
   1.218 +		}
   1.219 +	
   1.220 +	RThread thread;
   1.221 +	iThreadId = thread.Id();
   1.222 +	
   1.223 +	if(aStatusAvailable)
   1.224 +		{
   1.225 +		Add(aStatusAvailable, EReqAvailable);
   1.226 +		}
   1.227 +	if(aStatusDisplayed)
   1.228 +		{
   1.229 +		Add(aStatusDisplayed, EReqDisplayed, 0, aTimeStamp);
   1.230 +		}
   1.231 +	if(aStatusDisplayedXTimes)
   1.232 +		{
   1.233 +		Add(aStatusDisplayedXTimes, EReqDisplayedXTimes, *aDisplayedXTimes);
   1.234 +		}
   1.235 +    iLock.Signal();
   1.236 +	}
   1.237 +
   1.238 +/** 
   1.239 +   Add notification to the list. The function is called from the SUS thread.
   1.240 +   The client of this API must use a lock mechanizm to preserve data integrity.
   1.241 +*/
   1.242 +TInt CTContentUpdateReceiver::Add(TRequestStatus *aStatus, RequestType aType, 
   1.243 +			TInt aDisplayedXTimes, TUint32* aTimeStamp)
   1.244 +	{
   1.245 + 	TInt index = iNumberElements;
   1.246 +	TInt max = sizeof(iArray) / sizeof(iArray[0]) - 1;
   1.247 +	if(index >= max)
   1.248 +		return KErrOverflow;
   1.249 +	
   1.250 +	iArray[index].iStatus = aStatus;
   1.251 +	iArray[index].iType = aType;
   1.252 +	iArray[index].iDisplayedXTimes = aDisplayedXTimes;
   1.253 +	iArray[index].iTimeStamp = aTimeStamp;
   1.254 +	
   1.255 +	iNumberElements++;
   1.256 +	return KErrNone;
   1.257 +	}
   1.258 +
   1.259 +/** 
   1.260 +   Remove notification from the list.
   1.261 +   The function is called from the backend thread. 
   1.262 +   The client of this API must use a lock mechanizm to preserve data integrity.
   1.263 +*/
   1.264 +void CTContentUpdateReceiver::Remove(TInt aIndex) 
   1.265 +	{
   1.266 +	TInt max = sizeof(iArray) / sizeof(iArray[0]) - 1;
   1.267 +	if((aIndex < 0) || (aIndex >= max))
   1.268 +			return;
   1.269 +	
   1.270 +	iNumberElements--;
   1.271 +	if(aIndex < iNumberElements)
   1.272 +		{
   1.273 +		Mem::Move(&iArray[aIndex], &iArray[aIndex + 1], (iNumberElements - aIndex) * sizeof(RequestObject));
   1.274 +		iArray[iNumberElements].iType= EReqEmpty;
   1.275 +		}
   1.276 +	else
   1.277 +		{
   1.278 +		iArray[aIndex].iType = EReqEmpty;
   1.279 +		}	
   1.280 +	}
   1.281 +	
   1.282 +TInt CTContentUpdateReceiver::ThreadFunction(TAny* aAny)
   1.283 +	{
   1.284 +	  // get clean-up stack
   1.285 +	CTrapCleanup* cleanup=CTrapCleanup::New();
   1.286 +	RThread thread;
   1.287 +	_LIT(KTestReceiver, "TestReceiver");
   1.288 +	__ASSERT_ALWAYS(cleanup!=NULL, thread.Panic( KTestReceiver, KErrNoMemory));
   1.289 +	
   1.290 +	  // create an active scheduler and server
   1.291 +	CActiveScheduler *pA = new CActiveScheduler;
   1.292 +	__ASSERT_ALWAYS(pA != NULL, thread.Panic( KTestReceiver, KErrNoMemory));
   1.293 +
   1.294 +	  //Install the active scheduler
   1.295 +	CActiveScheduler::Install(pA);
   1.296 +
   1.297 +	CTContentUpdateReceiver *pCB = NULL;
   1.298 +	TInt screen = * (static_cast <TInt*> (aAny));
   1.299 +	TRAPD(err, pCB = CTContentUpdateReceiver::NewL(screen));
   1.300 +	__ASSERT_ALWAYS(err == KErrNone, thread.Panic( KTestReceiver, err));
   1.301 +	
   1.302 + 	*(static_cast <CTContentUpdateReceiver**> (aAny)) = pCB;
   1.303 +    
   1.304 +      // Let everyone know that we are ready to
   1.305 +      // deal with requests.
   1.306 +	RThread::Rendezvous(KErrNone);
   1.307 +	  // And start fielding requests from client(s).
   1.308 +	CActiveScheduler::Start();
   1.309 +
   1.310 +     // Tidy up... 	
   1.311 +	delete pCB;
   1.312 +	delete pA;
   1.313 +	delete cleanup; 
   1.314 +	
   1.315 +	return KErrNone;
   1.316 +	}
   1.317 +
   1.318 +_LIT(KMaskBackend, "CompositionBackend_%d");
   1.319 +const TUint KDefaultHeapSize=0x10000;
   1.320 +
   1.321 +EXPORT_C TInt StartTestUpdateReceiver(CTContentUpdateReceiver*& aReceiver, TInt aScreen)
   1.322 +	{
   1.323 +	RThread compositionThread;
   1.324 +	TInt res = KErrGeneral;
   1.325 +	TBuf<64> contentUpdateReceiverThreadName;
   1.326 +	TBuf<64> contentUpdateReceiverThreadMask;
   1.327 +	
   1.328 +	// Guarantee uniqueness of thread name by using timestamp
   1.329 +	TTime tm;
   1.330 +	TBuf<32> timeStamp;
   1.331 +	tm.UniversalTime();
   1.332 +	TRAP(res, tm.FormatL(timeStamp, _L("_%H%T%S%C")));
   1.333 +	if(res != KErrNone)
   1.334 +		{
   1.335 +		return res;
   1.336 +		}
   1.337 +
   1.338 +	contentUpdateReceiverThreadName.Format(KMaskBackend, aScreen);
   1.339 +	contentUpdateReceiverThreadName.Append(timeStamp);
   1.340 +	contentUpdateReceiverThreadMask = contentUpdateReceiverThreadName;
   1.341 +	contentUpdateReceiverThreadMask.Insert(0, _L("*"));
   1.342 +	TFindThread findThread(contentUpdateReceiverThreadMask);
   1.343 +	TFullName name;
   1.344 +	  // Need to check that the thread exists.
   1.345 +	if (findThread.Next(name)!=KErrNone)
   1.346 +		{
   1.347 +		aReceiver = reinterpret_cast <CTContentUpdateReceiver*> (aScreen);
   1.348 +		
   1.349 +		  // Create the thread for the server.
   1.350 +		res = compositionThread.Create(contentUpdateReceiverThreadName,
   1.351 +			CTContentUpdateReceiver::ThreadFunction,
   1.352 +			KDefaultStackSize,
   1.353 +			KDefaultHeapSize,
   1.354 +			KDefaultHeapSize,
   1.355 +			(TAny*) &aReceiver
   1.356 +			);
   1.357 +			
   1.358 +          // The thread has been created OK so get it started - however
   1.359 +          // we need to make sure that it has started before we continue.
   1.360 +		if (res==KErrNone)
   1.361 +			{
   1.362 +			TRequestStatus rendezvousStatus;
   1.363 +			compositionThread.SetPriority(EPriorityNormal);
   1.364 +			compositionThread.Rendezvous(rendezvousStatus);
   1.365 +			compositionThread.Resume();
   1.366 +			User::WaitForRequest(rendezvousStatus);
   1.367 +			res = rendezvousStatus.Int();
   1.368 +			}
   1.369 +		}
   1.370 +		compositionThread.Close();
   1.371 +		return res;
   1.372 +	}
   1.373 +
   1.374 +EXPORT_C void CloseTestUpdateReceiver(CTContentUpdateReceiver* aReceiver)
   1.375 +	{
   1.376 +	if(!aReceiver)
   1.377 +		return;
   1.378 +
   1.379 +	TBuf<64> contentUpdateReceiverThreadName;
   1.380 +	contentUpdateReceiverThreadName.Format(KMaskBackend, aReceiver->Screen());
   1.381 +	TBuf<64> contentUpdateReceiverThreadMask;
   1.382 +	contentUpdateReceiverThreadMask = contentUpdateReceiverThreadName;
   1.383 +	contentUpdateReceiverThreadMask.Insert(0, _L("*"));
   1.384 +	contentUpdateReceiverThreadMask.Append('*');
   1.385 +	TFindThread findThread(contentUpdateReceiverThreadMask);
   1.386 +	TFullName name;
   1.387 +	RThread thread;
   1.388 +	if((findThread.Next(name)!=KErrNone) ||
   1.389 +		(thread.Open(findThread) != KErrNone))
   1.390 +		{
   1.391 +		thread.Close();
   1.392 +		return;
   1.393 +		}
   1.394 +	TRequestStatus status; 
   1.395 +	thread.Logon(status);
   1.396 +	if(aReceiver)
   1.397 +		aReceiver->Stop();
   1.398 +	User::WaitForRequest(status);
   1.399 +	thread.Close();
   1.400 +	}