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