2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "core/animation/TimedItem.h"
34 #include "core/animation/Player.h"
35 #include "core/animation/TimedItemCalculations.h"
41 Timing::FillMode resolvedFillMode(Timing::FillMode fillMode, bool isAnimation)
43 if (fillMode != Timing::FillModeAuto)
46 return Timing::FillModeNone;
47 return Timing::FillModeBoth;
52 TimedItem::TimedItem(const Timing& timing, PassOwnPtr<EventDelegate> eventDelegate)
57 , m_eventDelegate(eventDelegate)
59 , m_isFirstSample(true)
61 , m_lastUpdateTime(nullValue())
63 m_specified.assertValid();
66 double TimedItem::iterationDuration() const
68 double result = std::isnan(m_specified.iterationDuration) ? intrinsicIterationDuration() : m_specified.iterationDuration;
73 double TimedItem::repeatedDuration() const
75 const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_specified.iterationCount);
80 double TimedItem::activeDuration() const
82 const double result = m_specified.playbackRate
83 ? repeatedDuration() / abs(m_specified.playbackRate)
84 : std::numeric_limits<double>::infinity();
89 void TimedItem::updateSpecifiedTiming(const Timing& timing)
94 m_player->setNeedsUpdate();
97 bool TimedItem::updateInheritedTime(double inheritedTime) const
99 bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(isNull(m_lastUpdateTime) && isNull(inheritedTime)));
100 m_needsUpdate = false;
101 m_lastUpdateTime = inheritedTime;
103 const double previousIteration = m_calculated.currentIteration;
104 const Phase previousPhase = m_calculated.phase;
106 const double localTime = inheritedTime - m_startTime;
107 double timeToNextIteration = std::numeric_limits<double>::infinity();
109 const double activeDuration = this->activeDuration();
111 const Phase currentPhase = calculatePhase(activeDuration, localTime, m_specified);
112 // FIXME: parentPhase depends on groups being implemented.
113 const TimedItem::Phase parentPhase = TimedItem::PhaseActive;
114 const double activeTime = calculateActiveTime(activeDuration, resolvedFillMode(m_specified.fillMode, isAnimation()), localTime, parentPhase, currentPhase, m_specified);
116 double currentIteration;
118 if (const double iterationDuration = this->iterationDuration()) {
119 const double startOffset = multiplyZeroAlwaysGivesZero(m_specified.iterationStart, iterationDuration);
120 ASSERT(startOffset >= 0);
121 const double scaledActiveTime = calculateScaledActiveTime(activeDuration, activeTime, startOffset, m_specified);
122 const double iterationTime = calculateIterationTime(iterationDuration, repeatedDuration(), scaledActiveTime, startOffset, m_specified);
124 currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
125 timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified) / iterationDuration;
127 if (!isNull(iterationTime)) {
128 timeToNextIteration = (iterationDuration - iterationTime) / abs(m_specified.playbackRate);
129 if (activeDuration - activeTime < timeToNextIteration)
130 timeToNextIteration = std::numeric_limits<double>::infinity();
133 const double localIterationDuration = 1;
134 const double localRepeatedDuration = localIterationDuration * m_specified.iterationCount;
135 ASSERT(localRepeatedDuration >= 0);
136 const double localActiveDuration = m_specified.playbackRate ? localRepeatedDuration / abs(m_specified.playbackRate) : std::numeric_limits<double>::infinity();
137 ASSERT(localActiveDuration >= 0);
138 const double localLocalTime = localTime < m_specified.startDelay ? localTime : localActiveDuration + m_specified.startDelay;
139 const TimedItem::Phase localCurrentPhase = calculatePhase(localActiveDuration, localLocalTime, m_specified);
140 const double localActiveTime = calculateActiveTime(localActiveDuration, resolvedFillMode(m_specified.fillMode, isAnimation()), localLocalTime, parentPhase, localCurrentPhase, m_specified);
141 const double startOffset = m_specified.iterationStart * localIterationDuration;
142 ASSERT(startOffset >= 0);
143 const double scaledActiveTime = calculateScaledActiveTime(localActiveDuration, localActiveTime, startOffset, m_specified);
144 const double iterationTime = calculateIterationTime(localIterationDuration, localRepeatedDuration, scaledActiveTime, startOffset, m_specified);
146 currentIteration = calculateCurrentIteration(localIterationDuration, iterationTime, scaledActiveTime, m_specified);
147 timeFraction = calculateTransformedTime(currentIteration, localIterationDuration, iterationTime, m_specified);
150 m_calculated.currentIteration = currentIteration;
151 m_calculated.timeFraction = timeFraction;
153 m_calculated.phase = currentPhase;
154 m_calculated.isInEffect = !isNull(activeTime);
155 m_calculated.isInPlay = phase() == PhaseActive && (!m_parent || m_parent->isInPlay());
156 m_calculated.isCurrent = phase() == PhaseBefore || isInPlay() || (m_parent && m_parent->isCurrent());
157 m_calculated.localTime = m_lastUpdateTime - m_startTime;
160 // Test for events even if timing didn't need an update as the player may have gained a start time.
161 // FIXME: Refactor so that we can ASSERT(m_player) here, this is currently required to be nullable for testing.
162 if (!m_player || m_player->hasStartTime()) {
163 // This logic is specific to CSS animation events and assumes that all
164 // animations start after the DocumentTimeline has started.
165 if (m_eventDelegate && (m_isFirstSample || previousPhase != phase() || (phase() == PhaseActive && previousIteration != m_calculated.currentIteration)))
166 m_eventDelegate->onEventCondition(this, m_isFirstSample, previousPhase, previousIteration);
167 m_isFirstSample = false;
170 bool didTriggerStyleRecalc = false;
172 // FIXME: This probably shouldn't be recursive.
173 didTriggerStyleRecalc = updateChildrenAndEffects();
174 m_calculated.timeToForwardsEffectChange = calculateTimeToEffectChange(true, localTime, timeToNextIteration);
175 m_calculated.timeToReverseEffectChange = calculateTimeToEffectChange(false, localTime, timeToNextIteration);
177 return didTriggerStyleRecalc;
180 const TimedItem::CalculatedTiming& TimedItem::ensureCalculated() const
184 if (m_player->needsUpdate())
186 ASSERT(!m_player->needsUpdate());
190 } // namespace WebCore