os/graphics/graphicscomposition/openwfsupport/test/tstreamoperation/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)
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
sl@0
   127
// Close only closes the local array, while Reset resets the active array (may be external)
sl@0
   128
void CSurfaceUtility::RHeapSurfaceArray::Close()
sl@0
   129
	{
sl@0
   130
	iLocalArray.Close();
sl@0
   131
	}
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
sl@0
   138
// Close only closes the local array, while Reset resets the active array (may be external)
sl@0
   139
inline void CSurfaceUtility::RHeapSurfaceArray::Reset()
sl@0
   140
	{
sl@0
   141
	iUseArray->Reset();
sl@0
   142
	}
sl@0
   143
sl@0
   144
void CSurfaceUtility::RHeapSurfaceArray::AppendL(const TSurfaceId &anEntry)
sl@0
   145
	{
sl@0
   146
	iUseArray->AppendL(anEntry);
sl@0
   147
	}
sl@0
   148
sl@0
   149
TInt CSurfaceUtility::RHeapSurfaceArray::Find(const TSurfaceId &anEntry) const
sl@0
   150
	{
sl@0
   151
	return iUseArray->Find(anEntry);
sl@0
   152
	}
sl@0
   153
sl@0
   154
void CSurfaceUtility::RHeapSurfaceArray::Remove(TInt anIndex)
sl@0
   155
	{
sl@0
   156
	iUseArray->Remove(anIndex);
sl@0
   157
	}
sl@0
   158
sl@0
   159
/**
sl@0
   160
Cleanup stack helper object, holding references to both utility and surface, so
sl@0
   161
that the standard Close() semantics can be used.
sl@0
   162
*/
sl@0
   163
class TSurfaceCleanup
sl@0
   164
	{
sl@0
   165
public:
sl@0
   166
	TSurfaceCleanup(CSurfaceUtility& aUtility, TSurfaceId& aSurface)
sl@0
   167
		: iUtility(aUtility), iSurface(aSurface)
sl@0
   168
		{}
sl@0
   169
	void Close()
sl@0
   170
		{
sl@0
   171
		// Removes the surface from the list of surfaces to clean up, and closes
sl@0
   172
		// the surface reference.
sl@0
   173
		iUtility.DestroySurface(iSurface);
sl@0
   174
		}
sl@0
   175
private:
sl@0
   176
	CSurfaceUtility& iUtility;
sl@0
   177
	TSurfaceId& iSurface;
sl@0
   178
	};
sl@0
   179
sl@0
   180
/**
sl@0
   181
Get the size of a surface.
sl@0
   182
sl@0
   183
@param aSurface The surface to get the size for.
sl@0
   184
@return The size in pixels, or empty on failure.
sl@0
   185
*/
sl@0
   186
TSize CSurfaceUtility::SurfaceSize(const TSurfaceId& aSurface)
sl@0
   187
	{
sl@0
   188
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   189
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   190
sl@0
   191
	if (iManager.SurfaceInfo(aSurface, infoBuf) == KErrNone)
sl@0
   192
		{
sl@0
   193
		return info.iSize;
sl@0
   194
		}
sl@0
   195
sl@0
   196
	return TSize();
sl@0
   197
	}
sl@0
   198
sl@0
   199
/**
sl@0
   200
Create a surface using the surface manager.
sl@0
   201
sl@0
   202
Stores the ID for tear down, as well as returning it.
sl@0
   203
sl@0
   204
@param aSize Dimensions of the surface.
sl@0
   205
@param aPixelFormat	UID of the pixel format.
sl@0
   206
@param aStride	Stride value for the surface (usually bytes per pixel * width)
sl@0
   207
@leave May leave due to lack of memory.
sl@0
   208
@return New surface's ID.
sl@0
   209
*/
sl@0
   210
