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.
5 #include "cc/layers/picture_image_layer_impl.h"
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"
22 class TestablePictureImageLayerImpl : public PictureImageLayerImpl {
24 TestablePictureImageLayerImpl(LayerTreeImpl* tree_impl, int id)
25 : PictureImageLayerImpl(tree_impl, id) {
28 PictureLayerTilingSet* tilings() { return tilings_.get(); }
30 friend class PictureImageLayerImplTest;
33 class PictureImageLayerImplTest : public testing::Test {
35 PictureImageLayerImplTest()
36 : proxy_(base::MessageLoopProxy::current()),
37 host_impl_(ImplSidePaintingSettings(),
39 &shared_bitmap_manager_) {
40 tiling_client_.SetTileSize(ImplSidePaintingSettings().default_tile_size);
41 host_impl_.CreatePendingTree();
42 host_impl_.InitializeRenderer(
43 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
46 scoped_ptr<TestablePictureImageLayerImpl> CreateLayer(int id,
47 WhichTree which_tree) {
48 LayerTreeImpl* tree = NULL;
51 tree = host_impl_.active_tree();
54 tree = host_impl_.pending_tree();
60 TestablePictureImageLayerImpl* layer =
61 new TestablePictureImageLayerImpl(tree, id);
62 layer->SetBounds(gfx::Size(100, 200));
63 layer->tilings_.reset(new PictureLayerTilingSet(&tiling_client_,
65 layer->pile_ = tiling_client_.pile();
66 return make_scoped_ptr(layer);
69 void UpdateDrawProperties() {
70 host_impl_.pending_tree()->UpdateDrawProperties();
75 FakeLayerTreeHostImpl host_impl_;
76 TestSharedBitmapManager shared_bitmap_manager_;
77 FakePictureLayerTilingClient tiling_client_;
80 TEST_F(PictureImageLayerImplTest, CalculateContentsScale) {
81 scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
82 layer->SetDrawsContent(true);
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,
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);
95 TEST_F(PictureImageLayerImplTest, AreVisibleResourcesReady) {
96 scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
97 layer->SetBounds(gfx::Size(100, 200));
98 layer->SetDrawsContent(true);
100 UpdateDrawProperties();
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,
108 layer->UpdateTilePriorities();
110 EXPECT_TRUE(layer->AreVisibleResourcesReady());
113 TEST_F(PictureImageLayerImplTest, IgnoreIdealContentScale) {
114 scoped_ptr<TestablePictureImageLayerImpl> pending_layer(
115 CreateLayer(1, PENDING_TREE));
116 pending_layer->SetDrawsContent(true);
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,
130 animating_transform_to_screen,
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,
143 animating_transform_to_screen,
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());
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);
163 EXPECT_EQ(DrawQuad::TILED_CONTENT, quad_culler.quad_list()[0]->material);
165 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
166 EXPECT_FALSE(data.had_incomplete_tile);