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