1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CC_ANIMATION_ANIMATION_HOST_H_
6 #define CC_ANIMATION_ANIMATION_HOST_H_
10 #include "base/containers/scoped_ptr_hash_map.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "cc/animation/animation_events.h"
15 #include "cc/base/cc_export.h"
16 #include "cc/trees/mutator_host_client.h"
24 class AnimationPlayer
;
25 class AnimationRegistrar
;
26 class AnimationTimeline
;
27 class ElementAnimations
;
28 class LayerAnimationController
;
31 enum class ThreadInstance
{ MAIN
, IMPL
};
33 typedef std::vector
<scoped_refptr
<AnimationTimeline
>> AnimationTimelineList
;
35 // An AnimationHost contains all the state required to play animations.
36 // Specifically, it owns all the AnimationTimelines objects.
37 // There is just one AnimationHost for LayerTreeHost on main renderer thread
38 // and just one AnimationHost for LayerTreeHostImpl on impl thread.
39 // We synchronize them during the commit process in a one-way data flow process
40 // (PushPropertiesTo).
41 // An AnimationHost talks to its correspondent LayerTreeHost via
42 // LayerTreeMutatorsClient interface.
43 // AnimationHost has it's own instance of AnimationRegistrar,
44 // we want to merge AnimationRegistrar into AnimationHost.
45 class CC_EXPORT AnimationHost
{
47 static scoped_ptr
<AnimationHost
> Create(ThreadInstance thread_instance
);
48 virtual ~AnimationHost();
50 void AddAnimationTimeline(scoped_refptr
<AnimationTimeline
> timeline
);
51 void RemoveAnimationTimeline(scoped_refptr
<AnimationTimeline
> timeline
);
52 AnimationTimeline
* GetTimelineById(int timeline_id
) const;
54 void ClearTimelines();
56 void RegisterLayer(int layer_id
, LayerTreeType tree_type
);
57 void UnregisterLayer(int layer_id
, LayerTreeType tree_type
);
59 void RegisterPlayerForLayer(int layer_id
, AnimationPlayer
* player
);
60 void UnregisterPlayerForLayer(int layer_id
, AnimationPlayer
* player
);
62 ElementAnimations
* GetElementAnimationsForLayerId(int layer_id
) const;
64 // TODO(loyso): Get rid of LayerAnimationController.
65 LayerAnimationController
* GetControllerForLayerId(int layer_id
) const;
67 // Parent LayerTreeHost or LayerTreeHostImpl.
68 MutatorHostClient
* mutator_host_client() { return mutator_host_client_
; }
69 const MutatorHostClient
* mutator_host_client() const {
70 return mutator_host_client_
;
72 void SetMutatorHostClient(MutatorHostClient
* client
);
74 void SetNeedsCommit();
76 void PushPropertiesTo(AnimationHost
* host_impl
);
78 AnimationRegistrar
* animation_registrar() const {
79 return animation_registrar_
.get();
82 void SetSupportsScrollAnimations(bool supports_scroll_animations
);
83 bool SupportsScrollAnimations() const;
84 bool NeedsAnimateLayers() const;
86 bool ActivateAnimations();
87 bool AnimateLayers(base::TimeTicks monotonic_time
);
88 bool UpdateAnimationState(bool start_ready_animations
,
89 AnimationEventsVector
* events
);
91 scoped_ptr
<AnimationEventsVector
> CreateEvents();
92 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
94 bool ScrollOffsetAnimationWasInterrupted(int layer_id
) const;
96 bool IsAnimatingFilterProperty(int layer_id
, LayerTreeType tree_type
) const;
97 bool IsAnimatingOpacityProperty(int layer_id
, LayerTreeType tree_type
) const;
98 bool IsAnimatingTransformProperty(int layer_id
,
99 LayerTreeType tree_type
) const;
101 bool HasPotentiallyRunningFilterAnimation(int layer_id
,
102 LayerTreeType tree_type
) const;
103 bool HasPotentiallyRunningOpacityAnimation(int layer_id
,
104 LayerTreeType tree_type
) const;
105 bool HasPotentiallyRunningTransformAnimation(int layer_id
,
106 LayerTreeType tree_type
) const;
108 bool HasAnyAnimationTargetingProperty(
110 Animation::TargetProperty property
) const;
112 bool FilterIsAnimatingOnImplOnly(int layer_id
) const;
113 bool OpacityIsAnimatingOnImplOnly(int layer_id
) const;
114 bool TransformIsAnimatingOnImplOnly(int layer_id
) const;
116 bool HasFilterAnimationThatInflatesBounds(int layer_id
) const;
117 bool HasTransformAnimationThatInflatesBounds(int layer_id
) const;
118 bool HasAnimationThatInflatesBounds(int layer_id
) const;
120 bool FilterAnimationBoundsForBox(int layer_id
,
121 const gfx::BoxF
& box
,
122 gfx::BoxF
* bounds
) const;
123 bool TransformAnimationBoundsForBox(int layer_id
,
124 const gfx::BoxF
& box
,
125 gfx::BoxF
* bounds
) const;
127 bool HasOnlyTranslationTransforms(int layer_id
) const;
128 bool AnimationsPreserveAxisAlignment(int layer_id
) const;
130 bool MaximumTargetScale(int layer_id
, float* max_scale
) const;
131 bool AnimationStartScale(int layer_id
, float* start_scale
) const;
133 bool HasAnyAnimation(int layer_id
) const;
134 bool HasActiveAnimation(int layer_id
) const;
136 void ImplOnlyScrollAnimationCreate(int layer_id
,
137 const gfx::ScrollOffset
& target_offset
,
138 const gfx::ScrollOffset
& current_offset
);
139 bool ImplOnlyScrollAnimationUpdateTarget(
141 const gfx::Vector2dF
& scroll_delta
,
142 const gfx::ScrollOffset
& max_scroll_offset
,
143 base::TimeTicks frame_monotonic_time
);
146 explicit AnimationHost(ThreadInstance thread_instance
);
148 void PushTimelinesToImplThread(AnimationHost
* host_impl
) const;
149 void RemoveTimelinesFromImplThread(AnimationHost
* host_impl
) const;
150 void PushPropertiesToImplThread(AnimationHost
* host_impl
);
152 void EraseTimelines(AnimationTimelineList::iterator begin
,
153 AnimationTimelineList::iterator end
);
155 // TODO(loyso): For now AnimationPlayers share LayerAnimationController object
156 // if they are attached to the same element(layer). Note that Element can
157 // contain many Layers.
158 typedef base::ScopedPtrHashMap
<int, scoped_ptr
<ElementAnimations
>>
159 LayerToElementAnimationsMap
;
160 LayerToElementAnimationsMap layer_to_element_animations_map_
;
162 AnimationTimelineList timelines_
;
163 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
164 MutatorHostClient
* mutator_host_client_
;
166 class ScrollOffsetAnimations
;
167 scoped_ptr
<ScrollOffsetAnimations
> scroll_offset_animations_
;
169 const ThreadInstance thread_instance_
;
171 DISALLOW_COPY_AND_ASSIGN(AnimationHost
);
176 #endif // CC_ANIMATION_ANIMATION_HOST_H_