| 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 "core/animation/AnimationEffect.h" | |
| 32 | |
| 33 #include "core/animation/Animation.h" | |
| 34 #include "core/animation/AnimationEffectTiming.h" | |
| 35 #include "core/animation/ComputedTimingProperties.h" | |
| 36 #include "core/animation/TimingCalculations.h" | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 namespace { | |
| 41 | |
| 42 Timing::FillMode resolvedFillMode(Timing::FillMode fillMode, bool isAnimation) | |
| 43 { | |
| 44 if (fillMode != Timing::FillMode::AUTO) | |
| 45 return fillMode; | |
| 46 if (isAnimation) | |
| 47 return Timing::FillMode::NONE; | |
| 48 return Timing::FillMode::BOTH; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 AnimationEffect::AnimationEffect(const Timing& timing, EventDelegate* eventDeleg
ate) | |
| 54 : m_animation(nullptr) | |
| 55 , m_timing(timing) | |
| 56 , m_eventDelegate(eventDelegate) | |
| 57 , m_calculated() | |
| 58 , m_needsUpdate(true) | |
| 59 , m_lastUpdateTime(nullValue()) | |
| 60 { | |
| 61 m_timing.assertValid(); | |
| 62 } | |
| 63 | |
| 64 double AnimationEffect::iterationDuration() const | |
| 65 { | |
| 66 double result = std::isnan(m_timing.iterationDuration) ? intrinsicIterationD
uration() : m_timing.iterationDuration; | |
| 67 DCHECK_GE(result, 0); | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 double AnimationEffect::repeatedDuration() const | |
| 72 { | |
| 73 const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_tim
ing.iterationCount); | |
| 74 DCHECK_GE(result, 0); | |
| 75 return result; | |
| 76 } | |
| 77 | |
| 78 double AnimationEffect::activeDurationInternal() const | |
| 79 { | |
| 80 const double result = m_timing.playbackRate | |
| 81 ? repeatedDuration() / std::abs(m_timing.playbackRate) | |
| 82 : std::numeric_limits<double>::infinity(); | |
| 83 DCHECK_GE(result, 0); | |
| 84 return result; | |
| 85 } | |
| 86 | |
| 87 void AnimationEffect::updateSpecifiedTiming(const Timing& timing) | |
| 88 { | |
| 89 // FIXME: Test whether the timing is actually different? | |
| 90 m_timing = timing; | |
| 91 invalidate(); | |
| 92 if (m_animation) | |
| 93 m_animation->setOutdated(); | |
| 94 specifiedTimingChanged(); | |
| 95 } | |
| 96 | |
| 97 void AnimationEffect::getComputedTiming(ComputedTimingProperties& computedTiming
) | |
| 98 { | |
| 99 // ComputedTimingProperties members. | |
| 100 computedTiming.setEndTime(endTimeInternal() * 1000); | |
| 101 computedTiming.setActiveDuration(activeDurationInternal() * 1000); | |
| 102 | |
| 103 if (ensureCalculated().isInEffect) { | |
| 104 computedTiming.setLocalTime(ensureCalculated().localTime * 1000); | |
| 105 computedTiming.setProgress(ensureCalculated().progress); | |
| 106 computedTiming.setCurrentIteration(ensureCalculated().currentIteration); | |
| 107 } else { | |
| 108 computedTiming.setLocalTimeToNull(); | |
| 109 computedTiming.setProgressToNull(); | |
| 110 computedTiming.setCurrentIterationToNull(); | |
| 111 } | |
| 112 | |
| 113 // KeyframeEffectOptions members. | |
| 114 computedTiming.setDelay(specifiedTiming().startDelay * 1000); | |
| 115 computedTiming.setEndDelay(specifiedTiming().endDelay * 1000); | |
| 116 computedTiming.setFill(Timing::fillModeString(resolvedFillMode(specifiedTimi
ng().fillMode, isKeyframeEffect()))); | |
| 117 computedTiming.setIterationStart(specifiedTiming().iterationStart); | |
| 118 computedTiming.setIterations(specifiedTiming().iterationCount); | |
| 119 | |
| 120 UnrestrictedDoubleOrString duration; | |
| 121 duration.setUnrestrictedDouble(iterationDuration() * 1000); | |
| 122 computedTiming.setDuration(duration); | |
| 123 | |
| 124 computedTiming.setDirection(Timing::playbackDirectionString(specifiedTiming(
).direction)); | |
| 125 computedTiming.setEasing(specifiedTiming().timingFunction->toString()); | |
| 126 } | |
| 127 | |
| 128 ComputedTimingProperties AnimationEffect::getComputedTiming() | |
| 129 { | |
| 130 ComputedTimingProperties result; | |
| 131 getComputedTiming(result); | |
| 132 return result; | |
| 133 } | |
| 134 | |
| 135 | |
| 136 void AnimationEffect::updateInheritedTime(double inheritedTime, TimingUpdateReas
on reason) const | |
| 137 { | |
| 138 bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(
isNull(m_lastUpdateTime) && isNull(inheritedTime))) || (animation() && animation
()->effectSuppressed()); | |
| 139 m_needsUpdate = false; | |
| 140 m_lastUpdateTime = inheritedTime; | |
| 141 | |
| 142 const double localTime = inheritedTime; | |
| 143 double timeToNextIteration = std::numeric_limits<double>::infinity(); | |
| 144 if (needsUpdate) { | |
| 145 const double activeDuration = this->activeDurationInternal(); | |
| 146 | |
| 147 const Phase currentPhase = calculatePhase(activeDuration, localTime, m_t
iming); | |
| 148 // FIXME: parentPhase depends on groups being implemented. | |
| 149 const AnimationEffect::Phase parentPhase = AnimationEffect::PhaseActive; | |
| 150 const double activeTime = calculateActiveTime(activeDuration, resolvedFi
llMode(m_timing.fillMode, isKeyframeEffect()), localTime, parentPhase, currentPh
ase, m_timing); | |
| 151 | |
| 152 double currentIteration; | |
| 153 double progress; | |
| 154 if (const double iterationDuration = this->iterationDuration()) { | |
| 155 const double startOffset = multiplyZeroAlwaysGivesZero(m_timing.iter
ationStart, iterationDuration); | |
| 156 DCHECK_GE(startOffset, 0); | |
| 157 const double scaledActiveTime = calculateScaledActiveTime(activeDura
tion, activeTime, startOffset, m_timing); | |
| 158 const double iterationTime = calculateIterationTime(iterationDuratio
n, repeatedDuration(), scaledActiveTime, startOffset, currentPhase, m_timing); | |
| 159 | |
| 160 currentIteration = calculateCurrentIteration(iterationDuration, iter
ationTime, scaledActiveTime, m_timing); | |
| 161 const double transformedTime = calculateTransformedTime(currentItera
tion, iterationDuration, iterationTime, m_timing); | |
| 162 | |
| 163 // The infinite iterationDuration case here is a workaround because | |
| 164 // the specified behaviour does not handle infinite durations well. | |
| 165 // There is an open issue against the spec to fix this: | |
| 166 // https://github.com/w3c/web-animations/issues/142 | |
| 167 if (!std::isfinite(iterationDuration)) | |
| 168 progress = fmod(m_timing.iterationStart, 1.0); | |
| 169 else | |
| 170 progress = transformedTime / iterationDuration; | |
| 171 | |
| 172 if (!isNull(iterationTime)) { | |
| 173 timeToNextIteration = (iterationDuration - iterationTime) / std:
:abs(m_timing.playbackRate); | |
| 174 if (activeDuration - activeTime < timeToNextIteration) | |
| 175 timeToNextIteration = std::numeric_limits<double>::infinity(
); | |
| 176 } | |
| 177 } else { | |
| 178 const double localIterationDuration = 1; | |
| 179 const double localRepeatedDuration = localIterationDuration * m_timi
ng.iterationCount; | |
| 180 DCHECK_GE(localRepeatedDuration, 0); | |
| 181 const double localActiveDuration = m_timing.playbackRate ? localRepe
atedDuration / std::abs(m_timing.playbackRate) : std::numeric_limits<double>::in
finity(); | |
| 182 DCHECK_GE(localActiveDuration, 0); | |
| 183 const double localLocalTime = localTime < m_timing.startDelay ? loca
lTime : localActiveDuration + m_timing.startDelay; | |
| 184 const AnimationEffect::Phase localCurrentPhase = calculatePhase(loca
lActiveDuration, localLocalTime, m_timing); | |
| 185 const double localActiveTime = calculateActiveTime(localActiveDurati
on, resolvedFillMode(m_timing.fillMode, isKeyframeEffect()), localLocalTime, par
entPhase, localCurrentPhase, m_timing); | |
| 186 const double startOffset = m_timing.iterationStart * localIterationD
uration; | |
| 187 DCHECK_GE(startOffset, 0); | |
| 188 const double scaledActiveTime = calculateScaledActiveTime(localActiv
eDuration, localActiveTime, startOffset, m_timing); | |
| 189 const double iterationTime = calculateIterationTime(localIterationDu
ration, localRepeatedDuration, scaledActiveTime, startOffset, currentPhase, m_ti
ming); | |
| 190 | |
| 191 currentIteration = calculateCurrentIteration(localIterationDuration,
iterationTime, scaledActiveTime, m_timing); | |
| 192 progress = calculateTransformedTime(currentIteration, localIteration
Duration, iterationTime, m_timing); | |
| 193 } | |
| 194 | |
| 195 m_calculated.currentIteration = currentIteration; | |
| 196 m_calculated.progress = progress; | |
| 197 | |
| 198 m_calculated.phase = currentPhase; | |
| 199 m_calculated.isInEffect = !isNull(activeTime); | |
| 200 m_calculated.isInPlay = getPhase() == PhaseActive; | |
| 201 m_calculated.isCurrent = getPhase() == PhaseBefore || isInPlay(); | |
| 202 m_calculated.localTime = m_lastUpdateTime; | |
| 203 } | |
| 204 | |
| 205 // Test for events even if timing didn't need an update as the animation may
have gained a start time. | |
| 206 // FIXME: Refactor so that we can DCHECK(m_animation) here, this is currentl
y required to be nullable for testing. | |
| 207 if (reason == TimingUpdateForAnimationFrame && (!m_animation || m_animation-
>hasStartTime() || m_animation->paused())) { | |
| 208 if (m_eventDelegate) | |
| 209 m_eventDelegate->onEventCondition(*this); | |
| 210 } | |
| 211 | |
| 212 if (needsUpdate) { | |
| 213 // FIXME: This probably shouldn't be recursive. | |
| 214 updateChildrenAndEffects(); | |
| 215 m_calculated.timeToForwardsEffectChange = calculateTimeToEffectChange(tr
ue, localTime, timeToNextIteration); | |
| 216 m_calculated.timeToReverseEffectChange = calculateTimeToEffectChange(fal
se, localTime, timeToNextIteration); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 const AnimationEffect::CalculatedTiming& AnimationEffect::ensureCalculated() con
st | |
| 221 { | |
| 222 if (!m_animation) | |
| 223 return m_calculated; | |
| 224 if (m_animation->outdated()) | |
| 225 m_animation->update(TimingUpdateOnDemand); | |
| 226 DCHECK(!m_animation->outdated()); | |
| 227 return m_calculated; | |
| 228 } | |
| 229 | |
| 230 AnimationEffectTiming* AnimationEffect::timing() | |
| 231 { | |
| 232 return AnimationEffectTiming::create(this); | |
| 233 } | |
| 234 | |
| 235 DEFINE_TRACE(AnimationEffect) | |
| 236 { | |
| 237 visitor->trace(m_animation); | |
| 238 visitor->trace(m_eventDelegate); | |
| 239 } | |
| 240 | |
| 241 } // namespace blink | |
| OLD | NEW |