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