windowing/windowserver/nga/SERVER/openwfc/registeredsurfacemap.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2007-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 "registeredsurfacemap.h"
       
    17 #include "panics.h"
       
    18 #include <graphics/wsscene.h>
       
    19 #include <w32std.h>
       
    20 
       
    21 /**
       
    22 Destructor for the surface map, will unregister every surface in every client.
       
    23 */
       
    24 CRegisteredSurfaceMap::~CRegisteredSurfaceMap()
       
    25 	{
       
    26 	TInt tempCount = iSessionSurfaces.Count();
       
    27 	for (TInt ii = 0; ii < tempCount; ii++)
       
    28 		{
       
    29 		iSessionSurfaces[ii].RemoveAll(iScene);
       
    30 		}
       
    31 	iSessionSurfaces.Close();
       
    32 	}
       
    33 
       
    34 /**
       
    35 Constructor for the surface map.
       
    36 */
       
    37 CRegisteredSurfaceMap::CRegisteredSurfaceMap(MWsScene& aScene):iScene(aScene)
       
    38 	{}
       
    39 
       
    40 /**
       
    41 Basic compare function, looking at the address of each session.
       
    42 @param aLeft Left session for comparison.
       
    43 @param aRight Right session for comparison.
       
    44 @return Negative if left is smaller than right, positive if left is greater than right,
       
    45 zero if they are the same.
       
    46 */
       
    47 TInt CRegisteredSurfaceMap::CompareDeviceSurfaces (const TSessionSurfaces& aLeft, const TSessionSurfaces& aRight)
       
    48 	{
       
    49 	return (TInt)&aLeft.Session()-(TInt)&aRight.Session();
       
    50 	}
       
    51 
       
    52 /**
       
    53 Function to register a surface on a client.  Will go through a process to add the surface to a list
       
    54 for the client to keep track of which surfaces are registered for which clients.
       
    55 @param aClient The client for which to register the surface.
       
    56 @param aSurfaceId The surface to register on the client.
       
    57 @return KErrNone on success or a system-wide error code.
       
    58 @see CWsClient::RegisterSurface
       
    59 */
       
    60 TInt CRegisteredSurfaceMap::Add(const CWsClient& aClient, const TSurfaceId& aSurfaceId)
       
    61 	{
       
    62 	TInt err;
       
    63 	TInt tempPos;
       
    64 	TSessionSurfaces tempDeviceSurface(aClient);
       
    65 	
       
    66 	//find client
       
    67 	err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder<TSessionSurfaces>(CompareDeviceSurfaces));
       
    68 	
       
    69 	//if the client is found
       
    70 	if (err == KErrNone)
       
    71 		{
       
    72 		err = iSessionSurfaces[tempPos].AddSurfaceId(aSurfaceId, iScene);
       
    73 		}
       
    74 	else
       
    75 		{
       
    76 		if (err == KErrNotFound)
       
    77 			{
       
    78 			err = iSessionSurfaces.Insert(tempDeviceSurface, tempPos);
       
    79 			if (err == KErrNone)
       
    80 				{
       
    81 				err = iSessionSurfaces[tempPos].AddSurfaceId(aSurfaceId, iScene);
       
    82 				if ((err != KErrNone) && (err != KErrInUse))	//it didnt manage to register the surface id
       
    83 					{
       
    84 					iSessionSurfaces.Remove(tempPos);
       
    85 					}
       
    86 				}
       
    87 			}
       
    88 		}
       
    89 	return err;
       
    90 	}
       
    91 
       
    92 /**
       
    93 Function to unregister a specific surface on a specific client.  Goes through a process to check
       
    94 that the surface is registered for the client before remove it from the list and unregistering it.
       
    95 @param aClient The client for which to unregister the surface.
       
    96 @param aSurfaceId The surface to unregister on the client.
       
    97 @return KErrNone on success or a system-wide error code.
       
    98 @see CWsClient::UnregisterSurface
       
    99 */
       
   100 TInt CRegisteredSurfaceMap::Remove(CWsClient& aClient, const TSurfaceId& aSurfaceId)
       
   101 	{
       
   102 	TInt err;
       
   103 	TInt tempPos;
       
   104 	TSessionSurfaces tempDeviceSurface(aClient);
       
   105 	
       
   106 	//find client
       
   107 	err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder<TSessionSurfaces>(CompareDeviceSurfaces));
       
   108 	TInt surfaceAmountLeftOrError;
       
   109 	//if found
       
   110 	if (err == KErrNone)
       
   111 		{
       
   112 		surfaceAmountLeftOrError = iSessionSurfaces[tempPos].RemoveSurfaceId(aSurfaceId, iScene);
       
   113 		if (surfaceAmountLeftOrError == 0)	//there are no more surfaces registered for the client
       
   114 			{
       
   115 			iSessionSurfaces.Remove(tempPos);
       
   116 			}
       
   117 		if (surfaceAmountLeftOrError < 0)	//if an error was returned
       
   118 			{
       
   119 			err = surfaceAmountLeftOrError;
       
   120 			}
       
   121 		}
       
   122 	return err;
       
   123 	}
       
   124 
       
   125 /**
       
   126 Function that will unregister all surfaces for a specific client.
       
   127 @param aClient The client for which to unregister all clients.
       
   128 @return KErrNone on success or a system-wide error code.
       
   129 @see CWsTop::ClearSurfaceMap
       
   130 @see CWsClient::~CWsClient()
       
   131 */
       
   132 TInt CRegisteredSurfaceMap::RemoveAll(CWsClient& aClient)
       
   133 	{
       
   134 	TInt err;
       
   135 	TInt tempPos;
       
   136 	TSessionSurfaces tempDeviceSurface(aClient);
       
   137 	
       
   138 	//find client
       
   139 	err = iSessionSurfaces.FindInOrder(tempDeviceSurface, tempPos, TLinearOrder<TSessionSurfaces>(CompareDeviceSurfaces));
       
   140 	
       
   141 	//if found
       
   142 	if (err == KErrNone)
       
   143 		{
       
   144 		err = iSessionSurfaces[tempPos].RemoveAll(iScene);
       
   145 		}
       
   146 	if (err == KErrNone || err == KErrInUse)
       
   147 		{
       
   148 		iSessionSurfaces.Remove(tempPos);
       
   149 		}
       
   150 	return err;
       
   151 	}
       
   152 
       
   153 TSessionSurfaces::TSessionSurfaces(const CWsClient& aSession):iSession(aSession)
       
   154 	{}
       
   155 
       
   156 TInt TSessionSurfaces::CompareIds(const TSurfaceId& aLeft, const TSurfaceId& aRight)
       
   157 	{
       
   158 	//Compare each internal id of the surface
       
   159 	TUint32 ll;
       
   160 	TUint32 rr;
       
   161 	TInt32 tempResult;
       
   162 	ll = aLeft.iInternal[0];
       
   163 	rr = aRight.iInternal[0];
       
   164 	tempResult = ll-rr;
       
   165 	if (tempResult == 0)
       
   166 		{
       
   167 		ll = aLeft.iInternal[1];
       
   168 		rr = aRight.iInternal[1];
       
   169 		tempResult = ll-rr;
       
   170 		if (tempResult == 0)
       
   171 			{
       
   172 			ll = aLeft.iInternal[2];
       
   173 			rr = aRight.iInternal[2];
       
   174 			tempResult = ll-rr;
       
   175 			if (tempResult == 0)
       
   176 				{
       
   177 				ll = aLeft.iInternal[3];
       
   178 				rr = aRight.iInternal[3];
       
   179 				tempResult = ll-rr;
       
   180 				}
       
   181 			}
       
   182 		}
       
   183 	if (tempResult != 0)	//if they are different ids
       
   184 		{
       
   185 		if (ll < rr)
       
   186 			{
       
   187 			return -1;
       
   188 			}
       
   189 		else
       
   190 			{
       
   191 			return 1;
       
   192 			}
       
   193 		}
       
   194 	return 0;
       
   195 	}
       
   196 
       
   197 TInt TSessionSurfaces::AddSurfaceId(const TSurfaceId& aSurfaceId, MWsScene& aScene)
       
   198 	{
       
   199 	TInt err;
       
   200 	TInt tempPos;
       
   201 	//find surface id
       
   202 	err = iSurfaces.FindInOrder(aSurfaceId, tempPos, TLinearOrder<TSurfaceId>(CompareIds));
       
   203 	
       
   204 	if (err == KErrNotFound)
       
   205 		{
       
   206 		err = iSurfaces.Insert(aSurfaceId, tempPos);
       
   207 		if (err == KErrNone)	//successfully added surface, can now register
       
   208 			{
       
   209 			err = aScene.RegisterSurface(aSurfaceId);
       
   210 			}
       
   211 		}
       
   212 	else
       
   213 		{
       
   214 		if (err == KErrNone)
       
   215 			{
       
   216 			//shouldnt be registered more than once
       
   217 			err = KErrInUse;
       
   218 			}
       
   219 		}
       
   220 	return err;
       
   221 	}
       
   222 
       
   223 TInt TSessionSurfaces::RemoveSurfaceId(const TSurfaceId& aSurfaceId, MWsScene& aScene)
       
   224 	{
       
   225 	TInt surfaceAmountLeftOrError;
       
   226 	TInt tempPos;
       
   227 
       
   228 	//find surface id
       
   229 	surfaceAmountLeftOrError = iSurfaces.FindInOrder(aSurfaceId, tempPos, 
       
   230 			TLinearOrder<TSurfaceId>(CompareIds));
       
   231 	if (surfaceAmountLeftOrError!=KErrNone)
       
   232 		surfaceAmountLeftOrError = iSurfaces.FindInOrder(aSurfaceId, tempPos, 
       
   233 				TLinearOrder<TSurfaceId>(CompareIds));
       
   234 	//if found
       
   235 	if (surfaceAmountLeftOrError == KErrNone)
       
   236 		{
       
   237 		
       
   238 		surfaceAmountLeftOrError = aScene.UnregisterSurface(aSurfaceId);
       
   239 		if ((surfaceAmountLeftOrError == KErrNone) || (surfaceAmountLeftOrError == KErrInUse))
       
   240 			{
       
   241 			iSurfaces.Remove(tempPos);
       
   242 			surfaceAmountLeftOrError = iSurfaces.Count();
       
   243 			}
       
   244 		else
       
   245 			{
       
   246 			WS_ASSERT_DEBUG(EFalse,EWsPanicSurfaceMapError);	//Unexpected error
       
   247 			}
       
   248 		}
       
   249 	return surfaceAmountLeftOrError;
       
   250 	}
       
   251 
       
   252 TInt TSessionSurfaces::RemoveAll(MWsScene& aScene)
       
   253 	{
       
   254     TInt err = KErrNone;
       
   255     TInt returnValue = KErrNone;
       
   256     TInt tempSize = iSurfaces.Count();
       
   257     for (TInt ii = 0; ii < tempSize; ii++)
       
   258         {
       
   259         err = aScene.UnregisterSurface(iSurfaces[ii]);
       
   260         WS_ASSERT_ALWAYS((err == KErrNone) || (err == KErrInUse), EWsPanicSurfaceMapError);
       
   261         if (err != KErrNone && returnValue == KErrNone)
       
   262             {   //return first error code
       
   263             returnValue = err;
       
   264             }
       
   265         }
       
   266     iSurfaces.Close();
       
   267     return returnValue;
       
   268 	}