os/graphics/graphicscomposition/openwftest/src/surfaceutility.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2010 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 <stdlib.h>
sl@0
    23
#include <s32file.h> 
sl@0
    24
#include "surfaceutility.h"
sl@0
    25
sl@0
    26
#ifdef __cplusplus
sl@0
    27
extern "C" {
sl@0
    28
#endif
sl@0
    29
sl@0
    30
WFC_API_CALL WFCNativeStreamType WFC_APIENTRY
sl@0
    31
extwfcGetOnScreenStream(WFCDevice dev, WFCContext context) WFC_APIEXIT;
sl@0
    32
sl@0
    33
#ifdef __cplusplus
sl@0
    34
}
sl@0
    35
#endif
sl@0
    36
sl@0
    37
CSurfaceUtility::CSurfaceUtility()
sl@0
    38
: iSurfaces(NULL)
sl@0
    39
	{
sl@0
    40
	}
sl@0
    41
	
sl@0
    42
CSurfaceUtility* CSurfaceUtility::NewL()
sl@0
    43
	{
sl@0
    44
	CSurfaceUtility* utility = new (ELeave)CSurfaceUtility();
sl@0
    45
	CleanupStack::PushL(utility);
sl@0
    46
	utility->ConstructL();
sl@0
    47
	CleanupStack::Pop(utility);
sl@0
    48
	return utility;
sl@0
    49
	}
sl@0
    50
	
sl@0
    51
void CSurfaceUtility::ConstructL()
sl@0
    52
	{
sl@0
    53
	TInt r = iManager.Open();
sl@0
    54
	if (r != KErrNone)
sl@0
    55
		{
sl@0
    56
		LOG(("Surface manager failed to open: %d", r));
sl@0
    57
		User::Leave(r);
sl@0
    58
		}
sl@0
    59
	
sl@0
    60
	r = iSurfaceUpdateSession.Connect();
sl@0
    61
	if (r != KErrNone)
sl@0
    62
		{
sl@0
    63
		LOG(("Failed to connect to update server: %d", r));
sl@0
    64
		User::Leave(r);
sl@0
    65
		}	
sl@0
    66
	}
sl@0
    67
sl@0
    68
CSurfaceUtility::~CSurfaceUtility()
sl@0
    69
	{
sl@0
    70
	DestroyAll();
sl@0
    71
sl@0
    72
	iSurfaces.Close();
sl@0
    73
sl@0
    74
	iManager.Close();
sl@0
    75
sl@0
    76
	iSurfaceUpdateSession.Close();	
sl@0
    77
	}
sl@0
    78
sl@0
    79
TBool CSurfaceUtility::DestroyAll()
sl@0
    80
	{
sl@0
    81
	TInt err = 	KErrNone;
sl@0
    82
	TInt jj = iSurfaces.Count() - 1;
sl@0
    83
	if (jj<0)
sl@0
    84
		return EFalse;
sl@0
    85
	for (; jj >= 0; jj--)
sl@0
    86
		{
sl@0
    87
		err = iManager.CloseSurface(iSurfaces[jj]);
sl@0
    88
		if (err!=KErrNone)
sl@0
    89
			{
sl@0
    90
			LOG(("Error closing surface: 0x%X\n", err));
sl@0
    91
			}
sl@0
    92
		TInt offset;
sl@0
    93
		err = iManager.GetBufferOffset(iSurfaces[jj],0,offset);
sl@0
    94
		if (err==KErrNone)
sl@0
    95
		    {
sl@0
    96
            LOG(("Error: closed surface still accessible: index %i surface %08X %08X %08X %08X\n", jj, iSurfaces[jj]));
sl@0
    97
		    }
sl@0
    98
		}
sl@0
    99
	iSurfaces.Reset();
sl@0
   100
	return ETrue;
sl@0
   101
	}
sl@0
   102
sl@0
   103
/***************************************
sl@0
   104
 * The aim of the THeapSurfaceArray is to locally switch in the specified heap for any array operation
sl@0
   105
 ***************************************/
sl@0
   106
sl@0
   107
CSurfaceUtility::RHeapSurfaceArray::RHeapSurfaceArray(RHeapSurfaceArray* aUseExternalArray)
sl@0
   108
	:	iUseArray(aUseExternalArray?aUseExternalArray->iUseArray:&this->iLocalArray),
