First public contribution.
1 // Copyright (c) 1997-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.
18 #include <bitdrawscaling.h>
19 #include <bitdrawinterfaceid.h>
22 const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems
24 CFbsBitmapDevice::CFbsBitmapDevice():
29 /** Frees all resources owned by the object prior to its destruction. */
30 EXPORT_C CFbsBitmapDevice::~CFbsBitmapDevice()
36 /** Constructs the object from the specified Font and Bitmap server
39 @param aFbsBitmap A pointer to a Font and Bitmap server managed bitmap.
40 @param aLibname Name of the library to create the low-level CFbsDrawDevice
42 @return A pointer to the newly constructed graphics device. */
43 EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap,
44 const TDesC& /*aLibname*/)
46 return CFbsBitmapDevice::NewL(aFbsBitmap);
49 /** Allocates and constructs the device with the bitmap. Also creates a 2D graphics
50 accelerator which is owned and used by the device.
52 @param aFbsBitmap A pointer to the font and bitmap server managed bitmap.
53 @leave KErrArgument The bitmap's handle is zero.
54 @leave KErrAccessDenied The bitmap is in the ROM.
55 @return A pointer to the newly constructed device.
56 @panic EBitgdiPanicInvalidBitmap aFbsBitmap is NULL. */
57 EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap)
59 BG_ASSERT_ALWAYS(aFbsBitmap != NULL,EBitgdiPanicInvalidBitmap);
61 CFbsBitmapDevice* self = new(ELeave) CFbsBitmapDevice;
62 CleanupStack::PushL(self);
63 self->ConstructL(aFbsBitmap);
64 CleanupStack::Pop(); // self
68 void CFbsBitmapDevice::ConstructL(CFbsBitmap* aFbsBitmap)
70 if(!aFbsBitmap->Handle())
71 User::Leave(KErrArgument);
72 if(aFbsBitmap->IsRomBitmap())
73 User::Leave(KErrAccessDenied);
75 iTypefaceStore = CFbsTypefaceStore::NewL(this);
76 iFbsBmp = new(ELeave) CFbsBitGcBitmap;
77 User::LeaveIfError(iFbsBmp->Duplicate(aFbsBitmap->Handle()));
78 const TDisplayMode dispMode = iFbsBmp->DisplayMode();
80 iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iFbsBmp->SizeInPixels(), dispMode, iFbsBmp->DataStride());
82 TInt hwHandle = aFbsBitmap->HardwareBitmapHandle();
85 TRAP_IGNORE(iGraphicsAccelerator = CHardwareGraphicsAccelerator::NewL(RHardwareBitmap(hwHandle)));
87 if (iGraphicsAccelerator==NULL)
89 TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
93 /** Resizes the device.
95 @param aSize The new size in pixels.
96 @return KErrNone, if successful; otherwise another of the system-wide error
98 EXPORT_C TInt CFbsBitmapDevice::Resize(const TSize& aSize)
100 if(!iDrawDevice || !iFbsBmp)
103 if(aSize.iWidth < 0 || aSize.iHeight < 0)
106 if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
109 if (iFbsBmp->HardwareBitmapHandle())
111 return KErrNotSupported;
114 const TDisplayMode dispMode = iFbsBmp->DisplayMode();
115 CFbsDrawDevice* drawDevice = NULL;
116 TRAPD(err, drawDevice = CFbsDrawDevice::NewBitmapDeviceL(aSize, dispMode, CFbsBitmap::ScanLineLength(aSize.iWidth, dispMode)));
120 TInt ret = iFbsBmp->Resize(aSize);
128 drawDevice->SetBits(NULL);
129 iDrawDevice = drawDevice;
130 iOrientation = CFbsBitGc::EGraphicsOrientationNormal;
132 // Now get a new GraphicsAccelerator but it doesn't matter if we fail, we can work without one
133 delete iGraphicsAccelerator;
134 iGraphicsAccelerator = NULL;
135 TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
140 This method is called when you are about to start direct drawing to the bitmap memory.
141 Calls to DrawingBegin() must be paired with a subsequent call to DrawingEnd().
142 Also, code must not leave between a DrawingBegin() - DrawingEnd() pair.
143 @param aAlways Not used.
145 @see CFbsBitmapDevice::DrawingEnd()
147 EXPORT_C void CFbsBitmapDevice::DrawingBegin(TBool /*aAlways*/)
149 iFbsBmp->BeginDataAccess();
154 This method is called when you have finished direct drawing to the bitmap memory.
155 Calls to DrawingEnd() must correspond to a prior call to DrawingBegin().
156 @param aAlways Not used.
158 @see CFbsBitmapDevice::DrawingBegin()
160 EXPORT_C void CFbsBitmapDevice::DrawingEnd(TBool /*aAlways*/)
162 iDrawDevice->SetBits(NULL);
163 iFbsBmp->EndDataAccess(EFalse);
166 void CFbsBitmapDevice::SetBits()
169 // Bitmap devices only support normal orientation
170 TInt devHeight = iDrawDevice->SizeInPixels().iHeight;
171 MScalingSettings* scaling;
172 if (iDrawDevice->GetInterface(KScalingSettingsInterfaceID, (TAny*&)scaling) == KErrNone)
174 TInt factorX, factorY, divisorX, divisorY;
175 scaling->Get(factorX, factorY, divisorX, divisorY);
176 // Both divisorX and divisorY should be 1
179 devHeight = (devHeight - 1) * factorY + 1;
183 BG_ASSERT_DEBUG(iFbsBmp->DataStride() == iDrawDevice->ScanLineBytes(), EBitgdiPanicInvalidBitmap);
184 BG_ASSERT_DEBUG(iFbsBmp->SizeInPixels().iHeight >= devHeight, EBitgdiPanicInvalidBitmap);
185 TUint32* data = iFbsBmp->DataAddress();
186 BG_ASSERT_ALWAYS(data, EBitgdiPanicInvalidBitmap);
187 iDrawDevice->SetBits(data);
191 /** Copies a scanline into a buffer.
193 The function provides a concrete implementation of the pure virtual
194 function CBitmapDevice::GetScanLine(). */
195 EXPORT_C void CFbsBitmapDevice::GetScanLine(TDes8& aBuf,
196 const TPoint& aPixel,
198 TDisplayMode aDispMode) const
200 iFbsBmp->BeginDataAccess();
201 CONST_CAST(CFbsBitmapDevice*,this)->SetBits();
202 CONST_CAST(CFbsBitmapDevice*,this)->DoGetScanLine(aBuf,aPixel,aLength,aDispMode);
203 iFbsBmp->EndDataAccess(ETrue);
206 /** Gets the RGB colour of an individual pixel on a bitmapped graphics
209 The function provides a concrete implementation of the pure virtual
210 function CBitmapDevice::GetPixel(). */
211 EXPORT_C void CFbsBitmapDevice::GetPixel(TRgb& aColor,const TPoint& aPoint) const
214 iDrawDevice->GetDrawRect(deviceRect);
215 if (!deviceRect.Contains(aPoint))
218 iFbsBmp->BeginDataAccess();
219 ((CFbsBitmapDevice*)this)->SetBits();
220 aColor = iDrawDevice->ReadPixel(aPoint.iX,aPoint.iY);
221 iFbsBmp->EndDataAccess(ETrue);
225 /** Converts a horizontal dimension of a device in pixels to a horizontal
228 The function provides a concrete implementation of the pure virtual
229 function MGraphicsDeviceMap::HorizontalPixelsToTwips(). */
230 EXPORT_C TInt CFbsBitmapDevice::HorizontalPixelsToTwips(TInt aPixels) const
232 return iFbsBmp->HorizontalPixelsToTwips(aPixels);
236 /** Converts a vertical dimension of a device in pixels to a vertical
239 The function provides a concrete implementation of the pure virtual
240 function MGraphicsDeviceMap::VerticalPixelsToTwips(). */
241 EXPORT_C TInt CFbsBitmapDevice::VerticalPixelsToTwips(TInt aPixels) const
243 return iFbsBmp->VerticalPixelsToTwips(aPixels);
247 /** Gets the size of the device, in twips.
249 @return The size of the device. */
250 EXPORT_C TSize CFbsBitmapDevice::SizeInTwips() const
252 return iFbsBmp->SizeInTwips();
255 /** Converts a horizontal dimension of a device in twips to a horizontal
258 The function provides a concrete implementation of the pure virtual
259 function MGraphicsDeviceMap::HorizontalTwipsToPixels(). */
260 EXPORT_C TInt CFbsBitmapDevice::HorizontalTwipsToPixels(TInt aTwips) const
262 return iFbsBmp->HorizontalTwipsToPixels(aTwips);
266 /** Converts a vertical dimension of a device in twips to a vertical
269 The function provides a concrete implementation of the pure virtual
270 function MGraphicsDeviceMap::VerticalTwipsToPixels(). */
271 EXPORT_C TInt CFbsBitmapDevice::VerticalTwipsToPixels(TInt aTwips) const
273 return iFbsBmp->VerticalTwipsToPixels(aTwips);
277 /** Gets the palette attributes of the device.
279 The function provides a concrete implementation of the pure virtual
280 function CGraphicsDevice::PaletteAttributes(). */
281 EXPORT_C void CFbsBitmapDevice::PaletteAttributes(TBool& aModifiable,TInt& aNumEntries) const
283 aModifiable = (iDrawDevice->DisplayMode() == EColor256);
284 aNumEntries = TDisplayModeUtils::NumDisplayModeColors(iDrawDevice->DisplayMode());
287 /** Sets the device's palette to the specified palette.
289 Setting the palette is only possible if the device has a modifiable palette,
290 which can be determined by calling PaletteAttributes().
292 The function provides a concrete implementation of the pure virtual
293 function CGraphicsDevice::SetPalette(). */
294 EXPORT_C void CFbsBitmapDevice::SetPalette(CPalette* aPalette)
296 SetCustomPalette(aPalette); // Have to ignore error for compatibility
300 /** Gets the device's current palette.
302 This function is only supported if the device has a modifiable palette,
303 which can be determined by calling PaletteAttributes().
305 The function provides a concrete implementation of the pure virtual
306 function CGraphicsDevice::GetPalette(). */
307 EXPORT_C TInt CFbsBitmapDevice::GetPalette(CPalette*& aPalette) const
309 return iDrawDevice->GetCustomPalette(aPalette);
313 The method swaps bitmap device's width and height.
314 For example: if the size is (40, 20), the swapped size will be (20, 40).
315 The device's content is not preserved.
316 The method leaves CFbsBitmapDevice object in a consistent state -
317 scaling settings will be set with their default values (the scaling is switched off),
318 the device's dither origin will be set to (0,0), scaling origin to (0,0).
320 Note: If the device was scaled or its dither origin was set with a non-default value,
321 it has to be rescaled again, respectivelly the dither origin has to be set again.
323 Note: All graphics contexts, already created by the device, should be
324 re-activated calling CFbsBitGc::Activate().
326 Note: Do not call SwapWidthAndHeight() between DrawingBegin() and DrawingEnd() calls!
328 @return KErrNone The call was successfull.
329 @return KErrAccessDenied ROM bitmap size can't be swapped.
330 @return KErrNotSupported Hardware bitmap size can't be swapped.
331 @return KErrGeneral iDrawDevice or iFbsBmp is NULL.
333 EXPORT_C TInt CFbsBitmapDevice::SwapWidthAndHeight()
335 if(!iDrawDevice || !iFbsBmp)
339 TInt err = iFbsBmp->SwapWidthAndHeight();
342 iDrawDevice->SwapWidthAndHeight();
349 Required to ensure BC between NGage and 7.0S platforms.
350 Functions are exported at ordinal corresponding to where NGage platform
351 has extended this library and must not be moved.
353 EXPORT_C void DummyReserved1()
355 User::Panic(_L("Dummy Function"), 0);
357 EXPORT_C void DummyReserved2()
359 User::Panic(_L("Dummy Function"), 0);
361 EXPORT_C void DummyReserved3()
363 User::Panic(_L("Dummy Function"), 0);
365 EXPORT_C void DummyReserved4()
367 User::Panic(_L("Dummy Function"), 0);
369 EXPORT_C void DummyReserved5()
371 User::Panic(_L("Dummy Function"), 0);
373 EXPORT_C void DummyReserved6()
375 User::Panic(_L("Dummy Function"), 0);
377 EXPORT_C void DummyReserved7()
379 User::Panic(_L("Dummy Function"), 0);
381 EXPORT_C void DummyReserved8()
383 User::Panic(_L("Dummy Function"), 0);
385 EXPORT_C void DummyReserved9()
387 User::Panic(_L("Dummy Function"), 0);
389 EXPORT_C void DummyReserved10()
391 User::Panic(_L("Dummy Function"), 0);