Home | History | Annotate | Download | only in animation
      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 WebCore {
     40 
     41 class AnimationTimeline;
     42 class ExceptionState;
     43 
     44 class AnimationPlayer FINAL : public RefCountedWillBeRefCountedGarbageCollected<AnimationPlayer>
     45     , public ActiveDOMObject
     46     , public EventTargetWithInlineData {
     47     REFCOUNTED_EVENT_TARGET(AnimationPlayer);
     48     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AnimationPlayer);
     49 public:
     50 
     51     ~AnimationPlayer();
     52     static PassRefPtrWillBeRawPtr<AnimationPlayer> create(ExecutionContext*, AnimationTimeline&, AnimationNode*);
     53 
     54     // Returns whether the player is finished.
     55     bool update(TimingUpdateReason);
     56 
     57     // timeToEffectChange returns:
     58     //  infinity  - if this player is no longer in effect
     59     //  0         - if this player requires an update on the next frame
     60     //  n         - if this player requires an update after 'n' units of time
     61     double timeToEffectChange();
     62 
     63     void cancel();
     64 
     65     double currentTime();
     66     void setCurrentTime(double newCurrentTime);
     67 
     68     double currentTimeInternal();
     69     void setCurrentTimeInternal(double newCurrentTime);
     70 
     71     bool paused() const { return m_paused && !m_isPausedForTesting; }
     72     void pause();
     73     void play();
     74     void reverse();
     75     void finish(ExceptionState&);
     76     bool finished() { return limited(currentTimeInternal()); }
     77     // FIXME: Resolve whether finished() should just return the flag, and
     78     // remove this method.
     79     bool finishedInternal() const { return m_finished; }
     80 
     81     DEFINE_ATTRIBUTE_EVENT_LISTENER(finish);
     82 
     83     virtual const AtomicString& interfaceName() const OVERRIDE;
     84     virtual ExecutionContext* executionContext() const OVERRIDE;
     85     virtual bool hasPendingActivity() const OVERRIDE;
     86     virtual void stop() OVERRIDE;
     87     virtual bool dispatchEvent(PassRefPtrWillBeRawPtr<Event>) OVERRIDE;
     88 
     89     double playbackRate() const { return m_playbackRate; }
     90     void setPlaybackRate(double);
     91     const AnimationTimeline* timeline() const { return m_timeline; }
     92     AnimationTimeline* timeline() { return m_timeline; }
     93 
     94 #if !ENABLE(OILPAN)
     95     void timelineDestroyed() { m_timeline = nullptr; }
     96 #endif
     97 
     98     bool hasStartTime() const { return !isNull(m_startTime); }
     99     double startTime() const { return m_startTime * 1000; }
    100     double startTimeInternal() const { return m_startTime; }
    101     void setStartTime(double startTime) { setStartTimeInternal(startTime / 1000); }
    102     void setStartTimeInternal(double, bool isUpdateFromCompositor = false);
    103 
    104     const AnimationNode* source() const { return m_content.get(); }
    105     AnimationNode* source() { return m_content.get(); }
    106     AnimationNode* source(bool& isNull) { isNull = !m_content; return m_content.get(); }
    107     void setSource(AnimationNode*);
    108 
    109     double timeLag() { return timeLagInternal() * 1000; }
    110     double timeLagInternal() { return currentTimeWithoutLag() - currentTimeInternal(); }
    111 
    112     // Pausing via this method is not reflected in the value returned by
    113     // paused() and must never overlap with pausing via pause().
    114     void pauseForTesting(double pauseTime);
    115     // This should only be used for CSS
    116     void unpause();
    117 
    118     void setOutdated();
    119     bool outdated() { return m_outdated; }
    120 
    121     bool canStartAnimationOnCompositor();
    122     bool maybeStartAnimationOnCompositor();
    123     void cancelAnimationOnCompositor();
    124     void schedulePendingAnimationOnCompositor();
    125     bool hasActiveAnimationsOnCompositor();
    126 
    127     class SortInfo {
    128     public:
    129         friend class AnimationPlayer;
    130         bool operator<(const SortInfo& other) const;
    131         double startTime() const { return m_startTime; }
    132     private:
    133         SortInfo(unsigned sequenceNumber, double startTime)
    134             : m_sequenceNumber(sequenceNumber)
    135             , m_startTime(startTime)
    136         {
    137         }
    138         unsigned m_sequenceNumber;
    139         double m_startTime;
    140     };
    141 
    142     const SortInfo& sortInfo() const { return m_sortInfo; }
    143 
    144     static bool hasLowerPriority(AnimationPlayer* player1, AnimationPlayer* player2)
    145     {
    146         return player1->sortInfo() < player2->sortInfo();
    147     }
    148 
    149 #if !ENABLE(OILPAN)
    150     // Checks if the AnimationStack is the last reference holder to the Player.
    151     // This won't be needed when AnimationPlayer is moved to Oilpan.
    152     bool canFree() const;
    153 #endif
    154 
    155     virtual bool addEventListener(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture = false) OVERRIDE;
    156 
    157     virtual void trace(Visitor*) OVERRIDE;
    158 
    159 private:
    160     AnimationPlayer(ExecutionContext*, AnimationTimeline&, AnimationNode*);
    161     double sourceEnd() const;
    162     bool limited(double currentTime) const;
    163     double currentTimeWithoutLag() const;
    164     double currentTimeWithLag() const;
    165     void updateTimingState(double newCurrentTime);
    166     void updateCurrentTimingState();
    167 
    168     double m_playbackRate;
    169     double m_startTime;
    170     double m_holdTime;
    171     double m_storedTimeLag;
    172 
    173     SortInfo m_sortInfo;
    174 
    175     RefPtrWillBeMember<AnimationNode> m_content;
    176     RawPtrWillBeMember<AnimationTimeline> m_timeline;
    177     // Reflects all pausing, including via pauseForTesting().
    178     bool m_paused;
    179     bool m_held;
    180     bool m_isPausedForTesting;
    181 
    182     // This indicates timing information relevant to the player has changed by
    183     // means other than the ordinary progression of time
    184     bool m_outdated;
    185 
    186     bool m_finished;
    187     // Holds a 'finished' event queued for asynchronous dispatch via the
    188     // ScriptedAnimationController. This object remains active until the
    189     // event is actually dispatched.
    190     RefPtrWillBeMember<Event> m_pendingFinishedEvent;
    191 };
    192 
    193 } // namespace
    194 
    195 #endif
    196