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 return make_scoped_ptr(layer
);
65 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
66 float ideal_contents_scale
,
67 float device_scale_factor
,
68 float page_scale_factor
,
69 float maximum_animation_contents_scale
,
70 bool animating_transform_to_screen
,
71 gfx::Rect viewport_rect
) {
72 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
73 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
74 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
75 layer
->draw_properties().maximum_animation_contents_scale
=
76 maximum_animation_contents_scale
;
77 layer
->draw_properties().screen_space_transform_is_animating
=
78 animating_transform_to_screen
;
79 layer
->draw_properties().visible_layer_rect
= viewport_rect
;
80 bool resourceless_software_draw
= false;
81 layer
->UpdateTiles(resourceless_software_draw
);
86 TestSharedBitmapManager shared_bitmap_manager_
;
87 TestTaskGraphRunner task_graph_runner_
;
88 FakeLayerTreeHostImpl host_impl_
;
91 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
92 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
93 layer
->SetDrawsContent(true);
95 gfx::Rect
viewport(100, 200);
96 SetupDrawPropertiesAndUpdateTiles(
97 layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false, viewport
);
98 EXPECT_FLOAT_EQ(1.f
, layer
->MaximumTilingContentsScale());
101 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
102 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
103 CreateLayer(1, PENDING_TREE
));
104 pending_layer
->SetDrawsContent(true);
106 gfx::Rect
viewport(100, 200);
108 // Set PictureLayerImpl::ideal_contents_scale_ to 2.f which is not equal
109 // to the content scale used by PictureImageLayerImpl.
110 const float suggested_ideal_contents_scale
= 2.f
;
111 const float device_scale_factor
= 3.f
;
112 const float page_scale_factor
= 4.f
;
113 const float maximum_animation_contents_scale
= 1.f
;
114 const bool animating_transform_to_screen
= false;
115 SetupDrawPropertiesAndUpdateTiles(pending_layer
.get(),
116 suggested_ideal_contents_scale
,
119 maximum_animation_contents_scale
,
120 animating_transform_to_screen
,
122 EXPECT_EQ(1.f
, pending_layer
->tilings()->tiling_at(0)->contents_scale());
124 // Push to active layer.
125 host_impl_
.pending_tree()->SetRootLayer(pending_layer
.Pass());
126 host_impl_
.ActivateSyncTree();
128 TestablePictureImageLayerImpl
* active_layer
=
129 static_cast<TestablePictureImageLayerImpl
*>(
130 host_impl_
.active_tree()->root_layer());
131 SetupDrawPropertiesAndUpdateTiles(active_layer
,
132 suggested_ideal_contents_scale
,
135 maximum_animation_contents_scale
,
136 animating_transform_to_screen
,
138 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
140 // Create resources for the tiles.
141 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
142 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
145 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
146 AppendQuadsData data
;
147 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, nullptr);
148 active_layer
->AppendQuads(render_pass
.get(), &data
);
149 active_layer
->DidDraw(nullptr);
151 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
.front()->material
);
153 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
154 EXPECT_EQ(0, data
.num_incomplete_tiles
);