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/ui_resource_layer.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "cc/resources/prioritized_resource_manager.h"
9 #include "cc/resources/resource_provider.h"
10 #include "cc/resources/resource_update_queue.h"
11 #include "cc/resources/scoped_ui_resource.h"
12 #include "cc/test/fake_layer_tree_host.h"
13 #include "cc/test/fake_layer_tree_host_client.h"
14 #include "cc/test/fake_output_surface.h"
15 #include "cc/test/fake_output_surface_client.h"
16 #include "cc/test/geometry_test_utils.h"
17 #include "cc/trees/layer_tree_host.h"
18 #include "cc/trees/occlusion_tracker.h"
19 #include "cc/trees/single_thread_proxy.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/skia/include/core/SkBitmap.h"
24 using ::testing::Mock
;
26 using ::testing::AtLeast
;
27 using ::testing::AnyNumber
;
32 class TestUIResourceLayer
: public UIResourceLayer
{
34 static scoped_refptr
<TestUIResourceLayer
> Create(
35 const LayerSettings
& settings
) {
36 return make_scoped_refptr(new TestUIResourceLayer(settings
));
39 UIResourceId
GetUIResourceId() {
40 if (ui_resource_holder_
)
41 return ui_resource_holder_
->id();
46 explicit TestUIResourceLayer(const LayerSettings
& settings
)
47 : UIResourceLayer(settings
) {
50 ~TestUIResourceLayer() override
{}
53 class UIResourceLayerTest
: public testing::Test
{
55 UIResourceLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
58 void SetUp() override
{
59 layer_tree_host_
= FakeLayerTreeHost::Create(&fake_client_
);
60 layer_tree_host_
->InitializeSingleThreaded(
61 &fake_client_
, base::ThreadTaskRunnerHandle::Get(), nullptr);
64 void TearDown() override
{
65 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
68 FakeLayerTreeHostClient fake_client_
;
69 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
70 LayerSettings layer_settings_
;
73 TEST_F(UIResourceLayerTest
, SetBitmap
) {
74 scoped_refptr
<UIResourceLayer
> test_layer
=
75 TestUIResourceLayer::Create(layer_settings_
);
76 ASSERT_TRUE(test_layer
.get());
77 test_layer
->SetBounds(gfx::Size(100, 100));
79 layer_tree_host_
->SetRootLayer(test_layer
);
80 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
81 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
83 ResourceUpdateQueue queue
;
84 gfx::Rect screen_space_clip_rect
;
85 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
86 test_layer
->SavePaintProperties();
87 test_layer
->Update(&queue
, &occlusion_tracker
);
89 EXPECT_FALSE(test_layer
->DrawsContent());
92 bitmap
.allocN32Pixels(10, 10);
93 bitmap
.setImmutable();
95 test_layer
->SetBitmap(bitmap
);
96 test_layer
->Update(&queue
, &occlusion_tracker
);
98 EXPECT_TRUE(test_layer
->DrawsContent());
101 TEST_F(UIResourceLayerTest
, SetUIResourceId
) {
102 scoped_refptr
<TestUIResourceLayer
> test_layer
=
103 TestUIResourceLayer::Create(layer_settings_
);
104 ASSERT_TRUE(test_layer
.get());
105 test_layer
->SetBounds(gfx::Size(100, 100));
107 layer_tree_host_
->SetRootLayer(test_layer
);
108 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
109 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
111 ResourceUpdateQueue queue
;
112 gfx::Rect screen_space_clip_rect
;
113 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
114 test_layer
->SavePaintProperties();
115 test_layer
->Update(&queue
, &occlusion_tracker
);
117 EXPECT_FALSE(test_layer
->DrawsContent());
119 bool is_opaque
= false;
120 scoped_ptr
<ScopedUIResource
> resource
= ScopedUIResource::Create(
121 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(10, 10), is_opaque
));
122 test_layer
->SetUIResourceId(resource
->id());
123 test_layer
->Update(&queue
, &occlusion_tracker
);
125 EXPECT_TRUE(test_layer
->DrawsContent());
127 // ID is preserved even when you set ID first and attach it to the tree.
128 layer_tree_host_
->SetRootLayer(nullptr);
129 scoped_ptr
<ScopedUIResource
> shared_resource
= ScopedUIResource::Create(
130 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(5, 5), is_opaque
));
131 test_layer
->SetUIResourceId(shared_resource
->id());
132 layer_tree_host_
->SetRootLayer(test_layer
);
133 EXPECT_EQ(shared_resource
->id(), test_layer
->GetUIResourceId());
134 EXPECT_TRUE(test_layer
->DrawsContent());
137 TEST_F(UIResourceLayerTest
, BitmapClearedOnSetUIResourceId
) {
138 scoped_refptr
<UIResourceLayer
> test_layer
=
139 TestUIResourceLayer::Create(layer_settings_
);
140 ASSERT_TRUE(test_layer
.get());
141 test_layer
->SetBounds(gfx::Size(100, 100));
144 bitmap
.allocN32Pixels(10, 10);
145 bitmap
.setImmutable();
146 ASSERT_FALSE(bitmap
.isNull());
147 ASSERT_TRUE(bitmap
.pixelRef()->unique());
149 test_layer
->SetBitmap(bitmap
);
150 ASSERT_FALSE(bitmap
.pixelRef()->unique());
152 test_layer
->SetUIResourceId(0);
153 EXPECT_TRUE(bitmap
.pixelRef()->unique());