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 AnimationNode_h
     32 #define AnimationNode_h
     33 
     34 #include "bindings/core/v8/ScriptWrappable.h"
     35 #include "core/animation/Timing.h"
     36 #include "platform/heap/Handle.h"
     37 #include "wtf/OwnPtr.h"
     38 #include "wtf/PassOwnPtr.h"
     39 #include "wtf/RefCounted.h"
     40 
     41 namespace blink {
     42 
     43 class AnimationPlayer;
     44 class AnimationNode;
     45 class AnimationNodeTiming;
     46 
     47 enum TimingUpdateReason {
     48     TimingUpdateOnDemand,
     49     TimingUpdateForAnimationFrame
     50 };
     51 
     52 static inline bool isNull(double value)
     53 {
     54     return std::isnan(value);
     55 }
     56 
     57 static inline double nullValue()
     58 {
     59     return std::numeric_limits<double>::quiet_NaN();
     60 }
     61 
     62 class AnimationNode : public RefCountedWillBeGarbageCollectedFinalized<AnimationNode>, public ScriptWrappable {
     63     DEFINE_WRAPPERTYPEINFO();
     64     friend class AnimationPlayer; // Calls attach/detach, updateInheritedTime.
     65 public:
     66     // Note that logic in CSSAnimations depends on the order of these values.
     67     enum Phase {
     68         PhaseBefore,
     69         PhaseActive,
     70         PhaseAfter,
     71         PhaseNone,
     72     };
     73 
     74     class EventDelegate : public NoBaseWillBeGarbageCollectedFinalized<EventDelegate> {
     75     public:
     76         virtual ~EventDelegate() { }
     77         virtual void onEventCondition(const AnimationNode*) = 0;
     78         virtual void trace(Visitor*) { }
     79     };
     80 
     81     virtual ~AnimationNode() { }
     82 
     83     virtual bool isAnimation() const { return false; }
     84 
     85     Phase phase() const { return ensureCalculated().phase; }
     86     bool isCurrent() const { return ensureCalculated().isCurrent; }
     87     bool isInEffect() const { return ensureCalculated().isInEffect; }
     88     bool isInPlay() const { return ensureCalculated().isInPlay; }
     89     double timeToForwardsEffectChange() const { return ensureCalculated().timeToForwardsEffectChange; }
     90     double timeToReverseEffectChange() const { return ensureCalculated().timeToReverseEffectChange; }
     91 
     92     double currentIteration() const { return ensureCalculated().currentIteration; }
     93     double iterationDuration() const;
     94 
     95     // This method returns time in ms as it is unused except via the API.
     96     double duration() const { return iterationDuration() * 1000; }
     97 
     98     double activeDuration() const { return activeDurationInternal() * 1000; }
     99     double activeDurationInternal() const;
    100     double timeFraction() const { return ensureCalculated().timeFraction; }
    101     double startTime() const { return m_startTime * 1000; }
    102     double startTimeInternal() const { return m_startTime; }
    103     double endTime() const { return endTimeInternal() * 1000; }
    104     double endTimeInternal() const { return startTime() + specifiedTiming().startDelay + activeDurationInternal() + specifiedTiming().endDelay; }
    105 
    106     const AnimationPlayer* player() const { return m_player; }
    107     AnimationPlayer* player() { return m_player; }
    108     const Timing& specifiedTiming() const { return m_timing; }
    109     PassRefPtrWillBeRawPtr<AnimationNodeTiming> timing();
    110     void updateSpecifiedTiming(const Timing&);
    111 
    112     // This method returns time in ms as it is unused except via the API.
    113     double localTime(bool& isNull) const { isNull = !m_player; return ensureCalculated().localTime * 1000; }
    114     double currentIteration(bool& isNull) const { isNull = !ensureCalculated().isInEffect; return ensureCalculated().currentIteration; }
    115 
    116     void setName(const String& name) { m_name = name; }
    117     const String& name() const { return m_name; }
    118 
    119     virtual void trace(Visitor*);
    120 
    121 protected:
    122     explicit AnimationNode(const Timing&, PassOwnPtrWillBeRawPtr<EventDelegate> = nullptr);
    123 
    124     // When AnimationNode receives a new inherited time via updateInheritedTime
    125     // it will (if necessary) recalculate timings and (if necessary) call
    126     // updateChildrenAndEffects.
    127     void updateInheritedTime(double inheritedTime, TimingUpdateReason) const;
    128     void invalidate() const { m_needsUpdate = true; };
    129     bool hasEvents() const { return m_eventDelegate; }
    130     void clearEventDelegate() { m_eventDelegate = nullptr; }
    131 
    132     virtual void attach(AnimationPlayer* player)
    133     {
    134         m_player = player;
    135     }
    136 
    137     virtual void detach()
    138     {
    139         ASSERT(m_player);
    140         m_player = nullptr;
    141     }
    142 
    143     double repeatedDuration() const;
    144 
    145     virtual void updateChildrenAndEffects() const = 0;
    146     virtual double intrinsicIterationDuration() const { return 0; };
    147     virtual double calculateTimeToEffectChange(bool forwards, double localTime, double timeToNextIteration) const = 0;
    148     virtual void specifiedTimingChanged() { }
    149 
    150     // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
    151     RawPtrWillBeMember<AnimationNode> m_parent;
    152     const double m_startTime;
    153     RawPtrWillBeMember<AnimationPlayer> m_player;
    154     Timing m_timing;
    155     OwnPtrWillBeMember<EventDelegate> m_eventDelegate;
    156 
    157     mutable struct CalculatedTiming {
    158         Phase phase;
    159         double currentIteration;
    160         double timeFraction;
    161         bool isCurrent;
    162         bool isInEffect;
    163         bool isInPlay;
    164         double localTime;
    165         double timeToForwardsEffectChange;
    166         double timeToReverseEffectChange;
    167     } m_calculated;
    168     mutable bool m_needsUpdate;
    169     mutable double m_lastUpdateTime;
    170     String m_name;
    171 
    172     const CalculatedTiming& ensureCalculated() const;
    173 };
    174 
    175 } // namespace blink
    176 
    177 #endif
    178