os/graphics/graphicsdeviceinterface/bitgdi/sbit/BITMAPDV.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <bitdev.h>
sl@0
    17
#include <bitdraw.h>
sl@0
    18
#include <bitdrawscaling.h>
sl@0
    19
#include <bitdrawinterfaceid.h>
sl@0
    20
#include "BITPANIC.H"
sl@0
    21
sl@0
    22
const TInt KMaxPixelSize = KMaxTInt / 4; // Maximum pixel size to avoid some overflow problems
sl@0
    23
sl@0
    24
CFbsBitmapDevice::CFbsBitmapDevice():
sl@0
    25
	CFbsDevice()
sl@0
    26
	{}
sl@0
    27
sl@0
    28
sl@0
    29
/** Frees all resources owned by the object prior to its destruction. */
sl@0
    30
EXPORT_C CFbsBitmapDevice::~CFbsBitmapDevice()
sl@0
    31
	{
sl@0
    32
	delete iFbsBmp;
sl@0
    33
	}
sl@0
    34
sl@0
    35
sl@0
    36
/** Constructs the object from the specified Font and Bitmap server
sl@0
    37
managed bitmap.
sl@0
    38
sl@0
    39
@param aFbsBitmap A pointer to a Font and Bitmap server managed bitmap.
sl@0
    40
@param aLibname Name of the library to create the low-level CFbsDrawDevice 
sl@0
    41
object from.
sl@0
    42
@return A pointer to the newly constructed graphics device. */
sl@0
    43
EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap,
sl@0
    44
												  const TDesC& /*aLibname*/)
sl@0
    45
    {
sl@0
    46
	return CFbsBitmapDevice::NewL(aFbsBitmap);
sl@0
    47
	}
sl@0
    48
sl@0
    49
/** Allocates and constructs the device with the bitmap. Also creates a 2D graphics 
sl@0
    50
accelerator which is owned and used by the device.
sl@0
    51
sl@0
    52
@param aFbsBitmap A pointer to the font and bitmap server managed bitmap.
sl@0
    53
@leave KErrArgument The bitmap's handle is zero.
sl@0
    54
@leave KErrAccessDenied The bitmap is in the ROM.
sl@0
    55
@return A pointer to the newly constructed device.
sl@0
    56
@panic EBitgdiPanicInvalidBitmap aFbsBitmap is NULL. */
sl@0
    57
EXPORT_C CFbsBitmapDevice* CFbsBitmapDevice::NewL(CFbsBitmap* aFbsBitmap)
sl@0
    58
	{
sl@0
    59
	BG_ASSERT_ALWAYS(aFbsBitmap != NULL,EBitgdiPanicInvalidBitmap);
sl@0
    60
sl@0
    61
	CFbsBitmapDevice* self = new(ELeave) CFbsBitmapDevice;
sl@0
    62
	CleanupStack::PushL(self);
sl@0
    63
	self->ConstructL(aFbsBitmap);
sl@0
    64
	CleanupStack::Pop(); // self
sl@0
    65
	return self;
sl@0
    66
	}
sl@0
    67
sl@0
    68
void CFbsBitmapDevice::ConstructL(CFbsBitmap* aFbsBitmap)
sl@0
    69
	{
sl@0
    70
	if(!aFbsBitmap->Handle())
sl@0
    71
		User::Leave(KErrArgument);
sl@0
    72
	if(aFbsBitmap->IsRomBitmap())
sl@0
    73
		User::Leave(KErrAccessDenied);
sl@0
    74
sl@0
    75
	iTypefaceStore = CFbsTypefaceStore::NewL(this);
sl@0
    76
	iFbsBmp = new(ELeave) CFbsBitGcBitmap;
sl@0
    77
	User::LeaveIfError(iFbsBmp->Duplicate(aFbsBitmap->Handle()));
sl@0
    78
	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
sl@0
    79
sl@0
    80
	iDrawDevice = CFbsDrawDevice::NewBitmapDeviceL(iFbsBmp->SizeInPixels(), dispMode, iFbsBmp->DataStride());
sl@0
    81
sl@0
    82
	TInt hwHandle = aFbsBitmap->HardwareBitmapHandle();
sl@0
    83
	if(hwHandle)
sl@0
    84
		{
sl@0
    85
		TRAP_IGNORE(iGraphicsAccelerator = CHardwareGraphicsAccelerator::NewL(RHardwareBitmap(hwHandle)));
sl@0
    86
		}
sl@0
    87
	if (iGraphicsAccelerator==NULL)
sl@0
    88
		{
sl@0
    89
		TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
sl@0
    90
		}
sl@0
    91
	}
