graphicshwdrivers/surfacemgr/test/src/tsurfacemanagermultithread.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 // Surface manager multi-threaded test code
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @test
       
    21  @internalComponent - Internal Symbian test code
       
    22 */
       
    23 
       
    24 #include "tsurfacemanagermultithread.h"
       
    25 #include <e32base.h>
       
    26 #include <e32cons.h>
       
    27 #include <e32test.h>
       
    28 #include <e32std.h>
       
    29 #include <e32cmn.h>
       
    30 #include <e32def.h>	  
       
    31 
       
    32 static TSurfaceId globalSurfaceId;	
       
    33 static TInt testResult;
       
    34 
       
    35 typedef CTSurfaceManagerMultiThread::TInfo TInfo;
       
    36 
       
    37 CTSurfaceManagerMultiThread::CTSurfaceManagerMultiThread(CTestStep* aStep):
       
    38 	CTGraphicsBase(aStep)
       
    39 	{
       
    40 	
       
    41 	}
       
    42 
       
    43 CTSurfaceManagerMultiThread::~CTSurfaceManagerMultiThread()
       
    44 	{
       
    45 	iSurfaceManager.Close();
       
    46 	}
       
    47 
       
    48 void CTSurfaceManagerMultiThread::RunTestCaseL(TInt aCurTestCase)
       
    49 	{
       
    50 	TInt procHandles1  =0;
       
    51 	TInt threadHandles1=0;
       
    52 	RThread().HandleCount(procHandles1, threadHandles1);
       
    53 	((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(KUnknownSYMTestCaseIDName);
       
    54 	switch(aCurTestCase)
       
    55 		{
       
    56 /**
       
    57 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0078
       
    58 @SYMPREQ				PREQ1879, PREQ1007
       
    59 @SYMREQ					REQ8222,REQ8223
       
    60 @SYMTestPriority		High 
       
    61 @SYMTestCaseDesc		Test accessing surface in another thread without opening it
       
    62 						Thread 1: Create Surface 
       
    63 						Thread 2: Map Surface - KErrNone (still accessible)
       
    64 						Thread 2: SurfaceInfo - KErrNone (still accessible)
       
    65 @SYMTestStatus			Implemented
       
    66 @SYMTestActions			Call CreateSurface(),MapSurface(), SurfaceInfo()
       
    67 @SYMTestExpectedResults The surface can be successfully accessed in other threads without first opening it.
       
    68 */
       
    69 	case 1:
       
    70 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0078"));
       
    71 		TestAccessSurfaceWithoutOpeningL();
       
    72 		break;
       
    73 /**
       
    74 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0079
       
    75 @SYMPREQ				PREQ1879, PREQ1007
       
    76 @SYMREQ					REQ8222,REQ8223
       
    77 @SYMTestPriority		High 
       
    78 @SYMTestCaseDesc		Test accessing a surface created in the existing shared chunk in another thread without opening it.
       
    79 						Thread 1: Create a Surface in the new chunk with valid creation attributes
       
    80 						Thread 1: Map the surface in the current process to get the chunk handle
       
    81 						Thread 1: Create a new surface in the existing chunk with valid creation attributes
       
    82 						Thread 2: Map the second Surface – KErrNone (still accessible)
       
    83 						Thread 2: SurfaceInfo – KErrNone (still accessible)
       
    84 @SYMTestStatus			Implemented
       
    85 @SYMTestActions			Call CreateSurface(),MapSurface(), SurfaceInfo()
       
    86 @SYMTestExpectedResults MapSurface, SurfaceInfo return KerrNone
       
    87 */	
       
    88 	case 2:
       
    89         ((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0079"));
       
    90 		TestAccessSurfaceInExistingSharedChunkL();
       
    91 		break;
       
    92 /**
       
    93 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0080
       
    94 @SYMPREQ				PREQ1879, PREQ1007
       
    95 @SYMREQ					REQ8222,REQ8223
       
    96 @SYMTestPriority		High 
       
    97 @SYMTestCaseDesc		Test closing surface in another thread without opening it
       
    98 						Thread 1: Create Surface 
       
    99 						Thread 2: Close Surface - KErrNone
       
   100 @SYMTestStatus			Implemented
       
   101 @SYMTestActions			Call CreateSurface(),CloseSurface()
       
   102 @SYMTestExpectedResults The surface can be successfully closed in other threads without first opening it.
       
   103 */
       
   104 	case 3:
       
   105 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0080"));
       
   106 		TestCloseSurfaceWithoutOpeningL();
       
   107 		break;
       
   108 /**
       
   109 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0081
       
   110 @SYMPREQ				PREQ1879, PREQ1007
       
   111 @SYMREQ					REQ8222,REQ8223
       
   112 @SYMTestPriority		High 
       
   113 @SYMTestCaseDesc		Test accessing an surface which is created in another thread but closed in current thread 
       
   114 						Thread 2: Create Surface
       
   115 						Thread 1: Close Surface - KerrNone
       
   116 						Thread 2: Map Surface - KErrArgument
       
   117 						Thread 2: SurfaceInfo - KErrArgument
       
   118 @SYMTestStatus			Implemented
       
   119 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo()
       
   120 @SYMTestExpectedResults The surface cant be accessd even after it is closed in other threads.
       
   121 */
       
   122 	case 4:
       
   123 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0081"));
       
   124 		TestAccessSurfaceClosedThreadL();
       
   125 		break;
       
   126 /**
       
   127 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0067
       
   128 @SYMPREQ				PREQ1879, PREQ1007
       
   129 @SYMREQ					REQ8222,REQ8223
       
   130 @SYMTestPriority		High 
       
   131 @SYMTestCaseDesc		Test closing surface created in the existing chunk in another thread without opening it 
       
   132 						Thread 1: Create a Surface in a new shared chunk with valid attributes
       
   133 						Thread 1: Map the surface in the process to get the shared chunk handle
       
   134 						Thread 1: Create a new surface in the existing shared chunk
       
   135 						Thread 2: Close the second surface – KErrNone (the second surface is deleted)
       
   136 						Thread 2: Call SurfaceInfo to the first surface and it returns KErrNone, but it returns KErrArgument to the second surface.
       
   137 						Thread 2: Close the first surface and check it returns KErrNone (the first surface is deleted).
       
   138 						Thread 1: Call SurfaceInfo to both surfaces and return KErrArgument
       
   139 						Thread 1: Check the chunk is still accessible by calling RChunk::Base() 
       
   140 						Thread 1: Close the chunk handle  
       
   141 @SYMTestStatus			Implemented
       
   142 @SYMTestActions			Call CreateSurface(),CloseSurface()
       
   143 @SYMTestExpectedResults CloseSurface returns KErrNone. The chunk handle is not closed even when both surfaces are deleted.
       
   144 */		
       
   145 	case 5:
       
   146 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0067"));
       
   147 		TestCloseSurfaceExistingSharedChunkL();
       
   148 		break;
       
   149 
       
   150 /**
       
   151 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0068
       
   152 @SYMPREQ				PREQ1007
       
   153 @SYMREQ					REQ8222,REQ8223
       
   154 @SYMTestPriority		High 
       
   155 @SYMTestCaseDesc		Test closing an surface which is already closed in other thread return KErrArgument 
       
   156 						Thread 2: Create Surface
       
   157 						Thread 1: Close Surface - KerrNone
       
   158 						Thread 2: Close Surface - KErrArgument
       
   159 @SYMTestStatus			Implemented
       
   160 @SYMTestActions			Call CreateSurface(),CloseSurface()
       
   161 @SYMTestExpectedResults The surface cant be closed again.
       
   162 */
       
   163 	case 6:
       
   164 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0068"));
       
   165 		TestCloseSurfaceClosedThreadL();
       
   166 		break;
       
   167 /**
       
   168 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0069
       
   169 @SYMPREQ				PREQ1007
       
   170 @SYMREQ					REQ8222,REQ8223
       
   171 @SYMTestPriority		High 
       
   172 @SYMTestCaseDesc		Test accessing an surface which is already opened and closed in another thread, given that the process still owns the surface 
       
   173 						Thread 1: Create Surface
       
   174 						Thread 2: Open Surface -- KErrNone
       
   175 						Thread 2: Close Surface -- KErrNone
       
   176 						Thread 1: Map Surface - KErrNone
       
   177 						Thread 1: SurfaceInfo - KErrNone
       
   178 @SYMTestStatus			Implemented
       
   179 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo, OpenSurface()
       
   180 @SYMTestExpectedResults The surface is still accessible in the thread residing the owning process
       
   181 */
       
   182 	case 7:
       
   183 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0069"));
       
   184 		TestAccessSurfaceOpenedClosedThreadL();
       
   185 		break;
       
   186 /**
       
   187 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0070
       
   188 @SYMPREQ				PREQ1007
       
   189 @SYMREQ					REQ8222,REQ8223
       
   190 @SYMTestPriority		High 
       
   191 @SYMTestCaseDesc		Test closing an surface which is already opened and closed in another thread, given that the process still owns the surface
       
   192     					Thread 1: Create Surface
       
   193 						Thread 2: Open Surface
       
   194 						Thread 2: Close Surface
       
   195 						Thread 1: Close Surface - KErrNone
       
   196 @SYMTestStatus			Implemented
       
   197 @SYMTestActions			Call CreateSurface(),CloseSurface(), OpenSurface()
       
   198 @SYMTestExpectedResults The surface is still closible in the thread residing in the owning process
       
   199 */
       
   200 	case 8:
       
   201 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0070"));
       
   202 		TestCloseSurfaceOpenedClosedThreadL();
       
   203 		break;
       
   204 /**
       
   205 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0071
       
   206 @SYMPREQ				PREQ1007
       
   207 @SYMREQ					REQ8222,REQ8223
       
   208 @SYMTestPriority		High 
       
   209 @SYMTestCaseDesc		Test accessing surface in another thread when the opening thread is killed   
       
   210 						Thread 1: Create Surface 
       
   211 						Thread 2: Open Surface
       
   212 						Thread 2: Kill the thread
       
   213 						Thread 1: Map Surface - KErrNone
       
   214 						Thread 1: Surface Info -- KErrNone
       
   215 @SYMTestStatus			Implemented
       
   216 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo(), OpenSurface()
       
   217 @SYMTestExpectedResults The surface is accessible when the opening thread is killed
       
   218 */
       
   219 	case 9:
       
   220 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0071"));
       
   221 		TestAccessSurfaceOpenedKilledThreadL();
       
   222 		break;
       
   223 /**
       
   224 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0072
       
   225 @SYMPREQ				PREQ1007
       
   226 @SYMREQ					REQ8222,REQ8223
       
   227 @SYMTestPriority		High 
       
   228 @SYMTestCaseDesc		Test closing surface in another thread when the opening thread is killed   
       
   229 						Thread 1: Create Surface 
       
   230 						Thread 2: Open Surface
       
   231 						Thread 2: Kill the thread
       
   232 						Thread 1: Close Surface - KErrNone
       
   233 @SYMTestStatus			Implemented
       
   234 @SYMTestActions			Call CreateSurface(),CloseSurface(), OpenSurface()
       
   235 @SYMTestExpectedResults The surface is closible when the opening thread is killed
       
   236 */
       
   237 	case 10:
       
   238 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0072"));
       
   239 		TestCloseSurfaceOpenedKilledThreadL();
       
   240 		break;
       
   241 /**
       
   242 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0073
       
   243 @SYMPREQ				PREQ1007
       
   244 @SYMREQ					REQ8222,REQ8223
       
   245 @SYMTestPriority		High 
       
   246 @SYMTestCaseDesc		Test accessing surface in another thread when the creating thread is killed and it is closed in other threads
       
   247 						Thread 2: Create Surface
       
   248 						Thread 1: Close Surface
       
   249 						Thread 2: Kill Thread
       
   250 						Thread 1: Map Surface - KErrArgument
       
   251 						Thread 1: Surface Info - KErrArgument
       
   252 @SYMTestStatus			Implemented
       
   253 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo(),
       
   254 @SYMTestExpectedResults The surface is accessible when the creating thread is killed and it is closed in other threads
       
   255 */
       
   256 	case 11:
       
   257 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0073"));
       
   258 		TestAccessSurfaceCreateKilledThreadL();
       
   259 		break;
       
   260 /**
       
   261 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0074
       
   262 @SYMPREQ				PREQ1007
       
   263 @SYMREQ					REQ8222,REQ8223
       
   264 @SYMTestPriority		High 
       
   265 @SYMTestCaseDesc		Test accessing surface in another thread when the creating thread is killed and it is closed in other threads
       
   266 						Thread 2: Create Surface
       
   267 						Thread 1: Close Surface
       
   268 						Thread 2: Kill Thread
       
   269 						Thread 1: Close Surface - KErrArgument
       
   270 @SYMTestStatus			Implemented
       
   271 @SYMTestActions			Call CreateSurface(),CloseSurface()
       
   272 @SYMTestExpectedResults The surface is accessible when the creating thread is killed and it is closed in other threads
       
   273 */
       
   274 	case 12:
       
   275 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0074"));
       
   276 		TestCloseSurfaceCreateKilledThreadL();
       
   277 		break;
       
   278 /**
       
   279 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0075
       
   280 @SYMPREQ				PREQ1007
       
   281 @SYMREQ					REQ8222,REQ8223
       
   282 @SYMTestPriority		High 
       
   283 @SYMTestCaseDesc		Test closing on one thread doesn't prevent opening on other Threads, provided one Thread still owns surface
       
   284     					Thread 1: Create Surface
       
   285 						Thread 2: Open Surface
       
   286 						Thread 2: Close Surface
       
   287 						Thread 3: Open Surface - KerrNone
       
   288 						Thread 3: Map Surface - KerrNone
       
   289 						Thread 3: SurfaceInfo -- KErrNone
       
   290 @SYMTestStatus			Implemented
       
   291 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo(),
       
   292 @SYMTestExpectedResults It is expected that closing on one thread doesn't prevent opening on other Threads, provided one Thread still owns surface
       
   293 */
       
   294 	case 13:
       
   295 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0075"));
       
   296 		TestAccessSurfaceThreeThreadsL();
       
   297 		break;	
       
   298 /**
       
   299 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0076
       
   300 @SYMPREQ				PREQ1007
       
   301 @SYMREQ					REQ8222,REQ8223
       
   302 @SYMTestPriority		High 
       
   303 @SYMTestCaseDesc		Test surface can be accessed from third thread when creating thread dies and second thread closes
       
   304     					Thread 2: Create Surface
       
   305 						Thread 1: Open Surface
       
   306 						Thread 3: Open Surface
       
   307 						Thread 1: Close Surface
       
   308 						Thread 2: Kill Thread
       
   309  					 	Thread 3: Close Surface
       
   310 						Thread 3: Map Surface - KerrNone
       
   311 						Thread 3: SurfaceInfo -- KErrNone
       
   312 @SYMTestStatus			Implemented
       
   313 @SYMTestActions			Call CreateSurface(),CloseSurface(), MapSurface(), SurfaceInfo(), OpenSurface()
       
   314 @SYMTestExpectedResults It is expected that surface can be accessed from third thread when creating thread dies and second thread closes
       
   315 */
       
   316 	case 14:
       
   317 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0076"));
       
   318 		TestAccessSurfaceDieCloseOtherThreadsL();
       
   319 		break;
       
   320 /**
       
   321 @SYMTestCaseID			GRAPHICS-SURFACEMANAGER-0077
       
   322 @SYMPREQ				PREQ1007
       
   323 @SYMREQ					REQ8222,REQ8223
       
   324 @SYMTestPriority		High 
       
   325 @SYMTestCaseDesc		Test that the surface can be opened from the third Thread after creating thread dies and second and third thread close the surface
       
   326    						Thread 2: Create Surface
       
   327 						Thread 1: Open Surface
       
   328 						Thread 3: Open Surface
       
   329 						Thread 1: Close Surface
       
   330 						Thread 2: Kill Thread
       
   331  					    Thread 3: Close Surface
       
   332 						Thread 3: Open Surface - KerrNone
       
   333 @SYMTestStatus			Implemented
       
   334 @SYMTestActions			Call CreateSurface(),CloseSurface(), OpenSurface()
       
   335 @SYMTestExpectedResults It is expected that the surface can be opened from the third Thread after creating thread dies and second and third thread close the surface
       
   336 */
       
   337 	case 15:
       
   338 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(_L("GRAPHICS-SURFACEMANAGER-0077"));
       
   339 		TestOpenSurfaceDieCloseOtherThreadsL();
       
   340 		break;
       
   341 	default:
       
   342 		((CTSurfaceManagerMultiThreadStep*)iStep)->SetTestStepID(KNotATestSYMTestCaseIDName);
       
   343 		((CTSurfaceManagerMultiThreadStep*)iStep)->CloseTMSGraphicsStep();
       
   344 		TestComplete();
       
   345 		break;
       
   346 		}
       
   347 	((CTSurfaceManagerMultiThreadStep*)iStep)->RecordTestResultL();
       
   348 	// Handle check
       
   349 	TInt procHandles2  =0;
       
   350 	TInt threadHandles2=0;
       
   351 	RThread().HandleCount(procHandles2,threadHandles2);
       
   352 	if (threadHandles1 != threadHandles2)
       
   353 		{
       
   354 		User::Leave(KErrGeneral);  // Thread-owned handles not closed
       
   355 		}
       
   356  	}
       
   357 
       
   358 void CTSurfaceManagerMultiThread::CloseSurfaceWhenLeave(TAny* aInfo) 
       
   359 	{
       
   360 	TInt err1 = ((TInfo*)aInfo)->iSurfaceManager.CloseSurface(((TInfo*)aInfo)->iSurfaceId);
       
   361 	TInt err2 = ((TInfo*)aInfo)->iSurfaceManager.CloseSurface(((TInfo*)aInfo)->iSurfaceIdNew);
       
   362 	if (err1!=KErrNone || err2!=KErrNone)
       
   363 	   	RDebug::Print(_L("Error closing surfaces err1= %d, err2= %d\n"),err1,err2);	
       
   364 	}
       
   365 
       
   366 void CTSurfaceManagerMultiThread::TestAccessSurfaceWithoutOpeningL()
       
   367 	{
       
   368 	User::LeaveIfError(iSurfaceManager.Open());
       
   369     
       
   370 	// Setup attributes
       
   371     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   372 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   373 	attributes.iSize = TSize(480,16);
       
   374 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   375 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   376 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   377 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   378 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   379 
       
   380 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
   381 	attributes.iHintCount = 2;
       
   382 	attributes.iSurfaceHints = hints;
       
   383 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   384 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   385 	
       
   386 	attributes.iContiguous = ETrue;
       
   387 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   388 	attributes.iMappable = ETrue;
       
   389 	
       
   390 	// Test create surface doesn't return an error
       
   391 	TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   392     // Logging info   
       
   393     INFO_PRINTF1(_L("Test accessing surface in another thread without opening it\r\n"));
       
   394     // Create a semaphore
       
   395 	RSemaphore sem;
       
   396 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   397 	CleanupClosePushL(sem);	
       
   398 	
       
   399 	// Create a second thread in the current process and opens the handle
       
   400 	iInfo.iSurfaceManager = iSurfaceManager;
       
   401 	iInfo.iSurfaceId = iSurfaceId;
       
   402 	iInfo.iSurfaceIdNew = TSurfaceId::CreateNullId();
       
   403 	iInfo.iThreadTestCase = EMapSurfaceInfo;		
       
   404 	// Create a TCleanupItem object
       
   405     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   406 		
       
   407     RThread thread1;
       
   408     _LIT(KNameThreadSecond1, "Test_SurfaceManager_ThreadSecond1");
       
   409 	User::LeaveIfError(thread1.Create(KNameThreadSecond1,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   410    	CleanupStack::Pop();
       
   411     // Launch second thread
       
   412 	TRequestStatus statusThreadSecond;
       
   413 	thread1.Logon(statusThreadSecond);
       
   414 	thread1.SetPriority(EPriorityLess);
       
   415 	thread1.Resume();	
       
   416 	testResult = EAllZero;
       
   417 	// Passes control to the second process
       
   418 	sem.Wait();
       
   419 	thread1.Suspend();
       
   420 
       
   421 	// Test results in second thread
       
   422 	TEST (testResult = (EMapSurfaceTestPassed | ESurfaceInfoTestPassed));
       
   423 
       
   424 	// Pass control off to the second process again
       
   425 	thread1.Resume();
       
   426 
       
   427 	// Wait for a second for the second thread to terminate
       
   428 	User::WaitForRequest(statusThreadSecond);
       
   429 
       
   430 	//Close the handles
       
   431     CleanupStack::PopAndDestroy(1, &sem);  //invoke surface manager to close the surface also close the handle to RSemaphore
       
   432     thread1.Close();
       
   433     iSurfaceManager.Close();
       
   434 	}
       
   435 
       
   436 void CTSurfaceManagerMultiThread::TestAccessSurfaceInExistingSharedChunkL()
       
   437     {
       
   438     // Logging info   
       
   439     INFO_PRINTF1(_L("Test accessing a surface created in the existing shared chunk in another thread without opening it\r\n"));
       
   440 
       
   441     User::LeaveIfError(iSurfaceManager.Open());
       
   442     
       
   443     // Setup attributes 
       
   444     
       
   445     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   446     RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   447 
       
   448     attributes.iSize = TSize(100,100);
       
   449     attributes.iBuffers = 1;                // number of buffers in the surface
       
   450     attributes.iPixelFormat = EUidPixelFormatYUV_422SemiPlanar;     // 2bpp
       
   451     attributes.iStride = 400;               // Number of bytes between start of one line and start of next
       
   452     attributes.iOffsetToFirstBuffer = 0;    // way of reserving space before the surface pixel data
       
   453     attributes.iAlignment = 2;          // alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   454 
       
   455     RSurfaceManager::THintPair hints[2];    // two hint pairs specified
       
   456     attributes.iHintCount = 2;
       
   457     attributes.iSurfaceHints = hints;
       
   458     hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   459     hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   460     
       
   461     attributes.iContiguous = ETrue;
       
   462     attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   463     attributes.iOffsetBetweenBuffers = 0;
       
   464     attributes.iMappable = ETrue;
       
   465 
       
   466     // Test create surface doesn't return an error
       
   467     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   468     
       
   469     //Map the surface in the current processs
       
   470     RChunk handle;
       
   471     TEST(KErrNone == iSurfaceManager.MapSurface(iSurfaceId,handle));
       
   472     
       
   473     // Cache, Contiguous and Alignment attributes are ignored for the already existing chunks
       
   474     RSurfaceManager::TSurfaceCreationAttributesBuf buff;
       
   475     RSurfaceManager::TSurfaceCreationAttributes& attributesNew = buff();
       
   476 
       
   477     attributesNew.iSize = TSize(480,16);
       
   478     attributesNew.iBuffers = 2;             // number of buffers in the surface
       
   479     attributesNew.iPixelFormat = EUidPixelFormatYUV_422Reversed;        // 2bpp
       
   480     attributesNew.iStride = 1013;               // Number of bytes between start of one line and start of next
       
   481     attributesNew.iOffsetToFirstBuffer = 0; // way of reserving space before the surface pixel data
       
   482     
       
   483     attributesNew.iHintCount = 1;
       
   484     attributesNew.iSurfaceHints = hints;
       
   485     hints[0].Set(TUid::Uid(0x124545), 50, EFalse);
       
   486     
       
   487     attributesNew.iAlignment = 2;
       
   488     attributesNew.iOffsetBetweenBuffers = 0;
       
   489     attributes.iMappable = ETrue;
       
   490     
       
   491     // Test create surface doesn't return an error
       
   492     // For the time being KErrArgument will be returned as the core codes are
       
   493     // not ready to check the passed in shared chunk handle.
       
   494     TEST(KErrNone == iSurfaceManager.CreateSurface(buff, iSurfaceIdNew, handle));
       
   495 
       
   496 
       
   497     // Create a semaphore
       
   498     RSemaphore sem;
       
   499     TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   500     CleanupClosePushL(sem); 
       
   501     
       
   502     // Create a second thread in the current process and opens the handle
       
   503     iInfo.iSurfaceManager = iSurfaceManager;
       
   504     iInfo.iSurfaceId = iSurfaceIdNew;
       
   505     iInfo.iSurfaceIdNew = iSurfaceId;
       
   506     iInfo.iThreadTestCase = EMapSurfaceInfo;    
       
   507     // Create a TCleanupItem object
       
   508     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   509     
       
   510     RThread thread2;
       
   511     _LIT(KNameThreadSecond2, "Test_SurfaceManager_ThreadSecond2");
       
   512     User::LeaveIfError(thread2.Create(KNameThreadSecond2,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   513  
       
   514     CleanupStack::Pop();
       
   515     // Launch second thread
       
   516     TRequestStatus statusThreadSecond;
       
   517     thread2.Logon(statusThreadSecond);
       
   518     thread2.SetPriority(EPriorityLess);
       
   519     thread2.Resume();   
       
   520     testResult = EAllZero;
       
   521     // Passes control to the second process
       
   522     sem.Wait();
       
   523     thread2.Suspend();
       
   524 
       
   525     // Test results in second thread
       
   526     TEST (testResult = (EMapSurfaceTestPassed | ESurfaceInfoTestPassed));
       
   527 
       
   528     // Pass control off to the second process again
       
   529     thread2.Resume();
       
   530 
       
   531     // Wait for a second for the second thread to terminate
       
   532     User::WaitForRequest(statusThreadSecond);
       
   533     
       
   534     //Close the handles
       
   535     CleanupStack::PopAndDestroy(1, &sem);  //invoke surface manager to close the surface also close the handle to RSemaphore
       
   536     handle.Close();
       
   537     thread2.Close();
       
   538     iSurfaceManager.Close();
       
   539     
       
   540     }
       
   541 
       
   542 void CTSurfaceManagerMultiThread::TestCloseSurfaceWithoutOpeningL()
       
   543 	{
       
   544 	User::LeaveIfError(iSurfaceManager.Open());
       
   545     
       
   546 	// Setup attributes
       
   547     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   548 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   549 	
       
   550 	attributes.iSize = TSize(480,16);
       
   551 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   552 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   553 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   554 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   555 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   556 
       
   557 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
   558 	attributes.iHintCount = 2;
       
   559 	attributes.iSurfaceHints = hints;
       
   560 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   561 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   562 	
       
   563 	attributes.iContiguous = ETrue;
       
   564 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   565 	attributes.iMappable = ETrue;
       
   566 	
       
   567 	// Test create surface doesn't return an error
       
   568     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   569    
       
   570     INFO_PRINTF1(_L("Test closing surface in another thread without opening it\r\n"));
       
   571 	
       
   572 	// Create a semaphore
       
   573 	RSemaphore sem;
       
   574 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   575    	CleanupClosePushL(sem);	
       
   576 
       
   577   	// Creates a second thread in the current process and opens the handle
       
   578 	iInfo.iSurfaceManager = iSurfaceManager;
       
   579 	iInfo.iSurfaceId = iSurfaceId;
       
   580 	iInfo.iThreadTestCase = ECloseSurfaces;	
       
   581    	// Create a TCleanupItem object
       
   582     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   583 
       
   584     RThread thread3;
       
   585     _LIT(KNameThreadSecond3, "Test_SurfaceManager_ThreadSecond3");
       
   586 	User::LeaveIfError(thread3.Create(KNameThreadSecond3,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   587     CleanupStack::Pop();
       
   588     // Launch second thread to close the surface
       
   589 	TRequestStatus statusThreadSecond;
       
   590 	thread3.Logon(statusThreadSecond);
       
   591 	thread3.SetPriority(EPriorityLess);
       
   592 	thread3.Resume();	
       
   593 	testResult = EAllZero;
       
   594 	// Passes control to the second thread
       
   595 	sem.Wait();
       
   596 	thread3.Suspend();
       
   597 
       
   598 	// Test the results in the second thread
       
   599 	TEST(testResult == ECloseSurfaceTestPassed);
       
   600 	
       
   601 	thread3.Resume();
       
   602 	// Wait for a second for the second process to terminate
       
   603 	User::WaitForRequest(statusThreadSecond);
       
   604 	
       
   605 	//Close the handle
       
   606     CleanupStack::PopAndDestroy(1, &sem);
       
   607     thread3.Close();
       
   608 	iSurfaceManager.Close();
       
   609 	}
       
   610 
       
   611 void CTSurfaceManagerMultiThread::TestAccessSurfaceClosedThreadL()
       
   612 	{
       
   613 	User::LeaveIfError(iSurfaceManager.Open());
       
   614     // Logging info
       
   615     INFO_PRINTF1(_L("Test accessing an surface which is created in another thread but closed in current thread\r\n"));
       
   616     
       
   617     // Create a semaphore
       
   618 	RSemaphore sem;
       
   619 	RSemaphore semMain;
       
   620 	
       
   621 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   622     CleanupClosePushL(sem);	
       
   623     TEST(KErrNone == semMain.CreateGlobal(KMainThreadSemaphore, 0));
       
   624     CleanupClosePushL(semMain);	
       
   625 
       
   626 	// Creates a second thread in the current process and opens the handle
       
   627 	iInfo.iSurfaceManager = iSurfaceManager;
       
   628 	iInfo.iSurfaceId = iSurfaceId;
       
   629 	iInfo.iThreadTestCase = ECreateSurfaceMapInfo;	
       
   630 	// Create a TCleanupItem object
       
   631     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   632 
       
   633     RThread thread4;
       
   634     _LIT(KNameThreadSecond4, "Test_SurfaceManager_ThreadSecond4");
       
   635 	User::LeaveIfError(thread4.Create(KNameThreadSecond4,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   636     CleanupStack::Pop();
       
   637     // Launch the second trhead
       
   638 	TRequestStatus statusThreadSecond;
       
   639 	thread4.Logon(statusThreadSecond);
       
   640 	thread4.SetPriority(EPriorityLess);
       
   641 	thread4.Resume();	
       
   642     
       
   643     testResult = EAllZero;
       
   644   	// Passes control to the second process
       
   645     sem.Wait(); 
       
   646 	// Test creating surface in second thread
       
   647    	TEST(testResult == ECreateSurfaceTestPassed);
       
   648    	// Reset testResult
       
   649     testResult = EAllZero;
       
   650     // Test closing surface
       
   651     iSurfaceId = globalSurfaceId;
       
   652 	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
   653 	semMain.Signal();
       
   654     // Wait the second thread to terminate
       
   655     User::WaitForRequest(statusThreadSecond);
       
   656 
       
   657 	// Test mapsurface and surfaceinfo executed in the second thread
       
   658 	TEST(testResult == (EMapSurfaceTestFailed|ESurfaceInfoTestFailed));
       
   659 	// Close all the handles
       
   660     CleanupStack::PopAndDestroy(2, &sem);
       
   661     thread4.Close();
       
   662 	iSurfaceManager.Close();
       
   663 
       
   664 	}
       
   665 
       
   666 void CTSurfaceManagerMultiThread::TestCloseSurfaceExistingSharedChunkL()
       
   667     {
       
   668     // Logging info   
       
   669     INFO_PRINTF1(_L("Test closing surface created in the existing chunk in another thread without opening it\r\n"));
       
   670  
       
   671     User::LeaveIfError(iSurfaceManager.Open());
       
   672     // Setup attributes 
       
   673     
       
   674     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   675     RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   676 
       
   677     attributes.iSize = TSize(100,100);
       
   678     attributes.iBuffers = 1;                // number of buffers in the surface
       
   679     attributes.iPixelFormat = EUidPixelFormatYUV_422SemiPlanar;     // 2bpp
       
   680     attributes.iStride = 400;               // Number of bytes between start of one line and start of next
       
   681     attributes.iOffsetToFirstBuffer = 0;    // way of reserving space before the surface pixel data
       
   682     attributes.iAlignment = 2;          // alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   683 
       
   684     RSurfaceManager::THintPair hints[2];    // two hint pairs specified
       
   685     attributes.iHintCount = 2;
       
   686     attributes.iSurfaceHints = hints;
       
   687     hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   688     hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   689 
       
   690     attributes.iContiguous = ETrue;
       
   691     attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   692     attributes.iOffsetBetweenBuffers = 0;
       
   693     attributes.iMappable = ETrue;
       
   694     
       
   695     // Test create surface doesn't return an error
       
   696     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   697     
       
   698     //Map the surface in the current processs
       
   699     RChunk handle;
       
   700     TEST(KErrNone == iSurfaceManager.MapSurface(iSurfaceId,handle));
       
   701     
       
   702     // Cache, Contiguous and Alignment attributes are ignored for the already existing chunks
       
   703     RSurfaceManager::TSurfaceCreationAttributesBuf buff;
       
   704     RSurfaceManager::TSurfaceCreationAttributes& attributesNew = buff();
       
   705 
       
   706     attributesNew.iSize = TSize(480,16);
       
   707     attributesNew.iBuffers = 2;             // number of buffers in the surface
       
   708     attributesNew.iPixelFormat = EUidPixelFormatYUV_422Reversed;        // 2bpp
       
   709     attributesNew.iStride = 1013;               // Number of bytes between start of one line and start of next
       
   710     attributesNew.iOffsetToFirstBuffer = 0; // way of reserving space before the surface pixel data
       
   711     
       
   712     attributesNew.iHintCount = 1;
       
   713     attributesNew.iSurfaceHints = hints;
       
   714     hints[0].Set(TUid::Uid(0x124545), 50, EFalse);
       
   715 
       
   716     attributesNew.iAlignment = 2;
       
   717     attributesNew.iOffsetBetweenBuffers = 0;
       
   718     attributesNew.iMappable = ETrue;
       
   719 
       
   720     // Test create surface doesn't return an error
       
   721     // For the time being KErrArgument will be returned as the core codes are
       
   722     // not ready to check the passed in shared chunk handle.
       
   723     TEST(KErrNone == iSurfaceManager.CreateSurface(buff, iSurfaceIdNew, handle));
       
   724 
       
   725     // Create a semaphore
       
   726     RSemaphore sem;
       
   727     TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   728     CleanupClosePushL(sem); 
       
   729     
       
   730     // Create a second thread in the current process and opens the handle
       
   731     iInfo.iSurfaceManager = iSurfaceManager;
       
   732     iInfo.iSurfaceId = iSurfaceId;
       
   733     iInfo.iSurfaceIdNew = iSurfaceIdNew;
       
   734     iInfo.iThreadTestCase = ECloseBothSurfaces; 
       
   735     // Create a TCleanupItem object
       
   736     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   737         
       
   738     RThread thread5;
       
   739     _LIT(KNameThreadSecond5, "Test_SurfaceManager_ThreadSecond5");
       
   740     User::LeaveIfError(thread5.Create(KNameThreadSecond5,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   741     CleanupStack::Pop();
       
   742     // Launch second thread
       
   743     TRequestStatus statusThreadSecond;
       
   744     thread5.Logon(statusThreadSecond);
       
   745     thread5.SetPriority(EPriorityLess);
       
   746     thread5.Resume();   
       
   747     testResult = EAllZero;
       
   748     // Passes control to the second process
       
   749     sem.Wait();
       
   750     thread5.Suspend();
       
   751 
       
   752     // Test results in second thread
       
   753     TEST (testResult = (ECloseSurfaceTestPassed | ESurfaceInfoTestPassed | ESurfaceInfoTestPassed2));
       
   754     RSurfaceManager::TInfoBuf infoBuf;
       
   755     TEST (KErrArgument == iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf));
       
   756     TEST (KErrArgument == iSurfaceManager.SurfaceInfo(iSurfaceIdNew, infoBuf));
       
   757 
       
   758     // Pass control off to the second process again
       
   759     thread5.Resume();
       
   760 
       
   761     // Wait for a second for the second thread to terminate
       
   762     User::WaitForRequest(statusThreadSecond);
       
   763 
       
   764     CleanupStack::PopAndDestroy(1, &sem);  //invoke surface manager to close the surface also close the handle to RSemaphore
       
   765     handle.Close();
       
   766     thread5.Close();
       
   767     iSurfaceManager.Close();
       
   768     }
       
   769 
       
   770 void CTSurfaceManagerMultiThread::TestCloseSurfaceClosedThreadL()
       
   771 	{
       
   772 	User::LeaveIfError(iSurfaceManager.Open());
       
   773     
       
   774 	// Setup attributes
       
   775     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   776 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   777 	
       
   778 	attributes.iSize = TSize(480,16);
       
   779 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   780 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   781 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   782 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   783 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   784 
       
   785 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
   786 	attributes.iHintCount = 2;
       
   787 	attributes.iSurfaceHints = hints;
       
   788 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   789 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   790 	
       
   791 	attributes.iContiguous = ETrue;
       
   792 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   793 	attributes.iOffsetBetweenBuffers = 0;
       
   794 	attributes.iMappable = ETrue;
       
   795     // Logging info
       
   796     INFO_PRINTF1(_L("Test closing an surface which is already closed in other thread return KErrArgument\r\n"));
       
   797     // Create a semaphore
       
   798 	RSemaphore sem;
       
   799 	RSemaphore semMain;
       
   800 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   801     CleanupClosePushL(sem);	
       
   802     TEST(KErrNone == semMain.CreateGlobal(KMainThreadSemaphore, 0));
       
   803     CleanupClosePushL(semMain);	
       
   804 
       
   805  	// Creates a second thread in the current process and opens the handle
       
   806 	iInfo.iSurfaceManager = iSurfaceManager;
       
   807 	iInfo.iSurfaceId = iSurfaceId;
       
   808 	iInfo.iThreadTestCase = ECreateSurfaceClose;	
       
   809 	
       
   810 	// Create a TCleanupItem object
       
   811     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   812 
       
   813     RThread thread6;
       
   814     _LIT(KNameThreadSecond6, "Test_SurfaceManager_ThreadSecond6");
       
   815 	User::LeaveIfError(thread6.Create(KNameThreadSecond6,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   816     CleanupStack::Pop();
       
   817     // Launch second thread
       
   818 	TRequestStatus statusThreadSecond;
       
   819 	thread6.Logon(statusThreadSecond);
       
   820 	thread6.SetPriority(EPriorityLess);
       
   821 	thread6.Resume();	
       
   822 	testResult = EAllZero;
       
   823   	// Passes control to the second thread
       
   824     sem.Wait(); 
       
   825     // Test creating surface in second thread
       
   826    	TEST(testResult == ECreateSurfaceTestPassed);
       
   827    	testResult = EAllZero;
       
   828     // Test closing surface
       
   829     iSurfaceId = globalSurfaceId;
       
   830 	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
   831     	
       
   832 	semMain.Signal();
       
   833 	// Wait for a second for the second process to terminate
       
   834     User::WaitForRequest(statusThreadSecond);
       
   835 
       
   836     TEST(testResult == ECloseSurfaceTestFailed);
       
   837     // Close all the handles
       
   838     CleanupStack::PopAndDestroy(2, &sem);
       
   839     thread6.Close();
       
   840 	iSurfaceManager.Close();
       
   841 	}
       
   842 
       
   843 void CTSurfaceManagerMultiThread::TestAccessSurfaceOpenedClosedThreadL()
       
   844 	{
       
   845 	User::LeaveIfError(iSurfaceManager.Open());
       
   846     
       
   847 	// Setup attributes
       
   848     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   849 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   850 	
       
   851 	attributes.iSize = TSize(480,16);
       
   852 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   853 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   854 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   855 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   856 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   857 
       
   858 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
   859 	attributes.iHintCount = 2;
       
   860 	attributes.iSurfaceHints = hints;
       
   861 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   862 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   863 	
       
   864 	attributes.iContiguous = ETrue;
       
   865 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   866 	attributes.iOffsetBetweenBuffers = 0;
       
   867 	attributes.iMappable = ETrue;
       
   868 	
       
   869 	// Test create surface doesn't return an error
       
   870 	TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   871     // Logging info
       
   872     INFO_PRINTF1(_L("Test accessing an surface which is already opened and closed in another thread, given that the process still owns the surface\r\n"));
       
   873  	// Create a semaphore
       
   874 	RSemaphore sem;
       
   875 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   876     CleanupClosePushL(sem);	
       
   877 
       
   878 	// Creates a second thread in the current process and opens the handle
       
   879 	iInfo.iSurfaceManager = iSurfaceManager;
       
   880 	iInfo.iSurfaceId = iSurfaceId;
       
   881 	iInfo.iThreadTestCase = EOpenCloseSurface;	
       
   882  	// Create a TCleanupItem object
       
   883     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   884 	
       
   885     RThread thread7;
       
   886     _LIT(KNameThreadSecond7, "Test_SurfaceManager_ThreadSecond7");
       
   887 	User::LeaveIfError(thread7.Create(KNameThreadSecond7,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   888    	CleanupStack::Pop();
       
   889    	
       
   890     // Launch the second thread
       
   891 	TRequestStatus statusThreadSecond;
       
   892 	thread7.Logon(statusThreadSecond);
       
   893 	thread7.SetPriority(EPriorityLess);
       
   894 	thread7.Resume();
       
   895 	testResult = EAllZero;
       
   896 	sem.Wait();	  
       
   897 	
       
   898 	thread7.Suspend();
       
   899 	// Test open and close surface in the second thread
       
   900 	TEST(testResult == (EOpenSurfaceTestPassed|ECloseSurfaceTestPassed));
       
   901 	
       
   902 	RChunk handle;
       
   903 	// To prove killing threads doesnt affect the ref count...
       
   904 	TEST(KErrNone == iSurfaceManager.MapSurface(iSurfaceId, handle));
       
   905 	handle.Close();
       
   906 	
       
   907 	RSurfaceManager::TInfoBuf infoBuf;
       
   908 	TEST(KErrNone ==iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf));
       
   909 	
       
   910 	thread7.Resume();
       
   911 	
       
   912 	// Wait for a second for the second process to terminate
       
   913     User::WaitForRequest(statusThreadSecond);
       
   914 
       
   915 	// Close all the handles
       
   916     CleanupStack::PopAndDestroy(1, &sem);
       
   917     thread7.Close();
       
   918 	iSurfaceManager.Close();
       
   919 	}	
       
   920 
       
   921 void CTSurfaceManagerMultiThread::TestCloseSurfaceOpenedClosedThreadL()
       
   922 	{
       
   923 	User::LeaveIfError(iSurfaceManager.Open());
       
   924     
       
   925 	// Setup attributes
       
   926     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   927 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   928 	
       
   929 	attributes.iSize = TSize(480,16);
       
   930 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   931 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   932 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   933 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   934 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
   935 
       
   936 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
   937 	attributes.iHintCount = 2;
       
   938 	attributes.iSurfaceHints = hints;
       
   939 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
   940 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
   941 	
       
   942 	attributes.iContiguous = ETrue;
       
   943 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
   944 	attributes.iOffsetBetweenBuffers = 0;
       
   945 	attributes.iMappable = ETrue;
       
   946 
       
   947 	// Test create surface doesn't return an error
       
   948     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
   949     
       
   950     INFO_PRINTF1(_L("Test closing an surface which is already opened and closed in another thread, given that the process still owns the surface\r\n"));
       
   951 	RSemaphore sem;
       
   952 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
   953    	CleanupClosePushL(sem);
       
   954 
       
   955 	// Creates a second thread in the current process and opens the handle
       
   956 	iInfo.iSurfaceManager = iSurfaceManager;
       
   957 	iInfo.iSurfaceId = iSurfaceId;
       
   958 	iInfo.iThreadTestCase = EOpenCloseSurface;	
       
   959  
       
   960 	// Create a TCleanupItem object
       
   961     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
   962 
       
   963     RThread thread8;
       
   964     _LIT(KNameThreadSecond8, "Test_SurfaceManager_ThreadSecond8");
       
   965 	User::LeaveIfError(thread8.Create(KNameThreadSecond8,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
   966     CleanupStack::Pop();
       
   967 	TRequestStatus statusThreadSecond;
       
   968 	thread8.Logon(statusThreadSecond);
       
   969 	thread8.SetPriority(EPriorityLess);
       
   970 	thread8.Resume();
       
   971 	testResult = EAllZero;
       
   972 	sem.Wait();	
       
   973 	thread8.Suspend();
       
   974 	// Test open and close surface in the second thread
       
   975 	TEST(testResult == (EOpenSurfaceTestPassed|ECloseSurfaceTestPassed));
       
   976 
       
   977 	thread8.Resume();
       
   978 	// Wait for a second for the second process to terminate
       
   979     User::WaitForRequest(statusThreadSecond);
       
   980 
       
   981 	// Close all the handles 
       
   982     CleanupStack::PopAndDestroy(1, &sem);
       
   983     thread8.Close();
       
   984 	iSurfaceManager.Close();
       
   985 	}
       
   986 
       
   987 void CTSurfaceManagerMultiThread::TestAccessSurfaceOpenedKilledThreadL()
       
   988 	{
       
   989 	User::LeaveIfError(iSurfaceManager.Open());
       
   990     
       
   991 	// Setup attributes
       
   992     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
   993 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
   994 	attributes.iSize = TSize(480,16);
       
   995 	attributes.iBuffers = 2;				// number of buffers in the surface
       
   996 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
   997 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
   998 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   999 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1000 
       
  1001 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1002 	attributes.iHintCount = 2;
       
  1003 	attributes.iSurfaceHints = hints;
       
  1004 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1005 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1006 	
       
  1007 	attributes.iContiguous = ETrue;
       
  1008 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1009 	attributes.iOffsetBetweenBuffers = 0;
       
  1010 	attributes.iMappable = ETrue;
       
  1011 
       
  1012 	// Test create surface doesn't return an error
       
  1013     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
  1014     
       
  1015     INFO_PRINTF1(_L("Test accessing surface in another thread when the opening thread is killed\r\n"));
       
  1016     // Create a semaphore
       
  1017 	RSemaphore sem;
       
  1018 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1019 	CleanupClosePushL(sem);
       
  1020 
       
  1021 	// Creates a second thread in the current process and opens the handle
       
  1022 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1023 	iInfo.iSurfaceId = iSurfaceId;
       
  1024 	iInfo.iThreadTestCase = EOpenKillSurface;	
       
  1025 	// Create a TCleanupItem object
       
  1026     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1027 
       
  1028     RThread thread9;
       
  1029     _LIT(KNameThreadSecond9, "Test_SurfaceManager_ThreadSecond9");
       
  1030 	User::LeaveIfError(thread9.Create(KNameThreadSecond9,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1031 	CleanupStack::Pop();
       
  1032 	// Launch the second thraed
       
  1033 	TRequestStatus statusThreadSecond;
       
  1034 	thread9.Logon(statusThreadSecond);
       
  1035 	thread9.SetPriority(EPriorityLess);
       
  1036 	thread9.Resume();
       
  1037 	// Waiting the second thread to terminate
       
  1038     User::WaitForRequest(statusThreadSecond);
       
  1039 
       
  1040    	RChunk handle;
       
  1041 	// Test the surface is still accessible
       
  1042 	TEST(KErrNone == iSurfaceManager.MapSurface(iSurfaceId, handle));
       
  1043 	handle.Close();
       
  1044 	
       
  1045 	RSurfaceManager::TInfoBuf infoBuf;
       
  1046 	TEST(KErrNone ==iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf));
       
  1047 	
       
  1048     // Close all the handles
       
  1049     CleanupStack::PopAndDestroy(1, &sem);
       
  1050     thread9.Close();
       
  1051 	iSurfaceManager.Close();
       
  1052 	}
       
  1053 
       
  1054 void CTSurfaceManagerMultiThread::TestCloseSurfaceOpenedKilledThreadL()
       
  1055 	{
       
  1056 	User::LeaveIfError(iSurfaceManager.Open());
       
  1057     
       
  1058 	// Setup attributes
       
  1059     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1060 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1061 	attributes.iSize = TSize(480,16);
       
  1062 	attributes.iBuffers = 2;				// number of buffers in the surface
       
  1063 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
  1064 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
  1065 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
  1066 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1067 
       
  1068 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1069 	attributes.iHintCount = 2;
       
  1070 	attributes.iSurfaceHints = hints;
       
  1071 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1072 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1073 	
       
  1074 	attributes.iContiguous = ETrue;
       
  1075 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1076 	attributes.iOffsetBetweenBuffers = 0;
       
  1077 	attributes.iMappable = ETrue;
       
  1078 	
       
  1079 	// Create a semaphore
       
  1080 	RSemaphore sem;
       
  1081 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1082 	CleanupClosePushL(sem);
       
  1083 	
       
  1084 	// Test create surface doesn't return an error
       
  1085     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
  1086     // Logging info
       
  1087     INFO_PRINTF1(_L("Test closing surface in another thread when the opening thread is killed\r\n"));
       
  1088   	// Creates a second thread in the current process and opens the handle
       
  1089 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1090 	iInfo.iSurfaceId = iSurfaceId;
       
  1091 	iInfo.iThreadTestCase = EOpenKillSurface;	
       
  1092 
       
  1093 	// Create a TCleanupItem object
       
  1094     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1095 
       
  1096     RThread thread10;
       
  1097     _LIT(KNameThreadSecond10, "Test_SurfaceManager_ThreadSecond10");
       
  1098 	User::LeaveIfError(thread10.Create(KNameThreadSecond10,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1099    	CleanupStack::Pop();
       
  1100 	// Launch the second thread
       
  1101 	TRequestStatus statusThreadSecond;
       
  1102 	thread10.Logon(statusThreadSecond);
       
  1103 	thread10.SetPriority(EPriorityLess);
       
  1104 	thread10.Resume();
       
  1105 	// Waiting the second thread to terminate
       
  1106     User::WaitForRequest(statusThreadSecond);
       
  1107 
       
  1108     // Test closing surface
       
  1109 	TEST(KErrNone ==iSurfaceManager.CloseSurface(iSurfaceId));
       
  1110 	
       
  1111 	// Close all the handles
       
  1112 	CleanupStack::PopAndDestroy(1, &sem);
       
  1113 	thread10.Close();
       
  1114 	iSurfaceManager.Close();
       
  1115 	}
       
  1116 
       
  1117 void CTSurfaceManagerMultiThread::TestAccessSurfaceCreateKilledThreadL()
       
  1118 	{
       
  1119 	User::LeaveIfError(iSurfaceManager.Open());
       
  1120     
       
  1121 	// Setup attributes
       
  1122     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1123 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1124 	attributes.iSize = TSize(480,16);
       
  1125 	attributes.iBuffers = 2;				// number of buffers in the surface
       
  1126 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
  1127 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
  1128 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
  1129 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1130 
       
  1131 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1132 	attributes.iHintCount = 2;
       
  1133 	attributes.iSurfaceHints = hints;
       
  1134 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1135 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1136 	
       
  1137 	attributes.iContiguous = ETrue;
       
  1138 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1139 	attributes.iOffsetBetweenBuffers = 0;
       
  1140 	attributes.iMappable = ETrue;
       
  1141 	
       
  1142     // Logging info
       
  1143     INFO_PRINTF1(_L("Test accessing surface in another thread when the creating thread is killed\r\n"));
       
  1144     // Create a semaphore
       
  1145 	RSemaphore sem;
       
  1146 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1147 	CleanupClosePushL(sem);
       
  1148 
       
  1149   	// Creates a second thread in the current process and opens the handle
       
  1150 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1151 	iInfo.iSurfaceId = iSurfaceId;
       
  1152 	iInfo.iThreadTestCase = ECreateKillSurface;	
       
  1153 
       
  1154 	// Create a TCleanupItem object
       
  1155     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1156 
       
  1157     RThread thread11;
       
  1158     _LIT(KNameThreadSecond11, "Test_SurfaceManager_ThreadSecond11");
       
  1159 	User::LeaveIfError(thread11.Create(KNameThreadSecond11,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1160    	CleanupStack::Pop();
       
  1161     // Launch the second thread
       
  1162 	TRequestStatus statusThreadSecond;
       
  1163 	thread11.Logon(statusThreadSecond);
       
  1164 	thread11.SetPriority(EPriorityLess);
       
  1165 	thread11.Resume();
       
  1166 	testResult = EAllZero;
       
  1167 	sem.Wait();	
       
  1168 	// Test creating surface in the second thread
       
  1169 	TEST(testResult = ECreateSurfaceTestPassed);
       
  1170     // Test closing surface
       
  1171    	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
  1172     // Wait the second thread to terminate
       
  1173     User::WaitForRequest(statusThreadSecond);
       
  1174 
       
  1175     // Test the surface is not accessible any more
       
  1176    	RChunk handle;
       
  1177 	TEST(KErrArgument == iSurfaceManager.MapSurface(globalSurfaceId, handle));
       
  1178 	handle.Close();
       
  1179 	RSurfaceManager::TInfoBuf infoBuf;
       
  1180 	TEST(KErrArgument ==iSurfaceManager.SurfaceInfo(globalSurfaceId, infoBuf));
       
  1181  	// Close all the handles
       
  1182 	CleanupStack::PopAndDestroy(1, &sem);
       
  1183 	thread11.Close();
       
  1184 	iSurfaceManager.Close();
       
  1185     }
       
  1186 	
       
  1187 /**
       
  1188 	210 Test closing surface in another thread when the creating thread is killed
       
  1189 	Thread 2: Create Surface 
       
  1190 	Thread 1: Close Surface
       
  1191 	Thread 2: Kill the thread
       
  1192 	Thread 1: Close Surface - KErrArgument
       
  1193 */
       
  1194 void CTSurfaceManagerMultiThread::TestCloseSurfaceCreateKilledThreadL()
       
  1195 	{
       
  1196 	User::LeaveIfError(iSurfaceManager.Open());
       
  1197     
       
  1198 	// Setup attributes
       
  1199     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1200 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1201 	attributes.iSize = TSize(480,16);
       
  1202 	attributes.iBuffers = 2;				// number of buffers in the surface
       
  1203 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
  1204 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
  1205 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
  1206 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1207 
       
  1208 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1209 	attributes.iHintCount = 2;
       
  1210 	attributes.iSurfaceHints = hints;
       
  1211 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1212 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1213 		
       
  1214 	attributes.iContiguous = ETrue;
       
  1215 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1216 	attributes.iOffsetBetweenBuffers = 0;
       
  1217 	attributes.iMappable = ETrue;
       
  1218 
       
  1219     // Logging info
       
  1220     INFO_PRINTF1(_L("Test closing surface in another thread when the creating thread is killed\r\n"));
       
  1221     // Create a semaphore
       
  1222 	RSemaphore sem;
       
  1223 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1224 	CleanupClosePushL(sem);
       
  1225 
       
  1226   	// Creates a second thread in the current process and opens the handle
       
  1227 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1228 	iInfo.iSurfaceId = iSurfaceId;
       
  1229 	iInfo.iThreadTestCase = ECreateKillSurface;	
       
  1230 
       
  1231 	// Create a TCleanupItem object
       
  1232     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1233 
       
  1234     RThread thread12;
       
  1235     _LIT(KNameThreadSecond12, "Test_SurfaceManager_ThreadSecond12");
       
  1236 	User::LeaveIfError(thread12.Create(KNameThreadSecond12,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1237    	CleanupStack::Pop();
       
  1238     // Launch the second thread
       
  1239 	TRequestStatus statusThreadSecond;
       
  1240 	thread12.Logon(statusThreadSecond);
       
  1241 	thread12.SetPriority(EPriorityLess);
       
  1242 	thread12.Resume();
       
  1243 	sem.Wait();	
       
  1244 	// Test creating surface in the second thread
       
  1245 	TEST(testResult = ECreateSurfaceTestPassed);
       
  1246     // Test closing surface
       
  1247    	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
  1248     // Wait the second thread to terminate
       
  1249     User::WaitForRequest(statusThreadSecond);
       
  1250      
       
  1251 	// Test the surface is removed and not closable   	
       
  1252 	TEST(KErrArgument ==iSurfaceManager.CloseSurface(globalSurfaceId));
       
  1253     // Close the handles
       
  1254    	CleanupStack::PopAndDestroy(1, &sem);
       
  1255    	thread12.Close();
       
  1256 	iSurfaceManager.Close();
       
  1257 	}
       
  1258 	
       
  1259 void CTSurfaceManagerMultiThread::TestAccessSurfaceThreeThreadsL()
       
  1260 	{
       
  1261 	User::LeaveIfError(iSurfaceManager.Open());
       
  1262     
       
  1263 	// Setup attributes
       
  1264     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1265 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1266 	attributes.iSize = TSize(480,16);
       
  1267 	attributes.iBuffers = 2;				// number of buffers in the surface
       
  1268 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
  1269 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
  1270 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
  1271 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1272 
       
  1273 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1274 	attributes.iHintCount = 2;
       
  1275 	attributes.iSurfaceHints = hints;
       
  1276 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1277 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1278 	
       
  1279 	attributes.iContiguous = ETrue;
       
  1280 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1281 	attributes.iOffsetBetweenBuffers = 0;
       
  1282 	attributes.iMappable = ETrue;
       
  1283 	
       
  1284 	// Test create surface doesn't return an error
       
  1285     TEST(KErrNone == iSurfaceManager.CreateSurface(buf, iSurfaceId));
       
  1286     // Logging info
       
  1287     INFO_PRINTF1(_L("Test closing on one thread doesn't prevent opening on other Threads, provided one Thread still owns surface\r\n"));
       
  1288     // Create a semaphore
       
  1289 	RSemaphore sem;
       
  1290 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore,0));
       
  1291 	CleanupClosePushL(sem);
       
  1292 
       
  1293    	// Creates a second thread in the current process and opens the handle
       
  1294 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1295 	iInfo.iSurfaceId = iSurfaceId;
       
  1296 	iInfo.iThreadTestCase = EOpenCloseSurfaceMultiThread;	
       
  1297 	// Create a TCleanupItem object
       
  1298     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1299 
       
  1300     RThread thread13;
       
  1301     _LIT(KNameThreadSecond13, "Test_SurfaceManager_ThreadSecond13");
       
  1302 	User::LeaveIfError(thread13.Create(KNameThreadSecond13,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1303    	CleanupStack::Pop();
       
  1304     // Launch the second thread
       
  1305 	TRequestStatus statusThreadSecond;
       
  1306 	thread13.Logon(statusThreadSecond);
       
  1307 	thread13.SetPriority(EPriorityLess);
       
  1308 	thread13.Resume();
       
  1309 	testResult = EAllZero;
       
  1310 	sem.Wait();	
       
  1311 	TEST(testResult == EOpenSurfaceTestPassed);
       
  1312 	testResult = EAllZero;
       
  1313 
       
  1314 	// Create a third thread in the same process and opens the handle
       
  1315 	iInfo2 = iInfo;
       
  1316 	iInfo2.iThreadTestCase = EOpenMapSurfaceInfoMultiThread;	
       
  1317 	// Create a TCleanupItem object
       
  1318     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo2));
       
  1319 
       
  1320     RThread thread13_2;
       
  1321     _LIT(KNameThreadSecond13_2, "Test_SurfaceManager_ThreadSecond13_2");
       
  1322 	User::LeaveIfError(thread13_2.Create(KNameThreadSecond13_2,ThreadThirdStart, KDefaultStackSize, &User::Heap(), &iInfo2));
       
  1323 	CleanupStack::Pop();
       
  1324 	// Launch the third thread
       
  1325 	TRequestStatus statusThreadThird;
       
  1326 	thread13_2.Logon(statusThreadThird);
       
  1327 	thread13_2.SetPriority(EPriorityLess);
       
  1328 	thread13_2.Resume();
       
  1329 	sem.Wait();
       
  1330 	TEST(testResult == EOpenSurfaceTestPassed);
       
  1331 	testResult = EAllZero;
       
  1332 	thread13.Resume();
       
  1333     User::WaitForRequest(statusThreadSecond);
       
  1334 
       
  1335 	TEST(testResult == ECloseSurfaceTestPassed);
       
  1336 	testResult = EAllZero;
       
  1337 
       
  1338 	thread13_2.Resume();
       
  1339     User::WaitForRequest(statusThreadThird);
       
  1340 
       
  1341 	TEST(testResult == (EMapSurfaceTestPassed|ESurfaceInfoTestPassed));
       
  1342 
       
  1343 	// Close all the handles
       
  1344 	CleanupStack::PopAndDestroy(1, &sem);
       
  1345 
       
  1346 	thread13.Close();
       
  1347 	thread13_2.Close();
       
  1348 	iSurfaceManager.Close();
       
  1349 	}
       
  1350 
       
  1351 void CTSurfaceManagerMultiThread::TestAccessSurfaceDieCloseOtherThreadsL()
       
  1352 	{
       
  1353 	User::LeaveIfError(iSurfaceManager.Open());
       
  1354     
       
  1355     // Logging info
       
  1356     INFO_PRINTF1(_L("Test surface can be accessed from third thread when creating thread dies and second thread closes\r\n"));
       
  1357     // Create a semaphore
       
  1358 	RSemaphore sem;
       
  1359 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1360 	CleanupClosePushL(sem);
       
  1361 
       
  1362 	// Create a second thread and opens the handle
       
  1363 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1364 	iInfo.iSurfaceId = iSurfaceId;
       
  1365 	iInfo.iThreadTestCase = ECreateKillSurface;	
       
  1366 
       
  1367 	// Create a TCleanupItem object
       
  1368     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1369 
       
  1370     RThread thread14;
       
  1371     _LIT(KNameThreadSecond14, "Test_SurfaceManager_ThreadSecond14");
       
  1372 	User::LeaveIfError(thread14.Create(KNameThreadSecond14,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1373    	CleanupStack::Pop();
       
  1374 	// Launch the second thread
       
  1375 	TRequestStatus statusThreadSecond;
       
  1376 	thread14.Logon(statusThreadSecond);
       
  1377 	thread14.SetPriority(EPriorityLess);
       
  1378 	thread14.Resume();
       
  1379 	testResult = EAllZero;
       
  1380 	sem.Wait();	
       
  1381 	//suspend the 2nd thread
       
  1382 	thread14.Suspend(); 
       
  1383 	
       
  1384 	TEST(testResult == ECreateSurfaceTestPassed);
       
  1385 	testResult = EAllZero;
       
  1386 	
       
  1387 	TEST(KErrNone == iSurfaceManager.OpenSurface(globalSurfaceId));
       
  1388 
       
  1389     // Create a third thread in the current process and opens the handle 
       
  1390 	iInfo2.iSurfaceManager = iSurfaceManager;
       
  1391 	iInfo2.iSurfaceId = globalSurfaceId;
       
  1392 	iInfo2.iSurfaceIdNew = iSurfaceId;
       
  1393 	iInfo2.iThreadTestCase = EOpenCloseMapSurfaceInfoMultiThread;	
       
  1394 	// Create a TCleanupItem object
       
  1395     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo2));
       
  1396 
       
  1397     RThread thread14_2;
       
  1398     _LIT(KNameThreadSecond14_2, "Test_SurfaceManager_ThreadSecond14_2");
       
  1399 	User::LeaveIfError(thread14_2.Create(KNameThreadSecond14_2,ThreadThirdStart, KDefaultStackSize, &User::Heap(), &iInfo2));
       
  1400    	CleanupStack::Pop();
       
  1401     // Launch the third thread
       
  1402 	TRequestStatus statusThreadThird;
       
  1403 	thread14_2.Logon(statusThreadThird);
       
  1404 	thread14_2.SetPriority(EPriorityLess);
       
  1405 	thread14_2.Resume();
       
  1406 	sem.Wait();
       
  1407 	// Test opening the surface in the second thread
       
  1408 	TEST(testResult == EOpenSurfaceTestPassed);
       
  1409 	testResult = EAllZero;
       
  1410     // Test closing the surface in the main thread
       
  1411 	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
  1412 
       
  1413 	thread14.Resume();
       
  1414     User::WaitForRequest(statusThreadSecond);
       
  1415 
       
  1416     thread14_2.Resume();
       
  1417     User::WaitForRequest(statusThreadThird);
       
  1418 
       
  1419 	// Test the execution results in the third thread
       
  1420 	TEST(testResult == (ECloseSurfaceTestPassed|EMapSurfaceTestPassed|ESurfaceInfoTestPassed));	
       
  1421 
       
  1422     // Close all the handles
       
  1423     CleanupStack::PopAndDestroy(1, &sem);
       
  1424     thread14.Close();
       
  1425     thread14_2.Close();
       
  1426 	iSurfaceManager.Close();
       
  1427 
       
  1428 	}
       
  1429 
       
  1430 void CTSurfaceManagerMultiThread::TestOpenSurfaceDieCloseOtherThreadsL()
       
  1431 	{
       
  1432 	User::LeaveIfError(iSurfaceManager.Open());
       
  1433     
       
  1434 	// Setup attributes
       
  1435     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1436 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1437 	attributes.iSize = TSize(480,16);
       
  1438 	attributes.iBuffers = 2;				// number of buffers in the surface
       
  1439 	attributes.iPixelFormat = EUidPixelFormatYUV_422Reversed;		// 2bpp
       
  1440 	attributes.iStride = 1013;				// Number of bytes between start of one line and start of next
       
  1441 	attributes.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
  1442 	attributes.iAlignment = RSurfaceManager::EPageAligned;			// alignment, 1,2,4,8,16,32,64 byte aligned or EPageAligned
       
  1443 
       
  1444 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1445 	attributes.iHintCount = 2;
       
  1446 	attributes.iSurfaceHints = hints;
       
  1447 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1448 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1449 		
       
  1450 	attributes.iContiguous = ETrue;
       
  1451 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1452 	attributes.iOffsetBetweenBuffers = 0;
       
  1453 	attributes.iMappable = ETrue;
       
  1454 
       
  1455     // Logging info
       
  1456     INFO_PRINTF1(_L("Test that the surface can be opened from the third Thread after creating thread dies and second and third thread close the surface\r\n"));
       
  1457     // Create a semaphore
       
  1458 	RSemaphore sem;
       
  1459 	TEST(KErrNone == sem.CreateGlobal(KMultiThreadSemaphore, 0));
       
  1460 	CleanupClosePushL(sem);
       
  1461 
       
  1462     // Create a second thread and opens the handle
       
  1463 	iInfo.iSurfaceManager = iSurfaceManager;
       
  1464 	iInfo.iSurfaceId = iSurfaceId;
       
  1465 	iInfo.iThreadTestCase = ECreateKillSurface;	
       
  1466 
       
  1467 	// Create a TCleanupItem object
       
  1468     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo));
       
  1469     RThread thread15;
       
  1470     _LIT(KNameThreadSecond15, "Test_SurfaceManager_ThreadSecond15");
       
  1471 	User::LeaveIfError(thread15.Create(KNameThreadSecond15,ThreadSecondStart, KDefaultStackSize, &User::Heap(), &iInfo));
       
  1472    	CleanupStack::Pop();
       
  1473    	// Launch the second thread
       
  1474 	TRequestStatus statusThreadSecond;
       
  1475 	thread15.Logon(statusThreadSecond);
       
  1476 	thread15.SetPriority(EPriorityLess);
       
  1477 	thread15.Resume();
       
  1478 	testResult = EAllZero;
       
  1479 	sem.Wait();	
       
  1480 
       
  1481 	thread15.Suspend(); //suspend the 2nd thread
       
  1482 
       
  1483 	TEST(testResult == ECreateSurfaceTestPassed);
       
  1484 	testResult = EAllZero;
       
  1485 	// Test creating the surface in the second thread	
       
  1486 	TEST(KErrNone == iSurfaceManager.OpenSurface(globalSurfaceId));
       
  1487 
       
  1488 	// Create a third thread and opens the handle
       
  1489 	iInfo2.iSurfaceManager = iSurfaceManager;
       
  1490 	iInfo2.iSurfaceId = globalSurfaceId;
       
  1491 	iInfo2.iSurfaceIdNew = iSurfaceId;
       
  1492 	iInfo2.iThreadTestCase = EOpenCloseOpenMultiThread;	
       
  1493 
       
  1494 	// Create a TCleanupItem object
       
  1495     CleanupStack::PushL(TCleanupItem((TCleanupOperation)CloseSurfaceWhenLeave, &iInfo2));
       
  1496     RThread thread15_2;
       
  1497     _LIT(KNameThreadSecond15_2, "Test_SurfaceManager_ThreadSecond15_2");
       
  1498 	User::LeaveIfError(thread15_2.Create(KNameThreadSecond15_2,ThreadThirdStart, KDefaultStackSize, &User::Heap(), &iInfo2));
       
  1499    	CleanupStack::Pop();
       
  1500 	// Launch the third thread
       
  1501 	TRequestStatus statusThreadThird;
       
  1502 	thread15_2.Logon(statusThreadThird);
       
  1503 	thread15_2.SetPriority(EPriorityLess);
       
  1504 	thread15_2.Resume();
       
  1505 	sem.Wait();
       
  1506 	// Test opening the surface in the third thread
       
  1507 	TEST(testResult == EOpenSurfaceTestPassed);
       
  1508 	// Test closing the surface in the main thread	
       
  1509 	TEST(KErrNone == iSurfaceManager.CloseSurface(globalSurfaceId));
       
  1510 	thread15.Resume();
       
  1511     User::WaitForRequest(statusThreadSecond);
       
  1512 
       
  1513     thread15_2.Resume();
       
  1514     User::WaitForRequest(statusThreadThird);
       
  1515 
       
  1516     // Test the surface is still accessible in the third thread
       
  1517 	TEST(testResult == (ECloseSurfaceTestPassed | EOpenSurfaceTestPassed));
       
  1518     // Close the handles
       
  1519 	CleanupStack::PopAndDestroy(1, &sem);
       
  1520 	thread15.Close();
       
  1521 	thread15_2.Close();
       
  1522 	iSurfaceManager.Close();
       
  1523 
       
  1524 	}
       
  1525 	
       
  1526 
       
  1527 /**
       
  1528 The second thread entry point
       
  1529 */
       
  1530 TInt CTSurfaceManagerMultiThread::ThreadSecondStart(TAny* aInfo)
       
  1531 	{
       
  1532 	TInt procHandles1  =0;
       
  1533 	TInt threadHandles1=0;
       
  1534 	RThread().HandleCount(procHandles1, threadHandles1);
       
  1535 	__UHEAP_MARK;
       
  1536 		
       
  1537 	CChildThreadWrapper* newThread = new CChildThreadWrapper(aInfo);
       
  1538 	if (newThread==NULL)
       
  1539 		{
       
  1540 		return KErrNoMemory;
       
  1541 		}
       
  1542 
       
  1543     RSemaphore sem;
       
  1544     RSemaphore semMain;
       
  1545  	TInt ret = sem.OpenGlobal(KMultiThreadSemaphore);
       
  1546     if (ret == KErrNone)
       
  1547     	{
       
  1548    		TThreadTestCase testCase = newThread->iThreadTestCase;
       
  1549 		switch (testCase)
       
  1550 			{
       
  1551 			case ECreateSurfaceMapInfo:
       
  1552 				User::LeaveIfError(semMain.OpenGlobal(KMainThreadSemaphore));
       
  1553 				newThread->CreateSurfaceThread();
       
  1554 				sem.Signal();
       
  1555 				semMain.Wait();
       
  1556 				newThread->MapSurfaceInfo();
       
  1557 				delete newThread;
       
  1558 				semMain.Close();
       
  1559 				break;
       
  1560 			case ECreateSurfaceClose:
       
  1561 				User::LeaveIfError(semMain.OpenGlobal(KMainThreadSemaphore));
       
  1562 				newThread->CreateSurfaceThread();
       
  1563 				sem.Signal();
       
  1564 				semMain.Wait();
       
  1565 				newThread->CloseSurface();
       
  1566 				delete newThread;
       
  1567 				semMain.Close();
       
  1568 				break;
       
  1569 			case EMapSurfaceInfo:
       
  1570 		 		newThread->MapSurfaceInfo();
       
  1571 		 		sem.Signal();
       
  1572 				delete newThread;
       
  1573 				break;
       
  1574 			case ECloseSurfaces:
       
  1575 				newThread->CloseSurface();
       
  1576 				sem.Signal();
       
  1577 				delete newThread;
       
  1578 				break;
       
  1579 			case EOpenCloseSurface:
       
  1580 				newThread->OpenSurface();
       
  1581 				newThread->CloseSurface();
       
  1582 				sem.Signal();
       
  1583 				delete newThread;
       
  1584 				break;
       
  1585 			case EOpenKillSurface:
       
  1586 				newThread->OpenSurface();
       
  1587 				delete newThread;
       
  1588     			sem.Close();
       
  1589     			RThread().Kill(ret);
       
  1590 				break;
       
  1591 			case ECreateKillSurface:
       
  1592 				newThread->CreateSurfaceThread();
       
  1593 				sem.Signal();
       
  1594 				delete newThread;
       
  1595     			sem.Close();
       
  1596     			RThread().Kill(ret);
       
  1597 				break;
       
  1598 			case EOpenCloseSurfaceMultiThread:
       
  1599 				newThread->OpenSurface(); 
       
  1600 				sem.Signal();
       
  1601 				RThread().Suspend();
       
  1602 				newThread->CloseSurface();
       
  1603 				delete newThread;
       
  1604 				sem.Close();
       
  1605 				return ret;
       
  1606 			case ECloseBothSurfaces:
       
  1607 				newThread->CloseBothSurfaces();
       
  1608 				sem.Signal();
       
  1609 				delete newThread;
       
  1610 				break;
       
  1611 			default:
       
  1612 				break;
       
  1613 			}
       
  1614 
       
  1615 		}
       
  1616 
       
  1617 	__UHEAP_MARKEND;
       
  1618      
       
  1619 	sem.Close();	
       
  1620 	TInt procHandles2  =0;
       
  1621 	TInt threadHandles2=0;
       
  1622 	RThread().HandleCount(procHandles2,threadHandles2);
       
  1623 	if (threadHandles1 != threadHandles2)
       
  1624 		{
       
  1625 		ret = KErrGeneral;  // Thread-owned handles not closed
       
  1626 		}
       
  1627 
       
  1628 	return ret;
       
  1629 	}
       
  1630 	
       
  1631 // Implementation of CChildThreadWrapper class 
       
  1632 CChildThreadWrapper::CChildThreadWrapper(TAny* aInfo) :
       
  1633 	iSurfaceManager    (((TInfo*)aInfo)->iSurfaceManager),
       
  1634 	iSurfaceId(((TInfo*)aInfo)->iSurfaceId),
       
  1635 	iSurfaceIdNew(((TInfo*)aInfo)->iSurfaceIdNew),
       
  1636 	iThreadTestCase(((TInfo*)aInfo)->iThreadTestCase)
       
  1637 	{
       
  1638 	}
       
  1639 
       
  1640 CChildThreadWrapper::~CChildThreadWrapper()
       
  1641 	{
       
  1642 	}
       
  1643 
       
  1644 void CChildThreadWrapper::CreateSurfaceThread()
       
  1645 	{
       
  1646 	// Setup attributes
       
  1647     RSurfaceManager::TSurfaceCreationAttributesBuf buf;
       
  1648 	RSurfaceManager::TSurfaceCreationAttributes& attributes = buf();
       
  1649 	
       
  1650 	attributes.iSize = TSize(200,200);
       
  1651 	attributes.iBuffers = 1;				
       
  1652 	attributes.iPixelFormat = EUidPixelFormatARGB_1555;		
       
  1653 	attributes.iStride = 1024;				
       
  1654 	attributes.iOffsetToFirstBuffer = 80;	
       
  1655 	attributes.iAlignment = 8;			
       
  1656 	attributes.iContiguous=ETrue;
       
  1657 
       
  1658 	RSurfaceManager::THintPair hints[2];	// two hint pairs specified
       
  1659 	attributes.iHintCount = 2;
       
  1660 	attributes.iSurfaceHints = hints;
       
  1661 	hints[0].Set(TUid::Uid(0x124578), 25, ETrue);
       
  1662 	hints[1].Set(TUid::Uid(0x237755), 50, ETrue);
       
  1663 
       
  1664 	attributes.iCacheAttrib = RSurfaceManager::ECached;
       
  1665 	attributes.iOffsetBetweenBuffers = 0;
       
  1666 	attributes.iMappable = ETrue;
       
  1667 
       
  1668 	if (KErrNone == iSurfaceManager.CreateSurface(buf,iSurfaceId))
       
  1669 		{
       
  1670 		testResult |= ECreateSurfaceTestPassed;
       
  1671 		}
       
  1672     else
       
  1673     	{
       
  1674     	testResult |= ECreateSurfaceTestFailed;	
       
  1675 	   	}
       
  1676 		
       
  1677 	// record the surfaceId as a global variable
       
  1678 	globalSurfaceId = iSurfaceId;
       
  1679 	}
       
  1680 	
       
  1681 void CChildThreadWrapper::MapSurfaceInfo()
       
  1682 	{
       
  1683 	// Access the surface and pass back the test results to the main thread
       
  1684 	RChunk handle;
       
  1685 	// Call Map Surface
       
  1686 	if (iSurfaceManager.MapSurface(iSurfaceId,handle)== KErrNone)
       
  1687 		{
       
  1688 		testResult |= EMapSurfaceTestPassed;
       
  1689 		}
       
  1690 	else 
       
  1691 		{
       
  1692 		testResult |= EMapSurfaceTestFailed;
       
  1693 		}
       
  1694 	handle.Close();
       
  1695 
       
  1696 	RSurfaceManager::TInfoBuf infoBuf;
       
  1697 	// Call Surface Info
       
  1698 	if (iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf) == KErrNone)
       
  1699 		{
       
  1700 		testResult |= ESurfaceInfoTestPassed;
       
  1701 		}
       
  1702 	else 
       
  1703 		{
       
  1704 		testResult |= ESurfaceInfoTestFailed;
       
  1705 		}
       
  1706 	} 
       
  1707 			   
       
  1708 void CChildThreadWrapper::CloseSurface()
       
  1709 	{
       
  1710 	// Call close surface and pass back the results to the main thread
       
  1711 	if (iSurfaceManager.CloseSurface(iSurfaceId) == KErrNone)
       
  1712 		{
       
  1713 		testResult |= ECloseSurfaceTestPassed;
       
  1714 		}
       
  1715 	else 
       
  1716 		{
       
  1717 		testResult |= ECloseSurfaceTestFailed;
       
  1718 		}
       
  1719 	} 
       
  1720 
       
  1721 void CChildThreadWrapper::OpenSurface()
       
  1722 	{
       
  1723 	// Call close surface and pass back the results to the main thread
       
  1724 	if (iSurfaceManager.OpenSurface(iSurfaceId) == KErrNone)
       
  1725 		{
       
  1726 		testResult |= EOpenSurfaceTestPassed;
       
  1727 		}
       
  1728 	else
       
  1729 		{
       
  1730 		testResult |= EOpenSurfaceTestFailed;
       
  1731 		}
       
  1732 	} 
       
  1733 		 
       
  1734 void CChildThreadWrapper::CloseBothSurfaces()
       
  1735 	{
       
  1736 	// Call close surface and pass back the results to the main thread
       
  1737 	if (iSurfaceManager.CloseSurface(iSurfaceIdNew) == KErrNone)
       
  1738 		{
       
  1739 		testResult |= ECloseSurfaceTestPassed;
       
  1740 		}
       
  1741 	else 
       
  1742 		{
       
  1743 		testResult |= ECloseSurfaceTestFailed;
       
  1744 		}
       
  1745 	RSurfaceManager::TInfoBuf infoBuf;
       
  1746 	// Call Surface Info
       
  1747 	if (iSurfaceManager.SurfaceInfo(iSurfaceId, infoBuf) == KErrNone)
       
  1748 		{
       
  1749 		testResult |= ESurfaceInfoTestPassed;
       
  1750 		}
       
  1751 	else 
       
  1752 		{
       
  1753 		testResult |= ESurfaceInfoTestFailed;
       
  1754 		}
       
  1755 	
       
  1756 	if (iSurfaceManager.SurfaceInfo(iSurfaceIdNew, infoBuf) == KErrArgument)
       
  1757 		{
       
  1758 		testResult |= ESurfaceInfoTestPassed2;
       
  1759 		}
       
  1760 	else 
       
  1761 		{
       
  1762 		testResult |= ESurfaceInfoTestFailed2;
       
  1763 		}
       
  1764 	iSurfaceManager.CloseSurface(iSurfaceId);
       
  1765 	}
       
  1766 /**
       
  1767 The third thread entry point
       
  1768 */
       
  1769 TInt CTSurfaceManagerMultiThread::ThreadThirdStart(TAny* aInfo)
       
  1770 	{
       
  1771 	TInt procHandles1  =0;
       
  1772 	TInt threadHandles1=0;
       
  1773 	RThread().HandleCount(procHandles1, threadHandles1);
       
  1774 	__UHEAP_MARK;
       
  1775 	CTrapCleanup* cleanupStack=CTrapCleanup::New();
       
  1776 	if (cleanupStack==NULL)
       
  1777 		{
       
  1778 		return KErrNoMemory;
       
  1779 		}
       
  1780 		
       
  1781 	CChildThreadWrapper* newThread = new CChildThreadWrapper(aInfo);
       
  1782 	if (newThread==NULL)
       
  1783 		{
       
  1784 		delete cleanupStack;
       
  1785 		return KErrNoMemory;
       
  1786 		}
       
  1787 		
       
  1788 	// Pass control back to the first process
       
  1789     RSemaphore sem;
       
  1790 	TInt ret = sem.OpenGlobal(KMultiThreadSemaphore);
       
  1791 	if (ret!=KErrNone)
       
  1792 		return ret;
       
  1793 		
       
  1794 	TInt err = KErrNone;
       
  1795     TThreadTestCase testCase = newThread->iThreadTestCase;
       
  1796 	switch (testCase)
       
  1797 		{
       
  1798 		case EOpenMapSurfaceInfoMultiThread:
       
  1799 			newThread->OpenSurface();
       
  1800 			sem.Signal();
       
  1801 			RThread().Suspend();
       
  1802 			newThread->MapSurfaceInfo();
       
  1803 			break;
       
  1804 		case EOpenCloseMapSurfaceInfoMultiThread:
       
  1805 			newThread->OpenSurface();
       
  1806 			sem.Signal();
       
  1807 			RThread().Suspend();
       
  1808 			newThread->CloseSurface();
       
  1809 			newThread->MapSurfaceInfo();
       
  1810 			break;
       
  1811 		case EOpenCloseOpenMultiThread:
       
  1812 			newThread->OpenSurface();
       
  1813 			sem.Signal();
       
  1814 			RThread().Suspend();
       
  1815 			newThread->CloseSurface();
       
  1816 			newThread->OpenSurface();
       
  1817 			break;
       
  1818 		default:
       
  1819 			break;
       
  1820 		}
       
  1821 
       
  1822    	delete newThread;
       
  1823 	delete cleanupStack;
       
  1824 	__UHEAP_MARKEND;
       
  1825 	
       
  1826     sem.Close();	
       
  1827 	TInt procHandles2  =0;
       
  1828 	TInt threadHandles2=0;
       
  1829 	RThread().HandleCount(procHandles2,threadHandles2);
       
  1830 	if (threadHandles1 != threadHandles2)
       
  1831 		{
       
  1832 		err = KErrGeneral;  // Thread-owned handles not closed
       
  1833 		}
       
  1834 
       
  1835 	return err;
       
  1836 	}
       
  1837 
       
  1838 
       
  1839 	
       
  1840 //--------------
       
  1841 __CONSTRUCT_STEP__(SurfaceManagerMultiThread)
       
  1842 
       
  1843 void CTSurfaceManagerMultiThreadStep::TestSetupL()
       
  1844 	{
       
  1845     }
       
  1846 
       
  1847 void CTSurfaceManagerMultiThreadStep::TestClose()
       
  1848 	{
       
  1849 	}