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