TSurfaceId CSurfaceUtility::CreateSurfaceL(const TSize& aSize, TUidPixelFormat aPixelFormat, TInt aStride, TInt aBuffers)
sl@0
   211
	{
sl@0
   212
	RSurfaceManager::TSurfaceCreationAttributesBuf bf;
sl@0
   213
	RSurfaceManager::TSurfaceCreationAttributes& b = bf();
sl@0
   214
	if (aStride<aSize.iWidth*BytesPerPixelL(aPixelFormat))
sl@0
   215
	    {
sl@0
   216
	    User::Leave(KErrOverflow);
sl@0
   217
	    }
sl@0
   218
	b.iSize.iWidth = aSize.iWidth;
sl@0
   219
	b.iSize.iHeight = aSize.iHeight;
sl@0
   220
	b.iBuffers = aBuffers;				// number of buffers in the surface
sl@0
   221
	b.iPixelFormat = aPixelFormat;
sl@0
   222
	b.iStride = aStride;		// Number of bytes between start of one line and start of next
sl@0
   223
	b.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
sl@0
   224
	b.iAlignment = 4;			// alignment, 1,2,4,8 byte aligned
sl@0
   225
	b.iContiguous = EFalse;
sl@0
   226
	b.iMappable = ETrue;
sl@0
   227
sl@0
   228
	TSurfaceId surface = TSurfaceId::CreateNullId();
sl@0
   229
sl@0
   230
	User::LeaveIfError(iManager.CreateSurface(bf, surface));
sl@0
   231
	iSurfaces.AppendL(surface);
sl@0
   232
	return surface;
sl@0
   233
	}
sl@0
   234
sl@0
   235
/**
sl@0
   236
A helper function that returns the bytes per pixel for a given pixel format uid
sl@0
   237
sl@0
   238
@param aPixelFormat Pixel format UID to convert
sl@0
   239
@return The bytes per pixel
sl@0
   240
*/
sl@0
   241
TInt CSurfaceUtility::BytesPerPixelL(TUidPixelFormat aPixelFormat)
sl@0
   242
	{
sl@0
   243
	TInt bytesPerPixel = 0;
sl@0
   244
	switch (aPixelFormat)
sl@0
   245
		{
sl@0
   246
		case EUidPixelFormatXRGB_8888:
sl@0
   247
		case EUidPixelFormatARGB_8888:
sl@0
   248
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   249
			{
sl@0
   250
			bytesPerPixel = 4;
sl@0
   251
			break;
sl@0
   252
			}
sl@0
   253
		case EUidPixelFormatXRGB_4444:
sl@0
   254
		case EUidPixelFormatARGB_4444:
sl@0
   255
		case EUidPixelFormatRGB_565:
sl@0
   256
			{
sl@0
   257
			bytesPerPixel = 2;
sl@0
   258
			break;
sl@0
   259
			}
sl@0
   260
		default:
sl@0
   261
			{
sl@0
   262
			User::Leave(KErrNotSupported);
sl@0
   263
			break;
sl@0
   264
			}
sl@0
   265
		}
sl@0
   266
	return bytesPerPixel;
sl@0
   267
	}
sl@0
   268
sl@0
   269
/**
sl@0
   270
Fill the given surface with a color.
sl@0
   271
sl@0
   272
@param aSurface	The surface to be filled.
sl@0
   273
@param aColor	The color to fill it with.
sl@0
   274
*/
sl@0
   275
