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
, false) {}
26 using PictureLayerImpl::UpdateIdealScales
;
27 using PictureLayerImpl::MaximumTilingContentsScale
;
29 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
31 friend class PictureImageLayerImplTest
;
34 class PictureImageLayerImplTest
: public testing::Test
{
36 PictureImageLayerImplTest()
37 : proxy_(base::MessageLoopProxy::current()),
38 host_impl_(ImplSidePaintingSettings(),
40 &shared_bitmap_manager_
) {
41 host_impl_
.CreatePendingTree();
42 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d());
45 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
46 WhichTree which_tree
) {
47 LayerTreeImpl
* tree
= nullptr;
50 tree
= host_impl_
.active_tree();
53 tree
= host_impl_
.pending_tree();
56 TestablePictureImageLayerImpl
* layer
=
57 new TestablePictureImageLayerImpl(tree
, id
);
58 layer
->raster_source_
= FakePicturePileImpl::CreateInfiniteFilledPile();
59 layer
->SetBounds(layer
->raster_source_
->GetSize());
60 layer
->SetContentBounds(layer
->raster_source_
->GetSize());
61 return make_scoped_ptr(layer
);
64 void SetupDrawPropertiesAndUpdateTiles(TestablePictureImageLayerImpl
* layer
,
65 float ideal_contents_scale
,
66 float device_scale_factor
,
67 float page_scale_factor
,
68 float maximum_animation_contents_scale
,
69 bool animating_transform_to_screen
,
70 gfx::Rect viewport_rect
) {
71 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
72 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
73 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
74 layer
->draw_properties().maximum_animation_contents_scale
=
75 maximum_animation_contents_scale
;
76 layer
->draw_properties().screen_space_transform_is_animating
=
77 animating_transform_to_screen
;
78 layer
->draw_properties().visible_content_rect
= viewport_rect
;
79 bool resourceless_software_draw
= false;
80 layer
->UpdateTiles(resourceless_software_draw
);
85 FakeLayerTreeHostImpl host_impl_
;
86 TestSharedBitmapManager shared_bitmap_manager_
;
89 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
90 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
91 layer
->SetDrawsContent(true);
93 gfx::Rect
viewport(100, 200);
94 SetupDrawPropertiesAndUpdateTiles(
95 layer
.get(), 2.f
, 3.f
, 4.f
, 1.f
, false, viewport
);
97 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_x());
98 EXPECT_FLOAT_EQ(1.f
, layer
->contents_scale_y());
99 EXPECT_FLOAT_EQ(1.f
, layer
->MaximumTilingContentsScale());
102 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
103 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
104 CreateLayer(1, PENDING_TREE
));
105 pending_layer
->SetDrawsContent(true);
107 gfx::Rect
viewport(100, 200);
109 // Set PictureLayerImpl::ideal_contents_scale_ to 2.f which is not equal
110 // to the content scale used by PictureImageLayerImpl.
111 const float suggested_ideal_contents_scale
= 2.f
;
112 const float device_scale_factor
= 3.f
;
113 const float page_scale_factor
= 4.f
;
114 const float maximum_animation_contents_scale
= 1.f
;
115 const bool animating_transform_to_screen
= false;
116 SetupDrawPropertiesAndUpdateTiles(pending_layer
.get(),
117 suggested_ideal_contents_scale
,
120 maximum_animation_contents_scale
,
121 animating_transform_to_screen
,
123 EXPECT_EQ(1.f
, pending_layer
->tilings()->tiling_at(0)->contents_scale());
125 // Push to active layer.
126 host_impl_
.pending_tree()->SetRootLayer(pending_layer
.Pass());
127 host_impl_
.ActivateSyncTree();
129 TestablePictureImageLayerImpl
* active_layer
=
130 static_cast<TestablePictureImageLayerImpl
*>(
131 host_impl_
.active_tree()->root_layer());
132 SetupDrawPropertiesAndUpdateTiles(active_layer
,
133 suggested_ideal_contents_scale
,
136 maximum_animation_contents_scale
,
137 animating_transform_to_screen
,
139 EXPECT_EQ(1.f
, active_layer
->tilings()->tiling_at(0)->contents_scale());
141 // Create resources for the tiles.
142 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
143 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting());
146 scoped_ptr
<RenderPass
> render_pass
= RenderPass::Create();
147 AppendQuadsData data
;
148 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, nullptr);
149 active_layer
->AppendQuads(render_pass
.get(), &data
);
150 active_layer
->DidDraw(nullptr);
152 EXPECT_EQ(DrawQuad::TILED_CONTENT
, render_pass
->quad_list
.front()->material
);
154 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
155 EXPECT_EQ(0, data
.num_incomplete_tiles
);