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/resources/ui_resource_bitmap.h"
      8 #include "cc/resources/ui_resource_client.h"
      9 #include "cc/test/fake_impl_proxy.h"
     10 #include "cc/test/fake_layer_tree_host_impl.h"
     11 #include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
     12 #include "cc/test/layer_test_common.h"
     13 #include "cc/test/mock_quad_culler.h"
     14 #include "cc/trees/single_thread_proxy.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "ui/gfx/transform.h"
     18 
     19 namespace cc {
     20 namespace {
     21 
     22 scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
     23     FakeUIResourceLayerTreeHostImpl* host_impl,
     24     gfx::Size bitmap_size,
     25     gfx::Size layer_size,
     26     bool opaque,
     27     UIResourceId uid) {
     28   gfx::Rect visible_content_rect(layer_size);
     29   scoped_ptr<UIResourceLayerImpl> layer =
     30       UIResourceLayerImpl::Create(host_impl->active_tree(), 1);
     31   layer->draw_properties().visible_content_rect = visible_content_rect;
     32   layer->SetBounds(layer_size);
     33   layer->SetContentBounds(layer_size);
     34   layer->CreateRenderSurface();
     35   layer->draw_properties().render_target = layer.get();
     36 
     37   SkBitmap skbitmap;
     38   skbitmap.setConfig(SkBitmap::kARGB_8888_Config,
     39                      bitmap_size.width(),
     40                      bitmap_size.height(),
     41                      0,
     42                      opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
     43   skbitmap.allocPixels();
     44   skbitmap.setImmutable();
     45   UIResourceBitmap bitmap(skbitmap);
     46 
     47   host_impl->CreateUIResource(uid, bitmap);
     48   layer->SetUIResourceId(uid);
     49 
     50   return layer.Pass();
     51 }
     52 
     53 void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer,
     54                   size_t expected_quad_size) {
     55   MockQuadCuller quad_culler;
     56   AppendQuadsData data;
     57   layer->AppendQuads(&quad_culler, &data);
     58 
     59   // Verify quad rects
     60   const QuadList& quads = quad_culler.quad_list();
     61   EXPECT_EQ(expected_quad_size, quads.size());
     62 }
     63 
     64 TEST(UIResourceLayerImplTest, VerifyDrawQuads) {
     65   FakeImplProxy proxy;
     66   FakeUIResourceLayerTreeHostImpl host_impl(&proxy);
     67   // Make sure we're appending quads when there are valid values.
     68   gfx::Size bitmap_size(100, 100);
     69   gfx::Size layer_size(100, 100);;
     70   size_t expected_quad_size = 1;
     71   bool opaque = true;
     72   UIResourceId uid = 1;
     73   scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
     74                                                                   bitmap_size,
     75                                                                   layer_size,
     76                                                                   opaque,
     77                                                                   uid);
     78   QuadSizeTest(layer.Pass(), expected_quad_size);
     79 
     80   // Make sure we're not appending quads when there are invalid values.
     81   expected_quad_size = 0;
     82   uid = 0;
     83   layer = GenerateUIResourceLayer(&host_impl,
     84                                   bitmap_size,
     85                                   layer_size,
     86                                   opaque,
     87                                   uid);
     88   QuadSizeTest(layer.Pass(), expected_quad_size);
     89 }
     90 
     91 void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer,
     92                  gfx::Rect expected_opaque_bounds) {
     93   MockQuadCuller quad_culler;
     94   AppendQuadsData data;
     95   layer->AppendQuads(&quad_culler, &data);
     96 
     97   // Verify quad rects
     98   const QuadList& quads = quad_culler.quad_list();
     99   EXPECT_GE(quads.size(), (size_t)0);
    100   gfx::Rect opaque_rect = quads.at(0)->opaque_rect;
    101   EXPECT_EQ(expected_opaque_bounds, opaque_rect);
    102 }
    103 
    104 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) {
    105   FakeImplProxy proxy;
    106   FakeUIResourceLayerTreeHostImpl host_impl(&proxy);
    107 
    108   gfx::Size bitmap_size(100, 100);
    109   gfx::Size layer_size(100, 100);;
    110   bool opaque = false;
    111   UIResourceId uid = 1;
    112   scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
    113                                                                   bitmap_size,
    114                                                                   layer_size,
    115                                                                   opaque,
    116                                                                   uid);
    117   gfx::Rect expected_opaque_bounds;
    118   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    119 
    120   opaque = true;
    121   layer = GenerateUIResourceLayer(&host_impl,
    122                                   bitmap_size,
    123                                   layer_size,
    124                                   opaque,
    125                                   uid);
    126   expected_opaque_bounds = gfx::Rect(layer->bounds());
    127   OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
    128 }
    129 
    130 TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) {
    131   FakeImplProxy proxy;
    132   FakeUIResourceLayerTreeHostImpl host_impl(&proxy);
    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 }  // namespace
    152 }  // namespace cc
    153