void CSurfaceUtility::FillSurfaceL(TSurfaceId& aSurface, const TRgb& aColor)
sl@0
   276
	{
sl@0
   277
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   278
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   279
sl@0
   280
	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
sl@0
   281
	TUint32 color = 0;
sl@0
   282
	TBool use16 = EFalse;
sl@0
   283
sl@0
   284
	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
sl@0
   285
		{
sl@0
   286
		User::Leave(KErrCorrupt);
sl@0
   287
		}
sl@0
   288
	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
sl@0
   289
		{
sl@0
   290
		User::Leave(KErrNotReady);
sl@0
   291
		}
sl@0
   292
sl@0
   293
	switch (info.iPixelFormat)
sl@0
   294
		{
sl@0
   295
		case EUidPixelFormatXRGB_8888:
sl@0
   296
			{
sl@0
   297
			color = aColor.Color16MU();
sl@0
   298
#ifdef ALPHA_FIX_24BIT
sl@0
   299
			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
sl@0
   300
#endif
sl@0
   301
			break;
sl@0
   302
			}
sl@0
   303
		case EUidPixelFormatARGB_8888:
sl@0
   304
			{
sl@0
   305
			color = aColor.Color16MA();
sl@0
   306
			break;
sl@0
   307
			}
sl@0
   308
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   309
			{
sl@0
   310
			color = aColor.Color16MAP();
sl@0
   311
			break;
sl@0
   312
			}
sl@0
   313
		case EUidPixelFormatXRGB_4444:
sl@0
   314
		case EUidPixelFormatARGB_4444:
sl@0
   315
			{
sl@0
   316
			color = aColor.Color4K();
sl@0
   317
			use16 = ETrue;
sl@0
   318
			break;
sl@0
   319
			}
sl@0
   320
		case EUidPixelFormatRGB_565:
sl@0
   321
			{
sl@0
   322
			color = aColor.Color64K();
sl@0
   323
			use16 = ETrue;
sl@0
   324
			break;
sl@0
   325
			}
sl@0
   326
		default:
sl@0
   327
			{
sl@0
   328
			User::Leave(KErrNotSupported);
sl@0
   329
			break;
sl@0
   330
			}
sl@0
   331
		}
sl@0
   332
sl@0
   333
	RChunk chunk;
sl@0
   334
	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
sl@0
   335
	CleanupClosePushL(chunk);
sl@0
   336
sl@0
   337
	TInt offsetToFirstBuffer;
sl@0
   338
	User::LeaveIfError(iManager.GetBufferOffset(aSurface, 0, offsetToFirstBuffer));
sl@0
   339
	TUint8* surfacePtr = chunk.Base() + offsetToFirstBuffer;
sl@0
   340
	TUint8* linePtr = surfacePtr;
sl@0
   341
sl@0
   342
	if (use16)
sl@0
   343
		{
sl@0
   344
		if ( info.iSize.iWidth*2>info.iStride)
sl@0
   345
			{
sl@0
   346
			User::Leave(KErrOverflow);
sl@0
   347
			}
sl@0
   348
		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
sl@0
   349
sl@0
   350
		// Fill first line
sl@0
   351
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   352
			{
sl@0
   353
			ptr[xx] = (TUint16)color;
sl@0
   354
			}
sl@0
   355
		}
sl@0
   356
	else
sl@0
   357
		{
sl@0
   358
		if ( info.iSize.iWidth*4>info.iStride)
sl@0
   359
			{
sl@0
   360
			User::Leave(KErrOverflow);
sl@0
   361
			}
sl@0
   362
		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
sl@0
   363
sl@0
   364
		// Fill first line
sl@0
   365
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   366
			{
sl@0
   367
			ptr[xx] = color;
sl@0
   368
			}
sl@0
   369
		}
sl@0
   370
sl@0
   371
	// Now copy that to the other lines
sl@0
   372
	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
sl@0
   373
		{
sl@0
   374
		linePtr += info.iStride;
sl@0
   375
		Mem::Copy(linePtr, surfacePtr, info.iSize.iWidth * BytesPerPixelL(info.iPixelFormat));
sl@0
   376
		}
sl@0
   377
	
sl@0
   378
	TInt err = SubmitUpdate(KAllScreens, aSurface, 0, NULL);
sl@0
   379
	if (err!=KErrNone)
sl@0
   380
		LOG(("Error submitting update: 0x%X\n", err));
sl@0
   381
sl@0
   382
	CleanupStack::PopAndDestroy(/* chunk */);
sl@0
   383
	}
sl@0
   384
sl@0
   385
/**
sl@0
   386
Fill the given surface with a color.
sl@0
   387
sl@0
   388
@param aSurface	The surface to be filled.
sl@0
   389
@param aBuffer 	The buffer to fill.
sl@0
   390
@param aColor	The color to fill it with.
sl@0
   391
*/
sl@0
   392
