photosgallery/viewframework/texturemanager/src/glxbitmapdecoderwrapper.cpp
changeset 0 4e91876724a2
child 18 bcb43dc84c44
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     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 namespace
       
    33 {
       
    34 const TInt KGlxDecodingThreshold = 3000000;
       
    35 
       
    36 
       
    37 // Photos low_ram_threshold [8.637 MB] as in oomconfig.xml
       
    38 const TInt KGlxCriticalRAMForPhotos = 9056550;
       
    39 const TInt KGlxDecodeBitmapFactor = 3;
       
    40 // All EXIF data is within the first KGlxMaxExifSize bytes of the file
       
    41 const TInt KGlxMaxExifSize = 0x10000;
       
    42 }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Two-phased constructor.
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CGlxBitmapDecoderWrapper* CGlxBitmapDecoderWrapper::NewL(MGlxBitmapDecoderObserver* aObserver)
       
    49     {
       
    50     TRACER("CGlxBitmapDecoderWrapper:: NewL ");
       
    51     CGlxBitmapDecoderWrapper* self = new(ELeave)CGlxBitmapDecoderWrapper();
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL(aObserver);
       
    54     CleanupStack::Pop(self);
       
    55     return self;
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // Constructor
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CGlxBitmapDecoderWrapper::CGlxBitmapDecoderWrapper()
       
    63                          :CActive( EPriorityLow )
       
    64 		        {
       
    65 		        TRACER("CGlxBitmapDecoderWrapper:: NewL ");
       
    66 		        }		
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // Destructor
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 
       
    73 CGlxBitmapDecoderWrapper::~CGlxBitmapDecoderWrapper()
       
    74     {
       
    75     TRACER("CGlxBitmapDecoderWrapper:: ~CGlxBitmapDecoderWrapper ");
       
    76     iFs.Close();
       
    77     Cancel();
       
    78     if (iImageDecoder)
       
    79         {
       
    80         delete iImageDecoder;
       
    81         iImageDecoder = NULL;
       
    82         }
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // ConstructL
       
    87 // ---------------------------------------------------------------------------
       
    88 void CGlxBitmapDecoderWrapper::ConstructL(MGlxBitmapDecoderObserver* aObserver)
       
    89     {
       
    90     TRACER("CGlxBitmapDecoderWrapper::ConstructL ");
       
    91     iObserver = aObserver;
       
    92     User::LeaveIfError(iFs.Connect());
       
    93     CActiveScheduler::Add( this );
       
    94     }
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // DoDecodeImageL
       
    98 // ---------------------------------------------------------------------------		
       
    99 
       
   100 void CGlxBitmapDecoderWrapper::DoDecodeImageL(const TDesC & aSourceFileName,TInt aIndex)
       
   101     {
       
   102     TRACER("CGlxBitmapDecoderWrapper:: DoDecodeImageL ");
       
   103     iThumbnailIndex = aIndex;
       
   104     //Variable used to get the decoder type used;
       
   105     TBool isExtDecoderUsed = ETrue;
       
   106     
       
   107 #ifdef _DEBUG
       
   108     iDecodeProcessstartTime.HomeTime();
       
   109 #endif                          
       
   110 
       
   111     if (iImageDecoder)
       
   112         {
       
   113         delete iImageDecoder;
       
   114         iImageDecoder = NULL;
       
   115         }
       
   116     // Use extended JPEG decoder
       
   117     TRAPD( err, iImageDecoder = CExtJpegDecoder::FileNewL(
       
   118             CExtJpegDecoder::EHwImplementation, iFs, aSourceFileName, CImageDecoder::EOptionNone ) );
       
   119     if ( KErrNone != err )
       
   120         {
       
   121         GLX_LOG_INFO( "DoDecodeImageL:: ESwImplementation" );
       
   122         TRAP(err,iImageDecoder = CExtJpegDecoder::FileNewL(
       
   123                 CExtJpegDecoder::ESwImplementation, iFs, aSourceFileName, CImageDecoder::EOptionNone ) );
       
   124         if ( KErrNone != err )
       
   125             {
       
   126             GLX_LOG_INFO( "DoDecodeImageL:: CImageDecoder" );
       
   127             // Not a JPEG - use standard decoder
       
   128             iImageDecoder = CImageDecoder::FileNewL( iFs, aSourceFileName, CImageDecoder::EOptionNone );
       
   129             isExtDecoderUsed = EFalse;
       
   130             }
       
   131         }
       
   132 #ifdef _DEBUG
       
   133         iStopTime.HomeTime();
       
   134         GLX_DEBUG1("=== DECODER CREATION ===");
       
   135         GLX_DEBUG2("Decoder Creation took <%d> us", 
       
   136                    (TInt)iStopTime.MicroSecondsFrom(iDecodeProcessstartTime).Int64());   
       
   137 #endif                          
       
   138 		
       
   139     TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   140     if(isExtDecoderUsed)
       
   141         {
       
   142         TUint16 orientation=0;
       
   143         //Read the orientation from the Exif header of the image if present
       
   144         TRAPD(error, orientation=GetOrientationL(aSourceFileName));
       
   145         if(KErrNone == error )
       
   146             {
       
   147             //Get the rotation angle and the flip status from orientation
       
   148             TInt rotAngle = 0;
       
   149             TBool flipStatus = EFalse;
       
   150             GetRotationParameters(orientation, rotAngle, flipStatus);
       
   151             //Set the parameters to the decoder
       
   152             CExtJpegDecoder* extDecoder = (CExtJpegDecoder*)iImageDecoder;
       
   153             extDecoder->SetRotationL(rotAngle);
       
   154             if(flipStatus)
       
   155                 {
       
   156                 extDecoder->SetMirroringL();
       
   157                 }
       
   158             //Switch Image Height and width in case orientation > 4 as that 
       
   159             //corresponds to angles 180 and 270 degrees
       
   160             if (orientation > 4)
       
   161                 {
       
   162                 imageSize.SetSize(imageSize.iHeight,imageSize.iWidth);
       
   163                 }
       
   164             }
       
   165 
       
   166         }
       
   167     iOriginalSize.iWidth = imageSize.iWidth;
       
   168     iOriginalSize.iHeight = imageSize.iHeight;
       
   169 
       
   170     if (iBitmap)
       
   171         {
       
   172         delete iBitmap;
       
   173         iBitmap = NULL;
       
   174         }
       
   175     DecodeImageL();
       
   176     }
       
   177 // ---------------------------------------------------------------------------
       
   178 // DecodeImageL
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CGlxBitmapDecoderWrapper::DecodeImageL()
       
   182     {
       
   183     TRACER("CGlxBitmapDecoderWrapper:: DecodeImageL ");
       
   184     TReal32 mFactor = 1;
       
   185     //Set Size according to level and state
       
   186     TReal32 width = iOriginalSize.iWidth;
       
   187     TReal32 height = iOriginalSize.iHeight;
       
   188     GLX_LOG_INFO1("DecodeImageL:width=%f", width);
       
   189     GLX_LOG_INFO1("DecodeImageL:height=%f",height);
       
   190 
       
   191     
       
   192     if ( KGlxDecodingThreshold < (width * height))
       
   193         {
       
   194         mFactor = TReal32(KGlxDecodingThreshold) / (width*height);
       
   195         }
       
   196 
       
   197     // create the destination bitmap
       
   198     if(!iBitmap)
       
   199         {
       
   200         TInt freeMemory = 0;
       
   201         HAL::Get( HALData::EMemoryRAMFree, freeMemory );
       
   202         width*=mFactor;
       
   203         height*=mFactor;
       
   204         TInt minmemorytodecode = KGlxDecodeBitmapFactor*width*height;     
       
   205         GLX_LOG_INFO2("DecodeImageL: after factoring width=%f, height=%f", width, height);
       
   206         GLX_LOG_INFO2("DecodeImageL:minmemorytodecode=%d, freememory=%d",
       
   207                 minmemorytodecode, freeMemory);
       
   208         if(minmemorytodecode < (freeMemory - KGlxCriticalRAMForPhotos))
       
   209             {
       
   210             GLX_LOG_INFO("DecodeImageL:RAM available decoding image");
       
   211 
       
   212             iBitmap = new (ELeave) CFbsBitmap();
       
   213             iBitmap->Create( TSize(width,height),iImageDecoder->FrameInfo().iFrameDisplayMode );
       
   214 #ifdef _DEBUG
       
   215             iStartTime.HomeTime(); // Get home time
       
   216 #endif                          
       
   217             iImageDecoder->Convert( &iStatus, *iBitmap );
       
   218             }
       
   219         else
       
   220             {
       
   221             //case when sufficient memory is not available
       
   222             //request OOM FW to release the required memory
       
   223             GLX_LOG_INFO("DecodeImageL:insufficient RAM - request OOM");
       
   224             TInt err = OOMRequestFreeMemoryL(minmemorytodecode);
       
   225             if(err == KErrNoMemory)
       
   226                 {
       
   227                 //if OOM fails, release Photos Cache
       
   228                 GLX_LOG_INFO("DecodeImageL:insufficient RAM - OOM failed - request Cache");
       
   229                 MGlxCache* cacheManager = MGlxCache::InstanceL();
       
   230                 cacheManager->ReleaseRAML(ETrue);            
       
   231                 cacheManager->Close();
       
   232                 //Try and release memory again
       
   233                 err = OOMRequestFreeMemoryL(minmemorytodecode);
       
   234                 }
       
   235             if(err != KErrNoMemory)
       
   236                 {
       
   237                 GLX_LOG_INFO("DecodeImageL:Sufficient RAM available");
       
   238                 iBitmap = new (ELeave) CFbsBitmap();
       
   239                 iBitmap->Create( TSize(width,height),iImageDecoder->FrameInfo().iFrameDisplayMode );
       
   240 #ifdef _DEBUG
       
   241 	            iStartTime.HomeTime(); // Get home time
       
   242 #endif                          
       
   243                 iImageDecoder->Convert( &iStatus, *iBitmap );
       
   244                 }
       
   245             else
       
   246                 {
       
   247                 GLX_LOG_INFO("NOT ENOUGH MEMORY - Using the Fullscreen Thumbnail For Zoom");
       
   248                 //release the file held by decoder immediately.
       
   249                 iImageDecoder->Cancel();
       
   250                 delete iImageDecoder;
       
   251                 iImageDecoder = NULL;
       
   252                 //Inform the client that there is no decode happened and there we take care 
       
   253                 //of showing the fullscreen thumbnail.
       
   254                 iObserver->HandleBitmapDecodedL(iThumbnailIndex,NULL);
       
   255                 return;                
       
   256                 }
       
   257             }
       
   258 
       
   259         SetActive();
       
   260         }
       
   261     }
       
   262 // ---------------------------------------------------------------------------
       
   263 // RunL
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CGlxBitmapDecoderWrapper::RunL()
       
   267     {
       
   268     TRACER("CGlxBitmapDecoderWrapper:: RunL ");
       
   269     if( iStatus == KErrNone )
       
   270         {
       
   271         iObserver->HandleBitmapDecodedL(iThumbnailIndex,iBitmap);
       
   272         iBitmap = NULL;
       
   273 
       
   274         //release the file held by decoder immediately.
       
   275         GLX_LOG_INFO( " CGlxBitmapDecoderWrapper::RunL:Decoding Finished" );
       
   276         iImageDecoder->Cancel();
       
   277         delete iImageDecoder;
       
   278         iImageDecoder = NULL;
       
   279 #ifdef _DEBUG
       
   280         iStopTime.HomeTime();
       
   281         GLX_DEBUG1("=== IMAGE DECODE ===");
       
   282         GLX_DEBUG2("=>Image Decode Took took <%d> us", 
       
   283                        (TInt)iStopTime.MicroSecondsFrom(iStartTime).Int64());            
       
   284 #endif                          
       
   285 
       
   286         }
       
   287     }
       
   288 // ---------------------------------------------------------------------------
       
   289 // DoCancel
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CGlxBitmapDecoderWrapper::DoCancel()
       
   293     {
       
   294     TRACER("CGlxBitmapDecoderWrapper:: DoCancel ");
       
   295     if(iImageDecoder)
       
   296         {
       
   297         GLX_LOG_INFO( " CGlxBitmapDecoderWrapper::DoCancel:Deleting" );
       
   298         iImageDecoder->Cancel();
       
   299         delete iImageDecoder;
       
   300         iImageDecoder = NULL;
       
   301         }
       
   302     if(iBitmap)
       
   303         {
       
   304         delete iBitmap;
       
   305         iBitmap = NULL;
       
   306         }
       
   307     }
       
   308 
       
   309 // ---------------------------------------------------------------------------
       
   310 // GetOrientationL
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 TUint16 CGlxBitmapDecoderWrapper::GetOrientationL(const TDesC& aFileName)
       
   314     {
       
   315     TRACER("CGlxBitmapDecoderWrapper:: GetOrientationL ");
       
   316     //Get Exif Metadata and the orientation tag from the file first
       
   317             RFile file;
       
   318             CleanupClosePushL(file);
       
   319             User::LeaveIfError(file.Open(iFs,
       
   320                     aFileName, EFileRead));
       
   321             TInt size;
       
   322             User::LeaveIfError(file.Size(size));
       
   323             if ( KGlxMaxExifSize < size )
       
   324                 {
       
   325                 size = KGlxMaxExifSize;
       
   326                 }
       
   327             TUint16 orientation = 9;
       
   328             HBufC8* exifData = HBufC8::NewLC(size);
       
   329             TPtr8 ptr(exifData->Des());
       
   330             User::LeaveIfError(file.Read(ptr));
       
   331             CExifRead* exifReader = NULL;
       
   332             TRAPD(exifErr,exifReader = CExifRead::NewL(*exifData, CExifRead::ENoJpeg));
       
   333             if(exifErr == KErrNone)
       
   334                 {
       
   335                 CleanupStack::PushL(exifReader);
       
   336 
       
   337                 TInt readErr = exifReader->GetOrientation(orientation);
       
   338                 if(readErr != KErrNone)
       
   339                     {
       
   340                     orientation = 9;
       
   341                     }
       
   342                 CleanupStack::PopAndDestroy(exifReader);
       
   343                 }
       
   344             CleanupStack::PopAndDestroy(exifData);
       
   345             //Close and pop file Session
       
   346             CleanupStack::PopAndDestroy(&file);
       
   347             return orientation;
       
   348     
       
   349     }
       
   350 // ---------------------------------------------------------------------------
       
   351 // DoCancel
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 void CGlxBitmapDecoderWrapper::GetRotationParameters(TUint16 aOrientation, TInt& aRotAngle, TBool& aFlipStatus) 
       
   355     {
       
   356     TRACER("CGlxBitmapDecoderWrapper:: DoCancel ");
       
   357     //Get the orientation and set rotation on the decoder 
       
   358     //as well as update the original size
       
   359     aRotAngle =  0;
       
   360     aFlipStatus = EFalse;
       
   361     TInt isOrientationOdd = aOrientation%2;
       
   362     if(aOrientation>8)
       
   363         {
       
   364         return;
       
   365         }
       
   366     if(aOrientation >= 3 && aOrientation < 5)
       
   367         {
       
   368         aRotAngle = 180;
       
   369         }
       
   370     else if(aOrientation >= 5 && aOrientation < 7)
       
   371         {
       
   372         aRotAngle = 90;
       
   373 
       
   374         }
       
   375     else if(aOrientation >= 7 && aOrientation <= 8)
       
   376         {
       
   377         aRotAngle = 270;
       
   378         }
       
   379     if(aOrientation>4 )
       
   380         {
       
   381         if(isOrientationOdd )
       
   382             {
       
   383             aFlipStatus = ETrue;
       
   384             }
       
   385         }   
       
   386 
       
   387     }
       
   388 
       
   389 // ---------------------------------------------------------------------------
       
   390 // OOMRequestFreeMemoryL
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 TInt CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL( TInt aBytesRequested)
       
   394     {
       
   395     TRACER("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL");
       
   396     GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL() aBytesRequested=%d",
       
   397             aBytesRequested);
       
   398     ROomMonitorSession oomMonitor;
       
   399     User::LeaveIfError( oomMonitor.Connect() );
       
   400     // No leaving code after this point, so no need to use cleanup stack
       
   401     // for oomMonitor
       
   402     TInt errorCode = oomMonitor.RequestFreeMemory( aBytesRequested );
       
   403     GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL(1) errorCode=%d",errorCode);
       
   404     if ( errorCode != KErrNone )
       
   405         {
       
   406         // try one more time 
       
   407         errorCode = oomMonitor.RequestFreeMemory( aBytesRequested );
       
   408         GLX_LOG_INFO1("CGlxBitmapDecoderWrapper::OOMRequestFreeMemoryL(2) errorCode=%d",errorCode);
       
   409         }
       
   410     oomMonitor.Close();
       
   411     return errorCode;
       
   412     }