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