void CSurfaceUtility::FillSurfaceL(TSurfaceId& aSurface, TInt aBuffer, const TRgb& aColor)
sl@0
   393
	{
sl@0
   394
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   395
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   396
sl@0
   397
	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
sl@0
   398
	TUint32 color = 0;
sl@0
   399
	TBool use16 = EFalse;
sl@0
   400
	TInt numBuffers = info.iBuffers;
sl@0
   401
	if (aBuffer < 0 || aBuffer >= numBuffers)
sl@0
   402
		{
sl@0
   403
		User::Leave(KErrArgument);
sl@0
   404
		}
sl@0
   405
sl@0
   406
	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
sl@0
   407
		{
sl@0
   408
		User::Leave(KErrCorrupt);
sl@0
   409
		}
sl@0
   410
	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
sl@0
   411
		{
sl@0
   412
		User::Leave(KErrNotReady);
sl@0
   413
		}
sl@0
   414
sl@0
   415
	switch (info.iPixelFormat)
sl@0
   416
		{
sl@0
   417
		case EUidPixelFormatXRGB_8888:
sl@0
   418
			{
sl@0
   419
			color = aColor.Color16MU();
sl@0
   420
#ifdef ALPHA_FIX_24BIT
sl@0
   421
			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
sl@0
   422
#endif
sl@0
   423
			break;
sl@0
   424
			}
sl@0
   425
		case EUidPixelFormatARGB_8888:
sl@0
   426
			{
sl@0
   427
			color = aColor.Color16MA();
sl@0
   428
			break;
sl@0
   429
			}
sl@0
   430
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   431
			{
sl@0
   432
			color = aColor.Color16MAP();
sl@0
   433
			break;
sl@0
   434
			}
sl@0
   435
		case EUidPixelFormatXRGB_4444:
sl@0
   436
		case EUidPixelFormatARGB_4444:
sl@0
   437
			{
sl@0
   438
			color = aColor.Color4K();
sl@0
   439
			use16 = ETrue;
sl@0
   440
			break;
sl@0
   441
			}
sl@0
   442
		case EUidPixelFormatRGB_565:
sl@0
   443
			{
sl@0
   444
			color = aColor.Color64K();
sl@0
   445
			use16 = ETrue;
sl@0
   446
			break;
sl@0
   447
			}
sl@0
   448
		default:
sl@0
   449
			{
sl@0
   450
			User::Leave(KErrNotSupported);
sl@0
   451
			break;
sl@0
   452
			}
sl@0
   453
		}
sl@0
   454
sl@0
   455
	RChunk chunk;
sl@0
   456
	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
sl@0
   457
	CleanupClosePushL(chunk);
sl@0
   458
sl@0
   459
	TInt offsetToBuffer;
sl@0
   460
	User::LeaveIfError(iManager.GetBufferOffset(aSurface, aBuffer, offsetToBuffer));
sl@0
   461
	TUint8* surfacePtr = chunk.Base() + offsetToBuffer;
sl@0
   462
	TUint8* linePtr = surfacePtr;
sl@0
   463
sl@0
   464
	if (use16)
sl@0
   465
		{
sl@0
   466
		if ( info.iSize.iWidth*2>info.iStride)
sl@0
   467
			{
sl@0
   468
			User::Leave(KErrOverflow);
sl@0
   469
			}
sl@0
   470
		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
sl@0
   471
sl@0
   472
		// Fill first line
sl@0
   473
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   474
			{
sl@0
   475
			ptr[xx] = (TUint16)color;
sl@0
   476
			}
sl@0
   477
		}
sl@0
   478
	else
sl@0
   479
		{
sl@0
   480
		if ( info.iSize.iWidth*4>info.iStride)
sl@0
   481
			{
sl@0
   482
			User::Leave(KErrOverflow);
sl@0
   483
			}
sl@0
   484
		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
sl@0
   485
sl@0
   486
		// Fill first line
sl@0
   487
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   488
			{
sl@0
   489
			ptr[xx] = color;
sl@0
   490
			}
sl@0
   491
		}
sl@0
   492
sl@0
   493
	// Now copy that to the other lines
sl@0
   494
	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
sl@0
   495
		{
sl@0
   496
		linePtr += info.iStride;
sl@0
   497
		Mem::Copy(linePtr, surfacePtr, info.iSize.iWidth * BytesPerPixelL(info.iPixelFormat));
sl@0
   498
		}
sl@0
   499
	
sl@0
   500
	TInt err = SubmitUpdate(KAllScreens, aSurface, 0, NULL);
sl@0
   501
	if (err!=KErrNone)
sl@0
   502
		LOG(("Error submitting update: 0x%X\n", err));
sl@0
   503
sl@0
   504
	CleanupStack::PopAndDestroy(/* chunk */);
sl@0
   505
	}
