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 << " rect: " << rect
.ToString()
55 << " quad: " << quad_rect
.ToString();
56 EXPECT_TRUE(remaining
.Contains(quad_rect
))
57 << quad_string
<< i
<< " remaining: " << remaining
.ToString()
58 << " quad: " << quad_rect
.ToString();
59 remaining
.Subtract(quad_rect
);
62 EXPECT_TRUE(remaining
.IsEmpty());
66 void LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
67 const QuadList
& quads
,
68 const gfx::Rect
& rect
,
69 const gfx::Rect
& occluded
,
70 size_t* partially_occluded_count
) {
71 // No quad should exist if it's fully occluded.
72 for (size_t i
= 0; i
< quads
.size(); ++i
) {
73 EXPECT_FALSE(occluded
.Contains(quads
[i
]->visible_rect
));
76 // Quads that are fully occluded on one axis only should be shrunken.
77 for (size_t i
= 0; i
< quads
.size(); ++i
) {
78 DrawQuad
* quad
= quads
[i
];
79 DCHECK(quad
->quadTransform().IsIdentityOrIntegerTranslation());
80 gfx::Rect target_rect
=
81 MathUtil::MapEnclosingClippedRect(quad
->quadTransform(), quad
->rect
);
82 gfx::Rect target_visible_rect
= MathUtil::MapEnclosingClippedRect(
83 quad
->quadTransform(), quad
->visible_rect
);
85 bool fully_occluded_horizontal
= target_rect
.x() >= occluded
.x() &&
86 target_rect
.right() <= occluded
.right();
87 bool fully_occluded_vertical
= target_rect
.y() >= occluded
.y() &&
88 target_rect
.bottom() <= occluded
.bottom();
89 bool should_be_occluded
=
90 target_rect
.Intersects(occluded
) &&
91 (fully_occluded_vertical
|| fully_occluded_horizontal
);
92 if (!should_be_occluded
) {
93 EXPECT_EQ(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
95 EXPECT_NE(quad
->rect
.ToString(), quad
->visible_rect
.ToString());
96 EXPECT_TRUE(quad
->rect
.Contains(quad
->visible_rect
));
97 ++(*partially_occluded_count
);
102 LayerTestCommon::LayerImplTest::LayerImplTest()
103 : host_(FakeLayerTreeHost::Create()),
105 LayerImpl::Create(host_
->host_impl()->active_tree(), 1)) {
106 scoped_ptr
<FakeOutputSurface
> output_surface
= FakeOutputSurface::Create3d();
107 host_
->host_impl()->InitializeRenderer(
108 output_surface
.PassAs
<OutputSurface
>());
111 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
113 void LayerTestCommon::LayerImplTest::CalcDrawProps(
114 const gfx::Size
& viewport_size
) {
115 LayerImplList layer_list
;
116 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
117 root_layer_impl_
.get(), viewport_size
, &layer_list
);
118 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
121 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
122 LayerImpl
* layer_impl
,
123 const gfx::Rect
& occluded
) {
124 AppendQuadsData data
;
126 quad_culler_
.clear_lists();
127 quad_culler_
.set_occluded_target_rect(occluded
);
128 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
129 layer_impl
->AppendQuads(&quad_culler_
, &data
);
130 layer_impl
->DidDraw(resource_provider());
133 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
134 LayerImpl
* layer_impl
,
135 const RenderPass::Id
& id
,
136 const gfx::Rect
& occluded
) {
137 AppendQuadsData
data(id
);
139 quad_culler_
.clear_lists();
140 quad_culler_
.set_occluded_target_rect(occluded
);
141 layer_impl
->WillDraw(DRAW_MODE_HARDWARE
, resource_provider());
142 layer_impl
->AppendQuads(&quad_culler_
, &data
);
143 layer_impl
->DidDraw(resource_provider());
146 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
147 RenderSurfaceImpl
* surface_impl
,
148 const gfx::Rect
& occluded
) {
149 AppendQuadsData data
;
151 quad_culler_
.clear_lists();
152 quad_culler_
.set_occluded_target_rect_for_contributing_surface(occluded
);
153 bool for_replica
= false;
154 RenderPass::Id
id(1, 1);
155 surface_impl
->AppendQuads(&quad_culler_
, &data
, for_replica
, id
);