camerauis/cameraxui/cxengine/src/cxeviewfindercontrolsymbian.cpp
branchRCL_3
changeset 54 bac7acad7cb3
parent 53 61bc0f252b2b
child 57 2c87b2808fd7
equal deleted inserted replaced
53:61bc0f252b2b 54:bac7acad7cb3
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include <w32std.h> // RWindow, RWsSession.
       
    18 #include <coemain.h> // CCoeEnv
       
    19 #include <coecntrl.h>
       
    20 #include <ecam/ecamdirectviewfinder.h>
       
    21 #include "cxutils.h"
       
    22 #include "cxecameradevicecontrolsymbian.h" // CxeCameraDevice
       
    23 #include "cxeviewfindercontrolsymbian.h"
       
    24 #include "cxesettings.h"
       
    25 #include "cxesettingsmappersymbian.h"
       
    26 #include "cxeerrormappingsymbian.h"
       
    27 #include "cxestate.h"
       
    28 #include "cxevideocontainer.h"
       
    29 #include "OstTraceDefinitions.h"
       
    30 #ifdef OST_TRACE_COMPILER_IN_USE
       
    31 #include "cxeviewfindercontrolsymbianTraces.h"
       
    32 #endif
       
    33 
       
    34 
       
    35 
       
    36 /**
       
    37 * CxeViewfinderControlSymbian::CxeViewfinderControlSymbian
       
    38 */
       
    39 CxeViewfinderControlSymbian::CxeViewfinderControlSymbian( CxeCameraDevice &cameraDevice,
       
    40         CxeCameraDeviceControl &cameraDeviceControl )
       
    41     : CxeStateMachine("CxeViewfinderControlSymbian"),
       
    42       mCameraDevice( cameraDevice ),
       
    43       mCameraDeviceControl( cameraDeviceControl ),
       
    44       mUiWindow(NULL),
       
    45       mVideoWindow(NULL),
       
    46       mVideoContainer(NULL),
       
    47       mDirectViewfinder( NULL ),
       
    48       mDirectViewfinderInUse( true )
       
    49 {
       
    50     CX_DEBUG_ENTER_FUNCTION();
       
    51 
       
    52     qRegisterMetaType<CxeViewfinderControlSymbian::State>();
       
    53     initializeStates();
       
    54 
       
    55     // connect signals from cameraDevice, so we recieve events when camera reference changes
       
    56     connect(&mCameraDevice, SIGNAL(prepareForCameraDelete()),
       
    57             this,SLOT(prepareForCameraDelete()));
       
    58 
       
    59     connect(&mCameraDevice, SIGNAL(prepareForRelease()),
       
    60             this,SLOT(prepareForRelease()));
       
    61 
       
    62     connect(&mCameraDevice, SIGNAL(cameraAllocated(CxeError::Id)),
       
    63             this,SLOT(handleCameraAllocated(CxeError::Id)));
       
    64 
       
    65     CX_DEBUG_EXIT_FUNCTION();
       
    66 }
       
    67 
       
    68 
       
    69 
       
    70 /**
       
    71 * CxeViewfinderControlSymbian::~CxeViewfinderControlSymbian()
       
    72 */
       
    73 CxeViewfinderControlSymbian::~CxeViewfinderControlSymbian()
       
    74 {
       
    75     CX_DEBUG_ENTER_FUNCTION();
       
    76 
       
    77     // do something
       
    78     stop(); // stop the vf
       
    79     releaseCurrentViewfinder(); // release resoruces
       
    80 
       
    81     delete mVideoContainer;
       
    82     mUiWindow = NULL;
       
    83     mVideoWindow = NULL;
       
    84 
       
    85     CX_DEBUG_EXIT_FUNCTION();
       
    86 }
       
    87 
       
    88 
       
    89 
       
    90 
       
    91 /**
       
    92 * CxeViewfinderControlSymbian::setWindow
       
    93 */
       
    94 void CxeViewfinderControlSymbian::setWindow(WId windowId)
       
    95 {
       
    96     CX_DEBUG_ENTER_FUNCTION();
       
    97 
       
    98     mUiWindow = static_cast<CCoeControl*>(windowId)->DrawableWindow();
       
    99 
       
   100     CX_DEBUG(("mUiWindow is 0x%08x", mUiWindow));
       
   101     TSize windowSize = mUiWindow->Size();
       
   102 
       
   103     CX_DEBUG(("mUiWindow size %dx%d", windowSize.iWidth, windowSize.iHeight));
       
   104 
       
   105     // Set the window rect
       
   106     TPoint point = TPoint(0,0);
       
   107     mWindowRect = TRect(point, windowSize);
       
   108 
       
   109     CX_DEBUG(("CxeViewfinderControlSymbian::setWindow() mWindowRect iTl=(%d, %d) iBr=(%d, %d)",
       
   110               mWindowRect.iTl.iX, mWindowRect.iTl.iY, mWindowRect.iBr.iX, mWindowRect.iBr.iY));
       
   111 
       
   112     CX_DEBUG_EXIT_FUNCTION();
       
   113 }
       
   114 
       
   115 
       
   116 /*!
       
   117 * Returns Device's Display resolution
       
   118 */
       
   119 QSize CxeViewfinderControlSymbian::deviceDisplayResolution() const
       
   120 {
       
   121     TSize windowSize(640, 360); // magic: default size if mUiWindow is NULL
       
   122 
       
   123     if (mUiWindow) {
       
   124         windowSize = mUiWindow->Size();
       
   125     }
       
   126 
       
   127     QSize displaySize = QSize(windowSize.iWidth, windowSize.iHeight);
       
   128 
       
   129     if (displaySize.height() > displaySize.width()) {
       
   130         // Window server orientation might differ from the Orbit UI orientation.
       
   131         // Swap width and height if needed.
       
   132         displaySize.transpose();
       
   133     }
       
   134 
       
   135     CX_DEBUG(("deviceDisplayResolution returning %dx%d", displaySize.width(),
       
   136                                                          displaySize.height()));
       
   137 
       
   138     return displaySize;
       
   139 }
       
   140 
       
   141 
       
   142 /**
       
   143 * Stop viewfinder
       
   144 */
       
   145 void CxeViewfinderControlSymbian::stop()
       
   146 {
       
   147     CX_DEBUG_ENTER_FUNCTION();
       
   148 
       
   149     if (state() == Running) {
       
   150         CX_DEBUG_ASSERT( mCameraDevice.camera() );
       
   151         CX_DEBUG( ( "Viewfinder is running stopping it" ) );
       
   152         mCameraDevice.camera()->StopViewFinder();
       
   153         setState( Ready );
       
   154     }
       
   155 
       
   156     CX_DEBUG_EXIT_FUNCTION();
       
   157 }
       
   158 
       
   159 
       
   160 /**!
       
   161 * Start the viewfinder
       
   162 */
       
   163 CxeError::Id CxeViewfinderControlSymbian::start()
       
   164 {
       
   165     CX_DEBUG_ENTER_FUNCTION();
       
   166     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_START_1, "msg: e_CX_VIEWFINDER_START 1");
       
   167 
       
   168     TInt err = KErrNone;
       
   169 
       
   170     if ( state() == Running ) {
       
   171         CX_DEBUG( ( "Viewfinder already running - ignored start()" ) );
       
   172         CX_DEBUG_EXIT_FUNCTION();
       
   173         return CxeError::None;
       
   174     }
       
   175 
       
   176     if (!mVideoWindow) {
       
   177         TRAP(err, createViewfinderWindowL());
       
   178         if (err != KErrNone) {
       
   179             CX_DEBUG_EXIT_FUNCTION();
       
   180             return CxeErrorHandlingSymbian::map(err);
       
   181         }
       
   182     }
       
   183 
       
   184     if (state() == Uninitialized) {
       
   185         err = initViewfinder();
       
   186     }
       
   187 
       
   188     // apply any settings here
       
   189     if ( mDirectViewfinderInUse && !err && mDirectViewfinder && state() == Ready ) {
       
   190 
       
   191         // for now only direct vf
       
   192         switch( mDirectViewfinder->ViewFinderState() ) {
       
   193         case CCamera::CCameraDirectViewFinder::EViewFinderInActive:
       
   194             {
       
   195             CX_DEBUG(("Calling StartViewFinderDirectL"));
       
   196 
       
   197             // Make UI surface transparent so viewfinder is visible
       
   198             if (mUiWindow) {
       
   199                 mUiWindow->SetSurfaceTransparency(ETrue);
       
   200             }
       
   201 
       
   202             // A local copy of the viewfinder coordinates (in WServ
       
   203             // coordinates). Needed because calling StartViewFinderDirectL()
       
   204             // will modify the rectangle and we don't want mWindowRect to be
       
   205             // modified.
       
   206             TRect activeViewfinderRect = mWindowRect;
       
   207 
       
   208             CX_DEBUG(("activeViewfinderRect iTl=(%d, %d) iBr=(%d, %d)",
       
   209                 activeViewfinderRect.iTl.iX, activeViewfinderRect.iTl.iY,
       
   210                 activeViewfinderRect.iBr.iX, activeViewfinderRect.iBr.iY));
       
   211 
       
   212             TRAP(err, mCameraDevice.camera()->StartViewFinderDirectL(
       
   213                 CCoeEnv::Static()->WsSession(),
       
   214                 *CCoeEnv::Static()->ScreenDevice(),
       
   215                 *mVideoWindow, activeViewfinderRect));
       
   216             OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_STARTUP, "msg: e_CX_STARTUP 0");
       
   217             setState(Running);
       
   218             break;
       
   219             }
       
   220         case CCamera::CCameraDirectViewFinder::EViewFinderPause:
       
   221             TRAP( err, mDirectViewfinder->ResumeViewFinderDirectL() );
       
   222             if (!err) {
       
   223                 setState( Running );
       
   224             }
       
   225             break;
       
   226         case CCamera::CCameraDirectViewFinder::EViewFinderActive:
       
   227             // Already running. Not considered as error.
       
   228             setState( Running );
       
   229             break;
       
   230         default:
       
   231             err = KErrNotSupported;
       
   232         }
       
   233     }
       
   234     else if ( !mDirectViewfinder ) {
       
   235         // Start bmp vf
       
   236         //TSize size(320, 240);
       
   237         TSize size(mWindowRect.Width(), mWindowRect.Height());
       
   238         CX_DEBUG(("Starting bitmap vf with size %d x %d", size.iWidth, size.iHeight));
       
   239         TRAP( err, mCameraDevice.camera()->StartViewFinderBitmapsL( size ) );
       
   240         CX_DEBUG(("Bitmap viewfinder modified to size %d x %d", size.iWidth, size.iHeight));
       
   241         if ( err ) {
       
   242             CX_DEBUG( ("StartViewfinderBitmapsL - err:%d", err) );
       
   243         }
       
   244         else {
       
   245             setState( Running );
       
   246             TRAP( err, mCameraDevice.camera()->SetViewFinderMirrorL(true) );
       
   247         }
       
   248     }
       
   249     else {
       
   250         // vf already running. nothing to do
       
   251     }
       
   252 
       
   253     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_START_2, "msg: e_CX_VIEWFINDER_START 0");
       
   254     CX_DEBUG_EXIT_FUNCTION();
       
   255     return CxeErrorHandlingSymbian::map(err);
       
   256 }
       
   257 
       
   258 
       
   259 /**
       
   260 * Intialize the viewfinder based on the VF mode
       
   261 */
       
   262 int CxeViewfinderControlSymbian::initViewfinder()
       
   263 {
       
   264     CX_DEBUG_ENTER_FUNCTION();
       
   265     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_INIT_1, "msg: e_CX_VIEWFINDER_INIT 1");
       
   266 
       
   267     TInt err = KErrNone;
       
   268     if (state() != Uninitialized) {
       
   269         return err;
       
   270     }
       
   271 
       
   272     // For now only direct vf. If there is any need for supporting other VF modes, condition checks
       
   273     // and handling of new states are needed here.
       
   274 
       
   275     // release first, the prev/current view finder instance.
       
   276     releaseCurrentViewfinder();
       
   277 
       
   278     mDirectViewfinderInUse = true;
       
   279     /*
       
   280     if ( mCameraDeviceControl.cameraIndex() == Cxe::PrimaryCameraIndex ) {
       
   281         mDirectViewfinderInUse = true;
       
   282     }
       
   283     else {
       
   284         mDirectViewfinderInUse = false;
       
   285     }*/
       
   286 
       
   287     if ( mDirectViewfinderInUse ) {
       
   288         // creating an new instance of vf
       
   289         TRAP( err, mDirectViewfinder = CCamera::CCameraDirectViewFinder::NewL( *mCameraDevice.camera() ) );
       
   290     }
       
   291 
       
   292     if ( !err ) {
       
   293         setState( Ready );
       
   294     }
       
   295     else {
       
   296         CX_DEBUG( ("error: %d", err ) );
       
   297     }
       
   298 
       
   299 #if defined(CXE_USE_DUMMY_CAMERA) || defined(__WINSCW__)
       
   300     //! @todo Temporary code for WINSCW, because CCamera::CCameraDirectViewFinder is not yet supported by dummy engine
       
   301     setState( Ready );
       
   302 #endif
       
   303 
       
   304     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_INIT_2, "msg: e_CX_VIEWFINDER_INIT 0");
       
   305     CX_DEBUG_EXIT_FUNCTION();
       
   306     return err;
       
   307 }
       
   308 
       
   309 
       
   310 /**
       
   311 * Create the window for viewfinder to render into.
       
   312 */
       
   313 void CxeViewfinderControlSymbian::createViewfinderWindowL()
       
   314 {
       
   315     CX_DEBUG_ENTER_FUNCTION();
       
   316     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_CREATE_WINDOW_1, "msg: e_CX_VIEWFINDER_CREATE_WINDOW 1");
       
   317 
       
   318     if (!mUiWindow) {
       
   319         CX_DEBUG( ( "mUiWindow not set - cannot create VF window!" ) );
       
   320         User::Leave(KErrNotReady);
       
   321     }
       
   322 
       
   323     // Make UI surface transparent so viewfinder is visible
       
   324     if (mUiWindow) {
       
   325         mUiWindow->SetSurfaceTransparency(ETrue);
       
   326     }
       
   327 
       
   328     delete mVideoContainer;
       
   329     mVideoContainer = NULL;
       
   330     mVideoContainer = new (ELeave) CxeVideoContainer();
       
   331     mVideoContainer->ConstructL();
       
   332     CX_DEBUG(("Viewfinder container created ok"));
       
   333 
       
   334     mVideoContainer->SetRect(mWindowRect);
       
   335     mVideoWindow = mVideoContainer->DrawableWindow();
       
   336     CX_DEBUG(("mVideoWindow is 0x%08x", mVideoWindow));
       
   337     CX_DEBUG(("mUiWindow ordinal position is: %d", mUiWindow->OrdinalPosition()));
       
   338 
       
   339     // Make sure UI window is on top of viewfinder
       
   340     CX_DEBUG(("Set viewfinder window ordinal.."));
       
   341     mVideoWindow->SetOrdinalPosition(-1);
       
   342     mUiWindow->SetOrdinalPosition(0);
       
   343 
       
   344     CX_DEBUG(("mVideoWindow ordinal position is: %d", mVideoWindow->OrdinalPosition()));
       
   345     CX_DEBUG(("mUiWindow ordinal position is: %d", mUiWindow->OrdinalPosition()));
       
   346 
       
   347     OstTrace0(camerax_performance, CXEVIEWFINDERCONTROL_CREATE_WINDOW_2, "msg: e_CX_VIEWFINDER_CREATE_WINDOW 0");
       
   348     CX_DEBUG_EXIT_FUNCTION();
       
   349 }
       
   350 
       
   351 
       
   352 void CxeViewfinderControlSymbian::prepareForRelease()
       
   353 {
       
   354     CX_DEBUG_ENTER_FUNCTION();
       
   355     stop();
       
   356     CX_DEBUG_EXIT_FUNCTION();
       
   357 }
       
   358 
       
   359 /**
       
   360 * Release Vf resources
       
   361 */
       
   362 void CxeViewfinderControlSymbian::releaseCurrentViewfinder()
       
   363 {
       
   364     CX_DEBUG_ENTER_FUNCTION();
       
   365 
       
   366     delete mDirectViewfinder;
       
   367     mDirectViewfinder = NULL;
       
   368     setState( Uninitialized );
       
   369 
       
   370     CX_DEBUG_EXIT_FUNCTION();
       
   371 }
       
   372 
       
   373 /**
       
   374 * Ecam reference changing, release resources
       
   375 */
       
   376 void CxeViewfinderControlSymbian::prepareForCameraDelete()
       
   377 {
       
   378     CX_DEBUG_ENTER_FUNCTION();
       
   379     stop(); // first stop the viewfinder
       
   380     releaseCurrentViewfinder();
       
   381     CX_DEBUG_EXIT_FUNCTION();
       
   382 }
       
   383 
       
   384 
       
   385 /**
       
   386 * Handle new camera instance.
       
   387 */
       
   388 void CxeViewfinderControlSymbian::handleCameraAllocated(CxeError::Id error)
       
   389 {
       
   390     CX_DEBUG_ENTER_FUNCTION();
       
   391     if (!error) {
       
   392         initViewfinder();
       
   393     }
       
   394     CX_DEBUG_EXIT_FUNCTION();
       
   395 }
       
   396 
       
   397 CxeViewfinderControl::State CxeViewfinderControlSymbian::state() const
       
   398 {
       
   399     return static_cast<State>( stateId() );
       
   400 }
       
   401 
       
   402 void CxeViewfinderControlSymbian::handleStateChanged( int newStateId, CxeError::Id error )
       
   403 {
       
   404     emit stateChanged( static_cast<State>( newStateId ), error );
       
   405 }
       
   406 
       
   407 void CxeViewfinderControlSymbian::initializeStates()
       
   408 {
       
   409     // addState( id, name, allowed next states )
       
   410     addState( new CxeState( Uninitialized , "Uninitialized", Ready ) );
       
   411     addState( new CxeState( Ready , "Ready", Uninitialized | Running ) );
       
   412     addState( new CxeState( Running , "Running", Uninitialized | Ready ) );
       
   413 
       
   414     setInitialState( Uninitialized );
       
   415 }
       
   416 
       
   417 void CxeViewfinderControlSymbian::handleVfFrame(MCameraBuffer* /*buffer*/, int /*error*/)
       
   418 {
       
   419     CX_DEBUG_IN_FUNCTION();
       
   420     CX_DEBUG( ( "Bitmap viewfinder not supported" ) );
       
   421     CX_ASSERT_ALWAYS(false);
       
   422 }
       
   423 
       
   424 // end of file