c25140341fac887d38f21bbd523ffc94c2cd5d5f
[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 TimedItem::TimedItem(const Timing& timing, PassOwnPtr<EventDelegate> eventDelegate)
40     : m_parent(0)
41     , m_startTime(0)
42     , m_player(0)
43     , m_specified(timing)
44     , m_eventDelegate(eventDelegate)
45     , m_calculated()
46     , m_isFirstSample(true)
47     , m_needsUpdate(true)
48     , m_lastUpdateTime(nullValue())
49 {
50     m_specified.assertValid();
51 }
52
53 double TimedItem::iterationDuration() const
54 {
55     double result = m_specified.hasIterationDuration ? m_specified.iterationDuration : intrinsicIterationDuration();
56     ASSERT(result >= 0);
57     return result;
58 }
59
60 double TimedItem::repeatedDuration() const
61 {
62     const double result = multiplyZeroAlwaysGivesZero(iterationDuration(), m_specified.iterationCount);
63     ASSERT(result >= 0);
64     return result;
65 }
66
67 double TimedItem::activeDuration() const
68 {
69     const double result = m_specified.playbackRate
70         ? repeatedDuration() / abs(m_specified.playbackRate)
71         : std::numeric_limits<double>::infinity();
72     ASSERT(result >= 0);
73     return result;
74 }
75
76 bool TimedItem::updateInheritedTime(double inheritedTime) const
77 {
78     bool needsUpdate = m_needsUpdate || (m_lastUpdateTime != inheritedTime && !(isNull(m_lastUpdateTime) && isNull(inheritedTime)));
79     m_needsUpdate = false;
80     m_lastUpdateTime = inheritedTime;
81
82     const double previousIteration = m_calculated.currentIteration;
83     const Phase previousPhase = m_calculated.phase;
84
85     const double localTime = inheritedTime - m_startTime;
86     double timeToNextIteration = std::numeric_limits<double>::infinity();
87     if (needsUpdate) {
88         const double activeDuration = this->activeDuration();
89
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);
94
95         double currentIteration;
96         double timeFraction;
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);
102
103             currentIteration = calculateCurrentIteration(iterationDuration, iterationTime, scaledActiveTime, m_specified);
104             timeFraction = calculateTransformedTime(currentIteration, iterationDuration, iterationTime, m_specified) / iterationDuration;
105
106             if (!isNull(iterationTime)) {
107                 timeToNextIteration = (iterationDuration - iterationTime) / abs(m_specified.playbackRate);
108                 if (activeDuration - activeTime < timeToNextIteration)
109                     timeToNextIteration = std::numeric_limits<double>::infinity();
110             }
111         } else {
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);
124
125             currentIteration = calculateCurrentIteration(localIterationDuration, iterationTime, scaledActiveTime, m_specified);
126             timeFraction = calculateTransformedTime(currentIteration, localIterationDuration, iterationTime, m_specified);
127         }
128
129         m_calculated.currentIteration = currentIteration;
130         m_calculated.timeFraction = timeFraction;
131
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());
136     }
137
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;
146     }
147
148     bool didTriggerStyleRecalc = false;
149     if (needsUpdate)  {
150         // FIXME: This probably shouldn't be recursive.
151         didTriggerStyleRecalc = updateChildrenAndEffects();
152         m_calculated.timeToEffectChange = calculateTimeToEffectChange(localTime, timeToNextIteration);
153     }
154     return didTriggerStyleRecalc;
155 }
156
157 } // namespace WebCore