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

Side by Side Diff: ui/compositor/layer.cc

Issue 1012583002: CC Animations: Port UI Browser Compositor to use compositor animation timelines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@redirect
Patch Set: Rebase. Created 5 years 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
« no previous file with comments | « ui/compositor/layer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "ui/compositor/layer.h" 5 #include "ui/compositor/layer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/trace_event/trace_event.h" 15 #include "base/trace_event/trace_event.h"
16 #include "cc/animation/animation_id_provider.h"
17 #include "cc/animation/animation_player.h"
18 #include "cc/animation/animation_timeline.h"
19 #include "cc/animation/element_animations.h"
16 #include "cc/layers/delegated_renderer_layer.h" 20 #include "cc/layers/delegated_renderer_layer.h"
17 #include "cc/layers/layer_settings.h" 21 #include "cc/layers/layer_settings.h"
18 #include "cc/layers/nine_patch_layer.h" 22 #include "cc/layers/nine_patch_layer.h"
19 #include "cc/layers/picture_layer.h" 23 #include "cc/layers/picture_layer.h"
20 #include "cc/layers/solid_color_layer.h" 24 #include "cc/layers/solid_color_layer.h"
21 #include "cc/layers/surface_layer.h" 25 #include "cc/layers/surface_layer.h"
22 #include "cc/layers/texture_layer.h" 26 #include "cc/layers/texture_layer.h"
23 #include "cc/output/copy_output_request.h" 27 #include "cc/output/copy_output_request.h"
24 #include "cc/output/delegated_frame_data.h" 28 #include "cc/output/delegated_frame_data.h"
25 #include "cc/output/filter_operation.h" 29 #include "cc/output/filter_operation.h"
26 #include "cc/output/filter_operations.h" 30 #include "cc/output/filter_operations.h"
27 #include "cc/playback/display_item_list_settings.h" 31 #include "cc/playback/display_item_list_settings.h"
28 #include "cc/resources/transferable_resource.h" 32 #include "cc/resources/transferable_resource.h"
33 #include "cc/trees/layer_tree_host.h"
29 #include "cc/trees/layer_tree_settings.h" 34 #include "cc/trees/layer_tree_settings.h"
30 #include "ui/compositor/compositor_switches.h" 35 #include "ui/compositor/compositor_switches.h"
31 #include "ui/compositor/dip_util.h" 36 #include "ui/compositor/dip_util.h"
32 #include "ui/compositor/layer_animator.h" 37 #include "ui/compositor/layer_animator.h"
33 #include "ui/compositor/paint_context.h" 38 #include "ui/compositor/paint_context.h"
34 #include "ui/gfx/animation/animation.h" 39 #include "ui/gfx/animation/animation.h"
35 #include "ui/gfx/canvas.h" 40 #include "ui/gfx/canvas.h"
36 #include "ui/gfx/display.h" 41 #include "ui/gfx/display.h"
37 #include "ui/gfx/geometry/point3_f.h" 42 #include "ui/gfx/geometry/point3_f.h"
38 #include "ui/gfx/geometry/point_conversions.h" 43 #include "ui/gfx/geometry/point_conversions.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 compositor_->SetRootLayer(NULL); 118 compositor_->SetRootLayer(NULL);
114 if (parent_) 119 if (parent_)
115 parent_->Remove(this); 120 parent_->Remove(this);
116 if (layer_mask_) 121 if (layer_mask_)
117 SetMaskLayer(NULL); 122 SetMaskLayer(NULL);
118 if (layer_mask_back_link_) 123 if (layer_mask_back_link_)
119 layer_mask_back_link_->SetMaskLayer(NULL); 124 layer_mask_back_link_->SetMaskLayer(NULL);
120 for (size_t i = 0; i < children_.size(); ++i) 125 for (size_t i = 0; i < children_.size(); ++i)
121 children_[i]->parent_ = NULL; 126 children_[i]->parent_ = NULL;
122 127
123 cc_layer_->RemoveLayerAnimationEventObserver(this); 128 if (animation_player_)
129 DetachPlayerLayer();
130 else
131 cc_layer_->RemoveLayerAnimationEventObserver(this);
132
124 cc_layer_->RemoveFromParent(); 133 cc_layer_->RemoveFromParent();
125 } 134 }
126 135
127 // static 136 // static
128 const cc::LayerSettings& Layer::UILayerSettings() { 137 const cc::LayerSettings& Layer::UILayerSettings() {
129 return g_ui_layer_settings.Get(); 138 return g_ui_layer_settings.Get();
130 } 139 }
131 140
132 // static 141 // static
133 void Layer::InitializeUILayerSettings() { 142 void Layer::InitializeUILayerSettings() {
(...skipping 17 matching lines...) Expand all
151 DCHECK(compositor); 160 DCHECK(compositor);
152 DCHECK(!compositor_); 161 DCHECK(!compositor_);
153 DCHECK(compositor->root_layer() == this); 162 DCHECK(compositor->root_layer() == this);
154 DCHECK(!parent_); 163 DCHECK(!parent_);
155 164
156 compositor_ = compositor; 165 compositor_ = compositor;
157 OnDeviceScaleFactorChanged(compositor->device_scale_factor()); 166 OnDeviceScaleFactorChanged(compositor->device_scale_factor());
158 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection()); 167 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection());
159 168
160 root_layer->AddChild(cc_layer_); 169 root_layer->AddChild(cc_layer_);
170 AttachAnimationPlayers(compositor->GetAnimationTimeline());
171 AttachPlayerLayer();
161 SendPendingThreadedAnimations(); 172 SendPendingThreadedAnimations();
162 } 173 }
163 174
164 void Layer::ResetCompositor() { 175 void Layer::ResetCompositor() {
165 DCHECK(!parent_); 176 DCHECK(!parent_);
166 if (compositor_) 177 if (compositor_) {
178 DetachPlayerLayer();
179 DetachAnimationPlayers(compositor_->GetAnimationTimeline());
167 RemoveAnimatorsInTreeFromCollection( 180 RemoveAnimatorsInTreeFromCollection(
168 compositor_->layer_animator_collection()); 181 compositor_->layer_animator_collection());
182 }
169 compositor_ = nullptr; 183 compositor_ = nullptr;
170 } 184 }
171 185
172 void Layer::Add(Layer* child) { 186 void Layer::Add(Layer* child) {
173 DCHECK(!child->compositor_); 187 DCHECK(!child->compositor_);
174 if (child->parent_) 188 if (child->parent_)
175 child->parent_->Remove(child); 189 child->parent_->Remove(child);
176 child->parent_ = this; 190 child->parent_ = this;
177 children_.push_back(child); 191 children_.push_back(child);
178 cc_layer_->AddChild(child->cc_layer_); 192 cc_layer_->AddChild(child->cc_layer_);
179 child->OnDeviceScaleFactorChanged(device_scale_factor_); 193 child->OnDeviceScaleFactorChanged(device_scale_factor_);
180 if (GetCompositor()) 194 Compositor* compositor = GetCompositor();
195 if (compositor) {
196 child->AttachAnimationPlayers(compositor->GetAnimationTimeline());
197 child->AttachPlayerLayer();
181 child->SendPendingThreadedAnimations(); 198 child->SendPendingThreadedAnimations();
199 }
182 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 200 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
183 if (collection) 201 if (collection)
184 child->AddAnimatorsInTreeToCollection(collection); 202 child->AddAnimatorsInTreeToCollection(collection);
185 } 203 }
186 204
187 void Layer::Remove(Layer* child) { 205 void Layer::Remove(Layer* child) {
188 // Current bounds are used to calculate offsets when layers are reparented. 206 // Current bounds are used to calculate offsets when layers are reparented.
189 // Stop (and complete) an ongoing animation to update the bounds immediately. 207 // Stop (and complete) an ongoing animation to update the bounds immediately.
190 LayerAnimator* child_animator = child->animator_.get(); 208 LayerAnimator* child_animator = child->animator_.get();
191 if (child_animator) 209 if (child_animator)
192 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); 210 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS);
193 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 211 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
194 if (collection) 212 if (collection)
195 child->RemoveAnimatorsInTreeFromCollection(collection); 213 child->RemoveAnimatorsInTreeFromCollection(collection);
196 214
215 child->DetachPlayerLayer();
216 Compositor* compositor = GetCompositor();
217 if (compositor)
218 child->DetachAnimationPlayers(compositor->GetAnimationTimeline());
219
197 std::vector<Layer*>::iterator i = 220 std::vector<Layer*>::iterator i =
198 std::find(children_.begin(), children_.end(), child); 221 std::find(children_.begin(), children_.end(), child);
199 DCHECK(i != children_.end()); 222 DCHECK(i != children_.end());
200 children_.erase(i); 223 children_.erase(i);
201 child->parent_ = NULL; 224 child->parent_ = NULL;
202 child->cc_layer_->RemoveFromParent(); 225 child->cc_layer_->RemoveFromParent();
203 } 226 }
204 227
205 void Layer::StackAtTop(Layer* child) { 228 void Layer::StackAtTop(Layer* child) {
206 if (children_.size() <= 1 || child == children_.back()) 229 if (children_.size() <= 1 || child == children_.back())
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 515
493 if (texture_layer_.get()) 516 if (texture_layer_.get())
494 texture_layer_->ClearClient(); 517 texture_layer_->ClearClient();
495 // TODO(piman): delegated_renderer_layer_ cleanup. 518 // TODO(piman): delegated_renderer_layer_ cleanup.
496 519
497 cc_layer_->RemoveAllChildren(); 520 cc_layer_->RemoveAllChildren();
498 if (cc_layer_->parent()) { 521 if (cc_layer_->parent()) {
499 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer); 522 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer);
500 } 523 }
501 cc_layer_->SetLayerClient(NULL); 524 cc_layer_->SetLayerClient(NULL);
502 cc_layer_->RemoveLayerAnimationEventObserver(this); 525
526 if (animation_player_)
527 DetachPlayerLayer();
528 else
529 cc_layer_->RemoveLayerAnimationEventObserver(this);
530
503 new_layer->SetOpacity(cc_layer_->opacity()); 531 new_layer->SetOpacity(cc_layer_->opacity());
504 new_layer->SetTransform(cc_layer_->transform()); 532 new_layer->SetTransform(cc_layer_->transform());
505 new_layer->SetPosition(cc_layer_->position()); 533 new_layer->SetPosition(cc_layer_->position());
506 new_layer->SetBackgroundColor(cc_layer_->background_color()); 534 new_layer->SetBackgroundColor(cc_layer_->background_color());
507 535
508 cc_layer_ = new_layer.get(); 536 cc_layer_ = new_layer.get();
509 content_layer_ = NULL; 537 content_layer_ = NULL;
510 solid_color_layer_ = NULL; 538 solid_color_layer_ = NULL;
511 texture_layer_ = NULL; 539 texture_layer_ = NULL;
512 delegated_renderer_layer_ = NULL; 540 delegated_renderer_layer_ = NULL;
513 surface_layer_ = NULL; 541 surface_layer_ = NULL;
514 542
515 cc_layer_->AddLayerAnimationEventObserver(this); 543 if (animation_player_)
544 AttachPlayerLayer();
545 else
546 cc_layer_->AddLayerAnimationEventObserver(this);
547
516 for (size_t i = 0; i < children_.size(); ++i) { 548 for (size_t i = 0; i < children_.size(); ++i) {
517 DCHECK(children_[i]->cc_layer_); 549 DCHECK(children_[i]->cc_layer_);
518 cc_layer_->AddChild(children_[i]->cc_layer_); 550 cc_layer_->AddChild(children_[i]->cc_layer_);
519 } 551 }
520 cc_layer_->SetLayerClient(this); 552 cc_layer_->SetLayerClient(this);
521 cc_layer_->SetTransformOrigin(gfx::Point3F()); 553 cc_layer_->SetTransformOrigin(gfx::Point3F());
522 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_); 554 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_);
523 cc_layer_->SetForceRenderSurface(force_render_surface_); 555 cc_layer_->SetForceRenderSurface(force_render_surface_);
524 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 556 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
525 cc_layer_->SetHideLayerAndSubtree(!visible_); 557 cc_layer_->SetHideLayerAndSubtree(!visible_);
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 } 1006 }
975 1007
976 float Layer::GetDeviceScaleFactor() const { 1008 float Layer::GetDeviceScaleFactor() const {
977 return device_scale_factor_; 1009 return device_scale_factor_;
978 } 1010 }
979 1011
980 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) { 1012 void Layer::AddThreadedAnimation(scoped_ptr<cc::Animation> animation) {
981 DCHECK(cc_layer_); 1013 DCHECK(cc_layer_);
982 // Until this layer has a compositor (and hence cc_layer_ has a 1014 // Until this layer has a compositor (and hence cc_layer_ has a
983 // LayerTreeHost), addAnimation will fail. 1015 // LayerTreeHost), addAnimation will fail.
984 if (GetCompositor()) 1016 if (GetCompositor()) {
985 cc_layer_->AddAnimation(animation.Pass()); 1017 animation_player_ ? animation_player_->AddAnimation(animation.Pass())
986 else 1018 : (void)cc_layer_->AddAnimation(animation.Pass());
1019 } else {
987 pending_threaded_animations_.push_back(animation.Pass()); 1020 pending_threaded_animations_.push_back(animation.Pass());
1021 }
988 } 1022 }
989 1023
990 void Layer::RemoveThreadedAnimation(int animation_id) { 1024 void Layer::RemoveThreadedAnimation(int animation_id) {
991 DCHECK(cc_layer_); 1025 DCHECK(cc_layer_);
992 if (pending_threaded_animations_.size() == 0) { 1026 if (pending_threaded_animations_.size() == 0) {
993 cc_layer_->RemoveAnimation(animation_id); 1027 animation_player_ ? animation_player_->RemoveAnimation(animation_id)
1028 : cc_layer_->RemoveAnimation(animation_id);
994 return; 1029 return;
995 } 1030 }
996 1031
997 pending_threaded_animations_.erase( 1032 pending_threaded_animations_.erase(
998 std::remove_if( 1033 std::remove_if(
999 pending_threaded_animations_.begin(), 1034 pending_threaded_animations_.begin(),
1000 pending_threaded_animations_.end(), 1035 pending_threaded_animations_.end(),
1001 [animation_id](const scoped_ptr<cc::Animation>& animation) { 1036 [animation_id](const scoped_ptr<cc::Animation>& animation) {
1002 return animation->id() == animation_id; 1037 return animation->id() == animation_id;
1003 }), 1038 }),
1004 pending_threaded_animations_.end()); 1039 pending_threaded_animations_.end());
1005 } 1040 }
1006 1041
1007 LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() { 1042 LayerAnimatorCollection* Layer::GetLayerAnimatorCollection() {
1008 Compositor* compositor = GetCompositor(); 1043 Compositor* compositor = GetCompositor();
1009 return compositor ? compositor->layer_animator_collection() : NULL; 1044 return compositor ? compositor->layer_animator_collection() : NULL;
1010 } 1045 }
1011 1046
1012 void Layer::SendPendingThreadedAnimations() { 1047 void Layer::SendPendingThreadedAnimations() {
1013 for (auto& animation : pending_threaded_animations_) 1048 for (auto& animation : pending_threaded_animations_) {
1014 cc_layer_->AddAnimation(std::move(animation)); 1049 animation_player_
1050 ? animation_player_->AddAnimation(std::move(animation))
1051 : (void)cc_layer_->AddAnimation(std::move(animation));
1052 }
1015 pending_threaded_animations_.clear(); 1053 pending_threaded_animations_.clear();
1016 1054
1017 for (auto* child : children_) 1055 for (auto* child : children_)
1018 child->SendPendingThreadedAnimations(); 1056 child->SendPendingThreadedAnimations();
1019 } 1057 }
1020 1058
1021 void Layer::CreateCcLayer() { 1059 void Layer::CreateCcLayer() {
1022 if (type_ == LAYER_SOLID_COLOR) { 1060 if (type_ == LAYER_SOLID_COLOR) {
1023 solid_color_layer_ = cc::SolidColorLayer::Create(UILayerSettings()); 1061 solid_color_layer_ = cc::SolidColorLayer::Create(UILayerSettings());
1024 cc_layer_ = solid_color_layer_.get(); 1062 cc_layer_ = solid_color_layer_.get();
1025 } else if (type_ == LAYER_NINE_PATCH) { 1063 } else if (type_ == LAYER_NINE_PATCH) {
1026 nine_patch_layer_ = cc::NinePatchLayer::Create(UILayerSettings()); 1064 nine_patch_layer_ = cc::NinePatchLayer::Create(UILayerSettings());
1027 cc_layer_ = nine_patch_layer_.get(); 1065 cc_layer_ = nine_patch_layer_.get();
1028 } else { 1066 } else {
1029 content_layer_ = cc::PictureLayer::Create(UILayerSettings(), this); 1067 content_layer_ = cc::PictureLayer::Create(UILayerSettings(), this);
1030 cc_layer_ = content_layer_.get(); 1068 cc_layer_ = content_layer_.get();
1031 } 1069 }
1032 cc_layer_->SetTransformOrigin(gfx::Point3F()); 1070 cc_layer_->SetTransformOrigin(gfx::Point3F());
1033 cc_layer_->SetContentsOpaque(true); 1071 cc_layer_->SetContentsOpaque(true);
1034 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 1072 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
1035 cc_layer_->AddLayerAnimationEventObserver(this); 1073 if (UILayerSettings().use_compositor_animation_timelines)
1074 animation_player_ =
1075 cc::AnimationPlayer::Create(cc::AnimationIdProvider::NextPlayerId());
1076 else
1077 cc_layer_->AddLayerAnimationEventObserver(this);
1036 cc_layer_->SetLayerClient(this); 1078 cc_layer_->SetLayerClient(this);
1037 RecomputePosition(); 1079 RecomputePosition();
1038 } 1080 }
1039 1081
1082 void Layer::AttachPlayerLayer() {
1083 DCHECK(cc_layer_);
1084
1085 if (animation_player_) {
1086 animation_player_->AttachLayer(cc_layer_->id());
1087 DCHECK(animation_player_->element_animations());
1088 animation_player_->element_animations()
1089 ->layer_animation_controller()
1090 ->AddEventObserver(this);
1091 }
1092
1093 for (size_t i = 0; i < children_.size(); ++i)
1094 children_[i]->AttachPlayerLayer();
1095 }
1096
1097 void Layer::DetachPlayerLayer() {
1098 if (animation_player_) {
1099 if (animation_player_->element_animations()) {
1100 animation_player_->element_animations()
1101 ->layer_animation_controller()
1102 ->RemoveEventObserver(this);
1103 animation_player_->DetachLayer();
1104 }
1105 }
1106
1107 for (size_t i = 0; i < children_.size(); ++i)
1108 children_[i]->DetachPlayerLayer();
1109 }
1110
1111 void Layer::AttachAnimationPlayers(cc::AnimationTimeline* timeline) {
1112 if (!timeline)
1113 return;
1114
1115 if (animation_player_)
1116 timeline->AttachPlayer(animation_player_);
1117 for (size_t i = 0; i < children_.size(); ++i)
1118 children_[i]->AttachAnimationPlayers(timeline);
1119 }
1120
1121 void Layer::DetachAnimationPlayers(cc::AnimationTimeline* timeline) {
1122 if (!timeline)
1123 return;
1124
1125 if (animation_player_)
1126 timeline->DetachPlayer(animation_player_);
1127 for (size_t i = 0; i < children_.size(); ++i)
1128 children_[i]->DetachAnimationPlayers(timeline);
1129 }
1130
1040 gfx::Transform Layer::transform() const { 1131 gfx::Transform Layer::transform() const {
1041 return cc_layer_->transform(); 1132 return cc_layer_->transform();
1042 } 1133 }
1043 1134
1044 void Layer::RecomputeDrawsContentAndUVRect() { 1135 void Layer::RecomputeDrawsContentAndUVRect() {
1045 DCHECK(cc_layer_); 1136 DCHECK(cc_layer_);
1046 gfx::Size size(bounds_.size()); 1137 gfx::Size size(bounds_.size());
1047 if (texture_layer_.get()) { 1138 if (texture_layer_.get()) {
1048 size.SetToMin(frame_size_in_dip_); 1139 size.SetToMin(frame_size_in_dip_);
1049 gfx::PointF uv_top_left(0.f, 0.f); 1140 gfx::PointF uv_top_left(0.f, 0.f);
(...skipping 28 matching lines...) Expand all
1078 animator_->RemoveFromCollection(collection); 1169 animator_->RemoveFromCollection(collection);
1079 for (auto* child : children_) 1170 for (auto* child : children_)
1080 child->RemoveAnimatorsInTreeFromCollection(collection); 1171 child->RemoveAnimatorsInTreeFromCollection(collection);
1081 } 1172 }
1082 1173
1083 bool Layer::IsAnimating() const { 1174 bool Layer::IsAnimating() const {
1084 return animator_.get() && animator_->is_animating(); 1175 return animator_.get() && animator_->is_animating();
1085 } 1176 }
1086 1177
1087 } // namespace ui 1178 } // namespace ui
OLDNEW
« no previous file with comments | « ui/compositor/layer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698