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.
34 #include "core/animation/Timing.h"
35 #include "wtf/OwnPtr.h"
36 #include "wtf/PassOwnPtr.h"
37 #include "wtf/RefCounted.h"
41 class AnimationPlayer;
43 class TimedItemTiming;
45 enum TimingUpdateReason {
47 TimingUpdateForAnimationFrame
50 static inline bool isNull(double value)
52 return std::isnan(value);
55 static inline double nullValue()
57 return std::numeric_limits<double>::quiet_NaN();
60 class TimedItem : public RefCounted<TimedItem> {
61 friend class AnimationPlayer; // Calls attach/detach, updateInheritedTime.
63 // Note that logic in CSSAnimations depends on the order of these values.
73 virtual ~EventDelegate() { };
74 virtual void onEventCondition(const TimedItem*) = 0;
77 virtual ~TimedItem() { }
79 virtual bool isAnimation() const { return false; }
81 Phase phase() const { return ensureCalculated().phase; }
82 bool isCurrent() const { return ensureCalculated().isCurrent; }
83 bool isInEffect() const { return ensureCalculated().isInEffect; }
84 bool isInPlay() const { return ensureCalculated().isInPlay; }
85 double timeToForwardsEffectChange() const { return ensureCalculated().timeToForwardsEffectChange; }
86 double timeToReverseEffectChange() const { return ensureCalculated().timeToReverseEffectChange; }
88 double currentIteration() const { return ensureCalculated().currentIteration; }
89 double iterationDuration() const;
91 // This method returns time in ms as it is unused except via the API.
92 double duration() const { return iterationDuration() * 1000; }
94 double activeDuration() const { return activeDurationInternal() * 1000; }
95 double activeDurationInternal() const;
96 double timeFraction() const { return ensureCalculated().timeFraction; }
97 double startTime() const { return m_startTime * 1000; }
98 double startTimeInternal() const { return m_startTime; }
99 double endTime() const { return endTimeInternal() * 1000; }
100 double endTimeInternal() const { return startTime() + specifiedTiming().startDelay + activeDurationInternal() + specifiedTiming().endDelay; }
102 const AnimationPlayer* player() const { return m_player; }
103 AnimationPlayer* player() { return m_player; }
104 AnimationPlayer* player(bool& isNull) { isNull = !m_player; return m_player; }
105 const Timing& specifiedTiming() const { return m_timing; }
106 PassRefPtr<TimedItemTiming> timing();
107 void updateSpecifiedTiming(const Timing&);
109 // This method returns time in ms as it is unused except via the API.
110 double localTime(bool& isNull) const { isNull = !m_player; return ensureCalculated().localTime * 1000; }
111 double currentIteration(bool& isNull) const { isNull = !ensureCalculated().isInEffect; return ensureCalculated().currentIteration; }
114 TimedItem(const Timing&, PassOwnPtr<EventDelegate> = nullptr);
116 // When TimedItem receives a new inherited time via updateInheritedTime
117 // it will (if necessary) recalculate timings and (if necessary) call
118 // updateChildrenAndEffects.
119 void updateInheritedTime(double inheritedTime, TimingUpdateReason) const;
120 void invalidate() const { m_needsUpdate = true; };
121 bool hasEvents() const { return m_eventDelegate; }
122 void clearEventDelegate() { m_eventDelegate = nullptr; }
126 double repeatedDuration() const;
128 virtual void updateChildrenAndEffects() const = 0;
129 virtual double intrinsicIterationDuration() const { return 0; };
130 virtual double calculateTimeToEffectChange(bool forwards, double localTime, double timeToNextIteration) const = 0;
131 virtual void didAttach() { };
132 virtual void willDetach() { };
133 virtual void specifiedTimingChanged() { };
135 void attach(AnimationPlayer* player)
148 // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
149 TimedItem* const m_parent;
150 const double m_startTime;
151 AnimationPlayer* m_player;
153 OwnPtr<EventDelegate> m_eventDelegate;
155 mutable struct CalculatedTiming {
157 double currentIteration;
163 double timeToForwardsEffectChange;
164 double timeToReverseEffectChange;
166 mutable bool m_needsUpdate;
167 mutable double m_lastUpdateTime;
169 const CalculatedTiming& ensureCalculated() const;
172 } // namespace WebCore