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"
39 TimedItem::TimedItem(const Timing& timing, PassOwnPtr<EventDelegate> eventDelegate)
44 , m_eventDelegate(eventDelegate)
46 , m_isFirstSample(true)
48 , m_lastUpdateTime(nullValue())
50 m_specified.assertValid();
53 double TimedItem::iterationDuration() const
55 double result = m_specified.hasIterationDuration ? m_specified.iterationDuration : intrinsicIterationDuration();
60 double TimedItem::repeatedDuration() const
62 const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_specified.iterationCount);
67 double TimedItem::activeDuration() const
69 const double result = m_specified.playbackRate
70 ? repeatedDuration() / abs(m_specified.playbackRate)
71 : std::numeric_limits<double>::infinity();
76 bool TimedItem::updateInheritedTime(double inheritedTime) const
78 bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(isNull(m_lastUpdateTime) && isNull(inheritedTime)));
79 m_needsUpdate = false;
80 m_lastUpdateTime = inheritedTime;
82 const double previousIteration = m_calculated.currentIteration;
83 const Phase previousPhase = m_calculated.phase;
85 const double localTime = inheritedTime - m_startTime;
86 double timeToNextIteration = std::numeric_limits<double>::infinity();
88 const double activeDuration = this->activeDuration();
90 const Phase currentPhase = calculatePhase(activeDuration, localTime, m_specified);
91 // FIXME: parentPhase depends on groups being implemented.
92 const TimedItem::Phase parentPhase = TimedItem::PhaseActive;
93 const double activeTime = calculateActiveTime(activeDuration, localTime, parentPhase, currentPhase, m_specified);
95 double currentIteration;
97 if (const double iterationDuration = this->iterationDuration()) {
98 const double startOffset = multiplyZeroAlwaysGivesZero(m_specified.iterationStart, iterationDuration);
99 ASSERT(startOffset >= 0);
100 const double scaledActiveTime = calculateScaledActiveTime(activeDuration, activeTime, startOffset, m_specified);
101 const double iterationTime = calculateIterationTime(iterationDuration, repeatedDuration(), scaledActiveTime, startOffset, m_specified);
103 currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
104 timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified) / iterationDuration;
106 if (!isNull(iterationTime)) {
107 timeToNextIteration = (iterationDuration - iterationTime) / abs(m_specified.playbackRate);
108 if (activeDuration - activeTime < timeToNextIteration)
109 timeToNextIteration = std::numeric_limits<double>::infinity();
112 const double localIterationDuration = 1;
113 const double localRepeatedDuration = localIterationDuration * m_specified.iterationCount;
114 ASSERT(localRepeatedDuration >= 0);
115 const double localActiveDuration = m_specified.playbackRate ? localRepeatedDuration / abs(m_specified.playbackRate) : std::numeric_limits<double>::infinity();
116 ASSERT(localActiveDuration >= 0);
117 const double localLocalTime = localTime < m_specified.startDelay ? localTime : localActiveDuration + m_specified.startDelay;
118 const TimedItem::Phase localCurrentPhase = calculatePhase(localActiveDuration, localLocalTime, m_specified);
119 const double localActiveTime = calculateActiveTime(localActiveDuration, localLocalTime, parentPhase, localCurrentPhase, m_specified);
120 const double startOffset = m_specified.iterationStart * localIterationDuration;
121 ASSERT(startOffset >= 0);
122 const double scaledActiveTime = calculateScaledActiveTime(localActiveDuration, localActiveTime, startOffset, m_specified);
123 const double iterationTime = calculateIterationTime(localIterationDuration, localRepeatedDuration, scaledActiveTime, startOffset, m_specified);
125 currentIteration = calculateCurrentIteration(localIterationDuration, iterationTime, scaledActiveTime, m_specified);
126 timeFraction = calculateTransformedTime(currentIteration, localIterationDuration, iterationTime, m_specified);
129 m_calculated.currentIteration = currentIteration;
130 m_calculated.timeFraction = timeFraction;
132 m_calculated.phase = currentPhase;
133 m_calculated.isInEffect = !isNull(activeTime);
134 m_calculated.isInPlay = phase() == PhaseActive && (!m_parent || m_parent->isInPlay());
135 m_calculated.isCurrent = phase() == PhaseBefore || isInPlay() || (m_parent && m_parent->isCurrent());
138 // Test for events even if timing didn't need an update as the player may have gained a start time.
139 // FIXME: Refactor so that we can ASSERT(m_player) here, this is currently required to be nullable for testing.
140 if (!m_player || m_player->hasStartTime()) {
141 // This logic is specific to CSS animation events and assumes that all
142 // animations start after the DocumentTimeline has started.
143 if (m_eventDelegate && (m_isFirstSample || previousPhase != phase() || (phase() == PhaseActive && previousIteration != m_calculated.currentIteration)))
144 m_eventDelegate->onEventCondition(this, m_isFirstSample, previousPhase, previousIteration);
145 m_isFirstSample = false;
148 bool didTriggerStyleRecalc = false;
150 // FIXME: This probably shouldn't be recursive.
151 didTriggerStyleRecalc = updateChildrenAndEffects();
152 m_calculated.timeToEffectChange = calculateTimeToEffectChange(localTime, timeToNextIteration);
154 return didTriggerStyleRecalc;
157 } // namespace WebCore