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

cc / animation / scroll_offset_animations_impl.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_ANIMATION_SCROLL_OFFSET_ANIMATIONS_IMPL_H_
#define CC_ANIMATION_SCROLL_OFFSET_ANIMATIONS_IMPL_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "cc/animation/animation_delegate.h"
#include "cc/animation/scroll_offset_animation_curve.h"
#include "cc/trees/mutator_host_client.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc {

class Animation;
class AnimationHost;
class AnimationTimeline;

// This class represents a scroll offset animation that is managed by only the
// impl thread, i.e. an impl-only scroll animation. It contains an
// AnimationTimeline and owns the (impl-only) scroll offset Animation running
// on a particular CC Layer. It exists only on the compositor thread.
class CC_ANIMATION_EXPORT ScrollOffsetAnimationImpl : public AnimationDelegate {
 public:
  explicit ScrollOffsetAnimationImpl(AnimationHost* animation_host);
  ScrollOffsetAnimationImpl(const ScrollOffsetAnimationImpl&) = delete;
  ~ScrollOffsetAnimationImpl() override;

  ScrollOffsetAnimationImpl& operator=(const ScrollOffsetAnimationImpl&) =
      delete;

  void AutoScrollAnimationCreate(ElementId element_id,
                                 const gfx::PointF& target_offset,
                                 const gfx::PointF& current_offset,
                                 float autoscroll_velocity,
                                 base::TimeDelta animation_start_offset);

  // |delayed_by| shrinks the duration of the
  // animation. |animation_start_offset| causes us to start the animation
  // partway through.
  void MouseWheelScrollAnimationCreate(ElementId element_id,
                                       const gfx::PointF& target_offset,
                                       const gfx::PointF& current_offset,
                                       base::TimeDelta delayed_by,
                                       base::TimeDelta animation_start_offset);

  std::optional<gfx::PointF> ScrollAnimationUpdateTarget(
      const gfx::Vector2dF& scroll_delta,
      const gfx::PointF& max_scroll_offset,
      base::TimeTicks frame_monotonic_time,
      base::TimeDelta delayed_by);

  // AnimationDelegate implementation.
  void NotifyAnimationStarted(base::TimeTicks monotonic_time,
                              int target_property,
                              int group) override {}
  void NotifyAnimationFinished(base::TimeTicks monotonic_time,
                               int target_property,
                               int group) override;
  void NotifyAnimationAborted(base::TimeTicks monotonic_time,
                              int target_property,
                              int group) override {}
  void NotifyAnimationTakeover(
      base::TimeTicks monotonic_time,
      int target_property,
      base::TimeTicks animation_start_time,
      std::unique_ptr<gfx::AnimationCurve> curve) override {}
  void NotifyLocalTimeUpdated(
      std::optional<base::TimeDelta> local_time) override {}

  // Aborts the currently running scroll offset animation on an element and
  // starts a new one offsetted by adjustment.
  void ScrollAnimationApplyAdjustment(ElementId element_id,
                                      const gfx::Vector2dF& adjustment);

  void ScrollAnimationAbort(bool needs_completion);
  void AnimatingElementRemovedByCommit();

  bool IsAnimating() const;
  bool IsAutoScrolling() const;
  ElementId GetElementId() const;

 private:
  void ScrollAnimationCreateInternal(ElementId element_id,
                                     std::unique_ptr<gfx::AnimationCurve> curve,
                                     base::TimeDelta animation_start_offset);

  void ReattachScrollOffsetAnimationIfNeeded(ElementId element_id);

  raw_ptr<AnimationHost> animation_host_;
  scoped_refptr<AnimationTimeline> scroll_offset_timeline_;
  scoped_refptr<Animation> scroll_offset_animation_;
  bool animation_is_autoscroll_ = false;
};

// Pre-MultiImplScrollAnimations:
// Contains an ScrollOffsetAnimationImpl which encapsulates the scroll offset
// animation running on a particular CC Layer.
// We have just one animation for impl-only scroll offset animations. I.e. only
// one element can have an impl-only scroll offset animation at any given time.
//
// Post-MultiImplScrollAnimations:
// Contains a map from ElementId to ScrollOffsetAnimationImpl that owns the impl
// only scroll offset animation running on a particular CC Layer.
//
// Note that this class only exists on the compositor thread.
class CC_ANIMATION_EXPORT ScrollOffsetAnimationsImpl {
 public:
  explicit ScrollOffsetAnimationsImpl(AnimationHost* animation_host);
  ScrollOffsetAnimationsImpl(const ScrollOffsetAnimationsImpl&) = delete;
  ~ScrollOffsetAnimationsImpl();

  ScrollOffsetAnimationsImpl& operator=(const ScrollOffsetAnimationsImpl&) =
      delete;

  void AutoScrollAnimationCreate(ElementId element_id,
                                 const gfx::PointF& target_offset,
                                 const gfx::PointF& current_offset,
                                 float autoscroll_velocity,
                                 base::TimeDelta animation_start_offset);

  // |delayed_by| shrinks the duration of the
  // animation. |animation_start_offset| causes us to start the animation
  // partway through.
  void MouseWheelScrollAnimationCreate(ElementId element_id,
                                       const gfx::PointF& target_offset,
                                       const gfx::PointF& current_offset,
                                       base::TimeDelta delayed_by,
                                       base::TimeDelta animation_start_offset);

  std::optional<gfx::PointF> ScrollAnimationUpdateTarget(
      const gfx::Vector2dF& scroll_delta,
      const gfx::PointF& max_scroll_offset,
      base::TimeTicks frame_monotonic_time,
      base::TimeDelta delayed_by,
      ElementId element_id);

  // Aborts the currently running scroll offset animation on an element and
  // starts a new one offsetted by adjustment.
  void ScrollAnimationApplyAdjustment(ElementId element_id,
                                      const gfx::Vector2dF& adjustment);

  void ScrollAnimationAbort(bool needs_completion, ElementId element_id);

  void HandleRemovedScrollAnimatingElements(bool commits_to_active);

  bool ElementHasImplOnlyScrollAnimation(ElementId element_id) const;
  bool HasImplOnlyScrollAnimatingElement() const;
  bool HasImplOnlyAutoScrollAnimatingElement() const;

  bool IsAnimating() const;
  bool IsAutoScrolling() const;
  ElementId GetElementId() const;

 private:
  // This retrieves the ScrollOffsetAnimationImpl object associated with the
  // given ElementId. It is only used when MultiImplScrollAnimations is enabled.
  ScrollOffsetAnimationImpl* GetScrollAnimation(ElementId element_id) const;

  raw_ptr<AnimationHost> animation_host_;

  // We have just one animation for impl-only scroll offset animations.
  // I.e. only one element can have an impl-only scroll offset animation at
  // any given time.
  std::unique_ptr<ScrollOffsetAnimationImpl> scroll_offset_animation_;

  // This maps each animating scroll container's ElementId to a
  // ScrollOffsetAnimationImpl object. It is only used when
  // MultiImplScrollAnimations is enabled.
  base::flat_map<ElementId, std::unique_ptr<ScrollOffsetAnimationImpl>>
      element_to_animation_map_;
};

}  // namespace cc

#endif  // CC_ANIMATION_SCROLL_OFFSET_ANIMATIONS_IMPL_H_