1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26
27#include "cc/CCQuadCuller.h"
28
29#include <gmock/gmock.h>
30#include <gtest/gtest.h>
31
32using namespace WebCore;
33
34namespace {
35
36class CCQuadCullerTest : public testing::Test {
37};
38
39class TestDrawQuad : public CCDrawQuad {
40public:
41 TestDrawQuad(const CCSharedQuadState* state, Material m, const IntRect& rect)
42 : CCDrawQuad(state, m, rect)
43 {
44 }
45
46 static PassOwnPtr<TestDrawQuad> create(const CCSharedQuadState* state, Material m, const IntRect& rect)
47 {
48 return adoptPtr(new TestDrawQuad(state, m, rect));
49 }
50};
51
52void setQuads(CCSharedQuadState* rootState, CCSharedQuadState* childState, CCQuadList& quadList)
53{
54 quadList.clear();
55
56 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(), IntSize(100, 100))));
57 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(100, 0), IntSize(100, 100))));
58 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(200, 0), IntSize(100, 100))));
59 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(0, 100), IntSize(100, 100))));
60 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(100, 100), IntSize(100, 100))));
61 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(200, 100), IntSize(100, 100))));
62 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(0, 200), IntSize(100, 100))));
63 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(100, 200), IntSize(100, 100))));
64 quadList.append(TestDrawQuad::create(rootState, CCDrawQuad::TiledContent, IntRect(IntPoint(200, 200), IntSize(100, 100))));
65
66 quadList.append(TestDrawQuad::create(childState, CCDrawQuad::TiledContent, IntRect(IntPoint(), IntSize(100, 100))));
67 quadList.append(TestDrawQuad::create(childState, CCDrawQuad::TiledContent, IntRect(IntPoint(100, 0), IntSize(100, 100))));
68 quadList.append(TestDrawQuad::create(childState, CCDrawQuad::TiledContent, IntRect(IntPoint(0, 100), IntSize(100, 100))));
69 quadList.append(TestDrawQuad::create(childState, CCDrawQuad::TiledContent, IntRect(IntPoint(100, 100), IntSize(100, 100))));
70}
71
72#define DECLARE_AND_INITIALIZE_TEST_QUADS \
73 CCQuadList quadList; \
74 TransformationMatrix childTransform; \
75 IntSize rootSize = IntSize(300, 300); \
76 IntRect rootRect = IntRect(IntPoint(), rootSize); \
77 IntSize childSize = IntSize(200, 200); \
78 IntRect childRect = IntRect(IntPoint(), childSize);
79
80TEST(CCQuadCullerTest, verifyCullChildLinesUpTopLeft)
81{
82 DECLARE_AND_INITIALIZE_TEST_QUADS
83
84 OwnPtr<CCSharedQuadState> rootState = CCSharedQuadState::create(TransformationMatrix(), TransformationMatrix(), rootRect, IntRect(), 1.0, true);
85 OwnPtr<CCSharedQuadState> childState = CCSharedQuadState::create(childTransform, TransformationMatrix(), childRect, IntRect(), 1.0, true);
86
87 setQuads(rootState.get(), childState.get(), quadList);
88 EXPECT_EQ(quadList.size(), 13u);
89 CCQuadCuller::cullOccludedQuads(quadList);
90 EXPECT_EQ(quadList.size(), 9u);
91}
92
93TEST(CCQuadCullerTest, verifyCullWhenChildOpacityNotOne)
94{
95 DECLARE_AND_INITIALIZE_TEST_QUADS
96
97 OwnPtr<CCSharedQuadState> rootState = CCSharedQuadState::create(TransformationMatrix(), TransformationMatrix(), rootRect, IntRect(), 1.0, true);
98 OwnPtr<CCSharedQuadState> childState = CCSharedQuadState::create(childTransform, TransformationMatrix(), childRect, IntRect(), 0.9, true);
99
100 setQuads(rootState.get(), childState.get(), quadList);
101 EXPECT_EQ(quadList.size(), 13u);
102 CCQuadCuller::cullOccludedQuads(quadList);
103 EXPECT_EQ(quadList.size(), 13u);
104}
105
106TEST(CCQuadCullerTest, verifyCullWhenChildOpaqueFlagFalse)
107{
108 DECLARE_AND_INITIALIZE_TEST_QUADS
109
110 OwnPtr<CCSharedQuadState> rootState = CCSharedQuadState::create(TransformationMatrix(), TransformationMatrix(), rootRect, IntRect(), 1.0, true);
111 OwnPtr<CCSharedQuadState> childState = CCSharedQuadState::create(childTransform, TransformationMatrix(), childRect, IntRect(), 1.0, false);
112
113 setQuads(rootState.get(), childState.get(), quadList);
114 EXPECT_EQ(quadList.size(), 13u);
115 CCQuadCuller::cullOccludedQuads(quadList);
116 EXPECT_EQ(quadList.size(), 13u);
117}
118
119TEST(CCQuadCullerTest, verifyCullCenterTileOnly)
120{
121 DECLARE_AND_INITIALIZE_TEST_QUADS
122
123 childTransform.translate(50, 50);
124
125 OwnPtr<CCSharedQuadState> rootState = CCSharedQuadState::create(TransformationMatrix(), TransformationMatrix(), rootRect, IntRect(), 1.0, true);
126 OwnPtr<CCSharedQuadState> childState = CCSharedQuadState::create(childTransform, TransformationMatrix(), childRect, IntRect(), 1.0, true);
127
128 setQuads(rootState.get(), childState.get(), quadList);
129 EXPECT_EQ(quadList.size(), 13u);
130 CCQuadCuller::cullOccludedQuads(quadList);
131 EXPECT_EQ(quadList.size(), 12u);
132}
133
134TEST(CCQuadCullerTest, verifyCullChildLinesUpBottomRight)
135{
136 DECLARE_AND_INITIALIZE_TEST_QUADS
137
138 childTransform.translate(100, 100);
139
140 OwnPtr<CCSharedQuadState> rootState = CCSharedQuadState::create(TransformationMatrix(), TransformationMatrix(), rootRect, IntRect(), 1.0, true);
141 OwnPtr<CCSharedQuadState> childState = CCSharedQuadState::create(childTransform, TransformationMatrix(), childRect, IntRect(), 1.0, true);
142
143 setQuads(rootState.get(), childState.get(), quadList);
144 EXPECT_EQ(quadList.size(), 13u);
145 CCQuadCuller::cullOccludedQuads(quadList);
146 EXPECT_EQ(quadList.size(), 9u);
147}
148
149} // namespace