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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
// 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/tree_synchronizer.h"
#include <algorithm>
#include "cc/layer.h"
#include "cc/layer_animation_controller.h"
#include "cc/layer_impl.h"
#include "cc/proxy.h"
#include "cc/single_thread_proxy.h"
#include "cc/test/animation_test_common.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
namespace {
class MockLayerImpl : public LayerImpl {
public:
static scoped_ptr<MockLayerImpl> create(LayerTreeImpl* treeImpl, int layerId)
{
return make_scoped_ptr(new MockLayerImpl(treeImpl, layerId));
}
virtual ~MockLayerImpl()
{
if (m_layerImplDestructionList)
m_layerImplDestructionList->push_back(id());
}
void setLayerImplDestructionList(std::vector<int>* list) { m_layerImplDestructionList = list; }
private:
MockLayerImpl(LayerTreeImpl* treeImpl, int layerId)
: LayerImpl(treeImpl, layerId)
, m_layerImplDestructionList(0)
{
}
std::vector<int>* m_layerImplDestructionList;
};
class MockLayer : public Layer {
public:
static scoped_refptr<MockLayer> create(std::vector<int>* layerImplDestructionList)
{
return make_scoped_refptr(new MockLayer(layerImplDestructionList));
}
virtual scoped_ptr<LayerImpl> createLayerImpl(LayerTreeImpl* treeImpl) OVERRIDE
{
return MockLayerImpl::create(treeImpl, m_layerId).PassAs<LayerImpl>();
}
virtual void pushPropertiesTo(LayerImpl* layerImpl) OVERRIDE
{
Layer::pushPropertiesTo(layerImpl);
MockLayerImpl* mockLayerImpl = static_cast<MockLayerImpl*>(layerImpl);
mockLayerImpl->setLayerImplDestructionList(m_layerImplDestructionList);
}
private:
MockLayer(std::vector<int>* layerImplDestructionList)
: Layer()
, m_layerImplDestructionList(layerImplDestructionList)
{
}
virtual ~MockLayer() { }
std::vector<int>* m_layerImplDestructionList;
};
class FakeLayerAnimationController : public LayerAnimationController {
public:
static scoped_refptr<LayerAnimationController> create()
{
return static_cast<LayerAnimationController*>(new FakeLayerAnimationController);
}
bool synchronizedAnimations() const { return m_synchronizedAnimations; }
private:
FakeLayerAnimationController()
: LayerAnimationController(1)
, m_synchronizedAnimations(false)
{ }
virtual ~FakeLayerAnimationController() { }
virtual void pushAnimationUpdatesTo(LayerAnimationController* controllerImpl)
{
LayerAnimationController::pushAnimationUpdatesTo(controllerImpl);
m_synchronizedAnimations = true;
}
bool m_synchronizedAnimations;
};
void expectTreesAreIdentical(Layer* layer, LayerImpl* layerImpl, LayerTreeImpl* treeImpl)
{
ASSERT_TRUE(layer);
ASSERT_TRUE(layerImpl);
EXPECT_EQ(layer->id(), layerImpl->id());
EXPECT_EQ(layerImpl->layerTreeImpl(), treeImpl);
EXPECT_EQ(layer->nonFastScrollableRegion(), layerImpl->nonFastScrollableRegion());
ASSERT_EQ(!!layer->maskLayer(), !!layerImpl->maskLayer());
if (layer->maskLayer())
expectTreesAreIdentical(layer->maskLayer(), layerImpl->maskLayer(), treeImpl);
ASSERT_EQ(!!layer->replicaLayer(), !!layerImpl->replicaLayer());
if (layer->replicaLayer())
expectTreesAreIdentical(layer->replicaLayer(), layerImpl->replicaLayer(), treeImpl);
const std::vector<scoped_refptr<Layer> >& layerChildren = layer->children();
const ScopedPtrVector<LayerImpl>& layerImplChildren = layerImpl->children();
ASSERT_EQ(layerChildren.size(), layerImplChildren.size());
for (size_t i = 0; i < layerChildren.size(); ++i)
expectTreesAreIdentical(layerChildren[i].get(), layerImplChildren[i], treeImpl);
}
class TreeSynchronizerTest : public testing::Test {
public:
TreeSynchronizerTest()
: m_hostImpl(&m_proxy)
{
}
protected:
FakeImplProxy m_proxy;
FakeLayerTreeHostImpl m_hostImpl;
};
// Attempts to synchronizes a null tree. This should not crash, and should
// return a null tree.
TEST_F(TreeSynchronizerTest, syncNullTree)
{
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(0, scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
EXPECT_TRUE(!layerImplTreeRoot.get());
}
// Constructs a very simple tree and synchronizes it without trying to reuse any preexisting layers.
TEST_F(TreeSynchronizerTest, syncSimpleTreeFromEmpty)
{
scoped_refptr<Layer> layerTreeRoot = Layer::create();
layerTreeRoot->addChild(Layer::create());
layerTreeRoot->addChild(Layer::create());
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
}
// Constructs a very simple tree and synchronizes it attempting to reuse some layers
TEST_F(TreeSynchronizerTest, syncSimpleTreeReusingLayers)
{
std::vector<int> layerImplDestructionList;
scoped_refptr<Layer> layerTreeRoot = MockLayer::create(&layerImplDestructionList);
layerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
layerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Add a new layer to the Layer side
layerTreeRoot->children()[0]->addChild(MockLayer::create(&layerImplDestructionList));
// Remove one.
layerTreeRoot->children()[1]->removeFromParent();
int secondLayerImplId = layerImplTreeRoot->children()[1]->id();
// Synchronize again. After the sync the trees should be equivalent and we should have created and destroyed one LayerImpl.
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
ASSERT_EQ(1u, layerImplDestructionList.size());
EXPECT_EQ(secondLayerImplId, layerImplDestructionList[0]);
}
// Constructs a very simple tree and checks that a stacking-order change is tracked properly.
TEST_F(TreeSynchronizerTest, syncSimpleTreeAndTrackStackingOrderChange)
{
std::vector<int> layerImplDestructionList;
// Set up the tree and sync once. child2 needs to be synced here, too, even though we
// remove it to set up the intended scenario.
scoped_refptr<Layer> layerTreeRoot = MockLayer::create(&layerImplDestructionList);
scoped_refptr<Layer> child2 = MockLayer::create(&layerImplDestructionList);
layerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
layerTreeRoot->addChild(child2);
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
layerImplTreeRoot->resetAllChangeTrackingForSubtree();
// re-insert the layer and sync again.
child2->removeFromParent();
layerTreeRoot->addChild(child2);
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Check that the impl thread properly tracked the change.
EXPECT_FALSE(layerImplTreeRoot->layerPropertyChanged());
EXPECT_FALSE(layerImplTreeRoot->children()[0]->layerPropertyChanged());
EXPECT_TRUE(layerImplTreeRoot->children()[1]->layerPropertyChanged());
}
TEST_F(TreeSynchronizerTest, syncSimpleTreeAndProperties)
{
scoped_refptr<Layer> layerTreeRoot = Layer::create();
layerTreeRoot->addChild(Layer::create());
layerTreeRoot->addChild(Layer::create());
// Pick some random properties to set. The values are not important, we're just testing that at least some properties are making it through.
gfx::PointF rootPosition = gfx::PointF(2.3f, 7.4f);
layerTreeRoot->setPosition(rootPosition);
float firstChildOpacity = 0.25f;
layerTreeRoot->children()[0]->setOpacity(firstChildOpacity);
gfx::Size secondChildBounds = gfx::Size(25, 53);
layerTreeRoot->children()[1]->setBounds(secondChildBounds);
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Check that the property values we set on the Layer tree are reflected in the LayerImpl tree.
gfx::PointF rootLayerImplPosition = layerImplTreeRoot->position();
EXPECT_EQ(rootPosition.x(), rootLayerImplPosition.x());
EXPECT_EQ(rootPosition.y(), rootLayerImplPosition.y());
EXPECT_EQ(firstChildOpacity, layerImplTreeRoot->children()[0]->opacity());
gfx::Size secondLayerImplChildBounds = layerImplTreeRoot->children()[1]->bounds();
EXPECT_EQ(secondChildBounds.width(), secondLayerImplChildBounds.width());
EXPECT_EQ(secondChildBounds.height(), secondLayerImplChildBounds.height());
}
TEST_F(TreeSynchronizerTest, reuseLayerImplsAfterStructuralChange)
{
std::vector<int> layerImplDestructionList;
// Set up a tree with this sort of structure:
// root --- A --- B ---+--- C
// |
// +--- D
scoped_refptr<Layer> layerTreeRoot = MockLayer::create(&layerImplDestructionList);
layerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
scoped_refptr<Layer> layerA = layerTreeRoot->children()[0].get();
layerA->addChild(MockLayer::create(&layerImplDestructionList));
scoped_refptr<Layer> layerB = layerA->children()[0].get();
layerB->addChild(MockLayer::create(&layerImplDestructionList));
scoped_refptr<Layer> layerC = layerB->children()[0].get();
layerB->addChild(MockLayer::create(&layerImplDestructionList));
scoped_refptr<Layer> layerD = layerB->children()[1].get();
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Now restructure the tree to look like this:
// root --- D ---+--- A
// |
// +--- C --- B
layerTreeRoot->removeAllChildren();
layerD->removeAllChildren();
layerTreeRoot->addChild(layerD);
layerA->removeAllChildren();
layerD->addChild(layerA);
layerC->removeAllChildren();
layerD->addChild(layerC);
layerB->removeAllChildren();
layerC->addChild(layerB);
// After another synchronize our trees should match and we should not have destroyed any LayerImpls
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
EXPECT_EQ(0u, layerImplDestructionList.size());
}
// Constructs a very simple tree, synchronizes it, then synchronizes to a totally new tree. All layers from the old tree should be deleted.
TEST_F(TreeSynchronizerTest, syncSimpleTreeThenDestroy)
{
std::vector<int> layerImplDestructionList;
scoped_refptr<Layer> oldLayerTreeRoot = MockLayer::create(&layerImplDestructionList);
oldLayerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
oldLayerTreeRoot->addChild(MockLayer::create(&layerImplDestructionList));
int oldTreeRootLayerId = oldLayerTreeRoot->id();
int oldTreeFirstChildLayerId = oldLayerTreeRoot->children()[0]->id();
int oldTreeSecondChildLayerId = oldLayerTreeRoot->children()[1]->id();
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(oldLayerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(oldLayerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Remove all children on the Layer side.
oldLayerTreeRoot->removeAllChildren();
// Synchronize again. After the sync all LayerImpls from the old tree should be deleted.
scoped_refptr<Layer> newLayerTreeRoot = Layer::create();
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(newLayerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(newLayerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
ASSERT_EQ(3u, layerImplDestructionList.size());
EXPECT_TRUE(std::find(layerImplDestructionList.begin(), layerImplDestructionList.end(), oldTreeRootLayerId) != layerImplDestructionList.end());
EXPECT_TRUE(std::find(layerImplDestructionList.begin(), layerImplDestructionList.end(), oldTreeFirstChildLayerId) != layerImplDestructionList.end());
EXPECT_TRUE(std::find(layerImplDestructionList.begin(), layerImplDestructionList.end(), oldTreeSecondChildLayerId) != layerImplDestructionList.end());
}
// Constructs+syncs a tree with mask, replica, and replica mask layers.
TEST_F(TreeSynchronizerTest, syncMaskReplicaAndReplicaMaskLayers)
{
scoped_refptr<Layer> layerTreeRoot = Layer::create();
layerTreeRoot->addChild(Layer::create());
layerTreeRoot->addChild(Layer::create());
layerTreeRoot->addChild(Layer::create());
// First child gets a mask layer.
scoped_refptr<Layer> maskLayer = Layer::create();
layerTreeRoot->children()[0]->setMaskLayer(maskLayer.get());
// Second child gets a replica layer.
scoped_refptr<Layer> replicaLayer = Layer::create();
layerTreeRoot->children()[1]->setReplicaLayer(replicaLayer.get());
// Third child gets a replica layer with a mask layer.
scoped_refptr<Layer> replicaLayerWithMask = Layer::create();
scoped_refptr<Layer> replicaMaskLayer = Layer::create();
replicaLayerWithMask->setMaskLayer(replicaMaskLayer.get());
layerTreeRoot->children()[2]->setReplicaLayer(replicaLayerWithMask.get());
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Remove the mask layer.
layerTreeRoot->children()[0]->setMaskLayer(0);
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Remove the replica layer.
layerTreeRoot->children()[1]->setReplicaLayer(0);
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
// Remove the replica mask.
replicaLayerWithMask->setMaskLayer(0);
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
expectTreesAreIdentical(layerTreeRoot.get(), layerImplTreeRoot.get(), m_hostImpl.activeTree());
}
TEST_F(TreeSynchronizerTest, synchronizeAnimations)
{
LayerTreeSettings settings;
FakeProxy proxy(scoped_ptr<Thread>(NULL));
DebugScopedSetImplThread impl(&proxy);
scoped_ptr<LayerTreeHostImpl> hostImpl = LayerTreeHostImpl::create(settings, 0, &proxy);
scoped_refptr<Layer> layerTreeRoot = Layer::create();
layerTreeRoot->setLayerAnimationController(FakeLayerAnimationController::create());
EXPECT_FALSE(static_cast<FakeLayerAnimationController*>(layerTreeRoot->layerAnimationController())->synchronizedAnimations());
scoped_ptr<LayerImpl> layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), scoped_ptr<LayerImpl>(), m_hostImpl.activeTree());
layerImplTreeRoot = TreeSynchronizer::synchronizeTrees(layerTreeRoot.get(), layerImplTreeRoot.Pass(), m_hostImpl.activeTree());
EXPECT_TRUE(static_cast<FakeLayerAnimationController*>(layerTreeRoot->layerAnimationController())->synchronizedAnimations());
}
} // namespace
} // namespace cc
|