sl@0
   109
	iExternalHeapRef(aUseExternalArray?aUseExternalArray->iExternalHeapRef:User::Heap())
sl@0
   110
	{
sl@0
   111
	
sl@0
   112
	}
sl@0
   113
/************************************
sl@0
   114
 * The following methods have been used by the surfaceutility... some require the heap wrapping, and some don't
sl@0
   115
 * Three strategies are needed for 7 methods...
sl@0
   116
 * Some methods only read the existing objects, so don't need a heap swap at all
sl@0
   117
 * Leaving methods have to use PopAndDestroy strategy to restore the heap on leaving or success
sl@0
   118
 * Non-leaving methods must not call PushL, so directly make SwitchHeap calls!
sl@0
   119
 ************************************/
sl@0
   120
sl@0
   121
// PopAndDestroy method to restore the heap
sl@0
   122
/*static*/ void	CSurfaceUtility::RHeapSurfaceArray::PopHeap(void* aHeapPtr)
sl@0
   123
	{
sl@0
   124
	RHeap* heapPtr=(RHeap*)aHeapPtr;
sl@0
   125
	User::SwitchHeap(heapPtr);
sl@0
   126
	}
sl@0
   127
sl@0
   128
TSurfaceId& CSurfaceUtility::RHeapSurfaceArray::operator[](TUint aIndex)
sl@0
   129
	{
sl@0
   130
	return iUseArray->operator[](aIndex);
sl@0
   131
	}
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
	iLocalArray.Close();
sl@0
   137
	}
sl@0
   138
sl@0
   139
TInt CSurfaceUtility::RHeapSurfaceArray::Count() const
sl@0
   140
	{
sl@0
   141
	return iUseArray->Count();
sl@0
   142
	}
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
	iUseArray->Reset();
sl@0
   148
	}
sl@0
   149
sl@0
   150
void CSurfaceUtility::RHeapSurfaceArray::AppendL(const TSurfaceId &anEntry)
sl@0
   151
	{
sl@0
   152
	iUseArray->AppendL(anEntry);
sl@0
   153
	}
sl@0
   154
sl@0
   155
TInt CSurfaceUtility::RHeapSurfaceArray::Find(const TSurfaceId &anEntry) const
sl@0
   156
	{
sl@0
   157
	return iUseArray->Find(anEntry);
sl@0
   158
	}
sl@0
   159
sl@0
   160
void CSurfaceUtility::RHeapSurfaceArray::Remove(TInt anIndex)
sl@0
   161
	{
sl@0
   162
	iUseArray->Remove(anIndex);
sl@0
   163
	}
sl@0
   164
sl@0
   165
/**
sl@0
   166
Cleanup stack helper object, holding references to both utility and surface, so
sl@0
   167
that the standard Close() semantics can be used.
sl@0
   168
*/
sl@0
   169
class TSurfaceCleanup
sl@0
   170
	{
sl@0
   171
public:
sl@0
   172
	TSurfaceCleanup(CSurfaceUtility& aUtility, TSurfaceId& aSurface)
sl@0
   173
		: iUtility(aUtility), iSurface(aSurface)
sl@0
   174
		{}
sl@0
   175
	void Close()
sl@0
   176
		{
sl@0
   177
		// Removes the surface from the list of surfaces to clean up, and closes
sl@0
   178
		// the surface reference.
sl@0
   179
		iUtility.DestroySurface(iSurface);
sl@0
   180
		}
sl@0
   181
private:
sl@0
   182
	CSurfaceUtility& iUtility;
sl@0
   183
	TSurfaceId& iSurface;
sl@0
   184
	};
sl@0
   185
sl@0
   186
/**
sl@0
   187
Get the size of a surface.
sl@0
   188
sl@0
   189
@param aSurface The surface to get the size for.
sl@0
   190
@return The size in pixels, or empty on failure.
sl@0
   191
*/
sl@0
   192
TSize CSurfaceUtility::SurfaceSize(const TSurfaceId& aSurface)
sl@0
   193
	{
sl@0
   194
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   195
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   196
sl@0
   197
	if (iManager.SurfaceInfo(aSurface, infoBuf) == KErrNone)
sl@0
   198
		{
sl@0
   199
		return info.iSize;
sl@0
   200
		}
sl@0
   201
sl@0
   202
	return TSize();
sl@0
   203
	}
