blob: dc24e1dd5c0623bebf4ec814ac030df450ba0fb4 [file] [log] [blame]
[email protected]efbdb3a2013-10-04 00:35:131// 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/append_quads_data.h"
6#include "cc/layers/ui_resource_layer_impl.h"
[email protected]c6707fd2014-06-23 05:50:367#include "cc/quads/draw_quad.h"
[email protected]efbdb3a2013-10-04 00:35:138#include "cc/resources/ui_resource_bitmap.h"
9#include "cc/resources/ui_resource_client.h"
10#include "cc/test/fake_impl_proxy.h"
11#include "cc/test/fake_layer_tree_host_impl.h"
12#include "cc/test/fake_ui_resource_layer_tree_host_impl.h"
13#include "cc/test/layer_test_common.h"
[email protected]4e2eb352014-03-20 17:25:4514#include "cc/test/test_shared_bitmap_manager.h"
[email protected]efbdb3a2013-10-04 00:35:1315#include "cc/trees/single_thread_proxy.h"
16#include "testing/gmock/include/gmock/gmock.h"
17#include "testing/gtest/include/gtest/gtest.h"
18#include "ui/gfx/transform.h"
19
20namespace cc {
21namespace {
22
23scoped_ptr<UIResourceLayerImpl> GenerateUIResourceLayer(
24 FakeUIResourceLayerTreeHostImpl* host_impl,
[email protected]64348ea2014-01-29 22:58:2625 const gfx::Size& bitmap_size,
26 const gfx::Size& layer_size,
[email protected]709c9542013-10-26 01:43:5127 bool opaque,
[email protected]efbdb3a2013-10-04 00:35:1328 UIResourceId uid) {
29 gfx::Rect visible_content_rect(layer_size);
30 scoped_ptr<UIResourceLayerImpl> layer =
31 UIResourceLayerImpl::Create(host_impl->active_tree(), 1);
32 layer->draw_properties().visible_content_rect = visible_content_rect;
33 layer->SetBounds(layer_size);
34 layer->SetContentBounds(layer_size);
35 layer->CreateRenderSurface();
36 layer->draw_properties().render_target = layer.get();
37
[email protected]0046982c2014-03-25 22:00:5138 UIResourceBitmap bitmap(bitmap_size, opaque);
[email protected]efbdb3a2013-10-04 00:35:1339
40 host_impl->CreateUIResource(uid, bitmap);
41 layer->SetUIResourceId(uid);
42
43 return layer.Pass();
44}
45
46void QuadSizeTest(scoped_ptr<UIResourceLayerImpl> layer,
47 size_t expected_quad_size) {
[email protected]b74d7f282014-06-13 22:01:4248 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
[email protected]b74d7f282014-06-13 22:01:4249
[email protected]efbdb3a2013-10-04 00:35:1350 AppendQuadsData data;
vmpstr11b77b432014-10-07 20:11:4051 layer->AppendQuads(render_pass.get(), Occlusion(), &data);
[email protected]efbdb3a2013-10-04 00:35:1352
53 // Verify quad rects
[email protected]c6707fd2014-06-23 05:50:3654 const QuadList& quads = render_pass->quad_list;
[email protected]efbdb3a2013-10-04 00:35:1355 EXPECT_EQ(expected_quad_size, quads.size());
56}
57
58TEST(UIResourceLayerImplTest, VerifyDrawQuads) {
59 FakeImplProxy proxy;
[email protected]4e2eb352014-03-20 17:25:4560 TestSharedBitmapManager shared_bitmap_manager;
61 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
[email protected]efbdb3a2013-10-04 00:35:1362 // Make sure we're appending quads when there are valid values.
63 gfx::Size bitmap_size(100, 100);
64 gfx::Size layer_size(100, 100);;
65 size_t expected_quad_size = 1;
[email protected]709c9542013-10-26 01:43:5166 bool opaque = true;
[email protected]efbdb3a2013-10-04 00:35:1367 UIResourceId uid = 1;
68 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
69 bitmap_size,
70 layer_size,
[email protected]709c9542013-10-26 01:43:5171 opaque,
[email protected]efbdb3a2013-10-04 00:35:1372 uid);
73 QuadSizeTest(layer.Pass(), expected_quad_size);
74
75 // Make sure we're not appending quads when there are invalid values.
76 expected_quad_size = 0;
77 uid = 0;
78 layer = GenerateUIResourceLayer(&host_impl,
79 bitmap_size,
80 layer_size,
[email protected]709c9542013-10-26 01:43:5181 opaque,
[email protected]efbdb3a2013-10-04 00:35:1382 uid);
83 QuadSizeTest(layer.Pass(), expected_quad_size);
84}
85
86void OpaqueBoundsTest(scoped_ptr<UIResourceLayerImpl> layer,
[email protected]0023fc72014-01-10 20:05:0687 const gfx::Rect& expected_opaque_bounds) {
[email protected]b74d7f282014-06-13 22:01:4288 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
[email protected]b74d7f282014-06-13 22:01:4289
[email protected]efbdb3a2013-10-04 00:35:1390 AppendQuadsData data;
vmpstr11b77b432014-10-07 20:11:4091 layer->AppendQuads(render_pass.get(), Occlusion(), &data);
[email protected]efbdb3a2013-10-04 00:35:1392
93 // Verify quad rects
[email protected]c6707fd2014-06-23 05:50:3694 const QuadList& quads = render_pass->quad_list;
[email protected]efbdb3a2013-10-04 00:35:1395 EXPECT_GE(quads.size(), (size_t)0);
weiliangc032e929d2014-09-26 01:55:0196 gfx::Rect opaque_rect = quads.front()->opaque_rect;
[email protected]efbdb3a2013-10-04 00:35:1397 EXPECT_EQ(expected_opaque_bounds, opaque_rect);
98}
99
[email protected]709c9542013-10-26 01:43:51100TEST(UIResourceLayerImplTest, VerifySetOpaqueOnSkBitmap) {
[email protected]efbdb3a2013-10-04 00:35:13101 FakeImplProxy proxy;
[email protected]4e2eb352014-03-20 17:25:45102 TestSharedBitmapManager shared_bitmap_manager;
103 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
[email protected]efbdb3a2013-10-04 00:35:13104
105 gfx::Size bitmap_size(100, 100);
106 gfx::Size layer_size(100, 100);;
[email protected]709c9542013-10-26 01:43:51107 bool opaque = false;
[email protected]efbdb3a2013-10-04 00:35:13108 UIResourceId uid = 1;
109 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(&host_impl,
110 bitmap_size,
111 layer_size,
[email protected]709c9542013-10-26 01:43:51112 opaque,
[email protected]efbdb3a2013-10-04 00:35:13113 uid);
[email protected]709c9542013-10-26 01:43:51114 gfx::Rect expected_opaque_bounds;
115 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
116
117 opaque = true;
118 layer = GenerateUIResourceLayer(&host_impl,
119 bitmap_size,
120 layer_size,
121 opaque,
122 uid);
bokancccfde72014-10-08 15:15:22123 expected_opaque_bounds = gfx::Rect(layer->bounds());
[email protected]709c9542013-10-26 01:43:51124 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
125}
126
127TEST(UIResourceLayerImplTest, VerifySetOpaqueOnLayer) {
128 FakeImplProxy proxy;
[email protected]4e2eb352014-03-20 17:25:45129 TestSharedBitmapManager shared_bitmap_manager;
130 FakeUIResourceLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
[email protected]709c9542013-10-26 01:43:51131
132 gfx::Size bitmap_size(100, 100);
133 gfx::Size layer_size(100, 100);
134 bool skbitmap_opaque = false;
135 UIResourceId uid = 1;
136 scoped_ptr<UIResourceLayerImpl> layer = GenerateUIResourceLayer(
137 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
[email protected]efbdb3a2013-10-04 00:35:13138 layer->SetContentsOpaque(false);
139 gfx::Rect expected_opaque_bounds;
140 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
141
[email protected]709c9542013-10-26 01:43:51142 layer = GenerateUIResourceLayer(
143 &host_impl, bitmap_size, layer_size, skbitmap_opaque, uid);
[email protected]efbdb3a2013-10-04 00:35:13144 layer->SetContentsOpaque(true);
bokancccfde72014-10-08 15:15:22145 expected_opaque_bounds = gfx::Rect(layer->bounds());
[email protected]efbdb3a2013-10-04 00:35:13146 OpaqueBoundsTest(layer.Pass(), expected_opaque_bounds);
147}
148
[email protected]35390892014-03-20 06:36:13149TEST(UIResourceLayerImplTest, Occlusion) {
150 gfx::Size layer_size(1000, 1000);
151 gfx::Size viewport_size(1000, 1000);
152
153 LayerTestCommon::LayerImplTest impl;
154
155 SkBitmap sk_bitmap;
156 sk_bitmap.allocN32Pixels(10, 10);
157 sk_bitmap.setImmutable();
158 UIResourceId uid = 5;
159 UIResourceBitmap bitmap(sk_bitmap);
160 impl.host_impl()->CreateUIResource(uid, bitmap);
161
162 UIResourceLayerImpl* ui_resource_layer_impl =
163 impl.AddChildToRoot<UIResourceLayerImpl>();
[email protected]35390892014-03-20 06:36:13164 ui_resource_layer_impl->SetBounds(layer_size);
165 ui_resource_layer_impl->SetContentBounds(layer_size);
166 ui_resource_layer_impl->SetDrawsContent(true);
167 ui_resource_layer_impl->SetUIResourceId(uid);
168
169 impl.CalcDrawProps(viewport_size);
170
171 {
172 SCOPED_TRACE("No occlusion");
173 gfx::Rect occluded;
174 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
175
176 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
177 gfx::Rect(layer_size));
178 EXPECT_EQ(1u, impl.quad_list().size());
179 }
180
181 {
182 SCOPED_TRACE("Full occlusion");
183 gfx::Rect occluded(ui_resource_layer_impl->visible_content_rect());
184 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
185
186 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
187 EXPECT_EQ(impl.quad_list().size(), 0u);
188 }
189
190 {
191 SCOPED_TRACE("Partial occlusion");
192 gfx::Rect occluded(200, 0, 800, 1000);
193 impl.AppendQuadsWithOcclusion(ui_resource_layer_impl, occluded);
194
195 size_t partially_occluded_count = 0;
hartmanng53af0fe2014-09-22 20:26:11196 LayerTestCommon::VerifyQuadsAreOccluded(
197 impl.quad_list(), occluded, &partially_occluded_count);
[email protected]35390892014-03-20 06:36:13198 // The layer outputs one quad, which is partially occluded.
199 EXPECT_EQ(1u, impl.quad_list().size());
200 EXPECT_EQ(1u, partially_occluded_count);
201 }
202}
203
[email protected]efbdb3a2013-10-04 00:35:13204} // namespace
205} // namespace cc