os/mm/mmplugins/cameraplugins/source/testcamera/test_snapshot.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 // testsnapshot.cpp
    15 // 
    16 //
    17 
    18 #include "test_snapshot.h"
    19 #include <ecamerrors.h>
    20 
    21 CTestCamSnapshot::CTestCamSnapshot(CTestCamera& aOwner): iOwner(aOwner), iSnapshotBuffer(iSnapshot)
    22 	{
    23 	Init();
    24 	}
    25 	
    26 CTestCamSnapshot::~CTestCamSnapshot()
    27 	{
    28 	delete iSnapshotImage;
    29 	iSnapshotImage = NULL;
    30 	delete iSnapshotImageGc;
    31 	iSnapshotImage = NULL;
    32 	delete iSnapshotImageDev;
    33 	iSnapshotImageDev = NULL;
    34 	
    35 	delete iSnapshot;
    36 	iSnapshot = NULL;
    37 	delete iSnapshotGc;
    38 	iSnapshotGc = NULL;
    39 	delete iSnapshotDev;
    40 	iSnapshotDev = NULL;
    41 	
    42 	iSupportedSnapshotRect.Close();
    43 	
    44 	CDataGlobal* globalData = static_cast <CDataGlobal*> (Dll::Tls());
    45 	if(globalData != NULL)
    46 		{
    47 		if(!globalData->iTestCamPresets && !globalData->iTestCamAdvSet && !globalData->iTestCamImgProc)
    48 			{
    49 			delete globalData;
    50 			Dll::FreeTls();
    51 			}	
    52 		else
    53 			{
    54 			globalData->iTestCamSnapshot = NULL;	
    55 			Dll::SetTls(globalData);
    56 			}
    57 		}
    58 	}
    59 		
    60 CTestCamSnapshot* CTestCamSnapshot::NewL(CTestCamera& aOwner)
    61 	{
    62 	CDataGlobal* globalData = static_cast <CDataGlobal*> (Dll::Tls());
    63 	
    64 	if(globalData == NULL)
    65 		{
    66 		globalData = new (ELeave) CDataGlobal;
    67 		CleanupStack::PushL(globalData);
    68 		globalData->iSnapshotReferenceCount = 0;
    69 		globalData->iTestCamSnapshot = new (ELeave) CTestCamSnapshot(aOwner);
    70 		CleanupStack::PushL(globalData->iTestCamSnapshot);
    71 		globalData->iTestCamSnapshot->ConstructL();
    72 		globalData->iTestCamSnapshot->iRefCount = 1;
    73 		User::LeaveIfError(Dll::SetTls(globalData));
    74 		CleanupStack::Pop(globalData->iTestCamSnapshot);
    75 		CleanupStack::Pop(globalData);
    76 		return globalData->iTestCamSnapshot;
    77 		}
    78 	else
    79 		{
    80 		if(globalData->iTestCamSnapshot == NULL)
    81 			{
    82 			globalData->iSnapshotReferenceCount = 0;
    83 			globalData->iTestCamSnapshot = new (ELeave) CTestCamSnapshot(aOwner);
    84 			CleanupStack::PushL(globalData->iTestCamSnapshot);
    85 			globalData->iTestCamSnapshot->ConstructL();
    86 			globalData->iTestCamSnapshot->iRefCount = 1;
    87 			User::LeaveIfError(Dll::SetTls(globalData));
    88 			CleanupStack::Pop(globalData->iTestCamSnapshot);
    89 			return globalData->iTestCamSnapshot;
    90 			}
    91 		
    92 		CTestCamSnapshot* self = globalData->iTestCamSnapshot;
    93 		
    94 		globalData->iSnapshotReferenceCount++;
    95 		self->iRefCount = globalData->iSnapshotReferenceCount + 1;
    96 		if (globalData->iSnapshotReferenceCount == KNumOfSnapshotExtensions-1 )
    97 			{
    98 			globalData->iTestCamSnapshot = NULL;
    99 			if(!globalData->iTestCamPresets && !globalData->iTestCamAdvSet && !globalData->iTestCamImgProc)
   100 				{
   101 				delete globalData;
   102 				Dll::FreeTls();
   103 				}
   104 			else
   105 				{
   106 				User::LeaveIfError(Dll::SetTls(globalData));	
   107 				}
   108 			}
   109 		else
   110 			{
   111 			User::LeaveIfError(Dll::SetTls(globalData));	
   112 			}
   113 		return self;		
   114 		}
   115 	}
   116 
   117 void CTestCamSnapshot::Release()
   118 	{
   119 	iRefCount--; 
   120 	if(iRefCount == 0)
   121 		{
   122 		iOwner.iSnapshotImpl = NULL;
   123 		delete this;
   124 		}
   125 	}	
   126 	
   127 void CTestCamSnapshot::Init()
   128 	{
   129 	iImageFormatsSupported = 0;
   130 	iImageFormatsSupported |= CCamera::EFormatFbsBitmapColor4K;
   131 	iImageFormatsSupported |= CCamera::EFormatFbsBitmapColor64K;
   132 	iImageFormatsSupported |= CCamera::EFormatFbsBitmapColor16M; 
   133 	iSnapshotPrepared = EFalse;
   134 	iSnapshotActive = EFalse;
   135 	iBgColor = KRgbWhite;
   136 	}
   137 
   138 void CTestCamSnapshot::ConstructL()
   139 	{
   140 	iSupportedSnapshotRect.Reset();
   141 	iSupportedSnapshotRect.AppendL(TRect(TPoint(0,0), iOwner.iImageSizes[0]));
   142 	iSupportedSnapshotRect.AppendL(TRect(TPoint(0,0), iOwner.iImageSizes[1]));
   143 	}
   144 
   145 TUint32 CTestCamSnapshot::SupportedFormats()
   146 	{
   147 	return iImageFormatsSupported;
   148 	}
   149 	
   150 void CTestCamSnapshot::PrepareSnapshotL(CCamera::TFormat aFormat, const TPoint& aPosition, const TSize& aSize, const TRgb& aBgColor, TBool /*aMaintainAspectRatio*/)
   151 	{
   152 	iOwner.CheckReserveAndPowerL();
   153 		
   154 	if (iOwner.iVideoCaptureActive || iOwner.iImageCaptureActive)
   155 		{
   156 		User::Leave(KErrNotReady);
   157 		}
   158 		
   159 	if (!(aFormat & iImageFormatsSupported) )
   160 		{
   161 		User::Leave(KErrNotSupported);
   162 		}
   163 	
   164 	//check if the snapshot size is supported
   165 	TRect snapshot_rect = TRect(aPosition, aSize);
   166 	
   167 	if(snapshot_rect != iSupportedSnapshotRect[0] && snapshot_rect != iSupportedSnapshotRect[1])
   168 		{
   169 		User::Leave(KErrNotSupported);
   170 		}
   171 
   172 	if (iSnapshotPrepared
   173 		&& iSnapshotFormat == aFormat
   174 		&& iSnapshotImageRect == snapshot_rect
   175 		&& iBgColor == aBgColor
   176 		)
   177 		{
   178 		return;
   179 		}
   180 
   181 	TDisplayMode displayMode = ENone;
   182 	switch (aFormat)
   183 		{
   184 		case CCamera::EFormatFbsBitmapColor4K :
   185 			displayMode = EColor4K;
   186 			break;
   187 		case CCamera::EFormatFbsBitmapColor64K :
   188 			displayMode = EColor64K;
   189 			break;
   190 		case CCamera::EFormatFbsBitmapColor16M :
   191 			displayMode = EColor16M;
   192 			break;
   193 		default :
   194 			User::Leave(KErrNotSupported);
   195 			break;
   196 		}
   197 
   198 	delete iSnapshotImage;
   199 	iSnapshotImage = NULL;
   200 	delete iSnapshotImageGc;
   201 	iSnapshotImageGc = NULL;
   202 	delete iSnapshotImageDev;
   203 	iSnapshotImageDev = NULL;
   204 
   205 	iSnapshotImage = new(ELeave) CFbsBitmap;
   206 	User::LeaveIfError(iSnapshotImage->Create(aSize, displayMode));
   207 	iSnapshotImageDev = CFbsBitmapDevice::NewL(iSnapshotImage);
   208 	User::LeaveIfError(iSnapshotImageDev->CreateContext(iSnapshotImageGc));
   209 	iSnapshotImageGc->SetPenColor(KRgbBlack);
   210 	iSnapshotImageGc->SetPenSize(TSize(KFrameFeatureBorderThickness, KFrameFeatureBorderThickness));
   211 	iSnapshotImageGc->SetBrushColor(aBgColor);
   212 	iSnapshotImageGc->SetFaded(ETrue);
   213 	
   214 	iSnapshotPrepared = ETrue;
   215 	iSnapshotImageRect = snapshot_rect;
   216 	iSnapshotFormat = aFormat;
   217 	iBgColor = aBgColor;
   218 	}
   219 	
   220 void CTestCamSnapshot::PrepareSnapshotL(CCamera::TFormat /*aFormat*/, const TSize& /*aSize*/, TBool /*aMaintainAspectRatio*/)
   221 	{
   222 	User::Leave(KErrNotSupported);
   223 	}
   224 	
   225 void CTestCamSnapshot::SetBgColorL(const TRgb& /*aBgColor*/)
   226 	{
   227 	User::Leave(KErrNotSupported);
   228 	}
   229 	
   230 void CTestCamSnapshot::SetPositionL(const TPoint& /*aPosition*/)
   231 	{
   232 	User::Leave(KErrNotSupported);
   233 	}
   234 	
   235 TBool CTestCamSnapshot::IsSnapshotActive() const
   236 	{
   237 	return iSnapshotActive;
   238 	}
   239 
   240 void CTestCamSnapshot::StartSnapshot()
   241 	{
   242 	TInt error = iOwner.CheckReserveAndPower();
   243 	
   244 	if(error == KErrNone)
   245 		{
   246 		if(!iSnapshotPrepared)
   247 			{
   248 			error = KErrNotReady;
   249 			}
   250 		}
   251 	
   252 	if(error != KErrNone)
   253 		{
   254 		TECAMEvent ecamevent(KUidECamEventCameraSnapshot, error);
   255 		
   256 		iOwner.iECamEvent = ecamevent;
   257 		iOwner.iHandleEventAsync.CallBack();	
   258 		return;	
   259 		}
   260 		
   261 	delete iSnapshot;
   262 	iSnapshot = NULL;
   263 	delete iSnapshotGc;
   264 	iSnapshot = NULL;
   265 	delete iSnapshotDev;
   266 	iSnapshotDev = NULL;
   267 	
   268 	TRAP(error, DoStartSnapshotL());
   269 	
   270 	if(error != KErrNone)
   271 		{
   272 		TECAMEvent ecamevent(KUidECamEventCameraSnapshot, error);
   273 		
   274 		iOwner.iECamEvent = ecamevent;
   275 		iOwner.iHandleEventAsync.CallBack();
   276 		}
   277 	}
   278 	
   279 void CTestCamSnapshot::DoStartSnapshotL()
   280 	{
   281 	CFbsBitmap* snapshot = new(ELeave) CFbsBitmap;
   282 	CleanupStack::PushL(snapshot);
   283 	User::LeaveIfError(snapshot->Create(iSnapshotImageRect.Size(), iSnapshotImage->DisplayMode()));
   284 	CFbsBitmapDevice* dev = CFbsBitmapDevice::NewL(snapshot);
   285 	CleanupStack::PushL(dev);
   286 	CFbsBitGc* gc = NULL;
   287 	User::LeaveIfError(dev->CreateContext(gc));
   288 	CleanupStack::Pop(dev);
   289 	CleanupStack::Pop(snapshot);
   290 	
   291 	iSnapshot = snapshot;
   292 	iSnapshotDev = dev;
   293 	iSnapshotGc = gc;
   294 		
   295 	iSnapshotActive = ETrue;
   296 	}
   297 
   298 void CTestCamSnapshot::StopSnapshot()
   299 	{
   300 	delete iSnapshotGc;
   301 	iSnapshotGc = NULL;
   302 	delete iSnapshotDev;
   303 	iSnapshotDev = NULL;
   304 	delete iSnapshot;
   305 	iSnapshot = NULL;
   306 	
   307 	iSnapshotActive = EFalse;
   308 	}
   309         	
   310 MCameraBuffer& CTestCamSnapshot::SnapshotDataL(RArray<TInt>& /*aFrameIndexOrder*/)
   311 	{
   312 	iSnapshotBuffer.iAvailable = EFalse;
   313 	return iSnapshotBuffer;
   314 	}
   315 	
   316 void CTestCamSnapshot::ServiceSnapshot(const CFbsBitGc& aSourceBitmapGc)
   317 	{
   318 	TInt error = KErrNone;
   319 	if(!iSnapshotBuffer.iAvailable)
   320 		{
   321 		error = KErrInUse;
   322 		}
   323 	else
   324 		{
   325 		iSnapshotGc->BitBlt(TPoint(0,0), aSourceBitmapGc);
   326 		}
   327 			
   328 	iOwner.iObserver2->HandleEvent(TECAMEvent(KUidECamEventCameraSnapshot, error));
   329 	}
   330 	
   331 void CTestCamSnapshot::ServiceBurstImageSnapshot(const CFbsBitGc& aSourceBitmapGc)
   332 	{
   333 	TInt error = KErrNone;
   334 	if(!iSnapshotBuffer.iAvailable)
   335 		{
   336 		error = KErrInUse;
   337 		}
   338 	else
   339 		{
   340 		iSnapshotGc->BitBlt(TPoint(0,0), aSourceBitmapGc);
   341 		}
   342 			
   343 	iOwner.iObserver2->HandleEvent(TECAMEvent(KUidECamEventCameraSnapshot, error));
   344 	
   345 	User::After(500000);
   346 	
   347 	iOwner.iStillImagePos.iX += KFramePosIncrement;
   348 	iOwner.iStillImagePos.iY += KFramePosIncrement;
   349 
   350 	if (iOwner.iStillImagePos.iX >= iOwner.iStillImageSize.iWidth)
   351 		{
   352 		iOwner.iStillImagePos.iX = 0;
   353 		}
   354 		
   355 	if (iOwner.iStillImagePos.iY >= iOwner.iStillImageSize.iHeight)
   356 		{
   357 		iOwner.iStillImagePos.iY = 0;
   358 		}
   359 	}
   360