scoped_layer_animation_settings.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (c) 2012 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.
  4. #include "ui/compositor/scoped_layer_animation_settings.h"
  5. #include <stddef.h>
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/observer_list.h"
  8. #include "ui/compositor/layer.h"
  9. #include "ui/compositor/layer_animation_observer.h"
  10. #include "ui/compositor/layer_animation_sequence.h"
  11. #include "ui/compositor/layer_animator.h"
  12. #include "ui/compositor/layer_observer.h"
  13. namespace ui {
  14. namespace {
  15. const int kScopedLayerAnimationDefaultTransitionDurationMs = 200;
  16. template <typename Trait>
  17. class ScopedLayerAnimationObserver : public ui::ImplicitAnimationObserver,
  18. public ui::LayerObserver {
  19. public:
  20. ScopedLayerAnimationObserver(ui::Layer* layer) : layer_(layer) {
  21. layer_->AddObserver(this);
  22. Trait::AddRequest(layer_);
  23. }
  24. ScopedLayerAnimationObserver(const ScopedLayerAnimationObserver&) = delete;
  25. ScopedLayerAnimationObserver& operator=(const ScopedLayerAnimationObserver&) =
  26. delete;
  27. ~ScopedLayerAnimationObserver() override {
  28. if (layer_)
  29. layer_->RemoveObserver(this);
  30. }
  31. // ui::ImplicitAnimationObserver overrides:
  32. void OnImplicitAnimationsCompleted() override {
  33. // If animation finishes before |layer_| is destoyed, we will remove the
  34. // request applied on the layer and remove |this| from the |layer_|
  35. // observer list when deleting |this|.
  36. if (layer_) {
  37. Trait::RemoveRequest(layer_);
  38. layer_->GetAnimator()->RemoveAndDestroyOwnedObserver(this);
  39. }
  40. }
  41. // ui::LayerObserver overrides:
  42. void LayerDestroyed(ui::Layer* layer) override {
  43. // If the animation is still going past layer destruction then we want the
  44. // layer to keep the request until the animation has finished. We will defer
  45. // deleting |this| until the animation finishes.
  46. layer_->RemoveObserver(this);
  47. layer_ = nullptr;
  48. }
  49. private:
  50. raw_ptr<ui::Layer> layer_;
  51. };
  52. struct RenderSurfaceCachingTrait {
  53. static void AddRequest(ui::Layer* layer) {
  54. layer->AddCacheRenderSurfaceRequest();
  55. }
  56. static void RemoveRequest(ui::Layer* layer) {
  57. layer->RemoveCacheRenderSurfaceRequest();
  58. }
  59. };
  60. using ScopedRenderSurfaceCaching =
  61. ScopedLayerAnimationObserver<RenderSurfaceCachingTrait>;
  62. struct DeferredPaintingTrait {
  63. static void AddRequest(ui::Layer* layer) { layer->AddDeferredPaintRequest(); }
  64. static void RemoveRequest(ui::Layer* layer) {
  65. layer->RemoveDeferredPaintRequest();
  66. }
  67. };
  68. using ScopedDeferredPainting =
  69. ScopedLayerAnimationObserver<DeferredPaintingTrait>;
  70. struct TrilinearFilteringTrait {
  71. static void AddRequest(ui::Layer* layer) {
  72. layer->AddTrilinearFilteringRequest();
  73. }
  74. static void RemoveRequest(ui::Layer* layer) {
  75. layer->RemoveTrilinearFilteringRequest();
  76. }
  77. };
  78. using ScopedTrilinearFiltering =
  79. ScopedLayerAnimationObserver<TrilinearFilteringTrait>;
  80. void AddObserverToSettings(
  81. ui::ScopedLayerAnimationSettings* settings,
  82. std::unique_ptr<ui::ImplicitAnimationObserver> observer) {
  83. settings->AddObserver(observer.get());
  84. settings->GetAnimator()->AddOwnedObserver(std::move(observer));
  85. }
  86. void AddScopedDeferredPaintingObserverRecursive(
  87. ui::Layer* layer,
  88. ui::ScopedLayerAnimationSettings* settings) {
  89. auto observer = std::make_unique<ScopedDeferredPainting>(layer);
  90. AddObserverToSettings(settings, std::move(observer));
  91. for (auto* child : layer->children())
  92. AddScopedDeferredPaintingObserverRecursive(child, settings);
  93. }
  94. } // namespace
  95. // ScopedLayerAnimationSettings ------------------------------------------------
  96. ScopedLayerAnimationSettings::ScopedLayerAnimationSettings(
  97. scoped_refptr<LayerAnimator> animator)
  98. : animator_(animator),
  99. old_is_transition_duration_locked_(
  100. animator->is_transition_duration_locked_),
  101. old_transition_duration_(animator->GetTransitionDuration()),
  102. old_tween_type_(animator->tween_type()),
  103. old_preemption_strategy_(animator->preemption_strategy()) {
  104. SetTransitionDuration(
  105. base::Milliseconds(kScopedLayerAnimationDefaultTransitionDurationMs));
  106. }
  107. ScopedLayerAnimationSettings::~ScopedLayerAnimationSettings() {
  108. animator_->is_transition_duration_locked_ =
  109. old_is_transition_duration_locked_;
  110. animator_->SetTransitionDuration(old_transition_duration_);
  111. animator_->set_tween_type(old_tween_type_);
  112. animator_->set_preemption_strategy(old_preemption_strategy_);
  113. for (auto* observer : observers_) {
  114. // Directly remove |observer| from |LayerAnimator::observers_| rather than
  115. // calling LayerAnimator::RemoveObserver(), to avoid removing it from the
  116. // observer list of LayerAnimationSequences that have already been
  117. // scheduled.
  118. animator_->observers_.RemoveObserver(observer);
  119. observer->SetActive(true);
  120. }
  121. }
  122. void ScopedLayerAnimationSettings::AddObserver(
  123. ImplicitAnimationObserver* observer) {
  124. observers_.insert(observer);
  125. animator_->AddObserver(observer);
  126. }
  127. void ScopedLayerAnimationSettings::SetTransitionDuration(
  128. base::TimeDelta duration) {
  129. animator_->SetTransitionDuration(duration);
  130. }
  131. base::TimeDelta ScopedLayerAnimationSettings::GetTransitionDuration() const {
  132. return animator_->GetTransitionDuration();
  133. }
  134. void ScopedLayerAnimationSettings::LockTransitionDuration() {
  135. animator_->is_transition_duration_locked_ = true;
  136. }
  137. void ScopedLayerAnimationSettings::SetTweenType(gfx::Tween::Type tween_type) {
  138. animator_->set_tween_type(tween_type);
  139. }
  140. gfx::Tween::Type ScopedLayerAnimationSettings::GetTweenType() const {
  141. return animator_->tween_type();
  142. }
  143. void ScopedLayerAnimationSettings::SetPreemptionStrategy(
  144. LayerAnimator::PreemptionStrategy strategy) {
  145. animator_->set_preemption_strategy(strategy);
  146. }
  147. LayerAnimator::PreemptionStrategy
  148. ScopedLayerAnimationSettings::GetPreemptionStrategy() const {
  149. return animator_->preemption_strategy();
  150. }
  151. void ScopedLayerAnimationSettings::CacheRenderSurface() {
  152. auto observer = std::make_unique<ScopedRenderSurfaceCaching>(
  153. animator_->delegate()->GetLayer());
  154. AddObserverToSettings(this, std::move(observer));
  155. }
  156. void ScopedLayerAnimationSettings::DeferPaint() {
  157. AddScopedDeferredPaintingObserverRecursive(animator_->delegate()->GetLayer(),
  158. this);
  159. }
  160. void ScopedLayerAnimationSettings::TrilinearFiltering() {
  161. auto observer = std::make_unique<ScopedTrilinearFiltering>(
  162. animator_->delegate()->GetLayer());
  163. AddObserverToSettings(this, std::move(observer));
  164. }
  165. } // namespace ui