build fix
[LibreOffice.git] / slideshow / source / engine / screenupdater.cxx
blob597da9daf600638f4bb3e7a774fc31b5eb545fe1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "screenupdater.hxx"
21 #include "listenercontainer.hxx"
23 #include <osl/diagnose.h>
25 #include <functional>
26 #include <memory>
27 #include <vector>
28 #include <algorithm>
30 namespace {
31 class UpdateLock : public ::slideshow::internal::ScreenUpdater::UpdateLock
33 public:
34 explicit UpdateLock (::slideshow::internal::ScreenUpdater& rUpdater);
35 virtual ~UpdateLock();
36 virtual void Activate() override;
37 private:
38 ::slideshow::internal::ScreenUpdater& mrUpdater;
39 bool mbIsActivated;
43 namespace slideshow
45 namespace internal
47 typedef std::vector<
48 std::pair<UnoViewSharedPtr,bool> > UpdateRequestVector;
50 struct ScreenUpdater::ImplScreenUpdater
52 /** List of registered ViewUpdaters, to consult for necessary
53 updates
55 ThreadUnsafeListenerContainer<
56 ViewUpdateSharedPtr,
57 std::vector<ViewUpdateSharedPtr> > maUpdaters;
59 /// Views that have been notified for update
60 UpdateRequestVector maViewUpdateRequests;
62 /// List of View. Used to issue screen updates on.
63 UnoViewContainer const& mrViewContainer;
65 /// True, if a notifyUpdate() for all views has been issued.
66 bool mbUpdateAllRequest;
68 /// True, if at least one notifyUpdate() call had bViewClobbered set
69 bool mbViewClobbered;
71 /// The screen is updated only when mnLockCount==0
72 sal_Int32 mnLockCount;
74 explicit ImplScreenUpdater( UnoViewContainer const& rViewContainer ) :
75 maUpdaters(),
76 maViewUpdateRequests(),
77 mrViewContainer(rViewContainer),
78 mbUpdateAllRequest(false),
79 mbViewClobbered(false),
80 mnLockCount(0)
84 ScreenUpdater::ScreenUpdater( UnoViewContainer const& rViewContainer ) :
85 mpImpl(new ImplScreenUpdater(rViewContainer) )
89 ScreenUpdater::~ScreenUpdater()
91 // outline because of pimpl
94 void ScreenUpdater::notifyUpdate()
96 mpImpl->mbUpdateAllRequest = true;
99 void ScreenUpdater::notifyUpdate( const UnoViewSharedPtr& rView,
100 bool bViewClobbered )
102 mpImpl->maViewUpdateRequests.push_back(
103 std::make_pair(rView, bViewClobbered) );
105 if( bViewClobbered )
106 mpImpl->mbViewClobbered = true;
109 void ScreenUpdater::commitUpdates()
111 if (mpImpl->mnLockCount > 0)
112 return;
114 // cases:
116 // (a) no update necessary at all
118 // (b) no ViewUpdate-generated update
119 // I. update all views requested -> for_each( mrViewContainer )
120 // II. update some views requested -> for_each( maViewUpdateRequests )
122 // (c) ViewUpdate-triggered update - update all views
125 // any ViewUpdate-triggered updates?
126 const bool bViewUpdatesNeeded(
127 mpImpl->maUpdaters.apply(
128 std::mem_fn(&ViewUpdate::needsUpdate)) );
130 if( bViewUpdatesNeeded )
132 mpImpl->maUpdaters.applyAll(
133 std::mem_fn((bool (ViewUpdate::*)())&ViewUpdate::update) );
136 if( bViewUpdatesNeeded ||
137 mpImpl->mbUpdateAllRequest )
139 // unconditionally update all views
140 for( const auto& pView : mpImpl->mrViewContainer )
142 if( mpImpl->mbViewClobbered )
143 pView->paintScreen();
144 else
145 pView->updateScreen();
148 else if( !mpImpl->maViewUpdateRequests.empty() )
150 // update notified views only
151 for( const auto& rViewUpdateRequest : mpImpl->maViewUpdateRequests )
153 // TODO(P1): this is O(n^2) in the number of views, if
154 // lots of views notify updates.
155 const UnoViewVector::const_iterator aEndOfViews(
156 mpImpl->mrViewContainer.end() );
157 UnoViewVector::const_iterator aFoundView;
158 if( (aFoundView=std::find(mpImpl->mrViewContainer.begin(),
159 aEndOfViews,
160 rViewUpdateRequest.first)) != aEndOfViews )
162 if( rViewUpdateRequest.second )
163 (*aFoundView)->paintScreen(); // force-paint
164 else
165 (*aFoundView)->updateScreen(); // update changes only
170 // done - clear requests
171 mpImpl->mbViewClobbered = false;
172 mpImpl->mbUpdateAllRequest = false;
173 UpdateRequestVector().swap( mpImpl->maViewUpdateRequests );
176 void ScreenUpdater::addViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
178 mpImpl->maUpdaters.add( rViewUpdate );
181 void ScreenUpdater::removeViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
183 mpImpl->maUpdaters.remove( rViewUpdate );
186 void ScreenUpdater::requestImmediateUpdate()
188 if (mpImpl->mnLockCount > 0)
189 return;
191 // TODO(F2): This will interfere with other updates, since it
192 // happens out-of-sync with main animation loop. Might cause
193 // artifacts.
194 for( auto const& pView : mpImpl->mrViewContainer )
195 pView->updateScreen();
198 void ScreenUpdater::lockUpdates()
200 ++mpImpl->mnLockCount;
201 OSL_ASSERT(mpImpl->mnLockCount>0);
204 void ScreenUpdater::unlockUpdates()
206 OSL_ASSERT(mpImpl->mnLockCount>0);
207 if (mpImpl->mnLockCount > 0)
209 --mpImpl->mnLockCount;
210 if (mpImpl->mnLockCount)
211 commitUpdates();
215 ::std::shared_ptr<ScreenUpdater::UpdateLock> ScreenUpdater::createLock()
217 return ::std::shared_ptr<ScreenUpdater::UpdateLock>(new ::UpdateLock(*this));
221 } // namespace internal
222 } // namespace slideshow
224 namespace {
226 UpdateLock::UpdateLock (
227 ::slideshow::internal::ScreenUpdater& rUpdater)
228 : mrUpdater(rUpdater),
229 mbIsActivated(false)
234 UpdateLock::~UpdateLock()
236 if (mbIsActivated)
237 mrUpdater.unlockUpdates();
241 void UpdateLock::Activate()
243 if ( ! mbIsActivated)
245 mbIsActivated = true;
246 mrUpdater.lockUpdates();
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */