Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / engine / screenupdater.cxx
blob7a950e031cd569aa62084926862e88f137ff204b
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.emplace_back(rView, bViewClobbered );
104 if( bViewClobbered )
105 mpImpl->mbViewClobbered = true;
108 void ScreenUpdater::commitUpdates()
110 if (mpImpl->mnLockCount > 0)
111 return;
113 // cases:
115 // (a) no update necessary at all
117 // (b) no ViewUpdate-generated update
118 // I. update all views requested -> for_each( mrViewContainer )
119 // II. update some views requested -> for_each( maViewUpdateRequests )
121 // (c) ViewUpdate-triggered update - update all views
124 // any ViewUpdate-triggered updates?
125 const bool bViewUpdatesNeeded(
126 mpImpl->maUpdaters.apply(
127 std::mem_fn(&ViewUpdate::needsUpdate)) );
129 if( bViewUpdatesNeeded )
131 mpImpl->maUpdaters.applyAll(
132 std::mem_fn((bool (ViewUpdate::*)())&ViewUpdate::update) );
135 if( bViewUpdatesNeeded ||
136 mpImpl->mbUpdateAllRequest )
138 // unconditionally update all views
139 for( const auto& pView : mpImpl->mrViewContainer )
141 if( mpImpl->mbViewClobbered )
142 pView->paintScreen();
143 else
144 pView->updateScreen();
147 else if( !mpImpl->maViewUpdateRequests.empty() )
149 // update notified views only
150 for( const auto& rViewUpdateRequest : mpImpl->maViewUpdateRequests )
152 // TODO(P1): this is O(n^2) in the number of views, if
153 // lots of views notify updates.
154 const UnoViewVector::const_iterator aEndOfViews(
155 mpImpl->mrViewContainer.end() );
156 UnoViewVector::const_iterator aFoundView;
157 if( (aFoundView=std::find(mpImpl->mrViewContainer.begin(),
158 aEndOfViews,
159 rViewUpdateRequest.first)) != aEndOfViews )
161 if( rViewUpdateRequest.second )
162 (*aFoundView)->paintScreen(); // force-paint
163 else
164 (*aFoundView)->updateScreen(); // update changes only
169 // done - clear requests
170 mpImpl->mbViewClobbered = false;
171 mpImpl->mbUpdateAllRequest = false;
172 UpdateRequestVector().swap( mpImpl->maViewUpdateRequests );
175 void ScreenUpdater::addViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
177 mpImpl->maUpdaters.add( rViewUpdate );
180 void ScreenUpdater::removeViewUpdate( ViewUpdateSharedPtr const& rViewUpdate )
182 mpImpl->maUpdaters.remove( rViewUpdate );
185 void ScreenUpdater::requestImmediateUpdate()
187 if (mpImpl->mnLockCount > 0)
188 return;
190 // TODO(F2): This will interfere with other updates, since it
191 // happens out-of-sync with main animation loop. Might cause
192 // artifacts.
193 for( auto const& pView : mpImpl->mrViewContainer )
194 pView->updateScreen();
197 void ScreenUpdater::lockUpdates()
199 ++mpImpl->mnLockCount;
200 OSL_ASSERT(mpImpl->mnLockCount>0);
203 void ScreenUpdater::unlockUpdates()
205 OSL_ASSERT(mpImpl->mnLockCount>0);
206 if (mpImpl->mnLockCount > 0)
208 --mpImpl->mnLockCount;
209 if (mpImpl->mnLockCount)
210 commitUpdates();
214 ::std::shared_ptr<ScreenUpdater::UpdateLock> ScreenUpdater::createLock()
216 return ::std::shared_ptr<ScreenUpdater::UpdateLock>(new ::UpdateLock(*this));
220 } // namespace internal
221 } // namespace slideshow
223 namespace {
225 UpdateLock::UpdateLock (
226 ::slideshow::internal::ScreenUpdater& rUpdater)
227 : mrUpdater(rUpdater),
228 mbIsActivated(false)
233 UpdateLock::~UpdateLock()
235 if (mbIsActivated)
236 mrUpdater.unlockUpdates();
240 void UpdateLock::Activate()
242 if ( ! mbIsActivated)
244 mbIsActivated = true;
245 mrUpdater.lockUpdates();
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */