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
=
45 MathUtil::MapClippedRect(iter
->quadTransform(), gfx::RectF(iter
->rect
));
47 // Before testing for exact coverage in the integer world, assert that
48 // rounding will not round the rect incorrectly.
49 ASSERT_TRUE(CanRectFBeSafelyRoundedToRect(quad_rectf
));
51 gfx::Rect quad_rect
= gfx::ToEnclosingRect(quad_rectf
);
53 EXPECT_TRUE(rect
.Contains(quad_rect
)) << quad_string
<< iter
.index()
54 << " rect: " << rect
.ToString()
55 << " quad: " << quad_rect
.ToString();
56 EXPECT_TRUE(remaining
.Contains(quad_rect
))
57 << quad_string
<< iter
.index() << " remaining: " << remaining
.ToString()
58 << " quad: " << quad_rect
.ToString();
59 remaining
.Subtract(quad_rect
);
62 EXPECT_TRUE(remaining
.IsEmpty());
66 void LayerTestCommon::VerifyQuadsAreOccluded(const QuadList
& quads
,
67 const gfx::Rect
& occluded
,
68 size_t* partially_occluded_count
) {
69 // No quad should exist if it's fully occluded.
70 for (const auto& quad
: quads
) {
71 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
72 quad
->quadTransform(), quad
->visible_rect
);
73 EXPECT_FALSE(occluded
.Contains(target_visible_rect
));
76 // Quads that are fully occluded on one axis only should be shrunken.
77 for (const auto& quad
: quads
) {
78 gfx::Rect target_rect
=
79 MathUtil::MapEnclosingClippedRect(quad
->quadTransform(), quad
->rect
);
80 if (!quad
->quadTransform().IsIdentityOrIntegerTranslation()) {
81 DCHECK(quad
->quadTransform().IsPositiveScaleOrTranslation())
82 << quad
->quadTransform().ToString();
83 gfx::RectF target_rectf
=
84 MathUtil::MapClippedRect(quad
->quadTransform(), quad
->rect
);
85 // Scale transforms allowed, as long as the final transformed rect
86 // ends up on integer boundaries for ease of testing.
87 DCHECK_EQ(target_rectf
.ToString(), gfx::RectF(target_rect
).ToString());
89 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
90 quad
->quadTransform(), quad
->visible_rect
);
92 bool fully_occluded_horizontal
= target_rect
.x() >= occluded
.x() &&
93 target_rect
.right() <= occluded
.right();
94 bool fully_occluded_vertical
= target_rect
.y() >= occluded
.y() &&
95 target_rect
.bottom() <= occluded
.bottom();
96 bool should_be_occluded
=
97 target_rect
.Intersects(occluded
) &&
98 (fully_occluded_vertical
|| fully_occluded_horizontal
);
99 if (!should_be_occluded
) {
100 EXPECT_EQ(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
102 EXPECT_NE(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
103 EXPECT_TRUE(quad
->rect
.Contains(quad
->visible_rect
));
104 ++(*partially_occluded_count
);
109 LayerTestCommon::LayerImplTest::LayerImplTest()
110 : client_(FakeLayerTreeHostClient::DIRECT_3D
),
111 host_(FakeLayerTreeHost::Create(&client_
)),
112 root_layer_impl_(LayerImpl::Create(host_
->host_impl()->active_tree(), 1)),
113 render_pass_(RenderPass::Create()) {
114 root_layer_impl_
->SetHasRenderSurface(true);
115 scoped_ptr
<FakeOutputSurface
> output_surface
= FakeOutputSurface::Create3d();
116 host_
->host_impl()->InitializeRenderer(FakeOutputSurface::Create3d());
119 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
121 void LayerTestCommon::LayerImplTest::CalcDrawProps(
122 const gfx::Size
& viewport_size
) {
123 LayerImplList layer_list
;
124 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
125 root_layer_impl_
.get(), viewport_size
, &layer_list
);
126 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
129 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
130 LayerImpl
* layer_impl
,
131 const gfx::Rect
& occluded
) {
132 AppendQuadsData data
;
134 render_pass_
->quad_list
.clear();
135 render_pass_
->shared_quad_state_list
.clear();
137 Occlusion
occlusion(layer_impl
->draw_transform(),
138 SimpleEnclosedRegion(occluded
),
139 SimpleEnclosedRegion());
140 layer_impl
->draw_properties().occlusion_in_content_space
= occlusion
;
142 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
143 layer_impl
->AppendQuads(render_pass_
.get(), &data
);
144 layer_impl
->DidDraw(resource_provider());
147 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
148 LayerImpl
* layer_impl
,
149 RenderPass
* given_render_pass
,
150 const gfx::Rect
& occluded
) {
151 AppendQuadsData data
;
153 given_render_pass
->quad_list
.clear();
154 given_render_pass
->shared_quad_state_list
.clear();
156 Occlusion
occlusion(layer_impl
->draw_transform(),
157 SimpleEnclosedRegion(occluded
),
158 SimpleEnclosedRegion());
159 layer_impl
->draw_properties().occlusion_in_content_space
= occlusion
;
161 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
162 layer_impl
->AppendQuads(given_render_pass
, &data
);
163 layer_impl
->DidDraw(resource_provider());
166 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
167 RenderSurfaceImpl
* surface_impl
,
168 const gfx::Rect
& occluded
) {
169 AppendQuadsData data
;
171 render_pass_
->quad_list
.clear();
172 render_pass_
->shared_quad_state_list
.clear();
174 surface_impl
->AppendQuads(
175 render_pass_
.get(), gfx::Transform(),
176 Occlusion(gfx::Transform(), SimpleEnclosedRegion(occluded
),
177 SimpleEnclosedRegion()),
178 SK_ColorBLACK
, 1.f
, nullptr, &data
, RenderPassId(1, 1));