Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CC_ANIMATION_ELEMENT_ANIMATIONS_H_ | 5 #ifndef CC_ANIMATION_ELEMENT_ANIMATIONS_H_ |
| 6 #define CC_ANIMATION_ELEMENT_ANIMATIONS_H_ | 6 #define CC_ANIMATION_ELEMENT_ANIMATIONS_H_ |
| 7 | 7 |
| 8 #include <bitset> | |
| 8 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | |
| 9 | 11 |
| 10 #include "base/containers/linked_list.h" | 12 #include "base/containers/linked_list.h" |
| 11 #include "base/macros.h" | 13 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/time/time.h" | |
| 16 #include "cc/animation/animation.h" | |
| 13 #include "cc/animation/animation_curve.h" | 17 #include "cc/animation/animation_curve.h" |
| 14 #include "cc/animation/animation_delegate.h" | 18 #include "cc/animation/animation_events.h" |
| 15 #include "cc/animation/layer_animation_controller.h" | 19 #include "cc/animation/target_property.h" |
| 16 #include "cc/animation/layer_animation_value_observer.h" | |
| 17 #include "cc/animation/layer_animation_value_provider.h" | |
| 18 #include "cc/base/cc_export.h" | 20 #include "cc/base/cc_export.h" |
| 21 #include "ui/gfx/geometry/scroll_offset.h" | |
| 22 #include "ui/gfx/transform.h" | |
| 19 | 23 |
| 20 namespace gfx { | 24 namespace gfx { |
| 21 class ScrollOffset; | 25 class BoxF; |
| 22 class Transform; | |
| 23 } | 26 } |
| 24 | 27 |
| 25 namespace cc { | 28 namespace cc { |
| 26 | 29 |
| 30 class AnimationDelegate; | |
| 31 class AnimationEvents; | |
| 27 class AnimationHost; | 32 class AnimationHost; |
| 28 class AnimationPlayer; | 33 class AnimationPlayer; |
| 29 class FilterOperations; | 34 class FilterOperations; |
| 30 class LayerAnimationController; | 35 class KeyframeValueList; |
| 31 enum class LayerTreeType; | 36 enum class LayerTreeType; |
| 32 | 37 |
| 33 // An ElementAnimations owns a list of all AnimationPlayers, attached to | 38 // An ElementAnimations owns a list of all AnimationPlayers, attached to |
| 34 // the layer. Also, it owns LayerAnimationController instance (1:1 | 39 // the layer. |
| 35 // relationship) | |
| 36 // ElementAnimations object redirects all events from LAC to the list | |
| 37 // of animation layers. | |
| 38 // This is a CC counterpart for blink::ElementAnimations (in 1:1 relationship). | 40 // This is a CC counterpart for blink::ElementAnimations (in 1:1 relationship). |
| 39 // No pointer to/from respective blink::ElementAnimations object for now. | 41 // No pointer to/from respective blink::ElementAnimations object for now. |
| 40 class CC_EXPORT ElementAnimations : public base::RefCounted<ElementAnimations>, | 42 class CC_EXPORT ElementAnimations : public base::RefCounted<ElementAnimations> { |
| 41 public AnimationDelegate, | |
| 42 public LayerAnimationValueObserver, | |
| 43 public LayerAnimationValueProvider { | |
| 44 public: | 43 public: |
| 45 static scoped_refptr<ElementAnimations> Create(AnimationHost* host); | 44 enum class ObserverType { ACTIVE, PENDING }; |
|
loyso (OOO)
2016/04/21 05:07:57
This is copied as-is from LAC. Will replace it wit
| |
| 46 | 45 |
| 47 int layer_id() const { | 46 static scoped_refptr<ElementAnimations> Create(); |
| 48 return layer_animation_controller_ ? layer_animation_controller_->id() : 0; | 47 |
| 49 } | 48 int layer_id() const { return layer_id_; } |
| 49 void SetLayerId(int layer_id); | |
| 50 | 50 |
| 51 // Parent AnimationHost. | 51 // Parent AnimationHost. |
| 52 AnimationHost* animation_host() { return animation_host_; } | 52 AnimationHost* animation_host() { return animation_host_; } |
| 53 const AnimationHost* animation_host() const { return animation_host_; } | 53 const AnimationHost* animation_host() const { return animation_host_; } |
| 54 void SetAnimationHost(AnimationHost* host); | |
| 54 | 55 |
| 55 void CreateLayerAnimationController(int layer_id); | 56 void InitValueObservations(); |
| 56 void DestroyLayerAnimationController(); | 57 void ClearValueObservations(); |
| 57 | 58 |
| 58 void LayerRegistered(int layer_id, LayerTreeType tree_type); | 59 void LayerRegistered(int layer_id, LayerTreeType tree_type); |
| 59 void LayerUnregistered(int layer_id, LayerTreeType tree_type); | 60 void LayerUnregistered(int layer_id, LayerTreeType tree_type); |
| 60 | 61 |
| 61 bool needs_active_value_observations() const { | |
| 62 return layer_animation_controller_->needs_active_value_observations(); | |
| 63 } | |
| 64 bool needs_pending_value_observations() const { | |
| 65 return layer_animation_controller_->needs_pending_value_observations(); | |
| 66 } | |
| 67 | |
| 68 void AddPlayer(AnimationPlayer* player); | 62 void AddPlayer(AnimationPlayer* player); |
| 69 void RemovePlayer(AnimationPlayer* player); | 63 void RemovePlayer(AnimationPlayer* player); |
| 70 bool IsEmpty() const; | 64 bool IsEmpty() const; |
| 71 | 65 |
| 72 typedef base::LinkedList<AnimationPlayer> PlayersList; | 66 typedef base::LinkedList<AnimationPlayer> PlayersList; |
| 73 typedef base::LinkNode<AnimationPlayer> PlayersListNode; | 67 typedef base::LinkNode<AnimationPlayer> PlayersListNode; |
| 74 const PlayersList& players_list() const { return *players_list_.get(); } | 68 const PlayersList& players_list() const { return *players_list_.get(); } |
| 75 | 69 |
| 70 // Ensures that the list of active animations on the main thread and the impl | |
| 71 // thread are kept in sync. This function does not take ownership of the impl | |
| 72 // thread ElementAnimations. | |
| 76 void PushPropertiesTo( | 73 void PushPropertiesTo( |
| 77 scoped_refptr<ElementAnimations> element_animations_impl); | 74 scoped_refptr<ElementAnimations> element_animations_impl); |
| 78 | 75 |
| 79 void AddAnimation(std::unique_ptr<Animation> animation); | 76 void AddAnimation(std::unique_ptr<Animation> animation); |
| 80 void PauseAnimation(int animation_id, base::TimeDelta time_offset); | 77 void PauseAnimation(int animation_id, base::TimeDelta time_offset); |
| 81 void RemoveAnimation(int animation_id); | 78 void RemoveAnimation(int animation_id); |
| 82 void AbortAnimation(int animation_id); | 79 void AbortAnimation(int animation_id); |
| 83 void AbortAnimations(TargetProperty::Type target_property, | 80 void AbortAnimations(TargetProperty::Type target_property, |
| 84 bool needs_completion = false); | 81 bool needs_completion = false); |
| 85 | 82 |
| 83 void Animate(base::TimeTicks monotonic_time); | |
| 84 void AccumulatePropertyUpdates(base::TimeTicks monotonic_time, | |
| 85 AnimationEvents* events); | |
| 86 | |
| 87 void UpdateState(bool start_ready_animations, AnimationEvents* events); | |
| 88 | |
| 89 // Make animations affect active observers if and only if they affect | |
| 90 // pending observers. Any animations that no longer affect any observers | |
| 91 // are deleted. | |
| 92 void ActivateAnimations(); | |
| 93 | |
| 86 // Returns the active animation animating the given property that is either | 94 // Returns the active animation animating the given property that is either |
| 87 // running, or is next to run, if such an animation exists. | 95 // running, or is next to run, if such an animation exists. |
| 88 Animation* GetAnimation(TargetProperty::Type target_property) const; | 96 Animation* GetAnimation(TargetProperty::Type target_property) const; |
| 89 | 97 |
| 90 // Returns the active animation for the given unique animation id. | 98 // Returns the active animation for the given unique animation id. |
| 91 Animation* GetAnimationById(int animation_id) const; | 99 Animation* GetAnimationById(int animation_id) const; |
| 92 | 100 |
| 101 // Returns true if there are any animations that have neither finished nor | |
| 102 // aborted. | |
| 103 bool HasActiveAnimation() const; | |
| 104 | |
| 105 // Returns true if there are any animations at all to process. | |
| 106 bool has_any_animation() const { return !animations_.empty(); } | |
| 107 | |
| 108 // Returns true if there is an animation that is either currently animating | |
| 109 // the given property or scheduled to animate this property in the future, and | |
| 110 // that affects the given observer type. | |
| 111 bool IsPotentiallyAnimatingProperty(TargetProperty::Type target_property, | |
| 112 ObserverType observer_type) const; | |
| 113 | |
| 114 // Returns true if there is an animation that is currently animating the given | |
| 115 // property and that affects the given observer type. | |
| 116 bool IsCurrentlyAnimatingProperty(TargetProperty::Type target_property, | |
| 117 ObserverType observer_type) const; | |
| 118 | |
| 119 void NotifyAnimationStarted(const AnimationEvent& event); | |
| 120 void NotifyAnimationFinished(const AnimationEvent& event); | |
| 121 void NotifyAnimationAborted(const AnimationEvent& event); | |
| 122 void NotifyAnimationPropertyUpdate(const AnimationEvent& event); | |
| 123 void NotifyAnimationTakeover(const AnimationEvent& event); | |
| 124 | |
| 125 bool needs_active_value_observations() const { | |
| 126 return needs_active_value_observations_; | |
| 127 } | |
| 128 bool needs_pending_value_observations() const { | |
| 129 return needs_pending_value_observations_; | |
| 130 } | |
| 131 | |
| 132 void set_needs_active_value_observations( | |
| 133 bool needs_active_value_observations) { | |
| 134 needs_active_value_observations_ = needs_active_value_observations; | |
| 135 } | |
| 136 void set_needs_pending_value_observations( | |
| 137 bool needs_pending_value_observations) { | |
| 138 needs_pending_value_observations_ = needs_pending_value_observations; | |
| 139 } | |
| 140 | |
| 141 bool HasFilterAnimationThatInflatesBounds() const; | |
| 142 bool HasTransformAnimationThatInflatesBounds() const; | |
| 143 bool HasAnimationThatInflatesBounds() const { | |
| 144 return HasTransformAnimationThatInflatesBounds() || | |
| 145 HasFilterAnimationThatInflatesBounds(); | |
| 146 } | |
| 147 | |
| 148 bool FilterAnimationBoundsForBox(const gfx::BoxF& box, | |
| 149 gfx::BoxF* bounds) const; | |
| 150 bool TransformAnimationBoundsForBox(const gfx::BoxF& box, | |
| 151 gfx::BoxF* bounds) const; | |
| 152 | |
| 153 bool HasAnimationThatAffectsScale() const; | |
| 154 | |
| 155 bool HasOnlyTranslationTransforms(ObserverType observer_type) const; | |
| 156 | |
| 157 bool AnimationsPreserveAxisAlignment() const; | |
| 158 | |
| 159 // Sets |start_scale| to the maximum of starting animation scale along any | |
| 160 // dimension at any destination in active animations. Returns false if the | |
| 161 // starting scale cannot be computed. | |
| 162 bool AnimationStartScale(ObserverType observer_type, | |
| 163 float* start_scale) const; | |
| 164 | |
| 165 // Sets |max_scale| to the maximum scale along any dimension at any | |
| 166 // destination in active animations. Returns false if the maximum scale cannot | |
| 167 // be computed. | |
| 168 bool MaximumTargetScale(ObserverType observer_type, float* max_scale) const; | |
| 169 | |
| 170 // When a scroll animation is removed on the main thread, its compositor | |
| 171 // thread counterpart continues producing scroll deltas until activation. | |
| 172 // These scroll deltas need to be cleared at activation, so that the active | |
| 173 // layer's scroll offset matches the offset provided by the main thread | |
| 174 // rather than a combination of this offset and scroll deltas produced by | |
| 175 // the removed animation. This is to provide the illusion of synchronicity to | |
| 176 // JS that simultaneously removes an animation and sets the scroll offset. | |
| 177 bool scroll_offset_animation_was_interrupted() const { | |
| 178 return scroll_offset_animation_was_interrupted_; | |
| 179 } | |
| 180 | |
| 181 bool needs_to_start_animations_for_testing() { | |
| 182 return needs_to_start_animations_; | |
| 183 } | |
| 184 | |
| 93 private: | 185 private: |
| 94 friend class base::RefCounted<ElementAnimations>; | 186 friend class base::RefCounted<ElementAnimations>; |
| 95 | 187 |
| 96 // TODO(loyso): Erase this when LAC merged into ElementAnimations. | 188 ElementAnimations(); |
| 97 friend class AnimationHost; | 189 ~ElementAnimations(); |
| 98 | 190 |
| 99 explicit ElementAnimations(AnimationHost* host); | 191 // A set of target properties. TargetProperty must be 0-based enum. |
| 100 ~ElementAnimations() override; | 192 using TargetProperties = |
| 193 std::bitset<TargetProperty::LAST_TARGET_PROPERTY + 1>; | |
| 101 | 194 |
| 102 // LayerAnimationValueObserver implementation. | 195 void PushNewAnimationsToImplThread( |
| 196 ElementAnimations* element_animations_impl) const; | |
| 197 void MarkAbortedAnimationsForDeletion( | |
| 198 ElementAnimations* element_animations_impl) const; | |
| 199 void RemoveAnimationsCompletedOnMainThread( | |
| 200 ElementAnimations* element_animations_impl) const; | |
| 201 void PushPropertiesToImplThread(ElementAnimations* element_animations_impl); | |
| 202 | |
| 203 void StartAnimations(base::TimeTicks monotonic_time); | |
| 204 void PromoteStartedAnimations(base::TimeTicks monotonic_time, | |
| 205 AnimationEvents* events); | |
| 206 void MarkFinishedAnimations(base::TimeTicks monotonic_time); | |
| 207 void MarkAnimationsForDeletion(base::TimeTicks monotonic_time, | |
| 208 AnimationEvents* events); | |
| 209 void PurgeAnimationsMarkedForDeletion(); | |
| 210 | |
| 211 void TickAnimations(base::TimeTicks monotonic_time); | |
| 212 | |
| 213 enum UpdateActivationType { NORMAL_ACTIVATION, FORCE_ACTIVATION }; | |
| 214 void UpdateActivation(UpdateActivationType type); | |
| 215 | |
| 216 void NotifyObserversOpacityAnimated(float opacity, | |
| 217 bool notify_active_observers, | |
| 218 bool notify_pending_observers); | |
| 219 void NotifyObserversTransformAnimated(const gfx::Transform& transform, | |
| 220 bool notify_active_observers, | |
| 221 bool notify_pending_observers); | |
| 222 void NotifyObserversFilterAnimated(const FilterOperations& filter, | |
| 223 bool notify_active_observers, | |
| 224 bool notify_pending_observers); | |
| 225 void NotifyObserversScrollOffsetAnimated( | |
| 226 const gfx::ScrollOffset& scroll_offset, | |
| 227 bool notify_active_observers, | |
| 228 bool notify_pending_observers); | |
| 229 | |
| 230 void NotifyObserversAnimationWaitingForDeletion(); | |
| 231 | |
| 232 void NotifyObserversTransformIsPotentiallyAnimatingChanged( | |
| 233 bool notify_active_observers, | |
| 234 bool notify_pending_observers); | |
| 235 | |
| 236 void UpdatePotentiallyAnimatingTransform(); | |
| 237 | |
| 103 void OnFilterAnimated(LayerTreeType tree_type, | 238 void OnFilterAnimated(LayerTreeType tree_type, |
| 104 const FilterOperations& filters) override; | 239 const FilterOperations& filters); |
| 105 void OnOpacityAnimated(LayerTreeType tree_type, float opacity) override; | 240 void OnOpacityAnimated(LayerTreeType tree_type, float opacity); |
| 106 void OnTransformAnimated(LayerTreeType tree_type, | 241 void OnTransformAnimated(LayerTreeType tree_type, |
| 107 const gfx::Transform& transform) override; | 242 const gfx::Transform& transform); |
| 108 void OnScrollOffsetAnimated(LayerTreeType tree_type, | 243 void OnScrollOffsetAnimated(LayerTreeType tree_type, |
| 109 const gfx::ScrollOffset& scroll_offset) override; | 244 const gfx::ScrollOffset& scroll_offset); |
| 110 void OnAnimationWaitingForDeletion() override; | 245 void OnAnimationWaitingForDeletion(); |
| 111 void OnTransformIsPotentiallyAnimatingChanged(LayerTreeType tree_type, | 246 void OnTransformIsPotentiallyAnimatingChanged(LayerTreeType tree_type, |
| 112 bool is_animating) override; | 247 bool is_animating); |
| 248 gfx::ScrollOffset ScrollOffsetForAnimation() const; | |
| 113 | 249 |
| 114 void CreateActiveValueObserver(); | 250 void NotifyPlayersAnimationStarted(base::TimeTicks monotonic_time, |
| 115 void DestroyActiveValueObserver(); | 251 TargetProperty::Type target_property, |
| 116 | 252 int group); |
| 117 void CreatePendingValueObserver(); | 253 void NotifyPlayersAnimationFinished(base::TimeTicks monotonic_time, |
| 118 void DestroyPendingValueObserver(); | 254 TargetProperty::Type target_property, |
| 119 | 255 int group); |
| 120 // AnimationDelegate implementation | 256 void NotifyPlayersAnimationAborted(base::TimeTicks monotonic_time, |
| 121 void NotifyAnimationStarted(base::TimeTicks monotonic_time, | 257 TargetProperty::Type target_property, |
| 122 TargetProperty::Type target_property, | 258 int group); |
| 123 int group) override; | 259 void NotifyPlayersAnimationPropertyUpdate(const AnimationEvent& event); |
| 124 void NotifyAnimationFinished(base::TimeTicks monotonic_time, | 260 void NotifyPlayersAnimationTakeover(base::TimeTicks monotonic_time, |
| 125 TargetProperty::Type target_property, | 261 TargetProperty::Type target_property, |
| 126 int group) override; | 262 double animation_start_time, |
| 127 void NotifyAnimationAborted(base::TimeTicks monotonic_time, | 263 std::unique_ptr<AnimationCurve> curve); |
| 128 TargetProperty::Type target_property, | |
| 129 int group) override; | |
| 130 void NotifyAnimationTakeover(base::TimeTicks monotonic_time, | |
| 131 TargetProperty::Type target_property, | |
| 132 double animation_start_time, | |
| 133 std::unique_ptr<AnimationCurve> curve) override; | |
| 134 | |
| 135 // LayerAnimationValueProvider implementation. | |
| 136 gfx::ScrollOffset ScrollOffsetForAnimation() const override; | |
| 137 | 264 |
| 138 std::unique_ptr<PlayersList> players_list_; | 265 std::unique_ptr<PlayersList> players_list_; |
| 266 AnimationHost* animation_host_; | |
| 267 int layer_id_; | |
| 268 std::vector<std::unique_ptr<Animation>> animations_; | |
| 139 | 269 |
| 140 // LAC is owned by ElementAnimations (1:1 relationship). | 270 // This is used to ensure that we don't spam the animation host. |
| 141 scoped_refptr<LayerAnimationController> layer_animation_controller_; | 271 bool is_active_; |
| 142 AnimationHost* animation_host_; | 272 |
| 273 base::TimeTicks last_tick_time_; | |
| 274 | |
| 275 bool needs_active_value_observations_; | |
| 276 bool needs_pending_value_observations_; | |
| 277 | |
| 278 // Only try to start animations when new animations are added or when the | |
| 279 // previous attempt at starting animations failed to start all animations. | |
| 280 bool needs_to_start_animations_; | |
| 281 | |
| 282 bool scroll_offset_animation_was_interrupted_; | |
| 283 | |
| 284 bool potentially_animating_transform_for_active_observers_; | |
| 285 bool potentially_animating_transform_for_pending_observers_; | |
| 143 | 286 |
| 144 DISALLOW_COPY_AND_ASSIGN(ElementAnimations); | 287 DISALLOW_COPY_AND_ASSIGN(ElementAnimations); |
| 145 }; | 288 }; |
| 146 | 289 |
| 147 } // namespace cc | 290 } // namespace cc |
| 148 | 291 |
| 149 #endif // CC_ANIMATION_ELEMENT_ANIMATIONS_H_ | 292 #endif // CC_ANIMATION_ELEMENT_ANIMATIONS_H_ |
| OLD | NEW |