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.
4
5#include "cc/layers/picture_image_layer_impl.h"
6
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"
17
18namespace cc {
19namespace {
20
21class TestablePictureImageLayerImpl : public PictureImageLayerImpl {
22 public:
23  TestablePictureImageLayerImpl(LayerTreeImpl* tree_impl, int id)
24      : PictureImageLayerImpl(tree_impl, id) {
25  }
26
27  PictureLayerTilingSet* tilings() { return tilings_.get(); }
28
29  friend class PictureImageLayerImplTest;
30};
31
32class PictureImageLayerImplTest : public testing::Test {
33 public:
34  PictureImageLayerImplTest()
35      : host_impl_(ImplSidePaintingSettings(), &proxy_) {
36    tiling_client_.SetTileSize(ImplSidePaintingSettings().default_tile_size);
37    host_impl_.CreatePendingTree();
38    host_impl_.InitializeRenderer(CreateFakeOutputSurface());
39  }
40
41  scoped_ptr<TestablePictureImageLayerImpl> CreateLayer(int id,
42                                                        WhichTree which_tree) {
43    LayerTreeImpl* tree = NULL;
44    switch (which_tree) {
45      case ACTIVE_TREE:
46        tree = host_impl_.active_tree();
47        break;
48      case PENDING_TREE:
49        tree = host_impl_.pending_tree();
50        break;
51      case NUM_TREES:
52        NOTREACHED();
53        break;
54    }
55    TestablePictureImageLayerImpl* layer =
56        new TestablePictureImageLayerImpl(tree, id);
57    layer->SetBounds(gfx::Size(100, 200));
58    layer->tilings_.reset(new PictureLayerTilingSet(&tiling_client_,
59                                                    layer->bounds()));
60    layer->pile_ = tiling_client_.pile();
61    return make_scoped_ptr(layer);
62  }
63
64  void UpdateDrawProperties() {
65    host_impl_.pending_tree()->UpdateDrawProperties();
66  }
67
68 protected:
69  FakeImplProxy proxy_;
70  FakeLayerTreeHostImpl host_impl_;
71  FakePictureLayerTilingClient tiling_client_;
72};
73
74TEST_F(PictureImageLayerImplTest, CalculateContentsScale) {
75  scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
76  layer->SetDrawsContent(true);
77
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,
83                                &content_bounds);
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);
87}
88
89TEST_F(PictureImageLayerImplTest, AreVisibleResourcesReady) {
90  scoped_ptr<TestablePictureImageLayerImpl> layer(CreateLayer(1, PENDING_TREE));
91  layer->SetBounds(gfx::Size(100, 200));
92  layer->SetDrawsContent(true);
93
94  UpdateDrawProperties();
95
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,
101                                &content_bounds);
102  layer->UpdateTilePriorities();
103
104  EXPECT_TRUE(layer->AreVisibleResourcesReady());
105}
106
107TEST_F(PictureImageLayerImplTest, IgnoreIdealContentScale) {
108  scoped_ptr<TestablePictureImageLayerImpl> pending_layer(
109      CreateLayer(1, PENDING_TREE));
110  pending_layer->SetDrawsContent(true);
111
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,
122                                        device_scale_factor,
123                                        page_scale_factor,
124                                        animating_transform_to_screen,
125                                        &contents_scale_x,
126                                        &contents_scale_y,
127                                        &content_bounds);
128
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,
135                                       device_scale_factor,
136                                       page_scale_factor,
137                                       animating_transform_to_screen,
138                                       &contents_scale_x,
139                                       &contents_scale_y,
140                                       &content_bounds);
141
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());
147
148  // Draw.
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);
156
157  EXPECT_EQ(DrawQuad::TILED_CONTENT, quad_culler.quad_list()[0]->material);
158
159  // Tiles are ready at correct scale, so should not set had_incomplete_tile.
160  EXPECT_FALSE(data.had_incomplete_tile);
161}
162
163}  // namespace
164}  // namespace cc
165