sl@0
    92
sl@0
    93
/** Resizes the device.
sl@0
    94
sl@0
    95
@param aSize The new size in pixels. 
sl@0
    96
@return KErrNone, if successful; otherwise another of the system-wide error 
sl@0
    97
codes. */
sl@0
    98
EXPORT_C TInt CFbsBitmapDevice::Resize(const TSize& aSize)
sl@0
    99
	{
sl@0
   100
	if(!iDrawDevice || !iFbsBmp)
sl@0
   101
		return(KErrGeneral);
sl@0
   102
sl@0
   103
	if(aSize.iWidth < 0 || aSize.iHeight < 0)
sl@0
   104
		return KErrArgument;
sl@0
   105
sl@0
   106
	if (aSize.iWidth > KMaxPixelSize || aSize.iHeight > KMaxPixelSize)
sl@0
   107
		return KErrTooBig;
sl@0
   108
sl@0
   109
    if (iFbsBmp->HardwareBitmapHandle())
sl@0
   110
        {
sl@0
   111
        return KErrNotSupported;
sl@0
   112
        }
sl@0
   113
sl@0
   114
	const TDisplayMode dispMode = iFbsBmp->DisplayMode();
sl@0
   115
	CFbsDrawDevice* drawDevice = NULL;
sl@0
   116
	TRAPD(err, drawDevice = CFbsDrawDevice::NewBitmapDeviceL(aSize, dispMode, CFbsBitmap::ScanLineLength(aSize.iWidth, dispMode)));
sl@0
   117
	if (err != KErrNone)
sl@0
   118
		return err;
sl@0
   119
sl@0
   120
	TInt ret = iFbsBmp->Resize(aSize);
sl@0
   121
	if (ret != KErrNone)
sl@0
   122
		{
sl@0
   123
		delete drawDevice;
sl@0
   124
		return ret;
sl@0
   125
		}
sl@0
   126
sl@0
   127
	delete iDrawDevice;
sl@0
   128
	drawDevice->SetBits(NULL);
sl@0
   129
	iDrawDevice = drawDevice;
sl@0
   130
	iOrientation = CFbsBitGc::EGraphicsOrientationNormal;
sl@0
   131
sl@0
   132
	// Now get a new GraphicsAccelerator but it doesn't matter if we fail, we can work without one
sl@0
   133
	delete iGraphicsAccelerator;
sl@0
   134
	iGraphicsAccelerator = NULL;
sl@0
   135
	TRAP_IGNORE(iGraphicsAccelerator = CSoftwareGraphicsAccelerator::NewL(iFbsBmp));
sl@0
   136
	return KErrNone;
sl@0
   137
	}
sl@0
   138
sl@0
   139
/**
sl@0
   140
This method is called when you are about to start direct drawing to the bitmap memory.
sl@0
   141
Calls to DrawingBegin() must be paired with a subsequent call to DrawingEnd().
sl@0
   142
Also, code must not leave between a DrawingBegin() - DrawingEnd() pair.
sl@0
   143
@param aAlways Not used.
sl@0
   144
sl@0
   145
@see CFbsBitmapDevice::DrawingEnd()
sl@0
   146
*/
sl@0
   147
EXPORT_C void CFbsBitmapDevice::DrawingBegin(TBool /*aAlways*/)
sl@0
   148
	{
sl@0
   149
	iFbsBmp->BeginDataAccess();
sl@0
   150
	SetBits();
sl@0
   151
	}
sl@0
   152
sl@0
   153
/**
sl@0
   154
This method is called when you have finished direct drawing to the bitmap memory.
sl@0
   155
Calls to DrawingEnd() must correspond to a prior call to DrawingBegin().
sl@0
   156
@param aAlways Not used.
sl@0
   157
sl@0
   158
@see CFbsBitmapDevice::DrawingBegin()
sl@0
   159
*/
sl@0
   160
EXPORT_C void CFbsBitmapDevice::DrawingEnd(TBool /*aAlways*/)
sl@0
   161
	{
sl@0
   162
	iDrawDevice->SetBits(NULL);
sl@0
   163
	iFbsBmp->EndDataAccess(EFalse);
sl@0
   164
	}
