os/graphics/graphicsdeviceinterface/bitgdi/sbit/BITMAPDV.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/graphics/graphicsdeviceinterface/bitgdi/sbit/BITMAPDV.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,392 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <bitdev.h>
    1.20 +#include <bitdraw.h>
    1.21 +#include <bitdrawscaling.h>
    1.22 +#include <bitdrawinterfaceid.h>
    1.23 +#include "BITPANIC.H"
    1.24 +
    1.25 +const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems
    1.26 +
    1.27 +CFbsBitmapDevice::CFbsBitmapDevice():
    1.28 +	CFbsDevice()
    1.29 +	{}
    1.30 +
    1.31 +
    1.32 +/** Frees all resources owned by the object prior to its destruction. */
    1.33 +EXPORT_C CFbsBitmapDevice::~CFbsBitmapDevice()
    1.34 +	{
    1.35 +	delete iFbsBmp;
    1.36 +	}
    1.37 +
    1.38 +
    1.39 +/** Constructs the object from the specified Font and Bitmap server
    1.40 +managed bitmap.
    1.41 +
    1.42 +@param aFbsBitmap A pointer to a Font and Bitmap server managed bitmap.
    1.43 +@param aLibname Name of the library to create the low-level CFbsDrawDevice 
    1.44 +object from.
    1.45 +@return A pointer to the newly constructed graphics device. */
    1.46 +EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap,
    1.47 +												  const TDesC& /*aLibname*/)
    1.48 +    {
    1.49 +	return CFbsBitmapDevice::NewL(aFbsBitmap);
    1.50 +	}
    1.51 +
    1.52 +/** Allocates and constructs the device with the bitmap. Also creates a 2D graphics 
    1.53 +accelerator which is owned and used by the device.
    1.54 +
    1.55 +@param aFbsBitmap A pointer to the font and bitmap server managed bitmap.
    1.56 +@leave KErrArgument The bitmap's handle is zero.
    1.57 +@leave KErrAccessDenied The bitmap is in the ROM.
    1.58 +@return A pointer to the newly constructed device.
    1.59 +@panic EBitgdiPanicInvalidBitmap aFbsBitmap is NULL. */
    1.60 +EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap)
    1.61 +	{
    1.62 +	BG_ASSERT_ALWAYS(aFbsBitmap != NULL,EBitgdiPanicInvalidBitmap);
    1.63 +
    1.64 +	CFbsBitmapDevice* self = new(ELeave) CFbsBitmapDevice;
    1.65 +	CleanupStack::PushL(self);
    1.66 +	self->ConstructL(aFbsBitmap);
    1.67 +	CleanupStack::Pop(); // self
    1.68 +	return self;
    1.69 +	}
    1.70 +
    1.71 +void CFbsBitmapDevice::ConstructL(CFbsBitmap* aFbsBitmap)
    1.72 +	{
    1.73 +	if(!aFbsBitmap->Handle())
    1.74 +		User::Leave(KErrArgument);
    1.75 +	if(aFbsBitmap->IsRomBitmap())
    1.76 +		User::Leave(KErrAccessDenied);
    1.77 +
    1.78 +	iTypefaceStore = CFbsTypefaceStore::NewL(this);
    1.79 +	iFbsBmp = new(ELeave) CFbsBitGcBitmap;
    1.80 +	User::LeaveIfError(iFbsBmp->Duplicate(aFbsBitmap->Handle()));
    1.81 +	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
    1.82 +
    1.83 +	iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iFbsBmp->SizeInPixels(), dispMode, iFbsBmp->DataStride());
    1.84 +
    1.85 +	TInt hwHandle = aFbsBitmap->HardwareBitmapHandle();
    1.86 +	if(hwHandle)
    1.87 +		{
    1.88 +		TRAP_IGNORE(iGraphicsAccelerator = CHardwareGraphicsAccelerator::NewL(RHardwareBitmap(hwHandle)));
    1.89 +		}
    1.90 +	if (iGraphicsAccelerator==NULL)
    1.91 +		{
    1.92 +		TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
    1.93 +		}
    1.94 +	}
    1.95 +
    1.96 +/** Resizes the device.
    1.97 +
    1.98 +@param aSize The new size in pixels. 
    1.99 +@return KErrNone, if successful; otherwise another of the system-wide error 
   1.100 +codes. */
   1.101 +EXPORT_C TInt CFbsBitmapDevice::Resize(const TSize& aSize)
   1.102 +	{
   1.103 +	if(!iDrawDevice || !iFbsBmp)
   1.104 +		return(KErrGeneral);
   1.105 +
   1.106 +	if(aSize.iWidth < 0 || aSize.iHeight < 0)
   1.107 +		return KErrArgument;
   1.108 +
   1.109 +	if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
   1.110 +		return KErrTooBig;
   1.111 +
   1.112 +    if (iFbsBmp->HardwareBitmapHandle())
   1.113 +        {
   1.114 +        return KErrNotSupported;
   1.115 +        }
   1.116 +
   1.117 +	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
   1.118 +	CFbsDrawDevice* drawDevice = NULL;
   1.119 +	TRAPD(err, drawDevice = CFbsDrawDevice::NewBitmapDeviceL(aSize, dispMode, CFbsBitmap::ScanLineLength(aSize.iWidth, dispMode)));
   1.120 +	if (err != KErrNone)
   1.121 +		return err;
   1.122 +
   1.123 +	TInt ret = iFbsBmp->Resize(aSize);
   1.124 +	if (ret != KErrNone)
   1.125 +		{
   1.126 +		delete drawDevice;
   1.127 +		return ret;
   1.128 +		}
   1.129 +
   1.130 +	delete iDrawDevice;
   1.131 +	drawDevice->SetBits(NULL);
   1.132 +	iDrawDevice = drawDevice;
   1.133 +	iOrientation = CFbsBitGc::EGraphicsOrientationNormal;
   1.134 +
   1.135 +	// Now get a new GraphicsAccelerator but it doesn't matter if we fail, we can work without one
   1.136 +	delete iGraphicsAccelerator;
   1.137 +	iGraphicsAccelerator = NULL;
   1.138 +	TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
   1.139 +	return KErrNone;
   1.140 +	}
   1.141 +
   1.142 +/**
   1.143 +This method is called when you are about to start direct drawing to the bitmap memory.
   1.144 +Calls to DrawingBegin() must be paired with a subsequent call to DrawingEnd().
   1.145 +Also, code must not leave between a DrawingBegin() - DrawingEnd() pair.
   1.146 +@param aAlways Not used.
   1.147 +
   1.148 +@see CFbsBitmapDevice::DrawingEnd()
   1.149 +*/
   1.150 +EXPORT_C void CFbsBitmapDevice::DrawingBegin(TBool /*aAlways*/)
   1.151 +	{
   1.152 +	iFbsBmp->BeginDataAccess();
   1.153 +	SetBits();
   1.154 +	}
   1.155 +
   1.156 +/**
   1.157 +This method is called when you have finished direct drawing to the bitmap memory.
   1.158 +Calls to DrawingEnd() must correspond to a prior call to DrawingBegin().
   1.159 +@param aAlways Not used.
   1.160 +
   1.161 +@see CFbsBitmapDevice::DrawingBegin()
   1.162 +*/
   1.163 +EXPORT_C void CFbsBitmapDevice::DrawingEnd(TBool /*aAlways*/)
   1.164 +	{
   1.165 +	iDrawDevice->SetBits(NULL);
   1.166 +	iFbsBmp->EndDataAccess(EFalse);
   1.167 +	}
   1.168 +
   1.169 +void CFbsBitmapDevice::SetBits()
   1.170 +	{
   1.171 +#ifdef _DEBUG
   1.172 +	// Bitmap devices only support normal orientation
   1.173 +	TInt devHeight = iDrawDevice->SizeInPixels().iHeight;
   1.174 +	MScalingSettings* scaling;
   1.175 +	if (iDrawDevice->GetInterface(KScalingSettingsInterfaceID, (TAny*&)scaling) == KErrNone)
   1.176 +		{
   1.177 +		TInt factorX, factorY, divisorX, divisorY;
   1.178 +		scaling->Get(factorX, factorY, divisorX, divisorY);
   1.179 +		// Both divisorX and divisorY should be 1
   1.180 +		if (factorY > 1)
   1.181 +			{
   1.182 +			devHeight = (devHeight - 1) * factorY + 1;
   1.183 +			}
   1.184 +		}
   1.185 +#endif
   1.186 +	BG_ASSERT_DEBUG(iFbsBmp->DataStride() == iDrawDevice->ScanLineBytes(), EBitgdiPanicInvalidBitmap);
   1.187 +	BG_ASSERT_DEBUG(iFbsBmp->SizeInPixels().iHeight >= devHeight, EBitgdiPanicInvalidBitmap);
   1.188 +	TUint32* data = iFbsBmp->DataAddress();
   1.189 +	BG_ASSERT_ALWAYS(data, EBitgdiPanicInvalidBitmap);
   1.190 +	iDrawDevice->SetBits(data);
   1.191 +	}
   1.192 +
   1.193 +
   1.194 +/** Copies a scanline into a buffer.
   1.195 +
   1.196 +The function provides a concrete implementation of the pure virtual
   1.197 +function CBitmapDevice::GetScanLine(). */
   1.198 +EXPORT_C void CFbsBitmapDevice::GetScanLine(TDes8& aBuf,
   1.199 +											const TPoint& aPixel,
   1.200 +											TInt aLength,
   1.201 +											TDisplayMode aDispMode) const
   1.202 + 	{
   1.203 +	iFbsBmp->BeginDataAccess();
   1.204 +	CONST_CAST(CFbsBitmapDevice*,this)->SetBits();
   1.205 +	CONST_CAST(CFbsBitmapDevice*,this)->DoGetScanLine(aBuf,aPixel,aLength,aDispMode);
   1.206 +	iFbsBmp->EndDataAccess(ETrue);
   1.207 +	}
   1.208 +
   1.209 +/** Gets the RGB colour of an individual pixel on a bitmapped graphics
   1.210 +device.
   1.211 +
   1.212 +The function provides a concrete implementation of the pure virtual
   1.213 +function CBitmapDevice::GetPixel(). */
   1.214 +EXPORT_C void CFbsBitmapDevice::GetPixel(TRgb& aColor,const TPoint& aPoint) const
   1.215 +	{
   1.216 +	TRect deviceRect;
   1.217 +	iDrawDevice->GetDrawRect(deviceRect);
   1.218 +	if (!deviceRect.Contains(aPoint))
   1.219 +		return;
   1.220 +
   1.221 +	iFbsBmp->BeginDataAccess();
   1.222 +	((CFbsBitmapDevice*)this)->SetBits();
   1.223 +	aColor = iDrawDevice->ReadPixel(aPoint.iX,aPoint.iY);
   1.224 +	iFbsBmp->EndDataAccess(ETrue);
   1.225 +	}
   1.226 +
   1.227 +
   1.228 +/** Converts a horizontal dimension of a device in pixels to a horizontal
   1.229 +dimension in twips.
   1.230 +
   1.231 +The function provides a concrete implementation of the pure virtual
   1.232 +function MGraphicsDeviceMap::HorizontalPixelsToTwips(). */	 
   1.233 +EXPORT_C TInt CFbsBitmapDevice::HorizontalPixelsToTwips(TInt aPixels) const
   1.234 +    {
   1.235 +	return iFbsBmp->HorizontalPixelsToTwips(aPixels);
   1.236 +	}
   1.237 +
   1.238 +
   1.239 +/** Converts a vertical dimension of a device in pixels to a vertical
   1.240 +dimension in twips.
   1.241 +
   1.242 +The function provides a concrete implementation of the pure virtual
   1.243 +function MGraphicsDeviceMap::VerticalPixelsToTwips(). */
   1.244 +EXPORT_C TInt CFbsBitmapDevice::VerticalPixelsToTwips(TInt aPixels) const
   1.245 +  	{
   1.246 +	return iFbsBmp->VerticalPixelsToTwips(aPixels);
   1.247 +	}
   1.248 +
   1.249 +
   1.250 +/** Gets the size of the device, in twips.
   1.251 +
   1.252 +@return The size of the device. */
   1.253 +EXPORT_C TSize CFbsBitmapDevice::SizeInTwips() const
   1.254 +	{
   1.255 +	return iFbsBmp->SizeInTwips();
   1.256 +	}
   1.257 +
   1.258 +/**  Converts a horizontal dimension of a device in twips to a horizontal
   1.259 +dimension in pixels.
   1.260 +
   1.261 +The function provides a concrete implementation of the pure virtual
   1.262 +function MGraphicsDeviceMap::HorizontalTwipsToPixels(). */
   1.263 +EXPORT_C TInt CFbsBitmapDevice::HorizontalTwipsToPixels(TInt aTwips) const
   1.264 +	{
   1.265 +	return iFbsBmp->HorizontalTwipsToPixels(aTwips);
   1.266 +	}
   1.267 +
   1.268 +
   1.269 +/** Converts a vertical dimension of a device in twips to a vertical
   1.270 +dimension in pixels.
   1.271 +
   1.272 +The function provides a concrete implementation of the pure virtual
   1.273 +function MGraphicsDeviceMap::VerticalTwipsToPixels(). */	
   1.274 +EXPORT_C TInt CFbsBitmapDevice::VerticalTwipsToPixels(TInt aTwips) const
   1.275 +    {
   1.276 +	return iFbsBmp->VerticalTwipsToPixels(aTwips);
   1.277 +	}
   1.278 +
   1.279 +
   1.280 +/** Gets the palette attributes of the device.
   1.281 +
   1.282 +The function provides a concrete implementation of the pure virtual
   1.283 +function CGraphicsDevice::PaletteAttributes(). */	
   1.284 +EXPORT_C void CFbsBitmapDevice::PaletteAttributes(TBool& aModifiable,TInt& aNumEntries) const
   1.285 +    {
   1.286 +	aModifiable = (iDrawDevice->DisplayMode() == EColor256);
   1.287 +	aNumEntries = TDisplayModeUtils::NumDisplayModeColors(iDrawDevice->DisplayMode());
   1.288 +	}
   1.289 +
   1.290 +/** Sets the device's palette to the specified palette.
   1.291 +
   1.292 +Setting the palette is only possible if the device has a modifiable palette, 
   1.293 +which can be determined by calling PaletteAttributes().
   1.294 +
   1.295 +The function provides a concrete implementation of the pure virtual
   1.296 +function CGraphicsDevice::SetPalette(). */
   1.297 +EXPORT_C void CFbsBitmapDevice::SetPalette(CPalette* aPalette)
   1.298 +	{
   1.299 +	SetCustomPalette(aPalette); // Have to ignore error for compatibility
   1.300 +	}
   1.301 +
   1.302 +
   1.303 +/** Gets the device's current palette.
   1.304 +
   1.305 +This function is only supported if the device has a modifiable palette, 
   1.306 +which can be determined by calling PaletteAttributes().
   1.307 +
   1.308 +The function provides a concrete implementation of the pure virtual
   1.309 +function CGraphicsDevice::GetPalette(). */	
   1.310 +EXPORT_C TInt CFbsBitmapDevice::GetPalette(CPalette*& aPalette) const
   1.311 +    {
   1.312 +	return iDrawDevice->GetCustomPalette(aPalette);
   1.313 +	}
   1.314 +
   1.315 +/**
   1.316 +The method swaps bitmap device's width and height.
   1.317 +For example: if the size is (40, 20), the swapped size will be (20, 40).
   1.318 +The device's content is not preserved.
   1.319 +The method leaves CFbsBitmapDevice object in a consistent state - 
   1.320 +scaling settings will be set with their default values (the scaling is switched off),
   1.321 +the device's dither origin will be set to (0,0), scaling origin to (0,0).
   1.322 +
   1.323 +Note: If the device was scaled or its dither origin was set with a non-default value,
   1.324 +it has to be rescaled again, respectivelly the dither origin has to be set again.
   1.325 +
   1.326 +Note: All graphics contexts, already created by the device, should be 
   1.327 +re-activated calling CFbsBitGc::Activate(). 
   1.328 +
   1.329 +Note: Do not call SwapWidthAndHeight() between DrawingBegin() and DrawingEnd() calls!
   1.330 +
   1.331 +@return KErrNone The call was successfull.
   1.332 +@return KErrAccessDenied ROM bitmap size can't be swapped.
   1.333 +@return KErrNotSupported Hardware bitmap size can't be swapped.
   1.334 +@return KErrGeneral iDrawDevice or iFbsBmp is NULL.
   1.335 +*/
   1.336 +EXPORT_C TInt CFbsBitmapDevice::SwapWidthAndHeight()
   1.337 +	{
   1.338 +	if(!iDrawDevice || !iFbsBmp)
   1.339 +		{
   1.340 +		return KErrGeneral;
   1.341 +		}
   1.342 +	TInt err = iFbsBmp->SwapWidthAndHeight();
   1.343 +	if(err == KErrNone)
   1.344 +		{
   1.345 +		iDrawDevice->SwapWidthAndHeight();
   1.346 +		}
   1.347 +	return err;
   1.348 +	}
   1.349 +
   1.350 +
   1.351 +/**
   1.352 + Required to ensure BC between NGage and 7.0S platforms.  
   1.353 + Functions are exported at ordinal corresponding to where NGage platform
   1.354 + has extended this library and must not be moved.
   1.355 + */
   1.356 +EXPORT_C void DummyReserved1()
   1.357 +	{
   1.358 +	User::Panic(_L("Dummy Function"), 0);
   1.359 +	}
   1.360 +EXPORT_C void DummyReserved2()
   1.361 +	{
   1.362 +	User::Panic(_L("Dummy Function"), 0);
   1.363 +	}
   1.364 +EXPORT_C void DummyReserved3()
   1.365 +	{
   1.366 +	User::Panic(_L("Dummy Function"), 0);
   1.367 +	}
   1.368 +EXPORT_C void DummyReserved4()
   1.369 +	{
   1.370 +	User::Panic(_L("Dummy Function"), 0);
   1.371 +	}
   1.372 +EXPORT_C void DummyReserved5()
   1.373 +	{
   1.374 +	User::Panic(_L("Dummy Function"), 0);
   1.375 +	}
   1.376 +EXPORT_C void DummyReserved6()
   1.377 +	{
   1.378 +	User::Panic(_L("Dummy Function"), 0);
   1.379 +	}
   1.380 +EXPORT_C void DummyReserved7()
   1.381 +	{
   1.382 +	User::Panic(_L("Dummy Function"), 0);
   1.383 +	}
   1.384 +EXPORT_C void DummyReserved8()
   1.385 +	{
   1.386 +	User::Panic(_L("Dummy Function"), 0);
   1.387 +	}
   1.388 +EXPORT_C void DummyReserved9()
   1.389 +	{
   1.390 +	User::Panic(_L("Dummy Function"), 0);
   1.391 +	}
   1.392 +EXPORT_C void DummyReserved10()
   1.393 +	{
   1.394 +	User::Panic(_L("Dummy Function"), 0);
   1.395 +	}