startupservices/startupanimation/sanimihlplugin/src/sanimihlctrl.cpp
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  Implementation of CSAnimIhlCtrl class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <IHLImageFactory.h>
       
    20 #include <MIHLBitmap.h>
       
    21 #include <MIHLFileImage.h>
       
    22 #include <IHLViewerFactory.h>
       
    23 #include <MIHLImageViewer.h>
       
    24 #include "sanimobserver.h"
       
    25 
       
    26 #include "sanimihlctrl.h"
       
    27 #include "assert.h"
       
    28 #include "trace.h"
       
    29 
       
    30 /** Some GIF animations have all frame delays set to zero. */
       
    31 const TInt KDefaultFrameDelay = 300000;
       
    32 
       
    33 // ======== LOCAL FUNCTIONS ========
       
    34 
       
    35 static TBool operator<=( const TSize& aLhs, const TSize& aRhs )
       
    36     {
       
    37     return aLhs.iWidth <= aRhs.iWidth && aLhs.iHeight <= aRhs.iHeight;
       
    38     }
       
    39 
       
    40 
       
    41 // ======== MEMBER FUNCTIONS ========
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CSAnimIhlCtrl::NewL
       
    45 //
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CSAnimIhlCtrl* CSAnimIhlCtrl::NewL(
       
    49     MSAnimObserver& aObserver,
       
    50     RFs& aFs,
       
    51     const TDesC& aFileName,
       
    52     const TDisplayMode aDisplayMode,
       
    53     const TSize& aSize,
       
    54     const TTimeIntervalMicroSeconds32& aFrameDelay,
       
    55     const TBool aScalingEnabled )
       
    56     {
       
    57     FUNC_LOG;
       
    58 
       
    59     CSAnimIhlCtrl* self = new( ELeave ) CSAnimIhlCtrl( aObserver, aFrameDelay );
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL( aFs, aFileName, aDisplayMode, aSize, aScalingEnabled );
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CSAnimIhlCtrl::~CSAnimIhlCtrl
       
    69 //
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CSAnimIhlCtrl::~CSAnimIhlCtrl()
       
    73     {
       
    74     FUNC_LOG;
       
    75 
       
    76     Cancel();
       
    77     iTimer.Close();
       
    78     delete iViewer;
       
    79     iFrames.ResetAndDestroy();
       
    80     delete iImage;
       
    81     iFile.Close();
       
    82     delete iBuffer;
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CSAnimIhlCtrl::BackroundColour
       
    88 //
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 TRgb CSAnimIhlCtrl::BackroundColour() const
       
    92     {
       
    93     FUNC_LOG;
       
    94 
       
    95     return iImage->BackgroundColor();
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CSAnimIhlCtrl::Load
       
   101 //
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 void CSAnimIhlCtrl::Load( const TCallBack& aCallBack )
       
   105     {
       
   106     FUNC_LOG;
       
   107     ASSERT_TRACE( iState == EInitial, SAnimPanic::EInternalError );
       
   108 
       
   109     if ( iState == EInitial )
       
   110         {
       
   111         iState = ELoading;
       
   112         iCallBack = aCallBack;
       
   113         iSuccessCode = KErrNone;
       
   114         delete iViewer;
       
   115         iViewer = NULL;
       
   116     
       
   117         if ( ( iType == EAnimation || iType == EStillImage ) && iFrames.Count() > 0 )
       
   118             {
       
   119             MIHLBitmap& frame = *( iFrames[ 0 ] );
       
   120             TRAP( iSuccessCode, iViewer = IHLViewerFactory::CreateImageViewerL(
       
   121                 iViewerSize, *iImage, frame, *this ) );
       
   122             if ( iSuccessCode != KErrNone )
       
   123                 {
       
   124                 Notify( iSuccessCode, EFailed );
       
   125                 }
       
   126             }
       
   127         else if ( iType == EMultiFrame && iFrameIndex < iFrameCount - 1 )
       
   128             {
       
   129             LoadNextSubImage();
       
   130             }
       
   131         else
       
   132             {
       
   133             Notify( KErrNotFound, EFailed );
       
   134             }
       
   135         }
       
   136     else
       
   137         {
       
   138         iSuccessCode = KErrAlreadyExists;
       
   139         aCallBack.CallBack();
       
   140         }
       
   141     }
       
   142 
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // CSAnimIhlCtrl::Play
       
   146 //
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CSAnimIhlCtrl::Play( const TCallBack& aCallBack )
       
   150     {
       
   151     FUNC_LOG;
       
   152     ASSERT_TRACE( iState == ELoaded, SAnimPanic::EInternalError );
       
   153 
       
   154     if ( iState == ELoaded )
       
   155         {
       
   156         iState = EPlaying;
       
   157         iCallBack = aCallBack;
       
   158         iSuccessCode = KErrNone;
       
   159         iFrameIndex = -1;
       
   160         ShowNextFrame();
       
   161         }
       
   162     else
       
   163         {
       
   164         iSuccessCode = KErrNotReady;
       
   165         aCallBack.CallBack();
       
   166         }
       
   167     }
       
   168 
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // CSAnimIhlCtrl::SuccessCode
       
   172 //
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TInt CSAnimIhlCtrl::SuccessCode() const
       
   176     {
       
   177     FUNC_LOG;
       
   178     INFO_1( "Success code: %d", iSuccessCode );
       
   179 
       
   180     return iSuccessCode;
       
   181     }
       
   182 
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // CSAnimIhlCtrl::Stop
       
   186 //
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CSAnimIhlCtrl::Stop()
       
   190     {
       
   191     FUNC_LOG;
       
   192     INFO_1( "Stop: state %d", iState );
       
   193 
       
   194     if ( iState == ELoading || iState == EPlaying )
       
   195         {
       
   196         StopAndNotify( KErrCancel, EFailed );
       
   197         }
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CSAnimIhlCtrl::DoCancel
       
   203 //
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CSAnimIhlCtrl::DoCancel()
       
   207     {
       
   208     FUNC_LOG;
       
   209 
       
   210     iImage->CancelLoad();
       
   211     iTimer.Cancel();
       
   212     }
       
   213 
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // CSAnimIhlCtrl::RunL
       
   217 //
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 void CSAnimIhlCtrl::RunL()
       
   221     {
       
   222     FUNC_LOG;
       
   223     ERROR( iStatus.Int(), "Timer completed with error" );
       
   224 
       
   225     if ( iStatus.Int() == KErrNone )
       
   226         {
       
   227         if ( iState == ELoading )
       
   228             {
       
   229             if ( iType == EMultiFrame && iFrameIndex < iFrameCount - 1 )
       
   230                 {
       
   231                 LoadNextSubImage();
       
   232                 }
       
   233             else
       
   234                 {
       
   235                 iFrameIndex = -1;
       
   236                 Notify( KErrNone, ELoaded );
       
   237                 }
       
   238             }
       
   239         else if ( iState == EPlaying )
       
   240             {
       
   241             ShowNextFrame();
       
   242             }
       
   243         }
       
   244     else
       
   245         {
       
   246         Notify( iStatus.Int(), EFailed );
       
   247         }
       
   248     }
       
   249 
       
   250 
       
   251 // ---------------------------------------------------------------------------
       
   252 // CSAnimIhlCtrl::CSAnimIhlCtrl
       
   253 //
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 CSAnimIhlCtrl::CSAnimIhlCtrl(
       
   257     MSAnimObserver& aObserver,
       
   258     const TTimeIntervalMicroSeconds32& aFrameDelay )
       
   259   : CActive( EPriorityStandard ),
       
   260     iObserver( aObserver ),
       
   261     iFrameDelay( aFrameDelay ),
       
   262     iFrameIndex( -1 ),
       
   263     iSuccessCode( KErrNone ),
       
   264     iType( EStillImage ),
       
   265     iState( EInitial )
       
   266     {
       
   267     FUNC_LOG;
       
   268 
       
   269     CActiveScheduler::Add( this );
       
   270     }
       
   271 
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // CSAnimIhlCtrl::ConstructL
       
   275 //
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void CSAnimIhlCtrl::ConstructL(
       
   279     RFs& aFs,
       
   280     const TDesC& aFileName,
       
   281     const TDisplayMode /*aDisplayMode*/,
       
   282     const TSize& aSize,
       
   283     const TBool aScalingEnabled )
       
   284     {
       
   285     FUNC_LOG;
       
   286 
       
   287     User::LeaveIfError( iTimer.CreateLocal() );
       
   288     User::LeaveIfError( iFile.Open(
       
   289         aFs, aFileName, EFileRead | EFileShareReadersOnly ) );
       
   290 	
       
   291     TInt fileSize;
       
   292     iFile.Size( fileSize );
       
   293     iBuffer = HBufC8::NewL( fileSize );
       
   294     
       
   295     TPtr8 fileDes = iBuffer->Des();
       
   296     iFile.Read( fileDes, fileSize );
       
   297     iImage = IHLImageFactory::OpenBufferedFileImageL(aFs,*iBuffer );
       
   298 
       
   299     iFrameCount = 1; // If it is not an animation, frame count is 1.
       
   300     if ( iImage->IsAnimation() )
       
   301         {
       
   302         iFrameCount = iImage->AnimationFrameCount();
       
   303         iType = EAnimation;
       
   304         }
       
   305     else if ( iImage->ImageCount() > 1 )
       
   306         {
       
   307         iFrameCount = iImage->ImageCount();
       
   308         iType = EMultiFrame;
       
   309         }
       
   310 
       
   311     INFO_2( "Type: %d, frame count: %d", iType, iFrameCount );
       
   312 
       
   313     iViewerSize = SelectSize( aSize, aScalingEnabled );
       
   314     if ( iType == EAnimation || iType == EStillImage )
       
   315         {
       
   316         MIHLBitmap* frame = IHLBitmap::CreateL();
       
   317         CleanupStack::PushL( frame );
       
   318         iFrames.AppendL( frame );
       
   319         CleanupStack::Pop( frame );
       
   320         }
       
   321     else if ( iType == EMultiFrame )
       
   322         {
       
   323         for ( TInt i = 0; i < iFrameCount; i++ )
       
   324             {
       
   325             MIHLBitmap* frame = IHLBitmap::CreateL();
       
   326             CleanupStack::PushL( frame );
       
   327             User::LeaveIfError( frame->Create( iViewerSize, iImage->DisplayMode(), EGray256 ) );
       
   328             iFrames.AppendL( frame );
       
   329             CleanupStack::Pop( frame );
       
   330             }
       
   331         }
       
   332     else
       
   333         {
       
   334         ERROR_GEN_1( "ConstructL: type %d unexpected", iType );
       
   335         User::Leave( KErrGeneral );
       
   336         }
       
   337     }
       
   338 
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // CSAnimIhlCtrl::SelectSize
       
   342 //
       
   343 // ---------------------------------------------------------------------------
       
   344 //
       
   345 TSize CSAnimIhlCtrl::SelectSize(
       
   346     const TSize& aSize,
       
   347     const TBool aScalingEnabled ) const
       
   348     {
       
   349     FUNC_LOG;
       
   350 
       
   351     TSize imageSize = iImage->Size(); // Default for all elses
       
   352     if ( aScalingEnabled && aSize != TSize( 0, 0 ) ) // Scaling requested
       
   353         {
       
   354         if ( iImage->IsFullyScaleable() )
       
   355             {
       
   356             INFO( "Image is fully scaleable" );
       
   357 
       
   358             imageSize = aSize;
       
   359             }
       
   360         else
       
   361             {
       
   362             const RArray<TSize>& loadSizeArray = iImage->CustomLoadSizeArray();
       
   363             if ( loadSizeArray.Count() > 0 )
       
   364                 {
       
   365                 INFO( "Image has custom load size array" );
       
   366 
       
   367                 imageSize = FindClosestMatch( aSize, imageSize, loadSizeArray );
       
   368                 }
       
   369             }
       
   370         }
       
   371 
       
   372     return imageSize;
       
   373     }
       
   374 
       
   375 
       
   376 // ---------------------------------------------------------------------------
       
   377 // CSAnimIhlCtrl::FindClosestMatch
       
   378 //
       
   379 // ---------------------------------------------------------------------------
       
   380 //
       
   381 TSize CSAnimIhlCtrl::FindClosestMatch(
       
   382     const TSize& aTarget,
       
   383     const TSize& aOptimalSize,
       
   384     const RArray<TSize>& aOptions ) const
       
   385     {
       
   386     FUNC_LOG;
       
   387     INFO_2( "Target size: (%d, %d)", aTarget.iWidth, aTarget.iHeight );
       
   388     INFO_2( "Optimal size: (%d, %d)", aOptimalSize.iWidth, aOptimalSize.iHeight );
       
   389 
       
   390     TSize selected( 0, 0 );
       
   391     if ( aOptimalSize <= aTarget )
       
   392         {
       
   393         selected = aOptimalSize;
       
   394         }
       
   395 
       
   396     TInt count = aOptions.Count();
       
   397     for ( TInt i = 0; i < count; i++ )
       
   398         {
       
   399         TSize item = aOptions[i];
       
   400 
       
   401         INFO_3( "Option %d: (%d, %d)", i, item.iWidth, item.iHeight );
       
   402 
       
   403         if ( item.iHeight <= aTarget.iHeight && item.iWidth <= aTarget.iWidth ) // Acceptable
       
   404             {
       
   405             if ( ( aTarget.iHeight - item.iHeight ) <
       
   406                  ( aTarget.iHeight - selected.iHeight ) )
       
   407                 {
       
   408                 selected = item;
       
   409                 }
       
   410             }
       
   411         }
       
   412 
       
   413     if ( selected == TSize( 0, 0 ) ) // No match found
       
   414         {
       
   415         selected = aOptimalSize;
       
   416         }
       
   417 
       
   418     INFO_2( "Selected option: (%d, %d)", selected.iWidth, selected.iHeight );
       
   419     return selected;
       
   420     }
       
   421 
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // CSAnimIhlCtrl::LoadNextSubImage
       
   425 //
       
   426 // ---------------------------------------------------------------------------
       
   427 //
       
   428 void CSAnimIhlCtrl::LoadNextSubImage()
       
   429     {
       
   430     FUNC_LOG;
       
   431     ASSERT_TRACE( iState == ELoading && !IsActive(), SAnimPanic::EInternalError );
       
   432 
       
   433     iFrameIndex++;
       
   434 
       
   435     INFO_2( "LoadNextSubImage: index: %d, frame count: %d", iFrameIndex, iFrameCount );
       
   436 
       
   437     ASSERT_TRACE( iFrameIndex < iFrames.Count(), SAnimPanic::EInternalError );
       
   438     MIHLBitmap* nextFrame = iFrames[ iFrameIndex ];
       
   439 
       
   440     iSuccessCode = iImage->Load( iStatus, *nextFrame, iFrameIndex );
       
   441 
       
   442     if ( iSuccessCode == KErrNone )
       
   443         {
       
   444         SetActive();
       
   445         }
       
   446     else
       
   447         {
       
   448         Notify( iSuccessCode, EFailed );
       
   449         }
       
   450     }
       
   451 
       
   452 
       
   453 // ---------------------------------------------------------------------------
       
   454 // CSAnimIhlCtrl::ShowNextFrame
       
   455 //
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void CSAnimIhlCtrl::ShowNextFrame()
       
   459     {
       
   460     FUNC_LOG;
       
   461     ASSERT_TRACE( iState == EPlaying, SAnimPanic::EInternalError );
       
   462 
       
   463     if ( iFrameIndex < iFrameCount - 1 )
       
   464         {
       
   465         iFrameIndex++;
       
   466 
       
   467         INFO_2( "ShowNextFrame: index: %d, frame count: %d", iFrameIndex, iFrameCount );
       
   468 
       
   469         if ( iType == EAnimation && iViewer )
       
   470             {
       
   471             UpdateFrameToScreen( 0 );
       
   472             iViewer->Play();
       
   473             }
       
   474         else if ( iType == EStillImage )
       
   475             {
       
   476             UpdateFrameToScreen( 0 );
       
   477             StartFrameTimer();
       
   478             }
       
   479         else if ( iType == EMultiFrame )
       
   480             {
       
   481             UpdateFrameToScreen( iFrameIndex );
       
   482             StartFrameTimer();
       
   483             }
       
   484         else
       
   485             {
       
   486             ERROR_GEN_1( "ShowNextFrame: type %d unexpected", iType );
       
   487             Notify( KErrGeneral, EFailed );
       
   488             }
       
   489         }
       
   490     else
       
   491         {
       
   492         StopAndNotify( KErrNone, EFinished );
       
   493         }
       
   494     }
       
   495 
       
   496 
       
   497 // ---------------------------------------------------------------------------
       
   498 // CSAnimIhlCtrl::UpdateFrameToScreen
       
   499 //
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 void CSAnimIhlCtrl::UpdateFrameToScreen( TInt aFrameIndex ) const
       
   503     {
       
   504     FUNC_LOG;
       
   505 
       
   506     if ( aFrameIndex >= 0 && aFrameIndex < iFrames.Count() )
       
   507         {
       
   508         MIHLBitmap& frame = *( iFrames[ aFrameIndex ] );
       
   509         if ( frame.HasMask() )
       
   510             {
       
   511             iObserver.UpdateScreen( frame.Bitmap(), frame.Mask() );
       
   512             }
       
   513         else
       
   514             {
       
   515             iObserver.UpdateScreen( frame.Bitmap() );
       
   516             }
       
   517         }
       
   518     else
       
   519         {
       
   520         ERROR_GEN_1( "UpdateFrameToScreen: aFrameIndex %d out of bounds", aFrameIndex );
       
   521         }
       
   522     }
       
   523 
       
   524 
       
   525 // ---------------------------------------------------------------------------
       
   526 // CSAnimIhlCtrl::StartFrameTimer
       
   527 //
       
   528 // ---------------------------------------------------------------------------
       
   529 //
       
   530 
       
   531 void CSAnimIhlCtrl::StartFrameTimer()
       
   532     {
       
   533     FUNC_LOG;
       
   534     ASSERT_TRACE( !IsActive(), SAnimPanic::EInternalError );
       
   535 
       
   536     if ( !IsActive() )
       
   537         {
       
   538         TTimeIntervalMicroSeconds32 delay = ( iFrameDelay.Int() == 0 ?
       
   539             KDefaultFrameDelay : iFrameDelay );
       
   540         INFO_2( "Frame delay: %d, default delay: %d", delay.Int(), iFrameDelay.Int() );
       
   541         iTimer.Cancel();
       
   542         iTimer.After( iStatus, delay );
       
   543         SetActive();
       
   544         }
       
   545     }
       
   546 
       
   547 
       
   548 // ---------------------------------------------------------------------------
       
   549 // CSAnimIhlCtrl::StopAndNotify
       
   550 //
       
   551 // ---------------------------------------------------------------------------
       
   552 //
       
   553 void CSAnimIhlCtrl::StopAndNotify( TInt aError, TInt aNextState )
       
   554     {
       
   555     FUNC_LOG;
       
   556 
       
   557     Cancel();
       
   558     if ( iViewer )
       
   559         {
       
   560         iViewer->Stop();
       
   561         }
       
   562     Notify( aError, aNextState );
       
   563     }
       
   564 
       
   565 
       
   566 // ---------------------------------------------------------------------------
       
   567 // CSAnimIhlCtrl::Notify
       
   568 //
       
   569 // ---------------------------------------------------------------------------
       
   570 //
       
   571 void CSAnimIhlCtrl::Notify( TInt aError, TInt aNextState )
       
   572     {
       
   573     FUNC_LOG;
       
   574     ERROR_1( aError, "Notify: state %d", aNextState );
       
   575 
       
   576     iSuccessCode = aError;
       
   577     iState = aNextState;
       
   578     iCallBack.CallBack();
       
   579     }
       
   580 
       
   581 
       
   582 // ---------------------------------------------------------------------------
       
   583 // CSAnimIhlCtrl::ViewerBitmapChangedL
       
   584 //
       
   585 // ---------------------------------------------------------------------------
       
   586 //
       
   587 void CSAnimIhlCtrl::ViewerBitmapChangedL()
       
   588     {
       
   589     FUNC_LOG;
       
   590     INFO_1( "ViewerBitmapChangedL: state %d", iState );
       
   591 
       
   592     if ( iState == ELoading )
       
   593         {
       
   594         iFrameIndex = -1;
       
   595         Notify( KErrNone, ELoaded );
       
   596         }
       
   597     else if ( iState == EPlaying )
       
   598         {
       
   599         ShowNextFrame();
       
   600         }
       
   601     else
       
   602         {
       
   603         StopAndNotify( KErrGeneral, EFailed );
       
   604         }
       
   605     }
       
   606 
       
   607 
       
   608 // ---------------------------------------------------------------------------
       
   609 // CSAnimIhlCtrl::ViewerError
       
   610 //
       
   611 // ---------------------------------------------------------------------------
       
   612 //
       
   613 void CSAnimIhlCtrl::ViewerError( TInt aError )
       
   614     {
       
   615     FUNC_LOG;
       
   616 
       
   617     StopAndNotify( aError, EFailed );
       
   618     }
       
   619