forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation_host.h
238 lines (184 loc) · 8.79 KB
/
animation_host.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_ANIMATION_ANIMATION_HOST_H_
#define CC_ANIMATION_ANIMATION_HOST_H_
#include <memory>
#include <unordered_map>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "cc/animation/animation_export.h"
#include "cc/animation/keyframe_model.h"
#include "cc/trees/mutator_host.h"
#include "cc/trees/mutator_host_client.h"
#include "ui/gfx/geometry/box_f.h"
#include "ui/gfx/geometry/vector2d_f.h"
namespace gfx {
class ScrollOffset;
}
namespace cc {
class Animation;
class AnimationTimeline;
class ElementAnimations;
class LayerTreeHost;
class KeyframeEffect;
class ScrollOffsetAnimations;
class ScrollOffsetAnimationsImpl;
enum class ThreadInstance { MAIN, IMPL };
// An AnimationHost contains all the state required to play animations.
// Specifically, it owns all the AnimationTimelines objects.
// There is just one AnimationHost for LayerTreeHost on main renderer thread
// and just one AnimationHost for LayerTreeHostImpl on impl thread.
// We synchronize them during the commit process in a one-way data flow process
// (PushPropertiesTo).
// An AnimationHost talks to its correspondent LayerTreeHost via
// MutatorHostClient interface.
class CC_ANIMATION_EXPORT AnimationHost : public MutatorHost,
public LayerTreeMutatorClient {
public:
using ElementToAnimationsMap =
std::unordered_map<ElementId,
scoped_refptr<ElementAnimations>,
ElementIdHash>;
using AnimationsList = std::vector<scoped_refptr<Animation>>;
static std::unique_ptr<AnimationHost> CreateMainInstance();
static std::unique_ptr<AnimationHost> CreateForTesting(
ThreadInstance thread_instance);
~AnimationHost() override;
void AddAnimationTimeline(scoped_refptr<AnimationTimeline> timeline);
void RemoveAnimationTimeline(scoped_refptr<AnimationTimeline> timeline);
AnimationTimeline* GetTimelineById(int timeline_id) const;
void RegisterKeyframeEffectForElement(ElementId element_id,
KeyframeEffect* keyframe_effect);
void UnregisterKeyframeEffectForElement(ElementId element_id,
KeyframeEffect* keyframe_effect);
scoped_refptr<ElementAnimations> GetElementAnimationsForElementId(
ElementId element_id) const;
// Parent LayerTreeHost or LayerTreeHostImpl.
MutatorHostClient* mutator_host_client() { return mutator_host_client_; }
const MutatorHostClient* mutator_host_client() const {
return mutator_host_client_;
}
void SetNeedsCommit();
void SetNeedsPushProperties();
bool needs_push_properties() const { return needs_push_properties_; }
bool SupportsScrollAnimations() const;
// MutatorHost implementation.
std::unique_ptr<MutatorHost> CreateImplInstance(
bool supports_impl_scrolling) const override;
void ClearMutators() override;
void RegisterElement(ElementId element_id,
ElementListType list_type) override;
void UnregisterElement(ElementId element_id,
ElementListType list_type) override;
void SetMutatorHostClient(MutatorHostClient* client) override;
void SetLayerTreeMutator(std::unique_ptr<LayerTreeMutator> mutator) override;
void PushPropertiesTo(MutatorHost* host_impl) override;
void SetSupportsScrollAnimations(bool supports_scroll_animations) override;
bool NeedsTickAnimations() const override;
bool ActivateAnimations() override;
bool TickAnimations(base::TimeTicks monotonic_time,
const ScrollTree& scroll_tree) override;
void TickScrollAnimations(base::TimeTicks monotonic_time,
const ScrollTree& scroll_tree) override;
bool UpdateAnimationState(bool start_ready_animations,
MutatorEvents* events) override;
std::unique_ptr<MutatorEvents> CreateEvents() override;
void SetAnimationEvents(std::unique_ptr<MutatorEvents> events) override;
bool ScrollOffsetAnimationWasInterrupted(ElementId element_id) const override;
bool IsAnimatingFilterProperty(ElementId element_id,
ElementListType list_type) const override;
bool IsAnimatingOpacityProperty(ElementId element_id,
ElementListType list_type) const override;
bool IsAnimatingTransformProperty(ElementId element_id,
ElementListType list_type) const override;
bool HasPotentiallyRunningFilterAnimation(
ElementId element_id,
ElementListType list_type) const override;
bool HasPotentiallyRunningOpacityAnimation(
ElementId element_id,
ElementListType list_type) const override;
bool HasPotentiallyRunningTransformAnimation(
ElementId element_id,
ElementListType list_type) const override;
bool HasAnyAnimationTargetingProperty(
ElementId element_id,
TargetProperty::Type property) const override;
bool HasOnlyTranslationTransforms(ElementId element_id,
ElementListType list_type) const override;
bool AnimationsPreserveAxisAlignment(ElementId element_id) const override;
bool MaximumTargetScale(ElementId element_id,
ElementListType list_type,
float* max_scale) const override;
bool AnimationStartScale(ElementId element_id,
ElementListType list_type,
float* start_scale) const override;
bool IsElementAnimating(ElementId element_id) const override;
bool HasTickingKeyframeModelForTesting(ElementId element_id) const override;
void ImplOnlyScrollAnimationCreate(
ElementId element_id,
const gfx::ScrollOffset& target_offset,
const gfx::ScrollOffset& current_offset,
base::TimeDelta delayed_by,
base::TimeDelta animation_start_offset) override;
bool ImplOnlyScrollAnimationUpdateTarget(
ElementId element_id,
const gfx::Vector2dF& scroll_delta,
const gfx::ScrollOffset& max_scroll_offset,
base::TimeTicks frame_monotonic_time,
base::TimeDelta delayed_by) override;
void ScrollAnimationAbort() override;
// This should only be called from the main thread.
ScrollOffsetAnimations& scroll_offset_animations() const;
// Registers the given animation as ticking. A ticking animation is one that
// has a running keyframe model.
void AddToTicking(scoped_refptr<Animation> animation);
// Unregisters the given animation. When this happens, the animation will no
// longer be ticked.
void RemoveFromTicking(scoped_refptr<Animation> animation);
const AnimationsList& ticking_animations_for_testing() const;
const ElementToAnimationsMap& element_animations_for_testing() const;
// LayerTreeMutatorClient.
void SetMutationUpdate(
std::unique_ptr<MutatorOutputState> output_state) override;
size_t CompositedAnimationsCount() const override;
size_t MainThreadAnimationsCount() const override;
bool CurrentFrameHadRAF() const override;
bool NextFrameHasPendingRAF() const override;
void SetAnimationCounts(size_t total_animations_count,
bool current_frame_had_raf,
bool next_frame_has_pending_raf);
private:
explicit AnimationHost(ThreadInstance thread_instance);
void PushTimelinesToImplThread(AnimationHost* host_impl) const;
void RemoveTimelinesFromImplThread(AnimationHost* host_impl) const;
void PushPropertiesToImplThread(AnimationHost* host_impl);
void EraseTimeline(scoped_refptr<AnimationTimeline> timeline);
bool NeedsTickMutator(base::TimeTicks monotonic_time,
const ScrollTree& scroll_tree) const;
// Return the animator state representing all ticking worklet animations.
std::unique_ptr<MutatorInputState> CollectAnimatorsState(
base::TimeTicks timeline_time,
const ScrollTree& scroll_tree);
ElementToAnimationsMap element_to_animations_map_;
AnimationsList ticking_animations_;
// A list of all timelines which this host owns.
using IdToTimelineMap =
std::unordered_map<int, scoped_refptr<AnimationTimeline>>;
IdToTimelineMap id_to_timeline_map_;
MutatorHostClient* mutator_host_client_;
std::unique_ptr<ScrollOffsetAnimations> scroll_offset_animations_;
std::unique_ptr<ScrollOffsetAnimationsImpl> scroll_offset_animations_impl_;
const ThreadInstance thread_instance_;
bool supports_scroll_animations_;
bool needs_push_properties_;
std::unique_ptr<LayerTreeMutator> mutator_;
size_t main_thread_animations_count_ = 0;
bool current_frame_had_raf_ = false;
bool next_frame_has_pending_raf_ = false;
DISALLOW_COPY_AND_ASSIGN(AnimationHost);
};
} // namespace cc
#endif // CC_ANIMATION_ANIMATION_HOST_H_