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 TimedItem_h
     32 #define TimedItem_h
     33 
     34 #include "core/animation/Timing.h"
     35 #include "wtf/OwnPtr.h"
     36 #include "wtf/PassOwnPtr.h"
     37 #include "wtf/RefCounted.h"
     38 
     39 namespace WebCore {
     40 
     41 class Player;
     42 class TimedItem;
     43 
     44 static inline bool isNull(double value)
     45 {
     46     return std::isnan(value);
     47 }
     48 
     49 static inline double nullValue()
     50 {
     51     return std::numeric_limits<double>::quiet_NaN();
     52 }
     53 
     54 class TimedItem : public RefCounted<TimedItem> {
     55     friend class Player; // Calls attach/detach, updateInheritedTime.
     56 public:
     57     // Note that logic in CSSAnimations depends on the order of these values.
     58     enum Phase {
     59         PhaseBefore,
     60         PhaseActive,
     61         PhaseAfter,
     62         PhaseNone,
     63     };
     64 
     65     class EventDelegate {
     66     public:
     67         virtual ~EventDelegate() { };
     68         virtual void onEventCondition(const TimedItem*, bool isFirstSample, Phase previousPhase, double previousIteration) = 0;
     69     };
     70 
     71     virtual ~TimedItem() { }
     72 
     73     virtual bool isAnimation() const { return false; }
     74 
     75     Phase phase() const { return ensureCalculated().phase; }
     76     bool isCurrent() const { return ensureCalculated().isCurrent; }
     77     bool isInEffect() const { return ensureCalculated().isInEffect; }
     78     bool isInPlay() const { return ensureCalculated().isInPlay; }
     79     double timeToEffectChange() const { return ensureCalculated().timeToEffectChange; }
     80 
     81     double currentIteration() const { return ensureCalculated().currentIteration; }
     82     double activeDuration() const { return ensureCalculated().activeDuration; }
     83     double timeFraction() const { return ensureCalculated().timeFraction; }
     84     double startTime() const { return m_startTime; }
     85     const Player* player() const { return m_player; }
     86     Player* player() { return m_player; }
     87     const Timing& specified() const { return m_specified; }
     88 
     89 protected:
     90     TimedItem(const Timing&, PassOwnPtr<EventDelegate> = nullptr);
     91 
     92     // When TimedItem receives a new inherited time via updateInheritedTime
     93     // it will (if necessary) recalculate timings and (if necessary) call
     94     // updateChildrenAndEffects.
     95     // Returns whether style recalc was triggered.
     96     bool updateInheritedTime(double inheritedTime) const;
     97     void invalidate() const { m_needsUpdate = true; };
     98 
     99 private:
    100     // Returns whether style recalc was triggered.
    101     virtual bool updateChildrenAndEffects() const = 0;
    102     virtual double intrinsicIterationDuration() const { return 0; };
    103     virtual double calculateTimeToEffectChange(double localTime, double timeToNextIteration) const = 0;
    104     virtual void didAttach() { };
    105     virtual void willDetach() { };
    106 
    107     void attach(Player* player)
    108     {
    109         m_player = player;
    110         didAttach();
    111     };
    112 
    113     void detach()
    114     {
    115         ASSERT(m_player);
    116         willDetach();
    117         m_player = 0;
    118     };
    119 
    120     // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
    121     TimedItem* const m_parent;
    122     const double m_startTime;
    123     Player* m_player;
    124     Timing m_specified;
    125     OwnPtr<EventDelegate> m_eventDelegate;
    126 
    127     // FIXME: Should be versioned by monotonic value on player.
    128     mutable struct CalculatedTiming {
    129         double activeDuration;
    130         Phase phase;
    131         double currentIteration;
    132         double timeFraction;
    133         bool isCurrent;
    134         bool isInEffect;
    135         bool isInPlay;
    136         double timeToEffectChange;
    137     } m_calculated;
    138     mutable bool m_isFirstSample;
    139     mutable bool m_needsUpdate;
    140     mutable double m_lastUpdateTime;
    141 
    142     // FIXME: Should check the version and reinherit time if inconsistent.
    143     const CalculatedTiming& ensureCalculated() const { return m_calculated; }
    144 };
    145 
    146 } // namespace WebCore
    147 
    148 #endif
    149