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/quads/draw_quad.h"
9 #include "cc/resources/tile_priority.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_picture_layer_tiling_client.h"
14 #include "cc/test/impl_side_painting_settings.h"
15 #include "cc/test/mock_occlusion_tracker.h"
16 #include "cc/test/test_shared_bitmap_manager.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 class TestablePictureImageLayerImpl
: public PictureImageLayerImpl
{
25 TestablePictureImageLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
26 : PictureImageLayerImpl(tree_impl
, id
) {
28 using PictureLayerImpl::UpdateIdealScales
;
29 using PictureLayerImpl::MaximumTilingContentsScale
;
30 using PictureLayerImpl::DoPostCommitInitializationIfNeeded
;
32 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
34 friend class PictureImageLayerImplTest
;
37 class PictureImageLayerImplTest
: public testing::Test
{
39 PictureImageLayerImplTest()
40 : proxy_(base::MessageLoopProxy::current()),
41 host_impl_(ImplSidePaintingSettings(),
43 &shared_bitmap_manager_
) {
44 tiling_client_
.SetTileSize(ImplSidePaintingSettings().default_tile_size
);
45 host_impl_
.CreatePendingTree();
46 host_impl_
.InitializeRenderer(
47 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>());
50 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
51 WhichTree which_tree
) {
52 LayerTreeImpl
* tree
= NULL
;
55 tree
= host_impl_
.active_tree();
58 tree
= host_impl_
.pending_tree();
64 TestablePictureImageLayerImpl
* layer
=
65 new TestablePictureImageLayerImpl(tree
, id
);
66 layer
->SetBounds(gfx::Size(100, 200));
67 layer
->SetContentBounds(gfx::Size(100, 200));
68 layer
->tilings_
.reset(new PictureLayerTilingSet(&tiling_client_
,
70 layer
->pile_
= tiling_client_
.GetPile();
71 return make_scoped_ptr(layer
);
74 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
75 float ideal_contents_scale
,
76 float device_scale_factor
,
77 float page_scale_factor
,
78 float maximum_animation_contents_scale
,
79 bool animating_transform_to_screen
) {
80 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
81 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
82 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
83 layer
->draw_properties().maximum_animation_contents_scale
=
84 maximum_animation_contents_scale
;
85 layer
->draw_properties().screen_space_transform_is_animating
=
86 animating_transform_to_screen
;
87 layer
->UpdateTiles(NULL
);
92 FakeLayerTreeHostImpl host_impl_
;
93 TestSharedBitmapManager shared_bitmap_manager_
;
94 FakePictureLayerTilingClient tiling_client_
;
97 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
98 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
99 layer
->SetDrawsContent(true);
101 SetupDrawPropertiesAndUpdateTiles(layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false);
103 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_x());
104 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_y());
105 EXPECT_FLOAT_EQ(1.f
, layer
->MaximumTilingContentsScale());
108 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
109 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
110 CreateLayer(1, PENDING_TREE
));
111 pending_layer
->SetDrawsContent(true);
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 float maximum_animation_contents_scale
= 1.f
;
119 const bool animating_transform_to_screen
= false;
120 SetupDrawPropertiesAndUpdateTiles(pending_layer
.get(),
121 suggested_ideal_contents_scale
,
124 maximum_animation_contents_scale
,
125 animating_transform_to_screen
);
126 EXPECT_EQ(1.f
, pending_layer
->tilings()->tiling_at(0)->contents_scale());
128 // Push to active layer.
129 host_impl_
.pending_tree()->SetRootLayer(pending_layer
.PassAs
<LayerImpl
>());
130 host_impl_
.ActivateSyncTree();
131 TestablePictureImageLayerImpl
* active_layer
=
132 static_cast<TestablePictureImageLayerImpl
*>(
133 host_impl_
.active_tree()->root_layer());
134 SetupDrawPropertiesAndUpdateTiles(active_layer
,
135 suggested_ideal_contents_scale
,
138 maximum_animation_contents_scale
,
139 animating_transform_to_screen
);
140 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
142 // Create tile and resource.
143 active_layer
->tilings()->tiling_at(0)->CreateAllTilesForTesting();
144 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
145 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
148 active_layer
->draw_properties().visible_content_rect
=
149 gfx::Rect(active_layer
->bounds());
150 MockOcclusionTracker
<LayerImpl
> occlusion_tracker
;
151 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
152 AppendQuadsData data
;
153 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, NULL
);
154 active_layer
->AppendQuads(render_pass
.get(), occlusion_tracker
, &data
);
155 active_layer
->DidDraw(NULL
);
157 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
[0]->material
);
159 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
160 EXPECT_FALSE(data
.had_incomplete_tile
);