[chromium] Early out in a new prepareToDraw() step if checkerboarding an accelerated...
[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 "CCLayerTestCommon.h"
30 #include "MockCCQuadCuller.h"
31 #include "cc/CCSingleThreadProxy.h"
32 #include "cc/CCTileDrawQuad.h"
33 #include <gmock/gmock.h>
34 #include <gtest/gtest.h>
35
36 using namespace WebCore;
37 using namespace CCLayerTestCommon;
38
39 namespace {
40
41 // Create a default tiled layer with textures for all tiles and a default
42 // visibility of the entire layer size.
43 static PassOwnPtr<CCTiledLayerImpl> createLayer(const IntSize& tileSize, const IntSize& layerSize, CCLayerTilingData::BorderTexelOption borderTexels)
44 {
45     OwnPtr<CCTiledLayerImpl> layer = CCTiledLayerImpl::create(0);
46     OwnPtr<CCLayerTilingData> tiler = CCLayerTilingData::create(tileSize, borderTexels);
47     tiler->setBounds(layerSize);
48     layer->setTilingData(*tiler);
49     layer->setSkipsDraw(false);
50     layer->setVisibleLayerRect(IntRect(IntPoint(), layerSize));
51     layer->setDrawOpacity(1);
52
53     int textureId = 1;
54     for (int i = 0; i < tiler->numTilesX(); ++i)
55         for (int j = 0; j < tiler->numTilesY(); ++j)
56             layer->pushTileProperties(i, j, static_cast<Platform3DObject>(textureId++), IntRect(0, 0, 1, 1));
57
58     return layer.release();
59 }
60
61 TEST(CCTiledLayerImplTest, emptyQuadList)
62 {
63     DebugScopedSetImplThread scopedImplThread;
64
65     const IntSize tileSize(90, 90);
66     const int numTilesX = 8;
67     const int numTilesY = 4;
68     const IntSize layerSize(tileSize.width() * numTilesX, tileSize.height() * numTilesY);
69
70     // Verify default layer does creates quads
71     {
72         OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
73         MockCCQuadCuller quadCuller;
74         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
75         bool usedCheckerboard = false;
76         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
77         const unsigned numTiles = numTilesX * numTilesY;
78         EXPECT_EQ(quadCuller.quadList().size(), numTiles);
79     }
80
81     // Layer with empty visible layer rect produces no quads
82     {
83         OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
84         layer->setVisibleLayerRect(IntRect());
85
86         MockCCQuadCuller quadCuller;
87         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
88         bool usedCheckerboard = false;
89         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
90         EXPECT_EQ(quadCuller.quadList().size(), 0u);
91     }
92
93     // Layer with non-intersecting visible layer rect produces no quads
94     {
95         OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
96
97         IntRect outsideBounds(IntPoint(-100, -100), IntSize(50, 50));
98         layer->setVisibleLayerRect(outsideBounds);
99
100         MockCCQuadCuller quadCuller;
101         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
102         bool usedCheckerboard = false;
103         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
104         EXPECT_EQ(quadCuller.quadList().size(), 0u);
105     }
106
107     // Layer with skips draw produces no quads
108     {
109         OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
110         layer->setSkipsDraw(true);
111
112         MockCCQuadCuller quadCuller;
113         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
114         bool usedCheckerboard = false;
115         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
116         EXPECT_EQ(quadCuller.quadList().size(), 0u);
117     }
118 }
119
120 TEST(CCTiledLayerImplTest, checkerboarding)
121 {
122     DebugScopedSetImplThread scopedImplThread;
123
124     const IntSize tileSize(10, 10);
125     const int numTilesX = 2;
126     const int numTilesY = 2;
127     const IntSize layerSize(tileSize.width() * numTilesX, tileSize.height() * numTilesY);
128
129     OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
130     OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
131
132     // No checkerboarding
133     {
134         MockCCQuadCuller quadCuller;
135         bool usedCheckerboard = false;
136         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
137         EXPECT_EQ(quadCuller.quadList().size(), 4u);
138         EXPECT_FALSE(usedCheckerboard);
139
140         for (size_t i = 0; i < quadCuller.quadList().size(); ++i)
141             EXPECT_EQ(quadCuller.quadList()[i]->material(), CCDrawQuad::TiledContent);
142     }
143
144     for (int i = 0; i < numTilesX; ++i)
145         for (int j = 0; j < numTilesY; ++j)
146             layer->pushTileProperties(i, j, static_cast<Platform3DObject>(0), IntRect());
147
148     // All checkerboarding
149     {
150         MockCCQuadCuller quadCuller;
151         bool usedCheckerboard = false;
152         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
153         EXPECT_TRUE(usedCheckerboard);
154         EXPECT_EQ(quadCuller.quadList().size(), 4u);
155         for (size_t i = 0; i < quadCuller.quadList().size(); ++i)
156             EXPECT_EQ(quadCuller.quadList()[i]->material(), CCDrawQuad::SolidColor);
157     }
158 }
159
160 static PassOwnPtr<CCSharedQuadState> getQuads(CCQuadList& quads, IntSize tileSize, const IntSize& layerSize, CCLayerTilingData::BorderTexelOption borderTexelOption, const IntRect& visibleLayerRect)
161 {
162     OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, borderTexelOption);
163     layer->setVisibleLayerRect(visibleLayerRect);
164     layer->setBounds(layerSize);
165
166     MockCCQuadCuller quadCuller(quads);
167     OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
168     bool usedCheckerboard = false;
169     layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
170     return sharedQuadState.release(); // The shared data must be owned as long as the quad list exists.
171 }
172
173 // Test with both border texels and without.
174 #define WITH_AND_WITHOUT_BORDER_TEST(testFixtureName)       \
175     TEST(CCTiledLayerImplTest, testFixtureName##NoBorders)  \
176     {                                                       \
177         testFixtureName(CCLayerTilingData::NoBorderTexels); \
178     }                                                       \
179     TEST(CCTiledLayerImplTest, testFixtureName##HasBorders) \
180     {                                                       \
181         testFixtureName(CCLayerTilingData::HasBorderTexels);\
182     }
183
184 static void coverageVisibleRectOnTileBoundaries(CCLayerTilingData::BorderTexelOption borders)
185 {
186     DebugScopedSetImplThread scopedImplThread;
187
188     IntSize layerSize(1000, 1000);
189     CCQuadList quads;
190     OwnPtr<CCSharedQuadState> sharedState;
191     sharedState = getQuads(quads, IntSize(100, 100), layerSize, borders, IntRect(IntPoint(), layerSize));
192     verifyQuadsExactlyCoverRect(quads, IntRect(IntPoint(), layerSize));
193 }
194 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectOnTileBoundaries);
195
196 static void coverageVisibleRectIntersectsTiles(CCLayerTilingData::BorderTexelOption borders)
197 {
198     DebugScopedSetImplThread scopedImplThread;
199
200     // This rect intersects the middle 3x3 of the 5x5 tiles.
201     IntPoint topLeft(65, 73);
202     IntPoint bottomRight(182, 198);
203     IntRect visibleLayerRect(topLeft, bottomRight - topLeft);
204
205     IntSize layerSize(250, 250);
206     CCQuadList quads;
207     OwnPtr<CCSharedQuadState> sharedState;
208     sharedState = getQuads(quads, IntSize(50, 50), IntSize(250, 250), CCLayerTilingData::NoBorderTexels, visibleLayerRect);
209     verifyQuadsExactlyCoverRect(quads, visibleLayerRect);
210 }
211 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsTiles);
212
213 static void coverageVisibleRectIntersectsBounds(CCLayerTilingData::BorderTexelOption borders)
214 {
215     DebugScopedSetImplThread scopedImplThread;
216
217     IntSize layerSize(220, 210);
218     IntRect visibleLayerRect(IntPoint(), layerSize);
219     CCQuadList quads;
220     OwnPtr<CCSharedQuadState> sharedState;
221     sharedState = getQuads(quads, IntSize(100, 100), layerSize, CCLayerTilingData::NoBorderTexels, visibleLayerRect);
222     verifyQuadsExactlyCoverRect(quads, visibleLayerRect);
223 }
224 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsBounds);
225
226 TEST(CCTiledLayerImplTest, textureInfoForLayerNoBorders)
227 {
228     DebugScopedSetImplThread scopedImplThread;
229
230     IntSize tileSize(50, 50);
231     IntSize layerSize(250, 250);
232     CCQuadList quads;
233     OwnPtr<CCSharedQuadState> sharedState;
234     sharedState = getQuads(quads, tileSize, layerSize, CCLayerTilingData::NoBorderTexels, IntRect(IntPoint(), layerSize));
235
236     for (size_t i = 0; i < quads.size(); ++i) {
237         ASSERT_EQ(quads[i]->material(), CCDrawQuad::TiledContent) << quadString << i;
238         CCTileDrawQuad* quad = static_cast<CCTileDrawQuad*>(quads[i].get());
239
240         EXPECT_NE(quad->textureId(), 0u) << quadString << i;
241         EXPECT_EQ(quad->textureOffset(), IntPoint()) << quadString << i;
242         EXPECT_EQ(quad->textureSize(), tileSize) << quadString << i;
243         EXPECT_EQ(IntRect(0, 0, 1, 1), quad->opaqueRect()) << quadString << i;
244     }
245 }
246
247 TEST(CCTiledLayerImplTest, tileOpaqueRectForLayerNoBorders)
248 {
249     DebugScopedSetImplThread scopedImplThread;
250
251     IntSize tileSize(50, 50);
252     IntSize layerSize(250, 250);
253     CCQuadList quads;
254     OwnPtr<CCSharedQuadState> sharedState;
255     sharedState = getQuads(quads, tileSize, layerSize, CCLayerTilingData::NoBorderTexels, IntRect(IntPoint(), layerSize));
256
257     for (size_t i = 0; i < quads.size(); ++i) {
258         ASSERT_EQ(quads[i]->material(), CCDrawQuad::TiledContent) << quadString << i;
259         CCTileDrawQuad* quad = static_cast<CCTileDrawQuad*>(quads[i].get());
260
261         EXPECT_EQ(IntRect(0, 0, 1, 1), quad->opaqueRect()) << quadString << i;
262     }
263 }
264
265 TEST(CCTiledLayerImplTest, backgroundCoversViewport)
266 {
267     DebugScopedSetImplThread scopedImplThread;
268
269     const IntSize tileSize(10, 10);
270     const int numTilesX = 2;
271     const int numTilesY = 2;
272     const unsigned numTiles = numTilesX * numTilesY;
273     const IntSize layerSize(tileSize.width() * numTilesX, tileSize.height() * numTilesY);
274     OwnPtr<CCTiledLayerImpl> layer = createLayer(tileSize, layerSize, CCLayerTilingData::NoBorderTexels);
275     layer->setBackgroundColor(Color::gray);
276     layer->setBackgroundCoversViewport(true);
277
278     // No gutter rects
279     {
280         IntRect clipRect = IntRect(IntPoint(), layerSize);
281         layer->setClipRect(clipRect);
282         layer->setVisibleLayerRect(IntRect(IntPoint(), layerSize));
283
284         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
285
286         MockCCQuadCuller quadCuller;
287         bool usedCheckerboard = false;
288         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
289         EXPECT_EQ(quadCuller.quadList().size(), numTiles);
290
291         for (size_t i = 0; i < quadCuller.quadList().size(); ++i)
292             EXPECT_EQ(quadCuller.quadList()[i]->material(), CCDrawQuad::TiledContent);
293     }
294
295     // Empty visible content area (fullscreen gutter rect)
296     {
297         IntRect clipRect = IntRect(100, 100, 100, 100);
298         layer->setClipRect(clipRect);
299         layer->setVisibleLayerRect(IntRect());
300
301         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
302         MockCCQuadCuller quadCuller;
303         bool usedCheckerboard = false;
304         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
305
306         for (size_t i = 0; i < quadCuller.quadList().size(); ++i)
307             EXPECT_EQ(quadCuller.quadList()[i]->material(), CCDrawQuad::SolidColor);
308
309         verifyQuadsExactlyCoverRect(quadCuller.quadList(), clipRect);
310     }
311
312     // Content area in middle of clip rect (four surrounding gutter rects)
313     {
314         IntRect clipRect = IntRect(-50, -50, 100, 100);
315         layer->setClipRect(clipRect);
316         layer->setVisibleLayerRect(IntRect(IntPoint(), layerSize));
317
318         OwnPtr<CCSharedQuadState> sharedQuadState = layer->createSharedQuadState();
319         MockCCQuadCuller quadCuller;
320         bool usedCheckerboard = false;
321         layer->appendQuads(quadCuller, sharedQuadState.get(), usedCheckerboard);
322
323         unsigned numContentTiles = 0, numGutterTiles = 0;
324         for (size_t i = 0; i < quadCuller.quadList().size(); ++i) {
325             if (quadCuller.quadList()[i]->material() == CCDrawQuad::TiledContent)
326                 numContentTiles++;
327             else if (quadCuller.quadList()[i]->material() == CCDrawQuad::SolidColor)
328                 numGutterTiles++;
329         }
330         EXPECT_EQ(numContentTiles, numTiles);
331         EXPECT_GE(numGutterTiles, 4u);
332
333         verifyQuadsExactlyCoverRect(quadCuller.quadList(), clipRect);
334     }
335 }
336
337 } // namespace