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