Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: cc/animation/animation_host.cc

Issue 1952563002: Rename and refactor ScrollOffsetAnimations to ScrollOffsetAnimationsImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/ScrollOffsetAnimationsHostImpl/ScrollOffsetAnimationsImpl Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "cc/animation/animation_host.h" 5 #include "cc/animation/animation_host.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/trace_event/trace_event.h" 11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h" 12 #include "base/trace_event/trace_event_argument.h"
13 #include "cc/animation/animation_delegate.h" 13 #include "cc/animation/animation_delegate.h"
14 #include "cc/animation/animation_events.h" 14 #include "cc/animation/animation_events.h"
15 #include "cc/animation/animation_id_provider.h" 15 #include "cc/animation/animation_id_provider.h"
16 #include "cc/animation/animation_player.h" 16 #include "cc/animation/animation_player.h"
17 #include "cc/animation/animation_timeline.h" 17 #include "cc/animation/animation_timeline.h"
18 #include "cc/animation/element_animations.h" 18 #include "cc/animation/element_animations.h"
19 #include "cc/animation/scroll_offset_animation_curve.h" 19 #include "cc/animation/scroll_offset_animation_curve.h"
20 #include "cc/animation/timing_function.h" 20 #include "cc/animation/timing_function.h"
21 #include "ui/gfx/geometry/box_f.h" 21 #include "ui/gfx/geometry/box_f.h"
22 #include "ui/gfx/geometry/scroll_offset.h" 22 #include "ui/gfx/geometry/scroll_offset.h"
23 23
24 namespace cc { 24 namespace cc {
25 25
26 class AnimationHost::ScrollOffsetAnimations : public AnimationDelegate {
27 public:
28 explicit ScrollOffsetAnimations(AnimationHost* animation_host)
29 : animation_host_(animation_host),
30 scroll_offset_timeline_(
31 AnimationTimeline::Create(AnimationIdProvider::NextTimelineId())),
32 scroll_offset_animation_player_(
33 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId())) {
34 scroll_offset_timeline_->set_is_impl_only(true);
35 scroll_offset_animation_player_->set_animation_delegate(this);
36
37 animation_host_->AddAnimationTimeline(scroll_offset_timeline_.get());
38 scroll_offset_timeline_->AttachPlayer(
39 scroll_offset_animation_player_.get());
40 }
41
42 ~ScrollOffsetAnimations() override {
43 scroll_offset_timeline_->DetachPlayer(
44 scroll_offset_animation_player_.get());
45 animation_host_->RemoveAnimationTimeline(scroll_offset_timeline_.get());
46 }
47
48 void ScrollAnimationCreate(ElementId element_id,
49 const gfx::ScrollOffset& target_offset,
50 const gfx::ScrollOffset& current_offset) {
51 std::unique_ptr<ScrollOffsetAnimationCurve> curve =
52 ScrollOffsetAnimationCurve::Create(
53 target_offset, EaseInOutTimingFunction::Create(),
54 ScrollOffsetAnimationCurve::DurationBehavior::INVERSE_DELTA);
55 curve->SetInitialValue(current_offset);
56
57 std::unique_ptr<Animation> animation = Animation::Create(
58 std::move(curve), AnimationIdProvider::NextAnimationId(),
59 AnimationIdProvider::NextGroupId(), TargetProperty::SCROLL_OFFSET);
60 animation->set_is_impl_only(true);
61
62 DCHECK(scroll_offset_animation_player_);
63 DCHECK(scroll_offset_animation_player_->animation_timeline());
64
65 ReattachScrollOffsetPlayerIfNeeded(element_id);
66
67 scroll_offset_animation_player_->AddAnimation(std::move(animation));
68 }
69
70 bool ScrollAnimationUpdateTarget(ElementId element_id,
71 const gfx::Vector2dF& scroll_delta,
72 const gfx::ScrollOffset& max_scroll_offset,
73 base::TimeTicks frame_monotonic_time) {
74 DCHECK(scroll_offset_animation_player_);
75 if (!scroll_offset_animation_player_->element_animations())
76 return false;
77
78 DCHECK_EQ(element_id, scroll_offset_animation_player_->element_id());
79
80 Animation* animation = scroll_offset_animation_player_->element_animations()
81 ->GetAnimation(TargetProperty::SCROLL_OFFSET);
82 if (!animation) {
83 scroll_offset_animation_player_->DetachElement();
84 return false;
85 }
86
87 ScrollOffsetAnimationCurve* curve =
88 animation->curve()->ToScrollOffsetAnimationCurve();
89
90 gfx::ScrollOffset new_target =
91 gfx::ScrollOffsetWithDelta(curve->target_value(), scroll_delta);
92 new_target.SetToMax(gfx::ScrollOffset());
93 new_target.SetToMin(max_scroll_offset);
94
95 curve->UpdateTarget(animation->TrimTimeToCurrentIteration(
96 frame_monotonic_time).InSecondsF(),
97 new_target);
98
99 return true;
100 }
101
102 void ScrollAnimationAbort(bool needs_completion) {
103 DCHECK(scroll_offset_animation_player_);
104 scroll_offset_animation_player_->AbortAnimations(
105 TargetProperty::SCROLL_OFFSET, needs_completion);
106 }
107
108 // AnimationDelegate implementation.
109 void NotifyAnimationStarted(base::TimeTicks monotonic_time,
110 TargetProperty::Type target_property,
111 int group) override {}
112 void NotifyAnimationFinished(base::TimeTicks monotonic_time,
113 TargetProperty::Type target_property,
114 int group) override {
115 DCHECK_EQ(target_property, TargetProperty::SCROLL_OFFSET);
116 DCHECK(animation_host_->mutator_host_client());
117 animation_host_->mutator_host_client()->ScrollOffsetAnimationFinished();
118 }
119 void NotifyAnimationAborted(base::TimeTicks monotonic_time,
120 TargetProperty::Type target_property,
121 int group) override {}
122 void NotifyAnimationTakeover(base::TimeTicks monotonic_time,
123 TargetProperty::Type target_property,
124 double animation_start_time,
125 std::unique_ptr<AnimationCurve> curve) override {
126 }
127
128 private:
129 void ReattachScrollOffsetPlayerIfNeeded(ElementId element_id) {
130 if (scroll_offset_animation_player_->element_id() != element_id) {
131 if (scroll_offset_animation_player_->element_id())
132 scroll_offset_animation_player_->DetachElement();
133 if (element_id)
134 scroll_offset_animation_player_->AttachElement(element_id);
135 }
136 }
137
138 AnimationHost* animation_host_;
139 scoped_refptr<AnimationTimeline> scroll_offset_timeline_;
140
141 // We have just one player for impl-only scroll offset animations.
142 // I.e. only one element can have an impl-only scroll offset animation at
143 // any given time.
144 scoped_refptr<AnimationPlayer> scroll_offset_animation_player_;
145
146 DISALLOW_COPY_AND_ASSIGN(ScrollOffsetAnimations);
147 };
148
149 std::unique_ptr<AnimationHost> AnimationHost::Create( 26 std::unique_ptr<AnimationHost> AnimationHost::Create(
150 ThreadInstance thread_instance) { 27 ThreadInstance thread_instance) {
151 return base::WrapUnique(new AnimationHost(thread_instance)); 28 return base::WrapUnique(new AnimationHost(thread_instance));
152 } 29 }
153 30
154 AnimationHost::AnimationHost(ThreadInstance thread_instance) 31 AnimationHost::AnimationHost(ThreadInstance thread_instance)
155 : mutator_host_client_(nullptr), 32 : mutator_host_client_(nullptr),
156 thread_instance_(thread_instance), 33 thread_instance_(thread_instance),
157 supports_scroll_animations_(false), 34 supports_scroll_animations_(false),
158 animation_waiting_for_deletion_(false) { 35 animation_waiting_for_deletion_(false) {
159 if (thread_instance_ == ThreadInstance::IMPL) 36 if (thread_instance_ == ThreadInstance::IMPL)
160 scroll_offset_animations_ = 37 scroll_offset_animations_impl_ =
161 base::WrapUnique(new ScrollOffsetAnimations(this)); 38 base::WrapUnique(new ScrollOffsetAnimationsImpl(this));
162 } 39 }
163 40
164 AnimationHost::~AnimationHost() { 41 AnimationHost::~AnimationHost() {
165 scroll_offset_animations_ = nullptr; 42 scroll_offset_animations_impl_ = nullptr;
166 43
167 ClearTimelines(); 44 ClearTimelines();
168 DCHECK(!mutator_host_client()); 45 DCHECK(!mutator_host_client());
169 DCHECK(element_to_animations_map_.empty()); 46 DCHECK(element_to_animations_map_.empty());
170 } 47 }
171 48
172 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const { 49 AnimationTimeline* AnimationHost::GetTimelineById(int timeline_id) const {
173 auto f = id_to_timeline_map_.find(timeline_id); 50 auto f = id_to_timeline_map_.find(timeline_id);
174 return f == id_to_timeline_map_.end() ? nullptr : f->second.get(); 51 return f == id_to_timeline_map_.end() ? nullptr : f->second.get();
175 } 52 }
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 511
635 bool AnimationHost::HasActiveAnimationForTesting(ElementId element_id) const { 512 bool AnimationHost::HasActiveAnimationForTesting(ElementId element_id) const {
636 auto element_animations = GetElementAnimationsForElementId(element_id); 513 auto element_animations = GetElementAnimationsForElementId(element_id);
637 return element_animations ? element_animations->HasActiveAnimation() : false; 514 return element_animations ? element_animations->HasActiveAnimation() : false;
638 } 515 }
639 516
640 void AnimationHost::ImplOnlyScrollAnimationCreate( 517 void AnimationHost::ImplOnlyScrollAnimationCreate(
641 ElementId element_id, 518 ElementId element_id,
642 const gfx::ScrollOffset& target_offset, 519 const gfx::ScrollOffset& target_offset,
643 const gfx::ScrollOffset& current_offset) { 520 const gfx::ScrollOffset& current_offset) {
644 DCHECK(scroll_offset_animations_); 521 DCHECK(scroll_offset_animations_impl_);
645 scroll_offset_animations_->ScrollAnimationCreate(element_id, target_offset, 522 scroll_offset_animations_impl_->ScrollAnimationCreate(
646 current_offset); 523 element_id, target_offset, current_offset);
647 } 524 }
648 525
649 bool AnimationHost::ImplOnlyScrollAnimationUpdateTarget( 526 bool AnimationHost::ImplOnlyScrollAnimationUpdateTarget(
650 ElementId element_id, 527 ElementId element_id,
651 const gfx::Vector2dF& scroll_delta, 528 const gfx::Vector2dF& scroll_delta,
652 const gfx::ScrollOffset& max_scroll_offset, 529 const gfx::ScrollOffset& max_scroll_offset,
653 base::TimeTicks frame_monotonic_time) { 530 base::TimeTicks frame_monotonic_time) {
654 DCHECK(scroll_offset_animations_); 531 DCHECK(scroll_offset_animations_impl_);
655 return scroll_offset_animations_->ScrollAnimationUpdateTarget( 532 return scroll_offset_animations_impl_->ScrollAnimationUpdateTarget(
656 element_id, scroll_delta, max_scroll_offset, frame_monotonic_time); 533 element_id, scroll_delta, max_scroll_offset, frame_monotonic_time);
657 } 534 }
658 535
659 void AnimationHost::ScrollAnimationAbort(bool needs_completion) { 536 void AnimationHost::ScrollAnimationAbort(bool needs_completion) {
660 DCHECK(scroll_offset_animations_); 537 DCHECK(scroll_offset_animations_impl_);
661 return scroll_offset_animations_->ScrollAnimationAbort(needs_completion); 538 return scroll_offset_animations_impl_->ScrollAnimationAbort(needs_completion);
662 } 539 }
663 540
664 void AnimationHost::DidActivateElementAnimations( 541 void AnimationHost::DidActivateElementAnimations(
665 ElementAnimations* element_animations) { 542 ElementAnimations* element_animations) {
666 DCHECK(element_animations->element_id()); 543 DCHECK(element_animations->element_id());
667 active_element_to_animations_map_[element_animations->element_id()] = 544 active_element_to_animations_map_[element_animations->element_id()] =
668 element_animations; 545 element_animations;
669 } 546 }
670 547
671 void AnimationHost::DidDeactivateElementAnimations( 548 void AnimationHost::DidDeactivateElementAnimations(
(...skipping 24 matching lines...) Expand all
696 const AnimationHost::ElementToAnimationsMap& 573 const AnimationHost::ElementToAnimationsMap&
697 AnimationHost::all_element_animations_for_testing() const { 574 AnimationHost::all_element_animations_for_testing() const {
698 return element_to_animations_map_; 575 return element_to_animations_map_;
699 } 576 }
700 577
701 void AnimationHost::OnAnimationWaitingForDeletion() { 578 void AnimationHost::OnAnimationWaitingForDeletion() {
702 animation_waiting_for_deletion_ = true; 579 animation_waiting_for_deletion_ = true;
703 } 580 }
704 581
705 } // namespace cc 582 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698