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 AnimationTimeline_h 32 #define AnimationTimeline_h 33 34 #include "bindings/core/v8/ScriptWrappable.h" 35 #include "core/animation/AnimationEffect.h" 36 #include "core/animation/AnimationPlayer.h" 37 #include "core/dom/Element.h" 38 #include "platform/Timer.h" 39 #include "platform/heap/Handle.h" 40 #include "wtf/RefCounted.h" 41 #include "wtf/RefPtr.h" 42 #include "wtf/Vector.h" 43 44 namespace blink { 45 46 class Document; 47 class AnimationNode; 48 49 // AnimationTimeline is constructed and owned by Document, and tied to its lifecycle. 50 class AnimationTimeline : public RefCountedWillBeGarbageCollectedFinalized<AnimationTimeline>, public ScriptWrappable { 51 DEFINE_WRAPPERTYPEINFO(); 52 public: 53 class PlatformTiming : public NoBaseWillBeGarbageCollectedFinalized<PlatformTiming> { 54 55 public: 56 // Calls AnimationTimeline's wake() method after duration seconds. 57 virtual void wakeAfter(double duration) = 0; 58 virtual void cancelWake() = 0; 59 virtual void serviceOnNextFrame() = 0; ~PlatformTiming()60 virtual ~PlatformTiming() { } trace(Visitor *)61 virtual void trace(Visitor*) { } 62 }; 63 64 static PassRefPtrWillBeRawPtr<AnimationTimeline> create(Document*, PassOwnPtrWillBeRawPtr<PlatformTiming> = nullptr); 65 ~AnimationTimeline(); 66 67 void serviceAnimations(TimingUpdateReason); 68 69 // Creates a player attached to this timeline, but without a start time. 70 AnimationPlayer* createAnimationPlayer(AnimationNode*); 71 AnimationPlayer* play(AnimationNode*); 72 WillBeHeapVector<RefPtrWillBeMember<AnimationPlayer> > getAnimationPlayers(); 73 74 #if !ENABLE(OILPAN) playerDestroyed(AnimationPlayer * player)75 void playerDestroyed(AnimationPlayer* player) 76 { 77 ASSERT(m_players.contains(player)); 78 m_players.remove(player); 79 } 80 #endif 81 hasPendingUpdates()82 bool hasPendingUpdates() const { return !m_playersNeedingUpdate.isEmpty(); } 83 double zeroTime(); 84 double currentTime(bool& isNull); 85 double currentTime(); 86 double currentTimeInternal(bool& isNull); 87 double currentTimeInternal(); 88 double effectiveTime(); 89 void pauseAnimationsForTesting(double); 90 91 void setOutdatedAnimationPlayer(AnimationPlayer*); 92 bool hasOutdatedAnimationPlayer() const; 93 document()94 Document* document() { return m_document.get(); } 95 #if !ENABLE(OILPAN) 96 void detachFromDocument(); 97 #endif 98 void wake(); 99 100 void trace(Visitor*); 101 102 protected: 103 AnimationTimeline(Document*, PassOwnPtrWillBeRawPtr<PlatformTiming>); 104 105 private: 106 RawPtrWillBeMember<Document> m_document; 107 double m_zeroTime; 108 // AnimationPlayers which will be updated on the next frame 109 // i.e. current, in effect, or had timing changed 110 WillBeHeapHashSet<RefPtrWillBeMember<AnimationPlayer> > m_playersNeedingUpdate; 111 WillBeHeapHashSet<RawPtrWillBeWeakMember<AnimationPlayer> > m_players; 112 113 friend class SMILTimeContainer; 114 static const double s_minimumDelay; 115 116 OwnPtrWillBeMember<PlatformTiming> m_timing; 117 118 class AnimationTimelineTiming FINAL : public PlatformTiming { 119 public: AnimationTimelineTiming(AnimationTimeline * timeline)120 AnimationTimelineTiming(AnimationTimeline* timeline) 121 : m_timeline(timeline) 122 , m_timer(this, &AnimationTimelineTiming::timerFired) 123 { 124 ASSERT(m_timeline); 125 } 126 127 virtual void wakeAfter(double duration) OVERRIDE; 128 virtual void cancelWake() OVERRIDE; 129 virtual void serviceOnNextFrame() OVERRIDE; 130 timerFired(Timer<AnimationTimelineTiming> *)131 void timerFired(Timer<AnimationTimelineTiming>*) { m_timeline->wake(); } 132 133 virtual void trace(Visitor*) OVERRIDE; 134 135 private: 136 RawPtrWillBeMember<AnimationTimeline> m_timeline; 137 Timer<AnimationTimelineTiming> m_timer; 138 }; 139 140 friend class AnimationAnimationTimelineTest; 141 }; 142 143 } // namespace blink 144 145 #endif 146