Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2007 Apple 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
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #ifndef AnimationBase_h
     30 #define AnimationBase_h
     31 
     32 #include "CSSPropertyNames.h"
     33 #include "core/platform/animation/CSSAnimationData.h"
     34 #include "core/rendering/style/RenderStyleConstants.h"
     35 #include "wtf/RefCounted.h"
     36 
     37 namespace WebCore {
     38 
     39 class AnimationBase;
     40 class AnimationController;
     41 class CompositeAnimation;
     42 class Element;
     43 class Node;
     44 class RenderObject;
     45 class RenderStyle;
     46 class TimingFunction;
     47 
     48 class AnimationBase : public RefCounted<AnimationBase> {
     49     friend class CompositeAnimation;
     50     friend class CSSPropertyAnimation;
     51 
     52 public:
     53     AnimationBase(const CSSAnimationData* transition, RenderObject* renderer, CompositeAnimation* compAnim);
     54     virtual ~AnimationBase() { }
     55 
     56     RenderObject* renderer() const { return m_object; }
     57     void clear()
     58     {
     59       endAnimation();
     60       m_object = 0;
     61       m_compAnim = 0;
     62     }
     63 
     64     double duration() const;
     65 
     66     // Animations and Transitions go through the states below. When entering the STARTED state
     67     // the animation is started. This may or may not require deferred response from the animator.
     68     // If so, we stay in this state until that response is received (and it returns the start time).
     69     // Otherwise, we use the current time as the start time and go immediately to AnimationStateLooping
     70     // or AnimationStateEnding.
     71     enum AnimState {
     72         AnimationStateNew,                  // animation just created, animation not running yet
     73         AnimationStateStartWaitTimer,       // start timer running, waiting for fire
     74         AnimationStateStartWaitStyleAvailable,   // waiting for style setup so we can start animations
     75         AnimationStateStartWaitResponse,    // animation started, waiting for response
     76         AnimationStateLooping,              // response received, animation running, loop timer running, waiting for fire
     77         AnimationStateEnding,               // received, animation running, end timer running, waiting for fire
     78         AnimationStatePausedWaitTimer,      // in pause mode when animation started
     79         AnimationStatePausedWaitStyleAvailable, // in pause mode when waiting for style setup
     80         AnimationStatePausedWaitResponse,   // animation paused when in STARTING state
     81         AnimationStatePausedRun,            // animation paused when in LOOPING or ENDING state
     82         AnimationStateDone,                 // end timer fired, animation finished and removed
     83         AnimationStateFillingForwards       // animation has ended and is retaining its final value
     84     };
     85 
     86     enum AnimStateInput {
     87         AnimationStateInputMakeNew,           // reset back to new from any state
     88         AnimationStateInputStartAnimation,    // animation requests a start
     89         AnimationStateInputRestartAnimation,  // force a restart from any state
     90         AnimationStateInputStartTimerFired,   // start timer fired
     91         AnimationStateInputStyleAvailable,    // style is setup, ready to start animating
     92         AnimationStateInputStartTimeSet,      // m_startTime was set
     93         AnimationStateInputLoopTimerFired,    // loop timer fired
     94         AnimationStateInputEndTimerFired,     // end timer fired
     95         AnimationStateInputPauseOverride,     // pause an animation due to override
     96         AnimationStateInputResumeOverride,    // resume an overridden animation
     97         AnimationStateInputPlayStateRunning,  // play state paused -> running
     98         AnimationStateInputPlayStatePaused,   // play state running -> paused
     99         AnimationStateInputEndAnimation       // force an end from any state
    100     };
    101 
    102     // Called when animation is in AnimationStateNew to start animation
    103     void updateStateMachine(AnimStateInput, double param);
    104 
    105     // Animation has actually started, at passed time
    106     void onAnimationStartResponse(double startTime)
    107     {
    108         updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime);
    109     }
    110 
    111     // Called to change to or from paused state
    112     void updatePlayState(EAnimPlayState);
    113     bool playStatePlaying() const;
    114 
    115     bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer; }
    116     bool preActive() const
    117     {
    118         return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse;
    119     }
    120 
    121     bool postActive() const { return m_animState == AnimationStateDone; }
    122     bool active() const { return !postActive() && !preActive(); }
    123     bool running() const { return !isNew() && !postActive(); }
    124     bool paused() const { return m_pauseTime >= 0; }
    125     bool isNew() const { return m_animState == AnimationStateNew; }
    126     bool waitingForStartTime() const { return m_animState == AnimationStateStartWaitResponse; }
    127     bool waitingForStyleAvailable() const { return m_animState == AnimationStateStartWaitStyleAvailable; }
    128 
    129     virtual double timeToNextService();
    130 
    131     double progress(double scale, double offset, const TimingFunction*) const;
    132 
    133     virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* /*currentStyle*/, RenderStyle* /*targetStyle*/, RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
    134     virtual void getAnimatedStyle(RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
    135 
    136     virtual bool shouldFireEvents() const { return false; }
    137 
    138     void fireAnimationEventsIfNeeded();
    139 
    140     bool animationsMatch(const CSSAnimationData*) const;
    141 
    142     void setAnimation(const CSSAnimationData* anim) { m_animation = const_cast<CSSAnimationData*>(anim); }
    143 
    144     // Return true if this animation is overridden. This will only be the case for
    145     // ImplicitAnimations and is used to determine whether or not we should force
    146     // set the start time. If an animation is overridden, it will probably not get
    147     // back the AnimationStateInputStartTimeSet input.
    148     virtual bool overridden() const { return false; }
    149 
    150     // Does this animation/transition involve the given property?
    151     virtual bool affectsProperty(CSSPropertyID /*property*/) const { return false; }
    152 
    153     bool isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, bool isRunningNow) const
    154     {
    155         if (acceleratedOnly && !m_isAccelerated)
    156             return false;
    157 
    158         if (isRunningNow)
    159             return (!waitingToStart() && !postActive()) && affectsProperty(property);
    160 
    161         return !postActive() && affectsProperty(property);
    162     }
    163 
    164     // FIXME: rename this using the "lists match" terminology.
    165     bool isTransformFunctionListValid() const { return m_transformFunctionListValid; }
    166     bool filterFunctionListsMatch() const { return m_filterFunctionListsMatch; }
    167 
    168     // Freeze the animation; used by DumpRenderTree.
    169     void freezeAtTime(double t);
    170 
    171     double beginAnimationUpdateTime() const;
    172 
    173     double getElapsedTime() const;
    174 
    175     void styleAvailable()
    176     {
    177         ASSERT(waitingForStyleAvailable());
    178         updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1);
    179     }
    180 
    181     const CSSAnimationData* animation() const { return m_animation.get(); }
    182 
    183 protected:
    184     virtual void overrideAnimations() { }
    185     virtual void resumeOverriddenAnimations() { }
    186 
    187     CompositeAnimation* compositeAnimation() { return m_compAnim; }
    188 
    189     // These are called when the corresponding timer fires so subclasses can do any extra work
    190     virtual void onAnimationStart(double /*elapsedTime*/) { }
    191     virtual void onAnimationIteration(double /*elapsedTime*/) { }
    192     virtual void onAnimationEnd(double /*elapsedTime*/) { }
    193 
    194     // timeOffset is an offset from the current time when the animation should start. Negative values are OK.
    195     // Return value indicates whether to expect an asynchronous notifyAnimationStarted() callback.
    196     virtual bool startAnimation(double /*timeOffset*/) { return false; }
    197     // timeOffset is the time at which the animation is being paused.
    198     virtual void pauseAnimation(double /*timeOffset*/) { }
    199     virtual void endAnimation() { }
    200 
    201     void goIntoEndingOrLoopingState();
    202 
    203     bool isAccelerated() const { return m_isAccelerated; }
    204 
    205     static void setNeedsStyleRecalc(Node*);
    206 
    207     void getTimeToNextEvent(double& time, bool& isLooping) const;
    208 
    209     double fractionalTime(double scale, double elapsedTime, double offset) const;
    210 
    211     AnimState m_animState;
    212 
    213     bool m_isAccelerated;
    214     bool m_transformFunctionListValid;
    215     bool m_filterFunctionListsMatch;
    216     double m_startTime;
    217     double m_pauseTime;
    218     double m_requestedStartTime;
    219 
    220     double m_totalDuration;
    221     double m_nextIterationDuration;
    222 
    223     RenderObject* m_object;
    224 
    225     RefPtr<CSSAnimationData> m_animation;
    226     CompositeAnimation* m_compAnim;
    227 };
    228 
    229 } // namespace WebCore
    230 
    231 #endif // AnimationBase_h
    232