[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] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 17 | typedef ScopedPtrHashMap<int, LayerImpl> ScopedPtrLayerImplMap; |
| 18 | typedef base::hash_map<int, LayerImpl*> RawPtrLayerImplMap; |
[email protected] | c228238 | 2012-12-16 00:46:03 | [diff] [blame] | 19 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 20 | void collectExistingLayerImplRecursive(ScopedPtrLayerImplMap& oldLayers, scoped_ptr<LayerImpl> layerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 21 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 22 | if (!layerImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 23 | return; |
| 24 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 25 | ScopedPtrVector<LayerImpl>& children = layerImpl->children(); |
[email protected] | ead39c5 | 2013-01-09 07:22:45 | [diff] [blame] | 26 | for (ScopedPtrVector<LayerImpl>::iterator it = children.begin(); it != children.end(); ++it) |
| 27 | collectExistingLayerImplRecursive(oldLayers, children.take(it)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 28 | |
[email protected] | 361bc00d | 2012-12-14 07:03:24 | [diff] [blame] | 29 | collectExistingLayerImplRecursive(oldLayers, layerImpl->takeMaskLayer()); |
| 30 | collectExistingLayerImplRecursive(oldLayers, layerImpl->takeReplicaLayer()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 31 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 32 | int id = layerImpl->id(); |
| 33 | oldLayers.set(id, layerImpl.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 36 | template <typename LayerType> |
| 37 | scoped_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 | |
| 54 | scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(Layer* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl) |
| 55 | { |
| 56 | return synchronizeTreesInternal(layerRoot, oldLayerImplRoot.Pass(), treeImpl); |
| 57 | } |
| 58 | |
| 59 | scoped_ptr<LayerImpl> TreeSynchronizer::synchronizeTrees(LayerImpl* layerRoot, scoped_ptr<LayerImpl> oldLayerImplRoot, LayerTreeImpl* treeImpl) |
| 60 | { |
| 61 | return synchronizeTreesInternal(layerRoot, oldLayerImplRoot.Pass(), treeImpl); |
| 62 | } |
| 63 | |
| 64 | template <typename LayerType> |
| 65 | scoped_ptr<LayerImpl> reuseOrCreateLayerImpl(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerType* layer, LayerTreeImpl* treeImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 66 | { |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 67 | scoped_ptr<LayerImpl> layerImpl = oldLayers.take(layer->id()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 68 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 69 | if (!layerImpl) |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 70 | layerImpl = layer->createLayerImpl(treeImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 71 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 72 | newLayers[layer->id()] = layerImpl.get(); |
| 73 | return layerImpl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 76 | template <typename LayerType> |
| 77 | scoped_ptr<LayerImpl> synchronizeTreesRecursiveInternal(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerType* layer, LayerTreeImpl* treeImpl) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 78 | { |
| 79 | if (!layer) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 80 | return scoped_ptr<LayerImpl>(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 81 | |
[email protected] | 8bef4057 | 2012-12-11 21:38:08 | [diff] [blame] | 82 | scoped_ptr<LayerImpl> layerImpl = reuseOrCreateLayerImpl(newLayers, oldLayers, layer, treeImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 83 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 84 | layerImpl->clearChildList(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 85 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 86 | layerImpl->addChild(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->childAt(i), treeImpl)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 87 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 88 | layerImpl->setMaskLayer(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->maskLayer(), treeImpl)); |
| 89 | layerImpl->setReplicaLayer(synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer->replicaLayer(), treeImpl)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 90 | |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 91 | // Remove all dangling pointers. The pointers will be setup later in updateScrollbarLayerPointersRecursive phase |
[email protected] | e45638c | 2013-01-17 22:01:40 | [diff] [blame] | 92 | layerImpl->setHorizontalScrollbarLayer(0); |
| 93 | layerImpl->setVerticalScrollbarLayer(0); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 94 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 95 | return layerImpl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 98 | scoped_ptr<LayerImpl> synchronizeTreesRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, Layer* layer, LayerTreeImpl* treeImpl) |
| 99 | { |
| 100 | return synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer, treeImpl); |
| 101 | } |
| 102 | |
| 103 | scoped_ptr<LayerImpl> synchronizeTreesRecursive(RawPtrLayerImplMap& newLayers, ScopedPtrLayerImplMap& oldLayers, LayerImpl* layer, LayerTreeImpl* treeImpl) |
| 104 | { |
| 105 | return synchronizeTreesRecursiveInternal(newLayers, oldLayers, layer, treeImpl); |
| 106 | } |
| 107 | |
| 108 | template <typename LayerType, typename ScrollbarLayerType> |
| 109 | void updateScrollbarLayerPointersRecursiveInternal(const RawPtrLayerImplMap& newLayers, LayerType* layer) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 110 | { |
| 111 | if (!layer) |
| 112 | return; |
| 113 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 114 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 115 | updateScrollbarLayerPointersRecursiveInternal<LayerType, ScrollbarLayerType>(newLayers, layer->childAt(i)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 116 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 117 | ScrollbarLayerType* scrollbarLayer = layer->toScrollbarLayer(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 118 | if (!scrollbarLayer) |
| 119 | return; |
| 120 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 121 | RawPtrLayerImplMap::const_iterator iter = newLayers.find(scrollbarLayer->id()); |
| 122 | ScrollbarLayerImpl* scrollbarLayerImpl = iter != newLayers.end() ? static_cast<ScrollbarLayerImpl*>(iter->second) : NULL; |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 123 | iter = newLayers.find(scrollbarLayer->scrollLayerId()); |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 124 | LayerImpl* scrollLayerImpl = iter != newLayers.end() ? iter->second : NULL; |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 125 | |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 126 | DCHECK(scrollbarLayerImpl); |
| 127 | DCHECK(scrollLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 128 | |
[email protected] | e45638c | 2013-01-17 22:01:40 | [diff] [blame] | 129 | if (scrollbarLayer->orientation() == WebKit::WebScrollbar::Horizontal) |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 130 | scrollLayerImpl->setHorizontalScrollbarLayer(scrollbarLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 131 | else |
[email protected] | 96baf3e | 2012-10-22 23:09:55 | [diff] [blame] | 132 | scrollLayerImpl->setVerticalScrollbarLayer(scrollbarLayerImpl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 135 | void updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, Layer* layer) |
| 136 | { |
| 137 | updateScrollbarLayerPointersRecursiveInternal<Layer, ScrollbarLayer>(newLayers, layer); |
| 138 | } |
| 139 | |
| 140 | void updateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap& newLayers, LayerImpl* layer) |
| 141 | { |
| 142 | updateScrollbarLayerPointersRecursiveInternal<LayerImpl, ScrollbarLayerImpl>(newLayers, layer); |
| 143 | } |
| 144 | |
| 145 | template <typename LayerType> |
| 146 | void pushPropertiesInternal(LayerType* layer, LayerImpl* layerImpl) |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 147 | { |
| 148 | if (!layer) { |
| 149 | DCHECK(!layerImpl); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | DCHECK_EQ(layer->id(), layerImpl->id()); |
| 154 | layer->pushPropertiesTo(layerImpl); |
| 155 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 156 | pushPropertiesInternal(layer->maskLayer(), layerImpl->maskLayer()); |
| 157 | pushPropertiesInternal(layer->replicaLayer(), layerImpl->replicaLayer()); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 158 | |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 159 | const ScopedPtrVector<LayerImpl>& implChildren = layerImpl->children(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 160 | DCHECK_EQ(layer->children().size(), implChildren.size()); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 161 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 162 | for (size_t i = 0; i < layer->children().size(); ++i) { |
| 163 | pushPropertiesInternal(layer->childAt(i), implChildren[i]); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 167 | void TreeSynchronizer::pushProperties(Layer* layer, LayerImpl* layerImpl) |
| 168 | { |
| 169 | pushPropertiesInternal(layer, layerImpl); |
| 170 | } |
| 171 | |
| 172 | void TreeSynchronizer::pushProperties(LayerImpl* layer, LayerImpl* layerImpl) |
| 173 | { |
| 174 | pushPropertiesInternal(layer, layerImpl); |
| 175 | } |
| 176 | |
| 177 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 178 | } // namespace cc |