sl@0
   204
sl@0
   205
/**
sl@0
   206
Create a surface using the surface manager.
sl@0
   207
sl@0
   208
Stores the ID for tear down, as well as returning it.
sl@0
   209
sl@0
   210
@param aSize Dimensions of the surface.
sl@0
   211
@param aPixelFormat	UID of the pixel format.
sl@0
   212
@param aStride	Stride value for the surface (usually bytes per pixel * width)
sl@0
   213
@param aContiguous  Contiguous flag for creating surfaces
sl@0
   214
@param aBuffers Number of buffers in the surface
sl@0
   215
@leave May leave due to lack of memory.
sl@0
   216
@return New surface's ID.
sl@0
   217
*/
sl@0
   218
TSurfaceId CSurfaceUtility::CreateSurfaceL(const TSize& aSize, TUidPixelFormat aPixelFormat, TInt aStride, TBool aContiguous, TInt aBuffers)
sl@0
   219
	{
sl@0
   220
	RSurfaceManager::TSurfaceCreationAttributesBuf bf;
sl@0
   221
	RSurfaceManager::TSurfaceCreationAttributes& b = bf();
sl@0
   222
	if (aStride<aSize.iWidth*BytesPerPixelL(aPixelFormat))
sl@0
   223
	    {
sl@0
   224
	    User::Leave(KErrOverflow);
sl@0
   225
	    }
sl@0
   226
	b.iSize.iWidth = aSize.iWidth;
sl@0
   227
	b.iSize.iHeight = aSize.iHeight;
sl@0
   228
	b.iBuffers = aBuffers;				// number of buffers in the surface
sl@0
   229
	b.iPixelFormat = aPixelFormat;
sl@0
   230
	b.iStride = aStride;		// Number of bytes between start of one line and start of next
sl@0
   231
	b.iOffsetToFirstBuffer = 0;	// way of reserving space before the surface pixel data
sl@0
   232
	b.iAlignment = 4;			// alignment, 1,2,4,8 byte aligned
sl@0
   233
	b.iContiguous = aContiguous;
sl@0
   234
	b.iMappable = ETrue;
sl@0
   235
sl@0
   236
	TSurfaceId surface = TSurfaceId::CreateNullId();
sl@0
   237
sl@0
   238
	User::LeaveIfError(iManager.CreateSurface(bf, surface));
sl@0
   239
	iSurfaces.AppendL(surface);
sl@0
   240
	return surface;
sl@0
   241
	}
sl@0
   242
sl@0
   243
/**
sl@0
   244
A helper function that returns the bytes per pixel for a given pixel format uid
sl@0
   245
sl@0
   246
@param aPixelFormat Pixel format UID to convert
sl@0
   247
@return The bytes per pixel
sl@0
   248
*/
sl@0
   249
TInt CSurfaceUtility::BytesPerPixelL(TUidPixelFormat aPixelFormat)
sl@0
   250
	{
sl@0
   251
	TInt bytesPerPixel = 0;
sl@0
   252
	switch (aPixelFormat)
sl@0
   253
		{
sl@0
   254
		case EUidPixelFormatXRGB_8888:
sl@0
   255
		case EUidPixelFormatARGB_8888:
sl@0
   256
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   257
			{
sl@0
   258
			bytesPerPixel = 4;
sl@0
   259
			break;
sl@0
   260
			}
sl@0
   261
		case EUidPixelFormatXRGB_4444:
sl@0
   262
		case EUidPixelFormatARGB_4444:
sl@0
   263
		case EUidPixelFormatRGB_565:
sl@0
   264
			{
sl@0
   265
			bytesPerPixel = 2;
sl@0
   266
			break;
sl@0
   267
			}
sl@0
   268
		default:
sl@0
   269
			{
sl@0
   270
			User::Leave(KErrNotSupported);
sl@0
   271
			break;
sl@0
   272
			}
sl@0
   273
		}
sl@0
   274
	return bytesPerPixel;
sl@0
   275
	}
sl@0
   276
sl@0
   277
/**
sl@0
   278
Fill buffer 0 of the given surface with a color.
sl@0
   279
sl@0
   280
@param aSurface	The surface to be filled.
sl@0
   281
@param aColor	The color to fill it with.
sl@0
   282
*/
sl@0
   283
