tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / page / animation / AnimationBase.h
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 "RenderStyleConstants.h"
33 #include <wtf/HashMap.h>
34 #include <wtf/HashSet.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/text/AtomicString.h>
37
38 namespace WebCore {
39
40 class Animation;
41 class AnimationBase;
42 class AnimationController;
43 class CompositeAnimation;
44 class Element;
45 class Node;
46 class RenderObject;
47 class RenderStyle;
48 class TimingFunction;
49
50 class AnimationBase : public RefCounted<AnimationBase> {
51     friend class CompositeAnimation;
52
53 public:
54     AnimationBase(const Animation* transition, RenderObject* renderer, CompositeAnimation* compAnim);
55     virtual ~AnimationBase() { }
56
57     RenderObject* renderer() const { return m_object; }
58     void clear()
59     {
60       endAnimation();
61       m_object = 0;
62       m_compAnim = 0;
63     }
64
65     double duration() const;
66
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.
72     enum AnimState { 
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
85     };
86
87     enum AnimStateInput {
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
101     };
102
103     // Called when animation is in AnimationStateNew to start animation
104     void updateStateMachine(AnimStateInput, double param);
105
106     // Animation has actually started, at passed time
107     void onAnimationStartResponse(double startTime)
108     {
109         updateStateMachine(AnimationBase::AnimationStateInputStartTimeSet, startTime);
110     }
111
112     // Called to change to or from paused state
113     void updatePlayState(EAnimPlayState);
114     bool playStatePlaying() const;
115
116     bool waitingToStart() const { return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer; }
117     bool preActive() const
118     {
119         return m_animState == AnimationStateNew || m_animState == AnimationStateStartWaitTimer || m_animState == AnimationStateStartWaitStyleAvailable || m_animState == AnimationStateStartWaitResponse;
120     }
121
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; }
129
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();
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(int /*property*/) const { return false; }
156
157     bool isAnimatingProperty(int 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     bool isTransformFunctionListValid() const { return m_transformFunctionListValid; }
169     
170     // Freeze the animation; used by DumpRenderTree.
171     void freezeAtTime(double t);
172
173     // Play and pause API
174     void play();
175     void pause();
176     
177     double beginAnimationUpdateTime() const;
178     
179     double getElapsedTime() const;
180     // Setting the elapsed time will adjust the start time and possibly pause time.
181     void setElapsedTime(double);
182     
183     void styleAvailable() 
184     {
185         ASSERT(waitingForStyleAvailable());
186         updateStateMachine(AnimationBase::AnimationStateInputStyleAvailable, -1);
187     }
188
189 #if USE(ACCELERATED_COMPOSITING)
190     static bool animationOfPropertyIsAccelerated(int prop);
191 #endif
192
193     static HashSet<int> animatableShorthandsAffectingProperty(int property);
194
195     const Animation* animation() const { return m_animation.get(); }
196
197 protected:
198     virtual void overrideAnimations() { }
199     virtual void resumeOverriddenAnimations() { }
200
201     CompositeAnimation* compositeAnimation() { return m_compAnim; }
202
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*/) { }
207     
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() { }
214
215     void goIntoEndingOrLoopingState();
216
217     bool isAccelerated() const { return m_isAccelerated; }
218
219     static bool propertiesEqual(int prop, const RenderStyle* a, const RenderStyle* b);
220     static int getPropertyAtIndex(int, bool& isShorthand);
221     static int getNumProperties();
222
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);
225
226     static void setNeedsStyleRecalc(Node*);
227     
228     void getTimeToNextEvent(double& time, bool& isLooping) const;
229
230     AnimState m_animState;
231
232     bool m_isAnimating;       // transition/animation requires continual timer firing
233     double m_startTime;
234     double m_pauseTime;
235     double m_requestedStartTime;
236     RenderObject* m_object;
237
238     RefPtr<Animation> m_animation;
239     CompositeAnimation* m_compAnim;
240     bool m_isAccelerated;
241     bool m_transformFunctionListValid;
242     double m_totalDuration, m_nextIterationDuration;
243     
244 private:
245     static void ensurePropertyMap();
246 };
247
248 } // namespace WebCore
249
250 #endif // AnimationBase_h