1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/tiled_layer_impl.h"
7 #include "cc/append_quads_data.h"
8 #include "cc/layer_tiling_data.h"
9 #include "cc/single_thread_proxy.h"
10 #include "cc/test/layer_test_common.h"
11 #include "cc/test/mock_quad_culler.h"
12 #include "cc/tile_draw_quad.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using namespace LayerTestCommon
;
21 // Create a default tiled layer with textures for all tiles and a default
22 // visibility of the entire layer size.
23 static scoped_ptr
<TiledLayerImpl
> createLayer(const gfx::Size
& tileSize
, const gfx::Size
& layerSize
, LayerTilingData::BorderTexelOption borderTexels
)
25 scoped_ptr
<TiledLayerImpl
> layer
= TiledLayerImpl::create(1);
26 scoped_ptr
<LayerTilingData
> tiler
= LayerTilingData::create(tileSize
, borderTexels
);
27 tiler
->setBounds(layerSize
);
28 layer
->setTilingData(*tiler
);
29 layer
->setSkipsDraw(false);
30 layer
->drawProperties().visible_content_rect
= gfx::Rect(gfx::Point(), layerSize
);
31 layer
->drawProperties().opacity
= 1;
32 layer
->setBounds(layerSize
);
33 layer
->setContentBounds(layerSize
);
34 layer
->createRenderSurface();
35 layer
->drawProperties().render_target
= layer
.get();
37 ResourceProvider::ResourceId resourceId
= 1;
38 for (int i
= 0; i
< tiler
->numTilesX(); ++i
)
39 for (int j
= 0; j
< tiler
->numTilesY(); ++j
)
40 layer
->pushTileProperties(i
, j
, resourceId
++, gfx::Rect(0, 0, 1, 1), false);
45 TEST(TiledLayerImplTest
, emptyQuadList
)
47 const gfx::Size
tileSize(90, 90);
48 const int numTilesX
= 8;
49 const int numTilesY
= 4;
50 const gfx::Size
layerSize(tileSize
.width() * numTilesX
, tileSize
.height() * numTilesY
);
52 // Verify default layer does creates quads
54 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, LayerTilingData::NoBorderTexels
);
55 MockQuadCuller quadCuller
;
57 layer
->appendQuads(quadCuller
, data
);
58 const unsigned numTiles
= numTilesX
* numTilesY
;
59 EXPECT_EQ(quadCuller
.quadList().size(), numTiles
);
62 // Layer with empty visible layer rect produces no quads
64 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, LayerTilingData::NoBorderTexels
);
65 layer
->drawProperties().visible_content_rect
= gfx::Rect();
67 MockQuadCuller quadCuller
;
69 layer
->appendQuads(quadCuller
, data
);
70 EXPECT_EQ(quadCuller
.quadList().size(), 0u);
73 // Layer with non-intersecting visible layer rect produces no quads
75 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, LayerTilingData::NoBorderTexels
);
77 gfx::Rect
outsideBounds(gfx::Point(-100, -100), gfx::Size(50, 50));
78 layer
->drawProperties().visible_content_rect
= outsideBounds
;
80 MockQuadCuller quadCuller
;
82 layer
->appendQuads(quadCuller
, data
);
83 EXPECT_EQ(quadCuller
.quadList().size(), 0u);
86 // Layer with skips draw produces no quads
88 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, LayerTilingData::NoBorderTexels
);
89 layer
->setSkipsDraw(true);
91 MockQuadCuller quadCuller
;
93 layer
->appendQuads(quadCuller
, data
);
94 EXPECT_EQ(quadCuller
.quadList().size(), 0u);
98 TEST(TiledLayerImplTest
, checkerboarding
)
100 const gfx::Size
tileSize(10, 10);
101 const int numTilesX
= 2;
102 const int numTilesY
= 2;
103 const gfx::Size
layerSize(tileSize
.width() * numTilesX
, tileSize
.height() * numTilesY
);
105 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, LayerTilingData::NoBorderTexels
);
107 // No checkerboarding
109 MockQuadCuller quadCuller
;
110 AppendQuadsData data
;
111 layer
->appendQuads(quadCuller
, data
);
112 EXPECT_EQ(quadCuller
.quadList().size(), 4u);
113 EXPECT_FALSE(data
.hadMissingTiles
);
115 for (size_t i
= 0; i
< quadCuller
.quadList().size(); ++i
)
116 EXPECT_EQ(quadCuller
.quadList()[i
]->material
, DrawQuad::TILED_CONTENT
);
119 for (int i
= 0; i
< numTilesX
; ++i
)
120 for (int j
= 0; j
< numTilesY
; ++j
)
121 layer
->pushTileProperties(i
, j
, 0, gfx::Rect(), false);
123 // All checkerboarding
125 MockQuadCuller quadCuller
;
126 AppendQuadsData data
;
127 layer
->appendQuads(quadCuller
, data
);
128 EXPECT_TRUE(data
.hadMissingTiles
);
129 EXPECT_EQ(quadCuller
.quadList().size(), 4u);
130 for (size_t i
= 0; i
< quadCuller
.quadList().size(); ++i
)
131 EXPECT_NE(quadCuller
.quadList()[i
]->material
, DrawQuad::TILED_CONTENT
);
135 static void getQuads(QuadList
& quads
, SharedQuadStateList
& sharedStates
, gfx::Size tileSize
, const gfx::Size
& layerSize
, LayerTilingData::BorderTexelOption borderTexelOption
, const gfx::Rect
& visibleContentRect
)
137 scoped_ptr
<TiledLayerImpl
> layer
= createLayer(tileSize
, layerSize
, borderTexelOption
);
138 layer
->drawProperties().visible_content_rect
= visibleContentRect
;
139 layer
->setBounds(layerSize
);
141 MockQuadCuller
quadCuller(quads
, sharedStates
);
142 AppendQuadsData data
;
143 layer
->appendQuads(quadCuller
, data
);
146 // Test with both border texels and without.
147 #define WITH_AND_WITHOUT_BORDER_TEST(testFixtureName) \
148 TEST(TiledLayerImplTest, testFixtureName##NoBorders) \
150 testFixtureName(LayerTilingData::NoBorderTexels); \
152 TEST(TiledLayerImplTest, testFixtureName##HasBorders) \
154 testFixtureName(LayerTilingData::HasBorderTexels);\
157 static void coverageVisibleRectOnTileBoundaries(LayerTilingData::BorderTexelOption borders
)
159 gfx::Size
layerSize(1000, 1000);
161 SharedQuadStateList sharedStates
;
162 getQuads(quads
, sharedStates
, gfx::Size(100, 100), layerSize
, borders
, gfx::Rect(gfx::Point(), layerSize
));
163 verifyQuadsExactlyCoverRect(quads
, gfx::Rect(gfx::Point(), layerSize
));
165 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectOnTileBoundaries
);
167 static void coverageVisibleRectIntersectsTiles(LayerTilingData::BorderTexelOption borders
)
169 // This rect intersects the middle 3x3 of the 5x5 tiles.
170 gfx::Point
topLeft(65, 73);
171 gfx::Point
bottomRight(182, 198);
172 gfx::Rect visibleContentRect
= gfx::BoundingRect(topLeft
, bottomRight
);
174 gfx::Size
layerSize(250, 250);
176 SharedQuadStateList sharedStates
;
177 getQuads(quads
, sharedStates
, gfx::Size(50, 50), gfx::Size(250, 250), LayerTilingData::NoBorderTexels
, visibleContentRect
);
178 verifyQuadsExactlyCoverRect(quads
, visibleContentRect
);
180 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsTiles
);
182 static void coverageVisibleRectIntersectsBounds(LayerTilingData::BorderTexelOption borders
)
184 gfx::Size
layerSize(220, 210);
185 gfx::Rect
visibleContentRect(gfx::Point(), layerSize
);
187 SharedQuadStateList sharedStates
;
188 getQuads(quads
, sharedStates
, gfx::Size(100, 100), layerSize
, LayerTilingData::NoBorderTexels
, visibleContentRect
);
189 verifyQuadsExactlyCoverRect(quads
, visibleContentRect
);
191 WITH_AND_WITHOUT_BORDER_TEST(coverageVisibleRectIntersectsBounds
);
193 TEST(TiledLayerImplTest
, textureInfoForLayerNoBorders
)
195 gfx::Size
tileSize(50, 50);
196 gfx::Size
layerSize(250, 250);
198 SharedQuadStateList sharedStates
;
199 getQuads(quads
, sharedStates
, tileSize
, layerSize
, LayerTilingData::NoBorderTexels
, gfx::Rect(gfx::Point(), layerSize
));
201 for (size_t i
= 0; i
< quads
.size(); ++i
) {
202 const TileDrawQuad
* quad
= TileDrawQuad::MaterialCast(quads
[i
]);
204 EXPECT_NE(0u, quad
->resource_id
) << quadString
<< i
;
205 EXPECT_EQ(gfx::RectF(gfx::PointF(), tileSize
), quad
->tex_coord_rect
) << quadString
<< i
;
206 EXPECT_EQ(tileSize
, quad
->texture_size
) << quadString
<< i
;
207 EXPECT_EQ(gfx::Rect(0, 0, 1, 1), quad
->opaque_rect
) << quadString
<< i
;