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

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: Rebase. Fix code review issues. 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
« no previous file with comments | « ui/compositor/layer.h ('k') | ui/compositor/layer_unittest.cc » ('j') | 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/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 ui
146 // or to reset it. 149 // layer.
147 DCHECK(!compositor || !compositor_); 150 DCHECK(compositor);
148 DCHECK(!compositor || compositor->root_layer() == this); 151 DCHECK(!compositor_);
152 DCHECK(compositor->root_layer() == this);
153 DCHECK(!parent_);
154
155 compositor_ = compositor;
156 OnDeviceScaleFactorChanged(compositor->device_scale_factor());
157 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection());
158 }
159
160 void Layer::SetCompositorRootCCLayer(scoped_refptr<cc::Layer> root_layer) {
161 // This function must only be called on the root ui layer.
162 root_layer->AddChild(cc_layer_);
163 AttachAnimationObservers();
164 SendPendingThreadedAnimations();
165 }
166
167 void Layer::ResetCompositor() {
149 DCHECK(!parent_); 168 DCHECK(!parent_);
150 if (compositor_) { 169 if (compositor_) {
170 DetachAnimationObservers();
151 RemoveAnimatorsInTreeFromCollection( 171 RemoveAnimatorsInTreeFromCollection(
152 compositor_->layer_animator_collection()); 172 compositor_->layer_animator_collection());
153 } 173 }
154 compositor_ = compositor; 174 compositor_ = nullptr;
155 if (compositor) {
156 OnDeviceScaleFactorChanged(compositor->device_scale_factor());
157 SendPendingThreadedAnimations();
158 AddAnimatorsInTreeToCollection(compositor_->layer_animator_collection());
159 }
160 } 175 }
161 176
162 void Layer::Add(Layer* child) { 177 void Layer::Add(Layer* child) {
163 DCHECK(!child->compositor_); 178 DCHECK(!child->compositor_);
164 if (child->parent_) 179 if (child->parent_)
165 child->parent_->Remove(child); 180 child->parent_->Remove(child);
166 child->parent_ = this; 181 child->parent_ = this;
167 children_.push_back(child); 182 children_.push_back(child);
168 cc_layer_->AddChild(child->cc_layer_); 183 cc_layer_->AddChild(child->cc_layer_);
169 child->OnDeviceScaleFactorChanged(device_scale_factor_); 184 child->OnDeviceScaleFactorChanged(device_scale_factor_);
170 if (GetCompositor()) 185 Compositor* compositor = GetCompositor();
186 if (compositor) {
187 child->AttachAnimationObservers();
171 child->SendPendingThreadedAnimations(); 188 child->SendPendingThreadedAnimations();
189 }
172 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 190 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
173 if (collection) 191 if (collection)
174 child->AddAnimatorsInTreeToCollection(collection); 192 child->AddAnimatorsInTreeToCollection(collection);
175 } 193 }
176 194
177 void Layer::Remove(Layer* child) { 195 void Layer::Remove(Layer* child) {
178 // Current bounds are used to calculate offsets when layers are reparented. 196 // Current bounds are used to calculate offsets when layers are reparented.
179 // Stop (and complete) an ongoing animation to update the bounds immediately. 197 // Stop (and complete) an ongoing animation to update the bounds immediately.
180 LayerAnimator* child_animator = child->animator_.get(); 198 LayerAnimator* child_animator = child->animator_.get();
181 if (child_animator) 199 if (child_animator)
182 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS); 200 child_animator->StopAnimatingProperty(ui::LayerAnimationElement::BOUNDS);
183 LayerAnimatorCollection* collection = GetLayerAnimatorCollection(); 201 LayerAnimatorCollection* collection = GetLayerAnimatorCollection();
184 if (collection) 202 if (collection)
185 child->RemoveAnimatorsInTreeFromCollection(collection); 203 child->RemoveAnimatorsInTreeFromCollection(collection);
186 204
205 child->DetachAnimationObservers();
206
187 std::vector<Layer*>::iterator i = 207 std::vector<Layer*>::iterator i =
188 std::find(children_.begin(), children_.end(), child); 208 std::find(children_.begin(), children_.end(), child);
189 DCHECK(i != children_.end()); 209 DCHECK(i != children_.end());
190 children_.erase(i); 210 children_.erase(i);
191 child->parent_ = NULL; 211 child->parent_ = NULL;
192 child->cc_layer_->RemoveFromParent(); 212 child->cc_layer_->RemoveFromParent();
193 } 213 }
194 214
195 void Layer::StackAtTop(Layer* child) { 215 void Layer::StackAtTop(Layer* child) {
196 if (children_.size() <= 1 || child == children_.back()) 216 if (children_.size() <= 1 || child == children_.back())
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 layer_mask->children().empty() && 350 layer_mask->children().empty() &&
331 !layer_mask->layer_mask_back_link_)); 351 !layer_mask->layer_mask_back_link_));
332 DCHECK(!layer_mask_back_link_); 352 DCHECK(!layer_mask_back_link_);
333 if (layer_mask_ == layer_mask) 353 if (layer_mask_ == layer_mask)
334 return; 354 return;
335 // We need to de-reference the currently linked object so that no problem 355 // We need to de-reference the currently linked object so that no problem
336 // arises if the mask layer gets deleted before this object. 356 // arises if the mask layer gets deleted before this object.
337 if (layer_mask_) 357 if (layer_mask_)
338 layer_mask_->layer_mask_back_link_ = NULL; 358 layer_mask_->layer_mask_back_link_ = NULL;
339 layer_mask_ = layer_mask; 359 layer_mask_ = layer_mask;
340 cc_layer_->SetMaskLayer( 360 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 361 // We need to reference the linked object so that it can properly break the
343 // link to us when it gets deleted. 362 // link to us when it gets deleted.
344 if (layer_mask) { 363 if (layer_mask) {
345 layer_mask->layer_mask_back_link_ = this; 364 layer_mask->layer_mask_back_link_ = this;
346 layer_mask->OnDeviceScaleFactorChanged(device_scale_factor_); 365 layer_mask->OnDeviceScaleFactorChanged(device_scale_factor_);
347 } 366 }
348 } 367 }
349 368
350 void Layer::SetBackgroundZoom(float zoom, int inset) { 369 void Layer::SetBackgroundZoom(float zoom, int inset) {
351 zoom_ = zoom; 370 zoom_ = zoom;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 502
484 if (texture_layer_.get()) 503 if (texture_layer_.get())
485 texture_layer_->ClearClient(); 504 texture_layer_->ClearClient();
486 // TODO(piman): delegated_renderer_layer_ cleanup. 505 // TODO(piman): delegated_renderer_layer_ cleanup.
487 506
488 cc_layer_->RemoveAllChildren(); 507 cc_layer_->RemoveAllChildren();
489 if (cc_layer_->parent()) { 508 if (cc_layer_->parent()) {
490 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer); 509 cc_layer_->parent()->ReplaceChild(cc_layer_, new_layer);
491 } 510 }
492 cc_layer_->SetLayerClient(NULL); 511 cc_layer_->SetLayerClient(NULL);
493 cc_layer_->RemoveLayerAnimationEventObserver(this); 512
513 DetachAnimationObservers();
514
494 new_layer->SetOpacity(cc_layer_->opacity()); 515 new_layer->SetOpacity(cc_layer_->opacity());
495 new_layer->SetTransform(cc_layer_->transform()); 516 new_layer->SetTransform(cc_layer_->transform());
496 new_layer->SetPosition(cc_layer_->position()); 517 new_layer->SetPosition(cc_layer_->position());
497 new_layer->SetBackgroundColor(cc_layer_->background_color()); 518 new_layer->SetBackgroundColor(cc_layer_->background_color());
498 519
499 cc_layer_ = new_layer.get(); 520 cc_layer_ = new_layer.get();
500 content_layer_ = NULL; 521 content_layer_ = NULL;
501 solid_color_layer_ = NULL; 522 solid_color_layer_ = NULL;
502 texture_layer_ = NULL; 523 texture_layer_ = NULL;
503 delegated_renderer_layer_ = NULL; 524 delegated_renderer_layer_ = NULL;
504 surface_layer_ = NULL; 525 surface_layer_ = NULL;
505 526
506 cc_layer_->AddLayerAnimationEventObserver(this); 527 AttachAnimationObservers();
528
507 for (size_t i = 0; i < children_.size(); ++i) { 529 for (size_t i = 0; i < children_.size(); ++i) {
508 DCHECK(children_[i]->cc_layer_); 530 DCHECK(children_[i]->cc_layer_);
509 cc_layer_->AddChild(children_[i]->cc_layer_); 531 cc_layer_->AddChild(children_[i]->cc_layer_);
510 } 532 }
511 cc_layer_->SetLayerClient(this); 533 cc_layer_->SetLayerClient(this);
512 cc_layer_->SetTransformOrigin(gfx::Point3F()); 534 cc_layer_->SetTransformOrigin(gfx::Point3F());
513 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_); 535 cc_layer_->SetContentsOpaque(fills_bounds_opaquely_);
514 cc_layer_->SetForceRenderSurface(force_render_surface_); 536 cc_layer_->SetForceRenderSurface(force_render_surface_);
515 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 537 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
516 cc_layer_->SetHideLayerAndSubtree(!visible_); 538 cc_layer_->SetHideLayerAndSubtree(!visible_);
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
1027 } else { 1049 } else {
1028 if (Layer::UsingPictureLayer()) 1050 if (Layer::UsingPictureLayer())
1029 content_layer_ = cc::PictureLayer::Create(this); 1051 content_layer_ = cc::PictureLayer::Create(this);
1030 else 1052 else
1031 content_layer_ = cc::ContentLayer::Create(this); 1053 content_layer_ = cc::ContentLayer::Create(this);
1032 cc_layer_ = content_layer_.get(); 1054 cc_layer_ = content_layer_.get();
1033 } 1055 }
1034 cc_layer_->SetTransformOrigin(gfx::Point3F()); 1056 cc_layer_->SetTransformOrigin(gfx::Point3F());
1035 cc_layer_->SetContentsOpaque(true); 1057 cc_layer_->SetContentsOpaque(true);
1036 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN); 1058 cc_layer_->SetIsDrawable(type_ != LAYER_NOT_DRAWN);
1037 cc_layer_->AddLayerAnimationEventObserver(this);
1038 cc_layer_->SetLayerClient(this); 1059 cc_layer_->SetLayerClient(this);
1039 RecomputePosition(); 1060 RecomputePosition();
1040 } 1061 }
1041 1062
1063 void Layer::AttachAnimationObservers() {
1064 if (cc_layer_->layer_animation_controller())
ajuma 2015/04/27 14:51:56 It's probably better to move this null check to cc
loyso (OOO) 2015/04/28 05:10:36 This is the only place in the project where cc::La
loyso (OOO) 2015/04/28 05:23:13 May we put a DCHECK here?
ajuma 2015/04/28 14:36:04 If a DCHECK works, that'd be great. If not, perhap
1065 cc_layer_->AddLayerAnimationEventObserver(this);
1066
1067 for (size_t i = 0; i < children_.size(); ++i)
1068 children_[i]->AttachAnimationObservers();
1069 }
1070
1071 void Layer::DetachAnimationObservers() {
1072 if (cc_layer_->layer_animation_controller())
1073 cc_layer_->RemoveLayerAnimationEventObserver(this);
1074
1075 for (size_t i = 0; i < children_.size(); ++i)
1076 children_[i]->DetachAnimationObservers();
1077 }
1078
1042 gfx::Transform Layer::transform() const { 1079 gfx::Transform Layer::transform() const {
1043 return cc_layer_->transform(); 1080 return cc_layer_->transform();
1044 } 1081 }
1045 1082
1046 void Layer::RecomputeDrawsContentAndUVRect() { 1083 void Layer::RecomputeDrawsContentAndUVRect() {
1047 DCHECK(cc_layer_); 1084 DCHECK(cc_layer_);
1048 gfx::Size size(bounds_.size()); 1085 gfx::Size size(bounds_.size());
1049 if (texture_layer_.get()) { 1086 if (texture_layer_.get()) {
1050 size.SetToMin(frame_size_in_dip_); 1087 size.SetToMin(frame_size_in_dip_);
1051 gfx::PointF uv_top_left(0.f, 0.f); 1088 gfx::PointF uv_top_left(0.f, 0.f);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 children_.end(), 1122 children_.end(),
1086 std::bind2nd(std::mem_fun(&Layer::RemoveAnimatorsInTreeFromCollection), 1123 std::bind2nd(std::mem_fun(&Layer::RemoveAnimatorsInTreeFromCollection),
1087 collection)); 1124 collection));
1088 } 1125 }
1089 1126
1090 bool Layer::IsAnimating() const { 1127 bool Layer::IsAnimating() const {
1091 return animator_.get() && animator_->is_animating(); 1128 return animator_.get() && animator_->is_animating();
1092 } 1129 }
1093 1130
1094 } // namespace ui 1131 } // namespace ui
OLDNEW
« no previous file with comments | « ui/compositor/layer.h ('k') | ui/compositor/layer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698