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/PassOwnPtr.h"
     36 #include "wtf/RefCounted.h"
     37 
     38 namespace WebCore {
     39 
     40 class Player;
     41 
     42 static inline bool isNull(double value)
     43 {
     44     return std::isnan(value);
     45 }
     46 
     47 static inline double nullValue()
     48 {
     49     return std::numeric_limits<double>::quiet_NaN();
     50 }
     51 
     52 class TimedItemEventDelegate {
     53 public:
     54     virtual ~TimedItemEventDelegate() { };
     55     virtual void onEventCondition(bool wasInPlay, bool isInPlay, double previousIteration, double currentIteration) = 0;
     56 };
     57 
     58 class TimedItem : public RefCounted<TimedItem> {
     59     friend class Player; // Calls attach/detach, updateInheritedTime.
     60 public:
     61     virtual ~TimedItem() { }
     62 
     63     bool isCurrent() const { return ensureCalculated().isCurrent; }
     64     bool isInEffect() const { return ensureCalculated().isInEffect; }
     65     bool isInPlay() const { return ensureCalculated().isInPlay; }
     66 
     67     double startTime() const { return m_startTime; }
     68 
     69     double currentIteration() const { return ensureCalculated().currentIteration; }
     70     double activeDuration() const { return ensureCalculated().activeDuration; }
     71     double timeFraction() const { return ensureCalculated().timeFraction; }
     72     Player* player() const { return m_player; }
     73 
     74     enum Phase {
     75         PhaseBefore,
     76         PhaseActive,
     77         PhaseAfter,
     78         PhaseNone,
     79     };
     80 
     81 protected:
     82     TimedItem(const Timing&, PassOwnPtr<TimedItemEventDelegate> = nullptr);
     83 
     84     // When TimedItem receives a new inherited time via updateInheritedTime
     85     // it will (if necessary) recalculate timings and (if necessary) call
     86     // updateChildrenAndEffects.
     87     void updateInheritedTime(double inheritedTime) const;
     88     virtual void updateChildrenAndEffects(bool wasInEffect) const = 0;
     89     virtual double intrinsicIterationDuration() const { return 0; };
     90     virtual void willDetach() = 0;
     91 
     92 private:
     93     void attach(Player* player) { m_player = player; };
     94     void detach()
     95     {
     96         ASSERT(m_player);
     97         willDetach();
     98         m_player = 0;
     99     };
    100 
    101     // FIXME: m_parent and m_startTime are placeholders, they depend on timing groups.
    102     TimedItem* const m_parent;
    103     const double m_startTime;
    104     Player* m_player;
    105     Timing m_specified;
    106     OwnPtr<TimedItemEventDelegate> m_eventDelegate;
    107 
    108     // FIXME: Should be versioned by monotonic value on player.
    109     mutable struct CalculatedTiming {
    110         CalculatedTiming();
    111         double activeDuration;
    112         double currentIteration;
    113         double timeFraction;
    114         bool isCurrent;
    115         bool isInEffect;
    116         bool isInPlay;
    117     } m_calculated;
    118 
    119     // FIXME: Should check the version and reinherit time if inconsistent.
    120     const CalculatedTiming& ensureCalculated() const { return m_calculated; }
    121 };
    122 
    123 } // namespace WebCore
    124 
    125 #endif
    126