camerauis/cameraapp/generic/src/CamMemoryMonitor.cpp
branchRCL_3
changeset 24 bac7acad7cb3
parent 0 1ddebce53859
child 25 2c87b2808fd7
equal deleted inserted replaced
23:61bc0f252b2b 24:bac7acad7cb3
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Class that monitors free memory and attempts to free it if necessary
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CamMemoryMonitor.h"
       
    19 
       
    20 
       
    21 #include <hal.h>
       
    22 #include <hal_data.h>
       
    23 #include <AknGlobalNote.h>
       
    24 #include <StringLoader.h>
       
    25 #include <vgacamsettings.rsg>
       
    26 #include "CamAppUi.h"
       
    27 #include "CamAppController.h"
       
    28 #include "camlogging.h"
       
    29 
       
    30 
       
    31 const static TInt KMemoryCheckInterval = 10*1000*1000;
       
    32 const static TInt KNeededMemoryLevel = 20*1024*1024;
       
    33 const static TInt KCriticalMemoryLevel = 18*1024*1024;
       
    34 
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // CCamMemoryMonitor::NewL
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CCamMemoryMonitor* CCamMemoryMonitor::NewL(CCamAppUi* aAppUi, CCamAppController* aController)
       
    41     {
       
    42     CCamMemoryMonitor* self = new (ELeave) CCamMemoryMonitor( aAppUi, aController );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop(); // self;
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // CCamMemoryMonitor::~CCamMemoryMonitor
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CCamMemoryMonitor::~CCamMemoryMonitor()
       
    54     {
       
    55     PRINT( _L("Camera => CCamMemoryMonitor::~CCamMemoryMonitor") )
       
    56     if ( iMemoryCheckTimer )
       
    57         {
       
    58         iMemoryCheckTimer->Cancel();
       
    59         delete iMemoryCheckTimer;
       
    60         }
       
    61     PRINT( _L("Camera <= CCamMemoryMonitor::~CCamMemoryMonitor") )
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // CCamMemoryMonitor::StartMonitoring
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 void CCamMemoryMonitor::StartMonitoring( TInt aLimit, TInt aRequestAmount )
       
    69     {
       
    70     PRINT( _L("Camera => CCamMemoryMonitor::StartMonitoring") )
       
    71     if ( !iMemoryCheckTimer->IsActive() )
       
    72         {
       
    73         iLimit = aLimit;
       
    74         iRequestAmount = aRequestAmount;
       
    75         iMemoryCheckTimer->Start( KMemoryCheckInterval, KMemoryCheckInterval,
       
    76                               TCallBack( MemoryCheck, this ) );
       
    77         }
       
    78     PRINT( _L("Camera <= CCamMemoryMonitor::StartMonitoring") )
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CCamMemoryMonitor::StartMonitoring
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 void CCamMemoryMonitor::StartMonitoring()
       
    86     {
       
    87     PRINT( _L("Camera => CCamMemoryMonitor::StartMonitoring") )
       
    88     if ( !iMemoryCheckTimer->IsActive() )
       
    89         {
       
    90         iMemoryCheckTimer->Start( KMemoryCheckInterval, KMemoryCheckInterval,
       
    91                               TCallBack( MemoryCheck, this ) );
       
    92         }
       
    93     PRINT( _L("Camera <= CCamMemoryMonitor::StartMonitoring") )
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // CCamMemoryMonitor::StopMonitoring
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 void CCamMemoryMonitor::StopMonitoring()
       
   101     {
       
   102     PRINT( _L("Camera => CCamMemoryMonitor::StopMonitoring") )
       
   103     iMemoryCheckTimer->Cancel();
       
   104     PRINT( _L("Camera <= CCamMemoryMonitor::StopMonitoring") )
       
   105     }
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CCamMemoryMonitor::CheckAndRequestMemoryL
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 TInt CCamMemoryMonitor::CheckAndRequestMemoryL( TInt aLimit, TInt aRequestAmount, TBool aShowNote )
       
   112     {
       
   113 
       
   114     PRINT( _L("Camera => CCamMemoryMonitor::CheckAndRequestMemoryL") )
       
   115     if ( IsActive() )
       
   116     {
       
   117         return KErrNone;
       
   118     }
       
   119 
       
   120     TInt memoryLeft = 0;
       
   121     TInt errorCode = KErrNone;
       
   122     HAL::Get( HALData::EMemoryRAMFree, memoryLeft );
       
   123     if ( memoryLeft < aLimit )
       
   124         {
       
   125         PRINT( _L("Camera <> CCamMemoryMonitor::CheckAndRequestMemoryL memory below limit, requesting") )
       
   126         HBufC* freeMemText = NULL;
       
   127         CAknGlobalNote* note = NULL;
       
   128         TInt noteId = 0;
       
   129         if ( aShowNote )
       
   130             {
       
   131 
       
   132             freeMemText = StringLoader::LoadL( R_NOTE_TEXT_FREE_MEMORY );
       
   133             CleanupStack::PushL( freeMemText );
       
   134             // show error note
       
   135             note = CAknGlobalNote::NewL();
       
   136             CleanupStack::PushL( note );
       
   137 
       
   138             note->SetTextProcessing( ETrue );
       
   139             note->SetPriority(CActive::EPriorityHigh);
       
   140             noteId = note->ShowNoteL(EAknGlobalWaitNote, *freeMemText);
       
   141             }
       
   142 
       
   143         User::LeaveIfError( iOomMonitor.Connect() );
       
   144 
       
   145         errorCode = iOomMonitor.RequestFreeMemory( aRequestAmount );
       
   146         if ( errorCode != KErrNone )
       
   147             {
       
   148             // try one more time
       
   149             PRINT( _L("Camera <> CCamMemoryMonitor::CheckAndRequestMemoryL request returned with error, let's try one more time") )
       
   150             errorCode = iOomMonitor.RequestFreeMemory( aRequestAmount );
       
   151             }
       
   152         iOomMonitor.Close();
       
   153 
       
   154 
       
   155         if ( aShowNote )
       
   156             {
       
   157             note->CancelNoteL(noteId);
       
   158             CleanupStack::PopAndDestroy( 2 ); // freeMemText & note
       
   159             }
       
   160         }
       
   161 
       
   162     PRINT1( _L("Camera <= CCamMemoryMonitor::CheckAndRequestMemoryL returning %d"), errorCode )
       
   163     return errorCode;
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // CCamMemoryMonitor::CheckAndRequestMemoryL
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 TInt CCamMemoryMonitor::CheckAndRequestMemoryL( TBool aShowNote )
       
   171     {
       
   172     return CheckAndRequestMemoryL( KCriticalMemoryLevel, KNeededMemoryLevel, aShowNote );
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CCamMemoryMonitor::RunL
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CCamMemoryMonitor::RunL()
       
   180     {
       
   181     PRINT( _L("Camera => CCamMemoryMonitor::RunL") )
       
   182     iOomMonitor.Close();
       
   183     PRINT( _L("Camera <= CCamMemoryMonitor::RunL") )
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CCamMemoryMonitor::DoCancel
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CCamMemoryMonitor::DoCancel()
       
   191     {
       
   192     PRINT( _L("Camera => CCamMemoryMonitor::DoCancel") )
       
   193     if ( IsActive() )
       
   194         {
       
   195         iOomMonitor.CancelRequestFreeMemory();
       
   196         iOomMonitor.Close();
       
   197         }
       
   198     PRINT( _L("Camera <= CCamMemoryMonitor::DoCancel") )
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CCamMemoryMonitor::CCamMemoryMonitor
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 CCamMemoryMonitor::CCamMemoryMonitor(CCamAppUi* aAppUi, CCamAppController* aController)
       
   206 : CActive( CActive::EPriorityIdle ),
       
   207   iLimit( KCriticalMemoryLevel ), iRequestAmount( KNeededMemoryLevel ),
       
   208   iAppUi( aAppUi ), iController( aController )
       
   209 
       
   210     {
       
   211     PRINT( _L("Camera => CCamMemoryMonitor::CCamMemoryMonitor") )
       
   212     CActiveScheduler::Add(this);
       
   213     PRINT( _L("Camera <= CCamMemoryMonitor::CCamMemoryMonitor") )
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // CCamMemoryMonitor::ConstructL
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 void CCamMemoryMonitor::ConstructL()
       
   221     {
       
   222     PRINT( _L("Camera => CCamMemoryMonitor::ConstructL") )
       
   223     iMemoryCheckTimer = CPeriodic::New( CActive::EPriorityIdle );
       
   224     PRINT( _L("Camera <= CCamMemoryMonitor::ConstructL") )
       
   225     }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // CCamMemoryMonitor::MemoryCheck
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 TInt CCamMemoryMonitor::MemoryCheck( TAny* aPtr )
       
   232     {
       
   233 
       
   234     PRINT( _L("Camera => CCamMemoryMonitor::MemoryCheck") )
       
   235     CCamMemoryMonitor* self = static_cast<CCamMemoryMonitor*>( aPtr );
       
   236 
       
   237     self->CheckMemory();
       
   238 
       
   239     PRINT( _L("Camera <= CCamMemoryMonitor::MemoryCheck") )
       
   240     return KErrNone;
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // CCamMemoryMonitor::CheckMemory
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 void CCamMemoryMonitor::CheckMemory()
       
   248     {
       
   249     PRINT( _L("Camera => CCamMemoryMonitor::CheckMemory") )
       
   250     if ( IsActive() )
       
   251         {
       
   252         PRINT( _L("Camera <= CCamMemoryMonitor::CheckMemory already requesting memory") )
       
   253         return;
       
   254         }
       
   255 
       
   256     if ( iAppUi->CurrentViewState() == ECamViewStatePreCapture &&
       
   257          iController->CurrentOperation() == ECamNoOperation &&
       
   258          !iAppUi->AppInBackground( EFalse ) )
       
   259         {
       
   260         PRINT( _L("Camera <> CCamMemoryMonitor::CheckMemory checking memory") )
       
   261         TInt memoryLeft = 0;
       
   262         HAL::Get( HALData::EMemoryRAMFree, memoryLeft );
       
   263         if ( memoryLeft < iLimit )
       
   264             {
       
   265             TInt err =  iOomMonitor.Connect();
       
   266             if ( !err )
       
   267                 {
       
   268                 PRINT( _L("Camera <> CCamMemoryMonitor::CheckMemory requesting memory") )
       
   269                 iOomMonitor.RequestFreeMemory( iRequestAmount, iStatus );
       
   270                 SetActive();
       
   271                 }
       
   272             else
       
   273                 {
       
   274                 PRINT1( _L("Camera <> CCamMemoryMonitor::CheckMemory failed to connect to OoMMonitor err=%d"), err )
       
   275                 }
       
   276             }
       
   277         else
       
   278             {
       
   279             PRINT( _L("Camera <> CCamMemoryMonitor::CheckMemory no need for memory request") )
       
   280             }
       
   281         }
       
   282     else
       
   283         {
       
   284         PRINT( _L("Camera <> CCamMemoryMonitor::CheckMemory not in correct state, not checking memory") )
       
   285         }
       
   286 
       
   287     PRINT( _L("Camera <= CCamMemoryMonitor::CheckMemory") )
       
   288     }