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