sl@0
   506
sl@0
   507
sl@0
   508
/**
sl@0
   509
Destroy a surface.
sl@0
   510
sl@0
   511
As well as destroying the surface, it is removed from the set held for
sl@0
   512
destruction during tear down.
sl@0
   513
sl@0
   514
@param aSurface	The surface to be destroyed.
sl@0
   515
*/
sl@0
   516
void CSurfaceUtility::DestroySurface(TSurfaceId& aSurface)
sl@0
   517
	{
sl@0
   518
	TInt index = iSurfaces.Find(aSurface);
sl@0
   519
	
sl@0
   520
	if (index != KErrNotFound)
sl@0
   521
		{
sl@0
   522
		iSurfaces.Remove(index);
sl@0
   523
		}
sl@0
   524
sl@0
   525
	TInt err = iManager.CloseSurface(aSurface);
sl@0
   526
	if (err!=KErrNone)
sl@0
   527
		LOG(("Error closing surfaces: 0x%X\n", err));
sl@0
   528
	}
sl@0
   529
sl@0
   530
sl@0
   531
/**
sl@0
   532
Submit an update to a surface to the update server.
sl@0
   533
sl@0
   534
@param aScreenNumber	The screen to be updated where the surface is shown.
sl@0
   535
@param aSurface	The surface which has been updated.
sl@0
   536
@param aRegion	The area of the surface affected, or NULL for all of it.
sl@0
   537
*/
sl@0
   538
TInt CSurfaceUtility::SubmitUpdate(TInt /* aScreenNumber */, const TSurfaceId& aSurface,TInt aBufferNumber, TInt aNullRegion)
sl@0
   539
    {
sl@0
   540
    if (aNullRegion==0)
sl@0
   541
        {
sl@0
   542
        return SubmitUpdate(KAllScreens, aSurface, aBufferNumber);
sl@0
   543
        }
sl@0
   544
    else
sl@0
   545
        if (aBufferNumber==0)
sl@0
   546
            {
sl@0
   547
            return SubmitUpdate(KAllScreens, aSurface, aNullRegion);
sl@0
   548
            }
sl@0
   549
        else
sl@0
   550
            {
sl@0
   551
            return KErrNotSupported;
sl@0
   552
            }
sl@0
   553
    }
sl@0
   554
sl@0
   555
TInt CSurfaceUtility::SubmitUpdate(TInt /* aScreenNumber */, const TSurfaceId& aSurface, const TRegion* aRegion,TInt aBufferNumber)
sl@0
   556
    {
sl@0
   557
    return SubmitUpdate(KAllScreens, aSurface, aBufferNumber, aRegion);
sl@0
   558
    }
sl@0
   559
sl@0
   560
TInt CSurfaceUtility::SubmitUpdate(TInt /* aScreenNumber */, const TSurfaceId& aSurface,TInt aBufferNumber, const TRegion* aRegion)
sl@0
   561
	{
sl@0
   562
	if (!iSurfaceUpdateSession.Handle())
sl@0
   563
	    {
sl@0
   564
	    iSurfaceUpdateSession.Connect();
sl@0
   565
	    }
sl@0
   566
    if (!iSurfaceUpdateSession.Handle())
sl@0
   567
        {
sl@0
   568
        LOG(("Error - SUS client not started!"));
sl@0
   569
        return KErrNotReady;
sl@0
   570
        }
sl@0
   571
    else
sl@0
   572
        {
sl@0
   573
        TInt err =iSurfaceUpdateSession.SubmitUpdate(KAllScreens, aSurface, aBufferNumber, aRegion); 
sl@0
   574
        if (err!=KErrNone)
sl@0
   575
            LOG(("Error submitting update: 0x%X\n", err));
sl@0
   576
        return err;
sl@0
   577
        }
sl@0
   578
	}
