1/*
2 * Copyright (C) 2009 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 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#pragma once
30
31#include "AnimationBase.h"
32#include "Timer.h"
33#include <wtf/HashMap.h>
34#include <wtf/HashSet.h>
35#include <wtf/Vector.h>
36
37namespace WebCore {
38
39class CompositeAnimation;
40class Document;
41class Frame;
42
43enum SetChanged { DoNotCallSetChanged, CallSetChanged };
44
45class CSSAnimationControllerPrivate {
46 WTF_MAKE_FAST_ALLOCATED;
47public:
48 explicit CSSAnimationControllerPrivate(Frame&);
49 ~CSSAnimationControllerPrivate();
50
51 // Returns the time until the next animation needs to be serviced, or -1 if there are none.
52 Optional<Seconds> updateAnimations(SetChanged callSetChanged = DoNotCallSetChanged);
53 void updateAnimationTimer(SetChanged callSetChanged = DoNotCallSetChanged);
54
55 CompositeAnimation& ensureCompositeAnimation(Element&);
56 bool clear(Element&);
57
58 void updateStyleIfNeededDispatcherFired();
59 void startUpdateStyleIfNeededDispatcher();
60 void addEventToDispatch(Element&, const AtomString& eventType, const String& name, double elapsedTime);
61 void addElementChangeToDispatch(Element&);
62
63 bool hasAnimations() const { return !m_compositeAnimations.isEmpty(); }
64
65 bool isSuspended() const { return m_isSuspended; }
66 void suspendAnimations();
67 void resumeAnimations();
68 void animationFrameCallbackFired();
69
70 void updateThrottlingState();
71 Seconds animationInterval() const;
72
73 void suspendAnimationsForDocument(Document*);
74 void resumeAnimationsForDocument(Document*);
75 bool animationsAreSuspendedForDocument(Document*);
76 void startAnimationsIfNotSuspended(Document*);
77 void detachFromDocument(Document*);
78
79 bool isRunningAnimationOnRenderer(RenderElement&, CSSPropertyID) const;
80 bool isRunningAcceleratedAnimationOnRenderer(RenderElement&, CSSPropertyID) const;
81
82 bool pauseAnimationAtTime(Element&, const AtomString& name, double t);
83 bool pauseTransitionAtTime(Element&, const String& property, double t);
84 unsigned numberOfActiveAnimations(Document*) const;
85
86 std::unique_ptr<RenderStyle> animatedStyleForElement(Element&);
87
88 bool computeExtentOfAnimation(Element&, LayoutRect&) const;
89
90 MonotonicTime beginAnimationUpdateTime();
91
92 void beginAnimationUpdate();
93 void endAnimationUpdate();
94 void receivedStartTimeResponse(MonotonicTime);
95
96 void addToAnimationsWaitingForStyle(AnimationBase&);
97 void removeFromAnimationsWaitingForStyle(AnimationBase&);
98
99 void addToAnimationsWaitingForStartTimeResponse(AnimationBase&, bool willGetResponse);
100 void removeFromAnimationsWaitingForStartTimeResponse(AnimationBase&);
101
102 void animationWillBeRemoved(AnimationBase&);
103
104 void updateAnimationTimerForElement(Element&);
105
106 bool allowsNewAnimationsWhileSuspended() const { return m_allowsNewAnimationsWhileSuspended; }
107 void setAllowsNewAnimationsWhileSuspended(bool);
108
109 void setRequiresLayout() { m_requiresLayout = true; }
110
111private:
112 void animationTimerFired();
113
114 void styleAvailable();
115 void fireEventsAndUpdateStyle();
116 void startTimeResponse(MonotonicTime);
117
118 HashMap<RefPtr<Element>, RefPtr<CompositeAnimation>> m_compositeAnimations;
119 Timer m_animationTimer;
120 Timer m_updateStyleIfNeededDispatcher;
121 Frame& m_frame;
122
123 struct EventToDispatch {
124 Ref<Element> element;
125 AtomString eventType;
126 String name;
127 double elapsedTime;
128 };
129 Vector<EventToDispatch> m_eventsToDispatch;
130 Vector<Ref<Element>> m_elementChangesToDispatch;
131 HashSet<Document*> m_suspendedDocuments;
132
133 Optional<MonotonicTime> m_beginAnimationUpdateTime;
134
135 using AnimationsSet = HashSet<RefPtr<AnimationBase>>;
136 AnimationsSet m_animationsWaitingForStyle;
137 AnimationsSet m_animationsWaitingForStartTimeResponse;
138
139 int m_beginAnimationUpdateCount;
140
141 bool m_waitingForAsyncStartNotification;
142 bool m_isSuspended { false };
143 bool m_requiresLayout { false };
144
145 // Used to flag whether we should revert to previous buggy
146 // behavior of allowing new transitions and animations to
147 // run even when this object is suspended.
148 bool m_allowsNewAnimationsWhileSuspended;
149};
150
151} // namespace WebCore
152