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 "Animation.h"
33#include "CSSPropertyNames.h"
34#include "RenderStyleConstants.h"
35#include <wtf/HashMap.h>
36#include <wtf/HashSet.h>
37#include <wtf/RefCounted.h>
38#include <wtf/text/AtomicString.h>
39
40namespace WebCore {
41
42class AnimationBase;
43class AnimationController;
44class CompositeAnimation;
45class Element;
46class Node;
47class RenderObject;
48class RenderStyle;
49class TimingFunction;
50
51class AnimationBase : public RefCounted<AnimationBase> {
52    friend class CompositeAnimation;
53    friend class CSSPropertyAnimation;
54
55public:
56    AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim);
57    virtual ~AnimationBase() { }
58
59    RenderObject* renderer() const { return m_object; }
60    void clear()
61    {
62      endAnimation();
63      m_object = 0;
64      m_compAnim = 0;
65    }
66
67    double duration() const;
68
69    // Animations and Transitions go through the states below. When entering the STARTED state
70    // the animation is started. This may or may not require deferred response from the animator.
71    // If so, we stay in this state until that response is received (and it returns the start time).
72    // Otherwise, we use the current time as the start time and go immediately to AnimationStateLooping
73    // or AnimationStateEnding.
74    enum AnimState {
75        AnimationStateNew,                  // animation just created, animation not running yet
76        AnimationStateStartWaitTimer,       // start timer running, waiting for fire
77        AnimationStateStartWaitStyleAvailable,   // waiting for style setup so we can start animations
78        AnimationStateStartWaitResponse,    // animation started, waiting for response
79        AnimationStateLooping,              // response received, animation running, loop timer running, waiting for fire
80        AnimationStateEnding,               // received, animation running, end timer running, waiting for fire
81        AnimationStatePausedNew,            // in pause mode when animation was created
82        AnimationStatePausedWaitTimer,      // in pause mode when animation started
83        AnimationStatePausedWaitStyleAvailable, // in pause mode when waiting for style setup
84        AnimationStatePausedWaitResponse,   // animation paused when in STARTING state
85        AnimationStatePausedRun,            // animation paused when in LOOPING or ENDING state
86        AnimationStateDone,                 // end timer fired, animation finished and removed
87        AnimationStateFillingForwards       // animation has ended and is retaining its final value
88    };
89
90    enum AnimStateInput {
91        AnimationStateInputMakeNew,           // reset back to new from any state
92        AnimationStateInputStartAnimation,    // animation requests a start
93        AnimationStateInputRestartAnimation,  // force a restart from any state
94        AnimationStateInputStartTimerFired,   // start timer fired
95        AnimationStateInputStyleAvailable,    // style is setup, ready to start animating
96        AnimationStateInputStartTimeSet,      // m_startTime was set
97        AnimationStateInputLoopTimerFired,    // loop timer fired
98        AnimationStateInputEndTimerFired,     // end timer fired
99        AnimationStateInputPauseOverride,     // pause an animation due to override
100        AnimationStateInputResumeOverride,    // resume an overridden animation
101        AnimationStateInputPlayStateRunning,  // play state paused -> running
102        AnimationStateInputPlayStatePaused,   // play state running -> paused
103        AnimationStateInputEndAnimation       // force an end from any state
104    };
105
106    // Called when animation is in AnimationStateNew to start animation
107    void updateStateMachine(AnimStateInput, double param);
108
109    // Animation has actually started, at passed time
110    void onAnimationStartResponse(double startTime)
111    {
112        updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime);
113    }
114
115    // Called to change to or from paused state
116    void updatePlayState(EAnimPlayState);
117    bool playStatePlaying() const;
118
119    bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStatePausedNew; }
120    bool preActive() const
121    {
122        return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse;
123    }
124
125    bool postActive() const { return m_animState == AnimationStateDone; }
126    bool active() const { return !postActive() && !preActive(); }
127    bool running() const { return !isNew() && !postActive(); }
128    bool paused() const { return m_pauseTime >= 0 || m_animState == AnimationStatePausedNew; }
129    bool isNew() const { return m_animState == AnimationStateNew || m_animState == AnimationStatePausedNew; }
130    bool waitingForStartTime() const { return m_animState == AnimationStateStartWaitResponse; }
131    bool waitingForStyleAvailable() const { return m_animState == AnimationStateStartWaitStyleAvailable; }
132
133    virtual double timeToNextService();
134
135    double progress(double scale, double offset, const TimingFunction*) const;
136
137    virtual void animate(CompositeAnimation*, RenderObject*, const RenderStyle* /*currentStyle*/, RenderStyle* /*targetStyle*/, RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
138    virtual void getAnimatedStyle(RefPtr<RenderStyle>& /*animatedStyle*/) = 0;
139
140    virtual bool shouldFireEvents() const { return false; }
141
142    void fireAnimationEventsIfNeeded();
143
144    bool animationsMatch(const Animation*) const;
145
146    void setAnimation(const Animation* anim) { m_animation = const_cast<Animation*>(anim); }
147
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; }
153
154    // Does this animation/transition involve the given property?
155    virtual bool affectsProperty(CSSPropertyID /*property*/) const { return false; }
156
157    bool isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, bool isRunningNow) const
158    {
159        if (acceleratedOnly && !m_isAccelerated)
160            return false;
161
162        if (isRunningNow)
163            return (!waitingToStart() && !postActive()) && affectsProperty(property);
164
165        return !postActive() && affectsProperty(property);
166    }
167
168    // FIXME: rename this using the "lists match" terminology.
169    bool isTransformFunctionListValid() const { return m_transformFunctionListValid; }
170#if ENABLE(CSS_FILTERS)
171    bool filterFunctionListsMatch() const { return m_filterFunctionListsMatch; }
172#endif
173
174    // Freeze the animation; used by DumpRenderTree.
175    void freezeAtTime(double t);
176
177    // Play and pause API
178    void play();
179    void pause();
180
181    double beginAnimationUpdateTime() const;
182
183    double getElapsedTime() const;
184    // Setting the elapsed time will adjust the start time and possibly pause time.
185    void setElapsedTime(double);
186
187    void styleAvailable()
188    {
189        ASSERT(waitingForStyleAvailable());
190        updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1);
191    }
192
193    const Animation* animation() const { return m_animation.get(); }
194
195protected:
196    virtual void overrideAnimations() { }
197    virtual void resumeOverriddenAnimations() { }
198
199    CompositeAnimation* compositeAnimation() { return m_compAnim; }
200
201    // These are called when the corresponding timer fires so subclasses can do any extra work
202    virtual void onAnimationStart(double /*elapsedTime*/) { }
203    virtual void onAnimationIteration(double /*elapsedTime*/) { }
204    virtual void onAnimationEnd(double /*elapsedTime*/) { }
205
206    // timeOffset is an offset from the current time when the animation should start. Negative values are OK.
207    // Return value indicates whether to expect an asynchronous notifyAnimationStarted() callback.
208    virtual bool startAnimation(double /*timeOffset*/) { return false; }
209    // timeOffset is the time at which the animation is being paused.
210    virtual void pauseAnimation(double /*timeOffset*/) { }
211    virtual void endAnimation() { }
212
213    void goIntoEndingOrLoopingState();
214
215    bool isAccelerated() const { return m_isAccelerated; }
216
217    static void setNeedsStyleRecalc(Node*);
218
219    void getTimeToNextEvent(double& time, bool& isLooping) const;
220
221    double fractionalTime(double scale, double elapsedTime, double offset) const;
222
223    AnimState m_animState;
224
225    bool m_isAccelerated;
226    bool m_transformFunctionListValid;
227#if ENABLE(CSS_FILTERS)
228    bool m_filterFunctionListsMatch;
229#endif
230    double m_startTime;
231    double m_pauseTime;
232    double m_requestedStartTime;
233
234    double m_totalDuration;
235    double m_nextIterationDuration;
236
237    RenderObject* m_object;
238
239    RefPtr<Animation> m_animation;
240    CompositeAnimation* m_compAnim;
241};
242
243} // namespace WebCore
244
245#endif // AnimationBase_h
246