Unreviewed, rolling out r105366.
[WebKit-https.git] / Source / WebKit / chromium / tests / CCTiledLayerImplTest.cpp
1 /*
2  * Copyright (C) 2012 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/CCTiledLayerImpl.h"
28
29 #include "Region.h"
30 #include "cc/CCSingleThreadProxy.h"
31 #include "cc/CCTileDrawQuad.h"
32 #include <gmock/gmock.h>
33 #include <gtest/gtest.h>
34
35 using namespace WebCore;
36
37 namespace {
38
39 // Create a default tiled layer with textures for all tiles and a default
40 // visibility of the entire layer size.
41 static PassRefPtr<CCTiledLayerImpl> createLayer(const IntSize& tileSize, const IntSize& layerSize, CCLayerTilingData::BorderTexelOption borderTexels)
42 {
43     RefPtr<CCTiledLayerImpl> layer = CCTiledLayerImpl::create(0);
44     OwnPtr<CCLayerTilingData> tiler = CCLayerTilingData::create(tileSize, borderTexels);
45     tiler->setBounds(layerSize);
46     layer->setTilingData(*tiler);
47     layer->setSkipsDraw(false);
48     layer->setVisibleLayerRect(IntRect(IntPoint(), layerSize));
49
50     int textureId = 1;
51     for (int i = 0; i < tiler->numTilesX(); ++i)
52         for (int j = 0; j < tiler->numTilesY(); ++j)
53             layer->syncTextureId(i, j, static_cast<Platform3DObject>(textureId++));
54
55     return layer.release();
56 }
57
58 TEST(CCTiledLayerImplTest, emptyQuadList)
59 {
60     DebugScopedSetImplThread scopedImplThread;
61
62     const IntSize tileSize(90, 90);
63     const int numTilesX = 8;
64     const int numTilesY = 4;
65     const IntSize layerSize(tileSize.width() * numTilesX, tileSize.height() * numTilesY);
66
67     // Verify default layer does creates quads
68     {
69         RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
70         CCQuadList quads;
71         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
72         layer->appendQuads(quads, sharedQuadState.get());
73         const unsigned numTiles = numTilesX * numTilesY;
74         EXPECT_EQ(quads.size(), numTiles);
75     }
76
77     // Layer with empty visible layer rect produces no quads
78     {
79         RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
80         layer->setVisibleLayerRect(IntRect());
81
82         CCQuadList quads;
83         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
84         layer->appendQuads(quads, sharedQuadState.get());
85         EXPECT_EQ(quads.size(), 0u);
86     }
87
88     // Layer with non-intersecting visible layer rect produces no quads
89     {
90         RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
91
92         IntRect outsideBounds(IntPoint(-100, -100), IntSize(50, 50));
93         layer->setVisibleLayerRect(outsideBounds);
94
95         CCQuadList quads;
96         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
97         layer->appendQuads(quads, sharedQuadState.get());
98         EXPECT_EQ(quads.size(), 0u);
99     }
100
101     // Layer with skips draw produces no quads
102     {
103         RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
104         layer->setSkipsDraw(true);
105
106         CCQuadList quads;
107         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
108         layer->appendQuads(quads, sharedQuadState.get());
109         EXPECT_EQ(quads.size(), 0u);
110     }
111 }
112
113 TEST(CCTiledLayerImplTest, checkerboarding)
114 {
115     DebugScopedSetImplThread scopedImplThread;
116
117     const IntSize tileSize(10, 10);
118     const int numTilesX = 2;
119     const int numTilesY = 2;
120     const IntSize layerSize(tileSize.width() * numTilesX, tileSize.height() * numTilesY);
121
122     RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
123     OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
124
125     // No checkerboarding
126     {
127         CCQuadList quads;
128         layer->appendQuads(quads, sharedQuadState.get());
129         EXPECT_EQ(quads.size(), 4u);
130
131         for (size_t i = 0; i < quads.size(); ++i)
132             EXPECT_EQ(quads[i]->material(), CCDrawQuad::TiledContent);
133     }
134
135     for (int i = 0; i < numTilesX; ++i)
136         for (int j = 0; j < numTilesY; ++j)
137             layer->syncTextureId(i, j, static_cast<Platform3DObject>(0));
138
139     // All checkerboarding
140     {
141         CCQuadList quads;
142         layer->appendQuads(quads, sharedQuadState.get());
143         EXPECT_EQ(quads.size(), 4u);
144         for (size_t i = 0; i < quads.size(); ++i)
145             EXPECT_EQ(quads[i]->material(), CCDrawQuad::SolidColor);
146     }
147 }
148
149 static bool completelyContains(const Region& container, const IntRect& rect)
150 {
151     Region tester(rect);
152     Vector<IntRect> rects = container.rects();
153     for (size_t i = 0; i < rects.size(); ++i)
154         tester.subtract(rects[i]);
155     return tester.isEmpty();
156 }
157
158 static void getQuads(CCQuadList& quads, IntSize tileSize, const IntSize& layerSize, CCLayerTilingData::BorderTexelOption borderTexelOption, const IntRect& visibleLayerRect)
159 {
160     RefPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, borderTexelOption);
161     layer->setVisibleLayerRect(visibleLayerRect);
162     layer->setBounds(layerSize);
163
164     OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
165     layer->appendQuads(quads, sharedQuadState.get());
166 }
167
168 // Align with expected and actual output
169 static const char* quadString = "    Quad: ";
170
171 static void verifyQuadsExactlyCoverRect(const CCQuadList& quads, const IntRect& rect)
172 {
173     Region remaining(rect);
174
175     for (size_t i = 0; i < quads.size(); ++i) {
176         CCDrawQuad* quad = quads[i].get();
177
178         EXPECT_TRUE(rect.contains(quad->quadRect())) << quadString << i;
179         EXPECT_TRUE(completelyContains(remaining, quad->quadRect())) << quadString << i;
180         remaining.subtract(Region(quad->quadRect()));
181     }
182
183     EXPECT_TRUE(remaining.isEmpty());
184 }
185
186 // Test with both border texels and without.
187 #define WITH_AND_WITHOUT_BORDER_TEST(testFixtureName)       \
188     TEST(CCTiledLayerImplTest, testFixtureName##NoBorders)  \
189     {                                                       \
190         testFixtureName(CCLayerTilingData::NoBorderTexels); \
191     }                                                       \
192     TEST(CCTiledLayerImplTest, testFixtureName##HasBorders) \
193     {                                                       \
194         testFixtureName(CCLayerTilingData::HasBorderTexels);\
195     }
196
197 static void coverageVisibleRectOnTileBoundaries(CCLayerTilingData::BorderTexelOption borders)
198 {
199     DebugScopedSetImplThread scopedImplThread;
200
201     IntSize layerSize(1000, 1000);
202     CCQuadList quads;
203     getQuads(quads, IntSize(100, 100), layerSize, borders, IntRect(IntPoint(), layerSize));
204     verifyQuadsExactlyCoverRect(quads, IntRect(IntPoint(), layerSize));
205 }
206 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectOnTileBoundaries);
207
208 static void coverageVisibleRectIntersectsTiles(CCLayerTilingData::BorderTexelOption borders)
209 {
210     DebugScopedSetImplThread scopedImplThread;
211
212     // This rect intersects the middle 3x3 of the 5x5 tiles.
213     IntPoint topLeft(65, 73);
214     IntPoint bottomRight(182, 198);
215     IntRect visibleLayerRect(topLeft, bottomRight - topLeft);
216
217     IntSize layerSize(250, 250);
218     CCQuadList quads;
219     getQuads(quads, IntSize(50, 50), IntSize(250, 250), CCLayerTilingData::NoBorderTexels, visibleLayerRect);
220     verifyQuadsExactlyCoverRect(quads, visibleLayerRect);
221 }
222 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsTiles);
223
224 static void coverageVisibleRectIntersectsBounds(CCLayerTilingData::BorderTexelOption borders)
225 {
226     DebugScopedSetImplThread scopedImplThread;
227
228     IntSize layerSize(220, 210);
229     IntRect visibleLayerRect(IntPoint(), layerSize);
230     CCQuadList quads;
231     getQuads(quads, IntSize(100, 100), layerSize, CCLayerTilingData::NoBorderTexels, visibleLayerRect);
232     verifyQuadsExactlyCoverRect(quads, visibleLayerRect);
233 }
234 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsBounds);
235
236 TEST(CCTiledLayerImplTest, textureInfoForLayerNoBorders)
237 {
238     DebugScopedSetImplThread scopedImplThread;
239
240     IntSize tileSize(50, 50);
241     IntSize layerSize(250, 250);
242     CCQuadList quads;
243     getQuads(quads, tileSize, layerSize, CCLayerTilingData::NoBorderTexels, IntRect(IntPoint(), layerSize));
244
245     for (size_t i = 0; i < quads.size(); ++i) {
246         ASSERT_EQ(quads[i]->material(), CCDrawQuad::TiledContent) << quadString << i;
247         CCTileDrawQuad* quad = static_cast<CCTileDrawQuad*>(quads[i].get());
248
249         EXPECT_NE(quad->textureId(), 0u) << quadString << i;
250         EXPECT_EQ(quad->textureOffset(), IntPoint()) << quadString << i;
251         EXPECT_EQ(quad->textureSize(), tileSize) << quadString << i;
252     }
253 }
254
255
256 } // namespace