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/trees/layer_tree_host_common.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/point_conversions.h"
16 #include "ui/gfx/rect.h"
17 #include "ui/gfx/rect_conversions.h"
18 #include "ui/gfx/size_conversions.h"
22 // Align with expected and actual output.
23 const char* LayerTestCommon::quad_string
= " Quad: ";
25 static bool CanRectFBeSafelyRoundedToRect(const gfx::RectF
& r
) {
26 // Ensure that range of float values is not beyond integer range.
27 if (!r
.IsExpressibleAsRect())
30 // Ensure that the values are actually integers.
31 if (gfx::ToFlooredPoint(r
.origin()) == r
.origin() &&
32 gfx::ToFlooredSize(r
.size()) == r
.size())
38 void LayerTestCommon::VerifyQuadsExactlyCoverRect(const QuadList
& quads
,
39 const gfx::Rect
& rect
) {
40 Region remaining
= rect
;
42 for (size_t i
= 0; i
< quads
.size(); ++i
) {
43 DrawQuad
* quad
= quads
[i
];
44 gfx::RectF quad_rectf
=
45 MathUtil::MapClippedRect(quad
->quadTransform(), gfx::RectF(quad
->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
<< i
;
54 EXPECT_TRUE(remaining
.Contains(quad_rect
)) << quad_string
<< i
;
55 remaining
.Subtract(quad_rect
);
58 EXPECT_TRUE(remaining
.IsEmpty());
62 void LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
63 const QuadList
& quads
,
64 const gfx::Rect
& rect
,
65 const gfx::Rect
& occluded
,
66 size_t* partially_occluded_count
) {
67 // No quad should exist if it's fully occluded.
68 for (size_t i
= 0; i
< quads
.size(); ++i
) {
69 EXPECT_FALSE(occluded
.Contains(quads
[i
]->visible_rect
));
72 // Quads that are fully occluded on one axis only should be shrunken.
73 for (size_t i
= 0; i
< quads
.size(); ++i
) {
74 DrawQuad
* quad
= quads
[i
];
75 DCHECK(quad
->quadTransform().IsIdentityOrIntegerTranslation());
76 gfx::Rect target_rect
=
77 MathUtil::MapEnclosingClippedRect(quad
->quadTransform(), quad
->rect
);
78 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
79 quad
->quadTransform(), quad
->visible_rect
);
81 bool fully_occluded_horizontal
= target_rect
.x() >= occluded
.x() &&
82 target_rect
.right() <= occluded
.right();
83 bool fully_occluded_vertical
= target_rect
.y() >= occluded
.y() &&
84 target_rect
.bottom() <= occluded
.bottom();
85 bool should_be_occluded
=
86 target_rect
.Intersects(occluded
) &&
87 (fully_occluded_vertical
|| fully_occluded_horizontal
);
88 if (!should_be_occluded
) {
89 EXPECT_EQ(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
91 EXPECT_NE(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
92 EXPECT_TRUE(quad
->rect
.Contains(quad
->visible_rect
));
93 ++(*partially_occluded_count
);
98 LayerTestCommon::LayerImplTest::LayerImplTest()
99 : host_(FakeLayerTreeHost::Create()),
101 LayerImpl::Create(host_
->host_impl()->active_tree(), 1)) {
102 scoped_ptr
<FakeOutputSurface
> output_surface
= FakeOutputSurface::Create3d();
103 host_
->host_impl()->InitializeRenderer(
104 output_surface
.PassAs
<OutputSurface
>());
107 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
109 void LayerTestCommon::LayerImplTest::CalcDrawProps(
110 const gfx::Size
& viewport_size
) {
111 LayerImplList layer_list
;
112 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
113 root_layer_impl_
.get(), viewport_size
, &layer_list
);
114 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
117 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
118 LayerImpl
* layer_impl
,
119 const gfx::Rect
& occluded
) {
120 AppendQuadsData data
;
122 quad_culler_
.clear_lists();
123 quad_culler_
.set_occluded_target_rect(occluded
);
124 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
125 layer_impl
->AppendQuads(&quad_culler_
, &data
);
126 layer_impl
->DidDraw(resource_provider());
129 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
130 LayerImpl
* layer_impl
,
131 const RenderPass::Id
& id
,
132 const gfx::Rect
& occluded
) {
133 AppendQuadsData
data(id
);
135 quad_culler_
.clear_lists();
136 quad_culler_
.set_occluded_target_rect(occluded
);
137 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
138 layer_impl
->AppendQuads(&quad_culler_
, &data
);
139 layer_impl
->DidDraw(resource_provider());
142 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
143 RenderSurfaceImpl
* surface_impl
,
144 const gfx::Rect
& occluded
) {
145 AppendQuadsData data
;
147 quad_culler_
.clear_lists();
148 quad_culler_
.set_occluded_target_rect_for_contributing_surface(occluded
);
149 bool for_replica
= false;
150 RenderPass::Id
id(1, 1);
151 surface_impl
->AppendQuads(&quad_culler_
, &data
, for_replica
, id
);