blob: 8c3a3700a8cdc31bb350fbfe69d1e1564709c356 [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]a8461d82012-10-16 21:11:145#include "cc/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]a8461d82012-10-16 21:11:149#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:3110#include "cc/layer_impl.h"
[email protected]c4040a522012-10-21 15:01:4011#include "cc/scrollbar_animation_controller.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]48871fc2013-01-23 07:36:5120void collectExistingLayerImplRecursive(ScopedPtrLayerImplMap& oldLayers, scoped_ptr<LayerImpl> layerImpl)
[email protected]94f206c12012-08-25 00:09:1421{
[email protected]96baf3e2012-10-22 23:09:5522 if (!layerImpl)
[email protected]94f206c12012-08-25 00:09:1423 return;
24
[email protected]48871fc2013-01-23 07:36:5125 ScopedPtrVector<LayerImpl>& children = layerImpl->children();
[email protected]ead39c52013-01-09 07:22:4526 for (ScopedPtrVector<LayerImpl>::iterator it = children.begin(); it != children.end(); ++it)
27 collectExistingLayerImplRecursive(oldLayers, children.take(it));
[email protected]94f206c12012-08-25 00:09:1428
[email protected]361bc00d2012-12-14 07:03:2429 collectExistingLayerImplRecursive(oldLayers, layerImpl->takeMaskLayer());
30 collectExistingLayerImplRecursive(oldLayers, layerImpl->takeReplicaLayer());
[email protected]94f206c12012-08-25 00:09:1431
[email protected]96baf3e2012-10-22 23:09:5532 int id = layerImpl->id();
33 oldLayers.set(id, layerImpl.Pass());
[email protected]94f206c12012-08-25 00:09:1434}
35
[email protected]48871fc2013-01-23 07:36:5136template <typename LayerType>
37scoped_ptr<LayerImpl> synchronizeTreesInternal(LayerType* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl)
38{
39 DCHECK(treeImpl);
40
41 TRACE_EVENT0("cc", "TreeSynchronizer::synchronizeTrees");
42 ScopedPtrLayerImplMap oldLayers;
43 RawPtrLayerImplMap newLayers;
44
45 collectExistingLayerImplRecursive(oldLayers, oldLayerImplRoot.Pass());
46
47 scoped_ptr<LayerImpl> newTree = synchronizeTreesRecursive(newLayers, oldLayers, layerRoot, treeImpl);
48
49 updateScrollbarLayerPointersRecursive(newLayers, layerRoot);
50
51 return newTree.Pass();
52}
53
54scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(Layer* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl)
55{
56 return synchronizeTreesInternal(layerRoot, oldLayerImplRoot.Pass(), treeImpl);
57}
58
59scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(LayerImpl* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl)
60{
61 return synchronizeTreesInternal(layerRoot, oldLayerImplRoot.Pass(), treeImpl);
62}
63
64template <typename LayerType>
65scoped_ptr<LayerImpl> reuseOrCreateLayerImpl(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerType* layer, LayerTreeImpl* treeImpl)
[email protected]94f206c12012-08-25 00:09:1466{
[email protected]96baf3e2012-10-22 23:09:5567 scoped_ptr<LayerImpl> layerImpl = oldLayers.take(layer->id());
[email protected]94f206c12012-08-25 00:09:1468
[email protected]96baf3e2012-10-22 23:09:5569 if (!layerImpl)
[email protected]8bef40572012-12-11 21:38:0870 layerImpl = layer->createLayerImpl(treeImpl);
[email protected]94f206c12012-08-25 00:09:1471
[email protected]96baf3e2012-10-22 23:09:5572 newLayers[layer->id()] = layerImpl.get();
73 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1474}
75
[email protected]48871fc2013-01-23 07:36:5176template <typename LayerType>
77scoped_ptr<LayerImpl> synchronizeTreesRecursiveInternal(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerType* layer, LayerTreeImpl* treeImpl)
[email protected]94f206c12012-08-25 00:09:1478{
79 if (!layer)
[email protected]96baf3e2012-10-22 23:09:5580 return scoped_ptr<LayerImpl>();
[email protected]94f206c12012-08-25 00:09:1481
[email protected]8bef40572012-12-11 21:38:0882 scoped_ptr<LayerImpl> layerImpl = reuseOrCreateLayerImpl(newLayers, oldLayers, layer, treeImpl);
[email protected]94f206c12012-08-25 00:09:1483
[email protected]96baf3e2012-10-22 23:09:5584 layerImpl->clearChildList();
[email protected]48871fc2013-01-23 07:36:5185 for (size_t i = 0; i < layer->children().size(); ++i)
86 layerImpl->addChild(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->childAt(i), treeImpl));
[email protected]94f206c12012-08-25 00:09:1487
[email protected]48871fc2013-01-23 07:36:5188 layerImpl->setMaskLayer(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->maskLayer(), treeImpl));
89 layerImpl->setReplicaLayer(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->replicaLayer(), treeImpl));
[email protected]94f206c12012-08-25 00:09:1490
[email protected]94f206c12012-08-25 00:09:1491 // Remove all dangling pointers. The pointers will be setup later in updateScrollbarLayerPointersRecursive phase
[email protected]e45638c2013-01-17 22:01:4092 layerImpl->setHorizontalScrollbarLayer(0);
93 layerImpl->setVerticalScrollbarLayer(0);
[email protected]94f206c12012-08-25 00:09:1494
[email protected]96baf3e2012-10-22 23:09:5595 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1496}
97
[email protected]48871fc2013-01-23 07:36:5198scoped_ptr<LayerImpl> synchronizeTreesRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl)
99{
100 return synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer, treeImpl);
101}
102
103scoped_ptr<LayerImpl> synchronizeTreesRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerImpl* layer, LayerTreeImpl* treeImpl)
104{
105 return synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer, treeImpl);
106}
107
108template <typename LayerType, typename ScrollbarLayerType>
109void updateScrollbarLayerPointersRecursiveInternal(const RawPtrLayerImplMap& newLayers, LayerType* layer)
[email protected]94f206c12012-08-25 00:09:14110{
111 if (!layer)
112 return;
113
[email protected]48871fc2013-01-23 07:36:51114 for (size_t i = 0; i < layer->children().size(); ++i)
115 updateScrollbarLayerPointersRecursiveInternal<LayerType, ScrollbarLayerType>(newLayers, layer->childAt(i));
[email protected]94f206c12012-08-25 00:09:14116
[email protected]48871fc2013-01-23 07:36:51117 ScrollbarLayerType* scrollbarLayer = layer->toScrollbarLayer();
[email protected]94f206c12012-08-25 00:09:14118 if (!scrollbarLayer)
119 return;
120
[email protected]96baf3e2012-10-22 23:09:55121 RawPtrLayerImplMap::const_iterator iter = newLayers.find(scrollbarLayer->id());
122 ScrollbarLayerImpl* scrollbarLayerImpl = iter != newLayers.end() ? static_cast<ScrollbarLayerImpl*>(iter->second) : NULL;
[email protected]e0bd43a2012-10-12 16:54:21123 iter = newLayers.find(scrollbarLayer->scrollLayerId());
[email protected]96baf3e2012-10-22 23:09:55124 LayerImpl* scrollLayerImpl = iter != newLayers.end() ? iter->second : NULL;
[email protected]e0bd43a2012-10-12 16:54:21125
[email protected]96baf3e2012-10-22 23:09:55126 DCHECK(scrollbarLayerImpl);
127 DCHECK(scrollLayerImpl);
[email protected]94f206c12012-08-25 00:09:14128
[email protected]e45638c2013-01-17 22:01:40129 if (scrollbarLayer->orientation() == WebKit::WebScrollbar::Horizontal)
[email protected]96baf3e2012-10-22 23:09:55130 scrollLayerImpl->setHorizontalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14131 else
[email protected]96baf3e2012-10-22 23:09:55132 scrollLayerImpl->setVerticalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14133}
134
[email protected]48871fc2013-01-23 07:36:51135void updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, Layer* layer)
136{
137 updateScrollbarLayerPointersRecursiveInternal<Layer, ScrollbarLayer>(newLayers, layer);
138}
139
140void updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, LayerImpl* layer)
141{
142 updateScrollbarLayerPointersRecursiveInternal<LayerImpl, ScrollbarLayerImpl>(newLayers, layer);
143}
144
145template <typename LayerType>
146void pushPropertiesInternal(LayerType* layer, LayerImpl* layerImpl)
[email protected]5c4824e12013-01-12 16:34:53147{
148 if (!layer) {
149 DCHECK(!layerImpl);
150 return;
151 }
152
153 DCHECK_EQ(layer->id(), layerImpl->id());
154 layer->pushPropertiesTo(layerImpl);
155
[email protected]48871fc2013-01-23 07:36:51156 pushPropertiesInternal(layer->maskLayer(), layerImpl->maskLayer());
157 pushPropertiesInternal(layer->replicaLayer(), layerImpl->replicaLayer());
[email protected]5c4824e12013-01-12 16:34:53158
[email protected]5c4824e12013-01-12 16:34:53159 const ScopedPtrVector<LayerImpl>& implChildren = layerImpl->children();
[email protected]48871fc2013-01-23 07:36:51160 DCHECK_EQ(layer->children().size(), implChildren.size());
[email protected]5c4824e12013-01-12 16:34:53161
[email protected]48871fc2013-01-23 07:36:51162 for (size_t i = 0; i < layer->children().size(); ++i) {
163 pushPropertiesInternal(layer->childAt(i), implChildren[i]);
[email protected]5c4824e12013-01-12 16:34:53164 }
165}
166
[email protected]48871fc2013-01-23 07:36:51167void TreeSynchronizer::pushProperties(Layer* layer, LayerImpl* layerImpl)
168{
169 pushPropertiesInternal(layer, layerImpl);
170}
171
172void TreeSynchronizer::pushProperties(LayerImpl* layer, LayerImpl* layerImpl)
173{
174 pushPropertiesInternal(layer, layerImpl);
175}
176
177
[email protected]bc5e77c2012-11-05 20:00:49178} // namespace cc