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 "base/thread_task_runner_handle.h"
8 #include "cc/layers/append_quads_data.h"
9 #include "cc/quads/draw_quad.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/test/test_task_graph_runner.h"
17 #include "cc/tiles/tile_priority.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 class TestablePictureImageLayerImpl
: public PictureImageLayerImpl
{
26 TestablePictureImageLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
27 : PictureImageLayerImpl(tree_impl
, id
, false) {}
28 using PictureLayerImpl::UpdateIdealScales
;
29 using PictureLayerImpl::MaximumTilingContentsScale
;
31 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
33 friend class PictureImageLayerImplTest
;
36 class PictureImageLayerImplTest
: public testing::Test
{
38 PictureImageLayerImplTest()
39 : proxy_(base::ThreadTaskRunnerHandle::Get()),
40 host_impl_(ImplSidePaintingSettings(),
42 &shared_bitmap_manager_
,
43 &task_graph_runner_
) {
44 host_impl_
.CreatePendingTree();
45 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d());
48 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
49 WhichTree which_tree
) {
50 LayerTreeImpl
* tree
= nullptr;
53 tree
= host_impl_
.active_tree();
56 tree
= host_impl_
.pending_tree();
59 TestablePictureImageLayerImpl
* layer
=
60 new TestablePictureImageLayerImpl(tree
, id
);
61 layer
->raster_source_
= FakePicturePileImpl::CreateInfiniteFilledPile();
62 layer
->SetBounds(layer
->raster_source_
->GetSize());
63 layer
->SetContentBounds(layer
->raster_source_
->GetSize());
64 return make_scoped_ptr(layer
);
67 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
68 float ideal_contents_scale
,
69 float device_scale_factor
,
70 float page_scale_factor
,
71 float maximum_animation_contents_scale
,
72 bool animating_transform_to_screen
,
73 gfx::Rect viewport_rect
) {
74 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
75 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
76 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
77 layer
->draw_properties().maximum_animation_contents_scale
=
78 maximum_animation_contents_scale
;
79 layer
->draw_properties().screen_space_transform_is_animating
=
80 animating_transform_to_screen
;
81 layer
->draw_properties().visible_content_rect
= viewport_rect
;
82 bool resourceless_software_draw
= false;
83 layer
->UpdateTiles(resourceless_software_draw
);
88 TestSharedBitmapManager shared_bitmap_manager_
;
89 TestTaskGraphRunner task_graph_runner_
;
90 FakeLayerTreeHostImpl host_impl_
;
93 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
94 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
95 layer
->SetDrawsContent(true);
97 gfx::Rect
viewport(100, 200);
98 SetupDrawPropertiesAndUpdateTiles(
99 layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false, viewport
);
101 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_x());
102 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_y());
103 EXPECT_FLOAT_EQ(1.f
, layer
->MaximumTilingContentsScale());
106 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
107 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
108 CreateLayer(1, PENDING_TREE
));
109 pending_layer
->SetDrawsContent(true);
111 gfx::Rect
viewport(100, 200);
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
,
127 EXPECT_EQ(1.f
, pending_layer
->tilings()->tiling_at(0)->contents_scale());
129 // Push to active layer.
130 host_impl_
.pending_tree()->SetRootLayer(pending_layer
.Pass());
131 host_impl_
.ActivateSyncTree();
133 TestablePictureImageLayerImpl
* active_layer
=
134 static_cast<TestablePictureImageLayerImpl
*>(
135 host_impl_
.active_tree()->root_layer());
136 SetupDrawPropertiesAndUpdateTiles(active_layer
,
137 suggested_ideal_contents_scale
,
140 maximum_animation_contents_scale
,
141 animating_transform_to_screen
,
143 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
145 // Create resources for the tiles.
146 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
147 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
150 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
151 AppendQuadsData data
;
152 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, nullptr);
153 active_layer
->AppendQuads(render_pass
.get(), &data
);
154 active_layer
->DidDraw(nullptr);
156 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
.front()->material
);
158 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
159 EXPECT_EQ(0, data
.num_incomplete_tiles
);