Update contrib.
1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "tsurfacehelper.h"
19 CSurfaceHelper* CSurfaceHelper::NewL()
21 CSurfaceHelper* helper = new(ELeave)CSurfaceHelper;
22 CleanupStack::PushL(helper);
24 CleanupStack::Pop(helper);
28 CSurfaceHelper::~CSurfaceHelper()
36 * Load logical device and open a surface manager
38 void CSurfaceHelper::ConstructL()
40 TInt res = iManager.Open();
48 * Create the surface with the given parameters.
49 * The surface will be owned by the instance of this class
51 TSurfaceId CSurfaceHelper::CreateSurfaceL(const TSize& aSize, TUidPixelFormat aPixelFormat, TInt aStride, TInt aBuffers)
53 RSurfaceManager::TSurfaceCreationAttributesBuf bf;
54 RSurfaceManager::TSurfaceCreationAttributes& b = bf();
56 b.iSize.iWidth = aSize.iWidth;
57 b.iSize.iHeight = aSize.iHeight;
58 b.iBuffers = aBuffers; // number of buffers in the surface
59 b.iPixelFormat = aPixelFormat;
60 b.iStride = aStride; // Number of bytes between start of one line and start of next
61 b.iOffsetToFirstBuffer = 0; // way of reserving space before the surface pixel data
62 b.iAlignment = 4; // alignment, 1,2,4,8 byte aligned
63 b.iContiguous = EFalse;
66 TSurfaceId surface = TSurfaceId::CreateNullId();
68 User::LeaveIfError(iManager.CreateSurface(bf, surface));
69 iSurfaces.AppendL(surface);
74 * Destroy all surfaces wich have been created by the instance of this class
76 void CSurfaceHelper::DestroySurfaces()
78 TInt numSurfaces = iSurfaces.Count();
79 for (TInt index = 0; index < numSurfaces; index++)
81 TSurfaceId surfaceId = iSurfaces[index];
82 TInt err = iManager.CloseSurface(surfaceId);
83 __ASSERT_DEBUG(err ==KErrNone, User::Panic(_L("CSurfaceHelper::DestroySurfaces"), err));
89 Fill a rectangle on the given surface.
91 @param aSurface The surface to be filled.
92 @param aStartPos Where to place the rectangle.
93 @param aSize Size of the rectangle.
94 @param aColor The colour to fill it with.
96 void CSurfaceHelper::FillRectangleL(const TSurfaceId& aSurface, const TPoint& aStartPos, const TSize& aSize, const TRgb& aColor)
98 RSurfaceManager::TInfoBuf infoBuf;
99 RSurfaceManager::TSurfaceInfoV01& info = infoBuf();
100 User::LeaveIfError(iManager.SurfaceInfo(aSurface, infoBuf));
103 if (info.iSize.iHeight<0 || info.iSize.iWidth<0 || info.iStride<0)
105 User::Leave(KErrCorrupt);
107 if (info.iSize.iHeight==0 || info.iSize.iWidth==0 || info.iStride==0)
109 User::Leave(KErrNotReady);
112 switch (info.iPixelFormat)
114 case EUidPixelFormatXRGB_8888:
116 color = aColor.Color16MU();
117 #ifdef ALPHA_FIX_24BIT
118 color |= ((ALPHA_FIX_24BIT)&0xff)<<24;
122 case EUidPixelFormatARGB_8888:
124 color = aColor.Color16MA();
127 case EUidPixelFormatARGB_8888_PRE:
129 color = aColor.Color16MAP();
132 case EUidPixelFormatRGB_565:
134 color = aColor.Color64K();
139 User::Leave(KErrNotSupported);
145 User::LeaveIfError(iManager.MapSurface(aSurface, chunk));
146 CleanupClosePushL(chunk);
147 TUint8* surfacePtr = chunk.Base();
149 // Check for out of bounds
150 TBool validRect = ETrue;
151 TInt surfaceWidth = info.iSize.iWidth;
152 TInt surfaceHeight = info.iSize.iHeight;
155 if ((aStartPos.iX + aSize.iWidth) > surfaceWidth)
160 if ((aStartPos.iY + aSize.iHeight) > surfaceHeight)
166 if ((aStartPos.iX < 0) || (aStartPos.iY < 0))
173 User::Leave(KErrOverflow);
176 if (info.iPixelFormat == EUidPixelFormatRGB_565)
178 if ( info.iSize.iWidth*2>info.iStride)
180 User::Leave(KErrOverflow);
184 User::LeaveIfError(iManager.GetBufferOffset(aSurface, 0, offset));
185 TUint16* ptr = reinterpret_cast<TUint16*>(surfacePtr + offset);
187 // Fill the rectangle
188 TInt yPos = aStartPos.iY;
189 TInt xPos = aStartPos.iX;
190 for (TInt yy = 0; yy < aSize.iHeight; ++yy)
192 ptr = reinterpret_cast<TUint16*>(surfacePtr + (yPos*info.iStride));
193 for (TInt xx = 0; xx < aSize.iWidth; ++xx)
204 if ( info.iSize.iWidth*4>info.iStride)
206 User::Leave(KErrOverflow);
210 User::LeaveIfError(iManager.GetBufferOffset(aSurface, 0, offset));
211 TUint32* ptr = reinterpret_cast<TUint32*>(surfacePtr + offset);
213 // Fill the rectangle
214 TInt yPos = aStartPos.iY;
215 TInt xPos = aStartPos.iX;
216 for (TInt yy = 0; yy < aSize.iHeight; ++yy)
218 ptr = reinterpret_cast<TUint32*>(surfacePtr+(yPos*info.iStride));
219 for (TInt xx = 0; xx < aSize.iWidth; ++xx)
228 CleanupStack::PopAndDestroy(&chunk);