Upstream version 5.34.98.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 Player;
42 class TimedItem;
43
44 static inline bool isNull(double value)
45 {
46     return std::isnan(value);
47 }
48
49 static inline double nullValue()
50 {
51     return std::numeric_limits<double>::quiet_NaN();
52 }
53
54 class TimedItem : public RefCounted<TimedItem> {
55     friend class Player; // Calls attach/detach, updateInheritedTime.
56 public:
57     // Note that logic in CSSAnimations depends on the order of these values.
58     enum Phase {
59         PhaseBefore,
60         PhaseActive,
61         PhaseAfter,
62         PhaseNone,
63     };
64
65     class EventDelegate {
66     public:
67         virtual ~EventDelegate() { };
68         virtual void onEventCondition(const TimedItem*, bool isFirstSample, Phase previousPhase, double previousIteration) = 0;
69     };
70
71     virtual ~TimedItem() { }
72
73     virtual bool isAnimation() const { return false; }
74
75     Phase phase() const { return ensureCalculated().phase; }
76     bool isCurrent() const { return ensureCalculated().isCurrent; }
77     bool isInEffect() const { return ensureCalculated().isInEffect; }
78     bool isInPlay() const { return ensureCalculated().isInPlay; }
79     double timeToEffectChange() const { return ensureCalculated().timeToEffectChange; }
80
81     double currentIteration() const { return ensureCalculated().currentIteration; }
82     double activeDuration() const;
83     double timeFraction() const { return ensureCalculated().timeFraction; }
84     double startTime() const { return m_startTime; }
85     double endTime() const { return startTime() + specified().startDelay + activeDuration(); }
86
87     const Player* player() const { return m_player; }
88     Player* player() { return m_player; }
89     Player* player(bool& isNull) { isNull = !m_player; return m_player; }
90     const Timing& specified() const { return m_specified; }
91
92 protected:
93     TimedItem(const Timing&, PassOwnPtr<EventDelegate> = nullptr);
94
95     // When TimedItem receives a new inherited time via updateInheritedTime
96     // it will (if necessary) recalculate timings and (if necessary) call
97     // updateChildrenAndEffects.
98     // Returns whether style recalc was triggered.
99     bool updateInheritedTime(double inheritedTime) const;
100     void invalidate() const { m_needsUpdate = true; };
101
102 private:
103     double iterationDuration() const;
104     double repeatedDuration() const;
105
106     // Returns whether style recalc was triggered.
107     virtual bool updateChildrenAndEffects() const = 0;
108     virtual double intrinsicIterationDuration() const { return 0; };
109     virtual double calculateTimeToEffectChange(double localTime, double timeToNextIteration) const = 0;
110     virtual void didAttach() { };
111     virtual void willDetach() { };
112
113     void attach(Player* player)
114     {
115         m_player = player;
116         didAttach();
117     };
118
119     void detach()
120     {
121         ASSERT(m_player);
122         willDetach();
123         m_player = 0;
124     };
125
126     // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
127     TimedItem* const m_parent;
128     const double m_startTime;
129     Player* m_player;
130     Timing m_specified;
131     OwnPtr<EventDelegate> m_eventDelegate;
132
133     // FIXME: Should be versioned by monotonic value on player.
134     mutable struct CalculatedTiming {
135         Phase phase;
136         double currentIteration;
137         double timeFraction;
138         bool isCurrent;
139         bool isInEffect;
140         bool isInPlay;
141         double timeToEffectChange;
142     } m_calculated;
143     mutable bool m_isFirstSample;
144     mutable bool m_needsUpdate;
145     mutable double m_lastUpdateTime;
146
147     // FIXME: Should check the version and reinherit time if inconsistent.
148     const CalculatedTiming& ensureCalculated() const { return m_calculated; }
149 };
150
151 } // namespace WebCore
152
153 #endif