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