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(
39 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>());
42 scoped_ptr
<TestablePictureImageLayerImpl
> CreateLayer(int id
,
43 WhichTree which_tree
) {
44 LayerTreeImpl
* tree
= NULL
;
47 tree
= host_impl_
.active_tree();
50 tree
= host_impl_
.pending_tree();
56 TestablePictureImageLayerImpl
* layer
=
57 new TestablePictureImageLayerImpl(tree
, id
);
58 layer
->SetBounds(gfx::Size(100, 200));
59 layer
->tilings_
.reset(new PictureLayerTilingSet(&tiling_client_
,
61 layer
->pile_
= tiling_client_
.pile();
62 return make_scoped_ptr(layer
);
65 void UpdateDrawProperties() {
66 host_impl_
.pending_tree()->UpdateDrawProperties();
71 FakeLayerTreeHostImpl host_impl_
;
72 FakePictureLayerTilingClient tiling_client_
;
75 TEST_F(PictureImageLayerImplTest
, CalculateContentsScale
) {
76 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
77 layer
->SetDrawsContent(true);
79 float contents_scale_x
;
80 float contents_scale_y
;
81 gfx::Size content_bounds
;
82 layer
->CalculateContentsScale(2.f
, 3.f
, 4.f
, false,
83 &contents_scale_x
, &contents_scale_y
,
85 EXPECT_FLOAT_EQ(1.f
, contents_scale_x
);
86 EXPECT_FLOAT_EQ(1.f
, contents_scale_y
);
87 EXPECT_EQ(layer
->bounds(), content_bounds
);
90 TEST_F(PictureImageLayerImplTest
, AreVisibleResourcesReady
) {
91 scoped_ptr
<TestablePictureImageLayerImpl
> layer(CreateLayer(1, PENDING_TREE
));
92 layer
->SetBounds(gfx::Size(100, 200));
93 layer
->SetDrawsContent(true);
95 UpdateDrawProperties();
97 float contents_scale_x
;
98 float contents_scale_y
;
99 gfx::Size content_bounds
;
100 layer
->CalculateContentsScale(2.f
, 3.f
, 4.f
, false,
101 &contents_scale_x
, &contents_scale_y
,
103 layer
->UpdateTilePriorities();
105 EXPECT_TRUE(layer
->AreVisibleResourcesReady());
108 TEST_F(PictureImageLayerImplTest
, IgnoreIdealContentScale
) {
109 scoped_ptr
<TestablePictureImageLayerImpl
> pending_layer(
110 CreateLayer(1, PENDING_TREE
));
111 pending_layer
->SetDrawsContent(true);
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 bool animating_transform_to_screen
= false;
119 float contents_scale_x
;
120 float contents_scale_y
;
121 gfx::Size content_bounds
;
122 pending_layer
->CalculateContentsScale(suggested_ideal_contents_scale
,
125 animating_transform_to_screen
,
130 // Push to active layer.
131 host_impl_
.ActivatePendingTree();
132 scoped_ptr
<TestablePictureImageLayerImpl
> active_layer(
133 CreateLayer(1, ACTIVE_TREE
));
134 pending_layer
->PushPropertiesTo(active_layer
.get());
135 active_layer
->CalculateContentsScale(suggested_ideal_contents_scale
,
138 animating_transform_to_screen
,
143 // Create tile and resource.
144 active_layer
->tilings()->tiling_at(0)->CreateAllTilesForTesting();
145 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(
146 active_layer
->tilings()->tiling_at(0)->AllTilesForTesting(),
147 host_impl_
.resource_provider());
150 active_layer
->draw_properties().visible_content_rect
=
151 gfx::Rect(active_layer
->bounds());
152 MockQuadCuller quad_culler
;
153 AppendQuadsData data
;
154 active_layer
->WillDraw(DRAW_MODE_SOFTWARE
, NULL
);
155 active_layer
->AppendQuads(&quad_culler
, &data
);
156 active_layer
->DidDraw(NULL
);
158 EXPECT_EQ(DrawQuad::TILED_CONTENT
, quad_culler
.quad_list()[0]->material
);
160 // Tiles are ready at correct scale, so should not set had_incomplete_tile.
161 EXPECT_FALSE(data
.had_incomplete_tile
);