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/test_shared_bitmap_manager.h"
15 #include "cc/test/test_task_graph_runner.h"
16 #include "cc/tiles/tile_priority.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
, false) {}
27 using PictureLayerImpl::UpdateIdealScales
;
28 using PictureLayerImpl::MaximumTilingContentsScale
;
30 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
32 friend class PictureImageLayerImplTest
;
35 class PictureImageLayerImplTest
: public testing::Test
{
37 PictureImageLayerImplTest()
38 : proxy_(base::ThreadTaskRunnerHandle::Get()),
39 host_impl_(LayerTreeSettings(),
41 &shared_bitmap_manager_
,
42 &task_graph_runner_
) {
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();
58 TestablePictureImageLayerImpl
* layer
=
59 new TestablePictureImageLayerImpl(tree
, id
);
60 layer
->raster_source_
= FakePicturePileImpl::CreateInfiniteFilledPile();
61 layer
->SetBounds(layer
->raster_source_
->GetSize());
62 layer
->SetContentBounds(layer
->raster_source_
->GetSize());
63 return make_scoped_ptr(layer
);
66 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
67 float ideal_contents_scale
,
68 float device_scale_factor
,
69 float page_scale_factor
,
70 float maximum_animation_contents_scale
,
71 bool animating_transform_to_screen
,
72 gfx::Rect viewport_rect
) {
73 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
74 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
75 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
76 layer
->draw_properties().maximum_animation_contents_scale
=
77 maximum_animation_contents_scale
;
78 layer
->draw_properties().screen_space_transform_is_animating
=
79 animating_transform_to_screen
;
80 layer
->draw_properties().visible_content_rect
= viewport_rect
;
81 bool resourceless_software_draw
= false;
82 layer
->UpdateTiles(resourceless_software_draw
);
87 TestSharedBitmapManager shared_bitmap_manager_
;
88 TestTaskGraphRunner task_graph_runner_
;
89 FakeLayerTreeHostImpl host_impl_
;
92 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
93 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
94 layer
->SetDrawsContent(true);
96 gfx::Rect
viewport(100, 200);
97 SetupDrawPropertiesAndUpdateTiles(
98 layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false, viewport
);
100 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_x());
101 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_y());
102 EXPECT_FLOAT_EQ(1.f
, layer
->MaximumTilingContentsScale());
105 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
106 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
107 CreateLayer(1, PENDING_TREE
));
108 pending_layer
->SetDrawsContent(true);
110 gfx::Rect
viewport(100, 200);
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
,
123 maximum_animation_contents_scale
,
124 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
.Pass());
130 host_impl_
.ActivateSyncTree();
132 TestablePictureImageLayerImpl
* active_layer
=
133 static_cast<TestablePictureImageLayerImpl
*>(
134 host_impl_
.active_tree()->root_layer());
135 SetupDrawPropertiesAndUpdateTiles(active_layer
,
136 suggested_ideal_contents_scale
,
139 maximum_animation_contents_scale
,
140 animating_transform_to_screen
,
142 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
144 // Create resources for the tiles.
145 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
146 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
149 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
150 AppendQuadsData data
;
151 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, nullptr);
152 active_layer
->AppendQuads(render_pass
.get(), &data
);
153 active_layer
->DidDraw(nullptr);
155 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
.front()->material
);
157 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
158 EXPECT_EQ(0, data
.num_incomplete_tiles
);