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/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/layer_test_common.h"
13 #include "cc/test/test_task_graph_runner.h"
14 #include "cc/tiles/layer_tiling_data.h"
15 #include "cc/trees/single_thread_proxy.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class TiledLayerImplTest
: public testing::Test
{
25 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
) {
26 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d());
29 scoped_ptr
<TiledLayerImpl
> CreateLayerNoTiles(
30 const gfx::Size
& tile_size
,
31 const gfx::Size
& layer_size
,
32 LayerTilingData::BorderTexelOption border_texels
) {
33 scoped_ptr
<TiledLayerImpl
> layer
=
34 TiledLayerImpl::Create(host_impl_
.active_tree(), 1);
35 scoped_ptr
<LayerTilingData
> tiler
=
36 LayerTilingData::Create(tile_size
, border_texels
);
37 tiler
->SetTilingSize(layer_size
);
38 layer
->SetTilingData(*tiler
);
39 layer
->set_skips_draw(false);
40 layer
->draw_properties().visible_content_rect
=
41 gfx::Rect(layer_size
);
42 layer
->draw_properties().opacity
= 1;
43 layer
->SetBounds(layer_size
);
44 layer
->SetContentBounds(layer_size
);
45 layer
->SetHasRenderSurface(true);
46 layer
->draw_properties().render_target
= layer
.get();
50 // Create a default tiled layer with textures for all tiles and a default
51 // visibility of the entire layer size.
52 scoped_ptr
<TiledLayerImpl
> CreateLayer(
53 const gfx::Size
& tile_size
,
54 const gfx::Size
& layer_size
,
55 LayerTilingData::BorderTexelOption border_texels
) {
56 scoped_ptr
<TiledLayerImpl
> layer
=
57 CreateLayerNoTiles(tile_size
, layer_size
, border_texels
);
59 for (int i
= 0; i
< layer
->TilingForTesting()->num_tiles_x(); ++i
) {
60 for (int j
= 0; j
< layer
->TilingForTesting()->num_tiles_y(); ++j
) {
61 ResourceId resource_id
= host_impl_
.resource_provider()->CreateResource(
62 gfx::Size(1, 1), GL_CLAMP_TO_EDGE
,
63 ResourceProvider::TEXTURE_HINT_IMMUTABLE
, RGBA_8888
);
64 layer
->PushTileProperties(i
, j
, resource_id
, false);
71 void GetQuads(RenderPass
* render_pass
,
72 const gfx::Size
& tile_size
,
73 const gfx::Size
& layer_size
,
74 LayerTilingData::BorderTexelOption border_texel_option
,
75 const gfx::Rect
& visible_content_rect
) {
76 scoped_ptr
<TiledLayerImpl
> layer
=
77 CreateLayer(tile_size
, layer_size
, border_texel_option
);
78 layer
->draw_properties().visible_content_rect
= visible_content_rect
;
79 layer
->SetBounds(layer_size
);
82 layer
->AppendQuads(render_pass
, &data
);
87 TestSharedBitmapManager shared_bitmap_manager_
;
88 TestTaskGraphRunner task_graph_runner_
;
89 FakeLayerTreeHostImpl host_impl_
;
92 TEST_F(TiledLayerImplTest
, EmptyQuadList
) {
93 gfx::Size
tile_size(90, 90);
96 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
97 tile_size
.height() * num_tiles_y
);
99 // Verify default layer does creates quads
101 scoped_ptr
<TiledLayerImpl
> layer
=
102 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
103 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
105 AppendQuadsData data
;
106 EXPECT_TRUE(layer
->WillDraw(DRAW_MODE_HARDWARE
, nullptr));
107 layer
->AppendQuads(render_pass
.get(), &data
);
108 layer
->DidDraw(nullptr);
109 unsigned num_tiles
= num_tiles_x
* num_tiles_y
;
110 EXPECT_EQ(render_pass
->quad_list
.size(), num_tiles
);
113 // Layer with empty visible layer rect produces no quads
115 scoped_ptr
<TiledLayerImpl
> layer
=
116 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
117 layer
->draw_properties().visible_content_rect
= gfx::Rect();
119 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
121 EXPECT_FALSE(layer
->WillDraw(DRAW_MODE_HARDWARE
, nullptr));
124 // Layer with non-intersecting visible layer rect produces no quads
126 scoped_ptr
<TiledLayerImpl
> layer
=
127 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
129 gfx::Rect
outside_bounds(-100, -100, 50, 50);
130 layer
->draw_properties().visible_content_rect
= outside_bounds
;
132 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
134 AppendQuadsData data
;
135 EXPECT_TRUE(layer
->WillDraw(DRAW_MODE_HARDWARE
, nullptr));
136 layer
->AppendQuads(render_pass
.get(), &data
);
137 layer
->DidDraw(nullptr);
138 EXPECT_EQ(render_pass
->quad_list
.size(), 0u);
141 // Layer with skips draw produces no quads
143 scoped_ptr
<TiledLayerImpl
> layer
=
144 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
145 layer
->set_skips_draw(true);
147 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
149 AppendQuadsData data
;
150 layer
->AppendQuads(render_pass
.get(), &data
);
151 EXPECT_EQ(render_pass
->quad_list
.size(), 0u);
155 TEST_F(TiledLayerImplTest
, Checkerboarding
) {
156 gfx::Size
tile_size(10, 10);
159 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
160 tile_size
.height() * num_tiles_y
);
162 scoped_ptr
<TiledLayerImpl
> layer
=
163 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
165 // No checkerboarding
167 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
169 AppendQuadsData data
;
170 layer
->AppendQuads(render_pass
.get(), &data
);
171 EXPECT_EQ(render_pass
->quad_list
.size(), 4u);
172 EXPECT_EQ(0u, data
.num_missing_tiles
);
174 for (const auto& quad
: render_pass
->quad_list
)
175 EXPECT_EQ(quad
->material
, DrawQuad::TILED_CONTENT
);
178 for (int i
= 0; i
< num_tiles_x
; ++i
)
179 for (int j
= 0; j
< num_tiles_y
; ++j
)
180 layer
->PushTileProperties(i
, j
, 0, false);
182 // All checkerboarding
184 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
186 AppendQuadsData data
;
187 layer
->AppendQuads(render_pass
.get(), &data
);
188 EXPECT_LT(0u, data
.num_missing_tiles
);
189 EXPECT_EQ(render_pass
->quad_list
.size(), 4u);
190 for (const auto& quad
: render_pass
->quad_list
)
191 EXPECT_NE(quad
->material
, DrawQuad::TILED_CONTENT
);
195 // Test with both border texels and without.
196 #define WITH_AND_WITHOUT_BORDER_TEST(text_fixture_name) \
197 TEST_F(TiledLayerImplBorderTest, text_fixture_name##NoBorders) { \
198 text_fixture_name(LayerTilingData::NO_BORDER_TEXELS); \
200 TEST_F(TiledLayerImplBorderTest, text_fixture_name##HasBorders) { \
201 text_fixture_name(LayerTilingData::HAS_BORDER_TEXELS); \
204 class TiledLayerImplBorderTest
: public TiledLayerImplTest
{
206 void CoverageVisibleRectOnTileBoundaries(
207 LayerTilingData::BorderTexelOption borders
) {
208 gfx::Size
layer_size(1000, 1000);
209 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
210 GetQuads(render_pass
.get(),
214 gfx::Rect(layer_size
));
215 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
216 gfx::Rect(layer_size
));
219 void CoverageVisibleRectIntersectsTiles(
220 LayerTilingData::BorderTexelOption borders
) {
221 // This rect intersects the middle 3x3 of the 5x5 tiles.
222 gfx::Point
top_left(65, 73);
223 gfx::Point
bottom_right(182, 198);
224 gfx::Rect visible_content_rect
= gfx::BoundingRect(top_left
, bottom_right
);
226 gfx::Size
layer_size(250, 250);
227 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
228 GetQuads(render_pass
.get(),
231 LayerTilingData::NO_BORDER_TEXELS
,
232 visible_content_rect
);
233 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
234 visible_content_rect
);
237 void CoverageVisibleRectIntersectsBounds(
238 LayerTilingData::BorderTexelOption borders
) {
239 gfx::Size
layer_size(220, 210);
240 gfx::Rect
visible_content_rect(layer_size
);
241 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
242 GetQuads(render_pass
.get(),
245 LayerTilingData::NO_BORDER_TEXELS
,
246 visible_content_rect
);
247 LayerTestCommon::VerifyQuadsExactlyCoverRect(render_pass
->quad_list
,
248 visible_content_rect
);
251 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectOnTileBoundaries
);
253 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectIntersectsTiles
);
255 WITH_AND_WITHOUT_BORDER_TEST(CoverageVisibleRectIntersectsBounds
);
257 TEST_F(TiledLayerImplTest
, TextureInfoForLayerNoBorders
) {
258 gfx::Size
tile_size(50, 50);
259 gfx::Size
layer_size(250, 250);
260 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
261 GetQuads(render_pass
.get(),
264 LayerTilingData::NO_BORDER_TEXELS
,
265 gfx::Rect(layer_size
));
267 for (auto iter
= render_pass
->quad_list
.cbegin();
268 iter
!= render_pass
->quad_list
.cend();
270 const TileDrawQuad
* quad
= TileDrawQuad::MaterialCast(*iter
);
272 EXPECT_NE(0u, quad
->resource_id
) << LayerTestCommon::quad_string
274 EXPECT_EQ(gfx::RectF(gfx::PointF(), tile_size
), quad
->tex_coord_rect
)
275 << LayerTestCommon::quad_string
<< iter
.index();
276 EXPECT_EQ(tile_size
, quad
->texture_size
) << LayerTestCommon::quad_string
281 TEST_F(TiledLayerImplTest
, GPUMemoryUsage
) {
282 gfx::Size
tile_size(20, 30);
283 int num_tiles_x
= 12;
284 int num_tiles_y
= 32;
285 gfx::Size
layer_size(tile_size
.width() * num_tiles_x
,
286 tile_size
.height() * num_tiles_y
);
288 scoped_ptr
<TiledLayerImpl
> layer
= CreateLayerNoTiles(
289 tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
291 EXPECT_EQ(layer
->GPUMemoryUsageInBytes(), 0u);
293 ResourceId resource_id
= 1;
294 layer
->PushTileProperties(0, 1, resource_id
++, false);
295 layer
->PushTileProperties(2, 3, resource_id
++, false);
296 layer
->PushTileProperties(2, 0, resource_id
++, false);
299 layer
->GPUMemoryUsageInBytes(),
300 static_cast<size_t>(3 * 4 * tile_size
.width() * tile_size
.height()));
302 ResourceId
empty_resource(0);
303 layer
->PushTileProperties(0, 1, empty_resource
, false);
304 layer
->PushTileProperties(2, 3, empty_resource
, false);
305 layer
->PushTileProperties(2, 0, empty_resource
, false);
307 EXPECT_EQ(layer
->GPUMemoryUsageInBytes(), 0u);
310 TEST_F(TiledLayerImplTest
, EmptyMask
) {
311 gfx::Size
tile_size(20, 20);
312 gfx::Size
layer_size(0, 0);
313 scoped_ptr
<TiledLayerImpl
> layer
=
314 CreateLayer(tile_size
, layer_size
, LayerTilingData::NO_BORDER_TEXELS
);
316 ResourceId mask_resource_id
;
317 gfx::Size mask_texture_size
;
318 layer
->GetContentsResourceId(&mask_resource_id
, &mask_texture_size
);
319 EXPECT_EQ(0u, mask_resource_id
);
320 EXPECT_EQ(0, layer
->TilingForTesting()->num_tiles_x());
321 EXPECT_EQ(0, layer
->TilingForTesting()->num_tiles_y());
324 TEST_F(TiledLayerImplTest
, Occlusion
) {
325 gfx::Size
tile_size(100, 100);
326 gfx::Size
layer_bounds(1000, 1000);
327 gfx::Size
viewport_size(1000, 1000);
329 LayerTestCommon::LayerImplTest impl
;
331 TiledLayerImpl
* tiled_layer
= impl
.AddChildToRoot
<TiledLayerImpl
>();
332 tiled_layer
->SetBounds(layer_bounds
);
333 tiled_layer
->SetContentBounds(layer_bounds
);
334 tiled_layer
->SetDrawsContent(true);
335 tiled_layer
->set_skips_draw(false);
337 scoped_ptr
<LayerTilingData
> tiler
=
338 LayerTilingData::Create(tile_size
, LayerTilingData::NO_BORDER_TEXELS
);
339 tiler
->SetTilingSize(layer_bounds
);
340 tiled_layer
->SetTilingData(*tiler
);
342 for (int i
= 0; i
< tiled_layer
->TilingForTesting()->num_tiles_x(); ++i
) {
343 for (int j
= 0; j
< tiled_layer
->TilingForTesting()->num_tiles_y(); ++j
) {
344 ResourceId resource_id
= impl
.resource_provider()->CreateResource(
345 gfx::Size(1, 1), GL_CLAMP_TO_EDGE
,
346 ResourceProvider::TEXTURE_HINT_IMMUTABLE
, RGBA_8888
);
347 tiled_layer
->PushTileProperties(i
, j
, resource_id
, false);
351 impl
.CalcDrawProps(viewport_size
);
354 SCOPED_TRACE("No occlusion");
356 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
358 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(),
359 gfx::Rect(layer_bounds
));
360 EXPECT_EQ(100u, impl
.quad_list().size());
364 SCOPED_TRACE("Full occlusion");
365 gfx::Rect
occluded(tiled_layer
->visible_content_rect());
366 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
368 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl
.quad_list(), gfx::Rect());
369 EXPECT_EQ(impl
.quad_list().size(), 0u);
373 SCOPED_TRACE("Partial occlusion");
374 gfx::Rect
occluded(150, 0, 200, 1000);
375 impl
.AppendQuadsWithOcclusion(tiled_layer
, occluded
);
377 size_t partially_occluded_count
= 0;
378 LayerTestCommon::VerifyQuadsAreOccluded(
379 impl
.quad_list(), occluded
, &partially_occluded_count
);
380 // The layer outputs one quad, which is partially occluded.
381 EXPECT_EQ(100u - 10u, impl
.quad_list().size());
382 EXPECT_EQ(10u + 10u, partially_occluded_count
);