void CSurfaceUtility::FillSurfaceL(TSurfaceId& aSurface, const TRgb& aColor)
sl@0
   284
	{
sl@0
   285
    FillSurfaceL(aSurface, 0, aColor);
sl@0
   286
	}
sl@0
   287
sl@0
   288
/**
sl@0
   289
Fill a specified buffer number of the given surface with a color.
sl@0
   290
sl@0
   291
@param aSurface	The surface to be filled.
sl@0
   292
@param aBuffer 	The buffer to fill.
sl@0
   293
@param aColor	The color to fill it with.
sl@0
   294
*/
sl@0
   295
void CSurfaceUtility::FillSurfaceL(TSurfaceId& aSurface, TInt aBuffer, const TRgb& aColor)
sl@0
   296
	{
sl@0
   297
	RSurfaceManager::TInfoBuf infoBuf;
sl@0
   298
	RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   299
sl@0
   300
	User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
sl@0
   301
	TUint32 color = 0;
sl@0
   302
	TBool use16 = EFalse;
sl@0
   303
	TInt numBuffers = info.iBuffers;
sl@0
   304
	if (aBuffer < 0 || aBuffer >= numBuffers)
sl@0
   305
		{
sl@0
   306
		User::Leave(KErrArgument);
sl@0
   307
		}
sl@0
   308
sl@0
   309
	if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
sl@0
   310
		{
sl@0
   311
		User::Leave(KErrCorrupt);
sl@0
   312
		}
sl@0
   313
	if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
sl@0
   314
		{
sl@0
   315
		User::Leave(KErrNotReady);
sl@0
   316
		}
sl@0
   317
sl@0
   318
	switch (info.iPixelFormat)
sl@0
   319
		{
sl@0
   320
		case EUidPixelFormatXRGB_8888:
sl@0
   321
			{
sl@0
   322
			color = aColor.Color16MU();
sl@0
   323
			break;
sl@0
   324
			}
sl@0
   325
		case EUidPixelFormatARGB_8888:
sl@0
   326
			{
sl@0
   327
			color = aColor.Color16MA();
sl@0
   328
			break;
sl@0
   329
			}
sl@0
   330
		case EUidPixelFormatARGB_8888_PRE:
sl@0
   331
			{
sl@0
   332
			color = aColor.Color16MAP();
sl@0
   333
			break;
sl@0
   334
			}
sl@0
   335
		case EUidPixelFormatXRGB_4444:
sl@0
   336
		case EUidPixelFormatARGB_4444:
sl@0
   337
			{
sl@0
   338
			color = aColor.Color4K();
sl@0
   339
			use16 = ETrue;
sl@0
   340
			break;
sl@0
   341
			}
sl@0
   342
		case EUidPixelFormatRGB_565:
sl@0
   343
			{
sl@0
   344
			color = aColor.Color64K();
sl@0
   345
			use16 = ETrue;
sl@0
   346
			break;
sl@0
   347
			}
sl@0
   348
		default:
sl@0
   349
			{
sl@0
   350
			User::Leave(KErrNotSupported);
sl@0
   351
			break;
sl@0
   352
			}
sl@0
   353
		}
sl@0
   354
sl@0
   355
	RChunk chunk;
sl@0
   356
	User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
sl@0
   357
	CleanupClosePushL(chunk);
sl@0
   358
sl@0
   359
	TInt offsetToBuffer;
sl@0
   360
	User::LeaveIfError(iManager.GetBufferOffset(aSurface, aBuffer, offsetToBuffer));
sl@0
   361
	TUint8* surfacePtr = chunk.Base() + offsetToBuffer;
sl@0
   362
	TUint8* linePtr = surfacePtr;
sl@0
   363
sl@0
   364
	if (use16)
sl@0
   365
		{
sl@0
   366
		if ( info.iSize.iWidth*2>info.iStride)
sl@0
   367
			{
sl@0
   368
			User::Leave(KErrOverflow);
sl@0
   369
			}
sl@0
   370
		TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr);
sl@0
   371
sl@0
   372
		// Fill first line
sl@0
   373
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   374
			{
sl@0
   375
			ptr[xx] = (TUint16)color;
sl@0
   376
			}
sl@0
   377
		}
