• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cc/test/layer_test_common.h"
6 
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"
19 
20 namespace cc {
21 
22 // Align with expected and actual output.
23 const char* LayerTestCommon::quad_string = "    Quad: ";
24 
CanRectFBeSafelyRoundedToRect(const gfx::RectF & r)25 static bool CanRectFBeSafelyRoundedToRect(const gfx::RectF& r) {
26   // Ensure that range of float values is not beyond integer range.
27   if (!r.IsExpressibleAsRect())
28     return false;
29 
30   // Ensure that the values are actually integers.
31   if (gfx::ToFlooredPoint(r.origin()) == r.origin() &&
32       gfx::ToFlooredSize(r.size()) == r.size())
33     return true;
34 
35   return false;
36 }
37 
VerifyQuadsExactlyCoverRect(const QuadList & quads,const gfx::Rect & rect)38 void LayerTestCommon::VerifyQuadsExactlyCoverRect(const QuadList& quads,
39                                                   const gfx::Rect& rect) {
40   Region remaining = rect;
41 
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));
46 
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));
50 
51     gfx::Rect quad_rect = gfx::ToEnclosingRect(quad_rectf);
52 
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);
60   }
61 
62   EXPECT_TRUE(remaining.IsEmpty());
63 }
64 
65 // static
VerifyQuadsCoverRectWithOcclusion(const QuadList & quads,const gfx::Rect & rect,const gfx::Rect & occluded,size_t * partially_occluded_count)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));
74   }
75 
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);
84 
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());
94     } else {
95       EXPECT_NE(quad->rect.ToString(), quad->visible_rect.ToString());
96       EXPECT_TRUE(quad->rect.Contains(quad->visible_rect));
97       ++(*partially_occluded_count);
98     }
99   }
100 }
101 
LayerImplTest()102 LayerTestCommon::LayerImplTest::LayerImplTest()
103     : host_(FakeLayerTreeHost::Create()),
104       root_layer_impl_(LayerImpl::Create(host_->host_impl()->active_tree(), 1)),
105       render_pass_(RenderPass::Create()),
106       quad_culler_(make_scoped_ptr(
107           new MockQuadCuller(render_pass_.get(), &occlusion_tracker_))) {
108   scoped_ptr<FakeOutputSurface> output_surface = FakeOutputSurface::Create3d();
109   host_->host_impl()->InitializeRenderer(
110       output_surface.PassAs<OutputSurface>());
111 }
112 
~LayerImplTest()113 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
114 
CalcDrawProps(const gfx::Size & viewport_size)115 void LayerTestCommon::LayerImplTest::CalcDrawProps(
116     const gfx::Size& viewport_size) {
117   LayerImplList layer_list;
118   LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
119       root_layer_impl_.get(), viewport_size, &layer_list);
120   LayerTreeHostCommon::CalculateDrawProperties(&inputs);
121 }
122 
AppendQuadsWithOcclusion(LayerImpl * layer_impl,const gfx::Rect & occluded)123 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
124     LayerImpl* layer_impl,
125     const gfx::Rect& occluded) {
126   AppendQuadsData data;
127 
128   quad_culler_->clear_lists();
129   quad_culler_->set_occluded_target_rect(occluded);
130   layer_impl->WillDraw(DRAW_MODE_HARDWARE, resource_provider());
131   layer_impl->AppendQuads(quad_culler_.get(), &data);
132   layer_impl->DidDraw(resource_provider());
133 }
134 
AppendQuadsForPassWithOcclusion(LayerImpl * layer_impl,const RenderPass::Id & id,const gfx::Rect & occluded)135 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
136     LayerImpl* layer_impl,
137     const RenderPass::Id& id,
138     const gfx::Rect& occluded) {
139   AppendQuadsData data(id);
140 
141   quad_culler_->clear_lists();
142   quad_culler_->set_occluded_target_rect(occluded);
143   layer_impl->WillDraw(DRAW_MODE_HARDWARE, resource_provider());
144   layer_impl->AppendQuads(quad_culler_.get(), &data);
145   layer_impl->DidDraw(resource_provider());
146 }
147 
AppendSurfaceQuadsWithOcclusion(RenderSurfaceImpl * surface_impl,const gfx::Rect & occluded)148 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
149     RenderSurfaceImpl* surface_impl,
150     const gfx::Rect& occluded) {
151   AppendQuadsData data;
152 
153   quad_culler_->clear_lists();
154   quad_culler_->set_occluded_target_rect_for_contributing_surface(occluded);
155   bool for_replica = false;
156   RenderPass::Id id(1, 1);
157   surface_impl->AppendQuads(quad_culler_.get(), &data, for_replica, id);
158 }
159 
160 }  // namespace cc
161