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