sl@0: // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "BITPANIC.H" sl@0: sl@0: const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems sl@0: sl@0: CFbsBitmapDevice::CFbsBitmapDevice(): sl@0: CFbsDevice() sl@0: {} sl@0: sl@0: sl@0: /** Frees all resources owned by the object prior to its destruction. */ sl@0: EXPORT_C CFbsBitmapDevice::~CFbsBitmapDevice() sl@0: { sl@0: delete iFbsBmp; sl@0: } sl@0: sl@0: sl@0: /** Constructs the object from the specified Font and Bitmap server sl@0: managed bitmap. sl@0: sl@0: @param aFbsBitmap A pointer to a Font and Bitmap server managed bitmap. sl@0: @param aLibname Name of the library to create the low-level CFbsDrawDevice sl@0: object from. sl@0: @return A pointer to the newly constructed graphics device. */ sl@0: EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap, sl@0: const TDesC& /*aLibname*/) sl@0: { sl@0: return CFbsBitmapDevice::NewL(aFbsBitmap); sl@0: } sl@0: sl@0: /** Allocates and constructs the device with the bitmap. Also creates a 2D graphics sl@0: accelerator which is owned and used by the device. sl@0: sl@0: @param aFbsBitmap A pointer to the font and bitmap server managed bitmap. sl@0: @leave KErrArgument The bitmap's handle is zero. sl@0: @leave KErrAccessDenied The bitmap is in the ROM. sl@0: @return A pointer to the newly constructed device. sl@0: @panic EBitgdiPanicInvalidBitmap aFbsBitmap is NULL. */ sl@0: EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap) sl@0: { sl@0: BG_ASSERT_ALWAYS(aFbsBitmap != NULL,EBitgdiPanicInvalidBitmap); sl@0: sl@0: CFbsBitmapDevice* self = new(ELeave) CFbsBitmapDevice; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aFbsBitmap); sl@0: CleanupStack::Pop(); // self sl@0: return self; sl@0: } sl@0: sl@0: void CFbsBitmapDevice::ConstructL(CFbsBitmap* aFbsBitmap) sl@0: { sl@0: if(!aFbsBitmap->Handle()) sl@0: User::Leave(KErrArgument); sl@0: if(aFbsBitmap->IsRomBitmap()) sl@0: User::Leave(KErrAccessDenied); sl@0: sl@0: iTypefaceStore = CFbsTypefaceStore::NewL(this); sl@0: iFbsBmp = new(ELeave) CFbsBitGcBitmap; sl@0: User::LeaveIfError(iFbsBmp->Duplicate(aFbsBitmap->Handle())); sl@0: const TDisplayMode dispMode = iFbsBmp->DisplayMode(); sl@0: sl@0: iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iFbsBmp->SizeInPixels(), dispMode, iFbsBmp->DataStride()); sl@0: sl@0: TInt hwHandle = aFbsBitmap->HardwareBitmapHandle(); sl@0: if(hwHandle) sl@0: { sl@0: TRAP_IGNORE(iGraphicsAccelerator = CHardwareGraphicsAccelerator::NewL(RHardwareBitmap(hwHandle))); sl@0: } sl@0: if (iGraphicsAccelerator==NULL) sl@0: { sl@0: TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp)); sl@0: } sl@0: } sl@0: sl@0: /** Resizes the device. sl@0: sl@0: @param aSize The new size in pixels. sl@0: @return KErrNone, if successful; otherwise another of the system-wide error sl@0: codes. */ sl@0: EXPORT_C TInt CFbsBitmapDevice::Resize(const TSize& aSize) sl@0: { sl@0: if(!iDrawDevice || !iFbsBmp) sl@0: return(KErrGeneral); sl@0: sl@0: if(aSize.iWidth < 0 || aSize.iHeight < 0) sl@0: return KErrArgument; sl@0: sl@0: if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize) sl@0: return KErrTooBig; sl@0: sl@0: if (iFbsBmp->HardwareBitmapHandle()) sl@0: { sl@0: return KErrNotSupported; sl@0: } sl@0: sl@0: const TDisplayMode dispMode = iFbsBmp->DisplayMode(); sl@0: CFbsDrawDevice* drawDevice = NULL; sl@0: TRAPD(err, drawDevice = CFbsDrawDevice::NewBitmapDeviceL(aSize, dispMode, CFbsBitmap::ScanLineLength(aSize.iWidth, dispMode))); sl@0: if (err != KErrNone) sl@0: return err; sl@0: sl@0: TInt ret = iFbsBmp->Resize(aSize); sl@0: if (ret != KErrNone) sl@0: { sl@0: delete drawDevice; sl@0: return ret; sl@0: } sl@0: sl@0: delete iDrawDevice; sl@0: drawDevice->SetBits(NULL); sl@0: iDrawDevice = drawDevice; sl@0: iOrientation = CFbsBitGc::EGraphicsOrientationNormal; sl@0: sl@0: // Now get a new GraphicsAccelerator but it doesn't matter if we fail, we can work without one sl@0: delete iGraphicsAccelerator; sl@0: iGraphicsAccelerator = NULL; sl@0: TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp)); sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** sl@0: This method is called when you are about to start direct drawing to the bitmap memory. sl@0: Calls to DrawingBegin() must be paired with a subsequent call to DrawingEnd(). sl@0: Also, code must not leave between a DrawingBegin() - DrawingEnd() pair. sl@0: @param aAlways Not used. sl@0: sl@0: @see CFbsBitmapDevice::DrawingEnd() sl@0: */ sl@0: EXPORT_C void CFbsBitmapDevice::DrawingBegin(TBool /*aAlways*/) sl@0: { sl@0: iFbsBmp->BeginDataAccess(); sl@0: SetBits(); sl@0: } sl@0: sl@0: /** sl@0: This method is called when you have finished direct drawing to the bitmap memory. sl@0: Calls to DrawingEnd() must correspond to a prior call to DrawingBegin(). sl@0: @param aAlways Not used. sl@0: sl@0: @see CFbsBitmapDevice::DrawingBegin() sl@0: */ sl@0: EXPORT_C void CFbsBitmapDevice::DrawingEnd(TBool /*aAlways*/) sl@0: { sl@0: iDrawDevice->SetBits(NULL); sl@0: iFbsBmp->EndDataAccess(EFalse); sl@0: } sl@0: sl@0: void CFbsBitmapDevice::SetBits() sl@0: { sl@0: #ifdef _DEBUG sl@0: // Bitmap devices only support normal orientation sl@0: TInt devHeight = iDrawDevice->SizeInPixels().iHeight; sl@0: MScalingSettings* scaling; sl@0: if (iDrawDevice->GetInterface(KScalingSettingsInterfaceID, (TAny*&)scaling) == KErrNone) sl@0: { sl@0: TInt factorX, factorY, divisorX, divisorY; sl@0: scaling->Get(factorX, factorY, divisorX, divisorY); sl@0: // Both divisorX and divisorY should be 1 sl@0: if (factorY > 1) sl@0: { sl@0: devHeight = (devHeight - 1) * factorY + 1; sl@0: } sl@0: } sl@0: #endif sl@0: BG_ASSERT_DEBUG(iFbsBmp->DataStride() == iDrawDevice->ScanLineBytes(), EBitgdiPanicInvalidBitmap); sl@0: BG_ASSERT_DEBUG(iFbsBmp->SizeInPixels().iHeight >= devHeight, EBitgdiPanicInvalidBitmap); sl@0: TUint32* data = iFbsBmp->DataAddress(); sl@0: BG_ASSERT_ALWAYS(data, EBitgdiPanicInvalidBitmap); sl@0: iDrawDevice->SetBits(data); sl@0: } sl@0: sl@0: sl@0: /** Copies a scanline into a buffer. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function CBitmapDevice::GetScanLine(). */ sl@0: EXPORT_C void CFbsBitmapDevice::GetScanLine(TDes8& aBuf, sl@0: const TPoint& aPixel, sl@0: TInt aLength, sl@0: TDisplayMode aDispMode) const sl@0: { sl@0: iFbsBmp->BeginDataAccess(); sl@0: CONST_CAST(CFbsBitmapDevice*,this)->SetBits(); sl@0: CONST_CAST(CFbsBitmapDevice*,this)->DoGetScanLine(aBuf,aPixel,aLength,aDispMode); sl@0: iFbsBmp->EndDataAccess(ETrue); sl@0: } sl@0: sl@0: /** Gets the RGB colour of an individual pixel on a bitmapped graphics sl@0: device. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function CBitmapDevice::GetPixel(). */ sl@0: EXPORT_C void CFbsBitmapDevice::GetPixel(TRgb& aColor,const TPoint& aPoint) const sl@0: { sl@0: TRect deviceRect; sl@0: iDrawDevice->GetDrawRect(deviceRect); sl@0: if (!deviceRect.Contains(aPoint)) sl@0: return; sl@0: sl@0: iFbsBmp->BeginDataAccess(); sl@0: ((CFbsBitmapDevice*)this)->SetBits(); sl@0: aColor = iDrawDevice->ReadPixel(aPoint.iX,aPoint.iY); sl@0: iFbsBmp->EndDataAccess(ETrue); sl@0: } sl@0: sl@0: sl@0: /** Converts a horizontal dimension of a device in pixels to a horizontal sl@0: dimension in twips. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function MGraphicsDeviceMap::HorizontalPixelsToTwips(). */ sl@0: EXPORT_C TInt CFbsBitmapDevice::HorizontalPixelsToTwips(TInt aPixels) const sl@0: { sl@0: return iFbsBmp->HorizontalPixelsToTwips(aPixels); sl@0: } sl@0: sl@0: sl@0: /** Converts a vertical dimension of a device in pixels to a vertical sl@0: dimension in twips. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function MGraphicsDeviceMap::VerticalPixelsToTwips(). */ sl@0: EXPORT_C TInt CFbsBitmapDevice::VerticalPixelsToTwips(TInt aPixels) const sl@0: { sl@0: return iFbsBmp->VerticalPixelsToTwips(aPixels); sl@0: } sl@0: sl@0: sl@0: /** Gets the size of the device, in twips. sl@0: sl@0: @return The size of the device. */ sl@0: EXPORT_C TSize CFbsBitmapDevice::SizeInTwips() const sl@0: { sl@0: return iFbsBmp->SizeInTwips(); sl@0: } sl@0: sl@0: /** Converts a horizontal dimension of a device in twips to a horizontal sl@0: dimension in pixels. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function MGraphicsDeviceMap::HorizontalTwipsToPixels(). */ sl@0: EXPORT_C TInt CFbsBitmapDevice::HorizontalTwipsToPixels(TInt aTwips) const sl@0: { sl@0: return iFbsBmp->HorizontalTwipsToPixels(aTwips); sl@0: } sl@0: sl@0: sl@0: /** Converts a vertical dimension of a device in twips to a vertical sl@0: dimension in pixels. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function MGraphicsDeviceMap::VerticalTwipsToPixels(). */ sl@0: EXPORT_C TInt CFbsBitmapDevice::VerticalTwipsToPixels(TInt aTwips) const sl@0: { sl@0: return iFbsBmp->VerticalTwipsToPixels(aTwips); sl@0: } sl@0: sl@0: sl@0: /** Gets the palette attributes of the device. sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function CGraphicsDevice::PaletteAttributes(). */ sl@0: EXPORT_C void CFbsBitmapDevice::PaletteAttributes(TBool& aModifiable,TInt& aNumEntries) const sl@0: { sl@0: aModifiable = (iDrawDevice->DisplayMode() == EColor256); sl@0: aNumEntries = TDisplayModeUtils::NumDisplayModeColors(iDrawDevice->DisplayMode()); sl@0: } sl@0: sl@0: /** Sets the device's palette to the specified palette. sl@0: sl@0: Setting the palette is only possible if the device has a modifiable palette, sl@0: which can be determined by calling PaletteAttributes(). sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function CGraphicsDevice::SetPalette(). */ sl@0: EXPORT_C void CFbsBitmapDevice::SetPalette(CPalette* aPalette) sl@0: { sl@0: SetCustomPalette(aPalette); // Have to ignore error for compatibility sl@0: } sl@0: sl@0: sl@0: /** Gets the device's current palette. sl@0: sl@0: This function is only supported if the device has a modifiable palette, sl@0: which can be determined by calling PaletteAttributes(). sl@0: sl@0: The function provides a concrete implementation of the pure virtual sl@0: function CGraphicsDevice::GetPalette(). */ sl@0: EXPORT_C TInt CFbsBitmapDevice::GetPalette(CPalette*& aPalette) const sl@0: { sl@0: return iDrawDevice->GetCustomPalette(aPalette); sl@0: } sl@0: sl@0: /** sl@0: The method swaps bitmap device's width and height. sl@0: For example: if the size is (40, 20), the swapped size will be (20, 40). sl@0: The device's content is not preserved. sl@0: The method leaves CFbsBitmapDevice object in a consistent state - sl@0: scaling settings will be set with their default values (the scaling is switched off), sl@0: the device's dither origin will be set to (0,0), scaling origin to (0,0). sl@0: sl@0: Note: If the device was scaled or its dither origin was set with a non-default value, sl@0: it has to be rescaled again, respectivelly the dither origin has to be set again. sl@0: sl@0: Note: All graphics contexts, already created by the device, should be sl@0: re-activated calling CFbsBitGc::Activate(). sl@0: sl@0: Note: Do not call SwapWidthAndHeight() between DrawingBegin() and DrawingEnd() calls! sl@0: sl@0: @return KErrNone The call was successfull. sl@0: @return KErrAccessDenied ROM bitmap size can't be swapped. sl@0: @return KErrNotSupported Hardware bitmap size can't be swapped. sl@0: @return KErrGeneral iDrawDevice or iFbsBmp is NULL. sl@0: */ sl@0: EXPORT_C TInt CFbsBitmapDevice::SwapWidthAndHeight() sl@0: { sl@0: if(!iDrawDevice || !iFbsBmp) sl@0: { sl@0: return KErrGeneral; sl@0: } sl@0: TInt err = iFbsBmp->SwapWidthAndHeight(); sl@0: if(err == KErrNone) sl@0: { sl@0: iDrawDevice->SwapWidthAndHeight(); sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Required to ensure BC between NGage and 7.0S platforms. sl@0: Functions are exported at ordinal corresponding to where NGage platform sl@0: has extended this library and must not be moved. sl@0: */ sl@0: EXPORT_C void DummyReserved1() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved2() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved3() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved4() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved5() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved6() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved7() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved8() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved9() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: } sl@0: EXPORT_C void DummyReserved10() sl@0: { sl@0: User::Panic(_L("Dummy Function"), 0); sl@0: }