sl@0
   579
sl@0
   580
void CSurfaceUtility::FillNativeStreamSurfaceL(TSurfaceId& aSurface, TUint8* aBufferPtr, const TRgb& aColor)
sl@0
   581
	{
sl@0
   582
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   583
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   584
sl@0
   585
	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
sl@0
   586
	TUint32 color = 0;
sl@0
   587
	TBool use16 = EFalse;
sl@0
   588
sl@0
   589
	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
sl@0
   590
		{
sl@0
   591
		User::Leave(KErrCorrupt);
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(KErrNotReady);
sl@0
   596
		}
sl@0
   597
sl@0
   598
	switch (info.iPixelFormat)
sl@0
   599
		{
sl@0
   600
		case EUidPixelFormatXRGB_8888:
sl@0
   601
			{
sl@0
   602
			color = aColor.Color16MU();
sl@0
   603
#ifdef ALPHA_FIX_24BIT
sl@0
   604
			color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
sl@0
   605
#endif
sl@0
   606
			break;
sl@0
   607
			}
sl@0
   608
		case EUidPixelFormatARGB_8888:
sl@0
   609
			{
sl@0
   610
			color = aColor.Color16MA();
sl@0
   611
			break;
sl@0
   612
			}
sl@0
   613
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   614
			{
sl@0
   615
			color = aColor.Color16MAP();
sl@0
   616
			break;
sl@0
   617
			}
sl@0
   618
		case EUidPixelFormatXRGB_4444:
sl@0
   619
		case EUidPixelFormatARGB_4444:
sl@0
   620
			{
sl@0
   621
			color = aColor.Color4K();
sl@0
   622
			use16 = ETrue;
sl@0
   623
			break;
sl@0
   624
			}
sl@0
   625
		case EUidPixelFormatRGB_565:
sl@0
   626
			{
sl@0
   627
			color = aColor.Color64K();
sl@0
   628
			use16 = ETrue;
sl@0
   629
			break;
sl@0
   630
			}
sl@0
   631
		default:
sl@0
   632
			{
sl@0
   633
			User::Leave(KErrNotSupported);
sl@0
   634
			break;
sl@0
   635
			}
sl@0
   636
		}
sl@0
   637
sl@0
   638
	TUint8* surfacePtr = aBufferPtr;
sl@0
   639
	TUint8* linePtr = surfacePtr;
sl@0
   640
sl@0
   641
	if (use16)
sl@0
   642
		{
sl@0
   643
		if ( info.iSize.iWidth*2>info.iStride)
sl@0
   644
			{
sl@0
   645
			User::Leave(KErrOverflow);
sl@0
   646
			}
sl@0
   647
		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
sl@0
   648
sl@0
   649
		// Fill first line
sl@0
   650
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   651
			{
sl@0
   652
			ptr[xx] = (TUint16)color;
sl@0
   653
			}
sl@0
   654
		}
sl@0
   655
	else
sl@0
   656
		{
sl@0
   657
		if ( info.iSize.iWidth*4>info.iStride)
sl@0
   658
			{
sl@0
   659
			User::Leave(KErrOverflow);
sl@0
   660
			}
sl@0
   661
		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
sl@0
   662
sl@0
   663
		// Fill first line
sl@0
   664
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   665
			{
sl@0
   666
			ptr[xx] = color;
sl@0
   667
			}
sl@0
   668
		}
sl@0
   669
sl@0
   670
	// Now copy that to the other lines
sl@0
   671
	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
sl@0
   672
		{
sl@0
   673
		linePtr += info.iStride;
sl@0
   674
		Mem::Copy(linePtr, surfacePtr, info.iSize.iWidth * BytesPerPixelL(info.iPixelFormat));
sl@0
   675
		}	
sl@0
   676
	}
