Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / TimedItem.h
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 #ifndef TimedItem_h
32 #define TimedItem_h
33
34 #include "core/animation/Timing.h"
35 #include "wtf/OwnPtr.h"
36 #include "wtf/PassOwnPtr.h"
37 #include "wtf/RefCounted.h"
38
39 namespace WebCore {
40
41 class AnimationPlayer;
42 class TimedItem;
43 class TimedItemTiming;
44
45 enum TimingUpdateReason {
46     TimingUpdateOnDemand,
47     TimingUpdateForAnimationFrame
48 };
49
50 static inline bool isNull(double value)
51 {
52     return std::isnan(value);
53 }
54
55 static inline double nullValue()
56 {
57     return std::numeric_limits<double>::quiet_NaN();
58 }
59
60 class TimedItem : public RefCounted<TimedItem> {
61     friend class AnimationPlayer; // Calls attach/detach, updateInheritedTime.
62 public:
63     // Note that logic in CSSAnimations depends on the order of these values.
64     enum Phase {
65         PhaseBefore,
66         PhaseActive,
67         PhaseAfter,
68         PhaseNone,
69     };
70
71     class EventDelegate {
72     public:
73         virtual ~EventDelegate() { };
74         virtual void onEventCondition(const TimedItem*) = 0;
75     };
76
77     virtual ~TimedItem() { }
78
79     virtual bool isAnimation() const { return false; }
80
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; }
87
88     double currentIteration() const { return ensureCalculated().currentIteration; }
89     double iterationDuration() const;
90
91     // This method returns time in ms as it is unused except via the API.
92     double duration() const { return iterationDuration() * 1000; }
93
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; }
101
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&);
108
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; }
112
113 protected:
114     TimedItem(const Timing&, PassOwnPtr<EventDelegate> = nullptr);
115
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; }
123
124 private:
125
126     double repeatedDuration() const;
127
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() { };
134
135     void attach(AnimationPlayer* player)
136     {
137         m_player = player;
138         didAttach();
139     };
140
141     void detach()
142     {
143         ASSERT(m_player);
144         willDetach();
145         m_player = 0;
146     };
147
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;
152     Timing m_timing;
153     OwnPtr<EventDelegate> m_eventDelegate;
154
155     mutable struct CalculatedTiming {
156         Phase phase;
157         double currentIteration;
158         double timeFraction;
159         bool isCurrent;
160         bool isInEffect;
161         bool isInPlay;
162         double localTime;
163         double timeToForwardsEffectChange;
164         double timeToReverseEffectChange;
165     } m_calculated;
166     mutable bool m_needsUpdate;
167     mutable double m_lastUpdateTime;
168
169     const CalculatedTiming& ensureCalculated() const;
170 };
171
172 } // namespace WebCore
173
174 #endif