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 void setAnimation(const CSSAnimationData* anim) { m_animation = const_cast<CSSAnimationData*>(anim); } 141 142 // Return true if this animation is overridden. This will only be the case for 143 // ImplicitAnimations and is used to determine whether or not we should force 144 // set the start time. If an animation is overridden, it will probably not get 145 // back the AnimationStateInputStartTimeSet input. 146 virtual bool overridden() const { return false; } 147 148 // Does this animation/transition involve the given property? 149 virtual bool affectsProperty(CSSPropertyID /*property*/) const { return false; } 150 151 bool isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, bool isRunningNow) const 152 { 153 if (acceleratedOnly && !m_isAccelerated) 154 return false; 155 156 if (isRunningNow) 157 return (!waitingToStart() && !postActive()) && affectsProperty(property); 158 159 return !postActive() && affectsProperty(property); 160 } 161 162 // FIXME: rename this using the "lists match" terminology. 163 bool isTransformFunctionListValid() const { return m_transformFunctionListValid; } 164 bool filterFunctionListsMatch() const { return m_filterFunctionListsMatch; } 165 166 // Freeze the animation; used by DumpRenderTree. 167 void freezeAtTime(double t); 168 169 double beginAnimationUpdateTime() const; 170 171 double getElapsedTime() const; 172 173 void styleAvailable() 174 { 175 ASSERT(waitingForStyleAvailable()); 176 updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1); 177 } 178 179 const CSSAnimationData* animation() const { return m_animation.get(); } 180 181 protected: 182 virtual void overrideAnimations() { } 183 virtual void resumeOverriddenAnimations() { } 184 185 CompositeAnimation* compositeAnimation() { return m_compAnim; } 186 187 // These are called when the corresponding timer fires so subclasses can do any extra work 188 virtual void onAnimationStart(double /*elapsedTime*/) { } 189 virtual void onAnimationIteration(double /*elapsedTime*/) { } 190 virtual void onAnimationEnd(double /*elapsedTime*/) { } 191 192 // timeOffset is an offset from the current time when the animation should start. Negative values are OK. 193 virtual void startAnimation(double /*timeOffset*/) { } 194 // timeOffset is the time at which the animation is being paused. 195 virtual void pauseAnimation(double /*timeOffset*/) { } 196 virtual void endAnimation() { } 197 198 void goIntoEndingOrLoopingState(); 199 200 bool isAccelerated() const { return m_isAccelerated; } 201 202 static void setNeedsStyleRecalc(Node*); 203 204 void getTimeToNextEvent(double& time, bool& isLooping) const; 205 206 double fractionalTime(double scale, double elapsedTime, double offset) const; 207 208 AnimState m_animState; 209 210 bool m_isAccelerated; 211 bool m_transformFunctionListValid; 212 bool m_filterFunctionListsMatch; 213 double m_startTime; 214 double m_pauseTime; 215 double m_requestedStartTime; 216 217 double m_totalDuration; 218 double m_nextIterationDuration; 219 220 RenderObject* m_object; 221 222 RefPtr<CSSAnimationData> m_animation; 223 CompositeAnimation* m_compAnim; 224 }; 225 226 } // namespace WebCore 227 228 #endif // AnimationBase_h 229