sl@0
   677
sl@0
   678
TBool CSurfaceUtility::CompareSurfacesL(TSurfaceId& aSurface, TInt aBuffer, TSurfaceId& aStreamSurface, TUint8* aBufferPtr)
sl@0
   679
	{
sl@0
   680
	RSurfaceManager::TInfoBuf infoBuf1;
sl@0
   681
	RSurfaceManager::TSurfaceInfoV01& info1 = infoBuf1();
sl@0
   682
	
sl@0
   683
	RSurfaceManager::TInfoBuf infoBuf2;
sl@0
   684
	RSurfaceManager::TSurfaceInfoV01& info2 = infoBuf2();	
sl@0
   685
sl@0
   686
	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf1));
sl@0
   687
	User::LeaveIfError(iManager.SurfaceInfo(aStreamSurface, infoBuf2));
sl@0
   688
	TBool use16 = EFalse;
sl@0
   689
	
sl@0
   690
	if (aBuffer < 0 || aBuffer >= info1.iBuffers)
sl@0
   691
		{
sl@0
   692
		User::Leave(KErrArgument);
sl@0
   693
		}
sl@0
   694
sl@0
   695
	if (info1.iPixelFormat != info2.iPixelFormat)
sl@0
   696
		{
sl@0
   697
		User::Leave(KErrArgument);
sl@0
   698
		}	
sl@0
   699
	
sl@0
   700
	if ((info1.iSize.iHeight<0 || info1.iSize.iWidth<0 || info1.iStride<0) ||
sl@0
   701
		(info2.iSize.iHeight<0 || info2.iSize.iWidth<0 || info2.iStride<0))
sl@0
   702
		{
sl@0
   703
		User::Leave(KErrCorrupt);
sl@0
   704
		}
sl@0
   705
	if ((info1.iSize.iHeight==0 || info1.iSize.iWidth==0 || info1.iStride==0) ||
sl@0
   706
		(info2.iSize.iHeight==0 || info2.iSize.iWidth==0 || info2.iStride==0))
sl@0
   707
		{
sl@0
   708
		User::Leave(KErrNotReady);
sl@0
   709
		}
sl@0
   710
	if (info1.iSize != info2.iSize)
sl@0
   711
		{
sl@0
   712
		User::Leave(KErrArgument);
sl@0
   713
		}
sl@0
   714
sl@0
   715
	switch (info1.iPixelFormat)
sl@0
   716
		{
sl@0
   717
		case EUidPixelFormatXRGB_8888:
sl@0
   718
		case EUidPixelFormatARGB_8888:
sl@0
   719
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   720
			{
sl@0
   721
			break;
sl@0
   722
			}
sl@0
   723
		case EUidPixelFormatXRGB_4444:
sl@0
   724
		case EUidPixelFormatARGB_4444:
sl@0
   725
		case EUidPixelFormatRGB_565:
sl@0
   726
			{
sl@0
   727
			use16 = ETrue;
sl@0
   728
			break;
sl@0
   729
			}
sl@0
   730
		default:
sl@0
   731
			{
sl@0
   732
			User::Leave(KErrNotSupported);
sl@0
   733
			break;
sl@0
   734
			}
sl@0
   735
		}
sl@0
   736
sl@0
   737
	// Surface
sl@0
   738
	RChunk chunk;
sl@0
   739
	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
sl@0
   740
	CleanupClosePushL(chunk);
sl@0
   741
	
sl@0
   742
	TInt offsetToBuffer;
sl@0
   743
	User::LeaveIfError(iManager.GetBufferOffset(aSurface, aBuffer, offsetToBuffer));
sl@0
   744
	TUint8* surfacePtr1 = chunk.Base() + offsetToBuffer;
sl@0
   745
sl@0
   746
	// Native stream
sl@0
   747
	TUint8* surfacePtr2 = aBufferPtr;