sl@0
   378
	else
sl@0
   379
		{
sl@0
   380
		if ( info.iSize.iWidth*4>info.iStride)
sl@0
   381
			{
sl@0
   382
			User::Leave(KErrOverflow);
sl@0
   383
			}
sl@0
   384
		TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr);
sl@0
   385
sl@0
   386
		// Fill first line
sl@0
   387
		for (TInt xx = 0; xx < info.iSize.iWidth; xx++)
sl@0
   388
			{
sl@0
   389
			ptr[xx] = color;
sl@0
   390
			}
sl@0
   391
		}
sl@0
   392
sl@0
   393
	// Now copy that to the other lines
sl@0
   394
	for (TInt yy = 1; yy < info.iSize.iHeight; yy++)
sl@0
   395
		{
sl@0
   396
		linePtr += info.iStride;
sl@0
   397
		Mem::Copy(linePtr, surfacePtr, info.iSize.iWidth * BytesPerPixelL(info.iPixelFormat));
sl@0
   398
		}
sl@0
   399
	
sl@0
   400
	CleanupStack::PopAndDestroy(/* chunk */);
sl@0
   401
	}
sl@0
   402
sl@0
   403
/**
sl@0
   404
Destroy a surface.
sl@0
   405
sl@0
   406
As well as destroying the surface, it is removed from the set held for
sl@0
   407
destruction during tear down.
sl@0
   408
sl@0
   409
@param aSurface	The surface to be destroyed.
sl@0
   410
*/
sl@0
   411
void CSurfaceUtility::DestroySurface(TSurfaceId& aSurface)
sl@0
   412
	{
sl@0
   413
	TInt index = iSurfaces.Find(aSurface);
sl@0
   414
	
sl@0
   415
	if (index != KErrNotFound)
sl@0
   416
		{
sl@0
   417
		iSurfaces.Remove(index);
sl@0
   418
		}
sl@0
   419
sl@0
   420
	TInt err = iManager.CloseSurface(aSurface);
sl@0
   421
	if (err!=KErrNone)
sl@0
   422
		LOG(("Error closing surfaces: 0x%X\n", err));
sl@0
   423
	}
sl@0
   424
sl@0
   425
/**
sl@0
   426
Get surface header information
sl@0
   427
sl@0
   428
@param aSurface A surface to get the header info from.
sl@0
   429
@param aInfo Returned package info of the surface header.
sl@0
   430
sl@0
   431
@return KErrNone if successful, KErrArgument if the surface ID does not refer to a surface,
sl@0
   432
KErrAccessDenied if the surface is not open in the current process, otherwise a system wide
sl@0
   433
error code.
sl@0
   434
 */
sl@0
   435
TInt CSurfaceUtility::GetHeader(const TSurfaceId& aSurface, RSurfaceManager::TInfoBuf& aInfo) 
sl@0
   436
    {
sl@0
   437
    return iManager.SurfaceInfo(aSurface, aInfo);
sl@0
   438
    }
sl@0
   439
sl@0
   440
/**
sl@0
   441
Get buffer pointer to a surface
sl@0
   442
sl@0
   443
@param aSurface Surface of the buffer pointer.
sl@0
   444
@param aNumOfBuffer A number of buffer.
sl@0
   445
@param aChunk A chunk of memory.
sl@0
   446
sl@0
   447
@return A buffer pointer of the surface.
sl@0
   448
 */
sl@0
   449
TUint8* CSurfaceUtility::GetBufferPointerL(const TSurfaceId& aSurface, TInt aNumOfBuffer, RChunk& aChunk) 
sl@0
   450
    {
sl@0
   451
    TInt offsetToBuffer;
sl@0
   452
    User::LeaveIfError(iManager.MapSurface(aSurface, aChunk));
sl@0
   453
    User::LeaveIfError(iManager.GetBufferOffset(aSurface, aNumOfBuffer, offsetToBuffer));
sl@0
   454
    TUint8* surfacePtr = aChunk.Base() + offsetToBuffer;
sl@0
   455
    return surfacePtr;
sl@0
   456
    }
sl@0
   457
sl@0
   458
