sl@0: // Copyright (c) 2006-2010 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: // This module implements the functions that create the screen class depending
sl@0: // on the screen type.
sl@0: // Include files                                                   
sl@0: // 
sl@0: //
sl@0: 
sl@0: /**
sl@0:  @file
sl@0: */
sl@0: /********************************************************************/
sl@0: #include "BITDRAW.H"
sl@0: #include <hal.h>
sl@0: #include "ScreenInfo.h"
sl@0: #include "scdraw.h"
sl@0: #include "scdraw.inl"
sl@0: #include <graphics/gdi/gdiconsts.h>
sl@0: #include <graphics/suerror.h>
sl@0: /**
sl@0: Creates an instance of CFbsDrawDevice class.
sl@0: @param aScreenNo Screen number
sl@0: @param aDispMode Display mode
sl@0: @param aScreenInfo Screen parameters: video memory address and screen size
sl@0: @return A pointer to the created CFbsDrawDevice object
sl@0: @leave System-wide error code including KErrNoMemory
sl@0: @internalComponent
sl@0: */
sl@0: static CFbsDrawDevice* CreateInstanceL(TInt aScreenNo,
sl@0: 									   TDisplayMode aDispMode,
sl@0: 									   const TScreenInfo& aScreenInfo)
sl@0: 	{
sl@0: 	CFbsDrawDevice* drawDevice = NULL;
sl@0: 
sl@0: 	TInt modeCount;
sl@0: 	TInt matchedMode=-1;
sl@0: 	//there is some "ambiguity" about 24 and 32 bit modes... 
sl@0: 	//They are both byte per color component, and both actually have 32 bits per pixel memory use.
sl@0: 	//This ambiguity does not exist between 12 and 16 bit modes,
sl@0: 	//because they are distinct color component patterns (x444, 565)
sl@0: 	//but for now 24 and 32 bit modes are considered equivalent here.
sl@0: 
sl@0: 	if (HAL::Get(aScreenNo, HALData::EDisplayNumModes,modeCount)== KErrNone && modeCount>=1)
sl@0: 		{	//If multiple modes are supported then the highest bpp must be found
sl@0: 		
sl@0: 		TInt reqBpp= TDisplayModeUtils::NumDisplayModeBitsPerPixel(aDispMode);
sl@0: 		TInt reqBpp2=reqBpp;
sl@0: 		if ( reqBpp==24 || reqBpp==32 ) //Best to be specific here. Who knows how likely is 30 or 64 bpp support?
sl@0: 			{
sl@0: 			reqBpp2=32+24 - reqBpp;   //reflect 24<==>32
sl@0: 			//Important compile-time decision embedded here: Only one 32-bit mode is supported
sl@0: 			if(CFbsDrawDevice::DisplayMode16M() != aDispMode)
sl@0: 				{
sl@0: 				User::Leave(KErrNotSupported);
sl@0: 				}
sl@0: 			}
sl@0: 		for (TInt mode=0; mode<modeCount; mode++)
sl@0: 			{
sl@0: 			TInt modeBpp=mode;
sl@0: 			if(HAL::Get(aScreenNo, HALData::EDisplayBitsPerPixel, modeBpp) == KErrNone)
sl@0: 				{
sl@0: 				if (modeBpp==reqBpp || modeBpp==reqBpp2)
sl@0: 					{
sl@0: 					matchedMode=mode;
sl@0: 					break;
sl@0: 					}
sl@0: 				}
sl@0: 			}
sl@0: 		}
sl@0: 	if (matchedMode==-1)
sl@0: 		{	//This is the expected error code
sl@0: 		User::Leave(KErrNotSupported);
sl@0: 		}
sl@0: 	//Switch the display mode, call the constructor of the class defined
sl@0: 	switch(aDispMode)
sl@0: 		{
sl@0: 	/** Monochrome display mode (1 bpp) */
sl@0: 	case EGray2:
sl@0: 		{
sl@0: 		CDrawOneBppScreenBitmap* drawDeviceX = new (ELeave) CDrawOneBppScreenBitmap;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** Four grayscales display mode (2 bpp) */
sl@0: 	case EGray4:
sl@0: 		{
sl@0: 		CDrawTwoBppScreenBitmap* drawDeviceX = new (ELeave) CDrawTwoBppScreenBitmap;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** 16 grayscales display mode (4 bpp) */
sl@0: 	case EGray16:
sl@0: 		{
sl@0: 		CDrawFourBppScreenBitmapGray* drawDeviceX = new (ELeave) CDrawFourBppScreenBitmapGray;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** 256 grayscales display mode (8 bpp) */
sl@0: 	case EGray256:
sl@0: 		{
sl@0: 		CDrawEightBppScreenBitmapGray* drawDeviceX = new (ELeave) CDrawEightBppScreenBitmapGray;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** Low colour EGA 16 colour display mode (4 bpp) */
sl@0: 	case EColor16:
sl@0: 		{
sl@0: 		CDrawFourBppScreenBitmapColor* drawDeviceX = new (ELeave) CDrawFourBppScreenBitmapColor;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** 256 colour display mode (8 bpp) */
sl@0: 	case EColor256:
sl@0: 		{
sl@0: 		CDrawEightBppScreenBitmapColor* drawDeviceX = new (ELeave) CDrawEightBppScreenBitmapColor;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	/** 4,000 colour display mode (16 bpp) */
sl@0: 	case EColor4K: // 12 Bpp color mode
sl@0: 		{
sl@0: 		CDrawTwelveBppScreenBitmapColor* drawDeviceX = new (ELeave) CDrawTwelveBppScreenBitmapColor;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 
sl@0: 	case EColor64K: // 16 Bpp color mode
sl@0: 		{
sl@0: 		CDrawSixteenBppScreenBitmap* drawDeviceX = new (ELeave) CDrawSixteenBppScreenBitmap;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	case EColor16MU:
sl@0: 		{
sl@0: 		CDrawUTwentyFourBppScreenBitmap* drawDeviceX = new (ELeave) CDrawUTwentyFourBppScreenBitmap;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	case EColor16MA:
sl@0: 		{
sl@0: 		CDrawThirtyTwoBppScreenBitmapAlpha* drawDeviceX = new (ELeave) CDrawThirtyTwoBppScreenBitmapAlpha;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	case EColor16MAP:
sl@0: 		{
sl@0: 		CDrawThirtyTwoBppScreenBitmapAlphaPM* drawDeviceX = new (ELeave) CDrawThirtyTwoBppScreenBitmapAlphaPM;
sl@0: 		drawDevice=drawDeviceX;
sl@0: 		CleanupStack::PushL(drawDevice) ;
sl@0: 		User::LeaveIfError(drawDeviceX->ConstructScreen(
sl@0: 	                                                    aScreenNo,
sl@0: 														aScreenInfo.iAddress, 
sl@0: 														aScreenInfo.iSize,matchedMode));
sl@0: 		}
sl@0: 		break;
sl@0: 	default:
sl@0: 		User::Leave(KErrNotSupported);
sl@0: 		}
sl@0: 
sl@0: 	CleanupStack::Pop(drawDevice);
sl@0: 	return drawDevice;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /********************************************************************/
sl@0: /*  Implementation of CFbsDrawDevice class                          */
sl@0: /********************************************************************/
sl@0: 
sl@0: /**
sl@0: This function calls the correct constructor in function of the display mode.
sl@0: @param aInfo, Structure of the LCD info
sl@0: @param aDispMode, display mode   
sl@0: @return A pointer to just created screen device, which implements CFbsDrawDevice interface
sl@0: @deprecated Use CFbsDrawDevice::NewScreenDeviceL(TInt aScreenNo, TDisplayMode aDispMode)
sl@0: */
sl@0: EXPORT_C CFbsDrawDevice* CFbsDrawDevice::NewScreenDeviceL(TScreenInfoV01 aInfo, TDisplayMode aDispMode)
sl@0: 	{
sl@0: 	__ASSERT_ALWAYS(aInfo.iScreenAddressValid, Panic(EScreenDriverPanicInvalidWindowHandle));
sl@0: 	TScreenInfo screenInfo(aInfo.iScreenAddress, aInfo.iScreenSize);
sl@0: 	return ::CreateInstanceL(KDefaultScreenNo, aDispMode, screenInfo);
sl@0: 	}
sl@0: 	
sl@0: 	
sl@0: 
sl@0: /**
sl@0: Creates a new screen device instance, which implements CFbsDrawDevice interface.
sl@0: The method has to be implemented for each type of supported video hardware.
sl@0: @param aScreenNo Screen number
sl@0: @param aDispMode Requested display mode
sl@0: @return A pointer to just created screen device, which implements CFbsDrawDevice interface
sl@0: @leave KErrNoMemory Not enough memory
sl@0: 	   KErrNotSupported The requested screen device type is not supported
sl@0: */
sl@0: EXPORT_C CFbsDrawDevice* CFbsDrawDevice::NewScreenDeviceL(TInt aScreenNo,
sl@0: 														  TDisplayMode aDispMode)
sl@0: 	{
sl@0: 	TInt width = 0, height = 0;
sl@0: 	User::LeaveIfError(HAL::Get(aScreenNo, HALData::EDisplayXPixels, width));
sl@0: 	User::LeaveIfError(HAL::Get(aScreenNo, HALData::EDisplayYPixels, height));
sl@0: 	__ASSERT_ALWAYS(width > 0 && height > 0, Panic(EScreenDriverPanicInvalidHalValue));
sl@0: 	
sl@0: 	TUint8* address = 0;
sl@0: 	
sl@0: 	TScreenInfo screenInfo(address, TSize(width, height));
sl@0: 	return ::CreateInstanceL(aScreenNo, aDispMode, screenInfo);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0:  Depending on the current graphics hardware this 
sl@0:  will return one of the 16M video modes defined in
sl@0:  TDisplayMode, or ENone if a 16M video mode is not supported.
sl@0:  The method has to be implemented on all hardware platforms.
sl@0:  @return a 16M display mode or ENone.
sl@0:  ENone - it means that current hardware doesn't have 16M color mode.
sl@0: */
sl@0: EXPORT_C TDisplayMode CFbsDrawDevice::DisplayMode16M()
sl@0: 	{
sl@0: 	return EColor16MA;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Complete construction of the helper object.
sl@0: @param aScreenNo	The screen number, starting from 0.
sl@0: @param aPixelFormat	Pixel format UID or 0 for default based on bpp
sl@0: @return KErrNone or a system wide error value.
sl@0: */
sl@0: TInt CScreenDeviceHelper::Construct(TInt aScreenNo, TUidPixelFormat aPixelFormat, TUint aHalMode)
sl@0: 	{
sl@0: 	iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EScreenField] = aScreenNo;		// Screen number
sl@0: 	iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField] = aHalMode;			// Rotation and hal mode index
sl@0: 	iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::ETypeGuidField] = aPixelFormat;	//May be zero for non-GCE modes
sl@0: 	iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::ETypeClassField] 
sl@0: 			= ((TUint32)(TSurfaceId::EScreenSurface) << TSurfaceId::TScreenSurfaceUsage::ETypeClassShift);	// Type
sl@0: 	iAssignedOrientation = EDeviceOrientationNormal; // Actual rotation is held seperately from surface ID
sl@0: 
sl@0: 	TInt val = 0;
sl@0: 	iHasChunk = EFalse;
sl@0: 	TInt ret = HAL::Get(aScreenNo,HALData::EDisplayMemoryHandle,val);
sl@0: 	if (ret == KErrNone)
sl@0: 		{
sl@0: 		__ASSERT_DEBUG(val != 0, Panic(EScreenDriverPanicInvalidHalValue));
sl@0: 		RChunk chunk;
sl@0: 		ret = chunk.SetReturnedHandle(val);
sl@0: 		if (ret != KErrNone)
sl@0: 			{
sl@0: 			return ret;
sl@0: 			}
sl@0: 		iChunk = chunk;
sl@0: 		ret = iChunk.Duplicate(RThread(), EOwnerProcess);
sl@0: 		// Close before checking for errors, as we don't want to leave the
sl@0: 		// temporary chunk handle floating about. 
sl@0: 		chunk.Close();
sl@0: 		if (ret != KErrNone)
sl@0: 			{
sl@0: 			return ret;
sl@0: 			}
sl@0: 		iHasChunk = ETrue;
sl@0: 		}
sl@0: 	// KErrNotSupported is returned if we can't get the Handle because it's not a driver
sl@0: 	// that supports the concept. We don't return that error, since it's perfectly valid
sl@0: 	// to not support this. 
sl@0: 	else if (ret != KErrNotSupported)   
sl@0: 		{
sl@0: 		return ret;
sl@0: 		}
sl@0: 	return iSurfaceUpdateSession.Connect();
sl@0: 	}
sl@0: 
sl@0: CScreenDeviceHelper::~CScreenDeviceHelper()
sl@0: 	{
sl@0: 	
sl@0: 	iSurfaceUpdateSession.Close();
sl@0: 	iChunk.Close();
sl@0: 	}
sl@0: 
sl@0: void CScreenDeviceHelper::NotifyWhenAvailable(TRequestStatus& aStatus)
sl@0: 	{
sl@0: 	iSurfaceUpdateSession.NotifyWhenAvailable(aStatus);
sl@0: 	}
sl@0: 
sl@0: void CScreenDeviceHelper::CancelUpdateNotification()
sl@0: 	{
sl@0: 	iSurfaceUpdateSession.CancelAllUpdateNotifications();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Implementation of corresponding function in CDrawDevice, utilizing a tracked
sl@0: update region. Updates the screen from the surface, if the update region is
sl@0: not empty.
sl@0: */
sl@0: void CScreenDeviceHelper::Update()
sl@0: 	{
sl@0: 	TRequestStatus updateComplete = KRequestPending;
sl@0: 	Update(updateComplete);
sl@0: 	User::WaitForRequest(updateComplete);
sl@0: 	}
sl@0: 
sl@0: void CScreenDeviceHelper::Update(TRequestStatus& aStatus)
sl@0: 	{
sl@0: 	if (!iUpdateRegion.IsEmpty())
sl@0: 		{
sl@0: 		iSurfaceUpdateSession.NotifyWhenAvailable(aStatus);
sl@0: 		iSurfaceUpdateSession.SubmitUpdate(KAllScreens, iSurface, 0, &iUpdateRegion);
sl@0: 		iUpdateRegion.Clear();
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		TRequestStatus* pComplete=&aStatus;
sl@0: 		User::RequestComplete(pComplete,KErrNone);	    
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Implementation of corresponding function in CDrawDevice, utilizing a tracked
sl@0: update region. Adds the given rectangle to the update region.
sl@0: @param aRect	Rectangle to be added to the update region.
sl@0: */
sl@0: void CScreenDeviceHelper::UpdateRegion(const TRect& aRect)
sl@0: 	{
sl@0: 	if (aRect.IsEmpty())
sl@0: 		{
sl@0: 		// Adding an empty rectangle should have no effect.
sl@0: 		return;
sl@0: 		}
sl@0: 
sl@0: 	if (iUpdateRegion.CheckError())
sl@0: 		{
sl@0: 		// Try to ensure the region doesn't keep an error forever.
sl@0: 		iUpdateRegion.Clear();
sl@0: 		}
sl@0: 
sl@0: 	TRect bounds(iUpdateRegion.BoundingRect());
sl@0: 	iUpdateRegion.AddRect(aRect);
sl@0: 
sl@0: 	// If the region fills up, start again with the old bounding box plus this
sl@0: 	// rectangle.
sl@0: 	if (iUpdateRegion.CheckError())
sl@0: 		{
sl@0: 		iUpdateRegion.Clear();
sl@0: 		iUpdateRegion.AddRect(bounds);
sl@0: 		iUpdateRegion.AddRect(aRect);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Reset the update region to be empty without submitting any outstanding updates.
sl@0: */
sl@0: void CScreenDeviceHelper::ResetUpdateRegion()
sl@0: 	{
sl@0: 	iUpdateRegion.Clear();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: This function returns the current surface in use for this screen.
sl@0: @param aSurface	The identifier to be updated with the current screen surface.
sl@0: */
sl@0: void CScreenDeviceHelper::GetSurface(TSurfaceId& aSid) const
sl@0: 	{
sl@0: 	aSid = iSurface;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: This function is used to request the device orientations supported by the
sl@0: screen device.
sl@0: @return A bitwise combination of one or more TDeviceOrientation enumerated
sl@0: values indicating the device orientations that are supported by this device.
sl@0: */
sl@0: TUint CScreenDeviceHelper::DeviceOrientationsAvailable(const TSize& aScreenSize) const
sl@0: 	{
sl@0: 	//All that can be reported here is what the CScreenDevice can support via HAL
sl@0: 	//With generic driver, the runtime can be further restricted by the GCE
sl@0: 	if (	aScreenSize.iWidth	&&	aScreenSize.iWidth==aScreenSize.iHeight	)
sl@0: 		{
sl@0: 		return EDeviceOrientationNormal | EDeviceOrientation90CW |
sl@0: 			EDeviceOrientation180 | EDeviceOrientation270CW;
sl@0: 		}
sl@0: 	//Query base HAL for rotated view support
sl@0: 	TInt	offset1=iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField]|TSurfaceId::TScreenSurfaceUsage::EHalFlippedFlag;
sl@0: 	if (	HAL::Get(ScreenNumber(), HALData::EDisplayOffsetBetweenLines, offset1)==KErrNone
sl@0: 		&&	offset1!=0)
sl@0: 		{
sl@0: 		return EDeviceOrientationNormal | EDeviceOrientation90CW |
sl@0: 			EDeviceOrientation180 | EDeviceOrientation270CW;
sl@0: 		}
sl@0: 	else
sl@0: 		return EDeviceOrientationNormal | EDeviceOrientation180;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: This function selects the surface and device buffer to use in the screen
sl@0: driver for this screen. Normal and 180° rotations will generally use the same
sl@0: surface, while 90° and 270° will use another. The surfaces may have different
sl@0: width, height, stride and surface ID, so functions that make use of any of
sl@0: these may be affected after a change in surface orientation, and the return
sl@0: value should be checked for this reason.
sl@0: 
sl@0: This call does not change the way rendering is performed, but may operate on
sl@0: the underlying memory using a new shape. The call does not change the display
sl@0: controller's settings, as this is handled via the GCE. All this changes are the
sl@0: internal attributes of the screen device and driver objects. A CFbsBitGc object
sl@0: activated on the device should be reactivated, to update its own attributes, or
sl@0: drawing may be corrupted.
sl@0: 
sl@0: Note: while TDeviceOrientation values do not directly correspond to 
sl@0: CFbsBitGc::TGraphicsOrientation values, and cannot be used interchangeably, it
sl@0: is simple to generate the former from the latter using the left-shift operator
sl@0: (i.e. device == (1 &lt;&lt; graphics)). In particular a device orientation of
sl@0: 90 degrees clockwise is equivalent to a content orientation of 90 degrees anti-
sl@0: clockwise, which is what TGraphicsOrientation refers to for the equivalent
sl@0: setting. The letters &quot;CW&quot; in the TDeviceOrientation enumeration refer
sl@0: to a clockwise device rotation, so EDeviceOrientation90CW is a 90 degree
sl@0: clockwise rotation of the device.
sl@0: 
sl@0: @param aOrientation	The new device orientation, relative to the normal physical
sl@0: screen orientation.
sl@0: @param aNewSize The new pixel dimensions of the surface to be used.
sl@0: @return ETrue is returned if any of the surface, width, height or stride
sl@0: attributes of the screen device have changed as a result of the call or EFalse
sl@0: if none of the attributes have changed.
sl@0: */
sl@0: TBool CScreenDeviceHelper::SetDeviceOrientation(TDeviceOrientation aOrientation, TSize& aNewSize)
sl@0: 	{
sl@0: 	// Check only one orientation bit is set
sl@0: 	if (((TInt)aOrientation - 1) & aOrientation)
sl@0: 		{
sl@0: 		Panic(EScreenDriverPanicInvalidParameter);
sl@0: 		}
sl@0: 
sl@0: 	// Check the orientation is supported
sl@0: 	if ((DeviceOrientationsAvailable(aNewSize) & aOrientation) == 0)
sl@0: 		{
sl@0: 		Panic(EScreenDriverPanicInvalidParameter);
sl@0: 		}
sl@0: 
sl@0: 	iAssignedOrientation=aOrientation;
sl@0: 	
sl@0: 	return SetDeviceFlipMode(ConvertFlip(aOrientation),aNewSize);
sl@0: 	}
sl@0: /** Sets or clears the flipped flag indicating that the width and height have been swapped for a +/-90 deg rotation
sl@0:  *  Rotation is not required for square displays unless the Hal wants one.
sl@0:  **/
sl@0: TBool CScreenDeviceHelper::SetDeviceFlipMode(TBool aFlip, TSize& aNewSize)
sl@0: 	{
sl@0: 	//This is now a private method that doesn't validate aFlip
sl@0: 	TInt newFlipMode= iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField];
sl@0: 	if (aFlip)
sl@0: 		{
sl@0: 		newFlipMode|=TSurfaceId::TScreenSurfaceUsage::EHalFlippedFlag;
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		newFlipMode&=~TSurfaceId::TScreenSurfaceUsage::EHalFlippedFlag;
sl@0: 		}
sl@0: 	if (newFlipMode == iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField])
sl@0: 		{
sl@0: 		// No change to mode requested.
sl@0: 		return EFalse;
sl@0: 		}
sl@0: 	TInt err=0;
sl@0: 	err|=HAL::Get(ScreenNumber(), HALData::EDisplayXPixels, aNewSize.iWidth);
sl@0: 	err|=HAL::Get(ScreenNumber(), HALData::EDisplayYPixels, aNewSize.iHeight);
sl@0: 	__ASSERT_ALWAYS(err==KErrNone,Panic(EScreenDriverPanicInvalidHalValue));
sl@0: 	 if (aNewSize.iWidth==aNewSize.iHeight)
sl@0: 		{	//Attempt optimisation to not flip if the screen is square, so avoid recomposition.
sl@0: 		TInt	stride1=iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField];
sl@0: 		TInt	stride2=stride1^TSurfaceId::TScreenSurfaceUsage::EHalFlippedFlag;
sl@0: 		TInt	offset1=stride1;
sl@0: 		TInt	offset2=stride2;
sl@0: 		//Does the rotated mode have any other attributes that differ?
sl@0: 		//It is just about possible to imagine that the rotated display 
sl@0: 		//wants to use a different setting for the flipped legacy buffer for optimisation purposes.
sl@0: 		err|=HAL::Get(ScreenNumber(), HALData::EDisplayOffsetToFirstPixel, offset1);
sl@0: 		err|=HAL::Get(ScreenNumber(), HALData::EDisplayOffsetBetweenLines, stride1);
sl@0: 		//The existing mode settings should not fail... we are already in this mode!
sl@0: 		__ASSERT_ALWAYS(err==KErrNone,Panic(EScreenDriverPanicInvalidHalValue));
sl@0: 		
sl@0: 		TInt rotatedErr =	HAL::Get(ScreenNumber(), HALData::EDisplayOffsetToFirstPixel, offset2);
sl@0: 		rotatedErr |=		HAL::Get(ScreenNumber(), HALData::EDisplayOffsetBetweenLines, stride2);
sl@0: 		//The HAL may indicate rotation is not required by failing to return data or returning the same data
sl@0: 		if ( rotatedErr!=KErrNone || stride2==0 ) //Offset can legitimately be zero.
sl@0: 			{	
sl@0: 			// No change to mode supported.
sl@0: 			return EFalse;
sl@0: 			}
sl@0: 		if ( stride1==stride2 && offset1==offset2 )
sl@0: 			{
sl@0: 			// No change to mode needed.
sl@0: 			return EFalse;
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: 	iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField] = newFlipMode;
sl@0: 	if (aFlip)
sl@0: 		{
sl@0: 		// Swap width and height in the alternate orientation.
sl@0: 		aNewSize.SetSize(aNewSize.iHeight, aNewSize.iWidth);
sl@0: 		}
sl@0: 	return ETrue;
sl@0: 	}
sl@0: /**	Returns the stride for the given mode.
sl@0:  *  This method must not panic if it should fail!
sl@0:  **/
sl@0: TUint CScreenDeviceHelper::BytesPerScanline() const
sl@0: 	{
sl@0: 	TInt linepitchInBytes = iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField];
sl@0: 	TInt ret = HAL::Get(ScreenNumber(),HALData::EDisplayOffsetBetweenLines,linepitchInBytes);
sl@0: 	if (ret!=KErrNone)
sl@0: 		{
sl@0: 		return 0;
sl@0: 		}
sl@0: 	return linepitchInBytes ;
sl@0: 	}
sl@0: /** Returns the address for the image data
sl@0:  *  This method must not panic if it should fail!
sl@0:  **/
sl@0: void* CScreenDeviceHelper::AddressFirstPixel() const
sl@0: 	{
sl@0: 	TInt bufferStartAddress = iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField];
sl@0: 	TInt ret = KErrNone;
sl@0: 	if (iHasChunk)
sl@0: 		{
sl@0: 		// The "chunk" way to do this is to get the handle of the chunk, and then the base address of the 
sl@0: 		// chunk itself. 
sl@0: 		bufferStartAddress = (TInt)iChunk.Base();
sl@0: 		}	 
sl@0: 	else 
sl@0: 		{
sl@0: 		// Chunk not supported, use older HAL call to get the buffer address. 
sl@0: 		ret = HAL::Get(ScreenNumber(),HALData::EDisplayMemoryAddress,bufferStartAddress);	
sl@0: 		if (ret!=KErrNone)
sl@0: 			{
sl@0: 			return 0;
sl@0: 			}
sl@0: 		}
sl@0: 	TInt bufferOffsetFirstPixel = iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField];
sl@0: 	ret = HAL::Get(ScreenNumber(),HALData::EDisplayOffsetToFirstPixel,bufferOffsetFirstPixel);
sl@0: 	if (ret!=KErrNone)
sl@0: 		{
sl@0: 		return 0;
sl@0: 		}
sl@0: 	return (void*)(bufferStartAddress+bufferOffsetFirstPixel);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns the current device width/height flip state of this surface, representing a +/-90 deg rotation.
sl@0: **/
sl@0: TBool	CScreenDeviceHelper::DeviceFlipped() const
sl@0: 	{
sl@0: 	return  (iSurface.iInternal[TSurfaceId::TScreenSurfaceUsage::EHalField] & TSurfaceId::TScreenSurfaceUsage::EHalFlippedFlag) != 0;	//!=0 forces true --> 1
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Returns the current device orientation.
sl@0: */
sl@0: TDeviceOrientation CScreenDeviceHelper::DeviceOrientation() const
sl@0: 	{
sl@0: 	return iAssignedOrientation;
sl@0: 	}
sl@0: /**	Returns an accurate scaling factor between twips and pixels in width.
sl@0: 
sl@0:  **/
sl@0: TInt	CScreenDeviceHelper::HorzTwipsPerThousandPixels(const TSize& aPixels)const
sl@0: 	{
sl@0: 	__ASSERT_DEBUG(aPixels.iWidth, Panic(EScreenDriverPanicInvalidSize));
sl@0: 	
sl@0: 	TInt width = 0;
sl@0: 	TInt r = HAL::Get(ScreenNumber(), SecondIfFlipped(HAL::EDisplayXTwips,HAL::EDisplayYTwips), width);
sl@0: 	__ASSERT_DEBUG(r==KErrNone && width!=0, Panic(EScreenDriverPanicInvalidHalValue));
sl@0:    
sl@0: 	return (width * 1000) / aPixels.iWidth;
sl@0: 	}
sl@0: /**	Returns an accurate scaling factor between twips and pixels in height.
sl@0:  **/
sl@0: TInt	CScreenDeviceHelper::VertTwipsPerThousandPixels(const TSize& aPixels)const
sl@0: 	{
sl@0: 	__ASSERT_DEBUG(aPixels.iHeight, Panic(EScreenDriverPanicInvalidSize));
sl@0: 
sl@0: 	TInt height = 0;
sl@0: 	TInt r = HAL::Get(ScreenNumber(), SecondIfFlipped(HAL::EDisplayYTwips,HAL::EDisplayXTwips), height);
sl@0: 	__ASSERT_DEBUG(r==KErrNone && height!=0, Panic(EScreenDriverPanicInvalidHalValue));
sl@0: 
sl@0: 	return (height * 1000) / aPixels.iHeight;
sl@0: 	} 
sl@0: 
sl@0: