graphicstest/uibench/s60/src/surfaceutility.cpp
changeset 0 5d03bc08d59c
child 36 01a6848ebfd7
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  * @file
       
    18 */
       
    19 
       
    20 #include <e32std.h>
       
    21 #include <imageconversion.h>
       
    22 #include "surfaceutility.h"
       
    23 #include <BitmapTransforms.h> 
       
    24 
       
    25 //_LIT(KFileName,"surfacemanager");
       
    26 
       
    27 CSurfaceUtility::CSurfaceUtility(CSurfaceUtility* aClone/*=NULL*/)
       
    28 	:	iSurfaces(aClone?&(aClone->iSurfaces):NULL)
       
    29 	{
       
    30 	}
       
    31 	
       
    32 CSurfaceUtility* CSurfaceUtility::NewL(CSurfaceUtility* aClone/*=NULL*/)
       
    33 	{
       
    34 	CSurfaceUtility* utility = new (ELeave)CSurfaceUtility(aClone);
       
    35 	CleanupStack::PushL(utility);
       
    36 	utility->ConstructL();
       
    37 	CleanupStack::Pop(utility);
       
    38 	return utility;
       
    39 	}
       
    40 	
       
    41 void CSurfaceUtility::ConstructL()
       
    42 	{
       
    43 	TInt r = iManager.Open();
       
    44 	if (r != KErrNone)
       
    45 		{
       
    46 		LOG(("Surface manager failed to open: %d", r));
       
    47 		User::Leave(r);
       
    48 		}
       
    49 	
       
    50 	r = iSurfaceUpdateSession.Connect();
       
    51 	if (r != KErrNone)
       
    52 		{
       
    53 		LOG(("Failed to connect to update server: %d", r));
       
    54 		User::Leave(r);
       
    55 		}
       
    56 	}
       
    57 	
       
    58 CSurfaceUtility::~CSurfaceUtility()
       
    59 	{
       
    60 	DestroyAll();
       
    61 
       
    62 	iSurfaces.Close();
       
    63 
       
    64 	iManager.Close();
       
    65 
       
    66 	iSurfaceUpdateSession.Close();
       
    67 	
       
    68 	// the following call is needed because of a bug in CImageDecoder that 
       
    69 	// leaks heap memory
       
    70 	REComSession::FinalClose();
       
    71 	}
       
    72 
       
    73 TBool CSurfaceUtility::DestroyAll()
       
    74 	{
       
    75 	TInt err = 	KErrNone;
       
    76 	TInt jj = iSurfaces.Count() - 1;
       
    77 	if (jj<0)
       
    78 		return EFalse;
       
    79 	for (; jj >= 0; jj--)
       
    80 		{
       
    81 		err = iManager.CloseSurface(iSurfaces[jj]);
       
    82 		if (err!=KErrNone)
       
    83 			{
       
    84 			LOG(("Error closing surface: 0x%X\n", err));
       
    85 			}
       
    86 		}
       
    87 	iSurfaces.Reset();
       
    88 	return ETrue;
       
    89 	}
       
    90 
       
    91 /***************************************
       
    92  * The aim of the THeapSurfaceArray is to locally switch in the specified heap for any array operation
       
    93  ***************************************/
       
    94 
       
    95 CSurfaceUtility::RHeapSurfaceArray::RHeapSurfaceArray(RHeapSurfaceArray* aUseExternalArray)
       
    96 	:	iUseArray(aUseExternalArray?aUseExternalArray->iUseArray:&this->iLocalArray),
       
    97 	iExternalHeapRef(aUseExternalArray?aUseExternalArray->iExternalHeapRef:User::Heap())
       
    98 	{
       
    99 	
       
   100 	}
       
   101 /************************************
       
   102  * The following methods have been used by the surfaceutility... some require the heap wrapping, and some don't
       
   103  * I actually need three different startegies (count em) for 7 methods...
       
   104  * Some methods only read the existing objects, so don't need a heap swap at all
       
   105  * Leaving methods have to use PopAndDestroy strategy to restore the heap on leaving or success
       
   106  * Non-leaving methods must not call PushL, so directly make SwitchHeap calls!
       
   107  ************************************/
       
   108 
       
   109 // PopAndDestroy method to restore the heap
       
   110 /*static*/ void	CSurfaceUtility::RHeapSurfaceArray::PopHeap(void* aHeapPtr)
       
   111 	{
       
   112 	RHeap* heapPtr=(RHeap*)aHeapPtr;
       
   113 	User::SwitchHeap(heapPtr);
       
   114 	}
       
   115 
       
   116 // Switches and pushes the previous heap so it can be restored with PopAndDestroy
       
   117 /*static*/ void CSurfaceUtility::RHeapSurfaceArray::SwitchHeapLC(RHeap* aNewHeap)
       
   118 	{
       
   119 	CleanupStack::PushL(TCleanupItem(PopHeap,NULL));
       
   120 	CleanupStack::PushL(TCleanupItem(PopHeap,NULL));
       
   121 	CleanupStack::PushL(TCleanupItem(PopHeap,NULL));
       
   122 	CleanupStack::Pop(3);
       
   123 	RHeap* oldHeap=User::SwitchHeap(aNewHeap);
       
   124 	delete new char;
       
   125 	CleanupStack::PushL(TCleanupItem(PopHeap,oldHeap));
       
   126 	}
       
   127 
       
   128 
       
   129 TSurfaceId& CSurfaceUtility::RHeapSurfaceArray::operator[](TUint aIndex)
       
   130 	{
       
   131 	return iUseArray->operator[](aIndex);
       
   132 	}
       
   133 // Close only closes the local array, while Reset resets the active array (may be external)
       
   134 void CSurfaceUtility::RHeapSurfaceArray::Close()
       
   135 	{
       
   136 	RHeap* oldHeap=User::SwitchHeap(&iExternalHeapRef);
       
   137 	iLocalArray.Close();
       
   138 	User::SwitchHeap(oldHeap);
       
   139 	}
       
   140 TInt CSurfaceUtility::RHeapSurfaceArray::Count() const
       
   141 	{
       
   142 	return iUseArray->Count();
       
   143 	}
       
   144 // Close only closes the local array, while Reset resets the active array (may be external)
       
   145 inline void CSurfaceUtility::RHeapSurfaceArray::Reset()
       
   146 	{
       
   147 	RHeap* oldHeap=User::SwitchHeap(&iExternalHeapRef);
       
   148 	iUseArray->Reset();
       
   149 	User::SwitchHeap(oldHeap);
       
   150 	}
       
   151 void CSurfaceUtility::RHeapSurfaceArray::AppendL(const TSurfaceId &anEntry)
       
   152 	{
       
   153 	SwitchHeapLC(&iExternalHeapRef);
       
   154 	iUseArray->AppendL(anEntry);
       
   155 	CleanupStack::PopAndDestroy();
       
   156 	}
       
   157 TInt CSurfaceUtility::RHeapSurfaceArray::Find(const TSurfaceId &anEntry) const
       
   158 	{
       
   159 	return iUseArray->Find(anEntry);
       
   160 	}
       
   161 void CSurfaceUtility::RHeapSurfaceArray::Remove(TInt anIndex)
       
   162 	{
       
   163 	RHeap* oldHeap=User::SwitchHeap(&iExternalHeapRef);
       
   164 	iUseArray->Remove(anIndex);
       
   165 	User::SwitchHeap(oldHeap);
       
   166 	}
       
   167 
       
   168 
       
   169 
       
   170 
       
   171 /**
       
   172 Cleanup stack helper object, holding references to both utility and surface, so
       
   173 that the standard Close() semantics can be used.
       
   174 */
       
   175 class TSurfaceCleanup
       
   176 	{
       
   177 public:
       
   178 	TSurfaceCleanup(CSurfaceUtility& aUtility, TSurfaceId& aSurface)
       
   179 		: iUtility(aUtility), iSurface(aSurface)
       
   180 		{}
       
   181 	void Close()
       
   182 		{
       
   183 		// Removes the surface from the list of surfaces to clean up, and closes
       
   184 		// the surface reference.
       
   185 		iUtility.DestroySurface(iSurface);
       
   186 		}
       
   187 private:
       
   188 	CSurfaceUtility& iUtility;
       
   189 	TSurfaceId& iSurface;
       
   190 	};
       
   191 
       
   192 /**
       
   193 Read the given image file into a new surface.
       
   194 
       
   195 @param aFileName The name of the image file.
       
   196 @param aSurface Filled with the surface ID for the surface containing the pixels.
       
   197 */
       
   198 void CSurfaceUtility::CreateSurfaceFromFileL(const TDesC& aFileName, TSurfaceId& aSurface)
       
   199 	{
       
   200 	RFs fs;
       
   201 	
       
   202 	User::LeaveIfError(fs.Connect());
       
   203 	CleanupClosePushL(fs);
       
   204 	CImageDecoder* decoder = CImageDecoder::FileNewL(fs, aFileName, CImageDecoder::EOptionAlwaysThread);
       
   205 	CleanupStack::PushL(decoder);
       
   206 
       
   207 	const TFrameInfo& info = decoder->FrameInfo();
       
   208 
       
   209 	TSize size = info.iOverallSizeInPixels;
       
   210 	TInt stride = size.iWidth << 2;		// Default to four bytes per pixel
       
   211 	TDisplayMode bmpFormat = info.iFrameDisplayMode;
       
   212 	TUidPixelFormat pixelFormat = EUidPixelFormatUnknown;
       
   213 
       
   214 	switch (bmpFormat)
       
   215 		{
       
   216 		case EGray2:
       
   217 		case EGray4:
       
   218 		case EGray16:
       
   219 		case EGray256:
       
   220 		case EColor16:
       
   221 		case EColor256:
       
   222 		case EColor16M:
       
   223 		case EColor16MU:
       
   224 			{
       
   225 			bmpFormat = EColor16MU;
       
   226 			pixelFormat = EUidPixelFormatXRGB_8888;
       
   227 			break;
       
   228 			}
       
   229 		case EColor4K:
       
   230 			{
       
   231 			stride = size.iWidth << 1;
       
   232 			pixelFormat = EUidPixelFormatXRGB_4444;
       
   233 			break;
       
   234 			}
       
   235 		case EColor64K:
       
   236 			{
       
   237 			stride = size.iWidth << 1;
       
   238 			pixelFormat = EUidPixelFormatRGB_565;
       
   239 			break;
       
   240 			}
       
   241 		case EColor16MA:
       
   242 			{
       
   243 			pixelFormat = EUidPixelFormatARGB_8888;
       
   244 			break;
       
   245 			}
       
   246 		case EColor16MAP:
       
   247 			{
       
   248 			pixelFormat = EUidPixelFormatARGB_8888_PRE;
       
   249 			break;
       
   250 			}
       
   251 		default:
       
   252 			{
       
   253 			LOG(("Unsupported display mode: %d", bmpFormat));
       
   254 			User::Leave(KErrNotSupported);
       
   255 			break;
       
   256 			}
       
   257 		}
       
   258 
       
   259 	// Create an intermediary bitmap for decoding into
       
   260 	CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   261 	CleanupStack::PushL(bitmap);
       
   262 	User::LeaveIfError(bitmap->Create(size, info.iFrameDisplayMode));
       
   263 
       
   264 	// Create the final surface.
       
   265 	aSurface = CreateSurfaceL(size, pixelFormat, stride);
       
   266 	TSurfaceCleanup surfaceCleanup(*this, aSurface);
       
   267 	CleanupClosePushL(surfaceCleanup);
       
   268 
       
   269 	RChunk chunk;
       
   270 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   271 	CleanupClosePushL(chunk);
       
   272 
       
   273 	// Convert the image file into a Symbian bitmap
       
   274 	TRequestStatus status;
       
   275 	decoder->Convert(&status, *bitmap);
       
   276 	User::WaitForRequest(status);
       
   277 	User::LeaveIfError(status.Int());
       
   278 
       
   279 	// Copy the data from the bitmap into the surface.
       
   280 	TPoint start;
       
   281 	for (start.iY = 0; start.iY < size.iHeight; start.iY++)
       
   282 		{
       
   283 		// Set up a descriptor for the current line in the surface and get pixels.
       
   284 		TPtr8 ptr(chunk.Base() + start.iY * stride, stride);
       
   285 		bitmap->GetScanLine(ptr, start, size.iWidth, bmpFormat);
       
   286 		}
       
   287 
       
   288 	CleanupStack::PopAndDestroy(/* chunk */);
       
   289 	CleanupStack::Pop(/* surfaceCleanup */);
       
   290 	CleanupStack::PopAndDestroy(bitmap);
       
   291 	CleanupStack::PopAndDestroy(decoder);
       
   292 	CleanupStack::PopAndDestroy(/* fs */);
       
   293 	}
       
   294 
       
   295 void CSurfaceUtility::CopyBitmapSurfaceL(const CFbsBitmap* aBitmap, TSurfaceId& aSurface)
       
   296 	{
       
   297 	RChunk chunk;
       
   298 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   299 	CleanupClosePushL(chunk);
       
   300 	TSize bitmapSize = aBitmap->SizeInPixels();
       
   301 	TSize size = SurfaceSize(aSurface);
       
   302 	TInt stride = size.iWidth*4;		// Default to four bytes per pixel
       
   303 
       
   304 	// Copy the data from the bitmap into the surface.
       
   305 	TPoint start;
       
   306 	for (start.iY = 0; start.iY < bitmapSize.iHeight; start.iY++)
       
   307 		{
       
   308 		// Set up a descriptor for the current line in the surface and get pixels.
       
   309 		TPtr8 ptr(chunk.Base() + start.iY * stride, stride);
       
   310 		aBitmap->GetScanLine(ptr, start, bitmapSize.iWidth, EColor16MU);
       
   311 		}
       
   312 	CleanupStack::PopAndDestroy(/* chunk */);
       
   313 
       
   314 	}
       
   315 /**
       
   316 Copy the bitmap from a file to a surface.
       
   317 
       
   318 @param aFileName The name of the image file.
       
   319 @param aSurface Filled with the surface ID for the surface containing the pixels.
       
   320 */
       
   321 void CSurfaceUtility::CopyBitmapFromFileToSurfaceL(const TDesC& aFileName, const TSurfaceId& aSurface)
       
   322 	{
       
   323 	RFs fs;
       
   324 	
       
   325 	User::LeaveIfError(fs.Connect());
       
   326 	CleanupClosePushL(fs);
       
   327 	CImageDecoder* decoder = CImageDecoder::FileNewL(fs, aFileName, CImageDecoder::EOptionAlwaysThread);
       
   328 	CleanupStack::PushL(decoder);
       
   329 
       
   330 	const TFrameInfo& info = decoder->FrameInfo();
       
   331 
       
   332 	TSize size = SurfaceSize(aSurface);
       
   333 	TDisplayMode bmpFormat = info.iFrameDisplayMode;
       
   334 	TInt stride = size.iWidth << 2;		// Default to four bytes per pixel
       
   335 
       
   336 	// Create an intermediary bitmap for decoding into
       
   337 	CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   338 	CleanupStack::PushL(bitmap);
       
   339 	User::LeaveIfError(bitmap->Create(size, info.iFrameDisplayMode));
       
   340 
       
   341 	RChunk chunk;
       
   342 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   343 	CleanupClosePushL(chunk);
       
   344 
       
   345 	// Convert the image file into a Symbian bitmap
       
   346 	TRequestStatus status;
       
   347 	decoder->Convert(&status, *bitmap);
       
   348 	User::WaitForRequest(status);
       
   349 	User::LeaveIfError(status.Int());
       
   350 
       
   351 	// Copy the data from the bitmap into the surface.
       
   352 	TPoint start;
       
   353 	for (start.iY = 0; start.iY < size.iHeight; start.iY++)
       
   354 		{
       
   355 		// Set up a descriptor for the current line in the surface and get pixels.
       
   356 		TPtr8 ptr(chunk.Base() + start.iY * stride, stride);
       
   357 		bitmap->GetScanLine(ptr, start, size.iWidth, bmpFormat);
       
   358 		}
       
   359 
       
   360 	CleanupStack::PopAndDestroy(/* chunk */);
       
   361 	CleanupStack::PopAndDestroy(bitmap);
       
   362 	CleanupStack::PopAndDestroy(decoder);
       
   363 	CleanupStack::PopAndDestroy(/* fs */);
       
   364 	}
       
   365 
       
   366 /**
       
   367 Copy the bitmap from a file to a surface scaling the original image to cover the entire surface.
       
   368 
       
   369 @param aFileName The name of the image file.
       
   370 @param aSurface Filled with the surface ID for the surface containing the pixels.
       
   371 */
       
   372 void CSurfaceUtility::ScaleBitmapFromFileToSurfaceL(const TDesC& aFileName, const TSurfaceId& aSurface)
       
   373 	{
       
   374 
       
   375 	RFs fs;
       
   376 	User::LeaveIfError(fs.Connect());
       
   377 	CleanupClosePushL(fs);
       
   378 	CImageDecoder* decoder = CImageDecoder::FileNewL(fs, aFileName, CImageDecoder::EOptionAlwaysThread);
       
   379 	CleanupStack::PushL(decoder);
       
   380 
       
   381 	const TFrameInfo& info = decoder->FrameInfo();
       
   382 
       
   383 	TSize size = SurfaceSize(aSurface);
       
   384 	TInt stride = size.iWidth << 2;		// Default to four bytes per pixel
       
   385 
       
   386 	// Create an intermediary bitmap for decoding into
       
   387 	CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   388 	CleanupStack::PushL(bitmap);
       
   389 	User::LeaveIfError(bitmap->Create(info.iOverallSizeInPixels, info.iFrameDisplayMode));
       
   390 	
       
   391 	RChunk chunk;
       
   392 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   393 	CleanupClosePushL(chunk);
       
   394 	TUint8* surfacePixelData = chunk.Base() + PixelDataOffet(aSurface);
       
   395 	
       
   396 	// Convert the image file into a Symbian bitmap
       
   397 	// CImageDecoder::EOptionAlwaysThread setting above avoids need for Active Object
       
   398 	TRequestStatus status;
       
   399 	decoder->Convert(&status, *bitmap);
       
   400 	User::WaitForRequest(status);
       
   401 	User::LeaveIfError(status.Int());
       
   402 
       
   403 	// scale to fit surface
       
   404 	CBitmapScaler* scaler = CBitmapScaler::NewL();
       
   405 	CleanupStack::PushL(scaler);
       
   406 	scaler->SetQualityAlgorithm(CBitmapScaler::EMaximumQuality);
       
   407 	
       
   408 	CActiveListener* activeListener = CActiveListener::NewLC();
       
   409 	activeListener->Initialize();
       
   410 	scaler->Scale(&activeListener->iStatus, *bitmap, size, EFalse);
       
   411 	CActiveScheduler::Start();
       
   412 	
       
   413 	User::LeaveIfError(activeListener->iStatus.Int());
       
   414 
       
   415 	// Copy the data from the bitmap into the surface.
       
   416 	TPoint start;
       
   417 	for (start.iY = 0; start.iY < size.iHeight; start.iY++)
       
   418 		{
       
   419 		// Set up a descriptor for the current line in the surface and get pixels.
       
   420 		TPtr8 ptr(surfacePixelData + start.iY * stride, stride);
       
   421 		bitmap->GetScanLine(ptr, start, size.iWidth, EColor16MA);
       
   422 		}
       
   423 	CleanupStack::PopAndDestroy(activeListener);
       
   424 	CleanupStack::PopAndDestroy(scaler);
       
   425 	CleanupStack::PopAndDestroy(/* chunk */);
       
   426 	CleanupStack::PopAndDestroy(bitmap);
       
   427 	CleanupStack::PopAndDestroy(decoder);
       
   428 	CleanupStack::PopAndDestroy(/* fs */);	
       
   429 	}
       
   430 
       
   431 /**
       
   432 Get the size of a surface.
       
   433 
       
   434 @param aSurface The surface to get the size for.
       
   435 @return The size in pixels, or empty on failure.
       
   436 */
       
   437 TSize CSurfaceUtility::SurfaceSize(const TSurfaceId& aSurface)
       
   438 	{
       
   439 	RSurfaceManager::TInfoBuf infoBuf;
       
   440 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
   441 
       
   442 	if (iManager.SurfaceInfo(aSurface, infoBuf) == KErrNone)
       
   443 		{
       
   444 		return info.iSize;
       
   445 		}
       
   446 
       
   447 	return TSize();
       
   448 	}
       
   449 
       
   450 /**
       
   451 Get the offset into the chunk of the start of pixel data.
       
   452 
       
   453 @param aSurface The surface to get the size for.
       
   454 @return The offset bytes, or empty on failure.
       
   455 */
       
   456 TInt CSurfaceUtility::PixelDataOffet(const TSurfaceId& aSurface)
       
   457 	{
       
   458 	TInt offsetToFirstBuffer = 0;
       
   459 	iManager.GetBufferOffset(aSurface, 0, offsetToFirstBuffer);
       
   460 	
       
   461 	return offsetToFirstBuffer;
       
   462 	}
       
   463 
       
   464 
       
   465 /**
       
   466 Create a surface using the surface manager.
       
   467 
       
   468 Stores the ID for tear down, as well as returning it.
       
   469 
       
   470 @param aSize Dimensions of the surface.
       
   471 @param aPixelFormat	UID of the pixel format.
       
   472 @param aStride	Stride value for the surface (usually bytes per pixel * width)
       
   473 @leave May leave due to lack of memory.
       
   474 @return New surface's ID.
       
   475 */
       
   476 TSurfaceId CSurfaceUtility::CreateSurfaceL(const TSize& aSize, TUidPixelFormat aPixelFormat, TInt aStride, TInt aBuffers)
       
   477 	{
       
   478 	RSurfaceManager::TSurfaceCreationAttributesBuf bf;
       
   479 	RSurfaceManager::TSurfaceCreationAttributes& b = bf();
       
   480 	
       
   481 	b.iSize.iWidth = aSize.iWidth;
       
   482 	b.iSize.iHeight = aSize.iHeight;
       
   483 	b.iBuffers = aBuffers;				// number of buffers in the surface
       
   484 	b.iPixelFormat = aPixelFormat;
       
   485 	b.iStride = aStride;		// Number of bytes between start of one line and start of next
       
   486 	b.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
       
   487 	b.iAlignment = 4;			// alignment, 1,2,4,8 byte aligned
       
   488 	b.iContiguous=EFalse;
       
   489 
       
   490 	TSurfaceId surface = TSurfaceId::CreateNullId();
       
   491 
       
   492 	User::LeaveIfError(iManager.CreateSurface(bf, surface));
       
   493 	iSurfaces.AppendL(surface);
       
   494 	return surface;
       
   495 	}
       
   496 
       
   497 
       
   498 /**
       
   499 Fill the given surface with a color.
       
   500 
       
   501 @param aSurface	The surface to be filled.
       
   502 @param aColor	The color to fill it with.
       
   503 */
       
   504 void CSurfaceUtility::FillSurfaceL(TSurfaceId& aSurface, const TRgb& aColor)
       
   505 	{
       
   506 	RSurfaceManager::TInfoBuf infoBuf;
       
   507 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
   508 
       
   509 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
   510 	TUint32 color = 0;
       
   511 	TBool use16 = EFalse;
       
   512 
       
   513 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
   514 		{
       
   515 		User::Leave(KErrCorrupt);
       
   516 		}
       
   517 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
   518 		{
       
   519 		User::Leave(KErrNotReady);
       
   520 		}
       
   521 
       
   522 	switch (info.iPixelFormat)
       
   523 		{
       
   524 		case EUidPixelFormatXRGB_8888:
       
   525 			{
       
   526 			color = aColor.Color16MU();
       
   527 #ifdef ALPHA_FIX_24BIT
       
   528 			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
   529 #endif
       
   530 			break;
       
   531 			}
       
   532 		case EUidPixelFormatARGB_8888:
       
   533 			{
       
   534 			color = aColor.Color16MA();
       
   535 			break;
       
   536 			}
       
   537 		case EUidPixelFormatARGB_8888_PRE:
       
   538 			{
       
   539 			color = aColor.Color16MAP();
       
   540 			break;
       
   541 			}
       
   542 		case EUidPixelFormatXRGB_4444:
       
   543 		case EUidPixelFormatARGB_4444:
       
   544 			{
       
   545 			color = aColor.Color4K();
       
   546 			use16 = ETrue;
       
   547 			break;
       
   548 			}
       
   549 		case EUidPixelFormatRGB_565:
       
   550 			{
       
   551 			color = aColor.Color64K();
       
   552 			use16 = ETrue;
       
   553 			break;
       
   554 			}
       
   555 		default:
       
   556 			{
       
   557 			User::Leave(KErrNotSupported);
       
   558 			break;
       
   559 			}
       
   560 		}
       
   561 
       
   562 	RChunk chunk;
       
   563 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   564 
       
   565 	TUint8* surfacePtr = chunk.Base();
       
   566 	TUint8* linePtr = surfacePtr;
       
   567 
       
   568 	if (use16)
       
   569 		{
       
   570 		if ( info.iSize.iWidth*2>info.iStride)
       
   571 			{
       
   572 			User::Leave(KErrOverflow);
       
   573 			}
       
   574 		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
       
   575 
       
   576 		// Fill first line
       
   577 		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
       
   578 			{
       
   579 			ptr[xx] = (TUint16)color;
       
   580 			}
       
   581 		}
       
   582 	else
       
   583 		{
       
   584 		if ( info.iSize.iWidth*4>info.iStride)
       
   585 			{
       
   586 			User::Leave(KErrOverflow);
       
   587 			}
       
   588 		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
       
   589 
       
   590 		// Fill first line
       
   591 		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
       
   592 			{
       
   593 			ptr[xx] = color;
       
   594 			}
       
   595 		}
       
   596 
       
   597 	// Now copy that to the other lines
       
   598 	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
       
   599 		{
       
   600 		linePtr += info.iStride;
       
   601 		Mem::Move(linePtr, surfacePtr, info.iStride);
       
   602 		}
       
   603 
       
   604 	chunk.Close();
       
   605 
       
   606 	TInt err = iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
   607 	if (err!=KErrNone)
       
   608 		LOG(("Error submitting update: 0x%X\n", err));
       
   609 	}
       
   610 
       
   611 /**
       
   612 Fill the given memory chunk with a color.
       
   613 
       
   614 @param aSurface	The surface to be filled.
       
   615 @param aChunk	The surface to be filled.
       
   616 @param aColor	The color to fill it with.
       
   617 */
       
   618 void CSurfaceUtility::FillChunkL(TSurfaceId& aSurface, RChunk& aChunk, const TRgb& aColor, TInt aBufferNumber)
       
   619 	{
       
   620 	RSurfaceManager::TInfoBuf infoBuf;
       
   621 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
   622 
       
   623 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
   624 	TUint32 color = 0;
       
   625 	TBool use16 = EFalse;
       
   626 
       
   627 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
   628 		{
       
   629 		User::Leave(KErrCorrupt);
       
   630 		}
       
   631 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
   632 		{
       
   633 		User::Leave(KErrNotReady);
       
   634 		}
       
   635 
       
   636 	switch (info.iPixelFormat)
       
   637 		{
       
   638 		case EUidPixelFormatXRGB_8888:
       
   639 			{
       
   640 			color = aColor.Color16MU();
       
   641 #ifdef ALPHA_FIX_24BIT
       
   642 			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
   643 #endif
       
   644 			break;
       
   645 			}
       
   646 		case EUidPixelFormatARGB_8888:
       
   647 			{
       
   648 			color = aColor.Color16MA();
       
   649 			break;
       
   650 			}
       
   651 		case EUidPixelFormatARGB_8888_PRE:
       
   652 			{
       
   653 			color = aColor.Color16MAP();
       
   654 			break;
       
   655 			}
       
   656 		case EUidPixelFormatXRGB_4444:
       
   657 		case EUidPixelFormatARGB_4444:
       
   658 			{
       
   659 			color = aColor.Color4K();
       
   660 			use16 = ETrue;
       
   661 			break;
       
   662 			}
       
   663 		case EUidPixelFormatRGB_565:
       
   664 			{
       
   665 			color = aColor.Color64K();
       
   666 			use16 = ETrue;
       
   667 			break;
       
   668 			}
       
   669 		default:
       
   670 			{
       
   671 			User::Leave(KErrNotSupported);
       
   672 			break;
       
   673 			}
       
   674 		}
       
   675 
       
   676 	User::LeaveIfError(iManager.MapSurface(aSurface, aChunk));
       
   677 
       
   678 	TInt offsetToFirstBuffer;
       
   679 	User::LeaveIfError(iManager.GetBufferOffset(aSurface, 0, offsetToFirstBuffer));
       
   680 	TInt offsetToBufferNumber;
       
   681 	User::LeaveIfError(iManager.GetBufferOffset(aSurface, aBufferNumber, offsetToBufferNumber));
       
   682 		
       
   683 	TUint8* chunkPtr = aChunk.Base() + offsetToFirstBuffer;
       
   684 	TUint8* linePtr = aChunk.Base() + offsetToBufferNumber;
       
   685 	TUint8* surfPlanePtr = linePtr;
       
   686 
       
   687 	if (use16)
       
   688 		{
       
   689 		if ( info.iSize.iWidth*2>info.iStride)
       
   690 			{
       
   691 			aChunk.Close();
       
   692 			User::Leave(KErrOverflow);
       
   693 			}
       
   694 		TUint16* ptr = reinterpret_cast<TUint16*>(surfPlanePtr);
       
   695 
       
   696 		// Fill first line
       
   697 		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
       
   698 			{
       
   699 			ptr[xx] = (TUint16)color;
       
   700 			}
       
   701 		}
       
   702 	else
       
   703 		{
       
   704 		if ( info.iSize.iWidth*4>info.iStride)
       
   705 			{
       
   706 			aChunk.Close();
       
   707 			User::Leave(KErrOverflow);
       
   708 			}
       
   709 		TUint32* ptr = reinterpret_cast<TUint32*>(surfPlanePtr);
       
   710 
       
   711 		// Fill first line
       
   712 		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
       
   713 			{
       
   714 			ptr[xx] = color;
       
   715 			}
       
   716 		}
       
   717 
       
   718 	// Now copy that to the other lines
       
   719 	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
       
   720 		{
       
   721 		linePtr += info.iStride;
       
   722 		Mem::Copy(linePtr, surfPlanePtr, info.iSize.iWidth * BytesPerPixelL(info.iPixelFormat));
       
   723 		}
       
   724 
       
   725 	aChunk.Close();
       
   726 	}
       
   727 
       
   728 /**
       
   729 Fill a rectangle on the given surface.
       
   730 
       
   731 @param aSurface		The surface to be filled.
       
   732 @param aStartPos	Where to place the rectangle.
       
   733 @param aSize		Size of the rectangle.
       
   734 @param aColor		The colour to fill it with.
       
   735 */
       
   736 void CSurfaceUtility::FillRectangleL(TSurfaceId& aSurface, TPoint& aStartPos, TSize& aSize, const TRgb& aColor)
       
   737 	{
       
   738 	RSurfaceManager::TInfoBuf infoBuf;
       
   739 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
   740 
       
   741 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
   742 	TUint32 color = 0;
       
   743 	TBool use16 = EFalse;
       
   744 
       
   745 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
   746 		{
       
   747 		User::Leave(KErrCorrupt);
       
   748 		}
       
   749 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
   750 		{
       
   751 		User::Leave(KErrNotReady);
       
   752 		}
       
   753 
       
   754 	switch (info.iPixelFormat)
       
   755 		{
       
   756 		case EUidPixelFormatXRGB_8888:
       
   757 			{
       
   758 			color = aColor.Color16MU();
       
   759 #ifdef ALPHA_FIX_24BIT
       
   760 			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
   761 #endif
       
   762 			break;
       
   763 			}
       
   764 		case EUidPixelFormatARGB_8888:
       
   765 			{
       
   766 			color = aColor.Color16MA();
       
   767 			break;
       
   768 			}
       
   769 		case EUidPixelFormatARGB_8888_PRE:
       
   770 			{
       
   771 			color = aColor.Color16MAP();
       
   772 			break;
       
   773 			}
       
   774 		case EUidPixelFormatXRGB_4444:
       
   775 		case EUidPixelFormatARGB_4444:
       
   776 			{
       
   777 			color = aColor.Color4K();
       
   778 			use16 = ETrue;
       
   779 			break;
       
   780 			}
       
   781 		case EUidPixelFormatRGB_565:
       
   782 			{
       
   783 			color = aColor.Color64K();
       
   784 			use16 = ETrue;
       
   785 			break;
       
   786 			}
       
   787 		default:
       
   788 			{
       
   789 			User::Leave(KErrNotSupported);
       
   790 			break;
       
   791 			}
       
   792 		}
       
   793 
       
   794 	RChunk chunk;
       
   795 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   796 
       
   797 	TUint8* surfacePtr = chunk.Base();
       
   798 	
       
   799 	// Check for out of bounds
       
   800 	TBool validRect = ETrue;
       
   801 	TInt surfaceWidth = info.iSize.iWidth;
       
   802 	TInt surfaceHeight = info.iSize.iHeight;
       
   803 	
       
   804 	// Width and Height
       
   805 	if ((aStartPos.iX + aSize.iWidth) > surfaceWidth)
       
   806 		validRect = EFalse;
       
   807 	
       
   808 	if ((aStartPos.iY + aSize.iHeight) > surfaceHeight)
       
   809 		validRect = EFalse;
       
   810 	
       
   811 	// Starting position
       
   812 	if ((aStartPos.iX < 0) || (aStartPos.iY < 0))
       
   813 		validRect = EFalse;
       
   814 	
       
   815 	if (!validRect)
       
   816 		User::Leave(KErrOverflow);
       
   817 		
       
   818 	if (use16)
       
   819 		{
       
   820 		if ( info.iSize.iWidth*2>info.iStride)
       
   821 			{
       
   822 			User::Leave(KErrOverflow);
       
   823 			}
       
   824 		
       
   825 		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
       
   826 		
       
   827 		// Fill the rectangle
       
   828 		TInt yPos = aStartPos.iY;
       
   829 		TInt xPos = aStartPos.iX;
       
   830 		for (TInt yy = 0; yy < aSize.iHeight; ++yy)
       
   831 			{
       
   832 			ptr = reinterpret_cast<TUint16*>(surfacePtr+(yPos*info.iStride));
       
   833 			for (TInt xx = 0; xx < aSize.iWidth; ++xx)
       
   834 				{
       
   835 				ptr[xPos] = color;
       
   836 				xPos++;
       
   837 				}
       
   838 			xPos = aStartPos.iX;
       
   839 			yPos++;
       
   840 			}
       
   841 		}
       
   842 	else
       
   843 		{
       
   844 		if ( info.iSize.iWidth*4>info.iStride)
       
   845 			{
       
   846 			User::Leave(KErrOverflow);
       
   847 			}
       
   848 
       
   849 		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);		
       
   850 		
       
   851 		// Fill the rectangle
       
   852 		TInt yPos = aStartPos.iY;
       
   853 		TInt xPos = aStartPos.iX;
       
   854 		for (TInt yy = 0; yy < aSize.iHeight; ++yy)
       
   855 			{
       
   856 			ptr = reinterpret_cast<TUint32*>(surfacePtr+(yPos*info.iStride));
       
   857 			for (TInt xx = 0; xx < aSize.iWidth; ++xx)
       
   858 				{
       
   859 				ptr[xPos] = color;
       
   860 				xPos++;
       
   861 				}
       
   862 			xPos = aStartPos.iX;
       
   863 			yPos++;
       
   864 			}
       
   865 		}
       
   866 
       
   867 	chunk.Close();
       
   868 
       
   869 	TInt err = iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
   870 	if (err!=KErrNone)
       
   871 		LOG(("Error submitting update: 0x%X\n", err));
       
   872 	}
       
   873 
       
   874 /**
       
   875 Fill a rectangle on the given surface - does not submit update.
       
   876 
       
   877 @param aSurface		The surface to be filled.
       
   878 @param aStartPos	Where to place the rectangle.
       
   879 @param aSize		Size of the rectangle.
       
   880 @param aColor		The colour to fill it with.
       
   881 */
       
   882 void CSurfaceUtility::FillRectangleNoUpdateL(TSurfaceId& aSurface, TPoint& aStartPos, TSize& aSize, const TRgb& aColor)
       
   883 	{
       
   884 	RSurfaceManager::TInfoBuf infoBuf;
       
   885 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
   886 
       
   887 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
   888 	TUint32 color = 0;
       
   889 	TBool use16 = EFalse;
       
   890 
       
   891 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
   892 		{
       
   893 		User::Leave(KErrCorrupt);
       
   894 		}
       
   895 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
   896 		{
       
   897 		User::Leave(KErrNotReady);
       
   898 		}
       
   899 
       
   900 	switch (info.iPixelFormat)
       
   901 		{
       
   902 		case EUidPixelFormatXRGB_8888:
       
   903 			{
       
   904 			color = aColor.Color16MU();
       
   905 #ifdef ALPHA_FIX_24BIT
       
   906 			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
   907 #endif
       
   908 			break;
       
   909 			}
       
   910 		case EUidPixelFormatARGB_8888:
       
   911 			{
       
   912 			color = aColor.Color16MA();
       
   913 			break;
       
   914 			}
       
   915 		case EUidPixelFormatARGB_8888_PRE:
       
   916 			{
       
   917 			color = aColor.Color16MAP();
       
   918 			break;
       
   919 			}
       
   920 		case EUidPixelFormatXRGB_4444:
       
   921 		case EUidPixelFormatARGB_4444:
       
   922 			{
       
   923 			color = aColor.Color4K();
       
   924 			use16 = ETrue;
       
   925 			break;
       
   926 			}
       
   927 		case EUidPixelFormatRGB_565:
       
   928 			{
       
   929 			color = aColor.Color64K();
       
   930 			use16 = ETrue;
       
   931 			break;
       
   932 			}
       
   933 		default:
       
   934 			{
       
   935 			User::Leave(KErrNotSupported);
       
   936 			break;
       
   937 			}
       
   938 		}
       
   939 
       
   940 	RChunk chunk;
       
   941 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
   942 
       
   943 	TUint8* surfacePtr = chunk.Base();
       
   944 	
       
   945 	// Check for out of bounds
       
   946 	TBool validRect = ETrue;
       
   947 	TInt surfaceWidth = info.iSize.iWidth;
       
   948 	TInt surfaceHeight = info.iSize.iHeight;
       
   949 	
       
   950 	// Width and Height
       
   951 	if ((aStartPos.iX + aSize.iWidth) > surfaceWidth)
       
   952 		validRect = EFalse;
       
   953 	
       
   954 	if ((aStartPos.iY + aSize.iHeight) > surfaceHeight)
       
   955 		validRect = EFalse;
       
   956 	
       
   957 	// Starting position
       
   958 	if ((aStartPos.iX < 0) || (aStartPos.iY < 0))
       
   959 		validRect = EFalse;
       
   960 	
       
   961 	if (!validRect)
       
   962 		User::Leave(KErrOverflow);
       
   963 	
       
   964 	if (use16)
       
   965 		{
       
   966 		if ( info.iSize.iWidth*2>info.iStride)
       
   967 			{
       
   968 			User::Leave(KErrOverflow);
       
   969 			}
       
   970 		
       
   971 		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
       
   972 		
       
   973 		// Fill the rectangle
       
   974 		TInt yPos = aStartPos.iY;
       
   975 		TInt xPos = aStartPos.iX;
       
   976 		for (TInt yy = 0; yy < aSize.iHeight; ++yy)
       
   977 			{
       
   978 			ptr = reinterpret_cast<TUint16*>(surfacePtr+(yPos*info.iStride));
       
   979 			for (TInt xx = 0; xx < aSize.iWidth; ++xx)
       
   980 				{
       
   981 				ptr[xPos] = color;
       
   982 				xPos++;
       
   983 				}
       
   984 			xPos = aStartPos.iX;
       
   985 			yPos++;
       
   986 			}
       
   987 		}
       
   988 	else
       
   989 		{
       
   990 		if ( info.iSize.iWidth*4>info.iStride)
       
   991 			{
       
   992 			User::Leave(KErrOverflow);
       
   993 			}
       
   994 
       
   995 		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);		
       
   996 		
       
   997 		// Fill the rectangle
       
   998 		TInt yPos = aStartPos.iY;
       
   999 		TInt xPos = aStartPos.iX;
       
  1000 		for (TInt yy = 0; yy < aSize.iHeight; ++yy)
       
  1001 			{
       
  1002 			ptr = reinterpret_cast<TUint32*>(surfacePtr+(yPos*info.iStride));
       
  1003 			for (TInt xx = 0; xx < aSize.iWidth; ++xx)
       
  1004 				{
       
  1005 				ptr[xPos] = color;
       
  1006 				xPos++;
       
  1007 				}
       
  1008 			xPos = aStartPos.iX;
       
  1009 			yPos++;
       
  1010 			}
       
  1011 		}
       
  1012 
       
  1013 	chunk.Close();
       
  1014 
       
  1015 	}
       
  1016 
       
  1017 /**
       
  1018 Fill the given surface with a grid over a solid color.
       
  1019 
       
  1020 Similar to FillSurfaceL(), but with a grid overlayed. The pitch of the grid is
       
  1021 eight pixels.
       
  1022 
       
  1023 @param aSurface	The surface to be filled.
       
  1024 @param aColor	The color to fill it with.
       
  1025 @param aLines	The color of the grid lines.
       
  1026 */
       
  1027 void CSurfaceUtility::GridFillSurfaceL(TSurfaceId& aSurface, const TRgb& aColor, const TRgb& aLines)
       
  1028 	{
       
  1029 	RSurfaceManager::TInfoBuf infoBuf;
       
  1030 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
  1031 
       
  1032 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
  1033 	TUint32 color = 0;
       
  1034 	TUint32 lines = 0;
       
  1035 	TBool use16 = EFalse;
       
  1036 
       
  1037 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
  1038 		{
       
  1039 		User::Leave(KErrCorrupt);
       
  1040 		}
       
  1041 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
  1042 		{
       
  1043 		User::Leave(KErrNotReady);
       
  1044 		}
       
  1045 
       
  1046 	switch (info.iPixelFormat)
       
  1047 		{
       
  1048 		case EUidPixelFormatXRGB_8888:
       
  1049 			{
       
  1050 			color = aColor.Color16MU();
       
  1051 			lines = aLines.Color16MU();
       
  1052 #ifdef ALPHA_FIX_24BIT
       
  1053 			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1054 			lines |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1055 #endif
       
  1056 			break;
       
  1057 			}
       
  1058 		case EUidPixelFormatARGB_8888:
       
  1059 			{
       
  1060 			color = aColor.Color16MA();
       
  1061 			lines = aLines.Color16MA();
       
  1062 			break;
       
  1063 			}
       
  1064 		case EUidPixelFormatARGB_8888_PRE:
       
  1065 			{
       
  1066 			color = aColor.Color16MAP();
       
  1067 			lines = aLines.Color16MAP();
       
  1068 			break;
       
  1069 			}
       
  1070 		case EUidPixelFormatXRGB_4444:
       
  1071 		case EUidPixelFormatARGB_4444:
       
  1072 			{
       
  1073 			color = aColor.Color4K();
       
  1074 			lines = aLines.Color4K();
       
  1075 			use16 = ETrue;
       
  1076 			break;
       
  1077 			}
       
  1078 		case EUidPixelFormatRGB_565:
       
  1079 			{
       
  1080 			color = aColor.Color64K();
       
  1081 			lines = aLines.Color64K();
       
  1082 			use16 = ETrue;
       
  1083 			break;
       
  1084 			}
       
  1085 		default:
       
  1086 			{
       
  1087 			User::Leave(KErrNotSupported);
       
  1088 			break;
       
  1089 			}
       
  1090 		}
       
  1091 
       
  1092 	RChunk chunk;
       
  1093 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
  1094 
       
  1095 	TUint8* surfacePtr = chunk.Base();
       
  1096 	TUint8* linePtr = surfacePtr;
       
  1097 
       
  1098 	if (use16)
       
  1099 		{
       
  1100 		if ( info.iSize.iWidth*2>info.iStride)
       
  1101 			{
       
  1102 			User::Leave(KErrOverflow);
       
  1103 			}
       
  1104 		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
       
  1105 
       
  1106 		// Fill first line
       
  1107 		for (TInt xx1 = 0; xx1 < info.iSize.iWidth; xx1++)
       
  1108 			{
       
  1109 			ptr[xx1] = (TUint16)lines;
       
  1110 			}
       
  1111 
       
  1112 		// Fill second line
       
  1113 		ptr = reinterpret_cast<TUint16*>(surfacePtr + info.iStride);
       
  1114 		for (TInt xx2 = 0; xx2 < info.iSize.iWidth; xx2++)
       
  1115 			{
       
  1116 			// Vertical line every 8 pixels across
       
  1117 			ptr[xx2] = (TUint16)((xx2 & 7) ? color : lines);
       
  1118 			}
       
  1119 		}
       
  1120 	else
       
  1121 		{
       
  1122 		if ( info.iSize.iWidth*4>info.iStride)
       
  1123 			{
       
  1124 			User::Leave(KErrOverflow);
       
  1125 			}
       
  1126 		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
       
  1127 
       
  1128 		// Fill first line
       
  1129 		for (TInt xx3 = 0; xx3 < info.iSize.iWidth; xx3++)
       
  1130 			{
       
  1131 			ptr[xx3] = lines;
       
  1132 			}
       
  1133 
       
  1134 		// Fill second line
       
  1135 		ptr = reinterpret_cast<TUint32*>(surfacePtr + info.iStride);
       
  1136 		for (TInt xx4 = 0; xx4 < info.iSize.iWidth; xx4++)
       
  1137 			{
       
  1138 			// Vertical line every 8 pixels across
       
  1139 			ptr[xx4] = (xx4 & 7) ? color : lines;
       
  1140 			}
       
  1141 		}
       
  1142 	linePtr += info.iStride;
       
  1143 
       
  1144 	// Now copy that to the other lines
       
  1145 	for (TInt yy = 2; yy < info.iSize.iHeight; yy++)
       
  1146 		{
       
  1147 		linePtr += info.iStride;
       
  1148 		if (yy & 7)
       
  1149 			{
       
  1150 			// Copy second line
       
  1151 			Mem::Move(linePtr, surfacePtr + info.iStride, info.iStride);
       
  1152 			}
       
  1153 		else
       
  1154 			{
       
  1155 			// Copy first line
       
  1156 			Mem::Move(linePtr, surfacePtr, info.iStride);
       
  1157 			}
       
  1158 		}
       
  1159 	chunk.Close();
       
  1160 
       
  1161 	TInt err =iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
  1162 	if (err!=KErrNone)
       
  1163 		LOG(("Error submitting update: 0x%X\n", err));
       
  1164 
       
  1165 	}
       
  1166 
       
  1167 
       
  1168 /**
       
  1169 Fill the given surface with a pattern suitable for automated testing.
       
  1170 
       
  1171 @param aSurface	The surface to be filled.
       
  1172 */
       
  1173 void CSurfaceUtility::PatternFillSurfaceL(TSurfaceId& aSurface)
       
  1174 	{
       
  1175 	RSurfaceManager::TInfoBuf infoBuf;
       
  1176 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
  1177 
       
  1178 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));	
       
  1179 	
       
  1180 	// Fill the background
       
  1181 	FillSurfaceL(aSurface, TRgb(0x00000000));
       
  1182 
       
  1183 	TInt surfaceWidth = info.iSize.iWidth;
       
  1184 	TInt surfaceHeight = info.iSize.iHeight;
       
  1185 	
       
  1186 	// Create the 4 rectangles in the corners
       
  1187 	TPoint startPos(0,0);
       
  1188 	TSize size(15,15);
       
  1189 	TInt rectWidth = size.iWidth;
       
  1190 	TInt rectHeight = size.iHeight;
       
  1191 	// Top left
       
  1192 	FillRectangleL(aSurface, startPos, size, TRgb(0x0000ff));
       
  1193 	
       
  1194 	// Top right
       
  1195 	startPos.iX = surfaceWidth - rectWidth;
       
  1196 	startPos.iY = 0;
       
  1197 	FillRectangleL(aSurface, startPos, size, TRgb(0x00ff00));
       
  1198 	
       
  1199 	// Bottom left
       
  1200 	startPos.iX = 0;
       
  1201 	startPos.iY = surfaceHeight - rectHeight;
       
  1202 	FillRectangleL(aSurface, startPos, size, TRgb(0x00ffff));
       
  1203 	
       
  1204 	// Bottom right
       
  1205 	startPos.iX = surfaceWidth - rectWidth;
       
  1206 	startPos.iY = surfaceHeight - rectHeight;
       
  1207 	FillRectangleL(aSurface, startPos, size, TRgb(0xffffff));
       
  1208 	
       
  1209 	// Create the 4 side bars
       
  1210 	startPos.iX = 0;
       
  1211 	startPos.iY = 6;
       
  1212 	size.iWidth = 5;
       
  1213 	size.iHeight = surfaceHeight - 12;
       
  1214 	// Left
       
  1215 	FillRectangleL(aSurface, startPos, size, TRgb(0x808000));
       
  1216 	
       
  1217 	startPos.iX = surfaceWidth - size.iWidth;
       
  1218 	startPos.iY = 6;
       
  1219 	// Right
       
  1220 	FillRectangleL(aSurface, startPos, size, TRgb(0xff00ff));
       
  1221 	
       
  1222 	startPos.iX = 6;
       
  1223 	startPos.iY = surfaceHeight - size.iWidth;
       
  1224 	size.iWidth = surfaceWidth - 12;
       
  1225 	size.iHeight = 5;
       
  1226 	// Top
       
  1227 	FillRectangleL(aSurface, startPos, size, TRgb(0xaaaaaa));
       
  1228 	
       
  1229 	startPos.iX = 6;
       
  1230 	startPos.iY = 0;
       
  1231 	// Bottom
       
  1232 	FillRectangleL(aSurface, startPos, size, TRgb(0x000080));
       
  1233 	}
       
  1234 
       
  1235 
       
  1236 template <class TIntType> void
       
  1237 DdaLine(TUint aX1, TUint aY1,TUint aX2,TUint aY2, TUint aPixPerScan, TIntType* aBuffer, TIntType aColor)
       
  1238 	{
       
  1239 	TInt dx=aX2-aX1;
       
  1240 	TInt dy=aY2-aY1;
       
  1241 	TInt adx=dx,sdx=1;
       
  1242 	if (adx<0)
       
  1243 		{	adx=-adx; sdx=-1;	}
       
  1244 	TInt ady=dy,sdy=aPixPerScan;
       
  1245 	if (ady<0)
       
  1246 		{	ady=-ady; sdy=-aPixPerScan;	}
       
  1247 	//This is simplistic integert DDA.
       
  1248 	//The vertical cases are handled by this 1/2 accumulator:
       
  1249 	//	If adx is zero then we step in sdy indefinitely
       
  1250 	//  If ady is zero then we step in sdx indefinitely
       
  1251 	TInt accum=adx/2;
       
  1252 	
       
  1253 	TIntType* bufferend=aBuffer+aX2+aY2*aPixPerScan;
       
  1254 	aBuffer+=aX1+aY1*aPixPerScan;
       
  1255 	*aBuffer=aColor;
       
  1256 	while (aBuffer!=bufferend)
       
  1257 		{
       
  1258 		if (accum>0)
       
  1259 			{
       
  1260 			accum-=ady;
       
  1261 			aBuffer+=sdx;
       
  1262 			}
       
  1263 		else
       
  1264 			{
       
  1265 			accum+=adx;
       
  1266 			aBuffer+=sdy;
       
  1267 			}
       
  1268 		*aBuffer=aColor;
       
  1269 		}
       
  1270 	
       
  1271 
       
  1272 	}
       
  1273 template <class TIntType> void	
       
  1274 FanFill(const TPoint& aInnerXY,TUint aPixPerScan, TIntType* aSurfacePtr, TIntType aLinesTL, 
       
  1275 			TIntType aLinesBR, TIntType aLinesTR, TIntType aLinesBL)
       
  1276 	{
       
  1277 	
       
  1278 		DdaLine(aInnerXY.iX,0,aInnerXY.iX-aInnerXY.iX*180/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1279 		DdaLine(aInnerXY.iX,0,aInnerXY.iX-aInnerXY.iX*372/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1280 		DdaLine(aInnerXY.iX,0,aInnerXY.iX-aInnerXY.iX*591/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1281 		DdaLine(aInnerXY.iX,0,aInnerXY.iX-aInnerXY.iX*859/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1282 
       
  1283 		DdaLine(aInnerXY.iX,0,0,aInnerXY.iY*180/1024,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1284 		DdaLine(aInnerXY.iX,0,0,aInnerXY.iY*372/1024,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1285 		DdaLine(aInnerXY.iX,0,0,aInnerXY.iY*591/1024,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1286 		DdaLine(aInnerXY.iX,0,0,aInnerXY.iY*859/1024,aPixPerScan,aSurfacePtr,aLinesTR);
       
  1287 		
       
  1288 		DdaLine(0,aInnerXY.iY,aInnerXY.iX*180/1024,0,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1289 		DdaLine(0,aInnerXY.iY,aInnerXY.iX*372/1024,0,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1290 		DdaLine(0,aInnerXY.iY,aInnerXY.iX*591/1024,0,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1291 		DdaLine(0,aInnerXY.iY,aInnerXY.iX*859/1024,0,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1292 
       
  1293 		DdaLine(0,aInnerXY.iY,aInnerXY.iX,aInnerXY.iY-aInnerXY.iY*180/1024,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1294 		DdaLine(0,aInnerXY.iY,aInnerXY.iX,aInnerXY.iY-aInnerXY.iY*372/1024,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1295 		DdaLine(0,aInnerXY.iY,aInnerXY.iX,aInnerXY.iY-aInnerXY.iY*591/1024,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1296 		DdaLine(0,aInnerXY.iY,aInnerXY.iX,aInnerXY.iY-aInnerXY.iY*859/1024,aPixPerScan,aSurfacePtr,aLinesBL);
       
  1297 		
       
  1298 		DdaLine(0,0,aInnerXY.iX*180/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1299 		DdaLine(0,0,aInnerXY.iX*372/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1300 		DdaLine(0,0,aInnerXY.iX*591/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1301 		DdaLine(0,0,aInnerXY.iX*859/1024,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1302 
       
  1303 		DdaLine(0,0,aInnerXY.iX,aInnerXY.iY*180/1024,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1304 		DdaLine(0,0,aInnerXY.iX,aInnerXY.iY*372/1024,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1305 		DdaLine(0,0,aInnerXY.iX,aInnerXY.iY*591/1024,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1306 		DdaLine(0,0,aInnerXY.iX,aInnerXY.iY*859/1024,aPixPerScan,aSurfacePtr,aLinesTL);
       
  1307 		
       
  1308 		DdaLine(0,aInnerXY.iY-aInnerXY.iY*180/1024,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1309 		DdaLine(0,aInnerXY.iY-aInnerXY.iY*372/1024,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1310 		DdaLine(0,aInnerXY.iY-aInnerXY.iY*591/1024,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1311 		DdaLine(0,aInnerXY.iY-aInnerXY.iY*859/1024,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1312 
       
  1313 		DdaLine(aInnerXY.iX-aInnerXY.iX*180/1024,0,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1314 		DdaLine(aInnerXY.iX-aInnerXY.iX*372/1024,0,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1315 		DdaLine(aInnerXY.iX-aInnerXY.iX*591/1024,0,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1316 		DdaLine(aInnerXY.iX-aInnerXY.iX*859/1024,0,aInnerXY.iX,aInnerXY.iY,aPixPerScan,aSurfacePtr,aLinesBR);
       
  1317 	
       
  1318 	}
       
  1319 /**
       
  1320 Fill the given surface with a fan of lines over a solid color.
       
  1321 
       
  1322 Similar to FillSurfaceL(), but with a fan of lines overlayed. 
       
  1323 One fan is drawn about the top-left, and second fan at bottom-right.
       
  1324 The fan contains 8 segments.
       
  1325 
       
  1326 @param aSurface	The surface to be filled.
       
  1327 @param aColor	The color to fill it with.
       
  1328 @param aLines	The color of the grid lines.
       
  1329 */
       
  1330 void CSurfaceUtility::FanFillSurfaceL(TSurfaceId& aSurface, const TRgb& aColor, const TRgb& aLinesTL, const TRgb& aLinesBR)
       
  1331 	{
       
  1332 	FillSurfaceL(aSurface,aColor);
       
  1333 	RSurfaceManager::TInfoBuf infoBuf;
       
  1334 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
  1335 
       
  1336 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
  1337 	TUint32 linesTL = 0;
       
  1338 	TUint32 linesBR = 0;
       
  1339 	TUint32 linesTR = 0;
       
  1340 	TUint32 linesBL = 0;
       
  1341 	TBool use16 = EFalse;
       
  1342 	TRgb	rgbLinesTR(0,0,0);
       
  1343 	TRgb	rgbLinesBL(255,255,255);
       
  1344 
       
  1345 	switch (info.iPixelFormat)
       
  1346 		{
       
  1347 		case EUidPixelFormatXRGB_8888:
       
  1348 			{
       
  1349 			linesBR = aLinesBR.Color16MU();
       
  1350 			linesTL = aLinesTL.Color16MU();
       
  1351 			linesTR = rgbLinesTR.Color16MU();
       
  1352 			linesBL = rgbLinesBL.Color16MU();
       
  1353 #ifdef ALPHA_FIX_24BIT
       
  1354 			linesBR |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1355 			linesTL |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1356 			linesTR |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1357 			linesBL |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1358 #endif
       
  1359 			break;
       
  1360 			}
       
  1361 		case EUidPixelFormatARGB_8888:
       
  1362 			{
       
  1363 			linesBR = aLinesBR.Color16MA();
       
  1364 			linesTL = aLinesTL.Color16MA();
       
  1365 			linesTR = rgbLinesTR.Color16MA();
       
  1366 			linesBL = rgbLinesBL.Color16MA();
       
  1367 			break;
       
  1368 			}
       
  1369 		case EUidPixelFormatARGB_8888_PRE:
       
  1370 			{
       
  1371 			linesBR = aLinesBR.Color16MAP();
       
  1372 			linesTL = aLinesTL.Color16MAP();
       
  1373 			linesTR = rgbLinesTR.Color16MAP();
       
  1374 			linesBL = rgbLinesBL.Color16MAP();
       
  1375 			break;
       
  1376 			}
       
  1377 		case EUidPixelFormatXRGB_4444:
       
  1378 		case EUidPixelFormatARGB_4444:
       
  1379 			{
       
  1380 			linesBR = aLinesBR.Color4K();
       
  1381 			linesTL = aLinesTL.Color4K();
       
  1382 			linesTR = rgbLinesTR.Color4K();
       
  1383 			linesBL = rgbLinesBL.Color4K();
       
  1384 			use16 = ETrue;
       
  1385 			break;
       
  1386 			}
       
  1387 		case EUidPixelFormatRGB_565:
       
  1388 			{
       
  1389 			linesBR = aLinesBR.Color64K();
       
  1390 			linesTL = aLinesTL.Color64K();
       
  1391 			linesTR = rgbLinesTR.Color64K();
       
  1392 			linesBL = rgbLinesBL.Color64K();
       
  1393 			use16 = ETrue;
       
  1394 			break;
       
  1395 			}
       
  1396 		default:
       
  1397 			{
       
  1398 			User::Leave(KErrNotSupported);
       
  1399 			break;
       
  1400 			}
       
  1401 		}
       
  1402 	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
       
  1403 		{
       
  1404 		User::Leave(KErrCorrupt);
       
  1405 		}
       
  1406 	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
       
  1407 		{
       
  1408 		User::Leave(KErrNotReady);
       
  1409 		}
       
  1410 	RChunk chunk;
       
  1411 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
  1412 	TUint8* surfacePtr = chunk.Base();
       
  1413 	TPoint innerXY(info.iSize.iWidth-1,info.iSize.iHeight-1);
       
  1414 	if (use16)
       
  1415 		{
       
  1416 		if ( info.iSize.iWidth*2>info.iStride)
       
  1417 			{
       
  1418 			User::Leave(KErrOverflow);
       
  1419 			}
       
  1420 		FanFill<TUint16>(innerXY,info.iStride/2,(TUint16*)surfacePtr,linesTL,linesBR,linesBL,linesTR);
       
  1421 		}
       
  1422 	else
       
  1423 		{
       
  1424 		if ( info.iSize.iWidth*4>info.iStride)
       
  1425 			{
       
  1426 			User::Leave(KErrOverflow);
       
  1427 			}
       
  1428 		FanFill<TUint>(innerXY,info.iStride/4,(TUint*)surfacePtr,linesTL,linesBR,linesBL,linesTR);
       
  1429 		}
       
  1430 	
       
  1431 	chunk.Close();
       
  1432 
       
  1433 	iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
  1434 	}
       
  1435 /**
       
  1436 Fill the given surface with vertical line at the given position
       
  1437 
       
  1438 Similar to FillSurfaceL(), but with a vertical line overlayed. 
       
  1439 The position along the surface is given as a percentage from the left
       
  1440 
       
  1441 @param aSurface	The surface to be filled.
       
  1442 @param aColor	The color to fill it with.
       
  1443 @param aLine	The color of the line.
       
  1444 @param aPosition Position of the vertical line given as a percentage across the surface from the left edge
       
  1445 */
       
  1446 void CSurfaceUtility::LineFillSurfaceL(TSurfaceId& aSurface, const TRgb& aBackColor, const TRgb& aLineColor, TInt aPosition)
       
  1447 	{
       
  1448 	if (aPosition<0 || aPosition>100)
       
  1449 		{
       
  1450 		aPosition=0;
       
  1451 		}
       
  1452 	FillSurfaceL(aSurface,aBackColor);
       
  1453 	RSurfaceManager::TInfoBuf infoBuf;
       
  1454 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
  1455 	
       
  1456 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
  1457 	TUint32 lineColor = 0;
       
  1458 	TBool use16 = EFalse;
       
  1459 	
       
  1460 	switch (info.iPixelFormat)
       
  1461 		{
       
  1462 		case EUidPixelFormatXRGB_8888:
       
  1463 			{
       
  1464 			lineColor = aLineColor.Color16MU();
       
  1465 #ifdef ALPHA_FIX_24BIT
       
  1466 			lineColor |= ((ALPHA_FIX_24BIT)&0xff)<<24;
       
  1467 #endif
       
  1468 			break;
       
  1469 			}
       
  1470 		case EUidPixelFormatARGB_8888:
       
  1471 			{
       
  1472 			lineColor = aLineColor.Color16MA();
       
  1473 			break;
       
  1474 			}
       
  1475 		case EUidPixelFormatARGB_8888_PRE:
       
  1476 			{
       
  1477 			lineColor = aLineColor.Color16MAP();
       
  1478 			break;
       
  1479 			}
       
  1480 		case EUidPixelFormatXRGB_4444:
       
  1481 		case EUidPixelFormatARGB_4444:
       
  1482 			{
       
  1483 			lineColor = aLineColor.Color4K();
       
  1484 			use16 = ETrue;
       
  1485 			break;
       
  1486 			}
       
  1487 		case EUidPixelFormatRGB_565:
       
  1488 			{
       
  1489 			lineColor = aLineColor.Color64K();
       
  1490 			use16 = ETrue;
       
  1491 			break;
       
  1492 			}
       
  1493 		default:
       
  1494 			{
       
  1495 			User::Leave(KErrNotSupported);
       
  1496 			break;
       
  1497 			}
       
  1498 		}
       
  1499 	RChunk chunk;
       
  1500 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
  1501 	
       
  1502 	TUint8* surfacePtr = chunk.Base();
       
  1503 	if (use16)
       
  1504 		{
       
  1505 		DdaLine<TUint16>((info.iSize.iWidth*aPosition)/100,0,(info.iSize.iWidth*aPosition)/100,
       
  1506 				info.iSize.iHeight-1,info.iStride/2,(TUint16*)surfacePtr,lineColor);
       
  1507 		}
       
  1508 	else
       
  1509 		{
       
  1510 		DdaLine<TUint>((info.iSize.iWidth*aPosition)/100,0,(info.iSize.iWidth*aPosition)/100,
       
  1511 				info.iSize.iHeight-1,info.iStride/4,(TUint*)surfacePtr,lineColor);
       
  1512 		}
       
  1513 	chunk.Close();
       
  1514 	iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
  1515 	}
       
  1516 /**
       
  1517  * Generates a bitmap equivalent to the surface.
       
  1518  * Can reuse an existing bitmap or create a new bitmap.
       
  1519  * The existing bitmap must be an exact match (eg previously generated by this method)
       
  1520  **/
       
  1521 CFbsBitmap* CSurfaceUtility::EquivalentBitmapL(TSurfaceId& aSurface,CFbsBitmap* aCopyToMayBeNull)
       
  1522 	{
       
  1523 	RSurfaceManager::TInfoBuf infoBuf;
       
  1524 	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
       
  1525 
       
  1526 	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
       
  1527 	TInt bytesPerPixel=0;
       
  1528 	TDisplayMode	bitmapMode = ENone;
       
  1529 	switch (info.iPixelFormat)
       
  1530 		{
       
  1531 		case EUidPixelFormatXRGB_8888:
       
  1532 			{
       
  1533 			bitmapMode = EColor16MU;
       
  1534 			bytesPerPixel = 4;
       
  1535 			break;
       
  1536 			}
       
  1537 		case EUidPixelFormatARGB_8888:
       
  1538 			{
       
  1539 			bitmapMode=EColor16MA;
       
  1540 			bytesPerPixel = 4;
       
  1541 			break;
       
  1542 			}
       
  1543 		case EUidPixelFormatARGB_8888_PRE:
       
  1544 			{
       
  1545 			bitmapMode=EColor16MAP;
       
  1546 			bytesPerPixel = 4;
       
  1547 			break;
       
  1548 			}
       
  1549 		case EUidPixelFormatXRGB_4444:
       
  1550 		case EUidPixelFormatARGB_4444:
       
  1551 			{
       
  1552 			bitmapMode=EColor4K;
       
  1553 			bytesPerPixel = 2;
       
  1554 			break;
       
  1555 			}
       
  1556 		case EUidPixelFormatRGB_565:
       
  1557 			{
       
  1558 			bitmapMode=EColor64K;
       
  1559 			bytesPerPixel = 2;
       
  1560 			break;
       
  1561 			}
       
  1562 		default:
       
  1563 			{
       
  1564 			User::Leave(KErrNotSupported);
       
  1565 			break;
       
  1566 			}
       
  1567 		}
       
  1568 	CFbsBitmap* retVal=NULL;
       
  1569 	if (aCopyToMayBeNull)
       
  1570 		{
       
  1571 		retVal=aCopyToMayBeNull;
       
  1572 		if (retVal->SizeInPixels()!=info.iSize)
       
  1573 			User::Leave(KErrCorrupt);
       
  1574 		if (retVal->DisplayMode()!=bitmapMode)
       
  1575 			User::Leave(KErrCorrupt);
       
  1576 		}
       
  1577 	else
       
  1578 		{
       
  1579 		retVal=new CFbsBitmap;
       
  1580 		CleanupStack::PushL(retVal);
       
  1581 		User::LeaveIfError(retVal->Create(info.iSize,bitmapMode));
       
  1582 		}
       
  1583 	RChunk chunk;
       
  1584 	CleanupClosePushL(chunk);
       
  1585 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
  1586 	TUint8* surfacePtr = chunk.Base();
       
  1587 	TUint8* bitmapPtr = (TUint8*)retVal->DataAddress();
       
  1588 	TInt copyBytes=info.iSize.iWidth*bytesPerPixel;
       
  1589 	for (TInt y=0;y<info.iSize.iHeight;y++)
       
  1590 		{
       
  1591 		Mem::Copy(bitmapPtr,surfacePtr,copyBytes);
       
  1592 		surfacePtr+=info.iStride;
       
  1593 		bitmapPtr+=retVal->DataStride();
       
  1594 		}
       
  1595 	CleanupStack::PopAndDestroy(&chunk);
       
  1596 	if (!aCopyToMayBeNull)
       
  1597 		CleanupStack::Pop(retVal);
       
  1598 	return retVal;
       
  1599 	}
       
  1600 
       
  1601 /**
       
  1602 Destroy a surface.
       
  1603 
       
  1604 As well as destroying the surface, it is removed from the set held for
       
  1605 destruction during tear down.
       
  1606 
       
  1607 @param aSurface	The surface to be destroyed.
       
  1608 */
       
  1609 void CSurfaceUtility::DestroySurface(TSurfaceId& aSurface)
       
  1610 	{
       
  1611 	TInt index = iSurfaces.Find(aSurface);
       
  1612 	
       
  1613 	if (index != KErrNotFound)
       
  1614 		{
       
  1615 		iSurfaces.Remove(index);
       
  1616 		}
       
  1617 
       
  1618 	TInt err = iManager.CloseSurface(aSurface);
       
  1619 	if (err!=KErrNone)
       
  1620 		LOG(("Error closing surfaces: 0x%X\n", err));
       
  1621 	}
       
  1622 
       
  1623 
       
  1624 /**
       
  1625 Submit an update to a surface to the update server.
       
  1626 
       
  1627 @param aScreenNumber	The screen to be updated where the surface is shown.
       
  1628 @param aSurface	The surface which has been updated.
       
  1629 @param aRegion	The area of the surface affected, or NULL for all of it.*/
       
  1630 void CSurfaceUtility::SubmitUpdate(TInt aScreenNumber, const TSurfaceId& aSurface, const TRegion* aRegion,TInt aBufferNumber)
       
  1631 	{
       
  1632 	TInt err =iSurfaceUpdateSession.SubmitUpdate(aScreenNumber, aSurface, aBufferNumber, aRegion); 
       
  1633 	if (err!=KErrNone)
       
  1634 		LOG(("Error submitting update: 0x%X\n", err));
       
  1635 	}
       
  1636 
       
  1637 /**
       
  1638 Map and submit an update to a surface to the update server.
       
  1639 
       
  1640 @param aChunk	The chunk of memory to be mapped
       
  1641 @param aScreenNumber	The screen to be updated where the surface is shown.
       
  1642 @param aSurface	The surface which has been updated.
       
  1643 @param aRegion	The area of the surface affected, or NULL for all of it.*/
       
  1644 void CSurfaceUtility::MapAndSubmitUpdateL(RChunk& aChunk, 
       
  1645 		                                TInt aScreenNumber, 
       
  1646 		                                const TSurfaceId& aSurface, 
       
  1647 		                                const TRegion* aRegion)
       
  1648 	{
       
  1649 	User::LeaveIfError(iManager.MapSurface(aSurface, aChunk));
       
  1650 	aChunk.Close();
       
  1651 	TInt err =iSurfaceUpdateSession.SubmitUpdate(aScreenNumber, aSurface, 0, aRegion); 
       
  1652 	if (err!=KErrNone)
       
  1653 		LOG(("Error submitting update: 0x%X\n", err));
       
  1654 	}
       
  1655 
       
  1656 void CSurfaceUtility::MapSurfaceL(const TSurfaceId& aSurface, RChunk& aChunk)
       
  1657 	{
       
  1658 	User::LeaveIfError(iManager.MapSurface(aSurface, aChunk));
       
  1659 	}
       
  1660 
       
  1661 void CSurfaceUtility::CopyBitmapToSurfaceL(TSurfaceId& aSurface, const CFbsBitmap& aBitmap)
       
  1662 	{
       
  1663 	TSize size = SurfaceSize(aSurface);
       
  1664 	
       
  1665 	TDisplayMode bmpFormat = aBitmap.DisplayMode();
       
  1666 	TInt stride = size.iWidth * 4;		// Default to four bytes per pixel
       
  1667 
       
  1668 	RChunk chunk;
       
  1669 	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
       
  1670 	CleanupClosePushL(chunk);
       
  1671 
       
  1672 	// Copy the data from the bitmap into the surface.
       
  1673 	TPoint start;
       
  1674 	for (start.iY = 0; start.iY < size.iHeight; start.iY++)
       
  1675 		{
       
  1676 		// Set up a descriptor for the current line in the surface and get pixels.
       
  1677 		TPtr8 ptr(chunk.Base() + start.iY * stride, stride);
       
  1678 		aBitmap.GetScanLine(ptr, start, size.iWidth, bmpFormat);
       
  1679 		}
       
  1680 
       
  1681 	TInt err =iSurfaceUpdateSession.SubmitUpdate(0, aSurface, 0, NULL);
       
  1682 	if (err!=KErrNone)
       
  1683 		{
       
  1684 		LOG(("Error submitting update: 0x%X\n", err));
       
  1685 		}
       
  1686 
       
  1687 	CleanupStack::PopAndDestroy(/* chunk */);
       
  1688 	}
       
  1689 
       
  1690 /**
       
  1691 CActiveListener factory function
       
  1692 @return A CActiveListener object
       
  1693 */
       
  1694 CActiveListener* CActiveListener::NewLC()
       
  1695 	{
       
  1696     CActiveListener* self = new(ELeave) CActiveListener();
       
  1697 	CleanupStack::PushL(self);
       
  1698 	return self;
       
  1699 	}
       
  1700 
       
  1701 /**
       
  1702 Constructor for class CActiveListener
       
  1703 */
       
  1704 CActiveListener::CActiveListener() : CActive(EPriorityLow)
       
  1705 	{
       
  1706  	CActiveScheduler::Add(this);
       
  1707 	}
       
  1708 
       
  1709 /**
       
  1710 Destructor
       
  1711 */
       
  1712 CActiveListener::~CActiveListener()
       
  1713 	{
       
  1714 	}
       
  1715 
       
  1716 /**
       
  1717 Handles the request.
       
  1718 This function is derived from CActive
       
  1719 */
       
  1720 void CActiveListener::RunL()
       
  1721 	{
       
  1722 	CActiveScheduler::Stop();
       
  1723 	}
       
  1724 
       
  1725 /**
       
  1726 Cancels the outstanding request.
       
  1727 This function is derived from CActive
       
  1728 */
       
  1729 void CActiveListener::DoCancel()
       
  1730 	{
       
  1731 	}
       
  1732 
       
  1733 /**
       
  1734 Initializes the CActiveListener
       
  1735 */
       
  1736 void CActiveListener::Initialize()
       
  1737 	{
       
  1738 	iStatus = KRequestPending;
       
  1739 	SetActive();
       
  1740 	}
       
  1741 
       
  1742 /**
       
  1743 Check that the request has been cancelled.
       
  1744 @return A boolean indicating whether the request has been cancelled or not
       
  1745 */
       
  1746 TBool CActiveListener::IsRequestCancelled()
       
  1747 	{
       
  1748 	return (iStatus == KErrCancel);
       
  1749 	}
       
  1750 
       
  1751 /**
       
  1752 A helper function that returns the bytes per pixel for a given pixel format uid
       
  1753 
       
  1754 @param aPixelFormat Pixel format UID to convert
       
  1755 @return The bytes per pixel
       
  1756 */
       
  1757 TInt CSurfaceUtility::BytesPerPixelL(TUidPixelFormat aPixelFormat)
       
  1758 	{
       
  1759 	TInt bytesPerPixel = 0;
       
  1760 	switch (aPixelFormat)
       
  1761 		{
       
  1762 		case EUidPixelFormatXRGB_8888:
       
  1763 		case EUidPixelFormatARGB_8888:
       
  1764 		case EUidPixelFormatARGB_8888_PRE:
       
  1765 			{
       
  1766 			bytesPerPixel = 4;
       
  1767 			break;
       
  1768 			}
       
  1769 		case EUidPixelFormatXRGB_4444:
       
  1770 		case EUidPixelFormatARGB_4444:
       
  1771 		case EUidPixelFormatRGB_565:
       
  1772 			{
       
  1773 			bytesPerPixel = 2;
       
  1774 			break;
       
  1775 			}
       
  1776 		default:
       
  1777 			{
       
  1778 			User::Leave(KErrNotSupported);
       
  1779 			break;
       
  1780 			}
       
  1781 		}
       
  1782 	return bytesPerPixel;
       
  1783 	}
       
  1784