2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef AnimationBase_h
30 #define AnimationBase_h
32 #include "RenderStyleConstants.h"
33 #include <wtf/HashMap.h>
34 #include <wtf/HashSet.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/text/AtomicString.h>
42 class AnimationController;
43 class CompositeAnimation;
50 class AnimationBase : public RefCounted<AnimationBase> {
51 friend class CompositeAnimation;
54 AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim);
55 virtual ~AnimationBase() { }
57 RenderObject* renderer() const { return m_object; }
65 double duration() const;
67 // Animations and Transitions go through the states below. When entering the STARTED state
68 // the animation is started. This may or may not require deferred response from the animator.
69 // If so, we stay in this state until that response is received (and it returns the start time).
70 // Otherwise, we use the current time as the start time and go immediately to AnimationStateLooping
71 // or AnimationStateEnding.
73 AnimationStateNew, // animation just created, animation not running yet
74 AnimationStateStartWaitTimer, // start timer running, waiting for fire
75 AnimationStateStartWaitStyleAvailable, // waiting for style setup so we can start animations
76 AnimationStateStartWaitResponse, // animation started, waiting for response
77 AnimationStateLooping, // response received, animation running, loop timer running, waiting for fire
78 AnimationStateEnding, // received, animation running, end timer running, waiting for fire
79 AnimationStatePausedWaitTimer, // in pause mode when animation started
80 AnimationStatePausedWaitStyleAvailable, // in pause mode when waiting for style setup
81 AnimationStatePausedWaitResponse, // animation paused when in STARTING state
82 AnimationStatePausedRun, // animation paused when in LOOPING or ENDING state
83 AnimationStateDone, // end timer fired, animation finished and removed
84 AnimationStateFillingForwards // animation has ended and is retaining its final value
88 AnimationStateInputMakeNew, // reset back to new from any state
89 AnimationStateInputStartAnimation, // animation requests a start
90 AnimationStateInputRestartAnimation, // force a restart from any state
91 AnimationStateInputStartTimerFired, // start timer fired
92 AnimationStateInputStyleAvailable, // style is setup, ready to start animating
93 AnimationStateInputStartTimeSet, // m_startTime was set
94 AnimationStateInputLoopTimerFired, // loop timer fired
95 AnimationStateInputEndTimerFired, // end timer fired
96 AnimationStateInputPauseOverride, // pause an animation due to override
97 AnimationStateInputResumeOverride, // resume an overridden animation
98 AnimationStateInputPlayStateRunning, // play state paused -> running
99 AnimationStateInputPlayStatePaused, // play state running -> paused
100 AnimationStateInputEndAnimation // force an end from any state
103 // Called when animation is in AnimationStateNew to start animation
104 void updateStateMachine(AnimStateInput, double param);
106 // Animation has actually started, at passed time
107 void onAnimationStartResponse(double startTime)
109 updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime);
112 // Called to change to or from paused state
113 void updatePlayState(EAnimPlayState);
114 bool playStatePlaying() const;
116 bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer; }
117 bool preActive() const
119 return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse;
122 bool postActive() const { return m_animState == AnimationStateDone; }
123 bool active() const { return !postActive() && !preActive(); }
124 bool running() const { return !isNew() && !postActive(); }
125 bool paused() const { return m_pauseTime >= 0; }
126 bool isNew() const { return m_animState == AnimationStateNew; }
127 bool waitingForStartTime() const { return m_animState == AnimationStateStartWaitResponse; }
128 bool waitingForStyleAvailable() const { return m_animState == AnimationStateStartWaitStyleAvailable; }
130 // "animating" means that something is running that requires a timer to keep firing
131 // (e.g. a software animation)
132 void setAnimating(bool inAnimating = true) { m_isAnimating = inAnimating; }
133 virtual double timeToNextService();
135 double progress(double scale, double offset, const TimingFunction*) const;
137 virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* /*currentStyle*/, RenderStyle* /*targetStyle*/, RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
138 virtual void getAnimatedStyle(RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
140 virtual bool shouldFireEvents() const { return false; }
142 void fireAnimationEventsIfNeeded();
144 bool animationsMatch(const Animation*) const;
146 void setAnimation(const Animation* anim) { m_animation = const_cast<Animation*>(anim); }
148 // Return true if this animation is overridden. This will only be the case for
149 // ImplicitAnimations and is used to determine whether or not we should force
150 // set the start time. If an animation is overridden, it will probably not get
151 // back the AnimationStateInputStartTimeSet input.
152 virtual bool overridden() const { return false; }
154 // Does this animation/transition involve the given property?
155 virtual bool affectsProperty(int /*property*/) const { return false; }
157 bool isAnimatingProperty(int property, bool acceleratedOnly, bool isRunningNow) const
159 if (acceleratedOnly && !m_isAccelerated)
163 return (!waitingToStart() && !postActive()) && affectsProperty(property);
165 return !postActive() && affectsProperty(property);
168 bool isTransformFunctionListValid() const { return m_transformFunctionListValid; }
170 // Freeze the animation; used by DumpRenderTree.
171 void freezeAtTime(double t);
173 // Play and pause API
177 double beginAnimationUpdateTime() const;
179 double getElapsedTime() const;
180 // Setting the elapsed time will adjust the start time and possibly pause time.
181 void setElapsedTime(double);
183 void styleAvailable()
185 ASSERT(waitingForStyleAvailable());
186 updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1);
189 #if USE(ACCELERATED_COMPOSITING)
190 static bool animationOfPropertyIsAccelerated(int prop);
193 static HashSet<int> animatableShorthandsAffectingProperty(int property);
195 const Animation* animation() const { return m_animation.get(); }
198 virtual void overrideAnimations() { }
199 virtual void resumeOverriddenAnimations() { }
201 CompositeAnimation* compositeAnimation() { return m_compAnim; }
203 // These are called when the corresponding timer fires so subclasses can do any extra work
204 virtual void onAnimationStart(double /*elapsedTime*/) { }
205 virtual void onAnimationIteration(double /*elapsedTime*/) { }
206 virtual void onAnimationEnd(double /*elapsedTime*/) { }
208 // timeOffset is an offset from the current time when the animation should start. Negative values are OK.
209 // Return value indicates whether to expect an asynchronous notifyAnimationStarted() callback.
210 virtual bool startAnimation(double /*timeOffset*/) { return false; }
211 // timeOffset is the time at which the animation is being paused.
212 virtual void pauseAnimation(double /*timeOffset*/) { }
213 virtual void endAnimation() { }
215 void goIntoEndingOrLoopingState();
217 bool isAccelerated() const { return m_isAccelerated; }
219 static bool propertiesEqual(int prop, const RenderStyle* a, const RenderStyle* b);
220 static int getPropertyAtIndex(int, bool& isShorthand);
221 static int getNumProperties();
223 // Return true if we need to start software animation timers
224 static bool blendProperties(const AnimationBase* anim, int prop, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress);
226 static void setNeedsStyleRecalc(Node*);
228 void getTimeToNextEvent(double& time, bool& isLooping) const;
230 AnimState m_animState;
232 bool m_isAnimating; // transition/animation requires continual timer firing
235 double m_requestedStartTime;
236 RenderObject* m_object;
238 RefPtr<Animation> m_animation;
239 CompositeAnimation* m_compAnim;
240 bool m_isAccelerated;
241 bool m_transformFunctionListValid;
242 double m_totalDuration, m_nextIterationDuration;
245 static void ensurePropertyMap();
248 } // namespace WebCore
250 #endif // AnimationBase_h