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_pile_impl.h"
14 #include "cc/test/impl_side_painting_settings.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
) {
27 using PictureLayerImpl::UpdateIdealScales
;
28 using PictureLayerImpl::MaximumTilingContentsScale
;
29 using PictureLayerImpl::DoPostCommitInitializationIfNeeded
;
31 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
33 friend class PictureImageLayerImplTest
;
36 class PictureImageLayerImplTest
: public testing::Test
{
38 PictureImageLayerImplTest()
39 : proxy_(base::MessageLoopProxy::current()),
40 host_impl_(ImplSidePaintingSettings(),
42 &shared_bitmap_manager_
) {
43 host_impl_
.CreatePendingTree();
44 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d());
47 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
48 WhichTree which_tree
) {
49 LayerTreeImpl
* tree
= nullptr;
52 tree
= host_impl_
.active_tree();
55 tree
= host_impl_
.pending_tree();
61 TestablePictureImageLayerImpl
* layer
=
62 new TestablePictureImageLayerImpl(tree
, id
);
63 layer
->raster_source_
= FakePicturePileImpl::CreateInfiniteFilledPile();
64 layer
->SetBounds(layer
->raster_source_
->GetSize());
65 layer
->SetContentBounds(layer
->raster_source_
->GetSize());
66 return make_scoped_ptr(layer
);
69 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
70 float ideal_contents_scale
,
71 float device_scale_factor
,
72 float page_scale_factor
,
73 float maximum_animation_contents_scale
,
74 bool animating_transform_to_screen
,
75 gfx::Rect viewport_rect
) {
76 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
77 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
78 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
79 layer
->draw_properties().maximum_animation_contents_scale
=
80 maximum_animation_contents_scale
;
81 layer
->draw_properties().screen_space_transform_is_animating
=
82 animating_transform_to_screen
;
83 layer
->draw_properties().visible_content_rect
= viewport_rect
;
84 bool resourceless_software_draw
= false;
85 layer
->UpdateTiles(Occlusion(), resourceless_software_draw
);
90 FakeLayerTreeHostImpl host_impl_
;
91 TestSharedBitmapManager shared_bitmap_manager_
;
94 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
95 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
96 layer
->SetDrawsContent(true);
97 layer
->DoPostCommitInitializationIfNeeded();
99 gfx::Rect
viewport(100, 200);
100 SetupDrawPropertiesAndUpdateTiles(
101 layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false, viewport
);
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 gfx::Rect
viewport(100, 200);
115 // Set PictureLayerImpl::ideal_contents_scale_ to 2.f which is not equal
116 // to the content scale used by PictureImageLayerImpl.
117 const float suggested_ideal_contents_scale
= 2.f
;
118 const float device_scale_factor
= 3.f
;
119 const float page_scale_factor
= 4.f
;
120 const float maximum_animation_contents_scale
= 1.f
;
121 const bool animating_transform_to_screen
= false;
122 SetupDrawPropertiesAndUpdateTiles(pending_layer
.get(),
123 suggested_ideal_contents_scale
,
126 maximum_animation_contents_scale
,
127 animating_transform_to_screen
,
129 EXPECT_EQ(1.f
, pending_layer
->tilings()->tiling_at(0)->contents_scale());
131 // Push to active layer.
132 host_impl_
.pending_tree()->SetRootLayer(pending_layer
.Pass());
133 host_impl_
.ActivateSyncTree();
135 TestablePictureImageLayerImpl
* active_layer
=
136 static_cast<TestablePictureImageLayerImpl
*>(
137 host_impl_
.active_tree()->root_layer());
138 SetupDrawPropertiesAndUpdateTiles(active_layer
,
139 suggested_ideal_contents_scale
,
142 maximum_animation_contents_scale
,
143 animating_transform_to_screen
,
145 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
147 // Create resources for the tiles.
148 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
149 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
152 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
153 AppendQuadsData data
;
154 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, nullptr);
155 active_layer
->AppendQuads(render_pass
.get(), Occlusion(), &data
);
156 active_layer
->DidDraw(nullptr);
158 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
.front()->material
);
160 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
161 EXPECT_EQ(0, data
.num_incomplete_tiles
);