fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / slideshow / source / engine / activities / discreteactivitybase.cxx
blobcc5c01c3e86c5e56333bae8f450cef9e5b9f6205
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 .
21 // must be first
22 #include <canvas/debug.hxx>
23 #include <tools/diagnose_ex.h>
24 #include <canvas/verbosetrace.hxx>
26 #include <discreteactivitybase.hxx>
29 namespace slideshow
31 namespace internal
33 DiscreteActivityBase::DiscreteActivityBase( const ActivityParameters& rParms ) :
34 ActivityBase( rParms ),
35 mpWakeupEvent( rParms.mpWakeupEvent ),
36 maDiscreteTimes( rParms.maDiscreteTimes ),
37 mnSimpleDuration( rParms.mnMinDuration ),
38 mnCurrPerformCalls( 0 )
40 ENSURE_OR_THROW( mpWakeupEvent,
41 "DiscreteActivityBase::DiscreteActivityBase(): Invalid wakeup event" );
43 ENSURE_OR_THROW( !maDiscreteTimes.empty(),
44 "DiscreteActivityBase::DiscreteActivityBase(): time vector is empty, why do you create me?" );
46 #ifdef DBG_UTIL
47 // check parameters: rDiscreteTimes must be sorted in
48 // ascending order, and contain values only from the range
49 // [0,1]
50 for( ::std::size_t i=1, len=maDiscreteTimes.size(); i<len; ++i )
52 if( maDiscreteTimes[i] < 0.0 ||
53 maDiscreteTimes[i] > 1.0 ||
54 maDiscreteTimes[i-1] < 0.0 ||
55 maDiscreteTimes[i-1] > 1.0 )
57 ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time values not within [0,1] range!" );
60 if( maDiscreteTimes[i-1] > maDiscreteTimes[i] )
61 ENSURE_OR_THROW( false, "DiscreteActivityBase::DiscreteActivityBase(): time vector is not sorted in ascending order!" );
64 // TODO(E2): check this also in production code?
65 #endif
68 void DiscreteActivityBase::startAnimation()
70 // start timer on wakeup event
71 mpWakeupEvent->start();
74 sal_uInt32 DiscreteActivityBase::calcFrameIndex( sal_uInt32 nCurrCalls,
75 ::std::size_t nVectorSize ) const
77 if( isAutoReverse() )
79 // every full repeat run consists of one
80 // forward and one backward traversal.
81 sal_uInt32 nFrameIndex( nCurrCalls % (2*nVectorSize) );
83 // nFrameIndex values >= nVectorSize belong to
84 // the backward traversal
85 if( nFrameIndex >= nVectorSize )
86 nFrameIndex = 2*nVectorSize - nFrameIndex; // invert sweep
88 return nFrameIndex;
90 else
92 return nCurrCalls % nVectorSize ;
96 sal_uInt32 DiscreteActivityBase::calcRepeatCount( sal_uInt32 nCurrCalls,
97 ::std::size_t nVectorSize ) const
99 if( isAutoReverse() )
100 return nCurrCalls / (2*nVectorSize); // we've got 2 cycles per repeat
101 else
102 return nCurrCalls / nVectorSize;
105 bool DiscreteActivityBase::perform()
107 // call base class, for start() calls and end handling
108 if( !ActivityBase::perform() )
109 return false; // done, we're ended
111 const ::std::size_t nVectorSize( maDiscreteTimes.size() );
113 // actually perform something
114 // ==========================
116 // TODO(Q3): Refactor this mess
118 // call derived class with current frame index (modulo
119 // vector size, to cope with repeats)
120 perform( calcFrameIndex( mnCurrPerformCalls, nVectorSize ),
121 calcRepeatCount( mnCurrPerformCalls, nVectorSize ) );
123 // calc next index
124 ++mnCurrPerformCalls;
126 // calc currently reached repeat count
127 double nCurrRepeat( double(mnCurrPerformCalls) / nVectorSize );
129 // if auto-reverse is specified, halve the
130 // effective repeat count, since we pass every
131 // repeat run twice: once forward, once backward.
132 if( isAutoReverse() )
133 nCurrRepeat /= 2.0;
135 // schedule next frame, if either repeat is indefinite
136 // (repeat forever), or we've not yet reached the requested
137 // repeat count
138 if( !isRepeatCountValid() ||
139 nCurrRepeat < getRepeatCount() )
141 // add wake-up event to queue (modulo
142 // vector size, to cope with repeats).
144 // repeat is handled locally, only apply acceleration/deceleration.
145 // Scale time vector with simple duration, offset with full repeat
146 // times.
148 // Somewhat condensed, the argument for setNextTimeout below could
149 // be written as
151 // mnSimpleDuration*(nFullRepeats + calcAcceleratedTime( currentRepeatTime )),
153 // with currentRepeatTime = maDiscreteTimes[ currentRepeatIndex ]
155 // Note that calcAcceleratedTime() is only applied to the current repeat's value,
156 // not to the total resulting time. This is in accordance with the SMIL spec.
158 mpWakeupEvent->setNextTimeout(
159 mnSimpleDuration*(
160 calcRepeatCount(
161 mnCurrPerformCalls,
162 nVectorSize ) +
163 calcAcceleratedTime(
164 maDiscreteTimes[
165 calcFrameIndex(
166 mnCurrPerformCalls,
167 nVectorSize ) ] ) ) );
169 getEventQueue().addEvent( mpWakeupEvent );
171 else
173 // release event reference (relation to wakeup event
174 // is circular!)
175 mpWakeupEvent.reset();
177 // done with this activity
178 endActivity();
181 return false; // remove from queue, will be added back by the wakeup event.
184 void DiscreteActivityBase::dispose()
186 // dispose event
187 if( mpWakeupEvent )
188 mpWakeupEvent->dispose();
190 // release references
191 mpWakeupEvent.reset();
193 ActivityBase::dispose();
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */