os/graphics/graphicsdeviceinterface/bitgdi/sbit/BITMAPDV.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <bitdev.h>
    17 #include <bitdraw.h>
    18 #include <bitdrawscaling.h>
    19 #include <bitdrawinterfaceid.h>
    20 #include "BITPANIC.H"
    21 
    22 const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems
    23 
    24 CFbsBitmapDevice::CFbsBitmapDevice():
    25 	CFbsDevice()
    26 	{}
    27 
    28 
    29 /** Frees all resources owned by the object prior to its destruction. */
    30 EXPORT_C CFbsBitmapDevice::~CFbsBitmapDevice()
    31 	{
    32 	delete iFbsBmp;
    33 	}
    34 
    35 
    36 /** Constructs the object from the specified Font and Bitmap server
    37 managed bitmap.
    38 
    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 
    41 object from.
    42 @return A pointer to the newly constructed graphics device. */
    43 EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap,
    44 												  const TDesC& /*aLibname*/)
    45     {
    46 	return CFbsBitmapDevice::NewL(aFbsBitmap);
    47 	}
    48 
    49 /** Allocates and constructs the device with the bitmap. Also creates a 2D graphics 
    50 accelerator which is owned and used by the device.
    51 
    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)
    58 	{
    59 	BG_ASSERT_ALWAYS(aFbsBitmap != NULL,EBitgdiPanicInvalidBitmap);
    60 
    61 	CFbsBitmapDevice* self = new(ELeave) CFbsBitmapDevice;
    62 	CleanupStack::PushL(self);
    63 	self->ConstructL(aFbsBitmap);
    64 	CleanupStack::Pop(); // self
    65 	return self;
    66 	}
    67 
    68 void CFbsBitmapDevice::ConstructL(CFbsBitmap* aFbsBitmap)
    69 	{
    70 	if(!aFbsBitmap->Handle())
    71 		User::Leave(KErrArgument);
    72 	if(aFbsBitmap->IsRomBitmap())
    73 		User::Leave(KErrAccessDenied);
    74 
    75 	iTypefaceStore = CFbsTypefaceStore::NewL(this);
    76 	iFbsBmp = new(ELeave) CFbsBitGcBitmap;
    77 	User::LeaveIfError(iFbsBmp->Duplicate(aFbsBitmap->Handle()));
    78 	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
    79 
    80 	iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iFbsBmp->SizeInPixels(), dispMode, iFbsBmp->DataStride());
    81 
    82 	TInt hwHandle = aFbsBitmap->HardwareBitmapHandle();
    83 	if(hwHandle)
    84 		{
    85 		TRAP_IGNORE(iGraphicsAccelerator = CHardwareGraphicsAccelerator::NewL(RHardwareBitmap(hwHandle)));
    86 		}
    87 	if (iGraphicsAccelerator==NULL)
    88 		{
    89 		TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
    90 		}
    91 	}
    92 
    93 /** Resizes the device.
    94 
    95 @param aSize The new size in pixels. 
    96 @return KErrNone, if successful; otherwise another of the system-wide error 
    97 codes. */
    98 EXPORT_C TInt CFbsBitmapDevice::Resize(const TSize& aSize)
    99 	{
   100 	if(!iDrawDevice || !iFbsBmp)
   101 		return(KErrGeneral);
   102 
   103 	if(aSize.iWidth < 0 || aSize.iHeight < 0)
   104 		return KErrArgument;
   105 
   106 	if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
   107 		return KErrTooBig;
   108 
   109     if (iFbsBmp->HardwareBitmapHandle())
   110         {
   111         return KErrNotSupported;
   112         }
   113 
   114 	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
   115 	CFbsDrawDevice* drawDevice = NULL;
   116 	TRAPD(err, drawDevice = CFbsDrawDevice::NewBitmapDeviceL(aSize, dispMode, CFbsBitmap::ScanLineLength(aSize.iWidth, dispMode)));
   117 	if (err != KErrNone)
   118 		return err;
   119 
   120 	TInt ret = iFbsBmp->Resize(aSize);
   121 	if (ret != KErrNone)
   122 		{
   123 		delete drawDevice;
   124 		return ret;
   125 		}
   126 
   127 	delete iDrawDevice;
   128 	drawDevice->SetBits(NULL);
   129 	iDrawDevice = drawDevice;
   130 	iOrientation = CFbsBitGc::EGraphicsOrientationNormal;
   131 
   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));
   136 	return KErrNone;
   137 	}
   138 
   139 /**
   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.
   144 
   145 @see CFbsBitmapDevice::DrawingEnd()
   146 */
   147 EXPORT_C void CFbsBitmapDevice::DrawingBegin(TBool /*aAlways*/)
   148 	{
   149 	iFbsBmp->BeginDataAccess();
   150 	SetBits();
   151 	}
   152 
   153 /**
   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.
   157 
   158 @see CFbsBitmapDevice::DrawingBegin()
   159 */
   160 EXPORT_C void CFbsBitmapDevice::DrawingEnd(TBool /*aAlways*/)
   161 	{
   162 	iDrawDevice->SetBits(NULL);
   163 	iFbsBmp->EndDataAccess(EFalse);
   164 	}
   165 
   166 void CFbsBitmapDevice::SetBits()
   167 	{
   168 #ifdef _DEBUG
   169 	// Bitmap devices only support normal orientation
   170 	TInt devHeight = iDrawDevice->SizeInPixels().iHeight;
   171 	MScalingSettings* scaling;
   172 	if (iDrawDevice->GetInterface(KScalingSettingsInterfaceID, (TAny*&)scaling) == KErrNone)
   173 		{
   174 		TInt factorX, factorY, divisorX, divisorY;
   175 		scaling->Get(factorX, factorY, divisorX, divisorY);
   176 		// Both divisorX and divisorY should be 1
   177 		if (factorY > 1)
   178 			{
   179 			devHeight = (devHeight - 1) * factorY + 1;
   180 			}
   181 		}
   182 #endif
   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);
   188 	}
   189 
   190 
   191 /** Copies a scanline into a buffer.
   192 
   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,
   197 											TInt aLength,
   198 											TDisplayMode aDispMode) const
   199  	{
   200 	iFbsBmp->BeginDataAccess();
   201 	CONST_CAST(CFbsBitmapDevice*,this)->SetBits();
   202 	CONST_CAST(CFbsBitmapDevice*,this)->DoGetScanLine(aBuf,aPixel,aLength,aDispMode);
   203 	iFbsBmp->EndDataAccess(ETrue);
   204 	}
   205 
   206 /** Gets the RGB colour of an individual pixel on a bitmapped graphics
   207 device.
   208 
   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
   212 	{
   213 	TRect deviceRect;
   214 	iDrawDevice->GetDrawRect(deviceRect);
   215 	if (!deviceRect.Contains(aPoint))
   216 		return;
   217 
   218 	iFbsBmp->BeginDataAccess();
   219 	((CFbsBitmapDevice*)this)->SetBits();
   220 	aColor = iDrawDevice->ReadPixel(aPoint.iX,aPoint.iY);
   221 	iFbsBmp->EndDataAccess(ETrue);
   222 	}
   223 
   224 
   225 /** Converts a horizontal dimension of a device in pixels to a horizontal
   226 dimension in twips.
   227 
   228 The function provides a concrete implementation of the pure virtual
   229 function MGraphicsDeviceMap::HorizontalPixelsToTwips(). */	 
   230 EXPORT_C TInt CFbsBitmapDevice::HorizontalPixelsToTwips(TInt aPixels) const
   231     {
   232 	return iFbsBmp->HorizontalPixelsToTwips(aPixels);
   233 	}
   234 
   235 
   236 /** Converts a vertical dimension of a device in pixels to a vertical
   237 dimension in twips.
   238 
   239 The function provides a concrete implementation of the pure virtual
   240 function MGraphicsDeviceMap::VerticalPixelsToTwips(). */
   241 EXPORT_C TInt CFbsBitmapDevice::VerticalPixelsToTwips(TInt aPixels) const
   242   	{
   243 	return iFbsBmp->VerticalPixelsToTwips(aPixels);
   244 	}
   245 
   246 
   247 /** Gets the size of the device, in twips.
   248 
   249 @return The size of the device. */
   250 EXPORT_C TSize CFbsBitmapDevice::SizeInTwips() const
   251 	{
   252 	return iFbsBmp->SizeInTwips();
   253 	}
   254 
   255 /**  Converts a horizontal dimension of a device in twips to a horizontal
   256 dimension in pixels.
   257 
   258 The function provides a concrete implementation of the pure virtual
   259 function MGraphicsDeviceMap::HorizontalTwipsToPixels(). */
   260 EXPORT_C TInt CFbsBitmapDevice::HorizontalTwipsToPixels(TInt aTwips) const
   261 	{
   262 	return iFbsBmp->HorizontalTwipsToPixels(aTwips);
   263 	}
   264 
   265 
   266 /** Converts a vertical dimension of a device in twips to a vertical
   267 dimension in pixels.
   268 
   269 The function provides a concrete implementation of the pure virtual
   270 function MGraphicsDeviceMap::VerticalTwipsToPixels(). */	
   271 EXPORT_C TInt CFbsBitmapDevice::VerticalTwipsToPixels(TInt aTwips) const
   272     {
   273 	return iFbsBmp->VerticalTwipsToPixels(aTwips);
   274 	}
   275 
   276 
   277 /** Gets the palette attributes of the device.
   278 
   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
   282     {
   283 	aModifiable = (iDrawDevice->DisplayMode() == EColor256);
   284 	aNumEntries = TDisplayModeUtils::NumDisplayModeColors(iDrawDevice->DisplayMode());
   285 	}
   286 
   287 /** Sets the device's palette to the specified palette.
   288 
   289 Setting the palette is only possible if the device has a modifiable palette, 
   290 which can be determined by calling PaletteAttributes().
   291 
   292 The function provides a concrete implementation of the pure virtual
   293 function CGraphicsDevice::SetPalette(). */
   294 EXPORT_C void CFbsBitmapDevice::SetPalette(CPalette* aPalette)
   295 	{
   296 	SetCustomPalette(aPalette); // Have to ignore error for compatibility
   297 	}
   298 
   299 
   300 /** Gets the device's current palette.
   301 
   302 This function is only supported if the device has a modifiable palette, 
   303 which can be determined by calling PaletteAttributes().
   304 
   305 The function provides a concrete implementation of the pure virtual
   306 function CGraphicsDevice::GetPalette(). */	
   307 EXPORT_C TInt CFbsBitmapDevice::GetPalette(CPalette*& aPalette) const
   308     {
   309 	return iDrawDevice->GetCustomPalette(aPalette);
   310 	}
   311 
   312 /**
   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).
   319 
   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.
   322 
   323 Note: All graphics contexts, already created by the device, should be 
   324 re-activated calling CFbsBitGc::Activate(). 
   325 
   326 Note: Do not call SwapWidthAndHeight() between DrawingBegin() and DrawingEnd() calls!
   327 
   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.
   332 */
   333 EXPORT_C TInt CFbsBitmapDevice::SwapWidthAndHeight()
   334 	{
   335 	if(!iDrawDevice || !iFbsBmp)
   336 		{
   337 		return KErrGeneral;
   338 		}
   339 	TInt err = iFbsBmp->SwapWidthAndHeight();
   340 	if(err == KErrNone)
   341 		{
   342 		iDrawDevice->SwapWidthAndHeight();
   343 		}
   344 	return err;
   345 	}
   346 
   347 
   348 /**
   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.
   352  */
   353 EXPORT_C void DummyReserved1()
   354 	{
   355 	User::Panic(_L("Dummy Function"), 0);
   356 	}
   357 EXPORT_C void DummyReserved2()
   358 	{
   359 	User::Panic(_L("Dummy Function"), 0);
   360 	}
   361 EXPORT_C void DummyReserved3()
   362 	{
   363 	User::Panic(_L("Dummy Function"), 0);
   364 	}
   365 EXPORT_C void DummyReserved4()
   366 	{
   367 	User::Panic(_L("Dummy Function"), 0);
   368 	}
   369 EXPORT_C void DummyReserved5()
   370 	{
   371 	User::Panic(_L("Dummy Function"), 0);
   372 	}
   373 EXPORT_C void DummyReserved6()
   374 	{
   375 	User::Panic(_L("Dummy Function"), 0);
   376 	}
   377 EXPORT_C void DummyReserved7()
   378 	{
   379 	User::Panic(_L("Dummy Function"), 0);
   380 	}
   381 EXPORT_C void DummyReserved8()
   382 	{
   383 	User::Panic(_L("Dummy Function"), 0);
   384 	}
   385 EXPORT_C void DummyReserved9()
   386 	{
   387 	User::Panic(_L("Dummy Function"), 0);
   388 	}
   389 EXPORT_C void DummyReserved10()
   390 	{
   391 	User::Panic(_L("Dummy Function"), 0);
   392 	}