Home | History | Annotate | Download | only in layers
      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/layers/delegated_renderer_layer_impl.h"
      6 
      7 #include "cc/base/scoped_ptr_vector.h"
      8 #include "cc/layers/append_quads_data.h"
      9 #include "cc/layers/quad_sink.h"
     10 #include "cc/layers/solid_color_layer_impl.h"
     11 #include "cc/quads/render_pass_draw_quad.h"
     12 #include "cc/quads/solid_color_draw_quad.h"
     13 #include "cc/test/fake_delegated_renderer_layer_impl.h"
     14 #include "cc/test/fake_layer_tree_host_impl.h"
     15 #include "cc/test/fake_layer_tree_host_impl_client.h"
     16 #include "cc/test/fake_output_surface.h"
     17 #include "cc/test/fake_proxy.h"
     18 #include "cc/test/fake_rendering_stats_instrumentation.h"
     19 #include "cc/test/geometry_test_utils.h"
     20 #include "cc/test/mock_quad_culler.h"
     21 #include "cc/test/render_pass_test_common.h"
     22 #include "cc/test/render_pass_test_utils.h"
     23 #include "cc/test/test_web_graphics_context_3d.h"
     24 #include "cc/trees/layer_tree_host_impl.h"
     25 #include "cc/trees/layer_tree_impl.h"
     26 #include "cc/trees/single_thread_proxy.h"
     27 #include "testing/gtest/include/gtest/gtest.h"
     28 #include "ui/gfx/transform.h"
     29 
     30 namespace cc {
     31 namespace {
     32 
     33 class DelegatedRendererLayerImplTest : public testing::Test {
     34  public:
     35   DelegatedRendererLayerImplTest()
     36       : proxy_(),
     37         always_impl_thread_and_main_thread_blocked_(&proxy_) {
     38     LayerTreeSettings settings;
     39     settings.minimum_occlusion_tracking_size = gfx::Size();
     40 
     41     host_impl_ = LayerTreeHostImpl::Create(settings,
     42                                            &client_,
     43                                            &proxy_,
     44                                            &stats_instrumentation_);
     45     host_impl_->InitializeRenderer(CreateFakeOutputSurface());
     46     host_impl_->SetViewportSize(gfx::Size(10, 10));
     47   }
     48 
     49  protected:
     50   FakeProxy proxy_;
     51   FakeLayerTreeHostImplClient client_;
     52   DebugScopedSetImplThreadAndMainThreadBlocked
     53       always_impl_thread_and_main_thread_blocked_;
     54   FakeRenderingStatsInstrumentation stats_instrumentation_;
     55   scoped_ptr<LayerTreeHostImpl> host_impl_;
     56 };
     57 
     58 class DelegatedRendererLayerImplTestSimple
     59     : public DelegatedRendererLayerImplTest {
     60  public:
     61   DelegatedRendererLayerImplTestSimple()
     62       : DelegatedRendererLayerImplTest() {
     63     scoped_ptr<LayerImpl> root_layer = SolidColorLayerImpl::Create(
     64         host_impl_->active_tree(), 1).PassAs<LayerImpl>();
     65     scoped_ptr<LayerImpl> layer_before = SolidColorLayerImpl::Create(
     66         host_impl_->active_tree(), 2).PassAs<LayerImpl>();
     67     scoped_ptr<LayerImpl> layer_after = SolidColorLayerImpl::Create(
     68         host_impl_->active_tree(), 3).PassAs<LayerImpl>();
     69     scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer =
     70         FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 4);
     71 
     72     host_impl_->SetViewportSize(gfx::Size(100, 100));
     73     root_layer->SetBounds(gfx::Size(100, 100));
     74 
     75     layer_before->SetPosition(gfx::Point(20, 20));
     76     layer_before->SetBounds(gfx::Size(14, 14));
     77     layer_before->SetContentBounds(gfx::Size(14, 14));
     78     layer_before->SetDrawsContent(true);
     79     layer_before->SetForceRenderSurface(true);
     80 
     81     layer_after->SetPosition(gfx::Point(5, 5));
     82     layer_after->SetBounds(gfx::Size(15, 15));
     83     layer_after->SetContentBounds(gfx::Size(15, 15));
     84     layer_after->SetDrawsContent(true);
     85     layer_after->SetForceRenderSurface(true);
     86 
     87     delegated_renderer_layer->SetPosition(gfx::Point(3, 3));
     88     delegated_renderer_layer->SetBounds(gfx::Size(10, 10));
     89     delegated_renderer_layer->SetContentBounds(gfx::Size(10, 10));
     90     delegated_renderer_layer->SetDrawsContent(true);
     91     gfx::Transform transform;
     92     transform.Translate(1.0, 1.0);
     93     delegated_renderer_layer->SetTransform(transform);
     94 
     95     ScopedPtrVector<RenderPass> delegated_render_passes;
     96     TestRenderPass* pass1 = AddRenderPass(
     97         &delegated_render_passes,
     98         RenderPass::Id(9, 6),
     99         gfx::Rect(6, 6, 6, 6),
    100         gfx::Transform());
    101     AddQuad(pass1, gfx::Rect(0, 0, 6, 6), 33u);
    102     TestRenderPass* pass2 = AddRenderPass(
    103         &delegated_render_passes,
    104         RenderPass::Id(9, 7),
    105         gfx::Rect(7, 7, 7, 7),
    106         gfx::Transform());
    107     AddQuad(pass2, gfx::Rect(0, 0, 7, 7), 22u);
    108     AddRenderPassQuad(pass2, pass1);
    109     TestRenderPass* pass3 = AddRenderPass(
    110         &delegated_render_passes,
    111         RenderPass::Id(9, 8),
    112         gfx::Rect(0, 0, 8, 8),
    113         gfx::Transform());
    114     AddRenderPassQuad(pass3, pass2);
    115     delegated_renderer_layer->SetFrameDataForRenderPasses(
    116         &delegated_render_passes);
    117 
    118     // The RenderPasses should be taken by the layer.
    119     EXPECT_EQ(0u, delegated_render_passes.size());
    120 
    121     root_layer_ = root_layer.get();
    122     layer_before_ = layer_before.get();
    123     layer_after_ = layer_after.get();
    124     delegated_renderer_layer_ = delegated_renderer_layer.get();
    125 
    126     // Force the delegated RenderPasses to come before the RenderPass from
    127     // layer_after.
    128     layer_after->AddChild(delegated_renderer_layer.PassAs<LayerImpl>());
    129     root_layer->AddChild(layer_after.Pass());
    130 
    131     // Get the RenderPass generated by layer_before to come before the delegated
    132     // RenderPasses.
    133     root_layer->AddChild(layer_before.Pass());
    134     host_impl_->active_tree()->SetRootLayer(root_layer.Pass());
    135   }
    136 
    137  protected:
    138   LayerImpl* root_layer_;
    139   LayerImpl* layer_before_;
    140   LayerImpl* layer_after_;
    141   DelegatedRendererLayerImpl* delegated_renderer_layer_;
    142 };
    143 
    144 TEST_F(DelegatedRendererLayerImplTestSimple, AddsContributingRenderPasses) {
    145   LayerTreeHostImpl::FrameData frame;
    146   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    147 
    148   // Each non-DelegatedRendererLayer added one RenderPass. The
    149   // DelegatedRendererLayer added two contributing passes.
    150   ASSERT_EQ(5u, frame.render_passes.size());
    151 
    152   // The DelegatedRendererLayer should have added its contributing RenderPasses
    153   // to the frame.
    154   EXPECT_EQ(4, frame.render_passes[1]->id.layer_id);
    155   EXPECT_EQ(1, frame.render_passes[1]->id.index);
    156   EXPECT_EQ(4, frame.render_passes[2]->id.layer_id);
    157   EXPECT_EQ(2, frame.render_passes[2]->id.index);
    158   // And all other RenderPasses should be non-delegated.
    159   EXPECT_NE(4, frame.render_passes[0]->id.layer_id);
    160   EXPECT_EQ(0, frame.render_passes[0]->id.index);
    161   EXPECT_NE(4, frame.render_passes[3]->id.layer_id);
    162   EXPECT_EQ(0, frame.render_passes[3]->id.index);
    163   EXPECT_NE(4, frame.render_passes[4]->id.layer_id);
    164   EXPECT_EQ(0, frame.render_passes[4]->id.index);
    165 
    166   // The DelegatedRendererLayer should have added its RenderPasses to the frame
    167   // in order.
    168   EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(),
    169             frame.render_passes[1]->output_rect.ToString());
    170   EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(),
    171             frame.render_passes[2]->output_rect.ToString());
    172 
    173   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    174   host_impl_->DidDrawAllLayers(frame);
    175 }
    176 
    177 TEST_F(DelegatedRendererLayerImplTestSimple,
    178        AddsQuadsToContributingRenderPasses) {
    179   LayerTreeHostImpl::FrameData frame;
    180   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    181 
    182   // Each non-DelegatedRendererLayer added one RenderPass. The
    183   // DelegatedRendererLayer added two contributing passes.
    184   ASSERT_EQ(5u, frame.render_passes.size());
    185 
    186   // The DelegatedRendererLayer should have added its contributing RenderPasses
    187   // to the frame.
    188   EXPECT_EQ(4, frame.render_passes[1]->id.layer_id);
    189   EXPECT_EQ(1, frame.render_passes[1]->id.index);
    190   EXPECT_EQ(4, frame.render_passes[2]->id.layer_id);
    191   EXPECT_EQ(2, frame.render_passes[2]->id.index);
    192 
    193   // The DelegatedRendererLayer should have added copies of its quads to
    194   // contributing RenderPasses.
    195   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    196   EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(),
    197             frame.render_passes[1]->quad_list[0]->rect.ToString());
    198 
    199   // Verify it added the right quads.
    200   ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size());
    201   EXPECT_EQ(gfx::Rect(0, 0, 7, 7).ToString(),
    202             frame.render_passes[2]->quad_list[0]->rect.ToString());
    203   EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(),
    204             frame.render_passes[2]->quad_list[1]->rect.ToString());
    205   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    206   EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(),
    207             frame.render_passes[1]->quad_list[0]->rect.ToString());
    208 
    209   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    210   host_impl_->DidDrawAllLayers(frame);
    211 }
    212 
    213 TEST_F(DelegatedRendererLayerImplTestSimple, AddsQuadsToTargetRenderPass) {
    214   LayerTreeHostImpl::FrameData frame;
    215   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    216 
    217   // Each non-DelegatedRendererLayer added one RenderPass. The
    218   // DelegatedRendererLayer added two contributing passes.
    219   ASSERT_EQ(5u, frame.render_passes.size());
    220 
    221   // The layer's target is the RenderPass from layer_after_.
    222   EXPECT_EQ(RenderPass::Id(3, 0), frame.render_passes[3]->id);
    223 
    224   // The DelegatedRendererLayer should have added copies of quads in its root
    225   // RenderPass to its target RenderPass. The layer_after_ also adds one quad.
    226   ASSERT_EQ(2u, frame.render_passes[3]->quad_list.size());
    227 
    228   // Verify it added the right quads.
    229   EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(),
    230             frame.render_passes[3]->quad_list[0]->rect.ToString());
    231 
    232   // Its target layer should have a quad as well.
    233   EXPECT_EQ(gfx::Rect(0, 0, 15, 15).ToString(),
    234             frame.render_passes[3]->quad_list[1]->rect.ToString());
    235 
    236   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    237   host_impl_->DidDrawAllLayers(frame);
    238 }
    239 
    240 TEST_F(DelegatedRendererLayerImplTestSimple,
    241        QuadsFromRootRenderPassAreModifiedForTheTarget) {
    242   LayerTreeHostImpl::FrameData frame;
    243   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    244 
    245   // Each non-DelegatedRendererLayer added one RenderPass. The
    246   // DelegatedRendererLayer added two contributing passes.
    247   ASSERT_EQ(5u, frame.render_passes.size());
    248 
    249   // The DelegatedRendererLayer is at position 3,3 compared to its target, and
    250   // has a translation transform of 1,1. So its root RenderPass' quads should
    251   // all be transformed by that combined amount.
    252   // The DelegatedRendererLayer has a size of 10x10, but the root delegated
    253   // RenderPass has a size of 8x8, so any quads should be scaled by 10/8.
    254   gfx::Transform transform;
    255   transform.Translate(4.0, 4.0);
    256   transform.Scale(10.0 / 8.0, 10.0 / 8.0);
    257   EXPECT_TRANSFORMATION_MATRIX_EQ(
    258       transform, frame.render_passes[3]->quad_list[0]->quadTransform());
    259 
    260   // Quads from non-root RenderPasses should not be shifted though.
    261   ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size());
    262   EXPECT_TRANSFORMATION_MATRIX_EQ(
    263       gfx::Transform(), frame.render_passes[2]->quad_list[0]->quadTransform());
    264   EXPECT_TRANSFORMATION_MATRIX_EQ(
    265       gfx::Transform(), frame.render_passes[2]->quad_list[1]->quadTransform());
    266   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    267   EXPECT_TRANSFORMATION_MATRIX_EQ(
    268       gfx::Transform(), frame.render_passes[1]->quad_list[0]->quadTransform());
    269 
    270   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    271   host_impl_->DidDrawAllLayers(frame);
    272 }
    273 
    274 TEST_F(DelegatedRendererLayerImplTestSimple, DoesNotOwnARenderSurface) {
    275   LayerTreeHostImpl::FrameData frame;
    276   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    277 
    278   // If the DelegatedRendererLayer is axis aligned and has opacity 1, then it
    279   // has no need to be a RenderSurface for the quads it carries.
    280   EXPECT_FALSE(delegated_renderer_layer_->render_surface());
    281 
    282   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    283   host_impl_->DidDrawAllLayers(frame);
    284 }
    285 
    286 TEST_F(DelegatedRendererLayerImplTestSimple, DoesOwnARenderSurfaceForOpacity) {
    287   delegated_renderer_layer_->SetOpacity(0.5f);
    288 
    289   LayerTreeHostImpl::FrameData frame;
    290   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    291 
    292   // This test case has quads from multiple layers in the delegated renderer, so
    293   // if the DelegatedRendererLayer has opacity < 1, it should end up with a
    294   // render surface.
    295   EXPECT_TRUE(delegated_renderer_layer_->render_surface());
    296 
    297   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    298   host_impl_->DidDrawAllLayers(frame);
    299 }
    300 
    301 TEST_F(DelegatedRendererLayerImplTestSimple,
    302        DoesOwnARenderSurfaceForTransform) {
    303   gfx::Transform rotation;
    304   rotation.RotateAboutZAxis(30.0);
    305   delegated_renderer_layer_->SetTransform(rotation);
    306 
    307   LayerTreeHostImpl::FrameData frame;
    308   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    309 
    310   // This test case has quads from multiple layers in the delegated renderer, so
    311   // if the DelegatedRendererLayer has opacity < 1, it should end up with a
    312   // render surface.
    313   EXPECT_TRUE(delegated_renderer_layer_->render_surface());
    314 
    315   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    316   host_impl_->DidDrawAllLayers(frame);
    317 }
    318 
    319 class DelegatedRendererLayerImplTestOwnSurface
    320     : public DelegatedRendererLayerImplTestSimple {
    321  public:
    322   DelegatedRendererLayerImplTestOwnSurface()
    323       : DelegatedRendererLayerImplTestSimple() {
    324     delegated_renderer_layer_->SetForceRenderSurface(true);
    325   }
    326 };
    327 
    328 TEST_F(DelegatedRendererLayerImplTestOwnSurface, AddsRenderPasses) {
    329   LayerTreeHostImpl::FrameData frame;
    330   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    331 
    332   // Each non-DelegatedRendererLayer added one RenderPass. The
    333   // DelegatedRendererLayer added two contributing passes and its owned surface
    334   // added one pass.
    335   ASSERT_EQ(6u, frame.render_passes.size());
    336 
    337   // The DelegatedRendererLayer should have added its contributing RenderPasses
    338   // to the frame.
    339   EXPECT_EQ(4, frame.render_passes[1]->id.layer_id);
    340   EXPECT_EQ(1, frame.render_passes[1]->id.index);
    341   EXPECT_EQ(4, frame.render_passes[2]->id.layer_id);
    342   EXPECT_EQ(2, frame.render_passes[2]->id.index);
    343   // The DelegatedRendererLayer should have added a RenderPass for its surface
    344   // to the frame.
    345   EXPECT_EQ(4, frame.render_passes[1]->id.layer_id);
    346   EXPECT_EQ(0, frame.render_passes[3]->id.index);
    347   // And all other RenderPasses should be non-delegated.
    348   EXPECT_NE(4, frame.render_passes[0]->id.layer_id);
    349   EXPECT_EQ(0, frame.render_passes[0]->id.index);
    350   EXPECT_NE(4, frame.render_passes[4]->id.layer_id);
    351   EXPECT_EQ(0, frame.render_passes[4]->id.index);
    352   EXPECT_NE(4, frame.render_passes[5]->id.layer_id);
    353   EXPECT_EQ(0, frame.render_passes[5]->id.index);
    354 
    355   // The DelegatedRendererLayer should have added its RenderPasses to the frame
    356   // in order.
    357   EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(),
    358             frame.render_passes[1]->output_rect.ToString());
    359   EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(),
    360             frame.render_passes[2]->output_rect.ToString());
    361 
    362   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    363   host_impl_->DidDrawAllLayers(frame);
    364 }
    365 
    366 TEST_F(DelegatedRendererLayerImplTestOwnSurface,
    367        AddsQuadsToContributingRenderPasses) {
    368   LayerTreeHostImpl::FrameData frame;
    369   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    370 
    371   // Each non-DelegatedRendererLayer added one RenderPass. The
    372   // DelegatedRendererLayer added two contributing passes and its owned surface
    373   // added one pass.
    374   ASSERT_EQ(6u, frame.render_passes.size());
    375 
    376   // The DelegatedRendererLayer should have added its contributing RenderPasses
    377   // to the frame.
    378   EXPECT_EQ(4, frame.render_passes[1]->id.layer_id);
    379   EXPECT_EQ(1, frame.render_passes[1]->id.index);
    380   EXPECT_EQ(4, frame.render_passes[2]->id.layer_id);
    381   EXPECT_EQ(2, frame.render_passes[2]->id.index);
    382 
    383   // The DelegatedRendererLayer should have added copies of its quads to
    384   // contributing RenderPasses.
    385   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    386   EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(),
    387             frame.render_passes[1]->quad_list[0]->rect.ToString());
    388 
    389   // Verify it added the right quads.
    390   ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size());
    391   EXPECT_EQ(gfx::Rect(0, 0, 7, 7).ToString(),
    392             frame.render_passes[2]->quad_list[0]->rect.ToString());
    393   EXPECT_EQ(gfx::Rect(6, 6, 6, 6).ToString(),
    394             frame.render_passes[2]->quad_list[1]->rect.ToString());
    395   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    396   EXPECT_EQ(gfx::Rect(0, 0, 6, 6).ToString(),
    397             frame.render_passes[1]->quad_list[0]->rect.ToString());
    398 
    399   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    400   host_impl_->DidDrawAllLayers(frame);
    401 }
    402 
    403 TEST_F(DelegatedRendererLayerImplTestOwnSurface, AddsQuadsToTargetRenderPass) {
    404   LayerTreeHostImpl::FrameData frame;
    405   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    406 
    407   // Each non-DelegatedRendererLayer added one RenderPass. The
    408   // DelegatedRendererLayer added two contributing passes and its owned surface
    409   // added one pass.
    410   ASSERT_EQ(6u, frame.render_passes.size());
    411 
    412   // The layer's target is the RenderPass owned by itself.
    413   EXPECT_EQ(RenderPass::Id(4, 0), frame.render_passes[3]->id);
    414 
    415   // The DelegatedRendererLayer should have added copies of quads in its root
    416   // RenderPass to its target RenderPass.
    417   // The layer_after also adds one quad.
    418   ASSERT_EQ(1u, frame.render_passes[3]->quad_list.size());
    419 
    420   // Verify it added the right quads.
    421   EXPECT_EQ(gfx::Rect(7, 7, 7, 7).ToString(),
    422             frame.render_passes[3]->quad_list[0]->rect.ToString());
    423 
    424   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    425   host_impl_->DidDrawAllLayers(frame);
    426 }
    427 
    428 TEST_F(DelegatedRendererLayerImplTestOwnSurface,
    429        QuadsFromRootRenderPassAreNotModifiedForTheTarget) {
    430   LayerTreeHostImpl::FrameData frame;
    431   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    432 
    433   // Each non-DelegatedRendererLayer added one RenderPass. The
    434   // DelegatedRendererLayer added two contributing passes and its owned surface
    435   // added one pass.
    436   ASSERT_EQ(6u, frame.render_passes.size());
    437 
    438   // Because the DelegatedRendererLayer owns a RenderSurfaceImpl, its root
    439   // RenderPass' quads do not need to be translated at all. However, they are
    440   // scaled from the frame's size (8x8) to the layer's bounds (10x10).
    441   gfx::Transform transform;
    442   transform.Scale(10.0 / 8.0, 10.0 / 8.0);
    443   EXPECT_TRANSFORMATION_MATRIX_EQ(
    444       transform, frame.render_passes[3]->quad_list[0]->quadTransform());
    445 
    446   // Quads from non-root RenderPasses should not be shifted either.
    447   ASSERT_EQ(2u, frame.render_passes[2]->quad_list.size());
    448   EXPECT_TRANSFORMATION_MATRIX_EQ(
    449       gfx::Transform(), frame.render_passes[2]->quad_list[0]->quadTransform());
    450   EXPECT_TRANSFORMATION_MATRIX_EQ(
    451       gfx::Transform(), frame.render_passes[2]->quad_list[1]->quadTransform());
    452   ASSERT_EQ(1u, frame.render_passes[1]->quad_list.size());
    453   EXPECT_TRANSFORMATION_MATRIX_EQ(
    454       gfx::Transform(), frame.render_passes[1]->quad_list[0]->quadTransform());
    455 
    456   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    457   host_impl_->DidDrawAllLayers(frame);
    458 }
    459 
    460 class DelegatedRendererLayerImplTestTransform
    461     : public DelegatedRendererLayerImplTest {
    462  public:
    463   void SetUpTest() {
    464     host_impl_->SetDeviceScaleFactor(2.f);
    465 
    466     scoped_ptr<LayerImpl> root_layer = LayerImpl::Create(
    467         host_impl_->active_tree(), 1);
    468     scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer =
    469         FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 2);
    470 
    471     host_impl_->SetViewportSize(gfx::Size(200, 200));
    472     root_layer->SetBounds(gfx::Size(100, 100));
    473 
    474     delegated_renderer_layer->SetPosition(gfx::Point(20, 20));
    475     delegated_renderer_layer->SetBounds(gfx::Size(30, 30));
    476     delegated_renderer_layer->SetContentBounds(gfx::Size(30, 30));
    477     delegated_renderer_layer->SetDrawsContent(true);
    478     gfx::Transform transform;
    479     transform.Scale(2.0, 2.0);
    480     transform.Translate(8.0, 8.0);
    481     delegated_renderer_layer->SetTransform(transform);
    482 
    483     ScopedPtrVector<RenderPass> delegated_render_passes;
    484 
    485     gfx::Size child_pass_content_bounds(7, 7);
    486     gfx::Rect child_pass_rect(20, 20, 7, 7);
    487     gfx::Transform child_pass_transform;
    488     child_pass_transform.Scale(0.8, 0.8);
    489     child_pass_transform.Translate(9.0, 9.0);
    490     gfx::Rect child_pass_clip_rect(21, 21, 3, 3);
    491     bool child_pass_clipped = false;
    492 
    493     {
    494       TestRenderPass* pass = AddRenderPass(
    495           &delegated_render_passes,
    496           RenderPass::Id(10, 7),
    497           child_pass_rect,
    498           gfx::Transform());
    499       MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list);
    500       AppendQuadsData data(pass->id);
    501       SharedQuadState* shared_quad_state = quad_sink.UseSharedQuadState(
    502           SharedQuadState::Create());
    503       shared_quad_state->SetAll(
    504           child_pass_transform,
    505           child_pass_content_bounds,
    506           child_pass_rect,
    507           child_pass_clip_rect,
    508           child_pass_clipped,
    509           1.f);
    510 
    511       scoped_ptr<SolidColorDrawQuad> color_quad;
    512       color_quad = SolidColorDrawQuad::Create();
    513       color_quad->SetNew(shared_quad_state, gfx::Rect(20, 20, 3, 7), 1u, false);
    514       quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    515 
    516       color_quad = SolidColorDrawQuad::Create();
    517       color_quad->SetNew(shared_quad_state, gfx::Rect(23, 20, 4, 7), 1u, false);
    518       quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    519     }
    520 
    521     gfx::Size root_pass_content_bounds(50, 50);
    522     gfx::Rect root_pass_rect(0, 0, 50, 50);
    523     gfx::Transform root_pass_transform;
    524     root_pass_transform.Scale(1.5, 1.5);
    525     root_pass_transform.Translate(7.0, 7.0);
    526     gfx::Rect root_pass_clip_rect(10, 10, 35, 35);
    527     bool root_pass_clipped = root_delegated_render_pass_is_clipped_;
    528 
    529     TestRenderPass* pass = AddRenderPass(
    530         &delegated_render_passes,
    531         RenderPass::Id(9, 6),
    532         root_pass_rect,
    533         gfx::Transform());
    534     MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list);
    535     AppendQuadsData data(pass->id);
    536     SharedQuadState* shared_quad_state =
    537         quad_sink.UseSharedQuadState(SharedQuadState::Create());
    538     shared_quad_state->SetAll(
    539         root_pass_transform,
    540         root_pass_content_bounds,
    541         root_pass_rect,
    542         root_pass_clip_rect,
    543         root_pass_clipped,
    544         1.f);
    545 
    546     scoped_ptr<RenderPassDrawQuad> render_pass_quad =
    547         RenderPassDrawQuad::Create();
    548     render_pass_quad->SetNew(
    549         shared_quad_state,
    550         gfx::Rect(5, 5, 7, 7),  // rect
    551         RenderPass::Id(10, 7),  // render_pass_id
    552         false,  // is_replica
    553         0,  // mask_resource_id
    554         child_pass_rect,  // contents_changed_since_last_frame
    555         gfx::RectF(),  // mask_uv_rect
    556         FilterOperations(),  // filters
    557         skia::RefPtr<SkImageFilter>(),  // filter
    558         FilterOperations());  // background_filters
    559     quad_sink.Append(render_pass_quad.PassAs<DrawQuad>(), &data);
    560 
    561     scoped_ptr<SolidColorDrawQuad> color_quad;
    562     color_quad = SolidColorDrawQuad::Create();
    563     color_quad->SetNew(shared_quad_state, gfx::Rect(0, 0, 10, 10), 1u, false);
    564     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    565 
    566     color_quad = SolidColorDrawQuad::Create();
    567     color_quad->SetNew(shared_quad_state, gfx::Rect(0, 10, 10, 10), 2u, false);
    568     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    569 
    570     color_quad = SolidColorDrawQuad::Create();
    571     color_quad->SetNew(shared_quad_state, gfx::Rect(10, 0, 10, 10), 3u, false);
    572     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    573 
    574     color_quad = SolidColorDrawQuad::Create();
    575     color_quad->SetNew(shared_quad_state, gfx::Rect(10, 10, 10, 10), 4u, false);
    576     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    577 
    578     delegated_renderer_layer->SetFrameDataForRenderPasses(
    579         &delegated_render_passes);
    580 
    581     // The RenderPasses should be taken by the layer.
    582     EXPECT_EQ(0u, delegated_render_passes.size());
    583 
    584     root_layer_ = root_layer.get();
    585     delegated_renderer_layer_ = delegated_renderer_layer.get();
    586 
    587     root_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>());
    588     host_impl_->active_tree()->SetRootLayer(root_layer.Pass());
    589   }
    590 
    591   void VerifyRenderPasses(
    592       const LayerTreeHostImpl::FrameData& frame,
    593       size_t num_render_passes,
    594       const SharedQuadState** root_delegated_shared_quad_state,
    595       const SharedQuadState** contrib_delegated_shared_quad_state) {
    596     ASSERT_EQ(num_render_passes, frame.render_passes.size());
    597     // The contributing render pass in the DelegatedRendererLayer.
    598     EXPECT_EQ(2, frame.render_passes[0]->id.layer_id);
    599     EXPECT_EQ(1, frame.render_passes[0]->id.index);
    600     // The root render pass.
    601     EXPECT_EQ(1, frame.render_passes.back()->id.layer_id);
    602     EXPECT_EQ(0, frame.render_passes.back()->id.index);
    603 
    604     const QuadList& contrib_delegated_quad_list =
    605         frame.render_passes[0]->quad_list;
    606     ASSERT_EQ(2u, contrib_delegated_quad_list.size());
    607 
    608     const QuadList& root_delegated_quad_list =
    609         frame.render_passes[1]->quad_list;
    610     ASSERT_EQ(5u, root_delegated_quad_list.size());
    611 
    612     // All quads in a render pass should share the same state.
    613     *contrib_delegated_shared_quad_state =
    614         contrib_delegated_quad_list[0]->shared_quad_state;
    615     EXPECT_EQ(*contrib_delegated_shared_quad_state,
    616               contrib_delegated_quad_list[1]->shared_quad_state);
    617 
    618     *root_delegated_shared_quad_state =
    619         root_delegated_quad_list[0]->shared_quad_state;
    620     EXPECT_EQ(*root_delegated_shared_quad_state,
    621               root_delegated_quad_list[1]->shared_quad_state);
    622     EXPECT_EQ(*root_delegated_shared_quad_state,
    623               root_delegated_quad_list[2]->shared_quad_state);
    624     EXPECT_EQ(*root_delegated_shared_quad_state,
    625               root_delegated_quad_list[3]->shared_quad_state);
    626     EXPECT_EQ(*root_delegated_shared_quad_state,
    627               root_delegated_quad_list[4]->shared_quad_state);
    628 
    629     EXPECT_NE(*contrib_delegated_shared_quad_state,
    630               *root_delegated_shared_quad_state);
    631   }
    632 
    633  protected:
    634   LayerImpl* root_layer_;
    635   DelegatedRendererLayerImpl* delegated_renderer_layer_;
    636   bool root_delegated_render_pass_is_clipped_;
    637 };
    638 
    639 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsUnclipped_NoSurface) {
    640   root_delegated_render_pass_is_clipped_ = false;
    641   SetUpTest();
    642 
    643   LayerTreeHostImpl::FrameData frame;
    644   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    645 
    646   const SharedQuadState* root_delegated_shared_quad_state = NULL;
    647   const SharedQuadState* contrib_delegated_shared_quad_state = NULL;
    648   VerifyRenderPasses(
    649       frame,
    650       2,
    651       &root_delegated_shared_quad_state,
    652       &contrib_delegated_shared_quad_state);
    653 
    654   // When the quads don't have a clip of their own, the clip rect is set to
    655   // the drawable_content_rect of the delegated renderer layer.
    656   EXPECT_EQ(gfx::Rect(42, 42, 120, 120).ToString(),
    657             root_delegated_shared_quad_state->clip_rect.ToString());
    658 
    659   // Even though the quads in the root pass have no clip of their own, they
    660   // inherit the clip rect from the delegated renderer layer if it does not
    661   // own a surface.
    662   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
    663 
    664   gfx::Transform expected;
    665   // Device scale factor is 2.
    666   expected.Scale(2.0, 2.0);
    667   // This is the transform from the layer's space to its target.
    668   // The position (20) - the width / scale (30 / 2) = 20 - 15 = 5
    669   expected.Translate(5.0, 5.0);
    670   expected.Scale(2.0, 2.0);
    671   expected.Translate(8.0, 8.0);
    672   // The frame has size 50x50 but the layer's bounds are 30x30.
    673   expected.Scale(30.0 / 50.0, 30.0 / 50.0);
    674   // This is the transform within the source frame.
    675   expected.Scale(1.5, 1.5);
    676   expected.Translate(7.0, 7.0);
    677   EXPECT_TRANSFORMATION_MATRIX_EQ(
    678       expected, root_delegated_shared_quad_state->content_to_target_transform);
    679 
    680   // The contributing render pass should not be transformed from its input.
    681   EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(),
    682             contrib_delegated_shared_quad_state->clip_rect.ToString());
    683   EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped);
    684   expected.MakeIdentity();
    685   expected.Scale(0.8, 0.8);
    686   expected.Translate(9.0, 9.0);
    687   EXPECT_TRANSFORMATION_MATRIX_EQ(
    688       expected,
    689       contrib_delegated_shared_quad_state->content_to_target_transform);
    690 
    691   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    692   host_impl_->DidDrawAllLayers(frame);
    693 }
    694 
    695 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsClipped_NoSurface) {
    696   root_delegated_render_pass_is_clipped_ = true;
    697   SetUpTest();
    698 
    699   LayerTreeHostImpl::FrameData frame;
    700   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    701 
    702   const SharedQuadState* root_delegated_shared_quad_state = NULL;
    703   const SharedQuadState* contrib_delegated_shared_quad_state = NULL;
    704   VerifyRenderPasses(
    705       frame,
    706       2,
    707       &root_delegated_shared_quad_state,
    708       &contrib_delegated_shared_quad_state);
    709 
    710   // Since the quads have a clip_rect it should be modified by delegated
    711   // renderer layer's draw_transform.
    712   // The position of the resulting clip_rect is:
    713   // (clip rect position (10) * scale to layer (30/50) + translate (8)) *
    714   //     layer scale (2) + layer position (20) = 48
    715   // But the layer is centered, so: 48 - (width / 2) = 48 - 30 / 2 = 33
    716   // The device scale is 2, so everything gets doubled, giving 66.
    717   //
    718   // The size is 35x35 scaled to fit inside the layer's bounds at 30x30 from
    719   // a frame at 50x50: 35 * 2 (device scale) * 30 / 50 = 42. The device scale
    720   // doubles this to 84.
    721   EXPECT_EQ(gfx::Rect(66, 66, 84, 84).ToString(),
    722             root_delegated_shared_quad_state->clip_rect.ToString());
    723 
    724   // The quads had a clip and it should be preserved.
    725   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
    726 
    727   gfx::Transform expected;
    728   // Device scale factor is 2.
    729   expected.Scale(2.0, 2.0);
    730   // This is the transform from the layer's space to its target.
    731   // The position (20) - the width / scale (30 / 2) = 20 - 15 = 5
    732   expected.Translate(5.0, 5.0);
    733   expected.Scale(2.0, 2.0);
    734   expected.Translate(8.0, 8.0);
    735   // The frame has size 50x50 but the layer's bounds are 30x30.
    736   expected.Scale(30.0 / 50.0, 30.0 / 50.0);
    737   // This is the transform within the source frame.
    738   expected.Scale(1.5, 1.5);
    739   expected.Translate(7.0, 7.0);
    740   EXPECT_TRANSFORMATION_MATRIX_EQ(
    741       expected, root_delegated_shared_quad_state->content_to_target_transform);
    742 
    743   // The contributing render pass should not be transformed from its input.
    744   EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(),
    745             contrib_delegated_shared_quad_state->clip_rect.ToString());
    746   EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped);
    747   expected.MakeIdentity();
    748   expected.Scale(0.8, 0.8);
    749   expected.Translate(9.0, 9.0);
    750   EXPECT_TRANSFORMATION_MATRIX_EQ(
    751       expected,
    752       contrib_delegated_shared_quad_state->content_to_target_transform);
    753 
    754   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    755   host_impl_->DidDrawAllLayers(frame);
    756 }
    757 
    758 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsUnclipped_Surface) {
    759   root_delegated_render_pass_is_clipped_ = false;
    760   SetUpTest();
    761 
    762   delegated_renderer_layer_->SetForceRenderSurface(true);
    763 
    764   LayerTreeHostImpl::FrameData frame;
    765   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    766 
    767   const SharedQuadState* root_delegated_shared_quad_state = NULL;
    768   const SharedQuadState* contrib_delegated_shared_quad_state = NULL;
    769   VerifyRenderPasses(
    770       frame,
    771       3,
    772       &root_delegated_shared_quad_state,
    773       &contrib_delegated_shared_quad_state);
    774 
    775   // When the layer owns a surface, then its position and translation are not
    776   // a part of its draw transform.
    777   // The position of the resulting clip_rect is:
    778   // (clip rect position (10) * scale to layer (30/50)) * device scale (2) = 12
    779   // The size is 35x35 scaled to fit inside the layer's bounds at 30x30 from
    780   // a frame at 50x50: 35 * 2 (device scale) * 30 / 50 = 42.
    781   EXPECT_EQ(gfx::Rect(12, 12, 42, 42).ToString(),
    782             root_delegated_shared_quad_state->clip_rect.ToString());
    783 
    784   // Since the layer owns a surface it doesn't need to clip its quads, so
    785   // unclipped quads remain unclipped.
    786   EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped);
    787 
    788   gfx::Transform expected;
    789   // Device scale factor is 2.
    790   expected.Scale(2.0, 2.0);
    791   // The frame has size 50x50 but the layer's bounds are 30x30.
    792   expected.Scale(30.0 / 50.0, 30.0 / 50.0);
    793   // This is the transform within the source frame.
    794   expected.Scale(1.5, 1.5);
    795   expected.Translate(7.0, 7.0);
    796   EXPECT_TRANSFORMATION_MATRIX_EQ(
    797       expected, root_delegated_shared_quad_state->content_to_target_transform);
    798 
    799   // The contributing render pass should not be transformed from its input.
    800   EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(),
    801             contrib_delegated_shared_quad_state->clip_rect.ToString());
    802   EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped);
    803   expected.MakeIdentity();
    804   expected.Scale(0.8, 0.8);
    805   expected.Translate(9.0, 9.0);
    806   EXPECT_TRANSFORMATION_MATRIX_EQ(
    807       expected,
    808       contrib_delegated_shared_quad_state->content_to_target_transform);
    809 
    810   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    811   host_impl_->DidDrawAllLayers(frame);
    812 }
    813 
    814 TEST_F(DelegatedRendererLayerImplTestTransform, QuadsClipped_Surface) {
    815   root_delegated_render_pass_is_clipped_ = true;
    816   SetUpTest();
    817 
    818   delegated_renderer_layer_->SetForceRenderSurface(true);
    819 
    820   LayerTreeHostImpl::FrameData frame;
    821   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
    822 
    823   const SharedQuadState* root_delegated_shared_quad_state = NULL;
    824   const SharedQuadState* contrib_delegated_shared_quad_state = NULL;
    825   VerifyRenderPasses(
    826       frame,
    827       3,
    828       &root_delegated_shared_quad_state,
    829       &contrib_delegated_shared_quad_state);
    830 
    831   // When the layer owns a surface, then its position and translation are not
    832   // a part of its draw transform.
    833   // The position of the resulting clip_rect is:
    834   // (clip rect position (10) * scale to layer (30/50)) * device scale (2) = 12
    835   // The size is 35x35 scaled to fit inside the layer's bounds at 30x30 from
    836   // a frame at 50x50: 35 * 2 (device scale) * 30 / 50 = 42.
    837   EXPECT_EQ(gfx::Rect(12, 12, 42, 42).ToString(),
    838             root_delegated_shared_quad_state->clip_rect.ToString());
    839 
    840   // The quads had a clip and it should be preserved.
    841   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
    842 
    843   gfx::Transform expected;
    844   // Device scale factor is 2.
    845   expected.Scale(2.0, 2.0);
    846   // The frame has size 50x50 but the layer's bounds are 30x30.
    847   expected.Scale(30.0 / 50.0, 30.0 / 50.0);
    848   // This is the transform within the source frame.
    849   expected.Scale(1.5, 1.5);
    850   expected.Translate(7.0, 7.0);
    851   EXPECT_TRANSFORMATION_MATRIX_EQ(
    852       expected, root_delegated_shared_quad_state->content_to_target_transform);
    853 
    854   // The contributing render pass should not be transformed from its input.
    855   EXPECT_EQ(gfx::Rect(21, 21, 3, 3).ToString(),
    856             contrib_delegated_shared_quad_state->clip_rect.ToString());
    857   EXPECT_FALSE(contrib_delegated_shared_quad_state->is_clipped);
    858   expected.MakeIdentity();
    859   expected.Scale(0.8, 0.8);
    860   expected.Translate(9.0, 9.0);
    861   EXPECT_TRANSFORMATION_MATRIX_EQ(
    862       expected,
    863       contrib_delegated_shared_quad_state->content_to_target_transform);
    864 
    865   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
    866   host_impl_->DidDrawAllLayers(frame);
    867 }
    868 
    869 class DelegatedRendererLayerImplTestClip
    870     : public DelegatedRendererLayerImplTest {
    871  public:
    872   void SetUpTest() {
    873     scoped_ptr<LayerImpl> root_layer =
    874         LayerImpl::Create(host_impl_->active_tree(), 1);
    875     scoped_ptr<FakeDelegatedRendererLayerImpl> delegated_renderer_layer =
    876         FakeDelegatedRendererLayerImpl::Create(host_impl_->active_tree(), 2);
    877     scoped_ptr<LayerImpl> clip_layer =
    878         LayerImpl::Create(host_impl_->active_tree(), 3);
    879     scoped_ptr<LayerImpl> origin_layer =
    880         LayerImpl::Create(host_impl_->active_tree(), 4);
    881 
    882     host_impl_->SetViewportSize(gfx::Size(100, 100));
    883     root_layer->SetBounds(gfx::Size(100, 100));
    884 
    885     delegated_renderer_layer->SetPosition(gfx::Point(20, 20));
    886     delegated_renderer_layer->SetBounds(gfx::Size(50, 50));
    887     delegated_renderer_layer->SetContentBounds(gfx::Size(50, 50));
    888     delegated_renderer_layer->SetDrawsContent(true);
    889 
    890     ScopedPtrVector<RenderPass> delegated_render_passes;
    891 
    892     gfx::Size child_pass_content_bounds(7, 7);
    893     gfx::Rect child_pass_rect(20, 20, 7, 7);
    894     gfx::Transform child_pass_transform;
    895     gfx::Rect child_pass_clip_rect(21, 21, 3, 3);
    896     bool child_pass_clipped = false;
    897 
    898     {
    899       TestRenderPass* pass = AddRenderPass(
    900           &delegated_render_passes,
    901           RenderPass::Id(10, 7),
    902           child_pass_rect,
    903           gfx::Transform());
    904       MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list);
    905       AppendQuadsData data(pass->id);
    906       SharedQuadState* shared_quad_state =
    907           quad_sink.UseSharedQuadState(SharedQuadState::Create());
    908       shared_quad_state->SetAll(
    909           child_pass_transform,
    910           child_pass_content_bounds,
    911           child_pass_rect,
    912           child_pass_clip_rect,
    913           child_pass_clipped,
    914           1.f);
    915 
    916       scoped_ptr<SolidColorDrawQuad> color_quad;
    917       color_quad = SolidColorDrawQuad::Create();
    918       color_quad->SetNew(shared_quad_state, gfx::Rect(20, 20, 3, 7), 1u, false);
    919       quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    920 
    921       color_quad = SolidColorDrawQuad::Create();
    922       color_quad->SetNew(shared_quad_state, gfx::Rect(23, 20, 4, 7), 1u, false);
    923       quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    924     }
    925 
    926     gfx::Size root_pass_content_bounds(50, 50);
    927     gfx::Rect root_pass_rect(0, 0, 50, 50);
    928     gfx::Transform root_pass_transform;
    929     gfx::Rect root_pass_clip_rect(5, 5, 40, 40);
    930     bool root_pass_clipped = root_delegated_render_pass_is_clipped_;
    931 
    932     TestRenderPass* pass = AddRenderPass(
    933         &delegated_render_passes,
    934         RenderPass::Id(9, 6),
    935         root_pass_rect,
    936         gfx::Transform());
    937     MockQuadCuller quad_sink(&pass->quad_list, &pass->shared_quad_state_list);
    938     AppendQuadsData data(pass->id);
    939     SharedQuadState* shared_quad_state =
    940         quad_sink.UseSharedQuadState(SharedQuadState::Create());
    941     shared_quad_state->SetAll(root_pass_transform,
    942                               root_pass_content_bounds,
    943                               root_pass_rect,
    944                               root_pass_clip_rect,
    945                               root_pass_clipped,
    946                               1.f);
    947 
    948     scoped_ptr<RenderPassDrawQuad> render_pass_quad =
    949         RenderPassDrawQuad::Create();
    950     render_pass_quad->SetNew(
    951         shared_quad_state,
    952         gfx::Rect(5, 5, 7, 7),  // rect
    953         RenderPass::Id(10, 7),  // render_pass_id
    954         false,  // is_replica
    955         0,  // mask_resource_id
    956         child_pass_rect,  // contents_changed_since_last_frame
    957         gfx::RectF(),  // mask_uv_rect
    958         FilterOperations(),  // filters
    959         skia::RefPtr<SkImageFilter>(),  // filter
    960         FilterOperations());  // background_filters
    961     quad_sink.Append(render_pass_quad.PassAs<DrawQuad>(), &data);
    962 
    963     scoped_ptr<SolidColorDrawQuad> color_quad;
    964     color_quad = SolidColorDrawQuad::Create();
    965     color_quad->SetNew(shared_quad_state, gfx::Rect(0, 0, 10, 10), 1u, false);
    966     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    967 
    968     color_quad = SolidColorDrawQuad::Create();
    969     color_quad->SetNew(shared_quad_state, gfx::Rect(0, 10, 10, 10), 2u, false);
    970     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    971 
    972     color_quad = SolidColorDrawQuad::Create();
    973     color_quad->SetNew(shared_quad_state, gfx::Rect(10, 0, 10, 10), 3u, false);
    974     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    975 
    976     color_quad = SolidColorDrawQuad::Create();
    977     color_quad->SetNew(shared_quad_state, gfx::Rect(10, 10, 10, 10), 4u, false);
    978     quad_sink.Append(color_quad.PassAs<DrawQuad>(), &data);
    979 
    980     delegated_renderer_layer->SetFrameDataForRenderPasses(
    981         &delegated_render_passes);
    982 
    983     // The RenderPasses should be taken by the layer.
    984     EXPECT_EQ(0u, delegated_render_passes.size());
    985 
    986     root_layer_ = root_layer.get();
    987     delegated_renderer_layer_ = delegated_renderer_layer.get();
    988 
    989     if (clip_delegated_renderer_layer_) {
    990       gfx::Rect clip_rect(21, 27, 23, 21);
    991 
    992       clip_layer->SetPosition(clip_rect.origin());
    993       clip_layer->SetBounds(clip_rect.size());
    994       clip_layer->SetContentBounds(clip_rect.size());
    995       clip_layer->SetMasksToBounds(true);
    996 
    997       origin_layer->SetPosition(
    998           gfx::PointAtOffsetFromOrigin(-clip_rect.OffsetFromOrigin()));
    999 
   1000       origin_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>());
   1001       clip_layer->AddChild(origin_layer.Pass());
   1002       root_layer->AddChild(clip_layer.Pass());
   1003     } else {
   1004       root_layer->AddChild(delegated_renderer_layer.PassAs<LayerImpl>());
   1005     }
   1006 
   1007     host_impl_->active_tree()->SetRootLayer(root_layer.Pass());
   1008   }
   1009 
   1010  protected:
   1011   LayerImpl* root_layer_;
   1012   DelegatedRendererLayerImpl* delegated_renderer_layer_;
   1013   bool root_delegated_render_pass_is_clipped_;
   1014   bool clip_delegated_renderer_layer_;
   1015 };
   1016 
   1017 TEST_F(DelegatedRendererLayerImplTestClip,
   1018        QuadsUnclipped_LayerUnclipped_NoSurface) {
   1019   root_delegated_render_pass_is_clipped_ = false;
   1020   clip_delegated_renderer_layer_ = false;
   1021   SetUpTest();
   1022 
   1023   LayerTreeHostImpl::FrameData frame;
   1024   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1025 
   1026   ASSERT_EQ(2u, frame.render_passes.size());
   1027   const QuadList& contrib_delegated_quad_list =
   1028       frame.render_passes[0]->quad_list;
   1029   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1030   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1031   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1032   const SharedQuadState* root_delegated_shared_quad_state =
   1033       root_delegated_quad_list[0]->shared_quad_state;
   1034 
   1035   // When the quads don't have a clip of their own, the clip rect is set to
   1036   // the drawable_content_rect of the delegated renderer layer.
   1037   EXPECT_EQ(gfx::Rect(20, 20, 50, 50).ToString(),
   1038             root_delegated_shared_quad_state->clip_rect.ToString());
   1039   // Quads are clipped to the delegated renderer layer.
   1040   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1041 
   1042   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1043   host_impl_->DidDrawAllLayers(frame);
   1044 }
   1045 
   1046 TEST_F(DelegatedRendererLayerImplTestClip,
   1047        QuadsClipped_LayerUnclipped_NoSurface) {
   1048   root_delegated_render_pass_is_clipped_ = true;
   1049   clip_delegated_renderer_layer_ = false;
   1050   SetUpTest();
   1051 
   1052   LayerTreeHostImpl::FrameData frame;
   1053   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1054 
   1055   ASSERT_EQ(2u, frame.render_passes.size());
   1056   const QuadList& contrib_delegated_quad_list =
   1057       frame.render_passes[0]->quad_list;
   1058   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1059   const QuadList& root_delegated_quad_list =
   1060       frame.render_passes[1]->quad_list;
   1061   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1062   const SharedQuadState* root_delegated_shared_quad_state =
   1063       root_delegated_quad_list[0]->shared_quad_state;
   1064 
   1065   // When the quads have a clip of their own, it is used.
   1066   EXPECT_EQ(gfx::Rect(25, 25, 40, 40).ToString(),
   1067             root_delegated_shared_quad_state->clip_rect.ToString());
   1068   // Quads came with a clip rect.
   1069   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1070 
   1071   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1072   host_impl_->DidDrawAllLayers(frame);
   1073 }
   1074 
   1075 TEST_F(DelegatedRendererLayerImplTestClip,
   1076        QuadsUnclipped_LayerClipped_NoSurface) {
   1077   root_delegated_render_pass_is_clipped_ = false;
   1078   clip_delegated_renderer_layer_ = true;
   1079   SetUpTest();
   1080 
   1081   LayerTreeHostImpl::FrameData frame;
   1082   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1083 
   1084   ASSERT_EQ(2u, frame.render_passes.size());
   1085   const QuadList& contrib_delegated_quad_list =
   1086       frame.render_passes[0]->quad_list;
   1087   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1088   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1089   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1090   const SharedQuadState* root_delegated_shared_quad_state =
   1091       root_delegated_quad_list[0]->shared_quad_state;
   1092 
   1093   // When the quads don't have a clip of their own, the clip rect is set to
   1094   // the drawable_content_rect of the delegated renderer layer. When the layer
   1095   // is clipped, that should be seen in the quads' clip_rect.
   1096   EXPECT_EQ(gfx::Rect(21, 27, 23, 21).ToString(),
   1097             root_delegated_shared_quad_state->clip_rect.ToString());
   1098   // Quads are clipped to the delegated renderer layer.
   1099   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1100 
   1101   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1102   host_impl_->DidDrawAllLayers(frame);
   1103 }
   1104 
   1105 TEST_F(DelegatedRendererLayerImplTestClip,
   1106        QuadsClipped_LayerClipped_NoSurface) {
   1107   root_delegated_render_pass_is_clipped_ = true;
   1108   clip_delegated_renderer_layer_ = true;
   1109   SetUpTest();
   1110 
   1111   LayerTreeHostImpl::FrameData frame;
   1112   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1113 
   1114   ASSERT_EQ(2u, frame.render_passes.size());
   1115   const QuadList& contrib_delegated_quad_list =
   1116       frame.render_passes[0]->quad_list;
   1117   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1118   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1119   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1120   const SharedQuadState* root_delegated_shared_quad_state =
   1121       root_delegated_quad_list[0]->shared_quad_state;
   1122 
   1123   // When the quads have a clip of their own, it is used, but it is
   1124   // combined with the clip rect of the delegated renderer layer.
   1125   EXPECT_EQ(gfx::Rect(25, 27, 19, 21).ToString(),
   1126             root_delegated_shared_quad_state->clip_rect.ToString());
   1127   // Quads came with a clip rect.
   1128   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1129 
   1130   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1131   host_impl_->DidDrawAllLayers(frame);
   1132 }
   1133 
   1134 TEST_F(DelegatedRendererLayerImplTestClip,
   1135        QuadsUnclipped_LayerUnclipped_Surface) {
   1136   root_delegated_render_pass_is_clipped_ = false;
   1137   clip_delegated_renderer_layer_ = false;
   1138   SetUpTest();
   1139 
   1140   delegated_renderer_layer_->SetForceRenderSurface(true);
   1141 
   1142   LayerTreeHostImpl::FrameData frame;
   1143   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1144 
   1145   ASSERT_EQ(3u, frame.render_passes.size());
   1146   const QuadList& contrib_delegated_quad_list =
   1147       frame.render_passes[0]->quad_list;
   1148   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1149   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1150   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1151   const SharedQuadState* root_delegated_shared_quad_state =
   1152       root_delegated_quad_list[0]->shared_quad_state;
   1153 
   1154   // When the layer owns a surface, the quads don't need to be clipped
   1155   // further than they already specify. If they aren't clipped, then their
   1156   // clip rect is ignored, and they are not set as clipped.
   1157   EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped);
   1158 
   1159   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1160   host_impl_->DidDrawAllLayers(frame);
   1161 }
   1162 
   1163 TEST_F(DelegatedRendererLayerImplTestClip,
   1164        QuadsClipped_LayerUnclipped_Surface) {
   1165   root_delegated_render_pass_is_clipped_ = true;
   1166   clip_delegated_renderer_layer_ = false;
   1167   SetUpTest();
   1168 
   1169   delegated_renderer_layer_->SetForceRenderSurface(true);
   1170 
   1171   LayerTreeHostImpl::FrameData frame;
   1172   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1173 
   1174   ASSERT_EQ(3u, frame.render_passes.size());
   1175   const QuadList& contrib_delegated_quad_list =
   1176       frame.render_passes[0]->quad_list;
   1177   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1178   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1179   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1180   const SharedQuadState* root_delegated_shared_quad_state =
   1181       root_delegated_quad_list[0]->shared_quad_state;
   1182 
   1183   // When the quads have a clip of their own, it is used.
   1184   EXPECT_EQ(gfx::Rect(5, 5, 40, 40).ToString(),
   1185             root_delegated_shared_quad_state->clip_rect.ToString());
   1186   // Quads came with a clip rect.
   1187   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1188 
   1189   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1190   host_impl_->DidDrawAllLayers(frame);
   1191 }
   1192 
   1193 TEST_F(DelegatedRendererLayerImplTestClip,
   1194        QuadsUnclipped_LayerClipped_Surface) {
   1195   root_delegated_render_pass_is_clipped_ = false;
   1196   clip_delegated_renderer_layer_ = true;
   1197   SetUpTest();
   1198 
   1199   delegated_renderer_layer_->SetForceRenderSurface(true);
   1200 
   1201   LayerTreeHostImpl::FrameData frame;
   1202   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1203 
   1204   ASSERT_EQ(3u, frame.render_passes.size());
   1205   const QuadList& contrib_delegated_quad_list =
   1206       frame.render_passes[0]->quad_list;
   1207   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1208   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1209   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1210   const SharedQuadState* root_delegated_shared_quad_state =
   1211       root_delegated_quad_list[0]->shared_quad_state;
   1212 
   1213   // When the layer owns a surface, the quads don't need to be clipped
   1214   // further than they already specify. If they aren't clipped, then their
   1215   // clip rect is ignored, and they are not set as clipped.
   1216   EXPECT_FALSE(root_delegated_shared_quad_state->is_clipped);
   1217 
   1218   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1219   host_impl_->DidDrawAllLayers(frame);
   1220 }
   1221 
   1222 TEST_F(DelegatedRendererLayerImplTestClip, QuadsClipped_LayerClipped_Surface) {
   1223   root_delegated_render_pass_is_clipped_ = true;
   1224   clip_delegated_renderer_layer_ = true;
   1225   SetUpTest();
   1226 
   1227   delegated_renderer_layer_->SetForceRenderSurface(true);
   1228 
   1229   LayerTreeHostImpl::FrameData frame;
   1230   EXPECT_TRUE(host_impl_->PrepareToDraw(&frame, gfx::Rect()));
   1231 
   1232   ASSERT_EQ(3u, frame.render_passes.size());
   1233   const QuadList& contrib_delegated_quad_list =
   1234       frame.render_passes[0]->quad_list;
   1235   ASSERT_EQ(2u, contrib_delegated_quad_list.size());
   1236   const QuadList& root_delegated_quad_list = frame.render_passes[1]->quad_list;
   1237   ASSERT_EQ(5u, root_delegated_quad_list.size());
   1238   const SharedQuadState* root_delegated_shared_quad_state =
   1239       root_delegated_quad_list[0]->shared_quad_state;
   1240 
   1241   // When the quads have a clip of their own, it is used, but it is
   1242   // combined with the clip rect of the delegated renderer layer. If the
   1243   // layer owns a surface, then it does not have a clip rect of its own.
   1244   EXPECT_EQ(gfx::Rect(5, 5, 40, 40).ToString(),
   1245             root_delegated_shared_quad_state->clip_rect.ToString());
   1246   // Quads came with a clip rect.
   1247   EXPECT_TRUE(root_delegated_shared_quad_state->is_clipped);
   1248 
   1249   host_impl_->DrawLayers(&frame, base::TimeTicks::Now());
   1250   host_impl_->DidDrawAllLayers(frame);
   1251 }
   1252 
   1253 }  // namespace
   1254 }  // namespace cc
   1255