summaryrefslogtreecommitdiff
path: root/Source/WebKit/chromium/tests/CCLayerSorterTest.cpp
blob: b2cb479a85aceb7066ef99e998074238a8a3fd36 (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
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
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "CCLayerSorter.h"

#include "CCLayerImpl.h"
#include "CCMathUtil.h"
#include "CCSingleThreadProxy.h"
#include <gtest/gtest.h>
#include <public/WebTransformationMatrix.h>

using namespace WebCore;
using WebKit::WebTransformationMatrix;

namespace {

// Note: In the following overlap tests, the "camera" is looking down the negative Z axis,
// meaning that layers with smaller z values (more negative) are further from the camera
// and therefore must be drawn before layers with higher z values.

TEST(CCLayerSorterTest, BasicOverlap)
{
    CCLayerSorter::ABCompareResult overlapResult;
    const float zThreshold = 0.1f;
    float weight = 0;

    // Trivial test, with one layer directly obscuring the other.
    WebTransformationMatrix neg4Translate;
    neg4Translate.translate3d(0, 0, -4);
    CCLayerSorter::LayerShape front(2, 2, neg4Translate);

    WebTransformationMatrix neg5Translate;
    neg5Translate.translate3d(0, 0, -5);
    CCLayerSorter::LayerShape back(2, 2, neg5Translate);

    overlapResult = CCLayerSorter::checkOverlap(&front, &back, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::BBeforeA, overlapResult);
    EXPECT_EQ(1, weight);

    overlapResult = CCLayerSorter::checkOverlap(&back, &front, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
    EXPECT_EQ(1, weight);

    // One layer translated off to the right. No overlap should be detected.
    WebTransformationMatrix rightTranslate;
    rightTranslate.translate3d(10, 0, -5);
    CCLayerSorter::LayerShape backRight(2, 2, rightTranslate);
    overlapResult = CCLayerSorter::checkOverlap(&front, &backRight, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::None, overlapResult);

    // When comparing a layer with itself, z difference is always 0.
    overlapResult = CCLayerSorter::checkOverlap(&front, &front, zThreshold, weight);
    EXPECT_EQ(0, weight);
}

TEST(CCLayerSorterTest, RightAngleOverlap)
{
    CCLayerSorter::ABCompareResult overlapResult;
    const float zThreshold = 0.1f;
    float weight = 0;

    WebTransformationMatrix perspectiveMatrix;
    perspectiveMatrix.applyPerspective(1000);

    // Two layers forming a right angle with a perspective viewing transform.
    WebTransformationMatrix leftFaceMatrix;
    leftFaceMatrix.rotate3d(0, 1, 0, -90);
    leftFaceMatrix.translateRight3d(-1, 0, -5);
    leftFaceMatrix.translate(-1, -1);
    CCLayerSorter::LayerShape leftFace(2, 2, perspectiveMatrix * leftFaceMatrix);
    WebTransformationMatrix frontFaceMatrix;
    frontFaceMatrix.translate3d(0, 0, -4);
    frontFaceMatrix.translate(-1, -1);
    CCLayerSorter::LayerShape frontFace(2, 2, perspectiveMatrix * frontFaceMatrix);

    overlapResult = CCLayerSorter::checkOverlap(&frontFace, &leftFace, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::BBeforeA, overlapResult);
}

TEST(CCLayerSorterTest, IntersectingLayerOverlap)
{
    CCLayerSorter::ABCompareResult overlapResult;
    const float zThreshold = 0.1f;
    float weight = 0;

    WebTransformationMatrix perspectiveMatrix;
    perspectiveMatrix.applyPerspective(1000);

    // Intersecting layers. An explicit order will be returned based on relative z
    // values at the overlapping features but the weight returned should be zero.
    WebTransformationMatrix frontFaceMatrix;
    frontFaceMatrix.translate3d(0, 0, -4);
    frontFaceMatrix.translate(-1, -1);
    CCLayerSorter::LayerShape frontFace(2, 2, perspectiveMatrix * frontFaceMatrix);

    WebTransformationMatrix throughMatrix;
    throughMatrix.rotate3d(0, 1, 0, 45);
    throughMatrix.translateRight3d(0, 0, -4);
    throughMatrix.translate(-1, -1);
    CCLayerSorter::LayerShape rotatedFace(2, 2, perspectiveMatrix * throughMatrix);
    overlapResult = CCLayerSorter::checkOverlap(&frontFace, &rotatedFace, zThreshold, weight);
    EXPECT_NE(CCLayerSorter::None, overlapResult);
    EXPECT_EQ(0, weight);
}

TEST(CCLayerSorterTest, LayersAtAngleOverlap)
{
    CCLayerSorter::ABCompareResult overlapResult;
    const float zThreshold = 0.1f;
    float weight = 0;

    // Trickier test with layers at an angle.
    //
    //   -x . . . . 0 . . . . +x
    // -z             /
    //  :            /----B----
    //  0           C
    //  : ----A----/
    // +z         /
    //
    // C is in front of A and behind B (not what you'd expect by comparing centers).
    // A and B don't overlap, so they're incomparable.

    WebTransformationMatrix transformA;
    transformA.translate3d(-6, 0, 1);
    transformA.translate(-4, -10);
    CCLayerSorter::LayerShape layerA(8, 20, transformA);

    WebTransformationMatrix transformB;
    transformB.translate3d(6, 0, -1);
    transformB.translate(-4, -10);
    CCLayerSorter::LayerShape layerB(8, 20, transformB);

    WebTransformationMatrix transformC;
    transformC.rotate3d(0, 1, 0, 40);
    transformC.translate(-4, -10);
    CCLayerSorter::LayerShape layerC(8, 20, transformC);

    overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerC, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
    overlapResult = CCLayerSorter::checkOverlap(&layerC, &layerB, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
    overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerB, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::None, overlapResult);
}

TEST(CCLayerSorterTest, LayersUnderPathologicalPerspectiveTransform)
{
    CCLayerSorter::ABCompareResult overlapResult;
    const float zThreshold = 0.1f;
    float weight = 0;

    // On perspective projection, if w becomes negative, the re-projected point will be
    // invalid and un-usable. Correct code needs to clip away portions of the geometry
    // where w < 0. If the code uses the invalid value, it will think that a layer has
    // different bounds than it really does, which can cause things to sort incorrectly.

    WebTransformationMatrix perspectiveMatrix;
    perspectiveMatrix.applyPerspective(1);

    WebTransformationMatrix transformA;
    transformA.translate3d(-15, 0, -2);
    transformA.translate(-5, -5);
    CCLayerSorter::LayerShape layerA(10, 10, perspectiveMatrix * transformA);

    // With this sequence of transforms, when layer B is correctly clipped, it will be
    // visible on the left half of the projection plane, in front of layerA. When it is
    // not clipped, its bounds will actually incorrectly appear much smaller and the
    // correct sorting dependency will not be found.
    WebTransformationMatrix transformB;
    transformB.translate3d(0, 0, 0.7);
    transformB.rotate3d(0, 45, 0);
    transformB.translate(-5, -5);
    CCLayerSorter::LayerShape layerB(10, 10, perspectiveMatrix * transformB);

    // Sanity check that the test case actually covers the intended scenario, where part
    // of layer B go behind the w = 0 plane.
    FloatQuad testQuad = FloatQuad(FloatRect(FloatPoint(-0.5, -0.5), FloatSize(1, 1)));
    bool clipped = false;
    CCMathUtil::mapQuad(perspectiveMatrix * transformB, testQuad, clipped);
    ASSERT_TRUE(clipped);

    overlapResult = CCLayerSorter::checkOverlap(&layerA, &layerB, zThreshold, weight);
    EXPECT_EQ(CCLayerSorter::ABeforeB, overlapResult);
}

TEST(CCLayerSorterTest, verifyExistingOrderingPreservedWhenNoZDiff)
{
    DebugScopedSetImplThread thisScopeIsOnImplThread;

    // If there is no reason to re-sort the layers (i.e. no 3d z difference), then the
    // existing ordering provided on input should be retained. This test covers the fix in
    // https://bugs.webkit.org/show_bug.cgi?id=75046. Before this fix, ordering was
    // accidentally reversed, causing bugs in z-index ordering on websites when
    // preserves3D triggered the CCLayerSorter.

    // Input list of layers: [1, 2, 3, 4, 5].
    // Expected output: [3, 4, 1, 2, 5].
    //    - 1, 2, and 5 do not have a 3d z difference, and therefore their relative ordering should be retained.
    //    - 3 and 4 do not have a 3d z difference, and therefore their relative ordering should be retained.
    //    - 3 and 4 should be re-sorted so they are in front of 1, 2, and 5.

    OwnPtr<CCLayerImpl> layer1 = CCLayerImpl::create(1);
    OwnPtr<CCLayerImpl> layer2 = CCLayerImpl::create(2);
    OwnPtr<CCLayerImpl> layer3 = CCLayerImpl::create(3);
    OwnPtr<CCLayerImpl> layer4 = CCLayerImpl::create(4);
    OwnPtr<CCLayerImpl> layer5 = CCLayerImpl::create(5);

    WebTransformationMatrix BehindMatrix;
    BehindMatrix.translate3d(0, 0, 2);
    WebTransformationMatrix FrontMatrix;
    FrontMatrix.translate3d(0, 0, 1);

    layer1->setBounds(IntSize(10, 10));
    layer1->setContentBounds(IntSize(10, 10));
    layer1->setDrawTransform(BehindMatrix);
    layer1->setDrawsContent(true);

    layer2->setBounds(IntSize(20, 20));
    layer2->setContentBounds(IntSize(20, 20));
    layer2->setDrawTransform(BehindMatrix);
    layer2->setDrawsContent(true);

    layer3->setBounds(IntSize(30, 30));
    layer3->setContentBounds(IntSize(30, 30));
    layer3->setDrawTransform(FrontMatrix);
    layer3->setDrawsContent(true);

    layer4->setBounds(IntSize(40, 40));
    layer4->setContentBounds(IntSize(40, 40));
    layer4->setDrawTransform(FrontMatrix);
    layer4->setDrawsContent(true);

    layer5->setBounds(IntSize(50, 50));
    layer5->setContentBounds(IntSize(50, 50));
    layer5->setDrawTransform(BehindMatrix);
    layer5->setDrawsContent(true);

    Vector<CCLayerImpl*> layerList;
    layerList.append(layer1.get());
    layerList.append(layer2.get());
    layerList.append(layer3.get());
    layerList.append(layer4.get());
    layerList.append(layer5.get());

    ASSERT_EQ(static_cast<size_t>(5), layerList.size());
    EXPECT_EQ(1, layerList[0]->id());
    EXPECT_EQ(2, layerList[1]->id());
    EXPECT_EQ(3, layerList[2]->id());
    EXPECT_EQ(4, layerList[3]->id());
    EXPECT_EQ(5, layerList[4]->id());

    CCLayerSorter layerSorter;
    layerSorter.sort(layerList.begin(), layerList.end());

    ASSERT_EQ(static_cast<size_t>(5), layerList.size());
    EXPECT_EQ(3, layerList[0]->id());
    EXPECT_EQ(4, layerList[1]->id());
    EXPECT_EQ(1, layerList[2]->id());
    EXPECT_EQ(2, layerList[3]->id());
    EXPECT_EQ(5, layerList[4]->id());
}

} // namespace