/**
sl@0
   459
Get pixel color at a position.
sl@0
   460
sl@0
   461
@param aInfo Package info of a surface.
sl@0
   462
@param aPixelData Surface buffer pointer.
sl@0
   463
@param aPosition Position of the pixel.
sl@0
   464
sl@0
   465
@return Color of the pixel position.
sl@0
   466
*/
sl@0
   467
TRgb CSurfaceUtility::GetPixelL(RSurfaceManager::TInfoBuf& aInfo, TAny* aPixelData, TPoint aPosition) 
sl@0
   468
    {
sl@0
   469
    RSurfaceManager::TSurfaceInfoV01& info = aInfo();
sl@0
   470
    TInt stride = info.iStride;
sl@0
   471
    TUidPixelFormat pixelFormat = info.iPixelFormat;
sl@0
   472
    TInt bytesPerPixel = BytesPerPixelL(pixelFormat);
sl@0
   473
    TInt pixelStride = stride / bytesPerPixel;
sl@0
   474
    TUint pixel = aPosition.iY * pixelStride + aPosition.iX;
sl@0
   475
    TUint* pixels = reinterpret_cast< TUint* >( aPixelData );
sl@0
   476
    TRgb colour;
sl@0
   477
    colour.SetInternal(pixels[ pixel ]);
sl@0
   478
    return colour;
sl@0
   479
    }
sl@0
   480
sl@0
   481
/**
sl@0
   482
Check pixel color within a rectangle is as expected.
sl@0
   483
It checks every color channel of every pixel within the rectangle.
sl@0
   484
sl@0
   485
@param aSurface The surface to be checked.
sl@0
   486
@param aRect The rectangle for pixel checking.
sl@0
   487
@param aNumOfBuffer Number of buffer.
sl@0
   488
@param aExpectedColor The expected color.
sl@0
   489
@param aTolerance A tolerance value.
sl@0
   490
sl@0
   491
@return EFalse if a color channel of a pixel is outside the tolerance range. 
sl@0
   492
        ETrue if all pixel colors are within the tolerance range.
sl@0
   493
*/
sl@0
   494
TBool CSurfaceUtility::CheckRectColor(const TSurfaceId& aSurface, TRect& aRect, TInt aNumOfBuffer, const TRgb& aExpectedColor, TInt aTolerance) 
sl@0
   495
    {
sl@0
   496
    RSurfaceManager::TInfoBuf infoBuf;
sl@0
   497
    RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   498
    TInt error = GetHeader(aSurface, infoBuf);
sl@0
   499
    if (error != KErrNone)
sl@0
   500
    	{
sl@0
   501
		RDebug::Printf("Line %d GetHeader failed",__LINE__);
sl@0
   502
		return EFalse;
sl@0
   503
    	}
sl@0
   504
sl@0
   505
    RChunk chunk;
sl@0
   506
    TUint8* surfacePtr = NULL;
sl@0
   507
    TRAP_IGNORE(surfacePtr = GetBufferPointerL(aSurface, aNumOfBuffer, chunk));
sl@0
   508
    if (!surfacePtr)
sl@0
   509
    	{
sl@0
   510
		RDebug::Printf("Line %d GetBufferPointerL failed",__LINE__);
sl@0
   511
		return EFalse;
sl@0
   512
    	}
sl@0
   513
    
sl@0
   514
    TRgb color;
sl@0
   515
    // Check every colour channel of every pixel is within the tolerance
sl@0
   516
    for (TInt ii = aRect.iTl.iX; ii < aRect.iBr.iX; ++ii)
sl@0
   517
        {
sl@0
   518
        for (TInt jj = aRect.iTl.iY; jj < aRect.iBr.iY; ++jj)
sl@0
   519
            {
sl@0
   520
            color = GetPixelL(infoBuf, surfacePtr, TPoint(ii, jj));      
sl@0
   521
            TBool checkR = (color.Red() <= (aExpectedColor.Red() + aTolerance) && color.Red() >= (aExpectedColor.Red() - aTolerance));
sl@0
   522
            TBool checkG = (color.Green() <= (aExpectedColor.Green() + aTolerance) && color.Green() >= (aExpectedColor.Green() - aTolerance));
sl@0
   523
            TBool checkB = (color.Blue() <= (aExpectedColor.Blue() + aTolerance) && color.Blue() >= (aExpectedColor.Blue() - aTolerance));
sl@0
   524
            if (!checkR || !checkG || !checkB)
sl@0
   525
                {
sl@0
   526
                RDebug::Printf("At x=%d y=%d CheckRectColor has failed:",ii,jj);
sl@0
   527
                RDebug::Printf("Expected Red %d - Actual Red %d",aExpectedColor.Red(),color.Red());
sl@0
   528
                RDebug::Printf("Expected Green %d - Actual Green %d",aExpectedColor.Green(),color.Green());
sl@0
   529
                RDebug::Printf("Expected Blue %d - Actual Blue %d",aExpectedColor.Blue(),color.Blue());
sl@0
   530
                return EFalse;            
sl@0
   531
                }               
sl@0
   532
            }
sl@0
   533
        }
sl@0
   534
    return ETrue;
sl@0
   535
    }
