Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / AnimationPlayer.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 AnimationPlayer_h
32 #define AnimationPlayer_h
33
34 #include "core/animation/AnimationNode.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventTarget.h"
37 #include "wtf/RefPtr.h"
38
39 namespace blink {
40
41 class AnimationTimeline;
42 class ExceptionState;
43
44 class AnimationPlayer final : public RefCountedWillBeGarbageCollectedFinalized<AnimationPlayer>
45     , public ActiveDOMObject
46     , public EventTargetWithInlineData {
47     DEFINE_WRAPPERTYPEINFO();
48     REFCOUNTED_EVENT_TARGET(AnimationPlayer);
49     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AnimationPlayer);
50 public:
51     enum AnimationPlayState {
52         Idle,
53         Pending,
54         Running,
55         Paused,
56         Finished
57     };
58
59     ~AnimationPlayer();
60     static PassRefPtrWillBeRawPtr<AnimationPlayer> create(ExecutionContext*, AnimationTimeline&, AnimationNode*);
61
62     // Returns whether the player is finished.
63     bool update(TimingUpdateReason);
64
65     // timeToEffectChange returns:
66     //  infinity  - if this player is no longer in effect
67     //  0         - if this player requires an update on the next frame
68     //  n         - if this player requires an update after 'n' units of time
69     double timeToEffectChange();
70
71     void cancel();
72
73     double currentTime(bool& isNull);
74     double currentTime();
75     void setCurrentTime(double newCurrentTime);
76
77     double currentTimeInternal() const;
78
79     void setCurrentTimeInternal(double newCurrentTime, TimingUpdateReason = TimingUpdateOnDemand);
80     bool paused() const { return m_paused && !m_isPausedForTesting; }
81     static const char* playStateString(AnimationPlayState);
82     String playState() { return playStateString(playStateInternal()); }
83     AnimationPlayState playStateInternal() const;
84
85     void pause();
86     void play();
87     void reverse();
88     void finish(ExceptionState&);
89     bool finished() const { return m_playState != Idle && limited(currentTimeInternal()); }
90     bool playing() const { return !(playStateInternal() == Idle || finished() || m_paused || m_isPausedForTesting); }
91     // FIXME: Resolve whether finished() should just return the flag, and
92     // remove this method.
93     bool finishedInternal() const { return m_finished; }
94
95     DEFINE_ATTRIBUTE_EVENT_LISTENER(finish);
96
97     virtual const AtomicString& interfaceName() const override;
98     virtual ExecutionContext* executionContext() const override;
99     virtual bool hasPendingActivity() const override;
100     virtual void stop() override;
101     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) override;
102
103     double playbackRate() const;
104     void setPlaybackRate(double);
105     const AnimationTimeline* timeline() const { return m_timeline; }
106     AnimationTimeline* timeline() { return m_timeline; }
107
108 #if !ENABLE(OILPAN)
109     void timelineDestroyed() { m_timeline = nullptr; }
110 #endif
111
112     double calculateStartTime(double currentTime) const;
113     bool hasStartTime() const { return !isNull(m_startTime); }
114     double startTime(bool& isNull) const;
115     double startTime() const;
116     double startTimeInternal() const { return m_startTime; }
117     void setStartTime(double);
118     void setStartTimeInternal(double);
119
120     const AnimationNode* source() const { return m_content.get(); }
121     AnimationNode* source() { return m_content.get(); }
122     void setSource(AnimationNode*);
123
124     // Pausing via this method is not reflected in the value returned by
125     // paused() and must never overlap with pausing via pause().
126     void pauseForTesting(double pauseTime);
127     // This should only be used for CSS
128     void unpause();
129
130     void setOutdated();
131     bool outdated() { return m_outdated; }
132
133     bool canStartAnimationOnCompositor();
134     bool maybeStartAnimationOnCompositor();
135     void cancelAnimationOnCompositor();
136     bool hasActiveAnimationsOnCompositor();
137     void setCompositorPending(bool sourceChanged = false);
138     void notifyCompositorStartTime(double timelineTime);
139     void notifyStartTime(double timelineTime);
140
141
142     void preCommit(bool startOnCompositor);
143     void postCommit(double timelineTime);
144
145     unsigned sequenceNumber() const { return m_sequenceNumber; }
146
147     static bool hasLowerPriority(AnimationPlayer* player1, AnimationPlayer* player2)
148     {
149         return player1->sequenceNumber() < player2->sequenceNumber();
150     }
151
152 #if !ENABLE(OILPAN)
153     // Checks if the AnimationStack is the last reference holder to the Player.
154     // This won't be needed when AnimationPlayer is moved to Oilpan.
155     bool canFree() const;
156 #endif
157
158     virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false) override;
159
160     virtual void trace(Visitor*) override;
161
162 private:
163     AnimationPlayer(ExecutionContext*, AnimationTimeline&, AnimationNode*);
164
165     double sourceEnd() const;
166     bool limited(double currentTime) const;
167
168     AnimationPlayState calculatePlayState();
169     double calculateCurrentTime() const;
170
171     void unpauseInternal();
172     void uncancel();
173     void setPlaybackRateInternal(double);
174     void updateCurrentTimingState(TimingUpdateReason);
175
176
177     AnimationPlayState m_playState;
178     double m_playbackRate;
179     double m_startTime;
180     double m_holdTime;
181
182     unsigned m_sequenceNumber;
183
184     RefPtrWillBeMember<AnimationNode> m_content;
185     RawPtrWillBeMember<AnimationTimeline> m_timeline;
186     // Reflects all pausing, including via pauseForTesting().
187     bool m_paused;
188     bool m_held;
189     bool m_isPausedForTesting;
190
191     // This indicates timing information relevant to the player's effect
192     // has changed by means other than the ordinary progression of time
193     bool m_outdated;
194
195     bool m_finished;
196     // Holds a 'finished' event queued for asynchronous dispatch via the
197     // ScriptedAnimationController. This object remains active until the
198     // event is actually dispatched.
199     RefPtrWillBeMember<Event> m_pendingFinishedEvent;
200
201     enum CompositorAction {
202         None,
203         Pause,
204         Start,
205         PauseThenStart
206     };
207
208     class CompositorState {
209     public:
210         CompositorState(AnimationPlayer& player)
211             : startTime(player.m_startTime)
212             , holdTime(player.m_holdTime)
213             , playbackRate(player.m_playbackRate)
214             , sourceChanged(false)
215             , pendingAction(Start)
216         { }
217         double startTime;
218         double holdTime;
219         double playbackRate;
220         bool sourceChanged;
221         CompositorAction pendingAction;
222     };
223
224     enum CompositorPendingChange {
225         SetCompositorPending,
226         SetCompositorPendingWithSourceChanged,
227         DoNotSetCompositorPending,
228     };
229
230     class PlayStateUpdateScope {
231         STACK_ALLOCATED();
232     public:
233         PlayStateUpdateScope(AnimationPlayer&, TimingUpdateReason, CompositorPendingChange = SetCompositorPending);
234         ~PlayStateUpdateScope();
235     private:
236         AnimationPlayer& m_player;
237         AnimationPlayState m_initial;
238         CompositorPendingChange m_compositorPendingChange;
239     };
240
241     // This mirrors the known compositor state. It is created when a compositor
242     // animation is started. Updated once the start time is known and each time
243     // modifications are pushed to the compositor.
244     OwnPtr<CompositorState> m_compositorState;
245     bool m_compositorPending;
246     bool m_currentTimePending;
247 };
248
249 } // namespace blink
250
251 #endif