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/resources/tile_priority.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/fake_picture_layer_tiling_client.h"
13 #include "cc/test/impl_side_painting_settings.h"
14 #include "cc/test/mock_quad_culler.h"
15 #include "cc/trees/layer_tree_impl.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 class TestablePictureImageLayerImpl
: public PictureImageLayerImpl
{
23 TestablePictureImageLayerImpl(LayerTreeImpl
* tree_impl
, int id
)
24 : PictureImageLayerImpl(tree_impl
, id
) {
27 PictureLayerTilingSet
* tilings() { return tilings_
.get(); }
29 friend class PictureImageLayerImplTest
;
32 class PictureImageLayerImplTest
: public testing::Test
{
34 PictureImageLayerImplTest()
35 : host_impl_(ImplSidePaintingSettings(), &proxy_
) {
36 tiling_client_
.SetTileSize(ImplSidePaintingSettings().default_tile_size
);
37 host_impl_
.CreatePendingTree();
38 host_impl_
.InitializeRenderer(CreateFakeOutputSurface());
41 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
42 WhichTree which_tree
) {
43 LayerTreeImpl
* tree
= NULL
;
46 tree
= host_impl_
.active_tree();
49 tree
= host_impl_
.pending_tree();
55 TestablePictureImageLayerImpl
* layer
=
56 new TestablePictureImageLayerImpl(tree
, id
);
57 layer
->SetBounds(gfx::Size(100, 200));
58 layer
->tilings_
.reset(new PictureLayerTilingSet(&tiling_client_
,
60 layer
->pile_
= tiling_client_
.pile();
61 return make_scoped_ptr(layer
);
64 void UpdateDrawProperties() {
65 host_impl_
.pending_tree()->UpdateDrawProperties();
70 FakeLayerTreeHostImpl host_impl_
;
71 FakePictureLayerTilingClient tiling_client_
;
74 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
75 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
76 layer
->SetDrawsContent(true);
78 float contents_scale_x
;
79 float contents_scale_y
;
80 gfx::Size content_bounds
;
81 layer
->CalculateContentsScale(2.f
, 3.f
, 4.f
, false,
82 &contents_scale_x
, &contents_scale_y
,
84 EXPECT_FLOAT_EQ(1.f
, contents_scale_x
);
85 EXPECT_FLOAT_EQ(1.f
, contents_scale_y
);
86 EXPECT_EQ(layer
->bounds(), content_bounds
);
89 TEST_F(PictureImageLayerImplTest
, AreVisibleResourcesReady
) {
90 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
91 layer
->SetBounds(gfx::Size(100, 200));
92 layer
->SetDrawsContent(true);
94 UpdateDrawProperties();
96 float contents_scale_x
;
97 float contents_scale_y
;
98 gfx::Size content_bounds
;
99 layer
->CalculateContentsScale(2.f
, 3.f
, 4.f
, false,
100 &contents_scale_x
, &contents_scale_y
,
102 layer
->UpdateTilePriorities();
104 EXPECT_TRUE(layer
->AreVisibleResourcesReady());
107 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
108 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
109 CreateLayer(1, PENDING_TREE
));
110 pending_layer
->SetDrawsContent(true);
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 bool animating_transform_to_screen
= false;
118 float contents_scale_x
;
119 float contents_scale_y
;
120 gfx::Size content_bounds
;
121 pending_layer
->CalculateContentsScale(suggested_ideal_contents_scale
,
124 animating_transform_to_screen
,
129 // Push to active layer.
130 host_impl_
.ActivatePendingTree();
131 scoped_ptr
<TestablePictureImageLayerImpl
> active_layer(
132 CreateLayer(1, ACTIVE_TREE
));
133 pending_layer
->PushPropertiesTo(active_layer
.get());
134 active_layer
->CalculateContentsScale(suggested_ideal_contents_scale
,
137 animating_transform_to_screen
,
142 // Create tile and resource.
143 active_layer
->tilings()->tiling_at(0)->CreateAllTilesForTesting();
144 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
145 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting(),
146 host_impl_
.resource_provider());
149 active_layer
->draw_properties().visible_content_rect
=
150 gfx::Rect(active_layer
->bounds());
151 MockQuadCuller quad_culler
;
152 AppendQuadsData data
;
153 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, NULL
);
154 active_layer
->AppendQuads(&quad_culler
, &data
);
155 active_layer
->DidDraw(NULL
);
157 EXPECT_EQ(DrawQuad::TILED_CONTENT
, quad_culler
.quad_list()[0]->material
);
159 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
160 EXPECT_FALSE(data
.had_incomplete_tile
);