picture_image_layer_impl_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/picture_image_layer_impl.h" 6 7#include "cc/layers/append_quads_data.h" 8#include "cc/resources/tile_priority.h" 9#include "cc/test/fake_impl_proxy.h" 10#include "cc/test/fake_layer_tree_host_impl.h" 11#include "cc/test/fake_output_surface.h" 12#include "cc/test/fake_picture_layer_tiling_client.h" 13#include "cc/test/impl_side_painting_settings.h" 14#include "cc/test/mock_quad_culler.h" 15#include "cc/trees/layer_tree_impl.h" 16#include "testing/gtest/include/gtest/gtest.h" 17 18namespace cc { 19namespace { 20 21class TestablePictureImageLayerImpl : public PictureImageLayerImpl { 22 public: 23 TestablePictureImageLayerImpl(LayerTreeImpl* tree_impl, int id) 24 : PictureImageLayerImpl(tree_impl, id) { 25 } 26 27 PictureLayerTilingSet* tilings() { return tilings_.get(); } 28 29 friend class PictureImageLayerImplTest; 30}; 31 32class PictureImageLayerImplTest : public testing::Test { 33 public: 34 PictureImageLayerImplTest() 35 : host_impl_(ImplSidePaintingSettings(), &proxy_) { 36 tiling_client_.SetTileSize(ImplSidePaintingSettings().default_tile_size); 37 host_impl_.CreatePendingTree(); 38 host_impl_.InitializeRenderer( 39 FakeOutputSurface::Create3d().PassAs<OutputSurface>()); 40 } 41 42 scoped_ptr<TestablePictureImageLayerImpl> CreateLayer(int id, 43 WhichTree which_tree) { 44 LayerTreeImpl* tree = NULL; 45 switch (which_tree) { 46 case ACTIVE_TREE: 47 tree = host_impl_.active_tree(); 48 break; 49 case PENDING_TREE: 50 tree = host_impl_.pending_tree(); 51 break; 52 case NUM_TREES: 53 NOTREACHED(); 54 break; 55 } 56 TestablePictureImageLayerImpl* layer = 57 new TestablePictureImageLayerImpl(tree, id); 58 layer->SetBounds(gfx::Size(100, 200)); 59 layer->tilings_.reset(new PictureLayerTilingSet(&tiling_client_, 60 layer->bounds())); 61 layer->pile_ = tiling_client_.pile(); 62 return make_scoped_ptr(layer); 63 } 64 65 void UpdateDrawProperties() { 66 host_impl_.pending_tree()->UpdateDrawProperties(); 67 } 68 69 protected: 70 FakeImplProxy proxy_; 71 FakeLayerTreeHostImpl host_impl_; 72 FakePictureLayerTilingClient tiling_client_; 73}; 74 75TEST_F(PictureImageLayerImplTest, CalculateContentsScale) { 76 scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE)); 77 layer->SetDrawsContent(true); 78 79 float contents_scale_x; 80 float contents_scale_y; 81 gfx::Size content_bounds; 82 layer->CalculateContentsScale(2.f, 3.f, 4.f, false, 83 &contents_scale_x, &contents_scale_y, 84 &content_bounds); 85 EXPECT_FLOAT_EQ(1.f, contents_scale_x); 86 EXPECT_FLOAT_EQ(1.f, contents_scale_y); 87 EXPECT_EQ(layer->bounds(), content_bounds); 88} 89 90TEST_F(PictureImageLayerImplTest, AreVisibleResourcesReady) { 91 scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE)); 92 layer->SetBounds(gfx::Size(100, 200)); 93 layer->SetDrawsContent(true); 94 95 UpdateDrawProperties(); 96 97 float contents_scale_x; 98 float contents_scale_y; 99 gfx::Size content_bounds; 100 layer->CalculateContentsScale(2.f, 3.f, 4.f, false, 101 &contents_scale_x, &contents_scale_y, 102 &content_bounds); 103 layer->UpdateTilePriorities(); 104 105 EXPECT_TRUE(layer->AreVisibleResourcesReady()); 106} 107 108TEST_F(PictureImageLayerImplTest, IgnoreIdealContentScale) { 109 scoped_ptr<TestablePictureImageLayerImpl> pending_layer( 110 CreateLayer(1, PENDING_TREE)); 111 pending_layer->SetDrawsContent(true); 112 113 // Set PictureLayerImpl::ideal_contents_scale_ to 2.f which is not equal 114 // to the content scale used by PictureImageLayerImpl. 115 const float suggested_ideal_contents_scale = 2.f; 116 const float device_scale_factor = 3.f; 117 const float page_scale_factor = 4.f; 118 const bool animating_transform_to_screen = false; 119 float contents_scale_x; 120 float contents_scale_y; 121 gfx::Size content_bounds; 122 pending_layer->CalculateContentsScale(suggested_ideal_contents_scale, 123 device_scale_factor, 124 page_scale_factor, 125 animating_transform_to_screen, 126 &contents_scale_x, 127 &contents_scale_y, 128 &content_bounds); 129 130 // Push to active layer. 131 host_impl_.ActivatePendingTree(); 132 scoped_ptr<TestablePictureImageLayerImpl> active_layer( 133 CreateLayer(1, ACTIVE_TREE)); 134 pending_layer->PushPropertiesTo(active_layer.get()); 135 active_layer->CalculateContentsScale(suggested_ideal_contents_scale, 136 device_scale_factor, 137 page_scale_factor, 138 animating_transform_to_screen, 139 &contents_scale_x, 140 &contents_scale_y, 141 &content_bounds); 142 143 // Create tile and resource. 144 active_layer->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 145 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting( 146 active_layer->tilings()->tiling_at(0)->AllTilesForTesting(), 147 host_impl_.resource_provider()); 148 149 // Draw. 150 active_layer->draw_properties().visible_content_rect = 151 gfx::Rect(active_layer->bounds()); 152 MockQuadCuller quad_culler; 153 AppendQuadsData data; 154 active_layer->WillDraw(DRAW_MODE_SOFTWARE, NULL); 155 active_layer->AppendQuads(&quad_culler, &data); 156 active_layer->DidDraw(NULL); 157 158 EXPECT_EQ(DrawQuad::TILED_CONTENT, quad_culler.quad_list()[0]->material); 159 160 // Tiles are ready at correct scale, so should not set had_incomplete_tile. 161 EXPECT_FALSE(data.had_incomplete_tile); 162} 163 164} // namespace 165} // namespace cc 166