Home | History | Annotate | Download | only in layers
      1 // Copyright 2013 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/append_quads_data.h"
      6 #include "cc/layers/ui_resource_layer_impl.h"
      7 #include "cc/quads/draw_quad.h"
      8 #include "cc/resources/ui_resource_bitmap.h"
      9 #include "cc/resources/ui_resource_client.h"
     10 #include "cc/test/fake_impl_proxy.h"
     11 #include "cc/test/fake_layer_tree_host_impl.h"
     12 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
     13 #include "cc/test/layer_test_common.h"
     14 #include "cc/test/test_shared_bitmap_manager.h"
     15 #include "cc/trees/single_thread_proxy.h"
     16 #include "testing/gmock/include/gmock/gmock.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 #include "ui/gfx/transform.h"
     19 
     20 namespace cc {
     21 namespace {
     22 
     23 scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
     24     FakeUIResourceLayerTreeHostImpl* host_impl,
     25     const gfx::Size& bitmap_size,
     26     const gfx::Size& layer_size,
     27     bool opaque,
     28     UIResourceId uid) {
     29   gfx::Rect visible_content_rect(layer_size);
     30   scoped_ptr<UIResourceLayerImpl> layer =
     31       UIResourceLayerImpl::Create(host_impl->active_tree(), 1);
     32   layer->draw_properties().visible_content_rect = visible_content_rect;
     33   layer->SetBounds(layer_size);
     34   layer->SetContentBounds(layer_size);
     35   layer->CreateRenderSurface();
     36   layer->draw_properties().render_target = layer.get();
     37 
     38   UIResourceBitmap bitmap(bitmap_size, opaque);
     39 
     40   host_impl->CreateUIResource(uid, bitmap);
     41   layer->SetUIResourceId(uid);
     42 
     43   return layer.Pass();
     44 }
     45 
     46 void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer,
     47                   size_t expected_quad_size) {
     48   MockOcclusionTracker<LayerImpl> occlusion_tracker;
     49   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
     50 
     51   AppendQuadsData data;
     52   layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
     53 
     54   // Verify quad rects
     55   const QuadList& quads = render_pass->quad_list;
     56   EXPECT_EQ(expected_quad_size, quads.size());
     57 }
     58 
     59 TEST(UIResourceLayerImplTest, VerifyDrawQuads) {
     60   FakeImplProxy proxy;
     61   TestSharedBitmapManager shared_bitmap_manager;
     62   FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
     63   // Make sure we're appending quads when there are valid values.
     64   gfx::Size bitmap_size(100, 100);
     65   gfx::Size layer_size(100, 100);;
     66   size_t expected_quad_size = 1;
     67   bool opaque = true;
     68   UIResourceId uid = 1;
     69   scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
     70                                                                   bitmap_size,
     71                                                                   layer_size,
     72                                                                   opaque,
     73                                                                   uid);
     74   QuadSizeTest(layer.Pass(), expected_quad_size);
     75 
     76   // Make sure we're not appending quads when there are invalid values.
     77   expected_quad_size = 0;
     78   uid = 0;
     79   layer = GenerateUIResourceLayer(&host_impl,
     80                                   bitmap_size,
     81                                   layer_size,
     82                                   opaque,
     83                                   uid);
     84   QuadSizeTest(layer.Pass(), expected_quad_size);
     85 }
     86 
     87 void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer,
     88                  const gfx::Rect& expected_opaque_bounds) {
     89   MockOcclusionTracker<LayerImpl> occlusion_tracker;
     90   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
     91 
     92   AppendQuadsData data;
     93   layer->AppendQuads(render_pass.get(), occlusion_tracker, &data);
     94 
     95   // Verify quad rects
     96   const QuadList& quads = render_pass->quad_list;
     97   EXPECT_GE(quads.size(), (size_t)0);
     98   gfx::Rect opaque_rect = quads.front()->opaque_rect;
     99   EXPECT_EQ(expected_opaque_bounds, opaque_rect);
    100 }
    101 
    102 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) {
    103   FakeImplProxy proxy;
    104   TestSharedBitmapManager shared_bitmap_manager;
    105   FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
    106 
    107   gfx::Size bitmap_size(100, 100);
    108   gfx::Size layer_size(100, 100);;
    109   bool opaque = false;
    110   UIResourceId uid = 1;
    111   scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
    112                                                                   bitmap_size,
    113                                                                   layer_size,
    114                                                                   opaque,
    115                                                                   uid);
    116   gfx::Rect expected_opaque_bounds;
    117   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    118 
    119   opaque = true;
    120   layer = GenerateUIResourceLayer(&host_impl,
    121                                   bitmap_size,
    122                                   layer_size,
    123                                   opaque,
    124                                   uid);
    125   expected_opaque_bounds = gfx::Rect(layer->bounds());
    126   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    127 }
    128 
    129 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) {
    130   FakeImplProxy proxy;
    131   TestSharedBitmapManager shared_bitmap_manager;
    132   FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
    133 
    134   gfx::Size bitmap_size(100, 100);
    135   gfx::Size layer_size(100, 100);
    136   bool skbitmap_opaque = false;
    137   UIResourceId uid = 1;
    138   scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(
    139       &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
    140   layer->SetContentsOpaque(false);
    141   gfx::Rect expected_opaque_bounds;
    142   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    143 
    144   layer = GenerateUIResourceLayer(
    145       &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
    146   layer->SetContentsOpaque(true);
    147   expected_opaque_bounds = gfx::Rect(layer->bounds());
    148   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    149 }
    150 
    151 TEST(UIResourceLayerImplTest, Occlusion) {
    152   gfx::Size layer_size(1000, 1000);
    153   gfx::Size viewport_size(1000, 1000);
    154 
    155   LayerTestCommon::LayerImplTest impl;
    156 
    157   SkBitmap sk_bitmap;
    158   sk_bitmap.allocN32Pixels(10, 10);
    159   sk_bitmap.setImmutable();
    160   UIResourceId uid = 5;
    161   UIResourceBitmap bitmap(sk_bitmap);
    162   impl.host_impl()->CreateUIResource(uid, bitmap);
    163 
    164   UIResourceLayerImpl* ui_resource_layer_impl =
    165       impl.AddChildToRoot<UIResourceLayerImpl>();
    166   ui_resource_layer_impl->SetBounds(layer_size);
    167   ui_resource_layer_impl->SetContentBounds(layer_size);
    168   ui_resource_layer_impl->SetDrawsContent(true);
    169   ui_resource_layer_impl->SetUIResourceId(uid);
    170 
    171   impl.CalcDrawProps(viewport_size);
    172 
    173   {
    174     SCOPED_TRACE("No occlusion");
    175     gfx::Rect occluded;
    176     impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
    177 
    178     LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
    179                                                  gfx::Rect(layer_size));
    180     EXPECT_EQ(1u, impl.quad_list().size());
    181   }
    182 
    183   {
    184     SCOPED_TRACE("Full occlusion");
    185     gfx::Rect occluded(ui_resource_layer_impl->visible_content_rect());
    186     impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
    187 
    188     LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
    189     EXPECT_EQ(impl.quad_list().size(), 0u);
    190   }
    191 
    192   {
    193     SCOPED_TRACE("Partial occlusion");
    194     gfx::Rect occluded(200, 0, 800, 1000);
    195     impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
    196 
    197     size_t partially_occluded_count = 0;
    198     LayerTestCommon::VerifyQuadsAreOccluded(
    199         impl.quad_list(), occluded, &partially_occluded_count);
    200     // The layer outputs one quad, which is partially occluded.
    201     EXPECT_EQ(1u, impl.quad_list().size());
    202     EXPECT_EQ(1u, partially_occluded_count);
    203   }
    204 }
    205 
    206 }  // namespace
    207 }  // namespace cc
    208