blob: 8a8953b703bf7bdb1c0c6b43ff9a6069538c60c7 [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]8bef40572012-12-11 21:38:0817scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(Layer* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl)
[email protected]94f206c12012-08-25 00:09:1418{
[email protected]c2282382012-12-16 00:46:0319 DCHECK(treeImpl);
20
[email protected]0bf947242012-12-04 04:25:1121 TRACE_EVENT0("cc", "TreeSynchronizer::synchronizeTrees");
[email protected]96baf3e2012-10-22 23:09:5522 ScopedPtrLayerImplMap oldLayers;
23 RawPtrLayerImplMap newLayers;
[email protected]94f206c12012-08-25 00:09:1424
[email protected]96baf3e2012-10-22 23:09:5525 collectExistingLayerImplRecursive(oldLayers, oldLayerImplRoot.Pass());
[email protected]94f206c12012-08-25 00:09:1426
[email protected]8bef40572012-12-11 21:38:0827 scoped_ptr<LayerImpl> newTree = synchronizeTreeRecursive(newLayers, oldLayers, layerRoot, treeImpl);
[email protected]94f206c12012-08-25 00:09:1428
[email protected]96baf3e2012-10-22 23:09:5529 updateScrollbarLayerPointersRecursive(newLayers, layerRoot);
[email protected]94f206c12012-08-25 00:09:1430
[email protected]e0bd43a2012-10-12 16:54:2131 return newTree.Pass();
[email protected]94f206c12012-08-25 00:09:1432}
33
[email protected]96baf3e2012-10-22 23:09:5534void TreeSynchronizer::collectExistingLayerImplRecursive(ScopedPtrLayerImplMap& oldLayers, scoped_ptr<LayerImpl> layerImpl)
[email protected]94f206c12012-08-25 00:09:1435{
[email protected]96baf3e2012-10-22 23:09:5536 if (!layerImpl)
[email protected]94f206c12012-08-25 00:09:1437 return;
38
[email protected]96baf3e2012-10-22 23:09:5539 ScopedPtrVector<LayerImpl>& children = layerImpl->m_children;
[email protected]ead39c52013-01-09 07:22:4540 for (ScopedPtrVector<LayerImpl>::iterator it = children.begin(); it != children.end(); ++it)
41 collectExistingLayerImplRecursive(oldLayers, children.take(it));
[email protected]94f206c12012-08-25 00:09:1442
[email protected]361bc00d2012-12-14 07:03:2443 collectExistingLayerImplRecursive(oldLayers, layerImpl->takeMaskLayer());
44 collectExistingLayerImplRecursive(oldLayers, layerImpl->takeReplicaLayer());
[email protected]94f206c12012-08-25 00:09:1445
[email protected]96baf3e2012-10-22 23:09:5546 int id = layerImpl->id();
47 oldLayers.set(id, layerImpl.Pass());
[email protected]94f206c12012-08-25 00:09:1448}
49
[email protected]8bef40572012-12-11 21:38:0850scoped_ptr<LayerImpl> TreeSynchronizer::reuseOrCreateLayerImpl(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl)
[email protected]94f206c12012-08-25 00:09:1451{
[email protected]96baf3e2012-10-22 23:09:5552 scoped_ptr<LayerImpl> layerImpl = oldLayers.take(layer->id());
[email protected]94f206c12012-08-25 00:09:1453
[email protected]96baf3e2012-10-22 23:09:5554 if (!layerImpl)
[email protected]8bef40572012-12-11 21:38:0855 layerImpl = layer->createLayerImpl(treeImpl);
[email protected]94f206c12012-08-25 00:09:1456
[email protected]96baf3e2012-10-22 23:09:5557 newLayers[layer->id()] = layerImpl.get();
58 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1459}
60
[email protected]8bef40572012-12-11 21:38:0861scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTreeRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl)
[email protected]94f206c12012-08-25 00:09:1462{
63 if (!layer)
[email protected]96baf3e2012-10-22 23:09:5564 return scoped_ptr<LayerImpl>();
[email protected]94f206c12012-08-25 00:09:1465
[email protected]8bef40572012-12-11 21:38:0866 scoped_ptr<LayerImpl> layerImpl = reuseOrCreateLayerImpl(newLayers, oldLayers, layer, treeImpl);
[email protected]94f206c12012-08-25 00:09:1467
[email protected]96baf3e2012-10-22 23:09:5568 layerImpl->clearChildList();
69 const std::vector<scoped_refptr<Layer> >& children = layer->children();
[email protected]94f206c12012-08-25 00:09:1470 for (size_t i = 0; i < children.size(); ++i)
[email protected]8bef40572012-12-11 21:38:0871 layerImpl->addChild(synchronizeTreeRecursive(newLayers, oldLayers, children[i].get(), treeImpl));
[email protected]94f206c12012-08-25 00:09:1472
[email protected]8bef40572012-12-11 21:38:0873 layerImpl->setMaskLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->maskLayer(), treeImpl));
74 layerImpl->setReplicaLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->replicaLayer(), treeImpl));
[email protected]94f206c12012-08-25 00:09:1475
[email protected]94f206c12012-08-25 00:09:1476 // Remove all dangling pointers. The pointers will be setup later in updateScrollbarLayerPointersRecursive phase
[email protected]e45638c2013-01-17 22:01:4077 layerImpl->setHorizontalScrollbarLayer(0);
78 layerImpl->setVerticalScrollbarLayer(0);
[email protected]94f206c12012-08-25 00:09:1479
[email protected]96baf3e2012-10-22 23:09:5580 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1481}
82
[email protected]96baf3e2012-10-22 23:09:5583void TreeSynchronizer::updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, Layer* layer)
[email protected]94f206c12012-08-25 00:09:1484{
85 if (!layer)
86 return;
87
[email protected]96baf3e2012-10-22 23:09:5588 const std::vector<scoped_refptr<Layer> >& children = layer->children();
[email protected]94f206c12012-08-25 00:09:1489 for (size_t i = 0; i < children.size(); ++i)
90 updateScrollbarLayerPointersRecursive(newLayers, children[i].get());
91
[email protected]96baf3e2012-10-22 23:09:5592 ScrollbarLayer* scrollbarLayer = layer->toScrollbarLayer();
[email protected]94f206c12012-08-25 00:09:1493 if (!scrollbarLayer)
94 return;
95
[email protected]96baf3e2012-10-22 23:09:5596 RawPtrLayerImplMap::const_iterator iter = newLayers.find(scrollbarLayer->id());
97 ScrollbarLayerImpl* scrollbarLayerImpl = iter != newLayers.end() ? static_cast<ScrollbarLayerImpl*>(iter->second) : NULL;
[email protected]e0bd43a2012-10-12 16:54:2198 iter = newLayers.find(scrollbarLayer->scrollLayerId());
[email protected]96baf3e2012-10-22 23:09:5599 LayerImpl* scrollLayerImpl = iter != newLayers.end() ? iter->second : NULL;
[email protected]e0bd43a2012-10-12 16:54:21100
[email protected]96baf3e2012-10-22 23:09:55101 DCHECK(scrollbarLayerImpl);
102 DCHECK(scrollLayerImpl);
[email protected]94f206c12012-08-25 00:09:14103
[email protected]e45638c2013-01-17 22:01:40104 if (scrollbarLayer->orientation() == WebKit::WebScrollbar::Horizontal)
[email protected]96baf3e2012-10-22 23:09:55105 scrollLayerImpl->setHorizontalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14106 else
[email protected]96baf3e2012-10-22 23:09:55107 scrollLayerImpl->setVerticalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14108}
109
[email protected]5c4824e12013-01-12 16:34:53110void TreeSynchronizer::pushProperties(Layer* layer, LayerImpl* layerImpl)
111{
112 if (!layer) {
113 DCHECK(!layerImpl);
114 return;
115 }
116
117 DCHECK_EQ(layer->id(), layerImpl->id());
118 layer->pushPropertiesTo(layerImpl);
119
120 pushProperties(layer->maskLayer(), layerImpl->maskLayer());
121 pushProperties(layer->replicaLayer(), layerImpl->replicaLayer());
122
123 const std::vector<scoped_refptr<Layer> >& children = layer->children();
124 const ScopedPtrVector<LayerImpl>& implChildren = layerImpl->children();
125 DCHECK_EQ(children.size(), implChildren.size());
126
127 for (size_t i = 0; i < children.size(); ++i) {
128 pushProperties(children[i].get(), implChildren[i]);
129 }
130}
131
[email protected]bc5e77c2012-11-05 20:00:49132} // namespace cc