sl@0
   536
sl@0
   537
/**
sl@0
   538
Save on screen image to a .tga file
sl@0
   539
sl@0
   540
@param aSurface A surface to be saved
sl@0
   541
@param aBufferNumber The surface's buffer number
sl@0
   542
@param aDestination The path and name of the tga to save eg c:\\test\\img\\image1.tga
sl@0
   543
@return ETrue on successful calls
sl@0
   544
        Fails if GetBufferPointerL returns NULL pointer
sl@0
   545
 */
sl@0
   546
TBool CSurfaceUtility::SaveResultImageTGAL(const TSurfaceId& aSurface, TInt aBufferNumber, TDesC& aDestination)
sl@0
   547
    {
sl@0
   548
    RSurfaceManager::TInfoBuf infoBuf;
sl@0
   549
    RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
sl@0
   550
    User::LeaveIfError(GetHeader(aSurface, infoBuf));
sl@0
   551
    TInt stride = info.iStride;
sl@0
   552
    TSize surfaceSize = info.iSize;
sl@0
   553
    TUidPixelFormat pixelFormat = info.iPixelFormat;
sl@0
   554
    TInt bytesPerPixel = BytesPerPixelL(pixelFormat);
sl@0
   555
    TInt widthInBytes = surfaceSize.iWidth * bytesPerPixel;
sl@0
   556
    
sl@0
   557
    RFs fs;
sl@0
   558
    User::LeaveIfError(fs.Connect());
sl@0
   559
    CleanupClosePushL(fs); 
sl@0
   560
        
sl@0
   561
    // Create image file
sl@0
   562
    RFileWriteStream fstream;
sl@0
   563
    User::LeaveIfError(fstream.Replace(fs, aDestination, EFileShareAny|EFileWrite));
sl@0
   564
    CleanupClosePushL(fstream); 
sl@0
   565
    
sl@0
   566
    // Write header
sl@0
   567
    fstream.WriteUint8L(0);                 // ID Length
sl@0
   568
    fstream.WriteUint8L(0);                 // Color map type
sl@0
   569
    fstream.WriteUint8L(2);                 // Image type - Uncompressed, True-color Image
sl@0
   570
    fstream.WriteUint32L(0);                // Color map specification 5 bytes
sl@0
   571
    fstream.WriteUint8L(0);                 // Color map specification
sl@0
   572
    fstream.WriteUint32L(0);                // Image specification - origin of image
sl@0
   573
    fstream.WriteUint16L(static_cast<TUint16>(surfaceSize.iWidth));      // Image specification - Image width
sl@0
   574
    fstream.WriteUint16L(static_cast<TUint16>(surfaceSize.iHeight));     // Image specification - Image height
sl@0
   575
    fstream.WriteUint8L(32);                // Image specification - Pixel Depth (bits per pixel)
sl@0
   576
    fstream.WriteUint8L(1 << 5);            // Image specification - Image Descriptor, Screen destination of first pixel is top left 
sl@0
   577
sl@0
   578
    RChunk chunk;
sl@0
   579
    TUint8* surfacePtr = GetBufferPointerL(aSurface, aBufferNumber, chunk);
sl@0
   580
    if(surfacePtr == NULL)
sl@0
   581
        {
sl@0
   582
        CleanupStack::PopAndDestroy(2); 
sl@0
   583
        return EFalse;
sl@0
   584
        }
sl@0
   585
    
sl@0
   586
    // Write image line by line
sl@0
   587
    for(TInt ii = 0; ii < surfaceSize.iHeight; ++ii)
sl@0
   588
        {
sl@0
   589
        fstream.WriteL(surfacePtr, widthInBytes); 
sl@0
   590
        surfacePtr += stride;
sl@0
   591
        }
sl@0
   592
sl@0
   593
    fstream.CommitL();
sl@0
   594
    chunk.Close();
sl@0
   595
    CleanupStack::PopAndDestroy(2); 
sl@0
   596
    
sl@0
   597
    return ETrue;
sl@0
   598
    }
