blob: ca4f2bba343908464bfdcf69fcd142e1cb358ede [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
5#include "config.h"
6
[email protected]a8461d82012-10-16 21:11:147#include "cc/tree_synchronizer.h"
[email protected]94f206c12012-08-25 00:09:148
[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]96baf3e2012-10-22 23:09:5517scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(Layer* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeHostImpl* hostImpl)
[email protected]94f206c12012-08-25 00:09:1418{
[email protected]96baf3e2012-10-22 23:09:5519 ScopedPtrLayerImplMap oldLayers;
20 RawPtrLayerImplMap newLayers;
[email protected]94f206c12012-08-25 00:09:1421
[email protected]96baf3e2012-10-22 23:09:5522 collectExistingLayerImplRecursive(oldLayers, oldLayerImplRoot.Pass());
[email protected]94f206c12012-08-25 00:09:1423
[email protected]96baf3e2012-10-22 23:09:5524 scoped_ptr<LayerImpl> newTree = synchronizeTreeRecursive(newLayers, oldLayers, layerRoot, hostImpl);
[email protected]94f206c12012-08-25 00:09:1425
[email protected]96baf3e2012-10-22 23:09:5526 updateScrollbarLayerPointersRecursive(newLayers, layerRoot);
[email protected]94f206c12012-08-25 00:09:1427
[email protected]e0bd43a2012-10-12 16:54:2128 return newTree.Pass();
[email protected]94f206c12012-08-25 00:09:1429}
30
[email protected]96baf3e2012-10-22 23:09:5531void TreeSynchronizer::collectExistingLayerImplRecursive(ScopedPtrLayerImplMap& oldLayers, scoped_ptr<LayerImpl> layerImpl)
[email protected]94f206c12012-08-25 00:09:1432{
[email protected]96baf3e2012-10-22 23:09:5533 if (!layerImpl)
[email protected]94f206c12012-08-25 00:09:1434 return;
35
[email protected]96baf3e2012-10-22 23:09:5536 ScopedPtrVector<LayerImpl>& children = layerImpl->m_children;
[email protected]94f206c12012-08-25 00:09:1437 for (size_t i = 0; i < children.size(); ++i)
[email protected]96baf3e2012-10-22 23:09:5538 collectExistingLayerImplRecursive(oldLayers, children.take(i));
[email protected]94f206c12012-08-25 00:09:1439
[email protected]96baf3e2012-10-22 23:09:5540 collectExistingLayerImplRecursive(oldLayers, layerImpl->m_maskLayer.Pass());
41 collectExistingLayerImplRecursive(oldLayers, layerImpl->m_replicaLayer.Pass());
[email protected]94f206c12012-08-25 00:09:1442
[email protected]96baf3e2012-10-22 23:09:5543 int id = layerImpl->id();
44 oldLayers.set(id, layerImpl.Pass());
[email protected]94f206c12012-08-25 00:09:1445}
46
[email protected]96baf3e2012-10-22 23:09:5547scoped_ptr<LayerImpl> TreeSynchronizer::reuseOrCreateLayerImpl(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer)
[email protected]94f206c12012-08-25 00:09:1448{
[email protected]96baf3e2012-10-22 23:09:5549 scoped_ptr<LayerImpl> layerImpl = oldLayers.take(layer->id());
[email protected]94f206c12012-08-25 00:09:1450
[email protected]96baf3e2012-10-22 23:09:5551 if (!layerImpl)
52 layerImpl = layer->createLayerImpl();
[email protected]94f206c12012-08-25 00:09:1453
[email protected]96baf3e2012-10-22 23:09:5554 newLayers[layer->id()] = layerImpl.get();
55 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1456}
57
[email protected]96baf3e2012-10-22 23:09:5558scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTreeRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeHostImpl* hostImpl)
[email protected]94f206c12012-08-25 00:09:1459{
60 if (!layer)
[email protected]96baf3e2012-10-22 23:09:5561 return scoped_ptr<LayerImpl>();
[email protected]94f206c12012-08-25 00:09:1462
[email protected]96baf3e2012-10-22 23:09:5563 scoped_ptr<LayerImpl> layerImpl = reuseOrCreateLayerImpl(newLayers, oldLayers, layer);
[email protected]94f206c12012-08-25 00:09:1464
[email protected]96baf3e2012-10-22 23:09:5565 layerImpl->clearChildList();
66 const std::vector<scoped_refptr<Layer> >& children = layer->children();
[email protected]94f206c12012-08-25 00:09:1467 for (size_t i = 0; i < children.size(); ++i)
[email protected]96baf3e2012-10-22 23:09:5568 layerImpl->addChild(synchronizeTreeRecursive(newLayers, oldLayers, children[i].get(), hostImpl));
[email protected]94f206c12012-08-25 00:09:1469
[email protected]96baf3e2012-10-22 23:09:5570 layerImpl->setMaskLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->maskLayer(), hostImpl));
71 layerImpl->setReplicaLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->replicaLayer(), hostImpl));
[email protected]94f206c12012-08-25 00:09:1472
[email protected]96baf3e2012-10-22 23:09:5573 layer->pushPropertiesTo(layerImpl.get());
74 layerImpl->setLayerTreeHostImpl(hostImpl);
[email protected]94f206c12012-08-25 00:09:1475
76 // Remove all dangling pointers. The pointers will be setup later in updateScrollbarLayerPointersRecursive phase
[email protected]96baf3e2012-10-22 23:09:5577 if (ScrollbarAnimationController* scrollbarController = layerImpl->scrollbarAnimationController()) {
[email protected]94f206c12012-08-25 00:09:1478 scrollbarController->setHorizontalScrollbarLayer(0);
79 scrollbarController->setVerticalScrollbarLayer(0);
80 }
81
[email protected]96baf3e2012-10-22 23:09:5582 return layerImpl.Pass();
[email protected]94f206c12012-08-25 00:09:1483}
84
[email protected]96baf3e2012-10-22 23:09:5585void TreeSynchronizer::updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, Layer* layer)
[email protected]94f206c12012-08-25 00:09:1486{
87 if (!layer)
88 return;
89
[email protected]96baf3e2012-10-22 23:09:5590 const std::vector<scoped_refptr<Layer> >& children = layer->children();
[email protected]94f206c12012-08-25 00:09:1491 for (size_t i = 0; i < children.size(); ++i)
92 updateScrollbarLayerPointersRecursive(newLayers, children[i].get());
93
[email protected]96baf3e2012-10-22 23:09:5594 ScrollbarLayer* scrollbarLayer = layer->toScrollbarLayer();
[email protected]94f206c12012-08-25 00:09:1495 if (!scrollbarLayer)
96 return;
97
[email protected]96baf3e2012-10-22 23:09:5598 RawPtrLayerImplMap::const_iterator iter = newLayers.find(scrollbarLayer->id());
99 ScrollbarLayerImpl* scrollbarLayerImpl = iter != newLayers.end() ? static_cast<ScrollbarLayerImpl*>(iter->second) : NULL;
[email protected]e0bd43a2012-10-12 16:54:21100 iter = newLayers.find(scrollbarLayer->scrollLayerId());
[email protected]96baf3e2012-10-22 23:09:55101 LayerImpl* scrollLayerImpl = iter != newLayers.end() ? iter->second : NULL;
[email protected]e0bd43a2012-10-12 16:54:21102
[email protected]96baf3e2012-10-22 23:09:55103 DCHECK(scrollbarLayerImpl);
104 DCHECK(scrollLayerImpl);
[email protected]94f206c12012-08-25 00:09:14105
[email protected]96baf3e2012-10-22 23:09:55106 if (scrollbarLayerImpl->orientation() == WebKit::WebScrollbar::Horizontal)
107 scrollLayerImpl->setHorizontalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14108 else
[email protected]96baf3e2012-10-22 23:09:55109 scrollLayerImpl->setVerticalScrollbarLayer(scrollbarLayerImpl);
[email protected]94f206c12012-08-25 00:09:14110}
111
[email protected]bc5e77c2012-11-05 20:00:49112} // namespace cc