Home | History | Annotate | Download | only in test
      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/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"
     20 
     21 namespace cc {
     22 
     23 // Align with expected and actual output.
     24 const char* LayerTestCommon::quad_string = "    Quad: ";
     25 
     26 static bool CanRectFBeSafelyRoundedToRect(const gfx::RectF& r) {
     27   // Ensure that range of float values is not beyond integer range.
     28   if (!r.IsExpressibleAsRect())
     29     return false;
     30 
     31   // Ensure that the values are actually integers.
     32   if (gfx::ToFlooredPoint(r.origin()) == r.origin() &&
     33       gfx::ToFlooredSize(r.size()) == r.size())
     34     return true;
     35 
     36   return false;
     37 }
     38 
     39 void LayerTestCommon::VerifyQuadsExactlyCoverRect(const QuadList& quads,
     40                                                   const gfx::Rect& rect) {
     41   Region remaining = rect;
     42 
     43   size_t i = 0;
     44   for (QuadList::ConstIterator iter = quads.begin(); iter != quads.end();
     45        ++iter) {
     46     const DrawQuad* quad = &*iter;
     47     gfx::RectF quad_rectf =
     48         MathUtil::MapClippedRect(quad->quadTransform(), gfx::RectF(quad->rect));
     49 
     50     // Before testing for exact coverage in the integer world, assert that
     51     // rounding will not round the rect incorrectly.
     52     ASSERT_TRUE(CanRectFBeSafelyRoundedToRect(quad_rectf));
     53 
     54     gfx::Rect quad_rect = gfx::ToEnclosingRect(quad_rectf);
     55 
     56     EXPECT_TRUE(rect.Contains(quad_rect)) << quad_string << i
     57                                           << " rect: " << rect.ToString()
     58                                           << " quad: " << quad_rect.ToString();
     59     EXPECT_TRUE(remaining.Contains(quad_rect))
     60         << quad_string << i << " remaining: " << remaining.ToString()
     61         << " quad: " << quad_rect.ToString();
     62     remaining.Subtract(quad_rect);
     63 
     64     ++i;
     65   }
     66 
     67   EXPECT_TRUE(remaining.IsEmpty());
     68 }
     69 
     70 // static
     71 void LayerTestCommon::VerifyQuadsAreOccluded(const QuadList& quads,
     72                                              const gfx::Rect& occluded,
     73                                              size_t* partially_occluded_count) {
     74   // No quad should exist if it's fully occluded.
     75   for (QuadList::ConstIterator iter = quads.begin(); iter != quads.end();
     76        ++iter) {
     77     gfx::Rect target_visible_rect = MathUtil::MapEnclosingClippedRect(
     78         iter->quadTransform(), iter->visible_rect);
     79     EXPECT_FALSE(occluded.Contains(target_visible_rect));
     80   }
     81 
     82   // Quads that are fully occluded on one axis only should be shrunken.
     83   for (QuadList::ConstIterator iter = quads.begin(); iter != quads.end();
     84        ++iter) {
     85     const DrawQuad* quad = &*iter;
     86     DCHECK(quad->quadTransform().IsIdentityOrIntegerTranslation());
     87     gfx::Rect target_rect =
     88         MathUtil::MapEnclosingClippedRect(quad->quadTransform(), quad->rect);
     89     gfx::Rect target_visible_rect = MathUtil::MapEnclosingClippedRect(
     90         quad->quadTransform(), quad->visible_rect);
     91 
     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());
    101     } else {
    102       EXPECT_NE(quad->rect.ToString(), quad->visible_rect.ToString());
    103       EXPECT_TRUE(quad->rect.Contains(quad->visible_rect));
    104       ++(*partially_occluded_count);
    105     }
    106   }
    107 }
    108 
    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   scoped_ptr<FakeOutputSurface> output_surface = FakeOutputSurface::Create3d();
    115   host_->host_impl()->InitializeRenderer(
    116       output_surface.PassAs<OutputSurface>());
    117 }
    118 
    119 LayerTestCommon::LayerImplTest::~LayerImplTest() {}
    120 
    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);
    127 }
    128 
    129 void LayerTestCommon::LayerImplTest::AppendQuadsWithOcclusion(
    130     LayerImpl* layer_impl,
    131     const gfx::Rect& occluded) {
    132   AppendQuadsData data;
    133 
    134   render_pass_->quad_list.clear();
    135   render_pass_->shared_quad_state_list.clear();
    136   occlusion_tracker_.set_occluded_target_rect(occluded);
    137   layer_impl->WillDraw(DRAW_MODE_HARDWARE, resource_provider());
    138   layer_impl->AppendQuads(render_pass_.get(), occlusion_tracker_, &data);
    139   layer_impl->DidDraw(resource_provider());
    140 }
    141 
    142 void LayerTestCommon::LayerImplTest::AppendQuadsForPassWithOcclusion(
    143     LayerImpl* layer_impl,
    144     const RenderPassId& id,
    145     const gfx::Rect& occluded) {
    146   AppendQuadsData data(id);
    147 
    148   render_pass_->quad_list.clear();
    149   render_pass_->shared_quad_state_list.clear();
    150   occlusion_tracker_.set_occluded_target_rect(occluded);
    151   layer_impl->WillDraw(DRAW_MODE_HARDWARE, resource_provider());
    152   layer_impl->AppendQuads(render_pass_.get(), occlusion_tracker_, &data);
    153   layer_impl->DidDraw(resource_provider());
    154 }
    155 
    156 void LayerTestCommon::LayerImplTest::AppendSurfaceQuadsWithOcclusion(
    157     RenderSurfaceImpl* surface_impl,
    158     const gfx::Rect& occluded) {
    159   AppendQuadsData data;
    160 
    161   render_pass_->quad_list.clear();
    162   render_pass_->shared_quad_state_list.clear();
    163   occlusion_tracker_.set_occluded_target_rect_for_contributing_surface(
    164       occluded);
    165   bool for_replica = false;
    166   RenderPassId id(1, 1);
    167   surface_impl->AppendQuads(
    168       render_pass_.get(), occlusion_tracker_, &data, for_replica, id);
    169 }
    170 
    171 }  // namespace cc
    172