sl@0
   165
sl@0
   166
void CFbsBitmapDevice::SetBits()
sl@0
   167
	{
sl@0
   168
#ifdef _DEBUG
sl@0
   169
	// Bitmap devices only support normal orientation
sl@0
   170
	TInt devHeight = iDrawDevice->SizeInPixels().iHeight;
sl@0
   171
	MScalingSettings* scaling;
sl@0
   172
	if (iDrawDevice->GetInterface(KScalingSettingsInterfaceID, (TAny*&)scaling) == KErrNone)
sl@0
   173
		{
sl@0
   174
		TInt factorX, factorY, divisorX, divisorY;
sl@0
   175
		scaling->Get(factorX, factorY, divisorX, divisorY);
sl@0
   176
		// Both divisorX and divisorY should be 1
sl@0
   177
		if (factorY > 1)
sl@0
   178
			{
sl@0
   179
			devHeight = (devHeight - 1) * factorY + 1;
sl@0
   180
			}
sl@0
   181
		}
sl@0
   182
#endif
sl@0
   183
	BG_ASSERT_DEBUG(iFbsBmp->DataStride() == iDrawDevice->ScanLineBytes(), EBitgdiPanicInvalidBitmap);
sl@0
   184
	BG_ASSERT_DEBUG(iFbsBmp->SizeInPixels().iHeight >= devHeight, EBitgdiPanicInvalidBitmap);
sl@0
   185
	TUint32* data = iFbsBmp->DataAddress();
sl@0
   186
	BG_ASSERT_ALWAYS(data, EBitgdiPanicInvalidBitmap);
sl@0
   187
	iDrawDevice->SetBits(data);
sl@0
   188
	}
sl@0
   189
sl@0
   190
sl@0
   191
/** Copies a scanline into a buffer.
sl@0
   192
sl@0
   193
The function provides a concrete implementation of the pure virtual
sl@0
   194
function CBitmapDevice::GetScanLine(). */
sl@0
   195
EXPORT_C void CFbsBitmapDevice::GetScanLine(TDes8& aBuf,
sl@0
   196
											const TPoint& aPixel,
sl@0
   197
											TInt aLength,
sl@0
   198
											TDisplayMode aDispMode) const
sl@0
   199
 	{
sl@0
   200
	iFbsBmp->BeginDataAccess();
sl@0
   201
	CONST_CAST(CFbsBitmapDevice*,this)->SetBits();
sl@0
   202
	CONST_CAST(CFbsBitmapDevice*,this)->DoGetScanLine(aBuf,aPixel,aLength,aDispMode);
sl@0
   203
	iFbsBmp->EndDataAccess(ETrue);
sl@0
   204
	}
sl@0
   205
sl@0
   206
/** Gets the RGB colour of an individual pixel on a bitmapped graphics
sl@0
   207
device.
sl@0
   208
sl@0
   209
The function provides a concrete implementation of the pure virtual
sl@0
   210
function CBitmapDevice::GetPixel(). */
sl@0
   211
EXPORT_C void CFbsBitmapDevice::GetPixel(TRgb& aColor,const TPoint& aPoint) const
sl@0
   212
	{
sl@0
   213
	TRect deviceRect;
sl@0
   214
	iDrawDevice->GetDrawRect(deviceRect);
sl@0
   215
	if (!deviceRect.Contains(aPoint))
sl@0
   216
		return;
sl@0
   217
sl@0
   218
	iFbsBmp->BeginDataAccess();
sl@0
   219
	((CFbsBitmapDevice*)this)->SetBits();
sl@0
   220
	aColor = iDrawDevice->ReadPixel(aPoint.iX,aPoint.iY);
sl@0
   221
	iFbsBmp->EndDataAccess(ETrue);
sl@0
   222
	}
sl@0
   223
sl@0
   224
sl@0
   225
/** Converts a horizontal dimension of a device in pixels to a horizontal
sl@0
   226
dimension in twips.
sl@0
   227
sl@0
   228
The function provides a concrete implementation of the pure virtual
sl@0
   229
function MGraphicsDeviceMap::HorizontalPixelsToTwips(). */	 
sl@0
   230
EXPORT_C TInt CFbsBitmapDevice::HorizontalPixelsToTwips(TInt aPixels) const
sl@0
   231
    {
sl@0
   232
	return iFbsBmp->HorizontalPixelsToTwips(aPixels);
sl@0
   233
	}
