LCOV - code coverage report
Current view: top level - slideshow/source/engine - screenupdater.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 9 85 10.6 %
Date: 2015-06-13 12:38:46 Functions: 4 17 23.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "screenupdater.hxx"
      21             : #include "listenercontainer.hxx"
      22             : 
      23             : #include <osl/diagnose.h>
      24             : 
      25             : #include <boost/shared_ptr.hpp>
      26             : #include <boost/mem_fn.hpp>
      27             : #include <vector>
      28             : #include <algorithm>
      29             : 
      30             : namespace {
      31             :     class UpdateLock : public ::slideshow::internal::ScreenUpdater::UpdateLock
      32             :     {
      33             :     public:
      34             :         UpdateLock (::slideshow::internal::ScreenUpdater& rUpdater, const bool bStartLocked);
      35             :         virtual ~UpdateLock();
      36             :         virtual void Activate() SAL_OVERRIDE;
      37             :     private:
      38             :         ::slideshow::internal::ScreenUpdater& mrUpdater;
      39             :         bool mbIsActivated;
      40             :     };
      41             : }
      42             : 
      43             : namespace slideshow
      44             : {
      45             : namespace internal
      46             : {
      47             :     typedef std::vector<
      48             :         std::pair<UnoViewSharedPtr,bool> > UpdateRequestVector;
      49             : 
      50           1 :     struct ScreenUpdater::ImplScreenUpdater
      51             :     {
      52             :         /** List of registered ViewUpdaters, to consult for necessary
      53             :             updates
      54             :         */
      55             :         ThreadUnsafeListenerContainer<
      56             :             ViewUpdateSharedPtr,
      57             :             std::vector<ViewUpdateSharedPtr> > maUpdaters;
      58             : 
      59             :         /// Views that have been notified for update
      60             :         UpdateRequestVector                    maViewUpdateRequests;
      61             : 
      62             :         /// List of View. Used to issue screen updates on.
      63             :         UnoViewContainer const&                mrViewContainer;
      64             : 
      65             :         /// True, if a notifyUpdate() for all views has been issued.
      66             :         bool                                   mbUpdateAllRequest;
      67             : 
      68             :         /// True, if at least one notifyUpdate() call had bViewClobbered set
      69             :         bool                                   mbViewClobbered;
      70             : 
      71             :         /// The screen is updated only when mnLockCount==0
      72             :         sal_Int32 mnLockCount;
      73             : 
      74           1 :         explicit ImplScreenUpdater( UnoViewContainer const& rViewContainer ) :
      75             :             maUpdaters(),
      76             :             maViewUpdateRequests(),
      77             :             mrViewContainer(rViewContainer),
      78             :             mbUpdateAllRequest(false),
      79             :             mbViewClobbered(false),
      80           1 :             mnLockCount(0)
      81           1 :         {}
      82             :     };
      83             : 
      84           1 :     ScreenUpdater::ScreenUpdater( UnoViewContainer const& rViewContainer ) :
      85           1 :         mpImpl(new ImplScreenUpdater(rViewContainer) )
      86             :     {
      87           1 :     }
      88             : 
      89           1 :     ScreenUpdater::~ScreenUpdater()
      90             :     {
      91             :         // outline because of pimpl
      92           1 :     }
      93             : 
      94           0 :     void ScreenUpdater::notifyUpdate()
      95             :     {
      96           0 :         mpImpl->mbUpdateAllRequest = true;
      97           0 :     }
      98             : 
      99           0 :     void ScreenUpdater::notifyUpdate( const UnoViewSharedPtr& rView,
     100             :                                       bool                    bViewClobbered )
     101             :     {
     102           0 :         mpImpl->maViewUpdateRequests.push_back(
     103           0 :             std::make_pair(rView, bViewClobbered) );
     104             : 
     105           0 :         if( bViewClobbered )
     106           0 :             mpImpl->mbViewClobbered = true;
     107           0 :     }
     108             : 
     109           0 :     void ScreenUpdater::commitUpdates()
     110             :     {
     111           0 :         if (mpImpl->mnLockCount > 0)
     112           0 :             return;
     113             : 
     114             :         // cases:
     115             : 
     116             :         // (a) no update necessary at all
     117             : 
     118             :         // (b) no ViewUpdate-generated update
     119             :         //     I. update all views requested -> for_each( mrViewContainer )
     120             :         //    II. update some views requested -> for_each( maViewUpdateRequests )
     121             : 
     122             :         // (c) ViewUpdate-triggered update - update all views
     123             : 
     124             : 
     125             :         // any ViewUpdate-triggered updates?
     126             :         const bool bViewUpdatesNeeded(
     127           0 :             mpImpl->maUpdaters.apply(
     128           0 :                 boost::mem_fn(&ViewUpdate::needsUpdate)) );
     129             : 
     130           0 :         if( bViewUpdatesNeeded )
     131             :         {
     132           0 :             mpImpl->maUpdaters.applyAll(
     133           0 :                 boost::mem_fn((bool (ViewUpdate::*)())&ViewUpdate::update) );
     134             :         }
     135             : 
     136           0 :         if( bViewUpdatesNeeded ||
     137           0 :             mpImpl->mbUpdateAllRequest )
     138             :         {
     139             :             // unconditionally update all views
     140           0 :             std::for_each( mpImpl->mrViewContainer.begin(),
     141           0 :                            mpImpl->mrViewContainer.end(),
     142           0 :                            mpImpl->mbViewClobbered ?
     143           0 :                            boost::mem_fn(&View::paintScreen) :
     144           0 :                            boost::mem_fn(&View::updateScreen) );
     145             :         }
     146           0 :         else if( !mpImpl->maViewUpdateRequests.empty() )
     147             :         {
     148             :             // update notified views only
     149             :             UpdateRequestVector::const_iterator aIter(
     150           0 :                 mpImpl->maViewUpdateRequests.begin() );
     151             :             const UpdateRequestVector::const_iterator aEnd(
     152           0 :                 mpImpl->maViewUpdateRequests.end() );
     153           0 :             while( aIter != aEnd )
     154             :             {
     155             :                 // TODO(P1): this is O(n^2) in the number of views, if
     156             :                 // lots of views notify updates.
     157             :                 const UnoViewVector::const_iterator aEndOfViews(
     158           0 :                     mpImpl->mrViewContainer.end() );
     159           0 :                 UnoViewVector::const_iterator aFoundView;
     160           0 :                 if( (aFoundView=std::find(mpImpl->mrViewContainer.begin(),
     161             :                                           aEndOfViews,
     162           0 :                                           aIter->first)) != aEndOfViews )
     163             :                 {
     164           0 :                     if( aIter->second )
     165           0 :                         (*aFoundView)->paintScreen(); // force-paint
     166             :                     else
     167           0 :                         (*aFoundView)->updateScreen(); // update changes only
     168             :                 }
     169             : 
     170           0 :                 ++aIter;
     171             :             }
     172             :         }
     173             : 
     174             :         // done - clear requests
     175           0 :         mpImpl->mbViewClobbered = false;
     176           0 :         mpImpl->mbUpdateAllRequest = false;
     177           0 :         UpdateRequestVector().swap( mpImpl->maViewUpdateRequests );
     178             :     }
     179             : 
     180           0 :     void ScreenUpdater::addViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
     181             :     {
     182           0 :         mpImpl->maUpdaters.add( rViewUpdate );
     183           0 :     }
     184             : 
     185           0 :     void ScreenUpdater::removeViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
     186             :     {
     187           0 :         mpImpl->maUpdaters.remove( rViewUpdate );
     188           0 :     }
     189             : 
     190           0 :     void ScreenUpdater::requestImmediateUpdate()
     191             :     {
     192           0 :         if (mpImpl->mnLockCount > 0)
     193           0 :             return;
     194             : 
     195             :         // TODO(F2): This will interfere with other updates, since it
     196             :         // happens out-of-sync with main animation loop. Might cause
     197             :         // artifacts.
     198           0 :         std::for_each( mpImpl->mrViewContainer.begin(),
     199           0 :                        mpImpl->mrViewContainer.end(),
     200           0 :                        boost::mem_fn(&View::updateScreen) );
     201             :     }
     202             : 
     203           0 :     void ScreenUpdater::lockUpdates()
     204             :     {
     205           0 :         ++mpImpl->mnLockCount;
     206             :         OSL_ASSERT(mpImpl->mnLockCount>0);
     207           0 :     }
     208             : 
     209           0 :     void ScreenUpdater::unlockUpdates()
     210             :     {
     211             :         OSL_ASSERT(mpImpl->mnLockCount>0);
     212           0 :         if (mpImpl->mnLockCount > 0)
     213             :         {
     214           0 :             --mpImpl->mnLockCount;
     215           0 :             if (mpImpl->mnLockCount)
     216           0 :                 commitUpdates();
     217             :         }
     218           0 :     }
     219             : 
     220           0 :     ::boost::shared_ptr<ScreenUpdater::UpdateLock> ScreenUpdater::createLock (const bool bStartLocked)
     221             :     {
     222           0 :         return ::boost::shared_ptr<ScreenUpdater::UpdateLock>(new ::UpdateLock(*this, bStartLocked));
     223             :     }
     224             : 
     225             : 
     226             : } // namespace internal
     227             : } // namespace slideshow
     228             : 
     229             : namespace {
     230             : 
     231           0 : UpdateLock::UpdateLock (
     232             :     ::slideshow::internal::ScreenUpdater& rUpdater,
     233             :     const bool bStartLocked)
     234             :     : mrUpdater(rUpdater),
     235           0 :       mbIsActivated(false)
     236             : {
     237           0 :     if (bStartLocked)
     238           0 :         Activate();
     239           0 : }
     240             : 
     241             : 
     242             : 
     243             : 
     244           0 : UpdateLock::~UpdateLock()
     245             : {
     246           0 :     if (mbIsActivated)
     247           0 :         mrUpdater.unlockUpdates();
     248           0 : }
     249             : 
     250             : 
     251             : 
     252             : 
     253           0 : void UpdateLock::Activate()
     254             : {
     255           0 :     if ( ! mbIsActivated)
     256             :     {
     257           0 :         mbIsActivated = true;
     258           0 :         mrUpdater.lockUpdates();
     259             :     }
     260           0 : }
     261             : 
     262             : }
     263             : 
     264             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11