sl@0
   599
sl@0
   600
/**
sl@0
   601
Create directory for images to be saved
sl@0
   602
sl@0
   603
@param aDir Directory for images to be saved
sl@0
   604
@return ETrue on success
sl@0
   605
 */
sl@0
   606
TBool CSurfaceUtility::CreateImagePath(TDesC& aDir)
sl@0
   607
    {
sl@0
   608
    RFs fs;
sl@0
   609
    TInt err = fs.Connect();
sl@0
   610
    if (err == KErrNone)
sl@0
   611
        {
sl@0
   612
        err = fs.MkDirAll(aDir);
sl@0
   613
        if (err == KErrAlreadyExists)
sl@0
   614
            {
sl@0
   615
            err = KErrNone;
sl@0
   616
            }
sl@0
   617
        fs.Close();
sl@0
   618
        }
sl@0
   619
    return (err == KErrNone);
sl@0
   620
    }
sl@0
   621
    
sl@0
   622
/**
sl@0
   623
Submit an update to a surface to the update server.
sl@0
   624
sl@0
   625
@param aSurface	The surface which has been updated.
sl@0
   626
@param aRegion	The area of the surface affected, or NULL for all of it.
sl@0
   627
*/
sl@0
   628
TInt CSurfaceUtility::SubmitUpdate(const TSurfaceId& aSurface,TInt aBufferNumber, const TRegion* aRegion)
sl@0
   629
	{
sl@0
   630
	if (!iSurfaceUpdateSession.Handle())
sl@0
   631
	    {
sl@0
   632
	    iSurfaceUpdateSession.Connect();
sl@0
   633
	    }
sl@0
   634
    if (!iSurfaceUpdateSession.Handle())
sl@0
   635
        {
sl@0
   636
        LOG(("Error - SUS client not started!"));
sl@0
   637
        return KErrNotReady;
sl@0
   638
        }
sl@0
   639
    else
sl@0
   640
        {
sl@0
   641
        TInt err =iSurfaceUpdateSession.SubmitUpdate(KAllScreens, aSurface, aBufferNumber, aRegion); 
sl@0
   642
        if (err!=KErrNone)
sl@0
   643
            LOG(("Error submitting update: 0x%X\n", err));
sl@0
   644
        return err;
sl@0
   645
        }
sl@0
   646
	}
sl@0
   647
sl@0
   648
void CSurfaceUtility::NotifyWhenDisplayed(TRequestStatus& aStatusDisplayed, TTimeStamp& aTimeStamp)
sl@0
   649
    {
sl@0
   650
    iSurfaceUpdateSession.NotifyWhenDisplayed(aStatusDisplayed, aTimeStamp);
sl@0
   651
    }
sl@0
   652
sl@0
   653
void CSurfaceUtility::NotifyWhenDisplayedXTimes(TInt aCount, TRequestStatus& aStatusDisplayedX)
sl@0
   654
    {
sl@0
   655
    iSurfaceUpdateSession.NotifyWhenDisplayedXTimes(aCount, aStatusDisplayedX);
sl@0
   656
    }
sl@0
   657
sl@0
   658
void CSurfaceUtility::NotifyWhenAvailable(TRequestStatus& aStatusAvailable)
sl@0
   659
    {
sl@0
   660
    iSurfaceUpdateSession.NotifyWhenAvailable(aStatusAvailable);
sl@0
   661
    }
sl@0
   662
sl@0
   663
SymbianStreamType CSurfaceUtility::GetOnScreenStream(WFCDevice aDev, WFCContext aContext)
sl@0
   664
    {
sl@0
   665
    return reinterpret_cast<SymbianStreamType>(wfcGetOnScreenStream(aDev, aContext));
sl@0
   666
    }
sl@0
   667
sl@0
   668
sl@0
   669