tvout/tvoutengine/src/glxhdmisurfaceupdater.cpp
changeset 26 c499df2dbb33
child 36 6481344a6d67
equal deleted inserted replaced
24:99ad1390cd33 26:c499df2dbb33
       
     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:    
       
    15 *
       
    16 */
       
    17 
       
    18 #include <graphics/surface.h>
       
    19 #include <graphics/surfacemanager.h>
       
    20 #include <graphics/surfaceupdateclient.h>
       
    21 #include <e32math.h>
       
    22 #include <apgcli.h>
       
    23 
       
    24 #include <imageconversion.h> 
       
    25 #include <fbs.h>
       
    26 #include <glxtracer.h>
       
    27 #include <glxlog.h>
       
    28 
       
    29 #include "glxactivecallback.h"
       
    30 #include "glxhdmisurfaceupdater.h"
       
    31 #include "glxactivedecoder.h"
       
    32 #include <bitdev.h> 
       
    33 
       
    34 const TInt KMulFactorToCreateBitmap = 4;
       
    35 const TInt KZoomDelay = 10000;
       
    36 //100 , is decide for 20 steps of zooming , with each step being 5 pixels.
       
    37 const TInt KMaxZoomLimit = 100;
       
    38 //evey time we zoom , there is a increase in the ht amd width by 10 pixels.
       
    39 const TInt KSingleStepForZoom = 10;
       
    40 
       
    41 _LIT(KMimeJpeg,"image/jpeg");
       
    42 _LIT(KMimeJpg,"image/jpg");
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // NewLC
       
    46 // -----------------------------------------------------------------------------
       
    47 CGlxHdmiSurfaceUpdater* CGlxHdmiSurfaceUpdater::NewL(RWindow* aWindow, const TDesC& aImageFile, 
       
    48                                                       CFbsBitmap* aFsBitmap, 
       
    49                                                       MGlxGenCallback* aCallBack)
       
    50     {
       
    51     TRACER("CGlxHdmiSurfaceUpdater* CGlxHdmiSurfaceUpdater::NewL()");
       
    52     CGlxHdmiSurfaceUpdater* self = new (ELeave) CGlxHdmiSurfaceUpdater(aWindow, aCallBack);
       
    53     CleanupStack::PushL(self);
       
    54     self->ConstructL(aFsBitmap,aImageFile);
       
    55     CleanupStack::Pop(self);
       
    56     return self;
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // destructor 
       
    61 // -----------------------------------------------------------------------------
       
    62 CGlxHdmiSurfaceUpdater::~CGlxHdmiSurfaceUpdater()
       
    63     {
       
    64     TRACER("CGlxHdmiSurfaceUpdater::~CGlxHdmiSurfaceUpdater()");
       
    65     ReleaseContent();
       
    66     if(iWindow)
       
    67         {
       
    68         iWindow->RemoveBackgroundSurface(ETrue);
       
    69         }
       
    70 	if(iTimer && iTimer->IsActive())	 	 	 // Check for a CPeriodic Instance
       
    71         {
       
    72         iTimer->Cancel();
       
    73         }
       
    74     delete iTimer;   
       
    75     if (iGlxDecoderAO)
       
    76         {
       
    77         delete iGlxDecoderAO;
       
    78         }        
       
    79     iGlxDecoderAO = NULL;
       
    80     iFsSession.Close();
       
    81     if (iSurfManager)
       
    82         {
       
    83         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::~CGlxHdmiSurfaceUpdater() - Close"); 
       
    84         if(iSurfSessionConnected)
       
    85             {
       
    86             iSurfUpdateSession.Close();
       
    87             }
       
    88         if (iSurfChunk)
       
    89             {
       
    90             iSurfChunk->Close();
       
    91             }            
       
    92         delete iSurfChunk;
       
    93         iSurfChunk = NULL;        
       
    94         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::~CGlxHdmiSurfaceUpdater(). iSurfManager->CloseSurface()"); 
       
    95         iSurfManager->CloseSurface(iSurfId);
       
    96         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::~CGlxHdmiSurfaceUpdater(). iSurfManager->Close()"); 
       
    97         iSurfManager->Close();
       
    98         delete iSurfManager;
       
    99         iSurfManager = NULL;       
       
   100         }
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // ReleaseContent 
       
   105 // -----------------------------------------------------------------------------
       
   106 void CGlxHdmiSurfaceUpdater::ReleaseContent()
       
   107     {
       
   108     TRACER("void CGlxHdmiSurfaceUpdater::ReleaseContent()"); 
       
   109     if ( iGlxDecoderAO )
       
   110         {
       
   111         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -1");
       
   112         iGlxDecoderAO->Cancel();
       
   113         }
       
   114     if (iFsBitmap)
       
   115         {
       
   116         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -2");
       
   117         delete iFsBitmap;
       
   118         iFsBitmap = NULL;
       
   119         }
       
   120     for (TInt i=0; i<=2 ; i++)
       
   121         {
       
   122         GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::ReleaseContent() -3, %d", i);
       
   123         delete iDecodedBitmap[i];
       
   124         iDecodedBitmap[i]= NULL;
       
   125         }
       
   126     GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -4");
       
   127     if(iSurfBufferAO && iSurfBufferAO->IsActive())
       
   128         {
       
   129         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -5");
       
   130 		iSurfBufferAO->Cancel();
       
   131         }
       
   132     if ( iImageDecoder )
       
   133         {
       
   134         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -6");
       
   135         delete iImageDecoder;
       
   136         iImageDecoder = NULL;    
       
   137         }
       
   138     if (iSurfSessionConnected &&  iSurfManager)
       
   139         {
       
   140         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ReleaseContent() -7");
       
   141         iSurfUpdateSession.CancelAllUpdateNotifications();
       
   142         }
       
   143     if (iImagePath)
       
   144         {
       
   145         delete iImagePath;
       
   146         iImagePath = NULL;
       
   147         }
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CTor 
       
   152 // -----------------------------------------------------------------------------
       
   153 CGlxHdmiSurfaceUpdater::CGlxHdmiSurfaceUpdater(RWindow* aWindow,
       
   154                           MGlxGenCallback* aCallBack): 
       
   155                           iWindow(aWindow), iCallBack(aCallBack), 
       
   156                           iShwFsThumbnail(ETrue), iIsNonJpeg(EFalse)
       
   157     {
       
   158     TRACER("CGlxHdmiSurfaceUpdater::CGlxHdmiSurfaceUpdater()");
       
   159     // Implement nothing here
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // ConstructL 
       
   164 // -----------------------------------------------------------------------------
       
   165 void CGlxHdmiSurfaceUpdater::ConstructL(CFbsBitmap* aFsBitmap, const TDesC& aImageFile)
       
   166     {
       
   167     TRACER("CGlxHdmiSurfaceUpdater::ConstructL()");
       
   168     // Initiate the HDMI by assigning the necessary values
       
   169     InitiateHdmiL(aFsBitmap,aImageFile);
       
   170     
       
   171     TInt error = iFsSession.Connect ();
       
   172     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::ConstructL() FsSession Connect error = %d", error);
       
   173     User::LeaveIfError(error);
       
   174     iShiftToCloning = EFalse;
       
   175     // Create the active object
       
   176     iGlxDecoderAO = CGlxHdmiDecoderAO::NewL(this);
       
   177 
       
   178     // Create the HDMI Decoder
       
   179     CreateImageDecoderL();
       
   180 
       
   181     // Create the Bitmap
       
   182     CreateBitmapL();
       
   183 
       
   184     // Create the surface and AO for updating the surface
       
   185     TRAP_IGNORE(CreateHdmiL());
       
   186 
       
   187     if(iSurfManager)
       
   188         {
       
   189         error = iSurfUpdateSession.Connect();
       
   190         GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::ConstructL() Surface update Session Connect error = %d", error);
       
   191         User::LeaveIfError(error);
       
   192         iSurfSessionConnected = ETrue;
       
   193 
       
   194         ShowFsThumbnailL();
       
   195 #ifdef _DEBUG
       
   196         iStartTime.HomeTime();
       
   197 #endif
       
   198         //start decoding the image    
       
   199         iGlxDecoderAO->ConvertImageL(*iDecodedBitmap[EJpgDecodedBitmapIndex],iImageDecoder);   
       
   200         }
       
   201     iTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // UpdateNewImageL 
       
   206 // -----------------------------------------------------------------------------
       
   207 void CGlxHdmiSurfaceUpdater::UpdateNewImageL(const TDesC& aImageFile,CFbsBitmap* aFsBitmap)
       
   208     {
       
   209     TRACER("CGlxHdmiSurfaceUpdater::UpdateNewImageL()");
       
   210     ReleaseContent();   
       
   211     // Initiate the HDMI by assigning the necessary values
       
   212     InitiateHdmiL(aFsBitmap,aImageFile);
       
   213     //Cancel the zoom timers if any
       
   214     if(iTimer->IsActive())
       
   215         {
       
   216         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::UpdateNewImageL() - Cancel Timer");
       
   217         iTimer->Cancel();
       
   218         }
       
   219 
       
   220     // Create the HDMI Decoder
       
   221     CreateImageDecoderL();
       
   222 
       
   223     // Create the Bitmap
       
   224     CreateBitmapL();
       
   225     if(iSurfManager)
       
   226         {
       
   227         // Create the surface and AO for updating the surface
       
   228         CreateHdmiL(EFalse);
       
   229         }
       
   230     else
       
   231         {
       
   232         TRAP_IGNORE(CreateHdmiL(ETrue));
       
   233         }
       
   234 
       
   235 #ifdef _DEBUG
       
   236     iStartTime.HomeTime();
       
   237 #endif
       
   238     if(iSurfManager)
       
   239         {
       
   240         // Show FS Thumbnail
       
   241         ShowFsThumbnailL();
       
   242         //start decoding the image
       
   243         iGlxDecoderAO->ConvertImageL(*iDecodedBitmap[EJpgDecodedBitmapIndex],iImageDecoder);
       
   244         }
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // InitiateHdmiL 
       
   249 // -----------------------------------------------------------------------------
       
   250 void CGlxHdmiSurfaceUpdater::InitiateHdmiL(CFbsBitmap* aFsBitmap,const TDesC& aImageFile)
       
   251     {
       
   252     TRACER("CGlxHdmiSurfaceUpdater::InitiateHdmiL()");
       
   253     iImagePath = aImageFile.Alloc();
       
   254     iFsBitmap = new (ELeave) CFbsBitmap;
       
   255     iFsBitmap->Duplicate(aFsBitmap->Handle());
       
   256     iIsNonJpeg = EFalse;
       
   257     iTvScreenSize = iWindow->Size();
       
   258 
       
   259     iBitmapReady = EFalse;
       
   260     iLeftCornerForZoom.iX = 0; 
       
   261     iLeftCornerForZoom.iY = 0;
       
   262 
       
   263     iZoom = ETrue;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CreateHDMI 
       
   268 // -----------------------------------------------------------------------------
       
   269 void CGlxHdmiSurfaceUpdater::CreateHdmiL(TBool aCreateSurface)
       
   270     {
       
   271     TRACER("CGlxHdmiSurfaceUpdater::CreateHdmiL()");
       
   272     
       
   273     if (aCreateSurface)
       
   274         {
       
   275         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::CreateHdmiL() Create Surface");
       
   276         CreateSurfaceL();        
       
   277         }
       
   278     
       
   279     // Active objects for double buffered draw signalling
       
   280     if(!iSurfBufferAO)
       
   281         {
       
   282         iSurfBufferAO = new(ELeave) CGlxActiveCallBack(TCallBack(SurfBuffer0Ready, this),
       
   283                     CActive::EPriorityStandard-1);
       
   284         CActiveScheduler::Add(iSurfBufferAO);    
       
   285         }
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CreateSurfaceL 
       
   290 // -----------------------------------------------------------------------------
       
   291 void CGlxHdmiSurfaceUpdater::CreateSurfaceL()
       
   292     {
       
   293     TRACER("CGlxHdmiSurfaceUpdater::CreateSurfaceL()");
       
   294     iSurfManager = new(ELeave) RSurfaceManager();
       
   295     TInt error = iSurfManager->Open();
       
   296     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::CreateSurfaceL Open Surface manager error = %d", error);
       
   297     User::LeaveIfError(error);
       
   298     
       
   299     RSurfaceManager::TSurfaceCreationAttributesBuf attributes;
       
   300     attributes().iPixelFormat           = EUidPixelFormatARGB_8888;// EUidPixelFormatYUV_420Planar;
       
   301     attributes().iSize                  = iTvScreenSize;
       
   302     
       
   303     attributes().iBuffers               = 1;
       
   304     attributes().iStride                = iTvScreenSize.iWidth * KMulFactorToCreateBitmap;  
       
   305     attributes().iAlignment             = KMulFactorToCreateBitmap;
       
   306     attributes().iContiguous            = EFalse;
       
   307     attributes().iMappable              = ETrue;
       
   308         
       
   309     error = iSurfManager->CreateSurface(attributes, iSurfId);
       
   310     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::CreateSurfaceL, Creating surface error : %d",error);
       
   311     if(error == KErrNoMemory)
       
   312     	{
       
   313     	GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::CreateSurfaceL(). iSurfManager->Close()"); 
       
   314 		iSurfManager->Close();
       
   315 		delete iSurfManager;
       
   316 		iSurfManager = NULL;
       
   317     	if(iShiftToCloning == EFalse)
       
   318     		{
       
   319     		// Show Error Note
       
   320     		iShiftToCloning = ETrue;
       
   321     		}
       
   322     	ShiftToCloningMode();	 	 	 	 	 	 	  // Shift from Posting Mode to Cloning Mode
       
   323     	}
       
   324     User::LeaveIfError(error);
       
   325         
       
   326     //Map the surface and stride the surface info
       
   327     MapSurfaceL();
       
   328     // Set the Configuration to the surface ID when creating a surface
       
   329     iConfig.SetSurfaceId(iSurfId);
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // MapSurfaceL 
       
   334 // -----------------------------------------------------------------------------
       
   335 void CGlxHdmiSurfaceUpdater::MapSurfaceL()
       
   336     {
       
   337     TRACER("CGlxHdmiSurfaceUpdater::MapSurfaceL()");
       
   338     
       
   339     //Create chunk to map it to the surface ID.
       
   340     iSurfChunk = new(ELeave) RChunk();
       
   341     User::LeaveIfNull(iSurfChunk);    
       
   342     TInt error = iSurfManager->MapSurface(iSurfId, *iSurfChunk);
       
   343     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::MapSurfaceL(), MapSurface error : %d",error);
       
   344     User::LeaveIfError(error);
       
   345     
       
   346     // Get the info from the surfaceManager
       
   347     // and store pointers to the pixel data
       
   348     RSurfaceManager::TInfoBuf info;
       
   349     error = iSurfManager->SurfaceInfo(iSurfId, info);
       
   350     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::MapSurfaceL(), SurfaceInfo error : %d",error);
       
   351     User::LeaveIfError(error);  
       
   352     
       
   353     iSurfaceStride = info().iStride;    
       
   354     TInt offset = 0;
       
   355     iSurfManager->GetBufferOffset( iSurfId, 0,offset);
       
   356     iSurfBuffer = iSurfChunk->Base()+offset;
       
   357     }
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // SurfBuffer0Ready 
       
   361 // -----------------------------------------------------------------------------
       
   362 TInt CGlxHdmiSurfaceUpdater::SurfBuffer0Ready(TAny* /*aObject*/)
       
   363     {
       
   364     TRACER("CGlxHdmiSurfaceUpdater::SurfBuffer0Ready()");
       
   365     return ETrue;
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // Refresh 
       
   370 // -----------------------------------------------------------------------------
       
   371 void CGlxHdmiSurfaceUpdater::Refresh()
       
   372     {
       
   373     TRACER("CGlxHdmiSurfaceUpdater::Refresh()");
       
   374     // copy the decoded bitmap on to the surface
       
   375     SwapBuffers();     
       
   376 
       
   377     // refresh the window
       
   378     iCallBack->DoGenCallback();       
       
   379     }
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // SwapBuffers
       
   383 // This is used to sawp the buffers shown and to be shown 
       
   384 // After this is done, a refresh to the window should be done to refresh the TV
       
   385 // -----------------------------------------------------------------------------
       
   386 void CGlxHdmiSurfaceUpdater::SwapBuffers()
       
   387     {
       
   388     TRACER("CGlxHdmiSurfaceUpdater::SwapBuffers()"); 
       
   389     TInt bitmapIndex = KErrNotFound;
       
   390     
       
   391     if (iShwFsThumbnail) // show FS thumbnail
       
   392         {
       
   393         bitmapIndex = EFSBitmapIndex;
       
   394         }
       
   395     else if(iIsNonJpeg && !iShwFsThumbnail) // this indicates it is non jpeg image and decoding finished
       
   396         {
       
   397         bitmapIndex = ENonJpgDecodedBitmapIndex;
       
   398         }
       
   399     else                // if none of the above state means it is jpeg image and decoding finished
       
   400         {
       
   401         bitmapIndex = EJpgDecodedBitmapIndex;
       
   402         }
       
   403         
       
   404     // Lock the heap so that subsequent call to dataaddress doesnt happen
       
   405     iDecodedBitmap[bitmapIndex]->LockHeap();    
       
   406 
       
   407     // Data stride
       
   408     TUint fs = iDecodedBitmap[bitmapIndex]->DataStride();    
       
   409 
       
   410     //Bitmap address from where the data has to be copied.
       
   411     TUint8* from = (TUint8*)iDecodedBitmap[bitmapIndex]->DataAddress();
       
   412 
       
   413     //surface chunk address to where the bitmap data has to be copied.
       
   414     TUint8* to = (TUint8*)iSurfBuffer;    
       
   415 
       
   416     // To buffer (32 bit colors)
       
   417     TUint ts = iSurfaceStride;
       
   418     //No of bytes to be copied on to the surface.
       
   419     TUint bytes = iDecodedBitmap[bitmapIndex]->SizeInPixels().iWidth * KMulFactorToCreateBitmap;
       
   420 
       
   421     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::SwapBuffers() - decodeSize width = %d and height %d",
       
   422             iDecodedBitmap[bitmapIndex]->SizeInPixels().iWidth, 
       
   423             iDecodedBitmap[bitmapIndex]->SizeInPixels().iHeight );
       
   424 
       
   425     // Copy the bitmap on to the surface.
       
   426     for (TInt y = iDecodedBitmap[bitmapIndex]->SizeInPixels().iHeight; y >0; y--)
       
   427         {
       
   428         Mem::Copy(to, from, bytes);        
       
   429         to += ts;        
       
   430         from += fs;        
       
   431         }            
       
   432     iDecodedBitmap[bitmapIndex]->UnlockHeap();
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CreateBitmapL 
       
   437 // -----------------------------------------------------------------------------
       
   438 void CGlxHdmiSurfaceUpdater::CreateBitmapL()
       
   439     {
       
   440     TRACER("CGlxHdmiSurfaceUpdater::CreateBitmapL()");
       
   441     TSize imageSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   442     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::StartImageDecodeL() - bitmapsize=%d, %d",
       
   443             imageSize.iWidth,imageSize.iHeight);
       
   444     TReal32 scaleFactor = 0.0f;
       
   445     if (iTvScreenSize.iWidth * imageSize.iHeight > iTvScreenSize.iHeight
       
   446             * imageSize.iWidth)
       
   447         {
       
   448         scaleFactor = (TReal32) iTvScreenSize.iHeight
       
   449                 / (TReal32) imageSize.iHeight;
       
   450         }
       
   451     else
       
   452         {
       
   453         scaleFactor = (TReal32) iTvScreenSize.iWidth
       
   454                 / (TReal32) imageSize.iWidth;
       
   455         }
       
   456     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::StartImageDecodeL() - scaleFactor=%f",scaleFactor);
       
   457     iTargetBitmapSize.iHeight = imageSize.iHeight * scaleFactor;
       
   458     iTargetBitmapSize.iWidth = imageSize.iWidth * scaleFactor;
       
   459 	GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::StartImageDecodeL() - targetBitmapSize=%d, %d",
       
   460 	        iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   461     //create the bitmap for the required size
       
   462     iDecodedBitmap[EJpgDecodedBitmapIndex] = new (ELeave) CFbsBitmap();
       
   463     // to check if the Mimetype is supported by the decoder to to arbitary scaling
       
   464     // if not then we recalculate the size
       
   465     TInt err = iDecodedBitmap[EJpgDecodedBitmapIndex]->Create(ReCalculateSizeL(), EColor16MU);
       
   466     User::LeaveIfNull(iDecodedBitmap[EJpgDecodedBitmapIndex]);
       
   467     }
       
   468 
       
   469 // -----------------------------------------------------------------------------
       
   470 // DoesMimeTypeNeedsRecalculateL()
       
   471 // -----------------------------------------------------------------------------
       
   472 //
       
   473 TBool CGlxHdmiSurfaceUpdater::DoesMimeTypeNeedsRecalculateL()
       
   474     {
       
   475     TRACER("CGlxHdmiSurfaceUpdater::DoesMimeTypeNeedsRecalculateL");
       
   476     RApaLsSession session;
       
   477     TDataType mimeType;
       
   478     TUid uid;
       
   479     
       
   480     User::LeaveIfError( session.Connect() );
       
   481     CleanupClosePushL( session );
       
   482     User::LeaveIfError( session.AppForDocument( iImagePath->Des(), uid, mimeType ) );
       
   483     CleanupStack::PopAndDestroy(&session);
       
   484     
       
   485     if (mimeType.Des().Compare(KMimeJpeg)==0 ||
       
   486             mimeType.Des().Compare(KMimeJpg)==0)
       
   487         {
       
   488         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::DoesMimeTypeNeedsRecalculateL - jpeg");
       
   489         return EFalse;
       
   490         }
       
   491     else
       
   492         {
       
   493         GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::DoesMimeTypeNeedsRecalculateL - non jpeg");
       
   494         return ETrue; 
       
   495         }
       
   496     }
       
   497 
       
   498 // -----------------------------------------------------------------------------
       
   499 // ReCalculateSize 
       
   500 // -----------------------------------------------------------------------------
       
   501 TSize CGlxHdmiSurfaceUpdater::ReCalculateSizeL()
       
   502     {
       
   503     TRACER("CGlxHdmiSurfaceUpdater::ReCalculateSizeL()");
       
   504     if(DoesMimeTypeNeedsRecalculateL())
       
   505         {
       
   506         iIsNonJpeg = ETrue;
       
   507         TSize fullFrameSize = iImageDecoder->FrameInfo().iOverallSizeInPixels;
       
   508         // calculate the reduction factor on what size we need
       
   509         TInt reductionFactor = iImageDecoder->ReductionFactor(fullFrameSize, iTargetBitmapSize);
       
   510         // get the reduced size onto destination size
       
   511         TSize destSize;
       
   512         User::LeaveIfError(iImageDecoder->ReducedSize(fullFrameSize, reductionFactor, destSize));
       
   513         GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ReCalculateSizeL() - destSize=%d, %d",destSize.iWidth,destSize.iHeight);
       
   514         return destSize;
       
   515         }
       
   516     else
       
   517         {
       
   518         return iTargetBitmapSize;
       
   519         }
       
   520     }
       
   521 
       
   522 // -----------------------------------------------------------------------------
       
   523 // HandleRunL 
       
   524 // -----------------------------------------------------------------------------
       
   525 void CGlxHdmiSurfaceUpdater::HandleRunL(TRequestStatus& aStatus)
       
   526     {
       
   527     TRACER("CGlxHdmiSurfaceUpdater::HandleRunL()");
       
   528 
       
   529 #ifdef _DEBUG
       
   530     iStopTime.HomeTime();
       
   531     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::HandleRunL() ConvertImageL took"
       
   532             " <%d> us", (TInt)iStopTime.MicroSecondsFrom(iStartTime).Int64());
       
   533 #endif
       
   534     if(aStatus.Int() !=KErrNone)
       
   535         {
       
   536         GLX_LOG_INFO("HandleRunL - Convert failed");
       
   537         ShiftToCloningMode();
       
   538         }
       
   539     else if(iSurfManager)
       
   540         {        
       
   541         iShwFsThumbnail = EFalse;
       
   542         if (iIsNonJpeg)
       
   543             {
       
   544             // if this is non jpeg image, make sure we scale the bitmap
       
   545             ScaleDecodedBitmapL(ENonJpgDecodedBitmapIndex);
       
   546             }
       
   547         ProcessTvImage();
       
   548 		iBitmapReady = ETrue;
       
   549         }
       
   550 	//release imagedecoder after the conversion is over		
       
   551     if(iImageDecoder)
       
   552         {
       
   553         delete iImageDecoder;
       
   554         iImageDecoder = NULL;    
       
   555         }
       
   556     }
       
   557 
       
   558 // -----------------------------------------------------------------------------
       
   559 // CreateImageDecoderL 
       
   560 // -----------------------------------------------------------------------------
       
   561 void CGlxHdmiSurfaceUpdater::CreateImageDecoderL()
       
   562     {
       
   563     TRACER("CGlxHdmiController::CreateImageDecoderL()");
       
   564      // Create a decoder for the image in the named file
       
   565     TRAPD(error,iImageDecoder = CImageDecoder::FileNewL(iFsSession, 
       
   566             iImagePath->Des(), CImageDecoder::EOptionNone, KNullUid));
       
   567     GLX_LOG_INFO1("CreateImageDecoderL CImageDecoder:FileNewL error %d",error);
       
   568     User::LeaveIfError(error);
       
   569     }
       
   570 
       
   571 // -----------------------------------------------------------------------------
       
   572 // ActivateZoom 
       
   573 // -----------------------------------------------------------------------------
       
   574 void CGlxHdmiSurfaceUpdater::ActivateZoom(TBool aAutoZoomOut)
       
   575     {
       
   576     TRACER("CGlxHdmiSurfaceUpdater::ActivateZoom()");
       
   577     iZoom = ETrue;
       
   578     iAutoZoomOut = aAutoZoomOut;
       
   579     if(iTimer->IsActive())
       
   580         {
       
   581 		GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ActivateZoom() - Cancel Timer");
       
   582         iTimer->Cancel();
       
   583         }
       
   584     
       
   585     if(!iTimer->IsActive() && iBitmapReady)
       
   586        {
       
   587 	   GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::ActivateZoom() - Start Timer");
       
   588        iTimer->Start(KZoomDelay,KZoomDelay,TCallBack( TimeOut,this ));
       
   589        }
       
   590     }
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // DeactivateZoom 
       
   594 // -----------------------------------------------------------------------------
       
   595 void CGlxHdmiSurfaceUpdater::DeactivateZoom()
       
   596     {
       
   597     TRACER("CGlxHdmiSurfaceUpdater::DeactivateZoom()");
       
   598     
       
   599     if(iTimer->IsActive())
       
   600         {
       
   601 		GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::DeactivateZoom() - Cancel Timer");
       
   602         iTimer->Cancel();
       
   603         }
       
   604     if(!iTimer->IsActive() && iBitmapReady && iLeftCornerForZoom.iX)
       
   605        {
       
   606 	   GLX_LOG_INFO("CGlxHdmiSurfaceUpdater::DeactivateZoom() - Start Timer");
       
   607 	   iZoom = EFalse;
       
   608        iTimer->Start(KZoomDelay,KZoomDelay,TCallBack( TimeOut,this ));
       
   609        }
       
   610     }
       
   611 
       
   612 // ---------------------------------------------------------------------------
       
   613 // TimeOut
       
   614 // ---------------------------------------------------------------------------
       
   615 //  
       
   616 TInt CGlxHdmiSurfaceUpdater::TimeOut(TAny* aSelf)
       
   617     {
       
   618     TRACER("CGlxHdmiSurfaceUpdater::TimeOut");
       
   619     if(aSelf)
       
   620         {
       
   621         CGlxHdmiSurfaceUpdater* self = static_cast <CGlxHdmiSurfaceUpdater*> (aSelf);
       
   622         if (self)
       
   623             {            
       
   624              self->Zoom(ETrue);
       
   625             }
       
   626         }
       
   627     return KErrNone;
       
   628     }
       
   629 
       
   630 // -----------------------------------------------------------------------------
       
   631 // Zoom 
       
   632 // -----------------------------------------------------------------------------
       
   633 void CGlxHdmiSurfaceUpdater::Zoom(TBool aZoom)
       
   634     {
       
   635     TRACER("CGlxHdmiSurfaceUpdater::Zoom()");
       
   636 	
       
   637 	if(!iBitmapReady)
       
   638 		{
       
   639 		return;
       
   640 		}
       
   641 		
       
   642     if(iLeftCornerForZoom.iX == KMaxZoomLimit)
       
   643         {
       
   644         iZoom = EFalse;
       
   645         //If autozoomout is not set then cancel the timer and do
       
   646         //the zoom out on DeactivateZoom.
       
   647         if(!iAutoZoomOut)
       
   648             {
       
   649             iTimer->Cancel();			
       
   650             }
       
   651         }    
       
   652     if(aZoom && iZoom)
       
   653         {
       
   654         iTargetBitmapSize.iWidth = TInt(iTargetBitmapSize.iWidth-KSingleStepForZoom);
       
   655         iTargetBitmapSize.iHeight = TInt(iTargetBitmapSize.iHeight-KSingleStepForZoom);
       
   656         iLeftCornerForZoom.iX =iLeftCornerForZoom.iX+KSingleStepForZoom/2;
       
   657         iLeftCornerForZoom.iY =iLeftCornerForZoom.iY+KSingleStepForZoom/2;           
       
   658         GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::Zoom()--- 2,iTargetBitmapSize.iWidth = %d, iTargetBitmapSize.iHeight = %d", iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   659         iConfig.SetViewport(TRect(iLeftCornerForZoom.iX,iLeftCornerForZoom.iY,
       
   660                 iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight));
       
   661         }
       
   662     else
       
   663         {
       
   664         iTargetBitmapSize.iWidth = TInt(iTargetBitmapSize.iWidth+KSingleStepForZoom);
       
   665         iTargetBitmapSize.iHeight = TInt(iTargetBitmapSize.iHeight+KSingleStepForZoom);
       
   666         iLeftCornerForZoom.iX =iLeftCornerForZoom.iX-KSingleStepForZoom/2;
       
   667         iLeftCornerForZoom.iY =iLeftCornerForZoom.iY-KSingleStepForZoom/2; 
       
   668         if(iLeftCornerForZoom.iX == 0)
       
   669             {
       
   670             iTimer->Cancel();
       
   671             iZoom = ETrue;
       
   672             }
       
   673         GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::Zoom()--- 4,iTargetBitmapSize.iWidth = %d, iTargetBitmapSize.iHeight = %d", iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   674         iConfig.SetViewport(TRect(iLeftCornerForZoom.iX,iLeftCornerForZoom.iY,
       
   675                 iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight));
       
   676         }
       
   677     iWindow->SetBackgroundSurface(iConfig, ETrue);   
       
   678     }
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // ModifySurfacePostion 
       
   682 // -----------------------------------------------------------------------------
       
   683 void CGlxHdmiSurfaceUpdater::ModifySurfacePostion()
       
   684 	{
       
   685 	TRACER("CGlxHdmiSurfaceUpdater::ModifySurfacePostion()");
       
   686 	TPoint startPoint(0,0);
       
   687 	if (iTargetBitmapSize.iWidth <iTvScreenSize.iWidth)
       
   688 	    {
       
   689 	    startPoint.iX = (iTvScreenSize.iWidth - iTargetBitmapSize.iWidth)/2; 
       
   690 	    }
       
   691 	if (iTargetBitmapSize.iHeight <iTvScreenSize.iHeight)
       
   692 	    {
       
   693 	    startPoint.iY = (iTvScreenSize.iHeight - iTargetBitmapSize.iHeight)/2;
       
   694 	    }
       
   695     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - target iTargetBitmapSize=%d, %d",
       
   696             iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   697     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - startPoint =%d, %d",
       
   698             startPoint.iX,startPoint.iY);
       
   699 
       
   700     // target
       
   701     iConfig.SetExtent(TRect(startPoint.iX,startPoint.iY,(iTvScreenSize.iWidth-startPoint.iX),
       
   702             (iTvScreenSize.iHeight-startPoint.iY)));
       
   703     // source
       
   704     iConfig.SetViewport(TRect(TPoint(0,0),TSize(iTargetBitmapSize.iWidth,
       
   705             iTargetBitmapSize.iHeight)));
       
   706 #ifdef _DEBUG
       
   707     TRect ex, vp;
       
   708     iConfig.GetExtent(ex);
       
   709     iConfig.GetViewport(vp);
       
   710     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - vp - TL=%d, %d",vp.iTl.iX,vp.iTl.iY);
       
   711     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - vp - BR=%d, %d",vp.iBr.iX,vp.iBr.iY);
       
   712     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - ex - TL=%d, %d",ex.iTl.iX,ex.iTl.iY);
       
   713     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ModifySurfacePostion() - ex - BR=%d, %d",ex.iBr.iX,ex.iBr.iY);
       
   714 #endif
       
   715     }
       
   716 
       
   717 // -----------------------------------------------------------------------------
       
   718 // ShiftToCloningMode 
       
   719 // -----------------------------------------------------------------------------
       
   720 void CGlxHdmiSurfaceUpdater::ShiftToCloningMode()
       
   721 	{
       
   722 	TRACER("CGlxHdmiSurfaceUpdater::ShiftToCloningMode()");
       
   723 	iWindow->RemoveBackgroundSurface(ETrue);
       
   724 	}
       
   725 	
       
   726 // -----------------------------------------------------------------------------
       
   727 // ShiftToPostingMode 
       
   728 // -----------------------------------------------------------------------------
       
   729 void CGlxHdmiSurfaceUpdater::ShiftToPostingMode()
       
   730 	{
       
   731 	TRACER("CGlxHdmiSurfaceUpdater::ShiftToPostingMode()");
       
   732 	if(iSurfManager)
       
   733 		{
       
   734 #ifdef _DEBUG
       
   735 		TRect ex, vp;
       
   736 		iConfig.GetExtent(ex);
       
   737 		iConfig.GetViewport(vp);
       
   738 		GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ShiftToPostingMode() - vp - TL=%d, %d",vp.iTl.iX,vp.iTl.iY);
       
   739 		GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ShiftToPostingMode() - vp - BR=%d, %d",vp.iBr.iX,vp.iBr.iY);
       
   740 		GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ShiftToPostingMode() - ex - TL=%d, %d",ex.iTl.iX,ex.iTl.iY);
       
   741 		GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ShiftToPostingMode() - ex - BR=%d, %d",ex.iBr.iX,ex.iBr.iY);
       
   742 #endif
       
   743 		iWindow->SetBackgroundSurface(iConfig, ETrue);
       
   744 		iShiftToCloning = EFalse;
       
   745 		}
       
   746 	}
       
   747 
       
   748 // -----------------------------------------------------------------------------
       
   749 // ShowFsThumbnailL 
       
   750 // -----------------------------------------------------------------------------
       
   751 void CGlxHdmiSurfaceUpdater::ShowFsThumbnailL()
       
   752     {
       
   753     TRACER("CGlxHdmiSurfaceUpdater::ShowFsThumbnailL()");
       
   754     iShwFsThumbnail = ETrue;
       
   755     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ShowFsThumbnailL() iTargetBitmapSize Width=%d, Height=%d",
       
   756             iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   757     // Modify the surface position with respect to the buffer size 
       
   758     ModifySurfacePostion();
       
   759     // this is for FS thumbnail stored in array pointer 0
       
   760     ScaleDecodedBitmapL(EFSBitmapIndex);
       
   761     // Process the Image to TV
       
   762     ProcessTvImage();
       
   763     // set the surface onto background
       
   764     iWindow->SetBackgroundSurface(iConfig, ETrue); 
       
   765     }
       
   766 
       
   767 // -----------------------------------------------------------------------------
       
   768 // ScaleDecodedBitmapL 
       
   769 // -----------------------------------------------------------------------------
       
   770 void CGlxHdmiSurfaceUpdater::ScaleDecodedBitmapL(TInt aBitmapIndex)
       
   771     {
       
   772     TRACER("CGlxHdmiSurfaceUpdater::ScaleDecodedBitmapL()");
       
   773     GLX_LOG_INFO2("CGlxHdmiSurfaceUpdater::ScaleDecodedBitmapL() iTargetBitmapSize Width=%d, Height=%d",
       
   774             iTargetBitmapSize.iWidth,iTargetBitmapSize.iHeight);
       
   775 
       
   776     if (iDecodedBitmap[aBitmapIndex])
       
   777         {
       
   778         delete iDecodedBitmap[aBitmapIndex];
       
   779         iDecodedBitmap[aBitmapIndex] = NULL;
       
   780         }
       
   781     iDecodedBitmap[aBitmapIndex] = new (ELeave) CFbsBitmap();
       
   782     // create destination with size
       
   783     TInt err = iDecodedBitmap[aBitmapIndex]->Create(iTargetBitmapSize, EColor16MU);
       
   784     GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::ScaleDecodedBitmapL() creating bitmap error, err=%d",err);
       
   785     User::LeaveIfNull(iDecodedBitmap[aBitmapIndex]);
       
   786 
       
   787     // Create bitmap device for destination bitmap
       
   788     CFbsBitmapDevice* bitmapDevice = CFbsBitmapDevice::NewL(iDecodedBitmap[aBitmapIndex]);
       
   789     CleanupStack::PushL(bitmapDevice);
       
   790     
       
   791     CFbsBitGc* bitmapGc = CFbsBitGc::NewL();
       
   792     CleanupStack::PushL(bitmapGc);
       
   793     bitmapGc->Activate(bitmapDevice);
       
   794     if (aBitmapIndex==EFSBitmapIndex)
       
   795         {
       
   796         // scale the source bitmap which is a FS bitmap
       
   797         bitmapGc->DrawBitmap(TRect(iTargetBitmapSize), iFsBitmap);
       
   798         }
       
   799     else
       
   800         {
       
   801         // scale the source bitmap which is a decoded bitmap
       
   802         bitmapGc->DrawBitmap(TRect(iTargetBitmapSize), iDecodedBitmap[EJpgDecodedBitmapIndex]);
       
   803         }
       
   804     
       
   805     CleanupStack::PopAndDestroy(bitmapGc);
       
   806     CleanupStack::PopAndDestroy(bitmapDevice);
       
   807 
       
   808     }
       
   809 
       
   810 // -----------------------------------------------------------------------------
       
   811 // ProcessTvImage 
       
   812 // -----------------------------------------------------------------------------
       
   813 void CGlxHdmiSurfaceUpdater::ProcessTvImage()
       
   814     {
       
   815     TRACER("CGlxHdmiSurfaceUpdater::ProcessTvImage()");
       
   816     if (iSurfBufferAO->iStatus != KRequestPending
       
   817             && !iSurfBufferAO->IsActive())
       
   818         {
       
   819         Refresh();
       
   820         iSurfBufferAO->iStatus = KRequestPending;
       
   821         iSurfBufferAO->SetActive();
       
   822         iSurfUpdateSession.NotifyWhenAvailable(iSurfBufferAO->iStatus);
       
   823         TInt err = iSurfUpdateSession.SubmitUpdate(1, iSurfId, 0, NULL);
       
   824         GLX_LOG_INFO1("CGlxHdmiSurfaceUpdater::ProcessTvImage() Surfaceupdatesession error %d",err);
       
   825         }
       
   826     }
       
   827 
       
   828 
       
   829