[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] | 556fd29 | 2013-03-18 08:03:04 | [diff] [blame^] | 5 | #include "cc/trees/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] | 95e4e1a0 | 2013-03-18 07:09:09 | [diff] [blame] | 9 | #include "cc/animation/scrollbar_animation_controller.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 10 | #include "cc/layer.h" |
[email protected] | d50c686 | 2012-10-23 02:08:31 | [diff] [blame] | 11 | #include "cc/layer_impl.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] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 20 | void CollectExistingLayerImplRecursive(ScopedPtrLayerImplMap* old_layers, |
| 21 | scoped_ptr<LayerImpl> layer_impl) { |
| 22 | if (!layer_impl) |
| 23 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 24 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 25 | ScopedPtrVector<LayerImpl>& children = layer_impl->children(); |
| 26 | for (ScopedPtrVector<LayerImpl>::iterator it = children.begin(); |
| 27 | it != children.end(); |
| 28 | ++it) |
| 29 | CollectExistingLayerImplRecursive(old_layers, children.take(it)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 30 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 31 | CollectExistingLayerImplRecursive(old_layers, layer_impl->TakeMaskLayer()); |
| 32 | CollectExistingLayerImplRecursive(old_layers, layer_impl->TakeReplicaLayer()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 33 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 34 | int id = layer_impl->id(); |
| 35 | old_layers->set(id, layer_impl.Pass()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 36 | } |
| 37 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 38 | template <typename LayerType> |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 39 | scoped_ptr<LayerImpl> SynchronizeTreesInternal( |
| 40 | LayerType* layer_root, |
| 41 | scoped_ptr<LayerImpl> old_layer_impl_root, |
| 42 | LayerTreeImpl* tree_impl) { |
| 43 | DCHECK(tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 44 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 45 | TRACE_EVENT0("cc", "TreeSynchronizer::SynchronizeTrees"); |
| 46 | ScopedPtrLayerImplMap old_layers; |
| 47 | RawPtrLayerImplMap new_layers; |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 48 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 49 | CollectExistingLayerImplRecursive(&old_layers, old_layer_impl_root.Pass()); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 50 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 51 | scoped_ptr<LayerImpl> new_tree = SynchronizeTreesRecursive( |
| 52 | &new_layers, &old_layers, layer_root, tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 53 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 54 | UpdateScrollbarLayerPointersRecursive(&new_layers, layer_root); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 55 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 56 | return new_tree.Pass(); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 59 | scoped_ptr<LayerImpl> TreeSynchronizer::SynchronizeTrees( |
| 60 | Layer* layer_root, |
| 61 | scoped_ptr<LayerImpl> old_layer_impl_root, |
| 62 | LayerTreeImpl* tree_impl) { |
| 63 | return SynchronizeTreesInternal( |
| 64 | layer_root, old_layer_impl_root.Pass(), tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 67 | scoped_ptr<LayerImpl> TreeSynchronizer::SynchronizeTrees( |
| 68 | LayerImpl* layer_root, |
| 69 | scoped_ptr<LayerImpl> old_layer_impl_root, |
| 70 | LayerTreeImpl* tree_impl) { |
| 71 | return SynchronizeTreesInternal( |
| 72 | layer_root, old_layer_impl_root.Pass(), tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | template <typename LayerType> |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 76 | scoped_ptr<LayerImpl> ReuseOrCreateLayerImpl(RawPtrLayerImplMap* new_layers, |
| 77 | ScopedPtrLayerImplMap* old_layers, |
| 78 | LayerType* layer, |
| 79 | LayerTreeImpl* tree_impl) { |
| 80 | scoped_ptr<LayerImpl> layer_impl = old_layers->take(layer->id()); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 81 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 82 | if (!layer_impl) |
| 83 | layer_impl = layer->CreateLayerImpl(tree_impl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 84 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 85 | (*new_layers)[layer->id()] = layer_impl.get(); |
| 86 | return layer_impl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 89 | template <typename LayerType> |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 90 | scoped_ptr<LayerImpl> SynchronizeTreesRecursiveInternal( |
| 91 | RawPtrLayerImplMap* new_layers, |
| 92 | ScopedPtrLayerImplMap* old_layers, |
| 93 | LayerType* layer, |
| 94 | LayerTreeImpl* tree_impl) { |
| 95 | if (!layer) |
| 96 | return scoped_ptr<LayerImpl>(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 97 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 98 | scoped_ptr<LayerImpl> layer_impl = |
| 99 | ReuseOrCreateLayerImpl(new_layers, old_layers, layer, tree_impl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 100 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 101 | layer_impl->ClearChildList(); |
| 102 | for (size_t i = 0; i < layer->children().size(); ++i) { |
| 103 | layer_impl->AddChild(SynchronizeTreesRecursiveInternal( |
| 104 | new_layers, old_layers, layer->child_at(i), tree_impl)); |
| 105 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 106 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 107 | layer_impl->SetMaskLayer(SynchronizeTreesRecursiveInternal( |
| 108 | new_layers, old_layers, layer->mask_layer(), tree_impl)); |
| 109 | layer_impl->SetReplicaLayer(SynchronizeTreesRecursiveInternal( |
| 110 | new_layers, old_layers, layer->replica_layer(), tree_impl)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 111 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 112 | // Remove all dangling pointers. The pointers will be setup later in |
| 113 | // UpdateScrollbarLayerPointersRecursive phase |
| 114 | layer_impl->SetHorizontalScrollbarLayer(NULL); |
| 115 | layer_impl->SetVerticalScrollbarLayer(NULL); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 116 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 117 | return layer_impl.Pass(); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 120 | scoped_ptr<LayerImpl> SynchronizeTreesRecursive( |
| 121 | RawPtrLayerImplMap* new_layers, |
| 122 | ScopedPtrLayerImplMap* old_layers, |
| 123 | Layer* layer, |
| 124 | LayerTreeImpl* tree_impl) { |
| 125 | return SynchronizeTreesRecursiveInternal( |
| 126 | new_layers, old_layers, layer, tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 129 | scoped_ptr<LayerImpl> SynchronizeTreesRecursive( |
| 130 | RawPtrLayerImplMap* new_layers, |
| 131 | ScopedPtrLayerImplMap* old_layers, |
| 132 | LayerImpl* layer, |
| 133 | LayerTreeImpl* tree_impl) { |
| 134 | return SynchronizeTreesRecursiveInternal( |
| 135 | new_layers, old_layers, layer, tree_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | template <typename LayerType, typename ScrollbarLayerType> |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 139 | void UpdateScrollbarLayerPointersRecursiveInternal( |
| 140 | const RawPtrLayerImplMap* new_layers, |
| 141 | LayerType* layer) { |
| 142 | if (!layer) |
| 143 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 144 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 145 | for (size_t i = 0; i < layer->children().size(); ++i) { |
| 146 | UpdateScrollbarLayerPointersRecursiveInternal< |
| 147 | LayerType, ScrollbarLayerType>(new_layers, layer->child_at(i)); |
| 148 | } |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 149 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 150 | ScrollbarLayerType* scrollbar_layer = layer->ToScrollbarLayer(); |
[email protected] | b7c4783f | 2013-03-15 23:11:42 | [diff] [blame] | 151 | // Pinch-zoom scrollbars will have an invalid scrollLayerId, but they are |
| 152 | // managed by LayerTreeImpl and not LayerImpl, so should not be |
| 153 | // processed here. |
| 154 | if (!scrollbar_layer || (scrollbar_layer->scroll_layer_id() == |
| 155 | Layer::PINCH_ZOOM_ROOT_SCROLL_LAYER_ID)) |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 156 | return; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 157 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 158 | RawPtrLayerImplMap::const_iterator iter = |
| 159 | new_layers->find(scrollbar_layer->id()); |
| 160 | ScrollbarLayerImpl* scrollbar_layer_impl = |
| 161 | iter != new_layers->end() ? static_cast<ScrollbarLayerImpl*>(iter->second) |
| 162 | : NULL; |
| 163 | iter = new_layers->find(scrollbar_layer->scroll_layer_id()); |
| 164 | LayerImpl* scroll_layer_impl = |
| 165 | iter != new_layers->end() ? iter->second : NULL; |
[email protected] | e0bd43a | 2012-10-12 16:54:21 | [diff] [blame] | 166 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 167 | DCHECK(scrollbar_layer_impl); |
| 168 | DCHECK(scroll_layer_impl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 169 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 170 | if (scrollbar_layer->Orientation() == WebKit::WebScrollbar::Horizontal) |
| 171 | scroll_layer_impl->SetHorizontalScrollbarLayer(scrollbar_layer_impl); |
| 172 | else |
| 173 | scroll_layer_impl->SetVerticalScrollbarLayer(scrollbar_layer_impl); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 176 | void UpdateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap* new_layers, |
| 177 | Layer* layer) { |
| 178 | UpdateScrollbarLayerPointersRecursiveInternal<Layer, ScrollbarLayer>( |
| 179 | new_layers, layer); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 182 | void UpdateScrollbarLayerPointersRecursive(const RawPtrLayerImplMap* new_layers, |
| 183 | LayerImpl* layer) { |
| 184 | UpdateScrollbarLayerPointersRecursiveInternal<LayerImpl, ScrollbarLayerImpl>( |
| 185 | new_layers, layer); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | template <typename LayerType> |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 189 | void PushPropertiesInternal(LayerType* layer, LayerImpl* layer_impl) { |
| 190 | if (!layer) { |
| 191 | DCHECK(!layer_impl); |
| 192 | return; |
| 193 | } |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 194 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 195 | DCHECK_EQ(layer->id(), layer_impl->id()); |
| 196 | layer->PushPropertiesTo(layer_impl); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 197 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 198 | PushPropertiesInternal(layer->mask_layer(), layer_impl->mask_layer()); |
| 199 | PushPropertiesInternal(layer->replica_layer(), layer_impl->replica_layer()); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 200 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 201 | const ScopedPtrVector<LayerImpl>& impl_children = layer_impl->children(); |
| 202 | DCHECK_EQ(layer->children().size(), impl_children.size()); |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 203 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 204 | for (size_t i = 0; i < layer->children().size(); ++i) { |
| 205 | PushPropertiesInternal(layer->child_at(i), impl_children[i]); |
| 206 | } |
[email protected] | 5c4824e1 | 2013-01-12 16:34:53 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 209 | void TreeSynchronizer::PushProperties(Layer* layer, LayerImpl* layer_impl) { |
| 210 | PushPropertiesInternal(layer, layer_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | b5651c2 | 2013-03-14 15:06:33 | [diff] [blame] | 213 | void TreeSynchronizer::PushProperties(LayerImpl* layer, LayerImpl* layer_impl) { |
| 214 | PushPropertiesInternal(layer, layer_impl); |
[email protected] | 48871fc | 2013-01-23 07:36:51 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 217 | } // namespace cc |