photosgallery/viewframework/texturemanager/src/glxbitmapdecoderwrapper.cpp
branchRCL_3
changeset 60 5b3385a43d68
child 75 01504893d9cb
equal deleted inserted replaced
59:8e5f6eea9c9f 60:5b3385a43d68
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Texture Manager component/Decodes the Image in step for large sized images
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "glxbitmapdecoderwrapper.h"
       
    23 #include <glxtracer.h>      //  For Log
       
    24 #include <bitmaptransforms.h>
       
    25 #include <imageconversion.h>
       
    26 #include <ExifRead.h>
       
    27 #include <glxlog.h>
       
    28 #include <hal.h>
       
    29 #include <hal_data.h>
       
    30 #include <mglxcache.h>
       
    31 #include <oommonitorsession.h>
       
    32 #include <e32math.h>
       
    33 #include <apgcli.h>
       
    34 
       
    35 namespace
       
    36     {
       
    37     const TInt KGlxDecodingThreshold = 3000000;
       
    38     const TInt KGlxDecodingThresholdDimension = 2000;
       
    39 
       
    40     // Photos low_ram_threshold [8.637 MB] as in oomconfig.xml
       
    41     const TInt KGlxCriticalRAMForPhotos = 9056550;
       
    42     const TInt KGlxDecodeBitmapFactor = 3;
       
    43     
       
    44     _LIT(KMimeJpeg,"image/jpeg");
       
    45     _LIT(KMimeJpg,"image/jpg");
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // Two-phased constructor.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CGlxBitmapDecoderWrapper* CGlxBitmapDecoderWrapper::NewL(
       
    53         MGlxBitmapDecoderObserver* aObserver)
       
    54     {
       
    55     TRACER("CGlxBitmapDecoderWrapper::NewL()");
       
    56     CGlxBitmapDecoderWrapper* self = new (ELeave) CGlxBitmapDecoderWrapper();
       
    57     CleanupStack::PushL(self);
       
    58     self->ConstructL(aObserver);
       
    59     CleanupStack::Pop(self);
       
    60     return self;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CGlxBitmapDecoderWrapper::CGlxBitmapDecoderWrapper() :
       
    68     CActive(EPriorityLow)
       
    69     {
       
    70     TRACER("CGlxBitmapDecoderWrapper::CGlxBitmapDecoderWrapper()");
       
    71     }		
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Destructor
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 CGlxBitmapDecoderWrapper::~CGlxBitmapDecoderWrapper()
       
    78     {
       
    79     TRACER("CGlxBitmapDecoderWrapper::~CGlxBitmapDecoderWrapper()");
       
    80     iFs.Close();
       
    81     Cancel();
       
    82     if (iImageDecoder)
       
    83         {
       
    84         delete iImageDecoder;
       
    85         iImageDecoder = NULL;
       
    86         }
       
    87     if (iImagePath)
       
    88         {
       
    89         delete iImagePath;
       
    90         iImagePath = NULL;
       
    91         }
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // ConstructL
       
    96 // ---------------------------------------------------------------------------
       
    97 void CGlxBitmapDecoderWrapper::ConstructL(MGlxBitmapDecoderObserver* aObserver)
       
    98     {
       
    99     TRACER("CGlxBitmapDecoderWrapper::ConstructL ");
       
   100     iObserver = aObserver;
       
   101     User::LeaveIfError(iFs.Connect());
       
   102     CActiveScheduler::Add( this );
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // DoDecodeImageL
       
   107 // ---------------------------------------------------------------------------		
       
   108 void CGlxBitmapDecoderWrapper::DoDecodeImageL(const TDesC& aSourceFileName,
       
   109         TInt aIndex)
       
   110     {
       
   111     TRACER("CGlxBitmapDecoderWrapper::DoDecodeImageL()");
       
   112     GLX_LOG_URI("CGlxBitmapDecoderWrapper::DoDecodeImageL(%S)",
       
   113             &aSourceFileName);
       
   114 
       
   115     iThumbnailIndex = aIndex;
       
   116 
       
   117 #ifdef _DEBUG
       
   118     iStartTime.HomeTime();
       
   119 #endif                          
       
   120 
       
   121     if (iImageDecoder)
       
   122         {
       
   123         delete iImageDecoder;
       
   124         iImageDecoder = NULL;
       
   125         }
       
   126 
       
   127     CImageDecoder::TOptions options =
       
   128             (CImageDecoder::TOptions) (CImageDecoder::EOptionNoDither
       
   129                     | CImageDecoder::EOptionAlwaysThread);
       
   130 
       
   131     // Use extended JPEG decoder
       
   132     GLX_DEBUG1("DoDecodeImageL:: EHwImplementation" );
       
   133     TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL(
       
   134                     CExtJpegDecoder::EHwImplementation, iFs,
       
   135                     aSourceFileName, options ) );
       
   136     GLX_DEBUG2("DoDecodeImageL:: EHwImplementation (%d)", err);
       
   137     if (KErrNone != err)
       
   138         {
       
   139         TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL(
       
   140                         CExtJpegDecoder::ESwImplementation, iFs,
       
   141                         aSourceFileName, options ) );
       
   142         GLX_DEBUG2("DoDecodeImageL:: ESwImplementation (%d)", err);
       
   143         if (KErrNone != err)
       
   144             {
       
   145             GLX_DEBUG1("DoDecodeImageL::CImageDecoder");
       
   146             // Not a JPEG - use standard decoder
       
   147             iImageDecoder = CImageDecoder::FileNewL(iFs, aSourceFileName,
       
   148                     options);
       
   149             }
       
   150         }
       
   151 #ifdef _DEBUG
       
   152     iStopTime.HomeTime();
       
   153     GLX_DEBUG2("*** Decoder Creation took <%d> us ***",
       
   154             (TInt)iStopTime.MicroSecondsFrom(iStartTime).Int64());
       
   155 #endif                          
       
   156 
       
   157     TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   158     GLX_DEBUG3("GlxDecoderWrapper::DecodeImageL() - OverallSize: w=%d, h=%d",
       
   159             imageSize.iWidth, imageSize.iHeight);
       
   160     iOriginalSize.iWidth = imageSize.iWidth;
       
   161     iOriginalSize.iHeight = imageSize.iHeight;
       
   162 
       
   163     if (iBitmap)
       
   164         {
       
   165         delete iBitmap;
       
   166         iBitmap = NULL;
       
   167         }
       
   168     
       
   169     if (iImagePath)
       
   170         {
       
   171         delete iImagePath;
       
   172         iImagePath = NULL;
       
   173         }
       
   174     iImagePath = aSourceFileName.Alloc();
       
   175     
       
   176     DecodeImageL();
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // DecodeImageL
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CGlxBitmapDecoderWrapper::DecodeImageL()
       
   184     {
       
   185     TRACER("CGlxBitmapDecoderWrapper::DecodeImageL()");
       
   186     TReal32 mFactor = 1;
       
   187     TReal mFactor1 = 1;
       
   188     TReal32 mFactor2 = 1;
       
   189     //Set Size according to level and state
       
   190     TReal32 width = iOriginalSize.iWidth;
       
   191     TReal32 height = iOriginalSize.iHeight;
       
   192 
       
   193     if (KGlxDecodingThreshold < (width * height))
       
   194         {
       
   195         TReal tempFactor = TReal32(KGlxDecodingThreshold)
       
   196                 / (width * height);
       
   197         User::LeaveIfError(Math::Sqrt(mFactor1, tempFactor));
       
   198         GLX_DEBUG2("DecodeImageL() - mFactor1 = %f", mFactor1);
       
   199         }
       
   200 
       
   201     if (KGlxDecodingThresholdDimension < width
       
   202             || KGlxDecodingThresholdDimension < height)
       
   203         {
       
   204         mFactor2 = TReal32(KGlxDecodingThresholdDimension) / Max(width,
       
   205                 height);
       
   206         GLX_DEBUG2("DecodeImageL() - mFactor2 = %f", mFactor2);
       
   207         }
       
   208 
       
   209     mFactor = Min(TReal32(mFactor1), mFactor2);
       
   210     GLX_DEBUG2("DecodeImageL() - Final mFactor = %f", mFactor);
       
   211 
       
   212     // create the destination bitmap
       
   213     if (!iBitmap)
       
   214         {
       
   215         TInt freeMemory = 0;
       
   216         HAL::Get(HALData::EMemoryRAMFree, freeMemory);
       
   217         width *= mFactor;
       
   218         height *= mFactor;
       
   219         TInt minmemorytodecode = KGlxDecodeBitmapFactor * width * height;
       
   220         GLX_DEBUG3("DecodeImageL: minmemorytodecode=%d, freememory=%d",
       
   221                 minmemorytodecode, freeMemory);
       
   222         
       
   223         iTargetBitmapSize.iWidth = width;
       
   224         iTargetBitmapSize.iHeight = height;
       
   225         GLX_DEBUG3("DecodeImageL: iTargetBitmapSize w=%d, h=%d",
       
   226                 iTargetBitmapSize.iWidth, iTargetBitmapSize.iHeight);
       
   227         __ASSERT_DEBUG(width > 0 && height > 0, Panic(EGlxPanicIllegalArgument));      
       
   228 
       
   229         if (minmemorytodecode < (freeMemory - KGlxCriticalRAMForPhotos))
       
   230             {
       
   231             GLX_DEBUG1("DecodeImageL:RAM available decoding image");            
       
   232             iBitmap = new (ELeave) CFbsBitmap();
       
   233             iBitmap->Create(ReCalculateSizeL(),
       
   234                     iImageDecoder->FrameInfo().iFrameDisplayMode);
       
   235 #ifdef _DEBUG
       
   236             iStartTime.HomeTime(); // Get home time
       
   237 #endif                          
       
   238             iImageDecoder->Convert(&iStatus, *iBitmap);
       
   239             }
       
   240         else
       
   241             {
       
   242             //case when sufficient memory is not available
       
   243             //request OOM FW to release the required memory
       
   244             GLX_DEBUG1("DecodeImageL:insufficient RAM - request OOM");
       
   245             TInt err = OOMRequestFreeMemoryL(minmemorytodecode);
       
   246             if (err == KErrNoMemory)
       
   247                 {
       
   248                 //if OOM fails, release Photos Cache
       
   249                 GLX_DEBUG1("DecodeImageL:insufficient RAM - OOM failed"
       
   250                         " - request Cache");
       
   251                 MGlxCache* cacheManager = MGlxCache::InstanceL();
       
   252                 cacheManager->ReleaseRAML(ETrue);
       
   253                 cacheManager->Close();
       
   254                 //Try and release memory again
       
   255                 err = OOMRequestFreeMemoryL(minmemorytodecode);
       
   256                 }
       
   257             if (err != KErrNoMemory)
       
   258                 {
       
   259                 GLX_DEBUG1("DecodeImageL:Sufficient RAM available");                
       
   260                 iBitmap = new (ELeave) CFbsBitmap();
       
   261                 iBitmap->Create(ReCalculateSizeL(),
       
   262                         iImageDecoder->FrameInfo().iFrameDisplayMode);
       
   263 #ifdef _DEBUG
       
   264                 iStartTime.HomeTime(); // Get home time
       
   265 #endif                          
       
   266                 iImageDecoder->Convert(&iStatus, *iBitmap);
       
   267                 }
       
   268             else
       
   269                 {
       
   270                 GLX_DEBUG1("NOT ENOUGH MEMORY - "
       
   271                         "Using the Fullscreen Thumbnail For Zoom");
       
   272                 //release the file held by decoder immediately.
       
   273                 iImageDecoder->Cancel();
       
   274                 delete iImageDecoder;
       
   275                 iImageDecoder = NULL;
       
   276                 //Inform the client that there is no decode happened and there we take care 
       
   277                 //of showing the fullscreen thumbnail.
       
   278                 iObserver->HandleBitmapDecodedL(iThumbnailIndex, NULL);
       
   279                 return;
       
   280                 }
       
   281             }
       
   282 
       
   283         SetActive();
       
   284         }
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // RunL
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void CGlxBitmapDecoderWrapper::RunL()
       
   292     {
       
   293     TRACER("CGlxBitmapDecoderWrapper::RunL()");
       
   294     if( iStatus == KErrNone )
       
   295         {
       
   296         iObserver->HandleBitmapDecodedL(iThumbnailIndex,iBitmap);
       
   297         iBitmap = NULL;
       
   298 
       
   299         //release the file held by decoder immediately.
       
   300         GLX_DEBUG1("CGlxBitmapDecoderWrapper::RunL:Decoding Finished");
       
   301         iImageDecoder->Cancel();
       
   302         delete iImageDecoder;
       
   303         iImageDecoder = NULL;
       
   304 #ifdef _DEBUG
       
   305         iStopTime.HomeTime();
       
   306         GLX_DEBUG2("*** Image Decode took <%d> us ***", 
       
   307                        (TInt)iStopTime.MicroSecondsFrom(iStartTime).Int64());            
       
   308 #endif                          
       
   309 
       
   310         }
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // DoCancel
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 void CGlxBitmapDecoderWrapper::DoCancel()
       
   318     {
       
   319     TRACER("CGlxBitmapDecoderWrapper::DoCancel ");
       
   320     if(iImageDecoder)
       
   321         {
       
   322         GLX_DEBUG1("CGlxBitmapDecoderWrapper::DoCancel iImageDecoder delete");
       
   323         iImageDecoder->Cancel();
       
   324         delete iImageDecoder;
       
   325         iImageDecoder = NULL;
       
   326         }
       
   327     if(iBitmap)
       
   328         {
       
   329         delete iBitmap;
       
   330         iBitmap = NULL;
       
   331         }
       
   332     }
       
   333 
       
   334 // ---------------------------------------------------------------------------
       
   335 // OOMRequestFreeMemoryL
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 TInt CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL(TInt aBytesRequested)
       
   339     {
       
   340     TRACER("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL");
       
   341     GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL() "
       
   342             "aBytesRequested=%d", aBytesRequested);
       
   343     ROomMonitorSession oomMonitor;
       
   344     User::LeaveIfError(oomMonitor.Connect());
       
   345     // No leaving code after this point, so no need to use cleanup stack
       
   346     // for oomMonitor
       
   347     TInt errorCode = oomMonitor.RequestFreeMemory(aBytesRequested);
       
   348     GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL(1) "
       
   349             "errorCode=%d",errorCode);
       
   350     if (errorCode != KErrNone)
       
   351         {
       
   352         // try one more time 
       
   353         errorCode = oomMonitor.RequestFreeMemory(aBytesRequested);
       
   354         GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL(2) "
       
   355                 "errorCode=%d",errorCode);
       
   356         }
       
   357     oomMonitor.Close();
       
   358     return errorCode;
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // DoesMimeTypeNeedsRecalculateL()
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 TBool CGlxBitmapDecoderWrapper::DoesMimeTypeNeedsRecalculateL()
       
   366     {
       
   367     TRACER("CGlxBitmapDecoderWrapper::DoesMimeTypeNeedsRecalculateL");
       
   368     RApaLsSession session;
       
   369     TDataType mimeType;
       
   370     TUid uid;
       
   371 
       
   372     User::LeaveIfError(session.Connect());
       
   373     CleanupClosePushL(session);
       
   374     User::LeaveIfError(session.AppForDocument(iImagePath->Des(), uid,
       
   375             mimeType));
       
   376     CleanupStack::PopAndDestroy(&session);
       
   377 
       
   378     if (mimeType.Des().Compare(KMimeJpeg) == 0 || mimeType.Des().Compare(
       
   379             KMimeJpg) == 0)
       
   380         {
       
   381         GLX_LOG_INFO("CGlxBitmapDecoderWrapper::DoesMimeTypeNeedsRecalculateL - jpeg");
       
   382         return EFalse;
       
   383         }
       
   384     else
       
   385         {
       
   386         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::DoesMimeTypeNeedsRecalculateL - non jpeg");
       
   387         return ETrue;
       
   388         }
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // ReCalculateSize 
       
   393 // -----------------------------------------------------------------------------
       
   394 TSize CGlxBitmapDecoderWrapper::ReCalculateSizeL()
       
   395     {
       
   396     TRACER("CGlxBitmapDecoderWrapper::ReCalculateSizeL()");
       
   397     if (DoesMimeTypeNeedsRecalculateL())
       
   398         {
       
   399         TSize fullFrameSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   400         // calculate the reduction factor on what size we need
       
   401         TInt reductionFactor = iImageDecoder->ReductionFactor(fullFrameSize,
       
   402                 iTargetBitmapSize);
       
   403         // get the reduced size onto destination size
       
   404         TSize destSize;
       
   405         User::LeaveIfError(iImageDecoder->ReducedSize(fullFrameSize,
       
   406                 reductionFactor, destSize));
       
   407         GLX_LOG_INFO2("CGlxBitmapDecoderWrapper::ReCalculateSizeL() "
       
   408                         "destSize=%d, %d",destSize.iWidth,destSize.iHeight);
       
   409         return destSize;
       
   410         }
       
   411     else
       
   412         {
       
   413         return iTargetBitmapSize;
       
   414         }
       
   415     }