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/point_conversions.h"
17 #include "ui/gfx/rect.h"
18 #include "ui/gfx/rect_conversions.h"
19 #include "ui/gfx/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 (size_t i
= 0; i
< quads
.size(); ++i
) {
44 DrawQuad
* quad
= quads
[i
];
45 gfx::RectF quad_rectf
=
46 MathUtil::MapClippedRect(quad
->quadTransform(), gfx::RectF(quad
->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
<< i
55 << " rect: " << rect
.ToString()
56 << " quad: " << quad_rect
.ToString();
57 EXPECT_TRUE(remaining
.Contains(quad_rect
))
58 << quad_string
<< i
<< " remaining: " << remaining
.ToString()
59 << " quad: " << quad_rect
.ToString();
60 remaining
.Subtract(quad_rect
);
63 EXPECT_TRUE(remaining
.IsEmpty());
67 void LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
68 const QuadList
& quads
,
69 const gfx::Rect
& rect
,
70 const gfx::Rect
& occluded
,
71 size_t* partially_occluded_count
) {
72 // No quad should exist if it's fully occluded.
73 for (size_t i
= 0; i
< quads
.size(); ++i
) {
74 EXPECT_FALSE(occluded
.Contains(quads
[i
]->visible_rect
));
77 // Quads that are fully occluded on one axis only should be shrunken.
78 for (size_t i
= 0; i
< quads
.size(); ++i
) {
79 DrawQuad
* quad
= quads
[i
];
80 DCHECK(quad
->quadTransform().IsIdentityOrIntegerTranslation());
81 gfx::Rect target_rect
=
82 MathUtil::MapEnclosingClippedRect(quad
->quadTransform(), quad
->rect
);
83 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
84 quad
->quadTransform(), quad
->visible_rect
);
86 bool fully_occluded_horizontal
= target_rect
.x() >= occluded
.x() &&
87 target_rect
.right() <= occluded
.right();
88 bool fully_occluded_vertical
= target_rect
.y() >= occluded
.y() &&
89 target_rect
.bottom() <= occluded
.bottom();
90 bool should_be_occluded
=
91 target_rect
.Intersects(occluded
) &&
92 (fully_occluded_vertical
|| fully_occluded_horizontal
);
93 if (!should_be_occluded
) {
94 EXPECT_EQ(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
96 EXPECT_NE(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
97 EXPECT_TRUE(quad
->rect
.Contains(quad
->visible_rect
));
98 ++(*partially_occluded_count
);
103 LayerTestCommon::LayerImplTest::LayerImplTest()
104 : host_(FakeLayerTreeHost::Create()),
105 root_layer_impl_(LayerImpl::Create(host_
->host_impl()->active_tree(), 1)),
106 render_pass_(RenderPass::Create()) {
107 scoped_ptr
<FakeOutputSurface
> output_surface
= FakeOutputSurface::Create3d();
108 host_
->host_impl()->InitializeRenderer(
109 output_surface
.PassAs
<OutputSurface
>());
112 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
114 void LayerTestCommon::LayerImplTest::CalcDrawProps(
115 const gfx::Size
& viewport_size
) {
116 LayerImplList layer_list
;
117 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
118 root_layer_impl_
.get(), viewport_size
, &layer_list
);
119 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
122 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
123 LayerImpl
* layer_impl
,
124 const gfx::Rect
& occluded
) {
125 AppendQuadsData data
;
127 render_pass_
->quad_list
.clear();
128 render_pass_
->shared_quad_state_list
.clear();
129 occlusion_tracker_
.set_occluded_target_rect(occluded
);
130 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
131 layer_impl
->AppendQuads(render_pass_
.get(), occlusion_tracker_
, &data
);
132 layer_impl
->DidDraw(resource_provider());
135 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
136 LayerImpl
* layer_impl
,
137 const RenderPass::Id
& id
,
138 const gfx::Rect
& occluded
) {
139 AppendQuadsData
data(id
);
141 render_pass_
->quad_list
.clear();
142 render_pass_
->shared_quad_state_list
.clear();
143 occlusion_tracker_
.set_occluded_target_rect(occluded
);
144 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
145 layer_impl
->AppendQuads(render_pass_
.get(), occlusion_tracker_
, &data
);
146 layer_impl
->DidDraw(resource_provider());
149 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
150 RenderSurfaceImpl
* surface_impl
,
151 const gfx::Rect
& occluded
) {
152 AppendQuadsData data
;
154 render_pass_
->quad_list
.clear();
155 render_pass_
->shared_quad_state_list
.clear();
156 occlusion_tracker_
.set_occluded_target_rect_for_contributing_surface(
158 bool for_replica
= false;
159 RenderPass::Id
id(1, 1);
160 surface_impl
->AppendQuads(
161 render_pass_
.get(), occlusion_tracker_
, &data
, for_replica
, id
);