camerauis/cameraxui/cxengine/src/cxeautofocuscontrolsymbian.cpp
changeset 19 d9aefe59d544
child 21 fa6d9f75d6a6
child 28 3075d9b614e6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     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 
       
    18 #include <QMetaType>
       
    19 #include <QImage>
       
    20 #include <fbs.h>
       
    21 #include <ecam.h> // CCamera
       
    22 #include <ecam/ecamadvsettingsintf.h> // CCamera
       
    23 #include <ecamadvsettings.h>
       
    24 
       
    25 #include "cxesettings.h"
       
    26 #include "cxeautofocuscontrolsymbian.h"
       
    27 #include "cxutils.h"
       
    28 #include "cxecameradevice.h" // CxeCameraDevice
       
    29 #include "cxesettingsmappersymbian.h"
       
    30 #include "cxeerrormappingsymbian.h"
       
    31 #include "cxestate.h"
       
    32 
       
    33 #include "OstTraceDefinitions.h"
       
    34 #ifdef OST_TRACE_COMPILER_IN_USE
       
    35 #include "cxeautofocuscontrolsymbianTraces.h"
       
    36 #endif
       
    37 
       
    38 
       
    39 
       
    40 /*
       
    41 * CxeAutoFocusControlSymbian::CxeAutoFocusControlSymbian
       
    42 */
       
    43 CxeAutoFocusControlSymbian::CxeAutoFocusControlSymbian(CxeCameraDevice &cameraDevice)
       
    44     : CxeStateMachine("CxeAutoFocusControlSymbian"),
       
    45       mCameraDevice(cameraDevice),
       
    46       mAdvancedSettings(NULL),
       
    47       mCancelled(false)
       
    48 {
       
    49     CX_DEBUG_ENTER_FUNCTION();
       
    50 
       
    51     qRegisterMetaType<CxeAutoFocusControl::State>();
       
    52 
       
    53     initializeStates();
       
    54 
       
    55     // connect signals from cameraDevice, so we recieve events when camera reference changes
       
    56     QObject::connect( &cameraDevice,
       
    57                       SIGNAL(prepareForCameraDelete()),
       
    58                       this,SLOT(prepareForCameraDelete()) );
       
    59 
       
    60     QObject::connect( &cameraDevice,
       
    61                       SIGNAL(cameraAllocated(CxeError::Id)),
       
    62                       this,SLOT(handleCameraAllocated(CxeError::Id)) );
       
    63 
       
    64     QObject::connect( &cameraDevice,
       
    65                       SIGNAL(prepareForRelease()),
       
    66                       this,SLOT(prepareForRelease()) );
       
    67 
       
    68     initializeResources();
       
    69 
       
    70     CX_DEBUG_EXIT_FUNCTION();
       
    71 }
       
    72 
       
    73 
       
    74 
       
    75 /*
       
    76 * CxeAutoFocusControlSymbian::~CxeAutoFocusControlSymbian
       
    77 */
       
    78 CxeAutoFocusControlSymbian::~CxeAutoFocusControlSymbian()
       
    79 {
       
    80     CX_DEBUG_ENTER_FUNCTION();
       
    81     CX_DEBUG_EXIT_FUNCTION();
       
    82 }
       
    83 
       
    84 
       
    85 /*
       
    86 * Start Autofocus
       
    87 */
       
    88 CxeError::Id CxeAutoFocusControlSymbian::start()
       
    89 {
       
    90     CX_DEBUG( ("CxeAutoFocusControlSymbian::start() <> state: %d", state() ) );
       
    91 
       
    92     int err = KErrNone;
       
    93 
       
    94     CX_ASSERT_ALWAYS(mAdvancedSettings);
       
    95 
       
    96     if ( state() != CxeAutoFocusControl::InProgress && state() != CxeAutoFocusControl::Canceling  ) {
       
    97         CX_DEBUG(("CxeAutoFocusControlSymbian::start() calling SetAutoFocusType"));
       
    98         mCancelled = false;
       
    99         setState(InProgress);
       
   100         setFocusRange(mAFRange);
       
   101         setFocusType(CCamera::CCameraAdvancedSettings::EAutoFocusTypeSingle);
       
   102     } else { // AF was started earlier, can't start until it completes
       
   103         err = KErrInUse;
       
   104     }
       
   105 
       
   106     CX_DEBUG( ("CxeAutoFocusControlSymbian::start() <= err : %d", err ) );
       
   107 
       
   108     return CxeErrorHandlingSymbian::map(err);
       
   109 }
       
   110 
       
   111 
       
   112 
       
   113 /*
       
   114 * Cancel Autofocus
       
   115 */
       
   116 void CxeAutoFocusControlSymbian::cancel()
       
   117 {
       
   118     CX_DEBUG( ("CxeAutoFocusControlSymbian::cancel <> state: %d", state() ) );
       
   119 
       
   120     CX_DEBUG_ASSERT(mAdvancedSettings);
       
   121 
       
   122     if (!mCancelled) {
       
   123         if (state() == CxeAutoFocusControl::InProgress) {
       
   124             // Need to stop current AF first. Wait for AF event to proceed.
       
   125             setState(CxeAutoFocusControl::Canceling);
       
   126             setFocusType(CCamera::CCameraAdvancedSettings::EAutoFocusTypeOff);
       
   127 
       
   128         } else if (state() != CxeAutoFocusControl::Canceling) {
       
   129             // Cancel means move to hyperfocal.
       
   130             setState(CxeAutoFocusControl::Canceling);
       
   131             CX_DEBUG(("CxeAutoFocusControlSymbian::cancel() moving to hyperfocal"));
       
   132             setFocusRange(CCamera::CCameraAdvancedSettings::EFocusRangeHyperfocal);
       
   133             setFocusType(CCamera::CCameraAdvancedSettings::EAutoFocusTypeSingle);
       
   134         }
       
   135     }
       
   136     CX_DEBUG_EXIT_FUNCTION();
       
   137 }
       
   138 
       
   139 
       
   140 
       
   141 /*
       
   142 * Set Autofocus mode
       
   143 */
       
   144 void CxeAutoFocusControlSymbian::setMode(CxeAutoFocusControl::Mode newMode)
       
   145 {
       
   146     CX_DEBUG_ENTER_FUNCTION();
       
   147 
       
   148     CX_DEBUG_ASSERT(mAdvancedSettings);
       
   149 
       
   150     mAfMode = newMode;
       
   151     mAFRange = CxeSettingsMapperSymbian::Map2CameraAutofocus(mAfMode);
       
   152 
       
   153     CX_DEBUG(("CxeAutoFocusControlSymbian::setMode() mAFRange: %d", mAFRange));
       
   154 
       
   155     mCancelled = false;
       
   156     setFocusRange(mAFRange);
       
   157 
       
   158     CX_DEBUG_EXIT_FUNCTION();
       
   159 }
       
   160 
       
   161 
       
   162 /*
       
   163 * returns Autofocus mode
       
   164 */
       
   165 CxeAutoFocusControl::Mode CxeAutoFocusControlSymbian::mode() const
       
   166 {
       
   167     return mAfMode;
       
   168 }
       
   169 
       
   170 
       
   171 /*
       
   172 * To check if Autofocus is supported
       
   173 */
       
   174 bool CxeAutoFocusControlSymbian::supported() const
       
   175 {
       
   176     CX_DEBUG_ENTER_FUNCTION();
       
   177 
       
   178     bool supported =
       
   179         (supportedFocusTypes() != CCamera::CCameraAdvancedSettings::EAutoFocusTypeOff);
       
   180 
       
   181     CX_DEBUG_EXIT_FUNCTION();
       
   182     return supported;
       
   183 }
       
   184 
       
   185 
       
   186 
       
   187 /*
       
   188 * Slot for handling ECam events
       
   189 */
       
   190 void CxeAutoFocusControlSymbian::handleCameraEvent(int eventUid, int error)
       
   191 {
       
   192     CX_DEBUG_ENTER_FUNCTION();
       
   193 
       
   194     CX_DEBUG( ("CxeAutoFocusControlSymbian::handleCameraEvent <> state: %d error %d", state(), error ) );
       
   195     CX_DEBUG( ("CxeAutoFocusControlSymbian::handleCameraEvent <> uid: %x optimalfocusuid: %x focustype2uid %x",
       
   196               eventUid,
       
   197               KUidECamEventCameraSettingsOptimalFocusUidValue,
       
   198               KUidECamEventCameraSettingAutoFocusType2UidValue ));
       
   199 
       
   200     // We're only interested in autofocus events
       
   201     if ( eventUid == KUidECamEventCameraSettingsOptimalFocusUidValue ||
       
   202          eventUid == KUidECamEventCameraSettingAutoFocusType2UidValue ) {
       
   203          // Autofocus Event handle it.
       
   204          handleAfEvent(eventUid, error);
       
   205     }
       
   206 
       
   207     CX_DEBUG_EXIT_FUNCTION();
       
   208 }
       
   209 
       
   210 
       
   211 
       
   212 
       
   213 void CxeAutoFocusControlSymbian::prepareForRelease()
       
   214 {
       
   215 
       
   216     CX_DEBUG_ENTER_FUNCTION();
       
   217 
       
   218     // camera is anyway released, so no need to cancel the AF anyway
       
   219     setState(CxeAutoFocusControl::Unknown);
       
   220     mCancelled = false;
       
   221 
       
   222     CX_DEBUG_EXIT_FUNCTION();
       
   223 
       
   224 }
       
   225 
       
   226 
       
   227 
       
   228 /*
       
   229 * camera reference changing, release resources
       
   230 */
       
   231 void CxeAutoFocusControlSymbian::prepareForCameraDelete()
       
   232 {
       
   233     CX_DEBUG_ENTER_FUNCTION();
       
   234 
       
   235     prepareForRelease();
       
   236     mAdvancedSettings = NULL;
       
   237 
       
   238     CX_DEBUG_EXIT_FUNCTION();
       
   239 }
       
   240 
       
   241 
       
   242 
       
   243 /*
       
   244 * new camera available,
       
   245 */
       
   246 void CxeAutoFocusControlSymbian::handleCameraAllocated(CxeError::Id error)
       
   247 {
       
   248     CX_DEBUG_ENTER_FUNCTION();
       
   249     if (!error) {
       
   250         setState(CxeAutoFocusControl::Unknown);
       
   251         mCancelled = false;
       
   252         initializeResources();
       
   253     }
       
   254     CX_DEBUG_EXIT_FUNCTION();
       
   255 }
       
   256 
       
   257 /*
       
   258 * CxeAutoFocusControlSymbian::state
       
   259 */
       
   260 CxeAutoFocusControl::State CxeAutoFocusControlSymbian::state() const
       
   261 {
       
   262     return static_cast<State>( stateId() );
       
   263 }
       
   264 
       
   265 /*
       
   266 * CxeAutoFocusControlSymbian::handleStateChanged
       
   267 */
       
   268 void CxeAutoFocusControlSymbian::handleStateChanged( int newStateId, CxeError::Id error )
       
   269 {
       
   270     emit stateChanged(static_cast<State>(newStateId), error);
       
   271 }
       
   272 
       
   273 /*
       
   274 * CxeAutoFocusControlSymbian::initializeStates
       
   275 */
       
   276 void CxeAutoFocusControlSymbian::initializeStates()
       
   277 {
       
   278     // addState( id, name, allowed next states )
       
   279     addState( new CxeState( Unknown , "Unknown", InProgress | Canceling ) );
       
   280     addState( new CxeState( InProgress , "InProgress", Unknown | Failed | Ready | Canceling ) );
       
   281     addState( new CxeState( Failed , "Failed", InProgress | Unknown | Canceling ) );
       
   282     addState( new CxeState( Ready , "Ready", Unknown | InProgress | Canceling ) );
       
   283     addState( new CxeState( Canceling , "Canceling", Unknown ) );
       
   284 
       
   285     setInitialState( Unknown );
       
   286 }
       
   287 
       
   288 /*
       
   289 * CxeAutoFocusControlSymbian::initializeResources
       
   290 */
       
   291 void CxeAutoFocusControlSymbian::initializeResources()
       
   292 {
       
   293     CX_DEBUG_ENTER_FUNCTION();
       
   294 
       
   295     // No check if non-null. Not supported if zero pointer (see supported() ).
       
   296     mAdvancedSettings = mCameraDevice.advancedSettings();
       
   297 
       
   298     CX_DEBUG_EXIT_FUNCTION();
       
   299 }
       
   300 
       
   301 
       
   302 /*
       
   303 * CxeAutoFocusControlSymbian::setFocusRange
       
   304 */
       
   305 void CxeAutoFocusControlSymbian::setFocusRange(CCamera::CCameraAdvancedSettings::TFocusRange range)
       
   306 {
       
   307     CX_DEBUG_ENTER_FUNCTION();
       
   308     CX_DEBUG_ASSERT(mAdvancedSettings);
       
   309 
       
   310     mAdvancedSettings->SetFocusRange(range);
       
   311 
       
   312     CX_DEBUG_EXIT_FUNCTION();
       
   313 }
       
   314 
       
   315 /*
       
   316 * CxeAutoFocusControlSymbian::focusRange
       
   317 */
       
   318 CCamera::CCameraAdvancedSettings::TFocusRange CxeAutoFocusControlSymbian::focusRange() const
       
   319 {
       
   320     CX_DEBUG_ENTER_FUNCTION();
       
   321     const CCamera::CCameraAdvancedSettings::TFocusRange range(
       
   322         mAdvancedSettings
       
   323               ? mAdvancedSettings->FocusRange()
       
   324               : CCamera::CCameraAdvancedSettings::EFocusRangeAuto );
       
   325 
       
   326     CX_DEBUG_EXIT_FUNCTION();
       
   327     return range;
       
   328 }
       
   329 
       
   330 /*
       
   331 * CxeAutoFocusControlSymbian::setFocusType
       
   332 */
       
   333 void CxeAutoFocusControlSymbian::setFocusType(CCamera::CCameraAdvancedSettings::TAutoFocusType type)
       
   334 {
       
   335     CX_DEBUG_ENTER_FUNCTION();
       
   336     CX_DEBUG_ASSERT(mAdvancedSettings);
       
   337 
       
   338     mAdvancedSettings->SetAutoFocusType(type);
       
   339 
       
   340     CX_DEBUG_EXIT_FUNCTION();
       
   341 }
       
   342 
       
   343 /*
       
   344 * CxeAutoFocusControlSymbian::supportedFocusTypes
       
   345 */
       
   346 int CxeAutoFocusControlSymbian::supportedFocusTypes() const
       
   347 {
       
   348     CX_DEBUG_ENTER_FUNCTION();
       
   349 
       
   350     const int support( mAdvancedSettings
       
   351                      ? mAdvancedSettings->SupportedAutoFocusTypes()
       
   352                      : CCamera::CCameraAdvancedSettings::EAutoFocusTypeOff );
       
   353 
       
   354     CX_DEBUG_EXIT_FUNCTION();
       
   355     return support;
       
   356 }
       
   357 
       
   358 /*
       
   359 * CxeAutoFocusControlSymbian::focusType
       
   360 */
       
   361 CCamera::CCameraAdvancedSettings::TAutoFocusType CxeAutoFocusControlSymbian::focusType() const
       
   362 {
       
   363     CX_DEBUG_ENTER_FUNCTION();
       
   364 
       
   365     const CCamera::CCameraAdvancedSettings::TAutoFocusType type(
       
   366         mAdvancedSettings
       
   367               ? mAdvancedSettings->AutoFocusType()
       
   368               : CCamera::CCameraAdvancedSettings::EAutoFocusTypeOff );
       
   369 
       
   370     CX_DEBUG_EXIT_FUNCTION();
       
   371     return type;
       
   372 }
       
   373 
       
   374 
       
   375 /*
       
   376 * Image Scene mode changed, get the new autofocus value
       
   377 */
       
   378 void CxeAutoFocusControlSymbian::handleSceneChanged(CxeScene& scene)
       
   379 {
       
   380     CX_DEBUG_ENTER_FUNCTION();
       
   381 
       
   382     // whenever scene mode is changed we set the state to unknown
       
   383     setState(CxeAutoFocusControl::Unknown);
       
   384 
       
   385     // we are interested only in the AF range.
       
   386     if(scene.contains(CxeSettingIds::FOCAL_RANGE) && supported() ) {
       
   387         setMode(static_cast<CxeAutoFocusControl::Mode>(scene[CxeSettingIds::FOCAL_RANGE].toInt()));
       
   388     }
       
   389 
       
   390     CX_DEBUG_EXIT_FUNCTION();
       
   391 }
       
   392 
       
   393 
       
   394 
       
   395 /*
       
   396 * CxeAutoFocusControlSymbian::handleAfEvent
       
   397 */
       
   398 void CxeAutoFocusControlSymbian::handleAfEvent(int eventUid, int error)
       
   399 {
       
   400     CX_DEBUG_ENTER_FUNCTION();
       
   401 
       
   402     switch ( state() ) {
       
   403     case CxeAutoFocusControl::InProgress: {
       
   404         if (eventUid == KUidECamEventCameraSettingsOptimalFocusUidValue ) {
       
   405             OstTrace0(camerax_performance, CXEAUTOFOCUSCONTROLSYMBIAN_AF_LOCK, "msg: e_CX_AUTOFOCUS_LOCK 0");
       
   406 
       
   407             CX_DEBUG(("CxeAutoFocusControlSymbian::handleAfEvent <> KUidECamEventCameraSettingsOptimalFocus"));
       
   408             if (KErrNone == error) {
       
   409                 setState(CxeAutoFocusControl::Ready);
       
   410             } else {
       
   411                 setState(CxeAutoFocusControl::Failed, error);
       
   412             }
       
   413          }
       
   414          break;
       
   415         }
       
   416     case CxeAutoFocusControl::Canceling: {
       
   417         CX_DEBUG(("CxeAutoFocusControlSymbian::handleAfEvent <> Canceling"));
       
   418         // Cancelling started by setting AF off to stop ongoing focus operation.
       
   419         // Finalize cancelling by setting lens to hyperfocal position.
       
   420         if (eventUid == KUidECamEventCameraSettingAutoFocusType2UidValue) {
       
   421             if (focusType() == CCamera::CCameraAdvancedSettings::EAutoFocusTypeOff) {
       
   422                 setFocusRange(CCamera::CCameraAdvancedSettings::EFocusRangeHyperfocal);
       
   423                 setFocusType(CCamera::CCameraAdvancedSettings::EAutoFocusTypeSingle);
       
   424             }
       
   425         } else if (eventUid == KUidECamEventCameraSettingsOptimalFocusUidValue) {
       
   426             mCancelled = true;
       
   427             setState(CxeAutoFocusControl::Unknown);
       
   428         }
       
   429 
       
   430         break;
       
   431     }
       
   432     default:
       
   433         break;
       
   434     } // end switch
       
   435 
       
   436 }
       
   437 
       
   438 // end of file