os/graphics/graphicscomposition/openwfsupport/src/streammap.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 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 // streammap.cpp: creates and maintaines the association between native stream and a TSurfaceId
    15 // 
    16 //
    17 
    18 #include "streammap.h"
    19 #include <graphics/updateserverprovider.h>
    20 #include <graphics/surfacemanager.h>
    21 #include <e32property.h>
    22 #include <e32std.h>
    23 #include <e32cmn.h>
    24 #include "openwfcpanic.h"
    25 #include "surfacestream.h"
    26 #include "contentupdateproxy.h"
    27 
    28 static const TInt KOpenWfcInteropCleanupKey = 0x10286FC5;
    29 
    30 COpenWfcStreamMap* COpenWfcStreamMap::pInstance = NULL;
    31 
    32 TUint32 COpenWfcStreamMap::HashFunction(const TSurfaceId& aHashKey)
    33 	{
    34 	TPckgC<TSurfaceId> pckg(aHashKey);
    35 	return DefaultHash::Des8(pckg);
    36 	}
    37 
    38 EXPORT_C COpenWfcStreamMap& COpenWfcStreamMap::InstanceL()
    39 	{
    40 	// would have been nice to try to protect this area with a mutex, however,
    41 	// the preliminary analyses show we can guarantee the safe creation of the mutex
    42 	// by invoking InstanceL() from the place the CGce instances are created
    43 	// If necessary, in future, the code can be expanded by using a named mutex.
    44 	if (!pInstance)
    45 		{
    46 		COpenWfcStreamMap* newInstance = new(ELeave)COpenWfcStreamMap();
    47 		CleanupStack::PushL(newInstance);
    48 		newInstance->ConstructL();
    49 		CleanupStack::Pop(newInstance);
    50 		pInstance=newInstance;
    51 		}
    52 	return *pInstance;
    53 	}
    54 
    55 CSurfaceStream* COpenWfcStreamMap::Find(const TSurfaceId& aSurfaceId)
    56 	{
    57 	Guard g(iMutex);
    58 	CSurfaceStream** ns = iMap.Find(aSurfaceId);
    59 	// used if statement to ease debug help
    60 	if (ns)
    61 		{
    62 		(*ns)->AddReference();
    63 		return *ns;
    64 		}
    65 	return NULL;
    66 	}
    67 
    68 CSurfaceStream* COpenWfcStreamMap::AcquireL(const TSurfaceId& aSurfaceId)
    69 	{
    70 	Guard* pGuard = new(ELeave) Guard(iMutex);
    71 	User::LeaveIfNull(pGuard);
    72 	CleanupDeletePushL(pGuard);
    73 	
    74 	CSurfaceStream** ns = NULL;
    75 	CSurfaceStream* ret = NULL;
    76 	ns = iMap.Find(aSurfaceId);
    77 	if (ns)
    78 		{
    79 		WFCI_ASSERT_DEBUG((*ns), EOwfPanicInvalidHasMap);  // should have never happened
    80 		ret = *ns;
    81 		}
    82 	else
    83 		{
    84 		ret = CSurfaceStream::NewLC(aSurfaceId);
    85 		User::LeaveIfError(iMap.Insert(aSurfaceId, ret));
    86 		CleanupStack::Pop();
    87 		}
    88 	ret->AddReference();
    89 	CleanupStack::PopAndDestroy(pGuard);
    90 	return ret;
    91 	}
    92 
    93 EXPORT_C TInt COpenWfcStreamMap::Count()
    94 	{
    95 	Guard g(iMutex);
    96 	TInt count = iMap.Count();
    97 	return count;
    98 	}
    99 
   100 EXPORT_C RSurfaceManager& COpenWfcStreamMap::SurfaceManager()
   101 	{
   102     WFCI_ASSERT_DEBUG(iSurfaceManager, EOwfPanicInvalidHasMap);
   103 	return *iSurfaceManager;
   104 	}
   105 
   106 TInt COpenWfcStreamMap::LockDestroy(CSurfaceStream* aStream)
   107 	{
   108 	Guard g(iMutex);
   109 	TInt ret = KErrNone;
   110 	if (aStream)
   111 		{
   112 		if (aStream->RemainingReference())
   113 			{
   114 			const TSurfaceId& surfaceId = aStream->SurfaceId();
   115 			CSurfaceStream** ns = iMap.Find(surfaceId);
   116 			// used if statement to ease debug help
   117 			if (ns && ((*ns) == aStream))
   118 				{
   119 				TInt ret = iMap.Remove(surfaceId);
   120 				delete aStream;
   121 				}
   122 			else
   123 				{
   124 				ret = KErrNotFound;
   125 				}
   126 			}
   127 		else	// RemainingReference
   128 			{
   129 			ret = KErrInUse;
   130 			}
   131 		}
   132 	else	// aStream
   133 		{
   134 		ret = KErrArgument;
   135 		}
   136 	return ret;
   137 	}
   138 
   139 COpenWfcStreamMap::COpenWfcStreamMap():
   140 iMap(THashFunction32<TSurfaceId>(COpenWfcStreamMap::HashFunction), TIdentityRelation<TSurfaceId>()),
   141 iSurfaceManager(NULL),
   142 iRegisteredUpdaters()
   143 	{
   144 	}
   145 
   146 TInt COpenWfcStreamMap::DeleteSingleton(TAny* aData)
   147 /**
   148  * aData    A pointer to the heap registered for this callback
   149  */
   150     {
   151     // Blank the property for this callback
   152     RThread t;
   153     RProperty prop;
   154     TCallBack cb(NULL, NULL);
   155     TPckgC<TCallBack> cbPckg(cb);
   156     prop.Set(TUid::Uid(t.SecureId().iId), KOpenWfcInteropCleanupKey, cbPckg);
   157     prop.Close();
   158     t.Close();
   159     
   160     if (aData == &User::Heap())
   161         {
   162         delete pInstance;
   163         pInstance = NULL;                
   164         return ETrue;
   165         }
   166     return EFalse;
   167     }
   168 
   169 COpenWfcStreamMap::~COpenWfcStreamMap()
   170 	{
   171 	iMutex.Wait();
   172         {
   173         THashMapIter<TSurfaceId, CSurfaceStream*> iter(iMap);
   174         const TSurfaceId* nextKey = iter.NextKey();
   175         CSurfaceStream* const* ns = NULL;
   176         while (nextKey)
   177             {
   178             ns = iter.CurrentValue();
   179             if (ns && *ns)
   180                 {
   181                 delete (*ns);
   182                 }
   183             nextKey = iter.NextKey();		
   184             }
   185         }
   186 	iMap.Close();
   187     if (iSurfaceManager)
   188         {
   189         iSurfaceManager->Close();
   190         delete iSurfaceManager;
   191         iSurfaceManager = NULL;
   192         }
   193 	iMutex.Signal();
   194 	iMutex.Close();
   195 	
   196         {
   197         THashMapIter<TInt32, CExtensionContainer*> iter(iRegisteredUpdaters);
   198         const TInt32* nextKey = iter.NextKey();
   199         CExtensionContainer* const* extensionContainer = NULL;
   200         while (nextKey)
   201             {
   202             extensionContainer = iter.CurrentValue();
   203             if (extensionContainer && *extensionContainer)
   204                 {
   205                 delete (*extensionContainer);
   206                 }
   207             nextKey = iter.NextKey();       
   208             }
   209         }
   210 	iRegisteredUpdaters.Close();
   211 	}
   212 
   213 void COpenWfcStreamMap::ConstructL()
   214 	{
   215 	User::LeaveIfError(iMutex.CreateLocal());
   216 	iMap.Reserve(iInitialSize);
   217 	TSurfaceId surface = TSurfaceId::CreateNullId();
   218 	User::LeaveIfError(iMap.Insert(surface, NULL));
   219 
   220 	iSurfaceManager = new(ELeave) RSurfaceManager();
   221 	User::LeaveIfError(iSurfaceManager->Open());
   222 	RProcess process;
   223 	TUidType uidType = process.Type();
   224 	const TInt32 KWservUid = 268450592;
   225 	const TUid& uid1 = uidType[2];
   226 
   227 	if(uid1.iUid == KWservUid) //only wserv process can start the server
   228 		{
   229 		StartSurfaceUpdateServer(iSurfUpdateServ);
   230 		}
   231 	
   232 	// Register the cleanup function in a property defined by WServ
   233 	RThread t;		
   234 	TUid category = {t.SecureId().iId};
   235 	RProperty prop;
   236 	TCallBack cb(DeleteSingleton, &User::Heap());
   237     TPckgC<TCallBack> cbPckg(cb);
   238     
   239     // If the property cannot be set the assumption is that the cleanup is not needed
   240     (void) prop.Set(category, KOpenWfcInteropCleanupKey, cbPckg);
   241     prop.Close();
   242 	t.Close();	
   243 	
   244 	// StreamMap is constructed from main thread
   245 	SetMainHeap();
   246 	}
   247 
   248 COpenWfcStreamMap::Guard::Guard(RFastLock& aLock):
   249 iLock(aLock)
   250 	{
   251 	iLock.Wait();
   252 	}
   253 
   254 COpenWfcStreamMap::Guard::~Guard()
   255 	{
   256 	iLock.Signal();
   257 	}
   258 
   259 EXPORT_C RHeap* COpenWfcStreamMap::GetMainHeap()
   260 	{
   261 	return iMainHeap;
   262 	}
   263 
   264 void COpenWfcStreamMap::SetMainHeap()
   265 	{
   266 	iMainHeap = &User::Heap();
   267 	}
   268 
   269 TInt COpenWfcStreamMap::RegisterScreenNotifications(TInt aScreenNum, TInt aPriority,TInt aInternalVersion)
   270 	{
   271 	COpenWfcStreamMap* pInstance = NULL;
   272 	TRAPD(err,pInstance = &COpenWfcStreamMap::InstanceL());
   273 	if (err != KErrNone)
   274 	    {
   275 	    return err;
   276 	    }
   277 	
   278 	if (iRegisteredUpdaters.Find(aScreenNum)!= NULL)
   279 		{
   280 		return KErrAlreadyExists;
   281 		}
   282 	
   283 	CExtensionContainer* updateProxy = NULL;
   284 	TRAP(err, updateProxy = CContentUpdateProxy::NewL(aScreenNum, pInstance,aInternalVersion,aPriority));
   285 	if (err)
   286 		{
   287 		return err;
   288 		}
   289 	
   290 	if ((err = iRegisteredUpdaters.Insert(aScreenNum, updateProxy)) != KErrNone)
   291 	    {
   292         delete updateProxy;
   293 	    return err;
   294 	    }
   295 
   296     if (!pInstance->iSurfUpdateServ)
   297         {
   298         return KErrNotReady;    //For testing purposes the backend proxy still exists.
   299         }
   300 	err = iSurfUpdateServ->Register(aScreenNum, updateProxy, aPriority);
   301 	if (err!=KErrNone)
   302 	    {
   303         delete updateProxy;
   304         iRegisteredUpdaters.Remove(aScreenNum);
   305 	    }
   306 	return err;
   307 	}
   308 
   309 CExtensionContainer* COpenWfcStreamMap::RegisteredScreenNotifications(TInt aScreenNum)
   310     {
   311     return *iRegisteredUpdaters.Find(aScreenNum);
   312     }
   313 
   314 TInt COpenWfcStreamMap::UnregisterScreenNotifications(TInt aScreenNum)
   315 	{
   316 	TInt err = KErrNone;
   317 	CExtensionContainer** backend = iRegisteredUpdaters.Find(aScreenNum);
   318 	if (backend)
   319 	    {
   320 	    delete *backend;
   321 	    iRegisteredUpdaters.Remove(aScreenNum);
   322         if (iSurfUpdateServ)
   323             {
   324             err = iSurfUpdateServ->Register(aScreenNum, NULL, 0);
   325             }
   326         else
   327             {
   328             err = KErrNotReady;
   329             }
   330 	    }
   331 	else
   332 	    {
   333 	    err = KErrNotFound;
   334 	    }
   335 
   336 	return err;
   337 	}