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

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

Issue 1101823002: CC Animations: Make LayerAnimationController creation optional (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up. Created 5 years, 8 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 (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/base/scoped_ptr_algorithm.h" 16 #include "cc/base/scoped_ptr_algorithm.h"
17 #include "cc/layers/content_layer.h" 17 #include "cc/layers/content_layer.h"
18 #include "cc/layers/delegated_renderer_layer.h" 18 #include "cc/layers/delegated_renderer_layer.h"
19 #include "cc/layers/nine_patch_layer.h" 19 #include "cc/layers/nine_patch_layer.h"
20 #include "cc/layers/picture_layer.h" 20 #include "cc/layers/picture_layer.h"
21 #include "cc/layers/solid_color_layer.h" 21 #include "cc/layers/solid_color_layer.h"
22 #include "cc/layers/surface_layer.h" 22 #include "cc/layers/surface_layer.h"
23 #include "cc/layers/texture_layer.h" 23 #include "cc/layers/texture_layer.h"
24 #include "cc/output/copy_output_request.h" 24 #include "cc/output/copy_output_request.h"
25 #include "cc/output/delegated_frame_data.h" 25 #include "cc/output/delegated_frame_data.h"
26 #include "cc/output/filter_operation.h" 26 #include "cc/output/filter_operation.h"
27 #include "cc/output/filter_operations.h" 27 #include "cc/output/filter_operations.h"
28 #include "cc/resources/transferable_resource.h" 28 #include "cc/resources/transferable_resource.h"
29 #include "cc/trees/layer_tree_host.h"
29 #include "ui/compositor/compositor_switches.h" 30 #include "ui/compositor/compositor_switches.h"
30 #include "ui/compositor/dip_util.h" 31 #include "ui/compositor/dip_util.h"
31 #include "ui/compositor/layer_animator.h" 32 #include "ui/compositor/layer_animator.h"
32 #include "ui/compositor/paint_context.h" 33 #include "ui/compositor/paint_context.h"
33 #include "ui/gfx/animation/animation.h" 34 #include "ui/gfx/animation/animation.h"
34 #include "ui/gfx/canvas.h" 35 #include "ui/gfx/canvas.h"
35 #include "ui/gfx/display.h" 36 #include "ui/gfx/display.h"
36 #include "ui/gfx/geometry/point3_f.h" 37 #include "ui/gfx/geometry/point3_f.h"
37 #include "ui/gfx/geometry/point_conversions.h" 38 #include "ui/gfx/geometry/point_conversions.h"
38 #include "ui/gfx/geometry/size_conversions.h" 39 #include "ui/gfx/geometry/size_conversions.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 if (compositor_) 118 if (compositor_)
118 compositor_->SetRootLayer(NULL); 119 compositor_->SetRootLayer(NULL);
119 if (parent_) 120 if (parent_)
120 parent_->Remove(this); 121 parent_->Remove(this);
121 if (layer_mask_) 122 if (layer_mask_)
122 SetMaskLayer(NULL); 123 SetMaskLayer(NULL);
123 if (layer_mask_back_link_) 124 if (layer_mask_back_link_)
124 layer_mask_back_link_->SetMaskLayer(NULL); 125 layer_mask_back_link_->SetMaskLayer(NULL);
125 for (size_t i = 0; i < children_.size(); ++i) 126 for (size_t i = 0; i < children_.size(); ++i)
126 children_[i]->parent_ = NULL; 127 children_[i]->parent_ = NULL;
127 cc_layer_->RemoveLayerAnimationEventObserver(this); 128
129 DetachAnimationObservers();
130
128 cc_layer_->RemoveFromParent(); 131 cc_layer_->RemoveFromParent();
129 } 132 }
130 133
131 // static 134 // static
132 bool Layer::UsingPictureLayer() { 135 bool Layer::UsingPictureLayer() {
133 return g_ui_impl_side_painting_status.Get().enabled; 136 return g_ui_impl_side_painting_status.Get().enabled;
134 } 137 }
135 138
136 const Compositor* Layer::GetCompositor() const { 139 const Compositor* Layer::GetCompositor() const {
137 return GetRoot(this)->compositor_; 140 return GetRoot(this)->compositor_;
138 } 141 }
139 142
140 float Layer::opacity() const { 143 float Layer::opacity() const {
141 return cc_layer_->opacity(); 144 return cc_layer_->opacity();
142 } 145 }
143 146
144 void Layer::SetCompositor(Compositor* compositor) { 147 void Layer::SetCompositor(Compositor* compositor) {
145 // This function must only be called to set the compositor on the root layer, 148 // This function must only be called to set the compositor on the root layer.
146 // or to reset it. 149 DCHECK(compositor);
147 DCHECK(!compositor || !compositor_); 150 DCHECK(!compositor_);
148 DCHECK(!compositor || compositor->root_layer() == this); 151 DCHECK(compositor->root_layer() == this);
152 DCHECK(!parent_);
153
154 compositor_ = compositor;
155 OnDeviceScaleFactorChanged(compositor->device_scale_factor());
156 SendPendingThreadedAnimations();
ajuma 2015/04/24 16:02:16 This needs to happen after the cc layers are regis
loyso (OOO) 2015/04/27 06:34:08 Done.
157 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection());
158 }
159
160 void Layer::SetCompositorRootLayer(scoped_refptr<cc::Layer> root_layer) {
161 // This function must only be called to add to the compositor root CC layer.
162 root_layer->AddChild(cc_layer_);
163 AttachAnimationObservers();
164 }
165
166 void Layer::ResetCompositor() {
149 DCHECK(!parent_); 167 DCHECK(!parent_);
150 if (compositor_) { 168 if (compositor_) {
169 DetachAnimationObservers();
151 RemoveAnimatorsInTreeFromCollection( 170 RemoveAnimatorsInTreeFromCollection(
152 compositor_->layer_animator_collection()); 171 compositor_->layer_animator_collection());
153 } 172 }
154 compositor_ = compositor; 173 compositor_ = nullptr;
155 if (compositor) {
156 OnDeviceScaleFactorChanged(compositor->device_scale_factor());
157 SendPendingThreadedAnimations();
158 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection());
159 }
160 } 174 }
161 175
162 void Layer::Add(Layer* child) { 176 void Layer::Add(Layer* child) {
163 DCHECK(!child->compositor_); 177 DCHECK(!child->compositor_);
164 if (child->parent_) 178 if (child->parent_)
165 child->parent_->Remove(child); 179 child->parent_->Remove(child);
166 child->parent_ = this; 180 child->parent_ = this;
167 children_.push_back(child); 181 children_.push_back(child);
168 cc_layer_->AddChild(child->cc_layer_); 182 cc_layer_->AddChild(child->cc_layer_);
169 child->OnDeviceScaleFactorChanged(device_scale_factor_); 183 child->OnDeviceScaleFactorChanged(device_scale_factor_);
170 if (GetCompositor()) 184 Compositor* compositor = GetCompositor();
185 if (compositor) {
186 child->AttachAnimationObservers();
171 child->SendPendingThreadedAnimations(); 187 child->SendPendingThreadedAnimations();
188 }
172 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 189 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
173 if (collection) 190 if (collection)
174 child->AddAnimatorsInTreeToCollection(collection); 191 child->AddAnimatorsInTreeToCollection(collection);
175 } 192 }
176 193
177 void Layer::Remove(Layer* child) { 194 void Layer::Remove(Layer* child) {
178 // Current bounds are used to calculate offsets when layers are reparented. 195 // Current bounds are used to calculate offsets when layers are reparented.
179 // Stop (and complete) an ongoing animation to update the bounds immediately. 196 // Stop (and complete) an ongoing animation to update the bounds immediately.
180 LayerAnimator* child_animator = child->animator_.get(); 197 LayerAnimator* child_animator = child->animator_.get();
181 if (child_animator) 198 if (child_animator)
182 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); 199 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS);
183 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 200 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
184 if (collection) 201 if (collection)
185 child->RemoveAnimatorsInTreeFromCollection(collection); 202 child->RemoveAnimatorsInTreeFromCollection(collection);
186 203
204 child->DetachAnimationObservers();
205
187 std::vector<Layer*>::iterator i = 206 std::vector<Layer*>::iterator i =
188 std::find(children_.begin(), children_.end(), child); 207 std::find(children_.begin(), children_.end(), child);
189 DCHECK(i != children_.end()); 208 DCHECK(i != children_.end());
190 children_.erase(i); 209 children_.erase(i);
191 child->parent_ = NULL; 210 child->parent_ = NULL;
192 child->cc_layer_->RemoveFromParent(); 211 child->cc_layer_->RemoveFromParent();
193 } 212 }
194 213
195 void Layer::StackAtTop(Layer* child) { 214 void Layer::StackAtTop(Layer* child) {
196 if (children_.size() <= 1 || child == children_.back()) 215 if (children_.size() <= 1 || child == children_.back())
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 layer_mask->children().empty() && 349 layer_mask->children().empty() &&
331 !layer_mask->layer_mask_back_link_)); 350 !layer_mask->layer_mask_back_link_));
332 DCHECK(!layer_mask_back_link_); 351 DCHECK(!layer_mask_back_link_);
333 if (layer_mask_ == layer_mask) 352 if (layer_mask_ == layer_mask)
334 return; 353 return;
335 // We need to de-reference the currently linked object so that no problem 354 // We need to de-reference the currently linked object so that no problem
336 // arises if the mask layer gets deleted before this object. 355 // arises if the mask layer gets deleted before this object.
337 if (layer_mask_) 356 if (layer_mask_)
338 layer_mask_->layer_mask_back_link_ = NULL; 357 layer_mask_->layer_mask_back_link_ = NULL;
339 layer_mask_ = layer_mask; 358 layer_mask_ = layer_mask;
340 cc_layer_->SetMaskLayer( 359 cc_layer_->SetMaskLayer(layer_mask ? layer_mask->cc_layer_ : NULL);
341 layer_mask ? layer_mask->cc_layer() : NULL);
342 // We need to reference the linked object so that it can properly break the 360 // We need to reference the linked object so that it can properly break the
343 // link to us when it gets deleted. 361 // link to us when it gets deleted.
344 if (layer_mask) { 362 if (layer_mask) {
345 layer_mask->layer_mask_back_link_ = this; 363 layer_mask->layer_mask_back_link_ = this;
346 layer_mask->OnDeviceScaleFactorChanged(device_scale_factor_); 364 layer_mask->OnDeviceScaleFactorChanged(device_scale_factor_);
347 } 365 }
348 } 366 }
349 367
350 void Layer::SetBackgroundZoom(float zoom, int inset) { 368 void Layer::SetBackgroundZoom(float zoom, int inset) {
351 zoom_ = zoom; 369 zoom_ = zoom;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 501
484 if (texture_layer_.get()) 502 if (texture_layer_.get())
485 texture_layer_->ClearClient(); 503 texture_layer_->ClearClient();
486 // TODO(piman): delegated_renderer_layer_ cleanup. 504 // TODO(piman): delegated_renderer_layer_ cleanup.
487 505
488 cc_layer_->RemoveAllChildren(); 506 cc_layer_->RemoveAllChildren();
489 if (cc_layer_->parent()) { 507 if (cc_layer_->parent()) {
490 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer); 508 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer);
491 } 509 }
492 cc_layer_->SetLayerClient(NULL); 510 cc_layer_->SetLayerClient(NULL);
493 cc_layer_->RemoveLayerAnimationEventObserver(this); 511
512 DetachAnimationObservers();
513
494 new_layer->SetOpacity(cc_layer_->opacity()); 514 new_layer->SetOpacity(cc_layer_->opacity());
495 new_layer->SetTransform(cc_layer_->transform()); 515 new_layer->SetTransform(cc_layer_->transform());
496 new_layer->SetPosition(cc_layer_->position()); 516 new_layer->SetPosition(cc_layer_->position());
497 new_layer->SetBackgroundColor(cc_layer_->background_color()); 517 new_layer->SetBackgroundColor(cc_layer_->background_color());
498 518
499 cc_layer_ = new_layer.get(); 519 cc_layer_ = new_layer.get();
500 content_layer_ = NULL; 520 content_layer_ = NULL;
501 solid_color_layer_ = NULL; 521 solid_color_layer_ = NULL;
502 texture_layer_ = NULL; 522 texture_layer_ = NULL;
503 delegated_renderer_layer_ = NULL; 523 delegated_renderer_layer_ = NULL;
504 surface_layer_ = NULL; 524 surface_layer_ = NULL;
505 525
506 cc_layer_->AddLayerAnimationEventObserver(this); 526 AttachAnimationObservers();
527
507 for (size_t i = 0; i < children_.size(); ++i) { 528 for (size_t i = 0; i < children_.size(); ++i) {
508 DCHECK(children_[i]->cc_layer_); 529 DCHECK(children_[i]->cc_layer_);
509 cc_layer_->AddChild(children_[i]->cc_layer_); 530 cc_layer_->AddChild(children_[i]->cc_layer_);
510 } 531 }
511 cc_layer_->SetLayerClient(this); 532 cc_layer_->SetLayerClient(this);
512 cc_layer_->SetTransformOrigin(gfx::Point3F()); 533 cc_layer_->SetTransformOrigin(gfx::Point3F());
513 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_); 534 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_);
514 cc_layer_->SetForceRenderSurface(force_render_surface_); 535 cc_layer_->SetForceRenderSurface(force_render_surface_);
515 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 536 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
516 cc_layer_->SetHideLayerAndSubtree(!visible_); 537 cc_layer_->SetHideLayerAndSubtree(!visible_);
(...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after
1034 } else { 1055 } else {
1035 if (Layer::UsingPictureLayer()) 1056 if (Layer::UsingPictureLayer())
1036 content_layer_ = cc::PictureLayer::Create(this); 1057 content_layer_ = cc::PictureLayer::Create(this);
1037 else 1058 else
1038 content_layer_ = cc::ContentLayer::Create(this); 1059 content_layer_ = cc::ContentLayer::Create(this);
1039 cc_layer_ = content_layer_.get(); 1060 cc_layer_ = content_layer_.get();
1040 } 1061 }
1041 cc_layer_->SetTransformOrigin(gfx::Point3F()); 1062 cc_layer_->SetTransformOrigin(gfx::Point3F());
1042 cc_layer_->SetContentsOpaque(true); 1063 cc_layer_->SetContentsOpaque(true);
1043 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 1064 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
1044 cc_layer_->AddLayerAnimationEventObserver(this);
1045 cc_layer_->SetLayerClient(this); 1065 cc_layer_->SetLayerClient(this);
1046 RecomputePosition(); 1066 RecomputePosition();
1047 } 1067 }
1048 1068
1069 void Layer::AttachAnimationObservers() {
1070 if (cc_layer_->layer_animation_controller())
1071 cc_layer_->AddLayerAnimationEventObserver(this);
1072
1073 for (size_t i = 0; i < children_.size(); ++i)
1074 children_[i]->AttachAnimationObservers();
1075 }
1076
1077 void Layer::DetachAnimationObservers() {
1078 if (cc_layer_->layer_animation_controller())
1079 cc_layer_->RemoveLayerAnimationEventObserver(this);
1080
1081 for (size_t i = 0; i < children_.size(); ++i)
1082 children_[i]->DetachAnimationObservers();
1083 }
1084
1049 gfx::Transform Layer::transform() const { 1085 gfx::Transform Layer::transform() const {
1050 return cc_layer_->transform(); 1086 return cc_layer_->transform();
1051 } 1087 }
1052 1088
1053 void Layer::RecomputeDrawsContentAndUVRect() { 1089 void Layer::RecomputeDrawsContentAndUVRect() {
1054 DCHECK(cc_layer_); 1090 DCHECK(cc_layer_);
1055 gfx::Size size(bounds_.size()); 1091 gfx::Size size(bounds_.size());
1056 if (texture_layer_.get()) { 1092 if (texture_layer_.get()) {
1057 size.SetToMin(frame_size_in_dip_); 1093 size.SetToMin(frame_size_in_dip_);
1058 gfx::PointF uv_top_left(0.f, 0.f); 1094 gfx::PointF uv_top_left(0.f, 0.f);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 children_.end(), 1128 children_.end(),
1093 std::bind2nd(std::mem_fun(&Layer::RemoveAnimatorsInTreeFromCollection), 1129 std::bind2nd(std::mem_fun(&Layer::RemoveAnimatorsInTreeFromCollection),
1094 collection)); 1130 collection));
1095 } 1131 }
1096 1132
1097 bool Layer::IsAnimating() const { 1133 bool Layer::IsAnimating() const {
1098 return animator_.get() && animator_->is_animating(); 1134 return animator_.get() && animator_->is_animating();
1099 } 1135 }
1100 1136
1101 } // namespace ui 1137 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698