javauis/mmapi_akn/baseline/src.dsa/cmmacamerawindow.cpp
branchRCL_3
changeset 26 2455ef1f5bbc
parent 18 9ac0a0a7da70
equal deleted inserted replaced
25:ae942d28ec0e 26:2455ef1f5bbc
       
     1 /*
       
     2 * Copyright (c) 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:  This class implements MMMADisplayWindow functionality
       
    15 *                for Camera viewfinder usage.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //  Include Files
       
    21 #include <eikenv.h>
       
    22 #include <gdi.h>
       
    23 #include <jdebug.h>
       
    24 #include <AknsUtils.h>
       
    25 #include <AknsDrawUtils.h>
       
    26 
       
    27 #include <cameraapp.mbg>
       
    28 
       
    29 // Used for iDisplay member
       
    30 
       
    31 #include "cmmacamerawindow.h"
       
    32 
       
    33 // Camera viewfinder start delay
       
    34 const TInt KStarterTimeout = 100000; //100 msecs
       
    35 
       
    36 // Margin of icon displayed instead of viewfinder in case of error
       
    37 const TInt KErrorIconMargin = 5; //5 pixels
       
    38 
       
    39 
       
    40 _LIT(KCameraAppBitmapFile, "z:\\resource\\apps\\cameraapp.mif");
       
    41 
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CMMACameraWindow::NewL
       
    45 // Two-phased constructor.
       
    46 // Use this method to create a CMMACameraWindow instance.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CMMACameraWindow* CMMACameraWindow::NewL(TInt aCameraHandle)
       
    50 {
       
    51     CMMACameraWindow* self = new(ELeave)CMMACameraWindow(aCameraHandle);
       
    52     return self;
       
    53 }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CMMACameraWindow::~CMMACameraWindow
       
    58 // Destructor.
       
    59 // NOTE: iUICamera have to be deleted by ReleaseUiResources() called
       
    60 //       from UI thread
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CMMACameraWindow::~CMMACameraWindow()
       
    64 {
       
    65     // It's not allowed to delete any nonsharable object here.
       
    66     // This must be done in Destroy().
       
    67     // If the iDisplay is set,
       
    68     // instance should be deleted by sending
       
    69     // event from UI to be received by MUiEventConsumer.
       
    70 }
       
    71 
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // CMMACameraWindow::ViewFinderActive
       
    75 // Gets an information if the viewfinder is started.
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 TBool CMMACameraWindow::ViewFinderActive()
       
    79 {
       
    80     return iUICamera && iViewFinderVisible;
       
    81 }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // CMMACameraWindow::SetStarted
       
    86 // Notifies window about Started state change.
       
    87 // If started, tries to start viewfinder.
       
    88 // If not started, stops viewfinder.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CMMACameraWindow::SetStarted(TBool aStarted)
       
    92 {
       
    93     iStarted = aStarted;
       
    94     if (iDisplay)
       
    95     {
       
    96         if (iStarted)
       
    97         {
       
    98             iDisplay->UIGetCallback(
       
    99                 *this, CMMACameraWindow::EShowViewFinder);
       
   100         }
       
   101         else
       
   102         {
       
   103             iDisplay->UIGetCallback(
       
   104                 *this, CMMACameraWindow::EHideViewFinder);
       
   105         }
       
   106     }
       
   107 }
       
   108 
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // CMMACameraWindow::SetDisplay
       
   112 // Sets iDisplay used to invoke callbacks from UI thread.
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CMMACameraWindow::SetDisplay(MMMADisplay *aDisplay)
       
   116 {
       
   117     if (iDisplay != aDisplay)
       
   118     {
       
   119         if (iDisplay)
       
   120         {
       
   121             // Clear the resources created within the old Display
       
   122             iDisplay->UIGetCallback(
       
   123                 *this, CMMACameraWindow::EDeleteViewFinder);
       
   124         }
       
   125 
       
   126         // Set the new Display
       
   127         iDisplay = aDisplay;
       
   128 
       
   129         if (iDisplay)
       
   130         {
       
   131             // Get a DSA resources for the new Display
       
   132             iDisplay->UIGetDSAResources(*this, MMMADisplay::EMmaThread);
       
   133         }
       
   134     }
       
   135 }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // From class MMMADisplayWindow
       
   140 // CMMACameraWindow::SetDestinationBitmapL
       
   141 //
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CMMACameraWindow::SetDestinationBitmapL(CFbsBitmap* /*aBitmap*/)
       
   145 {
       
   146     // Ignored, this window will not be used for actual drawing
       
   147     DEBUG("CMMACameraWindow::SetDestinationBitmapL");
       
   148 }
       
   149 
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // From class MMMADisplayWindow
       
   153 // CMMACameraWindow::DrawFrameL
       
   154 //
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void CMMACameraWindow::DrawFrameL(const CFbsBitmap* /*aBitmap*/)
       
   158 {
       
   159     // Ignored, this window will not be used for actual drawing
       
   160 }
       
   161 
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // From class MMMADisplayWindow
       
   165 // CMMACameraWindow::SetDrawRect
       
   166 //
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CMMACameraWindow::SetDrawRect(const TRect& aRect)
       
   170 {
       
   171     // MMAPI thread
       
   172     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRect TL:%d,%d",
       
   173                aRect.iTl.iX, aRect.iTl.iY);
       
   174     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRect BR:%d,%d",
       
   175                aRect.iBr.iX, aRect.iBr.iY);
       
   176 
       
   177     iDrawRect = aRect;
       
   178 
       
   179     if (iDisplay)
       
   180     {
       
   181         iDisplay->UIGetCallback(*this, CMMACameraWindow::EResetViewFinder);
       
   182     }
       
   183 }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // From class MMMADisplayWindow
       
   188 // CMMACameraWindow::SetDrawRectThread
       
   189 //
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void CMMACameraWindow::SetDrawRectThread(const TRect& aRect)
       
   193 {
       
   194     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRectThread TL:%d,%d",
       
   195                aRect.iTl.iX, aRect.iTl.iY);
       
   196     DEBUG_INT2("MMA::CMMACameraWindow::SetDrawRectThread BR:%d,%d",
       
   197                aRect.iBr.iX, aRect.iBr.iY);
       
   198 
       
   199     // Note: Runs in UI thread only
       
   200 
       
   201     // Bitmap window's rect can be set in any thread.
       
   202     if (iDrawRect != aRect)
       
   203     {
       
   204         iDrawRect = aRect;
       
   205 
       
   206         // Stop and start ViewFinder to update position
       
   207         ResetViewFinder();
       
   208     }
       
   209 }
       
   210 
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // From class MMMADisplayWindow
       
   214 // CMMACameraWindow::DrawRect
       
   215 //
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 const TRect& CMMACameraWindow::DrawRect()
       
   219 {
       
   220     return iDrawRect;
       
   221 }
       
   222 
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // From class MMMADisplayWindow
       
   226 // CMMACameraWindow::WindowSize
       
   227 //
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 TSize CMMACameraWindow::WindowSize()
       
   231 {
       
   232     return iDrawRect.Size();
       
   233 }
       
   234 
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // From class MMMADisplayWindow
       
   238 // CMMACameraWindow::SetPosition
       
   239 //
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CMMACameraWindow::SetPosition(const TPoint& aPosition)
       
   243 {
       
   244     // Note: Runs in UI thread only
       
   245     SetDrawRectThread(TRect(aPosition, iDrawRect.Size()));
       
   246 }
       
   247 
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // From class MMMADisplayWindow
       
   251 // CMMACameraWindow::SetVisible
       
   252 //
       
   253 // ---------------------------------------------------------------------------
       
   254 //
       
   255 void CMMACameraWindow::SetVisible(TBool aVisible, TBool aUseEventServer)
       
   256 {
       
   257     iVisible = aVisible;
       
   258 
       
   259     if (aUseEventServer)
       
   260     {
       
   261         // We are in UI thread now.
       
   262         SetViewFinderVisibility(aVisible);
       
   263     }
       
   264     else
       
   265     {
       
   266         // MMAPI thread
       
   267         if (iDisplay)
       
   268         {
       
   269             if (aVisible)
       
   270             {
       
   271                 iDisplay->UIGetCallback(
       
   272                     *this, CMMACameraWindow::EShowViewFinder);
       
   273             }
       
   274             else
       
   275             {
       
   276                 iDisplay->UIGetCallback(
       
   277                     *this, CMMACameraWindow::EHideViewFinder);
       
   278             }
       
   279         }
       
   280     }
       
   281 }
       
   282 
       
   283 
       
   284 // ---------------------------------------------------------------------------
       
   285 // From class MMMADisplayWindow
       
   286 // CMMACameraWindow::SetWindowRect
       
   287 //
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 void CMMACameraWindow::SetWindowRect(
       
   291     const TRect& aRect,
       
   292     MMMADisplay::TThreadType aThreadType)
       
   293 {
       
   294     DEBUG_INT2("MMA::CMMACameraWindow::SetWindowRect TL:%d,%d",
       
   295                aRect.iTl.iX, aRect.iTl.iY);
       
   296     DEBUG_INT2("MMA::CMMACameraWindow::SetWindowRect BR:%d,%d",
       
   297                aRect.iBr.iX, aRect.iBr.iY);
       
   298 
       
   299     if (iClientRect != aRect)
       
   300     {
       
   301         iClientRect = aRect;
       
   302 
       
   303         if (aThreadType == MMMADisplay::EUiThread)
       
   304         {
       
   305             ResetViewFinder();
       
   306         }
       
   307         else
       
   308         {
       
   309             iDisplay->UIGetCallback(
       
   310                 *this, CMMACameraWindow::EResetViewFinder);
       
   311         }
       
   312     }
       
   313 }
       
   314 
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // From class MMMADisplayWindow
       
   318 // CMMACameraWindow::WindowRect
       
   319 //
       
   320 // ---------------------------------------------------------------------------
       
   321 //
       
   322 const TRect& CMMACameraWindow::WindowRect()
       
   323 {
       
   324     return iClientRect;
       
   325 }
       
   326 
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // From class MMMADisplayWindow
       
   330 // CMMACameraWindow::GetDisplayWindowType
       
   331 //
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 MMMADisplayWindow::TDisplayWindowType
       
   335 CMMACameraWindow::GetDisplayWindowType() const
       
   336 {
       
   337     return EDisplayWindowTypeIsCamera;
       
   338 }
       
   339 
       
   340 
       
   341 // ---------------------------------------------------------------------------
       
   342 // From class MMMADisplayWindow
       
   343 // CMMACameraWindow::IsVisible
       
   344 //
       
   345 // ---------------------------------------------------------------------------
       
   346 //
       
   347 TBool CMMACameraWindow::IsVisible() const
       
   348 {
       
   349     return iVisible;
       
   350 }
       
   351 
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // From class MMMADisplayWindow
       
   355 // CMMACameraWindow::ContainerSet
       
   356 //
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 void CMMACameraWindow::ContainerSet()
       
   360 {
       
   361     // We are in UI thread now
       
   362 
       
   363     // Container was probably not set when
       
   364     // iDisplay was set,
       
   365     // we can now try get the DSA stuff again
       
   366     if (!iUICamera && iDisplay)
       
   367     {
       
   368         // Get a DSA stuff for the new Display
       
   369         iDisplay->UIGetDSAResources(*this, MMMADisplay::EUiThread);
       
   370     }
       
   371 }
       
   372 
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // From class MMMADisplayWindow
       
   376 // CMMACameraWindow::ContainerDestroyed
       
   377 //
       
   378 // ---------------------------------------------------------------------------
       
   379 //
       
   380 void CMMACameraWindow::ContainerDestroyed()
       
   381 {
       
   382     // We are in UI thread now
       
   383     ReleaseUiResources();
       
   384 }
       
   385 
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // From class MCameraObserver
       
   389 // CMMACameraWindow::ReserveComplete
       
   390 //
       
   391 // ---------------------------------------------------------------------------
       
   392 //
       
   393 void CMMACameraWindow::ReserveComplete(TInt aError)
       
   394 {
       
   395     DEBUG_INT("MMA::CMMACameraWindow::ReserveComplete %d", aError);
       
   396 
       
   397     if (aError == KErrNone)
       
   398     {
       
   399         // camera will notify completion with PowerOnComplete method.
       
   400         iUICamera->PowerOn();
       
   401     }
       
   402 }
       
   403 
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // From class MCameraObserver
       
   407 // CMMACameraWindow::PowerOnComplete
       
   408 //
       
   409 // ---------------------------------------------------------------------------
       
   410 //
       
   411 void CMMACameraWindow::PowerOnComplete(TInt aError)
       
   412 {
       
   413     DEBUG_INT("MMA::CMMACameraWindow::PowerOnComplete %d", aError);
       
   414 
       
   415     if (aError == KErrNone)
       
   416     {
       
   417         iCameraPowerOn = ETrue;
       
   418         SetViewFinderVisibility(ETrue);
       
   419     }
       
   420 }
       
   421 
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // From class MCameraObserver
       
   425 // CMMACameraWindow::ViewFinderFrameReady
       
   426 //
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 void CMMACameraWindow::ViewFinderFrameReady(CFbsBitmap& /*aFrame*/)
       
   430 {
       
   431     // Empty implementation of the interface method
       
   432     DEBUG("MMA::CMMACameraWindow::ViewFinderFrameReady");
       
   433 }
       
   434 
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // From class MCameraObserver
       
   438 // CMMACameraWindow::ImageReady
       
   439 //
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 void CMMACameraWindow::ImageReady(CFbsBitmap* /*aBitmap*/,
       
   443                                   HBufC8* /*aData*/,
       
   444                                   TInt /*aError*/)
       
   445 {
       
   446     // Empty implementation of the interface method
       
   447     DEBUG("MMA::CMMACameraWindow::ImageReady");
       
   448 }
       
   449 
       
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // From class MCameraObserver
       
   453 // CMMACameraWindow::FrameBufferReady
       
   454 //
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 void CMMACameraWindow::FrameBufferReady(MFrameBuffer* /*aFrameBuffer*/,
       
   458                                         TInt /*aError*/)
       
   459 {
       
   460     // Empty implementation of the interface method
       
   461     DEBUG("MMA::CMMACameraWindow::FrameBufferReady");
       
   462 }
       
   463 
       
   464 
       
   465 // ---------------------------------------------------------------------------
       
   466 // From class MDirectScreenAccess.
       
   467 // CMMACameraWindow::AbortNow
       
   468 //
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CMMACameraWindow::AbortNow(
       
   472     RDirectScreenAccess::TTerminationReasons /*aReasons*/)
       
   473 {
       
   474     SetViewFinderVisibility(EFalse);
       
   475 }
       
   476 
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // From class MDirectScreenAccess.
       
   480 // CMMACameraWindow::Restart
       
   481 //
       
   482 // ---------------------------------------------------------------------------
       
   483 //
       
   484 void CMMACameraWindow::Restart(
       
   485     RDirectScreenAccess::TTerminationReasons /*aReasons*/)
       
   486 {
       
   487     SetViewFinderVisibility(ETrue);
       
   488 }
       
   489 
       
   490 
       
   491 // ---------------------------------------------------------------------------
       
   492 // CMMACameraWindow::CMMACameraWindow
       
   493 // C++ constructor.
       
   494 // ---------------------------------------------------------------------------
       
   495 //
       
   496 CMMACameraWindow::CMMACameraWindow(TInt aCameraHandle):
       
   497         iVisible(EFalse),
       
   498         iStarted(EFalse),
       
   499         iViewFinderVisible(EFalse),
       
   500         iCameraPowerOn(EFalse),
       
   501         iDrawRect(0, 0, 0, 0),
       
   502         iClientRect(0, 0, 0, 0),
       
   503         iCameraHandle(aCameraHandle),
       
   504         iUICamera(NULL),
       
   505         iDisplay(NULL),
       
   506         iStarterTimer(NULL),
       
   507         iDirectAccess(NULL),
       
   508         iErrorIconBitmap(NULL),
       
   509         iErrorIconMaskBitmap(NULL)
       
   510 {
       
   511 }
       
   512 
       
   513 
       
   514 // ---------------------------------------------------------------------------
       
   515 // CMMACameraWindow::UIStartViewFinder
       
   516 // Creates UI Camera.
       
   517 // This CCamera instance is duplicated from handle given to the constructor.
       
   518 // Have to be called from the thread which UI Camera will be controlled from.
       
   519 // ---------------------------------------------------------------------------
       
   520 //
       
   521 void CMMACameraWindow::UIStartViewFinder(
       
   522     RWsSession &aWs,
       
   523     CWsScreenDevice &aScreenDevice,
       
   524     RWindowBase &aWindow)
       
   525 {
       
   526     DEBUG("MMA::CMMACameraWindow::UIStartViewFinder");
       
   527 
       
   528     iWs = &aWs;
       
   529     iScreenDevice = &aScreenDevice;
       
   530     iWindow = &aWindow;
       
   531 
       
   532     if (!iUICamera)
       
   533     {
       
   534         TRAPD(error, iUICamera =
       
   535                   CCamera::NewDuplicateL(*this, iCameraHandle));
       
   536 
       
   537         DEBUG_INT(
       
   538             "MMA::CMMACameraWindow::UIStartViewFinder - NewDuplicateL %d",
       
   539             error);
       
   540 
       
   541         if (error == KErrNone)
       
   542         {
       
   543             iUICamera->Reserve();
       
   544         }
       
   545     }
       
   546 }
       
   547 
       
   548 
       
   549 // ---------------------------------------------------------------------------
       
   550 // CMMACameraWindow::SetViewFinderVisibility
       
   551 // If UI Camera have been created, it starts/hides the DirectViewFinder.
       
   552 // Have to be called from UI thread in which iUICamera instance was created.
       
   553 // ---------------------------------------------------------------------------
       
   554 //
       
   555 void CMMACameraWindow::SetViewFinderVisibility(TBool aVisible)
       
   556 {
       
   557     DEBUG_INT("MMA::CMMACameraWindow::SetViewFinderVisibility - %d",
       
   558               aVisible);
       
   559 
       
   560     if (!iUICamera || !iCameraPowerOn || iViewFinderVisible == aVisible)
       
   561     {
       
   562         return;
       
   563     }
       
   564 
       
   565     if (aVisible)
       
   566     {
       
   567         // Viewfinder is going to start
       
   568         if (!iVisible || !iStarted)
       
   569         {
       
   570             return;
       
   571         }
       
   572 
       
   573         iViewFinderVisible = ETrue;
       
   574 
       
   575         if (!iStarterTimer)
       
   576         {
       
   577             // If starter timer haven't been created yet, create an instance
       
   578             TRAPD(
       
   579                 timerErr,
       
   580                 iStarterTimer = CPeriodic::NewL(CActive::EPriorityIdle));
       
   581 
       
   582             if (timerErr != KErrNone)
       
   583             {
       
   584                 DEBUG_INT(
       
   585                     "MMA::CMMACameraWindow::SetViewFinderVisibility - timer error = %d", 
       
   586                     timerErr);
       
   587                 __ASSERT_DEBUG(EFalse, User::Invariant());
       
   588             }
       
   589         }
       
   590 
       
   591         if (!iStarterTimer->IsActive())
       
   592         {
       
   593             iStarterTimer->Start(
       
   594                 KStarterTimeout,
       
   595                 0,
       
   596                 TCallBack(StarterTimerCallback, this));
       
   597         }
       
   598     }
       
   599     else
       
   600     {
       
   601         // Viewfinder is going to be cancelled
       
   602         DEBUG(
       
   603             "MMA::CMMACameraWindow::SetViewFinderVisibility - Stopping VF");
       
   604 
       
   605         if (iStarterTimer && iStarterTimer->IsActive())
       
   606         {
       
   607             // Cancel the running starter timer
       
   608             iStarterTimer->Cancel();
       
   609         }
       
   610 
       
   611         if (iUICamera->ViewFinderActive())
       
   612         {
       
   613             iUICamera->StopViewFinder();
       
   614         }
       
   615         if (iDirectAccess->IsActive())
       
   616         {
       
   617             iDirectAccess->Cancel();
       
   618         }
       
   619 
       
   620         iViewFinderVisible = EFalse;
       
   621     }
       
   622 }
       
   623 
       
   624 
       
   625 // ---------------------------------------------------------------------------
       
   626 // CMMACameraWindow::StarterTimerCallback
       
   627 // Executed at the expiry of the iStartedTimer.
       
   628 // ---------------------------------------------------------------------------
       
   629 //
       
   630 TInt CMMACameraWindow::StarterTimerCallback(TAny* aThis)
       
   631 {
       
   632     CMMACameraWindow* self = static_cast<CMMACameraWindow*>(aThis);
       
   633     ASSERT(self);
       
   634 
       
   635     self->StartViewFinder();
       
   636 
       
   637     // We don't want to run the callback again
       
   638     return EFalse;
       
   639 }
       
   640 
       
   641 
       
   642 // ---------------------------------------------------------------------------
       
   643 // CMMACameraWindow::StartViewFinder
       
   644 // Starts viewfinder.
       
   645 // ---------------------------------------------------------------------------
       
   646 //
       
   647 void CMMACameraWindow::StartViewFinder()
       
   648 {
       
   649     DEBUG(" < StartViewFinder");
       
   650 
       
   651     ASSERT(iUICamera);
       
   652     ASSERT(iCameraPowerOn);
       
   653     ASSERT(iStarted);
       
   654 
       
   655     iStarterTimer->Cancel();
       
   656 
       
   657     DrawUnderlayer();
       
   658 
       
   659 
       
   660     // Set the drawing area
       
   661     TRect drawRect(iDrawRect);
       
   662     drawRect.Move(iClientRect.iTl);
       
   663 
       
   664     DEBUG_INT2(
       
   665         "MMA::CMMACameraWindow::StartViewFinder - Starting VF TL:%d,%d",
       
   666         drawRect.iTl.iX,
       
   667         drawRect.iTl.iY);
       
   668     DEBUG_INT2(
       
   669         "MMA::CMMACameraWindow::StartViewFinder - Starting VF BR:%d,%d",
       
   670         drawRect.iBr.iX,
       
   671         drawRect.iBr.iY);
       
   672 
       
   673     TRAPD(vfError, iUICamera->StartViewFinderDirectL(
       
   674               *iWs, *iScreenDevice, *iWindow, drawRect));
       
   675 
       
   676     if (vfError == KErrNone)
       
   677     {
       
   678         DEBUG("MMA::CMMACameraWindow::StartViewFinder - Start OK");
       
   679     }
       
   680     else
       
   681     {
       
   682         DEBUG_INT(
       
   683             "MMA::CMMACameraWindow::StartViewFinder()  - \
       
   684             StartViewFinderDirectL error=%d", vfError);
       
   685 
       
   686         TRAP_IGNORE(DrawViewFinderErrorL(vfError, drawRect));
       
   687     }
       
   688 
       
   689     DEBUG(" > StartViewFinder");
       
   690 }
       
   691 
       
   692 
       
   693 // ---------------------------------------------------------------------------
       
   694 // CMMACameraWindow::DrawViewFinderError()
       
   695 // Draws the error message to specified area.
       
   696 // Used in cases when viewfinder is unable to start.
       
   697 // ---------------------------------------------------------------------------
       
   698 //
       
   699 void CMMACameraWindow::DrawViewFinderErrorL(
       
   700     const TInt /*aError*/,
       
   701     const TRect& aDrawRect)
       
   702 {
       
   703 
       
   704     ASSERT(iDirectAccess);
       
   705     TInt dcError = KErrNone;
       
   706     if (!iDirectAccess->IsActive())
       
   707     {
       
   708         TRAP(dcError, iDirectAccess->StartL());
       
   709     }
       
   710 
       
   711     TRect drawRect(aDrawRect);
       
   712 
       
   713     if (dcError == KErrNone)
       
   714     {
       
   715         drawRect.Intersection(iClientRect);
       
   716         drawRect.Move(-iWindow->AbsPosition());
       
   717 
       
   718         CFbsBitGc* directGc = iDirectAccess->Gc();
       
   719         directGc->SetClippingRect(drawRect);
       
   720         directGc->SetBrushColor(TRgb(128,128,128));
       
   721         directGc->SetPenColor(TRgb(128,0,0));
       
   722         directGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   723         directGc->SetPenStyle(CGraphicsContext::ESolidPen);
       
   724         directGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
       
   725         directGc->DrawRect(drawRect);
       
   726 
       
   727         if (!iErrorIconBitmap || !iErrorIconMaskBitmap)
       
   728         {
       
   729             if (iErrorIconBitmap)
       
   730             {
       
   731                 delete iErrorIconBitmap;
       
   732                 iErrorIconBitmap = NULL;
       
   733             }
       
   734 
       
   735             if (iErrorIconMaskBitmap)
       
   736             {
       
   737                 delete iErrorIconMaskBitmap;
       
   738                 iErrorIconMaskBitmap = NULL;
       
   739             }
       
   740 
       
   741             AknsUtils::CreateIconL(
       
   742                 AknsUtils::SkinInstance(),
       
   743                 KAknsIIDQgnIndiCam4Camera,
       
   744                 iErrorIconBitmap,
       
   745                 iErrorIconMaskBitmap,
       
   746                 KCameraAppBitmapFile,
       
   747                 EMbmCameraappQgn_indi_cam4_camera,
       
   748                 EMbmCameraappQgn_indi_cam4_camera_mask
       
   749             );
       
   750         }
       
   751 
       
   752         //TRect iconRect
       
   753         drawRect.iTl.iX += KErrorIconMargin;
       
   754         drawRect.iTl.iY += KErrorIconMargin;
       
   755         drawRect.iBr.iX -= KErrorIconMargin;
       
   756         drawRect.iBr.iY -= KErrorIconMargin;
       
   757 
       
   758         if (iErrorIconBitmap->SizeInPixels() != drawRect.Size())
       
   759         {
       
   760             AknIconUtils::SetSize(iErrorIconBitmap, drawRect.Size());
       
   761             AknIconUtils::SetSize(iErrorIconMaskBitmap, drawRect.Size());
       
   762         }
       
   763 
       
   764         directGc->BitBltMasked(
       
   765             drawRect.iTl, iErrorIconBitmap,
       
   766             TRect(iErrorIconBitmap->SizeInPixels()),
       
   767             iErrorIconMaskBitmap, EFalse);
       
   768 
       
   769         iDirectAccess->ScreenDevice()->Update();
       
   770     }
       
   771 
       
   772 }
       
   773 
       
   774 
       
   775 // ---------------------------------------------------------------------------
       
   776 // CMMACameraWindow::ReleaseUiResources()
       
   777 // Stops the DirectViewFinder and deletes UI Camera instance
       
   778 // Have to be called from UI thread in which UI Camera
       
   779 // have been created before the CMMACameraWindow instance
       
   780 // is deleted.
       
   781 // ---------------------------------------------------------------------------
       
   782 //
       
   783 void CMMACameraWindow::ReleaseUiResources()
       
   784 {
       
   785     if (iUICamera)
       
   786     {
       
   787         SetViewFinderVisibility(EFalse);
       
   788         // iStarterTimer is cancelled by previous call
       
   789         // it can be deleted now
       
   790         delete iStarterTimer;
       
   791         iStarterTimer = NULL;
       
   792         iUICamera->Release();
       
   793         delete iUICamera;
       
   794         iUICamera = NULL;
       
   795         iCameraPowerOn = EFalse;
       
   796         iDirectAccess->Cancel();
       
   797         delete iDirectAccess;
       
   798         iDirectAccess = NULL;
       
   799         delete iErrorIconBitmap;
       
   800         iErrorIconBitmap = NULL;
       
   801         delete iErrorIconMaskBitmap;
       
   802         iErrorIconMaskBitmap = NULL;
       
   803     }
       
   804 }
       
   805 
       
   806 
       
   807 // ---------------------------------------------------------------------------
       
   808 // CMMACameraWindow::ResetViewFinder
       
   809 // Resets (stops and starts) viewfinder
       
   810 // ---------------------------------------------------------------------------
       
   811 //
       
   812 void CMMACameraWindow::ResetViewFinder()
       
   813 {
       
   814 
       
   815     if (iStarterTimer && !iStarterTimer->IsActive() && iStarted)
       
   816     {
       
   817         SetViewFinderVisibility(EFalse);
       
   818         SetViewFinderVisibility(ETrue);
       
   819     }
       
   820 }
       
   821 
       
   822 
       
   823 // ---------------------------------------------------------------------------
       
   824 // CMMACameraWindow::DrawUnderlayer
       
   825 // Draws the underlayer of viewfinder.
       
   826 // Used to clear the alpha value.
       
   827 // Uses Direct Screen Access.
       
   828 // ---------------------------------------------------------------------------
       
   829 //
       
   830 TBool CMMACameraWindow::DrawUnderlayer()
       
   831 {
       
   832     ASSERT(iDirectAccess);
       
   833 
       
   834 
       
   835     DEBUG("Draw under layer");
       
   836     TInt error = KErrNone;
       
   837     if (!iDirectAccess->IsActive())
       
   838     {
       
   839         TRAP(error, iDirectAccess->StartL());
       
   840     }
       
   841     if (error == KErrNone)
       
   842     {
       
   843         DEBUG("Drawing alpha pixels to zero");
       
   844         CFbsBitGc* directGc = iDirectAccess->Gc();
       
   845         TInt count = iDirectAccess->DrawingRegion()->Count();
       
   846         if (count > 0)
       
   847         {
       
   848             TRect drawRect(iDrawRect);
       
   849             drawRect.Move(iClientRect.iTl);
       
   850             drawRect.Intersection(iClientRect);
       
   851             drawRect.Move(-iWindow->AbsPosition());
       
   852 
       
   853             directGc->SetClippingRect(drawRect);
       
   854             directGc->SetBrushColor(TRgb(0,0,0,0));
       
   855             directGc->SetPenColor(TRgb(0,0,0,0));
       
   856             directGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   857             directGc->SetPenStyle(CGraphicsContext::ESolidPen);
       
   858             directGc->SetDrawMode(CGraphicsContext::EDrawModeWriteAlpha);
       
   859             directGc->DrawRect(drawRect);
       
   860             iDirectAccess->ScreenDevice()->Update();
       
   861         }
       
   862 
       
   863         if (count <= 0)
       
   864         {
       
   865             // DSA may work in the next attempt
       
   866             return EFalse;
       
   867         }
       
   868     }
       
   869     else
       
   870     {
       
   871         // DSA may work in the next attempt
       
   872         return EFalse;
       
   873     }
       
   874 
       
   875     // Success, no more attempt is needed
       
   876     return ETrue;
       
   877 }
       
   878 
       
   879 
       
   880 // ---------------------------------------------------------------------------
       
   881 // CMMACameraWindow::Destroy
       
   882 // Deletes this object.
       
   883 // ---------------------------------------------------------------------------
       
   884 //
       
   885 void CMMACameraWindow::Destroy()
       
   886 {
       
   887     // Delete itself
       
   888     delete this;
       
   889 }
       
   890 
       
   891 
       
   892 // ---------------------------------------------------------------------------
       
   893 // From class MUiEventConsumer.
       
   894 // CMMACameraWindow::MdcDSAResourcesCallback
       
   895 // Callback to iDisplay->UIGetDSAResources().
       
   896 // Invoked asynchronously from UI Event Server thread.
       
   897 // ---------------------------------------------------------------------------
       
   898 //
       
   899 void CMMACameraWindow::MdcDSAResourcesCallback(
       
   900     RWsSession &aWs,
       
   901     CWsScreenDevice &aScreenDevice,
       
   902     RWindowBase &aWindow)
       
   903 {
       
   904     TRAPD(error, iDirectAccess = CDirectScreenAccess::NewL(aWs,
       
   905                                  aScreenDevice,
       
   906                                  aWindow,
       
   907                                  *this));
       
   908     DEBUG_INT("MMA::CMMACameraWindow::MdcDSAResourcesCallback, error = %d", error);
       
   909     if (KErrNone != error)
       
   910     {
       
   911         return;
       
   912     }
       
   913     this->UIStartViewFinder(aWs, aScreenDevice, aWindow);
       
   914 }
       
   915 
       
   916 
       
   917 // ---------------------------------------------------------------------------
       
   918 // From class MUiEventConsumer.
       
   919 // CMMACameraWindow::MdcUICallback
       
   920 // Callback to iDisplay->UIGetCallback().
       
   921 // Invoked asynchronously from UI Event Server thread.
       
   922 // ---------------------------------------------------------------------------
       
   923 //
       
   924 void CMMACameraWindow::MdcUICallback(TInt aCallbackId)
       
   925 {
       
   926     DEBUG("MMA::CMMACameraWindow::MdcUICallback");
       
   927 
       
   928     switch (aCallbackId)
       
   929     {
       
   930     case EDeleteViewFinder:
       
   931         ReleaseUiResources();
       
   932         break;
       
   933     case EHideViewFinder:
       
   934         SetViewFinderVisibility(EFalse);
       
   935         break;
       
   936     case EShowViewFinder:
       
   937         SetViewFinderVisibility(ETrue);
       
   938         break;
       
   939     case EResetViewFinder:
       
   940         ResetViewFinder();
       
   941         break;
       
   942     case EDestroyWindow:
       
   943         Destroy();
       
   944         break;
       
   945     }
       
   946 }
       
   947 
       
   948 
       
   949 //  END OF FILE