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/test/layer_test_common.h"
7 #include "cc/base/math_util.h"
8 #include "cc/base/region.h"
9 #include "cc/layers/append_quads_data.h"
10 #include "cc/quads/draw_quad.h"
11 #include "cc/quads/render_pass.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/mock_occlusion_tracker.h"
14 #include "cc/trees/layer_tree_host_common.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/gfx/geometry/point_conversions.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/rect_conversions.h"
19 #include "ui/gfx/geometry/size_conversions.h"
23 // Align with expected and actual output.
24 const char* LayerTestCommon::quad_string
= " Quad: ";
26 static bool CanRectFBeSafelyRoundedToRect(const gfx::RectF
& r
) {
27 // Ensure that range of float values is not beyond integer range.
28 if (!r
.IsExpressibleAsRect())
31 // Ensure that the values are actually integers.
32 if (gfx::ToFlooredPoint(r
.origin()) == r
.origin() &&
33 gfx::ToFlooredSize(r
.size()) == r
.size())
39 void LayerTestCommon::VerifyQuadsExactlyCoverRect(const QuadList
& quads
,
40 const gfx::Rect
& rect
) {
41 Region remaining
= rect
;
43 for (auto iter
= quads
.cbegin(); iter
!= quads
.cend(); ++iter
) {
44 gfx::RectF quad_rectf
= MathUtil::MapClippedRect(
45 iter
->shared_quad_state
->quad_to_target_transform
,
46 gfx::RectF(iter
->rect
));
48 // Before testing for exact coverage in the integer world, assert that
49 // rounding will not round the rect incorrectly.
50 ASSERT_TRUE(CanRectFBeSafelyRoundedToRect(quad_rectf
));
52 gfx::Rect quad_rect
= gfx::ToEnclosingRect(quad_rectf
);
54 EXPECT_TRUE(rect
.Contains(quad_rect
)) << quad_string
<< iter
.index()
55 << " rect: " << rect
.ToString()
56 << " quad: " << quad_rect
.ToString();
57 EXPECT_TRUE(remaining
.Contains(quad_rect
))
58 << quad_string
<< iter
.index() << " remaining: " << remaining
.ToString()
59 << " quad: " << quad_rect
.ToString();
60 remaining
.Subtract(quad_rect
);
63 EXPECT_TRUE(remaining
.IsEmpty());
67 void LayerTestCommon::VerifyQuadsAreOccluded(const QuadList
& quads
,
68 const gfx::Rect
& occluded
,
69 size_t* partially_occluded_count
) {
70 // No quad should exist if it's fully occluded.
71 for (const auto& quad
: quads
) {
72 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
73 quad
->shared_quad_state
->quad_to_target_transform
, quad
->visible_rect
);
74 EXPECT_FALSE(occluded
.Contains(target_visible_rect
));
77 // Quads that are fully occluded on one axis only should be shrunken.
78 for (const auto& quad
: quads
) {
79 gfx::Rect target_rect
= MathUtil::MapEnclosingClippedRect(
80 quad
->shared_quad_state
->quad_to_target_transform
, quad
->rect
);
81 if (!quad
->shared_quad_state
->quad_to_target_transform
82 .IsIdentityOrIntegerTranslation()) {
83 DCHECK(quad
->shared_quad_state
->quad_to_target_transform
84 .IsPositiveScaleOrTranslation())
85 << quad
->shared_quad_state
->quad_to_target_transform
.ToString();
86 gfx::RectF target_rectf
= MathUtil::MapClippedRect(
87 quad
->shared_quad_state
->quad_to_target_transform
,
88 gfx::RectF(quad
->rect
));
89 // Scale transforms allowed, as long as the final transformed rect
90 // ends up on integer boundaries for ease of testing.
91 ASSERT_EQ(target_rectf
, gfx::RectF(target_rect
));
93 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
94 quad
->shared_quad_state
->quad_to_target_transform
, quad
->visible_rect
);
96 bool fully_occluded_horizontal
= target_rect
.x() >= occluded
.x() &&
97 target_rect
.right() <= occluded
.right();
98 bool fully_occluded_vertical
= target_rect
.y() >= occluded
.y() &&
99 target_rect
.bottom() <= occluded
.bottom();
100 bool should_be_occluded
=
101 target_rect
.Intersects(occluded
) &&
102 (fully_occluded_vertical
|| fully_occluded_horizontal
);
103 if (!should_be_occluded
) {
104 EXPECT_EQ(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
106 EXPECT_NE(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
107 EXPECT_TRUE(quad
->rect
.Contains(quad
->visible_rect
));
108 ++(*partially_occluded_count
);
113 LayerTestCommon::LayerImplTest::LayerImplTest()
114 : LayerImplTest(LayerTreeSettings()) {
117 LayerTestCommon::LayerImplTest::LayerImplTest(const LayerTreeSettings
& settings
)
118 : client_(FakeLayerTreeHostClient::DIRECT_3D
),
119 host_(FakeLayerTreeHost::Create(&client_
, &task_graph_runner_
, settings
)),
120 root_layer_impl_(LayerImpl::Create(host_
->host_impl()->active_tree(), 1)),
121 render_pass_(RenderPass::Create()),
123 root_layer_impl_
->SetHasRenderSurface(true);
124 scoped_ptr
<FakeOutputSurface
> output_surface
= FakeOutputSurface::Create3d();
125 host_
->host_impl()->InitializeRenderer(FakeOutputSurface::Create3d());
128 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
130 void LayerTestCommon::LayerImplTest::CalcDrawProps(
131 const gfx::Size
& viewport_size
) {
132 LayerImplList layer_list
;
133 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
134 root_layer_impl_
.get(), viewport_size
, &layer_list
);
135 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
138 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
139 LayerImpl
* layer_impl
,
140 const gfx::Rect
& occluded
) {
141 AppendQuadsData data
;
143 render_pass_
->quad_list
.clear();
144 render_pass_
->shared_quad_state_list
.clear();
146 Occlusion
occlusion(layer_impl
->draw_transform(),
147 SimpleEnclosedRegion(occluded
),
148 SimpleEnclosedRegion());
149 layer_impl
->draw_properties().occlusion_in_content_space
= occlusion
;
151 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
152 layer_impl
->AppendQuads(render_pass_
.get(), &data
);
153 layer_impl
->DidDraw(resource_provider());
156 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
157 LayerImpl
* layer_impl
,
158 RenderPass
* given_render_pass
,
159 const gfx::Rect
& occluded
) {
160 AppendQuadsData data
;
162 given_render_pass
->quad_list
.clear();
163 given_render_pass
->shared_quad_state_list
.clear();
165 Occlusion
occlusion(layer_impl
->draw_transform(),
166 SimpleEnclosedRegion(occluded
),
167 SimpleEnclosedRegion());
168 layer_impl
->draw_properties().occlusion_in_content_space
= occlusion
;
170 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
171 layer_impl
->AppendQuads(given_render_pass
, &data
);
172 layer_impl
->DidDraw(resource_provider());
175 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
176 RenderSurfaceImpl
* surface_impl
,
177 const gfx::Rect
& occluded
) {
178 AppendQuadsData data
;
180 render_pass_
->quad_list
.clear();
181 render_pass_
->shared_quad_state_list
.clear();
183 surface_impl
->AppendQuads(
184 render_pass_
.get(), gfx::Transform(),
185 Occlusion(gfx::Transform(), SimpleEnclosedRegion(occluded
),
186 SimpleEnclosedRegion()),
187 SK_ColorBLACK
, 1.f
, nullptr, &data
, RenderPassId(1, 1));