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/layers/tiled_layer_impl.h"
7 #include "cc/layers/append_quads_data.h"
8 #include "cc/quads/tile_draw_quad.h"
9 #include "cc/resources/layer_tiling_data.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/layer_test_common.h"
13 #include "cc/test/mock_quad_culler.h"
14 #include "cc/trees/single_thread_proxy.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 class TiledLayerImplTest
: public testing::Test
{
23 TiledLayerImplTest() : host_impl_(&proxy_
, &shared_bitmap_manager_
) {}
25 scoped_ptr
<TiledLayerImpl
> CreateLayerNoTiles(
26 const gfx::Size
& tile_size
,
27 const gfx::Size
& layer_size
,
28 LayerTilingData::BorderTexelOption border_texels
) {
29 scoped_ptr
<TiledLayerImpl
> layer
=
30 TiledLayerImpl::Create(host_impl_
.active_tree(), 1);
31 scoped_ptr
<LayerTilingData
> tiler
=
32 LayerTilingData::Create(tile_size
, border_texels
);
33 tiler
->SetTilingRect(gfx::Rect(layer_size
));
34 layer
->SetTilingData(*tiler
);
35 layer
->set_skips_draw(false);
36 layer
->draw_properties().visible_content_rect
=
37 gfx::Rect(layer_size
);
38 layer
->draw_properties().opacity
= 1;
39 layer
->SetBounds(layer_size
);
40 layer
->SetContentBounds(layer_size
);
41 layer
->CreateRenderSurface();
42 layer
->draw_properties().render_target
= layer
.get();
46 // Create a default tiled layer with textures for all tiles and a default
47 // visibility of the entire layer size.
48 scoped_ptr
<TiledLayerImpl
> CreateLayer(
49 const gfx::Size
& tile_size
,
50 const gfx::Size
& layer_size
,
51 LayerTilingData::BorderTexelOption border_texels
) {
52 scoped_ptr
<TiledLayerImpl
> layer
=
53 CreateLayerNoTiles(tile_size
, layer_size
, border_texels
);
55 ResourceProvider::ResourceId resource_id
= 1;
56 for (int i
= 0; i
< layer
->TilingForTesting()->num_tiles_x(); ++i
) {
57 for (int j
= 0; j
< layer
->TilingForTesting()->num_tiles_y(); ++j
) {
58 gfx::Rect
opaque_rect(
59 layer
->TilingForTesting()->tile_bounds(i
, j
).origin(),
61 layer
->PushTileProperties(i
, j
, resource_id
++, opaque_rect
, false);
68 void GetQuads(RenderPass
* render_pass
,
69 const gfx::Size
& tile_size
,
70 const gfx::Size
& layer_size
,
71 LayerTilingData::BorderTexelOption border_texel_option
,
72 const gfx::Rect
& visible_content_rect
) {
73 scoped_ptr
<TiledLayerImpl
> layer
=
74 CreateLayer(tile_size
, layer_size
, border_texel_option
);
75 layer
->draw_properties().visible_content_rect
= visible_content_rect
;
76 layer
->SetBounds(layer_size
);
78 MockQuadCuller
quad_culler(render_pass
);
80 layer
->AppendQuads(&quad_culler
, &data
);
85 TestSharedBitmapManager shared_bitmap_manager_
;
86 FakeLayerTreeHostImpl host_impl_
;
89 TEST_F(TiledLayerImplTest
, EmptyQuadList
) {
90 gfx::Size
tile_size(90, 90);
93 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
94 tile_size
.height() * num_tiles_y
);
96 // Verify default layer does creates quads
98 scoped_ptr
<TiledLayerImpl
> layer
=
99 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
100 MockQuadCuller quad_culler
;
101 AppendQuadsData data
;
102 EXPECT_TRUE(layer
->WillDraw(DRAW_MODE_HARDWARE
, NULL
));
103 layer
->AppendQuads(&quad_culler
, &data
);
104 layer
->DidDraw(NULL
);
105 unsigned num_tiles
= num_tiles_x
* num_tiles_y
;
106 EXPECT_EQ(quad_culler
.quad_list().size(), num_tiles
);
109 // Layer with empty visible layer rect produces no quads
111 scoped_ptr
<TiledLayerImpl
> layer
=
112 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
113 layer
->draw_properties().visible_content_rect
= gfx::Rect();
115 MockQuadCuller quad_culler
;
116 EXPECT_FALSE(layer
->WillDraw(DRAW_MODE_HARDWARE
, NULL
));
119 // Layer with non-intersecting visible layer rect produces no quads
121 scoped_ptr
<TiledLayerImpl
> layer
=
122 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
124 gfx::Rect
outside_bounds(-100, -100, 50, 50);
125 layer
->draw_properties().visible_content_rect
= outside_bounds
;
127 MockQuadCuller quad_culler
;
128 AppendQuadsData data
;
129 EXPECT_TRUE(layer
->WillDraw(DRAW_MODE_HARDWARE
, NULL
));
130 layer
->AppendQuads(&quad_culler
, &data
);
131 layer
->DidDraw(NULL
);
132 EXPECT_EQ(quad_culler
.quad_list().size(), 0u);
135 // Layer with skips draw produces no quads
137 scoped_ptr
<TiledLayerImpl
> layer
=
138 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
139 layer
->set_skips_draw(true);
141 MockQuadCuller quad_culler
;
142 AppendQuadsData data
;
143 layer
->AppendQuads(&quad_culler
, &data
);
144 EXPECT_EQ(quad_culler
.quad_list().size(), 0u);
148 TEST_F(TiledLayerImplTest
, Checkerboarding
) {
149 gfx::Size
tile_size(10, 10);
152 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
153 tile_size
.height() * num_tiles_y
);
155 scoped_ptr
<TiledLayerImpl
> layer
=
156 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
158 // No checkerboarding
160 MockQuadCuller quad_culler
;
161 AppendQuadsData data
;
162 layer
->AppendQuads(&quad_culler
, &data
);
163 EXPECT_EQ(quad_culler
.quad_list().size(), 4u);
164 EXPECT_EQ(0u, data
.num_missing_tiles
);
166 for (size_t i
= 0; i
< quad_culler
.quad_list().size(); ++i
)
167 EXPECT_EQ(quad_culler
.quad_list()[i
]->material
, DrawQuad::TILED_CONTENT
);
170 for (int i
= 0; i
< num_tiles_x
; ++i
)
171 for (int j
= 0; j
< num_tiles_y
; ++j
)
172 layer
->PushTileProperties(i
, j
, 0, gfx::Rect(), false);
174 // All checkerboarding
176 MockQuadCuller quad_culler
;
177 AppendQuadsData data
;
178 layer
->AppendQuads(&quad_culler
, &data
);
179 EXPECT_LT(0u, data
.num_missing_tiles
);
180 EXPECT_EQ(quad_culler
.quad_list().size(), 4u);
181 for (size_t i
= 0; i
< quad_culler
.quad_list().size(); ++i
)
182 EXPECT_NE(quad_culler
.quad_list()[i
]->material
, DrawQuad::TILED_CONTENT
);
186 // Test with both border texels and without.
187 #define WITH_AND_WITHOUT_BORDER_TEST(text_fixture_name) \
188 TEST_F(TiledLayerImplBorderTest, text_fixture_name##NoBorders) { \
189 text_fixture_name(LayerTilingData::NO_BORDER_TEXELS); \
191 TEST_F(TiledLayerImplBorderTest, text_fixture_name##HasBorders) { \
192 text_fixture_name(LayerTilingData::HAS_BORDER_TEXELS); \
195 class TiledLayerImplBorderTest
: public TiledLayerImplTest
{
197 void CoverageVisibleRectOnTileBoundaries(
198 LayerTilingData::BorderTexelOption borders
) {
199 gfx::Size
layer_size(1000, 1000);
200 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
201 GetQuads(render_pass
.get(),
205 gfx::Rect(layer_size
));
206 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
207 gfx::Rect(layer_size
));
210 void CoverageVisibleRectIntersectsTiles(
211 LayerTilingData::BorderTexelOption borders
) {
212 // This rect intersects the middle 3x3 of the 5x5 tiles.
213 gfx::Point
top_left(65, 73);
214 gfx::Point
bottom_right(182, 198);
215 gfx::Rect visible_content_rect
= gfx::BoundingRect(top_left
, bottom_right
);
217 gfx::Size
layer_size(250, 250);
218 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
219 GetQuads(render_pass
.get(),
222 LayerTilingData::NO_BORDER_TEXELS
,
223 visible_content_rect
);
224 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
225 visible_content_rect
);
228 void CoverageVisibleRectIntersectsBounds(
229 LayerTilingData::BorderTexelOption borders
) {
230 gfx::Size
layer_size(220, 210);
231 gfx::Rect
visible_content_rect(layer_size
);
232 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
233 GetQuads(render_pass
.get(),
236 LayerTilingData::NO_BORDER_TEXELS
,
237 visible_content_rect
);
238 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
239 visible_content_rect
);
242 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectOnTileBoundaries
);
244 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectIntersectsTiles
);
246 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectIntersectsBounds
);
248 TEST_F(TiledLayerImplTest
, TextureInfoForLayerNoBorders
) {
249 gfx::Size
tile_size(50, 50);
250 gfx::Size
layer_size(250, 250);
251 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
252 GetQuads(render_pass
.get(),
255 LayerTilingData::NO_BORDER_TEXELS
,
256 gfx::Rect(layer_size
));
258 for (size_t i
= 0; i
< render_pass
->quad_list
.size(); ++i
) {
259 const TileDrawQuad
* quad
=
260 TileDrawQuad::MaterialCast(render_pass
->quad_list
[i
]);
262 EXPECT_NE(0u, quad
->resource_id
) << LayerTestCommon::quad_string
<< i
;
263 EXPECT_EQ(gfx::RectF(gfx::PointF(), tile_size
), quad
->tex_coord_rect
)
264 << LayerTestCommon::quad_string
<< i
;
265 EXPECT_EQ(tile_size
, quad
->texture_size
) << LayerTestCommon::quad_string
267 EXPECT_EQ(gfx::Size(1, 1).ToString(), quad
->opaque_rect
.size().ToString())
268 << LayerTestCommon::quad_string
<< i
;
272 TEST_F(TiledLayerImplTest
, GPUMemoryUsage
) {
273 gfx::Size
tile_size(20, 30);
274 int num_tiles_x
= 12;
275 int num_tiles_y
= 32;
276 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
277 tile_size
.height() * num_tiles_y
);
279 scoped_ptr
<TiledLayerImpl
> layer
= CreateLayerNoTiles(
280 tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
282 EXPECT_EQ(layer
->GPUMemoryUsageInBytes(), 0u);
284 ResourceProvider::ResourceId resource_id
= 1;
285 layer
->PushTileProperties(0, 1, resource_id
++, gfx::Rect(0, 0, 1, 1), false);
286 layer
->PushTileProperties(2, 3, resource_id
++, gfx::Rect(0, 0, 1, 1), false);
287 layer
->PushTileProperties(2, 0, resource_id
++, gfx::Rect(0, 0, 1, 1), false);
290 layer
->GPUMemoryUsageInBytes(),
291 static_cast<size_t>(3 * 4 * tile_size
.width() * tile_size
.height()));
293 ResourceProvider::ResourceId
empty_resource(0);
294 layer
->PushTileProperties(0, 1, empty_resource
, gfx::Rect(0, 0, 1, 1), false);
295 layer
->PushTileProperties(2, 3, empty_resource
, gfx::Rect(0, 0, 1, 1), false);
296 layer
->PushTileProperties(2, 0, empty_resource
, gfx::Rect(0, 0, 1, 1), false);
298 EXPECT_EQ(layer
->GPUMemoryUsageInBytes(), 0u);
301 TEST_F(TiledLayerImplTest
, EmptyMask
) {
302 gfx::Size
tile_size(20, 20);
303 gfx::Size
layer_size(0, 0);
304 scoped_ptr
<TiledLayerImpl
> layer
=
305 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
307 EXPECT_EQ(0u, layer
->ContentsResourceId());
308 EXPECT_EQ(0, layer
->TilingForTesting()->num_tiles_x());
309 EXPECT_EQ(0, layer
->TilingForTesting()->num_tiles_y());
312 TEST_F(TiledLayerImplTest
, Occlusion
) {
313 gfx::Size
tile_size(100, 100);
314 gfx::Size
layer_bounds(1000, 1000);
315 gfx::Size
viewport_size(1000, 1000);
317 LayerTestCommon::LayerImplTest impl
;
319 TiledLayerImpl
* tiled_layer
= impl
.AddChildToRoot
<TiledLayerImpl
>();
320 tiled_layer
->SetAnchorPoint(gfx::PointF());
321 tiled_layer
->SetBounds(layer_bounds
);
322 tiled_layer
->SetContentBounds(layer_bounds
);
323 tiled_layer
->SetDrawsContent(true);
324 tiled_layer
->set_skips_draw(false);
326 scoped_ptr
<LayerTilingData
> tiler
=
327 LayerTilingData::Create(tile_size
, LayerTilingData::NO_BORDER_TEXELS
);
328 tiler
->SetTilingRect(gfx::Rect(layer_bounds
));
329 tiled_layer
->SetTilingData(*tiler
);
331 ResourceProvider::ResourceId resource_id
= 1;
332 for (int i
= 0; i
< tiled_layer
->TilingForTesting()->num_tiles_x(); ++i
) {
333 for (int j
= 0; j
< tiled_layer
->TilingForTesting()->num_tiles_y(); ++j
)
334 tiled_layer
->PushTileProperties(i
, j
, resource_id
++, gfx::Rect(), false);
337 impl
.CalcDrawProps(viewport_size
);
340 SCOPED_TRACE("No occlusion");
342 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
344 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(),
345 gfx::Rect(layer_bounds
));
346 EXPECT_EQ(100u, impl
.quad_list().size());
350 SCOPED_TRACE("Full occlusion");
351 gfx::Rect
occluded(tiled_layer
->visible_content_rect());
352 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
354 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
355 EXPECT_EQ(impl
.quad_list().size(), 0u);
359 SCOPED_TRACE("Partial occlusion");
360 gfx::Rect
occluded(150, 0, 200, 1000);
361 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
363 size_t partially_occluded_count
= 0;
364 LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
366 gfx::Rect(layer_bounds
),
368 &partially_occluded_count
);
369 // The layer outputs one quad, which is partially occluded.
370 EXPECT_EQ(100u - 10u, impl
.quad_list().size());
371 EXPECT_EQ(10u + 10u, partially_occluded_count
);