[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 1 | // 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] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 5 | #include "cc/tree_synchronizer.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | 0bf94724 | 2012-12-04 04:25:11 | [diff] [blame] | 7 | #include "base/debug/trace_event.h" |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 9 | #include "cc/layer.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 10 | #include "cc/layer_impl.h" |
[email protected] | c4040a52 | 2012-10-21 15:01:40 | [diff] [blame] | 11 | #include "cc/scrollbar_animation_controller.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 12 | #include "cc/scrollbar_layer.h" |
[email protected] | c4040a52 | 2012-10-21 15:01:40 | [diff] [blame] | 13 | #include "cc/scrollbar_layer_impl.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 14 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 15 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 16 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 17 | scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(Layer* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 18 | { |
[email protected] | c228238 | 2012-12-16 00:46:03 | [diff] [blame] | 19 | DCHECK(treeImpl); |
| 20 | |
[email protected] | 0bf94724 | 2012-12-04 04:25:11 | [diff] [blame] | 21 | TRACE_EVENT0("cc", "TreeSynchronizer::synchronizeTrees"); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | ScopedPtrLayerImplMap oldLayers; |
| 23 | RawPtrLayerImplMap newLayers; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 24 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 25 | collectExistingLayerImplRecursive(oldLayers, oldLayerImplRoot.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 26 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 27 | scoped_ptr<LayerImpl> newTree = synchronizeTreeRecursive(newLayers, oldLayers, layerRoot, treeImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 28 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 29 | updateScrollbarLayerPointersRecursive(newLayers, layerRoot); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 30 | |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 31 | return newTree.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 34 | void TreeSynchronizer::collectExistingLayerImplRecursive(ScopedPtrLayerImplMap& oldLayers, scoped_ptr<LayerImpl> layerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 35 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 36 | if (!layerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 37 | return; |
| 38 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 39 | ScopedPtrVector<LayerImpl>& children = layerImpl->m_children; |
[email protected] | ead39c5 | 2013-01-09 07:22:45 | [diff] [blame] | 40 | for (ScopedPtrVector<LayerImpl>::iterator it = children.begin(); it != children.end(); ++it) |
| 41 | collectExistingLayerImplRecursive(oldLayers, children.take(it)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 42 | |
[email protected] | 361bc00d | 2012-12-14 07:03:24 | [diff] [blame] | 43 | collectExistingLayerImplRecursive(oldLayers, layerImpl->takeMaskLayer()); |
| 44 | collectExistingLayerImplRecursive(oldLayers, layerImpl->takeReplicaLayer()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 45 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 46 | int id = layerImpl->id(); |
| 47 | oldLayers.set(id, layerImpl.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 50 | scoped_ptr<LayerImpl> TreeSynchronizer::reuseOrCreateLayerImpl(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 51 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 52 | scoped_ptr<LayerImpl> layerImpl = oldLayers.take(layer->id()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 53 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 54 | if (!layerImpl) |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 55 | layerImpl = layer->createLayerImpl(treeImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 56 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 57 | newLayers[layer->id()] = layerImpl.get(); |
| 58 | return layerImpl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 59 | } |
| 60 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 61 | scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTreeRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 62 | { |
| 63 | if (!layer) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 64 | return scoped_ptr<LayerImpl>(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 65 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 66 | scoped_ptr<LayerImpl> layerImpl = reuseOrCreateLayerImpl(newLayers, oldLayers, layer, treeImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 67 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 68 | layerImpl->clearChildList(); |
| 69 | const std::vector<scoped_refptr<Layer> >& children = layer->children(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 70 | for (size_t i = 0; i < children.size(); ++i) |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 71 | layerImpl->addChild(synchronizeTreeRecursive(newLayers, oldLayers, children[i].get(), treeImpl)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 72 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 73 | layerImpl->setMaskLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->maskLayer(), treeImpl)); |
| 74 | layerImpl->setReplicaLayer(synchronizeTreeRecursive(newLayers, oldLayers, layer->replicaLayer(), treeImpl)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 75 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 76 | // Remove all dangling pointers. The pointers will be setup later in updateScrollbarLayerPointersRecursive phase |
[email protected] | e45638c | 2013-01-17 22:01:40 | [diff] [blame^] | 77 | layerImpl->setHorizontalScrollbarLayer(0); |
| 78 | layerImpl->setVerticalScrollbarLayer(0); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 79 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 80 | return layerImpl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 83 | void TreeSynchronizer::updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, Layer* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 84 | { |
| 85 | if (!layer) |
| 86 | return; |
| 87 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 88 | const std::vector<scoped_refptr<Layer> >& children = layer->children(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 89 | for (size_t i = 0; i < children.size(); ++i) |
| 90 | updateScrollbarLayerPointersRecursive(newLayers, children[i].get()); |
| 91 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 92 | ScrollbarLayer* scrollbarLayer = layer->toScrollbarLayer(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | if (!scrollbarLayer) |
| 94 | return; |
| 95 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 96 | RawPtrLayerImplMap::const_iterator iter = newLayers.find(scrollbarLayer->id()); |
| 97 | ScrollbarLayerImpl* scrollbarLayerImpl = iter != newLayers.end() ? static_cast<ScrollbarLayerImpl*>(iter->second) : NULL; |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 98 | iter = newLayers.find(scrollbarLayer->scrollLayerId()); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 99 | LayerImpl* scrollLayerImpl = iter != newLayers.end() ? iter->second : NULL; |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 100 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 101 | DCHECK(scrollbarLayerImpl); |
| 102 | DCHECK(scrollLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 103 | |
[email protected] | e45638c | 2013-01-17 22:01:40 | [diff] [blame^] | 104 | if (scrollbarLayer->orientation() == WebKit::WebScrollbar::Horizontal) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 105 | scrollLayerImpl->setHorizontalScrollbarLayer(scrollbarLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 106 | else |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 107 | scrollLayerImpl->setVerticalScrollbarLayer(scrollbarLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 110 | void 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] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 132 | } // namespace cc |