sl@0
   234
sl@0
   235
sl@0
   236
/** Converts a vertical dimension of a device in pixels to a vertical
sl@0
   237
dimension in twips.
sl@0
   238
sl@0
   239
The function provides a concrete implementation of the pure virtual
sl@0
   240
function MGraphicsDeviceMap::VerticalPixelsToTwips(). */
sl@0
   241
EXPORT_C TInt CFbsBitmapDevice::VerticalPixelsToTwips(TInt aPixels) const
sl@0
   242
  	{
sl@0
   243
	return iFbsBmp->VerticalPixelsToTwips(aPixels);
sl@0
   244
	}
sl@0
   245
sl@0
   246
sl@0
   247
/** Gets the size of the device, in twips.
sl@0
   248
sl@0
   249
@return The size of the device. */
sl@0
   250
EXPORT_C TSize CFbsBitmapDevice::SizeInTwips() const
sl@0
   251
	{
sl@0
   252
	return iFbsBmp->SizeInTwips();
sl@0
   253
	}
sl@0
   254
sl@0
   255
/**  Converts a horizontal dimension of a device in twips to a horizontal
sl@0
   256
dimension in pixels.
sl@0
   257
sl@0
   258
The function provides a concrete implementation of the pure virtual
sl@0
   259
function MGraphicsDeviceMap::HorizontalTwipsToPixels(). */
sl@0
   260
EXPORT_C TInt CFbsBitmapDevice::HorizontalTwipsToPixels(TInt aTwips) const
sl@0
   261
	{
sl@0
   262
	return iFbsBmp->HorizontalTwipsToPixels(aTwips);
sl@0
   263
	}
sl@0
   264
sl@0
   265
sl@0
   266
/** Converts a vertical dimension of a device in twips to a vertical
sl@0
   267
dimension in pixels.
sl@0
   268
sl@0
   269
The function provides a concrete implementation of the pure virtual
sl@0
   270
function MGraphicsDeviceMap::VerticalTwipsToPixels(). */	
sl@0
   271
EXPORT_C TInt CFbsBitmapDevice::VerticalTwipsToPixels(TInt aTwips) const
sl@0
   272
    {
sl@0
   273
	return iFbsBmp->VerticalTwipsToPixels(aTwips);
sl@0
   274
	}
sl@0
   275
sl@0
   276
sl@0
   277
/** Gets the palette attributes of the device.
sl@0
   278
sl@0
   279
The function provides a concrete implementation of the pure virtual
sl@0
   280
function CGraphicsDevice::PaletteAttributes(). */	
sl@0
   281
EXPORT_C void CFbsBitmapDevice::PaletteAttributes(TBool& aModifiable,TInt& aNumEntries) const
sl@0
   282
    {
sl@0
   283
	aModifiable = (iDrawDevice->DisplayMode() == EColor256);
sl@0
   284
	aNumEntries = TDisplayModeUtils::NumDisplayModeColors(iDrawDevice->DisplayMode());
sl@0
   285
	}
sl@0
   286
sl@0
   287
/** Sets the device's palette to the specified palette.
sl@0
   288
sl@0
   289
Setting the palette is only possible if the device has a modifiable palette, 
sl@0
   290
which can be determined by calling PaletteAttributes().
sl@0
   291
sl@0
   292
The function provides a concrete implementation of the pure virtual
sl@0
   293
function CGraphicsDevice::SetPalette(). */
sl@0
   294
EXPORT_C void CFbsBitmapDevice::SetPalette(CPalette* aPalette)
sl@0
   295
	{
sl@0
   296
	SetCustomPalette(aPalette); // Have to ignore error for compatibility
sl@0
   297
	}
sl@0
   298
sl@0
   299
sl@0
   300
/** Gets the device's current palette.
sl@0
   301
sl@0
   302
This function is only supported if the device has a modifiable palette, 
sl@0
   303
which can be determined by calling PaletteAttributes().
sl@0
   304
sl@0
   305
The function provides a concrete implementation of the pure virtual
sl@0
   306
function CGraphicsDevice::GetPalette(). */	
sl@0
   307
EXPORT_C TInt CFbsBitmapDevice::GetPalette(CPalette*& aPalette) const
sl@0
   308
    {
sl@0
   309
	return iDrawDevice->GetCustomPalette(aPalette);
sl@0
   310
	}
