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 return make_scoped_refptr(new TestUIResourceLayer());
38 UIResourceId
GetUIResourceId() {
39 if (ui_resource_holder_
)
40 return ui_resource_holder_
->id();
45 TestUIResourceLayer() : UIResourceLayer() { SetIsDrawable(true); }
46 ~TestUIResourceLayer() override
{}
49 class UIResourceLayerTest
: public testing::Test
{
51 UIResourceLayerTest() : fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
54 void SetUp() override
{
55 layer_tree_host_
= FakeLayerTreeHost::Create(&fake_client_
);
56 layer_tree_host_
->InitializeSingleThreaded(
57 &fake_client_
, base::ThreadTaskRunnerHandle::Get(), nullptr);
60 void TearDown() override
{
61 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
64 FakeLayerTreeHostClient fake_client_
;
65 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
68 TEST_F(UIResourceLayerTest
, SetBitmap
) {
69 scoped_refptr
<UIResourceLayer
> test_layer
= TestUIResourceLayer::Create();
70 ASSERT_TRUE(test_layer
.get());
71 test_layer
->SetBounds(gfx::Size(100, 100));
73 layer_tree_host_
->SetRootLayer(test_layer
);
74 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
75 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
77 ResourceUpdateQueue queue
;
78 gfx::Rect screen_space_clip_rect
;
79 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
80 test_layer
->SavePaintProperties();
81 test_layer
->Update(&queue
, &occlusion_tracker
);
83 EXPECT_FALSE(test_layer
->DrawsContent());
86 bitmap
.allocN32Pixels(10, 10);
87 bitmap
.setImmutable();
89 test_layer
->SetBitmap(bitmap
);
90 test_layer
->Update(&queue
, &occlusion_tracker
);
92 EXPECT_TRUE(test_layer
->DrawsContent());
95 TEST_F(UIResourceLayerTest
, SetUIResourceId
) {
96 scoped_refptr
<TestUIResourceLayer
> test_layer
= TestUIResourceLayer::Create();
97 ASSERT_TRUE(test_layer
.get());
98 test_layer
->SetBounds(gfx::Size(100, 100));
100 layer_tree_host_
->SetRootLayer(test_layer
);
101 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
102 EXPECT_EQ(test_layer
->layer_tree_host(), layer_tree_host_
.get());
104 ResourceUpdateQueue queue
;
105 gfx::Rect screen_space_clip_rect
;
106 OcclusionTracker
<Layer
> occlusion_tracker(screen_space_clip_rect
);
107 test_layer
->SavePaintProperties();
108 test_layer
->Update(&queue
, &occlusion_tracker
);
110 EXPECT_FALSE(test_layer
->DrawsContent());
112 bool is_opaque
= false;
113 scoped_ptr
<ScopedUIResource
> resource
= ScopedUIResource::Create(
114 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(10, 10), is_opaque
));
115 test_layer
->SetUIResourceId(resource
->id());
116 test_layer
->Update(&queue
, &occlusion_tracker
);
118 EXPECT_TRUE(test_layer
->DrawsContent());
120 // ID is preserved even when you set ID first and attach it to the tree.
121 layer_tree_host_
->SetRootLayer(nullptr);
122 scoped_ptr
<ScopedUIResource
> shared_resource
= ScopedUIResource::Create(
123 layer_tree_host_
.get(), UIResourceBitmap(gfx::Size(5, 5), is_opaque
));
124 test_layer
->SetUIResourceId(shared_resource
->id());
125 layer_tree_host_
->SetRootLayer(test_layer
);
126 EXPECT_EQ(shared_resource
->id(), test_layer
->GetUIResourceId());
127 EXPECT_TRUE(test_layer
->DrawsContent());
130 TEST_F(UIResourceLayerTest
, BitmapClearedOnSetUIResourceId
) {
131 scoped_refptr
<UIResourceLayer
> test_layer
= TestUIResourceLayer::Create();
132 ASSERT_TRUE(test_layer
.get());
133 test_layer
->SetBounds(gfx::Size(100, 100));
136 bitmap
.allocN32Pixels(10, 10);
137 bitmap
.setImmutable();
138 ASSERT_FALSE(bitmap
.isNull());
139 ASSERT_TRUE(bitmap
.pixelRef()->unique());
141 test_layer
->SetBitmap(bitmap
);
142 ASSERT_FALSE(bitmap
.pixelRef()->unique());
144 test_layer
->SetUIResourceId(0);
145 EXPECT_TRUE(bitmap
.pixelRef()->unique());