Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / TimedItem.cpp
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/TimedItem.h"
33
34 #include "core/animation/Player.h"
35 #include "core/animation/TimedItemCalculations.h"
36
37 namespace WebCore {
38
39 namespace {
40
41 Timing::FillMode resolvedFillMode(Timing::FillMode fillMode, bool isAnimation)
42 {
43     if (fillMode != Timing::FillModeAuto)
44         return fillMode;
45     if (isAnimation)
46         return Timing::FillModeNone;
47     return Timing::FillModeBoth;
48 }
49
50 } // namespace
51
52 TimedItem::TimedItem(const Timing& timing, PassOwnPtr<EventDelegate> eventDelegate)
53     : m_parent(0)
54     , m_startTime(0)
55     , m_player(0)
56     , m_specified(timing)
57     , m_eventDelegate(eventDelegate)
58     , m_calculated()
59     , m_isFirstSample(true)
60     , m_needsUpdate(true)
61     , m_lastUpdateTime(nullValue())
62 {
63     m_specified.assertValid();
64 }
65
66 double TimedItem::iterationDuration() const
67 {
68     double result = std::isnan(m_specified.iterationDuration) ? intrinsicIterationDuration() : m_specified.iterationDuration;
69     ASSERT(result >= 0);
70     return result;
71 }
72
73 double TimedItem::repeatedDuration() const
74 {
75     const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_specified.iterationCount);
76     ASSERT(result >= 0);
77     return result;
78 }
79
80 double TimedItem::activeDuration() const
81 {
82     const double result = m_specified.playbackRate
83         ? repeatedDuration() / abs(m_specified.playbackRate)
84         : std::numeric_limits<double>::infinity();
85     ASSERT(result >= 0);
86     return result;
87 }
88
89 void TimedItem::updateSpecifiedTiming(const Timing& timing)
90 {
91     m_specified = timing;
92     invalidate();
93     if (m_player)
94         m_player->setNeedsUpdate();
95 }
96
97 bool TimedItem::updateInheritedTime(double inheritedTime) const
98 {
99     bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(isNull(m_lastUpdateTime) && isNull(inheritedTime)));
100     m_needsUpdate = false;
101     m_lastUpdateTime = inheritedTime;
102
103     const double previousIteration = m_calculated.currentIteration;
104     const Phase previousPhase = m_calculated.phase;
105
106     const double localTime = inheritedTime - m_startTime;
107     double timeToNextIteration = std::numeric_limits<double>::infinity();
108     if (needsUpdate) {
109         const double activeDuration = this->activeDuration();
110
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);
115
116         double currentIteration;
117         double timeFraction;
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);
123
124             currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
125             timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified) / iterationDuration;
126
127             if (!isNull(iterationTime)) {
128                 timeToNextIteration = (iterationDuration - iterationTime) / abs(m_specified.playbackRate);
129                 if (activeDuration - activeTime < timeToNextIteration)
130                     timeToNextIteration = std::numeric_limits<double>::infinity();
131             }
132         } else {
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);
145
146             currentIteration = calculateCurrentIteration(localIterationDuration, iterationTime, scaledActiveTime, m_specified);
147             timeFraction = calculateTransformedTime(currentIteration, localIterationDuration, iterationTime, m_specified);
148         }
149
150         m_calculated.currentIteration = currentIteration;
151         m_calculated.timeFraction = timeFraction;
152
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;
158     }
159
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;
168     }
169
170     bool didTriggerStyleRecalc = false;
171     if (needsUpdate)  {
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);
176     }
177     return didTriggerStyleRecalc;
178 }
179
180 const TimedItem::CalculatedTiming& TimedItem::ensureCalculated() const
181 {
182     if (!m_player)
183         return m_calculated;
184     if (m_player->needsUpdate())
185         m_player->update();
186     ASSERT(!m_player->needsUpdate());
187     return m_calculated;
188 }
189
190 } // namespace WebCore