summaryrefslogtreecommitdiffstats
path: root/cc/trees/tree_synchronizer.cc
blob: d50a57dbb676041d4b2aef4adf69026d9d5084f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/trees/tree_synchronizer.h"

#include <stddef.h>

#include <set>

#include "base/logging.h"
#include "base/trace_event/trace_event.h"
#include "cc/layers/layer.h"
#include "cc/layers/layer_collections.h"
#include "cc/layers/layer_impl.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_impl.h"

namespace cc {

template <typename LayerType>
void SynchronizeTreesInternal(LayerType* layer_root, LayerTreeImpl* tree_impl) {
  DCHECK(tree_impl);

  TRACE_EVENT0("cc", "TreeSynchronizer::SynchronizeTrees");
  scoped_ptr<OwnedLayerImplList> old_layers(tree_impl->DetachLayers());

  OwnedLayerImplMap old_layer_map;
  for (auto& it : *old_layers)
    old_layer_map[it->id()] = std::move(it);

  SynchronizeTreesRecursive(&old_layer_map, layer_root, tree_impl);

  for (auto& it : old_layer_map) {
    if (it.second) {
      // Need to ensure that layer destruction doesn't tear down child
      // LayerImpl that have been used in the new tree.
      it.second->children().clear();
    }
  }
}

void TreeSynchronizer::SynchronizeTrees(Layer* layer_root,
                                        LayerTreeImpl* tree_impl) {
  if (!layer_root)
    tree_impl->ClearLayers();
  else
    SynchronizeTreesInternal(layer_root, tree_impl);
}

void TreeSynchronizer::SynchronizeTrees(LayerImpl* layer_root,
                                        LayerTreeImpl* tree_impl) {
  if (!layer_root)
    tree_impl->ClearLayers();
  else
    SynchronizeTreesInternal(layer_root, tree_impl);
}

template <typename LayerType>
scoped_ptr<LayerImpl> ReuseOrCreateLayerImpl(OwnedLayerImplMap* old_layers,
                                             LayerType* layer,
                                             LayerTreeImpl* tree_impl) {
  if (!layer)
    return nullptr;
  scoped_ptr<LayerImpl> layer_impl = std::move((*old_layers)[layer->id()]);
  if (!layer_impl)
    layer_impl = layer->CreateLayerImpl(tree_impl);
  return layer_impl;
}

template <typename LayerType>
scoped_ptr<LayerImpl> SynchronizeTreesRecursiveInternal(
    OwnedLayerImplMap* old_layers,
    LayerType* layer,
    LayerTreeImpl* tree_impl) {
  if (!layer)
    return nullptr;

  scoped_ptr<LayerImpl> layer_impl(
      ReuseOrCreateLayerImpl(old_layers, layer, tree_impl));

  layer_impl->children().clear();
  for (size_t i = 0; i < layer->children().size(); ++i) {
    layer_impl->AddChild(SynchronizeTreesRecursiveInternal(
        old_layers, layer->child_at(i), tree_impl));
  }

  scoped_ptr<LayerImpl> mask_layer = SynchronizeTreesRecursiveInternal(
      old_layers, layer->mask_layer(), tree_impl);
  if (layer_impl->mask_layer() && mask_layer &&
      layer_impl->mask_layer() == mask_layer.get()) {
    // In this case, we only need to update the ownership, as we're essentially
    // just resetting the mask layer.
    tree_impl->AddLayer(std::move(mask_layer));
  } else {
    layer_impl->SetMaskLayer(std::move(mask_layer));
  }

  scoped_ptr<LayerImpl> replica_layer = SynchronizeTreesRecursiveInternal(
      old_layers, layer->replica_layer(), tree_impl);
  if (layer_impl->replica_layer() && replica_layer &&
      layer_impl->replica_layer() == replica_layer.get()) {
    // In this case, we only need to update the ownership, as we're essentially
    // just resetting the replica layer.
    tree_impl->AddLayer(std::move(replica_layer));
  } else {
    layer_impl->SetReplicaLayer(std::move(replica_layer));
  }

  return layer_impl;
}

void SynchronizeTreesRecursive(OwnedLayerImplMap* old_layers,
                               Layer* old_root,
                               LayerTreeImpl* tree_impl) {
  tree_impl->SetRootLayer(
      SynchronizeTreesRecursiveInternal(old_layers, old_root, tree_impl));
}

void SynchronizeTreesRecursive(OwnedLayerImplMap* old_layers,
                               LayerImpl* old_root,
                               LayerTreeImpl* tree_impl) {
  tree_impl->SetRootLayer(
      SynchronizeTreesRecursiveInternal(old_layers, old_root, tree_impl));
}

static void CheckScrollAndClipPointersRecursive(Layer* layer,
                                                LayerImpl* layer_impl) {
  DCHECK_EQ(!!layer, !!layer_impl);
  if (!layer)
    return;

  // Having a scroll parent on the impl thread implies having one the main
  // thread, too. The main thread may have a scroll parent that is not in the
  // tree because it's been removed but not deleted. In this case, the layer
  // impl will have no scroll parent. Same argument applies for clip parents and
  // scroll/clip children.
  DCHECK(!layer_impl->scroll_parent() || !!layer->scroll_parent());
  DCHECK(!layer_impl->clip_parent() || !!layer->clip_parent());
  DCHECK(!layer_impl->scroll_children() || !!layer->scroll_children());
  DCHECK(!layer_impl->clip_children() || !!layer->clip_children());

  if (layer_impl->scroll_parent())
    DCHECK_EQ(layer->scroll_parent()->id(), layer_impl->scroll_parent()->id());

  if (layer_impl->clip_parent())
    DCHECK_EQ(layer->clip_parent()->id(), layer_impl->clip_parent()->id());

  if (layer_impl->scroll_children()) {
    for (std::set<Layer*>::iterator it = layer->scroll_children()->begin();
         it != layer->scroll_children()->end(); ++it) {
      DCHECK_EQ((*it)->scroll_parent(), layer);
    }
    for (std::set<LayerImpl*>::iterator it =
             layer_impl->scroll_children()->begin();
         it != layer_impl->scroll_children()->end(); ++it) {
      DCHECK_EQ((*it)->scroll_parent(), layer_impl);
    }
  }

  if (layer_impl->clip_children()) {
    for (std::set<Layer*>::iterator it = layer->clip_children()->begin();
         it != layer->clip_children()->end(); ++it) {
      DCHECK_EQ((*it)->clip_parent(), layer);
    }
    for (std::set<LayerImpl*>::iterator it =
             layer_impl->clip_children()->begin();
         it != layer_impl->clip_children()->end(); ++it) {
      DCHECK_EQ((*it)->clip_parent(), layer_impl);
    }
  }

  for (size_t i = 0u; i < layer->children().size(); ++i) {
    CheckScrollAndClipPointersRecursive(layer->child_at(i),
                                        layer_impl->child_at(i));
  }
}

template <typename LayerType>
static void PushLayerPropertiesInternal(
    std::unordered_set<LayerType*> layers_that_should_push_properties,
    LayerTreeImpl* impl_tree) {
  for (auto layer : layers_that_should_push_properties) {
    LayerImpl* layer_impl = impl_tree->LayerById(layer->id());
    DCHECK(layer_impl);
    layer->PushPropertiesTo(layer_impl);
  }
}

void TreeSynchronizer::PushLayerProperties(LayerTreeImpl* pending_tree,
                                           LayerTreeImpl* active_tree) {
  PushLayerPropertiesInternal(pending_tree->LayersThatShouldPushProperties(),
                              active_tree);
}

void TreeSynchronizer::PushLayerProperties(LayerTreeHost* host_tree,
                                           LayerTreeImpl* impl_tree) {
  PushLayerPropertiesInternal(host_tree->LayersThatShouldPushProperties(),
                              impl_tree);

#if DCHECK_IS_ON()
  if (host_tree->root_layer() && impl_tree->root_layer())
    CheckScrollAndClipPointersRecursive(host_tree->root_layer(),
                                        impl_tree->root_layer());
#endif
}

}  // namespace cc