solid_color_layer_impl_unittest.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2012 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/solid_color_layer_impl.h"
6
7#include <vector>
8
9#include "cc/layers/append_quads_data.h"
10#include "cc/layers/solid_color_layer.h"
11#include "cc/quads/solid_color_draw_quad.h"
12#include "cc/test/fake_impl_proxy.h"
13#include "cc/test/fake_layer_tree_host.h"
14#include "cc/test/layer_test_common.h"
15#include "cc/test/mock_quad_culler.h"
16#include "cc/test/test_shared_bitmap_manager.h"
17#include "cc/trees/single_thread_proxy.h"
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21namespace cc {
22namespace {
23
24TEST(SolidColorLayerImplTest, VerifyTilingCompleteAndNoOverlap) {
25  MockQuadCuller quad_culler;
26  gfx::Size layer_size = gfx::Size(800, 600);
27  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
28
29  FakeImplProxy proxy;
30  TestSharedBitmapManager shared_bitmap_manager;
31  FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
32  scoped_ptr<SolidColorLayerImpl> layer =
33      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
34  layer->draw_properties().visible_content_rect = visible_content_rect;
35  layer->SetBounds(layer_size);
36  layer->SetContentBounds(layer_size);
37  layer->CreateRenderSurface();
38  layer->draw_properties().render_target = layer.get();
39
40  AppendQuadsData data;
41  layer->AppendQuads(&quad_culler, &data);
42
43  LayerTestCommon::VerifyQuadsExactlyCoverRect(quad_culler.quad_list(),
44                                               visible_content_rect);
45}
46
47TEST(SolidColorLayerImplTest, VerifyCorrectBackgroundColorInQuad) {
48  SkColor test_color = 0xFFA55AFF;
49
50  MockQuadCuller quad_culler;
51  gfx::Size layer_size = gfx::Size(100, 100);
52  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
53
54  FakeImplProxy proxy;
55  TestSharedBitmapManager shared_bitmap_manager;
56  FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
57  scoped_ptr<SolidColorLayerImpl> layer =
58      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
59  layer->draw_properties().visible_content_rect = visible_content_rect;
60  layer->SetBounds(layer_size);
61  layer->SetContentBounds(layer_size);
62  layer->SetBackgroundColor(test_color);
63  layer->CreateRenderSurface();
64  layer->draw_properties().render_target = layer.get();
65
66  AppendQuadsData data;
67  layer->AppendQuads(&quad_culler, &data);
68
69  ASSERT_EQ(quad_culler.quad_list().size(), 1U);
70  EXPECT_EQ(SolidColorDrawQuad::MaterialCast(quad_culler.quad_list()[0])->color,
71            test_color);
72}
73
74TEST(SolidColorLayerImplTest, VerifyCorrectOpacityInQuad) {
75  const float opacity = 0.5f;
76
77  MockQuadCuller quad_culler;
78  gfx::Size layer_size = gfx::Size(100, 100);
79  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
80
81  FakeImplProxy proxy;
82  TestSharedBitmapManager shared_bitmap_manager;
83  FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
84  scoped_ptr<SolidColorLayerImpl> layer =
85      SolidColorLayerImpl::Create(host_impl.active_tree(), 1);
86  layer->draw_properties().visible_content_rect = visible_content_rect;
87  layer->SetBounds(layer_size);
88  layer->SetContentBounds(layer_size);
89  layer->draw_properties().opacity = opacity;
90  layer->CreateRenderSurface();
91  layer->draw_properties().render_target = layer.get();
92
93  AppendQuadsData data;
94  layer->AppendQuads(&quad_culler, &data);
95
96  ASSERT_EQ(quad_culler.quad_list().size(), 1U);
97  EXPECT_EQ(opacity,
98            SolidColorDrawQuad::MaterialCast(quad_culler.quad_list()[0])
99                ->opacity());
100}
101
102TEST(SolidColorLayerImplTest, VerifyOpaqueRect) {
103  gfx::Size layer_size = gfx::Size(100, 100);
104  gfx::Rect visible_content_rect = gfx::Rect(layer_size);
105
106  scoped_refptr<SolidColorLayer> layer = SolidColorLayer::Create();
107  layer->SetBounds(layer_size);
108  layer->SetForceRenderSurface(true);
109
110  scoped_refptr<Layer> root = Layer::Create();
111  root->AddChild(layer);
112
113  scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create();
114  host->SetRootLayer(root);
115
116  RenderSurfaceLayerList render_surface_layer_list;
117  LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
118      root, gfx::Size(500, 500), &render_surface_layer_list);
119  LayerTreeHostCommon::CalculateDrawProperties(&inputs);
120
121  EXPECT_FALSE(layer->contents_opaque());
122  layer->SetBackgroundColor(SkColorSetARGBInline(255, 10, 20, 30));
123  EXPECT_TRUE(layer->contents_opaque());
124  {
125    scoped_ptr<SolidColorLayerImpl> layer_impl =
126        SolidColorLayerImpl::Create(host->host_impl()->active_tree(),
127                                    layer->id());
128    layer->PushPropertiesTo(layer_impl.get());
129
130    // The impl layer should call itself opaque as well.
131    EXPECT_TRUE(layer_impl->contents_opaque());
132
133    // Impl layer has 1 opacity, and the color is opaque, so the opaque_rect
134    // should be the full tile.
135    layer_impl->draw_properties().opacity = 1;
136
137    MockQuadCuller quad_culler;
138    AppendQuadsData data;
139    layer_impl->AppendQuads(&quad_culler, &data);
140
141    ASSERT_EQ(quad_culler.quad_list().size(), 1U);
142    EXPECT_EQ(visible_content_rect.ToString(),
143              quad_culler.quad_list()[0]->opaque_rect.ToString());
144  }
145
146  EXPECT_TRUE(layer->contents_opaque());
147  layer->SetBackgroundColor(SkColorSetARGBInline(254, 10, 20, 30));
148  EXPECT_FALSE(layer->contents_opaque());
149  {
150    scoped_ptr<SolidColorLayerImpl> layer_impl =
151        SolidColorLayerImpl::Create(host->host_impl()->active_tree(),
152                                    layer->id());
153    layer->PushPropertiesTo(layer_impl.get());
154
155    // The impl layer should callnot itself opaque anymore.
156    EXPECT_FALSE(layer_impl->contents_opaque());
157
158    // Impl layer has 1 opacity, but the color is not opaque, so the opaque_rect
159    // should be empty.
160    layer_impl->draw_properties().opacity = 1;
161
162    MockQuadCuller quad_culler;
163    AppendQuadsData data;
164    layer_impl->AppendQuads(&quad_culler, &data);
165
166    ASSERT_EQ(quad_culler.quad_list().size(), 1U);
167    EXPECT_EQ(gfx::Rect().ToString(),
168              quad_culler.quad_list()[0]->opaque_rect.ToString());
169  }
170}
171
172TEST(SolidColorLayerImplTest, Occlusion) {
173  gfx::Size layer_size(1000, 1000);
174  gfx::Size viewport_size(1000, 1000);
175
176  LayerTestCommon::LayerImplTest impl;
177
178  SolidColorLayerImpl* solid_color_layer_impl =
179      impl.AddChildToRoot<SolidColorLayerImpl>();
180  solid_color_layer_impl->SetBackgroundColor(SkColorSetARGB(255, 10, 20, 30));
181  solid_color_layer_impl->SetBounds(layer_size);
182  solid_color_layer_impl->SetContentBounds(layer_size);
183  solid_color_layer_impl->SetDrawsContent(true);
184
185  impl.CalcDrawProps(viewport_size);
186
187  {
188    SCOPED_TRACE("No occlusion");
189    gfx::Rect occluded;
190    impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded);
191
192    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
193                                                 gfx::Rect(layer_size));
194    EXPECT_EQ(16u, impl.quad_list().size());
195  }
196
197  {
198    SCOPED_TRACE("Full occlusion");
199    gfx::Rect occluded(solid_color_layer_impl->visible_content_rect());
200    impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded);
201
202    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
203    EXPECT_EQ(impl.quad_list().size(), 0u);
204  }
205
206  {
207    SCOPED_TRACE("Partial occlusion");
208    gfx::Rect occluded(200, 200, 256 * 3, 256 * 3);
209    impl.AppendQuadsWithOcclusion(solid_color_layer_impl, occluded);
210
211    size_t partially_occluded_count = 0;
212    LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
213        impl.quad_list(),
214        gfx::Rect(layer_size),
215        occluded,
216        &partially_occluded_count);
217    // 4 quads are completely occluded, 8 are partially occluded.
218    EXPECT_EQ(16u - 4u, impl.quad_list().size());
219    EXPECT_EQ(8u, partially_occluded_count);
220  }
221}
222
223}  // namespace
224}  // namespace cc
225