blob: ce071f2ba5e5f920025e8f6abb62872b2a9180e1 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// Copyright 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]556fd292013-03-18 08:03:045#include "cc/trees/tree_synchronizer.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]0bf947242012-12-04 04:25:117#include "base/debug/trace_event.h"
[email protected]5c4824e12013-01-12 16:34:538#include "base/logging.h"
[email protected]95e4e1a02013-03-18 07:09:099#include "cc/animation/scrollbar_animation_controller.h"
[email protected]a8461d82012-10-16 21:11:1410#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:3111#include "cc/layer_impl.h"
[email protected]a8461d82012-10-16 21:11:1412#include "cc/scrollbar_layer.h"
[email protected]c4040a522012-10-21 15:01:4013#include "cc/scrollbar_layer_impl.h"
[email protected]94f206c12012-08-25 00:09:1414
[email protected]9c88e562012-09-14 22:21:3015namespace cc {
[email protected]94f206c12012-08-25 00:09:1416
[email protected]48871fc2013-01-23 07:36:5117typedef ScopedPtrHashMap<int, LayerImpl> ScopedPtrLayerImplMap;
18typedef base::hash_map<int, LayerImpl*> RawPtrLayerImplMap;
[email protected]c2282382012-12-16 00:46:0319
[email protected]b5651c22013-03-14 15:06:3320void CollectExistingLayerImplRecursive(ScopedPtrLayerImplMap* old_layers,
21 scoped_ptr<LayerImpl> layer_impl) {
22 if (!layer_impl)
23 return;
[email protected]94f206c12012-08-25 00:09:1424
[email protected]b5651c22013-03-14 15:06:3325 ScopedPtrVector<LayerImpl>& children = layer_impl->children();
26 for (ScopedPtrVector<LayerImpl>::iterator it = children.begin();
27 it != children.end();
28 ++it)
29 CollectExistingLayerImplRecursive(old_layers, children.take(it));
[email protected]94f206c12012-08-25 00:09:1430
[email protected]b5651c22013-03-14 15:06:3331 CollectExistingLayerImplRecursive(old_layers, layer_impl->TakeMaskLayer());
32 CollectExistingLayerImplRecursive(old_layers, layer_impl->TakeReplicaLayer());
[email protected]94f206c12012-08-25 00:09:1433
[email protected]b5651c22013-03-14 15:06:3334 int id = layer_impl->id();
35 old_layers->set(id, layer_impl.Pass());
[email protected]94f206c12012-08-25 00:09:1436}
37
[email protected]48871fc2013-01-23 07:36:5138template <typename LayerType>
[email protected]b5651c22013-03-14 15:06:3339scoped_ptr<LayerImpl> SynchronizeTreesInternal(
40 LayerType* layer_root,
41 scoped_ptr<LayerImpl> old_layer_impl_root,
42 LayerTreeImpl* tree_impl) {
43 DCHECK(tree_impl);
[email protected]48871fc2013-01-23 07:36:5144
[email protected]b5651c22013-03-14 15:06:3345 TRACE_EVENT0("cc", "TreeSynchronizer::SynchronizeTrees");
46 ScopedPtrLayerImplMap old_layers;
47 RawPtrLayerImplMap new_layers;
[email protected]48871fc2013-01-23 07:36:5148
[email protected]b5651c22013-03-14 15:06:3349 CollectExistingLayerImplRecursive(&old_layers, old_layer_impl_root.Pass());
[email protected]48871fc2013-01-23 07:36:5150
[email protected]b5651c22013-03-14 15:06:3351 scoped_ptr<LayerImpl> new_tree = SynchronizeTreesRecursive(
52 &new_layers, &old_layers, layer_root, tree_impl);
[email protected]48871fc2013-01-23 07:36:5153
[email protected]b5651c22013-03-14 15:06:3354 UpdateScrollbarLayerPointersRecursive(&new_layers, layer_root);
[email protected]48871fc2013-01-23 07:36:5155
[email protected]b5651c22013-03-14 15:06:3356 return new_tree.Pass();
[email protected]48871fc2013-01-23 07:36:5157}
58
[email protected]b5651c22013-03-14 15:06:3359scoped_ptr<LayerImpl> TreeSynchronizer::SynchronizeTrees(
60 Layer* layer_root,
61 scoped_ptr<LayerImpl> old_layer_impl_root,
62 LayerTreeImpl* tree_impl) {
63 return SynchronizeTreesInternal(
64 layer_root, old_layer_impl_root.Pass(), tree_impl);
[email protected]48871fc2013-01-23 07:36:5165}
66
[email protected]b5651c22013-03-14 15:06:3367scoped_ptr<LayerImpl> TreeSynchronizer::SynchronizeTrees(
68 LayerImpl* layer_root,
69 scoped_ptr<LayerImpl> old_layer_impl_root,
70 LayerTreeImpl* tree_impl) {
71 return SynchronizeTreesInternal(
72 layer_root, old_layer_impl_root.Pass(), tree_impl);
[email protected]48871fc2013-01-23 07:36:5173}
74
75template <typename LayerType>
[email protected]b5651c22013-03-14 15:06:3376scoped_ptr<LayerImpl> ReuseOrCreateLayerImpl(RawPtrLayerImplMap* new_layers,
77 ScopedPtrLayerImplMap* old_layers,
78 LayerType* layer,
79 LayerTreeImpl* tree_impl) {
80 scoped_ptr<LayerImpl> layer_impl = old_layers->take(layer->id());
[email protected]94f206c12012-08-25 00:09:1481
[email protected]b5651c22013-03-14 15:06:3382 if (!layer_impl)
83 layer_impl = layer->CreateLayerImpl(tree_impl);
[email protected]94f206c12012-08-25 00:09:1484
[email protected]b5651c22013-03-14 15:06:3385 (*new_layers)[layer->id()] = layer_impl.get();
86 return layer_impl.Pass();
[email protected]94f206c12012-08-25 00:09:1487}
88
[email protected]48871fc2013-01-23 07:36:5189template <typename LayerType>
[email protected]b5651c22013-03-14 15:06:3390scoped_ptr<LayerImpl> SynchronizeTreesRecursiveInternal(
91 RawPtrLayerImplMap* new_layers,
92 ScopedPtrLayerImplMap* old_layers,
93 LayerType* layer,
94 LayerTreeImpl* tree_impl) {
95 if (!layer)
96 return scoped_ptr<LayerImpl>();
[email protected]94f206c12012-08-25 00:09:1497
[email protected]b5651c22013-03-14 15:06:3398 scoped_ptr<LayerImpl> layer_impl =
99 ReuseOrCreateLayerImpl(new_layers, old_layers, layer, tree_impl);
[email protected]94f206c12012-08-25 00:09:14100
[email protected]b5651c22013-03-14 15:06:33101 layer_impl->ClearChildList();
102 for (size_t i = 0; i < layer->children().size(); ++i) {
103 layer_impl->AddChild(SynchronizeTreesRecursiveInternal(
104 new_layers, old_layers, layer->child_at(i), tree_impl));
105 }
[email protected]94f206c12012-08-25 00:09:14106
[email protected]b5651c22013-03-14 15:06:33107 layer_impl->SetMaskLayer(SynchronizeTreesRecursiveInternal(
108 new_layers, old_layers, layer->mask_layer(), tree_impl));
109 layer_impl->SetReplicaLayer(SynchronizeTreesRecursiveInternal(
110 new_layers, old_layers, layer->replica_layer(), tree_impl));
[email protected]94f206c12012-08-25 00:09:14111
[email protected]b5651c22013-03-14 15:06:33112 // Remove all dangling pointers. The pointers will be setup later in
113 // UpdateScrollbarLayerPointersRecursive phase
114 layer_impl->SetHorizontalScrollbarLayer(NULL);
115 layer_impl->SetVerticalScrollbarLayer(NULL);
[email protected]94f206c12012-08-25 00:09:14116
[email protected]b5651c22013-03-14 15:06:33117 return layer_impl.Pass();
[email protected]94f206c12012-08-25 00:09:14118}
119
[email protected]b5651c22013-03-14 15:06:33120scoped_ptr<LayerImpl> SynchronizeTreesRecursive(
121 RawPtrLayerImplMap* new_layers,
122 ScopedPtrLayerImplMap* old_layers,
123 Layer* layer,
124 LayerTreeImpl* tree_impl) {
125 return SynchronizeTreesRecursiveInternal(
126 new_layers, old_layers, layer, tree_impl);
[email protected]48871fc2013-01-23 07:36:51127}
128
[email protected]b5651c22013-03-14 15:06:33129scoped_ptr<LayerImpl> SynchronizeTreesRecursive(
130 RawPtrLayerImplMap* new_layers,
131 ScopedPtrLayerImplMap* old_layers,
132 LayerImpl* layer,
133 LayerTreeImpl* tree_impl) {
134 return SynchronizeTreesRecursiveInternal(
135 new_layers, old_layers, layer, tree_impl);
[email protected]48871fc2013-01-23 07:36:51136}
137
138template <typename LayerType, typename ScrollbarLayerType>
[email protected]b5651c22013-03-14 15:06:33139void UpdateScrollbarLayerPointersRecursiveInternal(
140 const RawPtrLayerImplMap* new_layers,
141 LayerType* layer) {
142 if (!layer)
143 return;
[email protected]94f206c12012-08-25 00:09:14144
[email protected]b5651c22013-03-14 15:06:33145 for (size_t i = 0; i < layer->children().size(); ++i) {
146 UpdateScrollbarLayerPointersRecursiveInternal<
147 LayerType, ScrollbarLayerType>(new_layers, layer->child_at(i));
148 }
[email protected]94f206c12012-08-25 00:09:14149
[email protected]b5651c22013-03-14 15:06:33150 ScrollbarLayerType* scrollbar_layer = layer->ToScrollbarLayer();
[email protected]b7c4783f2013-03-15 23:11:42151 // Pinch-zoom scrollbars will have an invalid scrollLayerId, but they are
152 // managed by LayerTreeImpl and not LayerImpl, so should not be
153 // processed here.
154 if (!scrollbar_layer || (scrollbar_layer->scroll_layer_id() ==
155 Layer::PINCH_ZOOM_ROOT_SCROLL_LAYER_ID))
[email protected]b5651c22013-03-14 15:06:33156 return;
[email protected]94f206c12012-08-25 00:09:14157
[email protected]b5651c22013-03-14 15:06:33158 RawPtrLayerImplMap::const_iterator iter =
159 new_layers->find(scrollbar_layer->id());
160 ScrollbarLayerImpl* scrollbar_layer_impl =
161 iter != new_layers->end() ? static_cast<ScrollbarLayerImpl*>(iter->second)
162 : NULL;
163 iter = new_layers->find(scrollbar_layer->scroll_layer_id());
164 LayerImpl* scroll_layer_impl =
165 iter != new_layers->end() ? iter->second : NULL;
[email protected]e0bd43a2012-10-12 16:54:21166
[email protected]b5651c22013-03-14 15:06:33167 DCHECK(scrollbar_layer_impl);
168 DCHECK(scroll_layer_impl);
[email protected]94f206c12012-08-25 00:09:14169
[email protected]b5651c22013-03-14 15:06:33170 if (scrollbar_layer->Orientation() == WebKit::WebScrollbar::Horizontal)
171 scroll_layer_impl->SetHorizontalScrollbarLayer(scrollbar_layer_impl);
172 else
173 scroll_layer_impl->SetVerticalScrollbarLayer(scrollbar_layer_impl);
[email protected]94f206c12012-08-25 00:09:14174}
175
[email protected]b5651c22013-03-14 15:06:33176void UpdateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap* new_layers,
177 Layer* layer) {
178 UpdateScrollbarLayerPointersRecursiveInternal<Layer, ScrollbarLayer>(
179 new_layers, layer);
[email protected]48871fc2013-01-23 07:36:51180}
181
[email protected]b5651c22013-03-14 15:06:33182void UpdateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap* new_layers,
183 LayerImpl* layer) {
184 UpdateScrollbarLayerPointersRecursiveInternal<LayerImpl, ScrollbarLayerImpl>(
185 new_layers, layer);
[email protected]48871fc2013-01-23 07:36:51186}
187
188template <typename LayerType>
[email protected]b5651c22013-03-14 15:06:33189void PushPropertiesInternal(LayerType* layer, LayerImpl* layer_impl) {
190 if (!layer) {
191 DCHECK(!layer_impl);
192 return;
193 }
[email protected]5c4824e12013-01-12 16:34:53194
[email protected]b5651c22013-03-14 15:06:33195 DCHECK_EQ(layer->id(), layer_impl->id());
196 layer->PushPropertiesTo(layer_impl);
[email protected]5c4824e12013-01-12 16:34:53197
[email protected]b5651c22013-03-14 15:06:33198 PushPropertiesInternal(layer->mask_layer(), layer_impl->mask_layer());
199 PushPropertiesInternal(layer->replica_layer(), layer_impl->replica_layer());
[email protected]5c4824e12013-01-12 16:34:53200
[email protected]b5651c22013-03-14 15:06:33201 const ScopedPtrVector<LayerImpl>& impl_children = layer_impl->children();
202 DCHECK_EQ(layer->children().size(), impl_children.size());
[email protected]5c4824e12013-01-12 16:34:53203
[email protected]b5651c22013-03-14 15:06:33204 for (size_t i = 0; i < layer->children().size(); ++i) {
205 PushPropertiesInternal(layer->child_at(i), impl_children[i]);
206 }
[email protected]5c4824e12013-01-12 16:34:53207}
208
[email protected]b5651c22013-03-14 15:06:33209void TreeSynchronizer::PushProperties(Layer* layer, LayerImpl* layer_impl) {
210 PushPropertiesInternal(layer, layer_impl);
[email protected]48871fc2013-01-23 07:36:51211}
212
[email protected]b5651c22013-03-14 15:06:33213void TreeSynchronizer::PushProperties(LayerImpl* layer, LayerImpl* layer_impl) {
214 PushPropertiesInternal(layer, layer_impl);
[email protected]48871fc2013-01-23 07:36:51215}
216
[email protected]bc5e77c2012-11-05 20:00:49217} // namespace cc