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.
31 #ifndef AnimationPlayer_h
32 #define AnimationPlayer_h
34 #include "core/animation/AnimationNode.h"
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventTarget.h"
37 #include "wtf/RefPtr.h"
41 class AnimationTimeline;
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);
51 enum AnimationPlayState {
60 static PassRefPtrWillBeRawPtr<AnimationPlayer> create(ExecutionContext*, AnimationTimeline&, AnimationNode*);
62 // Returns whether the player is finished.
63 bool update(TimingUpdateReason);
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();
73 double currentTime(bool& isNull);
75 void setCurrentTime(double newCurrentTime);
77 double currentTimeInternal() const;
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;
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; }
95 DEFINE_ATTRIBUTE_EVENT_LISTENER(finish);
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;
103 double playbackRate() const;
104 void setPlaybackRate(double);
105 const AnimationTimeline* timeline() const { return m_timeline; }
106 AnimationTimeline* timeline() { return m_timeline; }
109 void timelineDestroyed() { m_timeline = nullptr; }
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);
120 const AnimationNode* source() const { return m_content.get(); }
121 AnimationNode* source() { return m_content.get(); }
122 void setSource(AnimationNode*);
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
131 bool outdated() { return m_outdated; }
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);
142 void preCommit(bool startOnCompositor);
143 void postCommit(double timelineTime);
145 unsigned sequenceNumber() const { return m_sequenceNumber; }
147 static bool hasLowerPriority(AnimationPlayer* player1, AnimationPlayer* player2)
149 return player1->sequenceNumber() < player2->sequenceNumber();
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;
158 virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false) override;
160 virtual void trace(Visitor*) override;
163 AnimationPlayer(ExecutionContext*, AnimationTimeline&, AnimationNode*);
165 double sourceEnd() const;
166 bool limited(double currentTime) const;
168 AnimationPlayState calculatePlayState();
169 double calculateCurrentTime() const;
171 void unpauseInternal();
173 void setPlaybackRateInternal(double);
174 void updateCurrentTimingState(TimingUpdateReason);
177 AnimationPlayState m_playState;
178 double m_playbackRate;
182 unsigned m_sequenceNumber;
184 RefPtrWillBeMember<AnimationNode> m_content;
185 RawPtrWillBeMember<AnimationTimeline> m_timeline;
186 // Reflects all pausing, including via pauseForTesting().
189 bool m_isPausedForTesting;
191 // This indicates timing information relevant to the player's effect
192 // has changed by means other than the ordinary progression of time
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;
201 enum CompositorAction {
208 class CompositorState {
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)
221 CompositorAction pendingAction;
224 enum CompositorPendingChange {
225 SetCompositorPending,
226 SetCompositorPendingWithSourceChanged,
227 DoNotSetCompositorPending,
230 class PlayStateUpdateScope {
233 PlayStateUpdateScope(AnimationPlayer&, TimingUpdateReason, CompositorPendingChange = SetCompositorPending);
234 ~PlayStateUpdateScope();
236 AnimationPlayer& m_player;
237 AnimationPlayState m_initial;
238 CompositorPendingChange m_compositorPendingChange;
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;