sl@0
   748
	
sl@0
   749
	TUint32 color1 = 0;
sl@0
   750
	TUint32 color2 = 0;
sl@0
   751
	
sl@0
   752
	if (use16)
sl@0
   753
		{
sl@0
   754
		if ((info1.iSize.iWidth*2>info1.iStride) ||
sl@0
   755
			(info2.iSize.iWidth*2>info2.iStride))
sl@0
   756
			{
sl@0
   757
			User::Leave(KErrOverflow);
sl@0
   758
			}
sl@0
   759
		
sl@0
   760
		TUint16* ptr1 = reinterpret_cast<TUint16*>(surfacePtr1);
sl@0
   761
		TUint16* ptr2 = reinterpret_cast<TUint16*>(surfacePtr2);
sl@0
   762
sl@0
   763
		// Fill first line
sl@0
   764
		for (TInt xx = 0; xx < info1.iSize.iWidth; xx++)
sl@0
   765
			{
sl@0
   766
			for (TInt yy = 0; yy < info1.iSize.iHeight; yy++)
sl@0
   767
				{
sl@0
   768
				color1 = (TUint16)ptr1[xx];
sl@0
   769
				color2 = (TUint16)ptr2[xx];
sl@0
   770
				if (color1 != color2)
sl@0
   771
					{
sl@0
   772
					return EFalse;
sl@0
   773
					}
sl@0
   774
				}
sl@0
   775
			}
sl@0
   776
		}
sl@0
   777
	else
sl@0
   778
		{
sl@0
   779
		if ((info1.iSize.iWidth*4>info1.iStride) ||
sl@0
   780
			(info2.iSize.iWidth*4>info2.iStride))
sl@0
   781
			{
sl@0
   782
			User::Leave(KErrOverflow);
sl@0
   783
			}
sl@0
   784
sl@0
   785
		TUint32* ptr1 = reinterpret_cast<TUint32*>(surfacePtr1);
sl@0
   786
		TUint32* ptr2 = reinterpret_cast<TUint32*>(surfacePtr2);		
sl@0
   787
sl@0
   788
		// Fill first line
sl@0
   789
		for (TInt xx = 0; xx < info1.iSize.iWidth; xx++)
sl@0
   790
			{
sl@0
   791
			for (TInt yy = 0; yy < info1.iSize.iHeight; yy++)
sl@0
   792
				{
sl@0
   793
				color1 = ptr1[xx];
sl@0
   794
				color2 = ptr2[xx];
sl@0
   795
				if (color1 != color2)
sl@0
   796
					{
sl@0
   797
					CleanupStack::PopAndDestroy(/* chunk */);
sl@0
   798
					return EFalse;
sl@0
   799
					}
sl@0
   800
				}
sl@0
   801
			}
sl@0
   802
		}	
sl@0
   803
		
sl@0
   804
	CleanupStack::PopAndDestroy(/* chunk */);
sl@0
   805
	return ETrue;
sl@0
   806
	}
sl@0
   807
sl@0
   808
void CSurfaceUtility::NotifyWhenDisplayed(TRequestStatus& aStatusDisplayed, TTimeStamp& aTimeStamp)
sl@0
   809
    {
sl@0
   810
    iSurfaceUpdateSession.NotifyWhenDisplayed(aStatusDisplayed, aTimeStamp);
sl@0
   811
    }
sl@0
   812
sl@0
   813
void CSurfaceUtility::NotifyWhenDisplayedXTimes(TInt aCount, TRequestStatus& aStatusDisplayedX)
sl@0
   814
    {
sl@0
   815
    iSurfaceUpdateSession.NotifyWhenDisplayedXTimes(aCount, aStatusDisplayedX);
sl@0
   816
    }
sl@0
   817
sl@0
   818
void CSurfaceUtility::NotifyWhenAvailable(TRequestStatus& aStatusAvailable)
sl@0
   819
    {
sl@0
   820
    iSurfaceUpdateSession.NotifyWhenAvailable(aStatusAvailable);
sl@0
   821
    }