sl@0
   311
sl@0
   312
/**
sl@0
   313
The method swaps bitmap device's width and height.
sl@0
   314
For example: if the size is (40, 20), the swapped size will be (20, 40).
sl@0
   315
The device's content is not preserved.
sl@0
   316
The method leaves CFbsBitmapDevice object in a consistent state - 
sl@0
   317
scaling settings will be set with their default values (the scaling is switched off),
sl@0
   318
the device's dither origin will be set to (0,0), scaling origin to (0,0).
sl@0
   319
sl@0
   320
Note: If the device was scaled or its dither origin was set with a non-default value,
sl@0
   321
it has to be rescaled again, respectivelly the dither origin has to be set again.
sl@0
   322
sl@0
   323
Note: All graphics contexts, already created by the device, should be 
sl@0
   324
re-activated calling CFbsBitGc::Activate(). 
sl@0
   325
sl@0
   326
Note: Do not call SwapWidthAndHeight() between DrawingBegin() and DrawingEnd() calls!
sl@0
   327
sl@0
   328
@return KErrNone The call was successfull.
sl@0
   329
@return KErrAccessDenied ROM bitmap size can't be swapped.
sl@0
   330
@return KErrNotSupported Hardware bitmap size can't be swapped.
sl@0
   331
@return KErrGeneral iDrawDevice or iFbsBmp is NULL.
sl@0
   332
*/
sl@0
   333
EXPORT_C TInt CFbsBitmapDevice::SwapWidthAndHeight()
sl@0
   334
	{
sl@0
   335
	if(!iDrawDevice || !iFbsBmp)
sl@0
   336
		{
sl@0
   337
		return KErrGeneral;
sl@0
   338
		}
sl@0
   339
	TInt err = iFbsBmp->SwapWidthAndHeight();
sl@0
   340
	if(err == KErrNone)
sl@0
   341
		{
sl@0
   342
		iDrawDevice->SwapWidthAndHeight();
sl@0
   343
		}
sl@0
   344
	return err;
sl@0
   345
	}
sl@0
   346
sl@0
   347
sl@0
   348
/**
sl@0
   349
 Required to ensure BC between NGage and 7.0S platforms.  
sl@0
   350
 Functions are exported at ordinal corresponding to where NGage platform
sl@0
   351
 has extended this library and must not be moved.
sl@0
   352
 */
sl@0
   353
EXPORT_C void DummyReserved1()
sl@0
   354
	{
sl@0
   355
	User::Panic(_L("Dummy Function"), 0);
sl@0
   356
	}
sl@0
   357
EXPORT_C void DummyReserved2()
sl@0
   358
	{
sl@0
   359
	User::Panic(_L("Dummy Function"), 0);
sl@0
   360
	}
sl@0
   361
EXPORT_C void DummyReserved3()
sl@0
   362
	{
sl@0
   363
	User::Panic(_L("Dummy Function"), 0);
sl@0
   364
	}
sl@0
   365
EXPORT_C void DummyReserved4()
sl@0
   366
	{
sl@0
   367
	User::Panic(_L("Dummy Function"), 0);
sl@0
   368
	}
sl@0
   369
EXPORT_C void DummyReserved5()
sl@0
   370
	{
sl@0
   371
	User::Panic(_L("Dummy Function"), 0);
sl@0
   372
	}
sl@0
   373
EXPORT_C void DummyReserved6()
sl@0
   374
	{
sl@0
   375
	User::Panic(_L("Dummy Function"), 0);
sl@0
   376
	}
sl@0
   377
EXPORT_C void DummyReserved7()
sl@0
   378
	{
sl@0
   379
	User::Panic(_L("Dummy Function"), 0);
sl@0
   380
	}
sl@0
   381
EXPORT_C void DummyReserved8()
sl@0
   382
	{
sl@0
   383
	User::Panic(_L("Dummy Function"), 0);
sl@0
   384
	}
sl@0
   385
EXPORT_C void DummyReserved9()
sl@0
   386
	{
sl@0
   387
	User::Panic(_L("Dummy Function"), 0);
sl@0
   388
	}
sl@0
   389
EXPORT_C void DummyReserved10()
sl@0
   390
	{
sl@0
   391
	User::Panic(_L("Dummy Function"), 0);
sl@0
   392
	}