OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 #include "core/animation/AnimationNode.h" | |
33 | |
34 #include "core/animation/AnimationNodeTiming.h" | |
35 #include "core/animation/AnimationPlayer.h" | |
36 #include "core/animation/ComputedTimingProperties.h" | |
37 #include "core/animation/TimingCalculations.h" | |
38 | |
39 namespace blink { | |
40 | |
41 namespace { | |
42 | |
43 Timing::FillMode resolvedFillMode(Timing::FillMode fillMode, bool isAnimation) | |
44 { | |
45 if (fillMode != Timing::FillModeAuto) | |
46 return fillMode; | |
47 if (isAnimation) | |
48 return Timing::FillModeNone; | |
49 return Timing::FillModeBoth; | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 AnimationNode::AnimationNode(const Timing& timing, PassOwnPtrWillBeRawPtr<EventD
elegate> eventDelegate) | |
55 : m_parent(nullptr) | |
56 , m_startTime(0) | |
57 , m_player(nullptr) | |
58 , m_timing(timing) | |
59 , m_eventDelegate(eventDelegate) | |
60 , m_calculated() | |
61 , m_needsUpdate(true) | |
62 , m_lastUpdateTime(nullValue()) | |
63 { | |
64 m_timing.assertValid(); | |
65 } | |
66 | |
67 double AnimationNode::iterationDuration() const | |
68 { | |
69 double result = std::isnan(m_timing.iterationDuration) ? intrinsicIterationD
uration() : m_timing.iterationDuration; | |
70 ASSERT(result >= 0); | |
71 return result; | |
72 } | |
73 | |
74 double AnimationNode::repeatedDuration() const | |
75 { | |
76 const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_tim
ing.iterationCount); | |
77 ASSERT(result >= 0); | |
78 return result; | |
79 } | |
80 | |
81 double AnimationNode::activeDurationInternal() const | |
82 { | |
83 const double result = m_timing.playbackRate | |
84 ? repeatedDuration() / std::abs(m_timing.playbackRate) | |
85 : std::numeric_limits<double>::infinity(); | |
86 ASSERT(result >= 0); | |
87 return result; | |
88 } | |
89 | |
90 void AnimationNode::updateSpecifiedTiming(const Timing& timing) | |
91 { | |
92 // FIXME: Test whether the timing is actually different? | |
93 m_timing = timing; | |
94 invalidate(); | |
95 if (m_player) | |
96 m_player->setOutdated(); | |
97 specifiedTimingChanged(); | |
98 } | |
99 | |
100 void AnimationNode::computedTiming(ComputedTimingProperties& computedTiming) | |
101 { | |
102 // ComputedTimingProperties members. | |
103 computedTiming.setStartTime(startTimeInternal() * 1000); | |
104 computedTiming.setEndTime(endTimeInternal() * 1000); | |
105 computedTiming.setActiveDuration(activeDurationInternal() * 1000); | |
106 | |
107 // FIXME: These should be null if not in effect, but current dictionary API | |
108 // will treat these as undefined. | |
109 if (ensureCalculated().isInEffect) { | |
110 computedTiming.setLocalTime(ensureCalculated().localTime * 1000); | |
111 computedTiming.setTimeFraction(ensureCalculated().timeFraction); | |
112 computedTiming.setCurrentIteration(ensureCalculated().currentIteration); | |
113 } | |
114 | |
115 // AnimationTimingProperties members. | |
116 computedTiming.setDelay(specifiedTiming().startDelay * 1000); | |
117 computedTiming.setEndDelay(specifiedTiming().endDelay * 1000); | |
118 computedTiming.setFill(Timing::fillModeString(resolvedFillMode(specifiedTimi
ng().fillMode, isAnimation()))); | |
119 computedTiming.setIterationStart(specifiedTiming().iterationStart); | |
120 computedTiming.setIterations(specifiedTiming().iterationCount); | |
121 | |
122 UnrestrictedDoubleOrString duration; | |
123 duration.setUnrestrictedDouble(iterationDuration() * 1000); | |
124 computedTiming.setDuration(duration); | |
125 | |
126 computedTiming.setPlaybackRate(specifiedTiming().playbackRate); | |
127 computedTiming.setDirection(Timing::playbackDirectionString(specifiedTiming(
).direction)); | |
128 computedTiming.setEasing(specifiedTiming().timingFunction->toString()); | |
129 } | |
130 | |
131 ComputedTimingProperties AnimationNode::computedTiming() | |
132 { | |
133 ComputedTimingProperties result; | |
134 computedTiming(result); | |
135 return result; | |
136 } | |
137 | |
138 | |
139 void AnimationNode::updateInheritedTime(double inheritedTime, TimingUpdateReason
reason) const | |
140 { | |
141 bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(
isNull(m_lastUpdateTime) && isNull(inheritedTime))); | |
142 m_needsUpdate = false; | |
143 m_lastUpdateTime = inheritedTime; | |
144 | |
145 const double localTime = inheritedTime - m_startTime; | |
146 double timeToNextIteration = std::numeric_limits<double>::infinity(); | |
147 if (needsUpdate) { | |
148 const double activeDuration = this->activeDurationInternal(); | |
149 | |
150 const Phase currentPhase = calculatePhase(activeDuration, localTime, m_t
iming); | |
151 // FIXME: parentPhase depends on groups being implemented. | |
152 const AnimationNode::Phase parentPhase = AnimationNode::PhaseActive; | |
153 const double activeTime = calculateActiveTime(activeDuration, resolvedFi
llMode(m_timing.fillMode, isAnimation()), localTime, parentPhase, currentPhase,
m_timing); | |
154 | |
155 double currentIteration; | |
156 double timeFraction; | |
157 if (const double iterationDuration = this->iterationDuration()) { | |
158 const double startOffset = multiplyZeroAlwaysGivesZero(m_timing.iter
ationStart, iterationDuration); | |
159 ASSERT(startOffset >= 0); | |
160 const double scaledActiveTime = calculateScaledActiveTime(activeDura
tion, activeTime, startOffset, m_timing); | |
161 const double iterationTime = calculateIterationTime(iterationDuratio
n, repeatedDuration(), scaledActiveTime, startOffset, m_timing); | |
162 | |
163 currentIteration = calculateCurrentIteration(iterationDuration, iter
ationTime, scaledActiveTime, m_timing); | |
164 timeFraction = calculateTransformedTime(currentIteration, iterationD
uration, iterationTime, m_timing) / iterationDuration; | |
165 | |
166 if (!isNull(iterationTime)) { | |
167 timeToNextIteration = (iterationDuration - iterationTime) / std:
:abs(m_timing.playbackRate); | |
168 if (activeDuration - activeTime < timeToNextIteration) | |
169 timeToNextIteration = std::numeric_limits<double>::infinity(
); | |
170 } | |
171 } else { | |
172 const double localIterationDuration = 1; | |
173 const double localRepeatedDuration = localIterationDuration * m_timi
ng.iterationCount; | |
174 ASSERT(localRepeatedDuration >= 0); | |
175 const double localActiveDuration = m_timing.playbackRate ? localRepe
atedDuration / std::abs(m_timing.playbackRate) : std::numeric_limits<double>::in
finity(); | |
176 ASSERT(localActiveDuration >= 0); | |
177 const double localLocalTime = localTime < m_timing.startDelay ? loca
lTime : localActiveDuration + m_timing.startDelay; | |
178 const AnimationNode::Phase localCurrentPhase = calculatePhase(localA
ctiveDuration, localLocalTime, m_timing); | |
179 const double localActiveTime = calculateActiveTime(localActiveDurati
on, resolvedFillMode(m_timing.fillMode, isAnimation()), localLocalTime, parentPh
ase, localCurrentPhase, m_timing); | |
180 const double startOffset = m_timing.iterationStart * localIterationD
uration; | |
181 ASSERT(startOffset >= 0); | |
182 const double scaledActiveTime = calculateScaledActiveTime(localActiv
eDuration, localActiveTime, startOffset, m_timing); | |
183 const double iterationTime = calculateIterationTime(localIterationDu
ration, localRepeatedDuration, scaledActiveTime, startOffset, m_timing); | |
184 | |
185 currentIteration = calculateCurrentIteration(localIterationDuration,
iterationTime, scaledActiveTime, m_timing); | |
186 timeFraction = calculateTransformedTime(currentIteration, localItera
tionDuration, iterationTime, m_timing); | |
187 } | |
188 | |
189 m_calculated.currentIteration = currentIteration; | |
190 m_calculated.timeFraction = timeFraction; | |
191 | |
192 m_calculated.phase = currentPhase; | |
193 m_calculated.isInEffect = !isNull(activeTime); | |
194 m_calculated.isInPlay = phase() == PhaseActive && (!m_parent || m_parent
->isInPlay()); | |
195 m_calculated.isCurrent = phase() == PhaseBefore || isInPlay() || (m_pare
nt && m_parent->isCurrent()); | |
196 m_calculated.localTime = m_lastUpdateTime - m_startTime; | |
197 } | |
198 | |
199 // Test for events even if timing didn't need an update as the player may ha
ve gained a start time. | |
200 // FIXME: Refactor so that we can ASSERT(m_player) here, this is currently r
equired to be nullable for testing. | |
201 if (reason == TimingUpdateForAnimationFrame && (!m_player || m_player->hasSt
artTime() || m_player->paused())) { | |
202 if (m_eventDelegate) | |
203 m_eventDelegate->onEventCondition(*this); | |
204 } | |
205 | |
206 if (needsUpdate) { | |
207 // FIXME: This probably shouldn't be recursive. | |
208 updateChildrenAndEffects(); | |
209 m_calculated.timeToForwardsEffectChange = calculateTimeToEffectChange(tr
ue, localTime, timeToNextIteration); | |
210 m_calculated.timeToReverseEffectChange = calculateTimeToEffectChange(fal
se, localTime, timeToNextIteration); | |
211 } | |
212 } | |
213 | |
214 const AnimationNode::CalculatedTiming& AnimationNode::ensureCalculated() const | |
215 { | |
216 if (!m_player) | |
217 return m_calculated; | |
218 if (m_player->outdated()) | |
219 m_player->update(TimingUpdateOnDemand); | |
220 ASSERT(!m_player->outdated()); | |
221 return m_calculated; | |
222 } | |
223 | |
224 PassRefPtrWillBeRawPtr<AnimationNodeTiming> AnimationNode::timing() | |
225 { | |
226 return AnimationNodeTiming::create(this); | |
227 } | |
228 | |
229 DEFINE_TRACE(AnimationNode) | |
230 { | |
231 visitor->trace(m_parent); | |
232 visitor->trace(m_player); | |
233 visitor->trace(m_eventDelegate); | |
234 } | |
235 | |
236 } // namespace blink | |
OLD | NEW |