1 // Copyright 2014 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 "base/message_loop/message_loop_proxy.h"
6 #include "base/threading/simple_thread.h"
7 #include "cc/layers/delegated_frame_provider.h"
8 #include "cc/layers/delegated_frame_resource_collection.h"
9 #include "cc/layers/delegated_renderer_layer.h"
10 #include "cc/layers/layer.h"
11 #include "cc/layers/solid_color_layer.h"
12 #include "cc/output/delegated_frame_data.h"
13 #include "cc/output/output_surface.h"
14 #include "cc/output/output_surface_client.h"
15 #include "cc/resources/resource_provider.h"
16 #include "cc/scheduler/begin_frame_source.h"
17 #include "cc/test/fake_delegated_renderer_layer.h"
18 #include "cc/test/test_context_provider.h"
19 #include "cc/trees/layer_tree_host.h"
20 #include "cc/trees/layer_tree_host_client.h"
21 #include "cc/trees/layer_tree_host_single_thread_client.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/skia/include/core/SkColor.h"
24 #include "ui/gfx/frame_time.h"
29 class NoMessageLoopOutputSurface
: public OutputSurface
{
31 NoMessageLoopOutputSurface() : OutputSurface(TestContextProvider::Create()) {}
32 ~NoMessageLoopOutputSurface() override
{}
34 // OutputSurface overrides.
35 void SwapBuffers(CompositorFrame
* frame
) override
{
37 client_
->DidSwapBuffers();
38 client_
->DidSwapBuffersComplete();
42 class LayerTreeHostNoMessageLoopTest
43 : public testing::Test
,
44 public base::DelegateSimpleThread::Delegate
,
45 public LayerTreeHostClient
,
46 public LayerTreeHostSingleThreadClient
{
48 LayerTreeHostNoMessageLoopTest()
49 : did_initialize_output_surface_(false),
51 did_commit_and_draw_frame_(false),
53 no_loop_thread_(this, "LayerTreeHostNoMessageLoopTest") {}
54 ~LayerTreeHostNoMessageLoopTest() override
{}
56 // LayerTreeHostClient overrides.
57 void WillBeginMainFrame(int frame_id
) override
{}
58 void BeginMainFrame(const BeginFrameArgs
& args
) override
{}
59 void DidBeginMainFrame() override
{}
60 void Layout() override
{}
61 void ApplyViewportDeltas(const gfx::Vector2d
& inner_delta
,
62 const gfx::Vector2d
& outer_delta
,
63 const gfx::Vector2dF
& elastic_overscroll_delta
,
65 float top_controls_delta
) override
{}
66 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
68 float top_controls_delta
) override
{}
69 void RequestNewOutputSurface() override
{
70 layer_tree_host_
->SetOutputSurface(
71 make_scoped_ptr
<OutputSurface
>(new NoMessageLoopOutputSurface
));
73 void DidInitializeOutputSurface() override
{
74 did_initialize_output_surface_
= true;
76 void DidFailToInitializeOutputSurface() override
{}
77 void WillCommit() override
{}
78 void DidCommit() override
{ did_commit_
= true; }
79 void DidCommitAndDrawFrame() override
{ did_commit_and_draw_frame_
= true; }
80 void DidCompleteSwapBuffers() override
{}
82 // LayerTreeHostSingleThreadClient overrides.
83 void DidPostSwapBuffers() override
{}
84 void DidAbortSwapBuffers() override
{}
87 no_loop_thread_
.Start();
88 no_loop_thread_
.Join();
91 // base::DelegateSimpleThread::Delegate override.
93 ASSERT_FALSE(base::MessageLoopProxy::current().get());
94 RunTestWithoutMessageLoop();
95 EXPECT_FALSE(base::MessageLoopProxy::current().get());
99 virtual void RunTestWithoutMessageLoop() = 0;
101 void SetupLayerTreeHost() {
102 LayerTreeSettings settings
;
103 settings
.single_thread_proxy_scheduler
= false;
104 settings
.verify_property_trees
= true;
105 layer_tree_host_
= LayerTreeHost::CreateSingleThreaded(
106 this, this, nullptr, nullptr, settings
, nullptr, nullptr);
107 layer_tree_host_
->SetViewportSize(size_
);
108 layer_tree_host_
->SetRootLayer(root_layer_
);
113 did_commit_and_draw_frame_
= false;
114 layer_tree_host_
->Composite(gfx::FrameTime::Now());
115 EXPECT_TRUE(did_initialize_output_surface_
);
116 EXPECT_TRUE(did_commit_
);
117 EXPECT_TRUE(did_commit_and_draw_frame_
);
120 void TearDownLayerTreeHost() {
121 // Explicit teardown to make failures easier to debug.
122 layer_tree_host_
= nullptr;
123 root_layer_
= nullptr;
126 // All protected member variables are accessed only on |no_loop_thread_|.
127 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
128 scoped_refptr
<Layer
> root_layer_
;
130 bool did_initialize_output_surface_
;
132 bool did_commit_and_draw_frame_
;
136 base::DelegateSimpleThread no_loop_thread_
;
139 class LayerTreeHostNoMessageLoopSmokeTest
140 : public LayerTreeHostNoMessageLoopTest
{
142 void RunTestWithoutMessageLoop() override
{
143 gfx::Size
size(100, 100);
145 // Set up root layer.
147 scoped_refptr
<SolidColorLayer
> solid_color_layer
=
148 SolidColorLayer::Create();
149 solid_color_layer
->SetBackgroundColor(SK_ColorRED
);
150 solid_color_layer
->SetBounds(size_
);
151 solid_color_layer
->SetIsDrawable(true);
152 root_layer_
= solid_color_layer
;
155 SetupLayerTreeHost();
157 TearDownLayerTreeHost();
161 TEST_F(LayerTreeHostNoMessageLoopSmokeTest
, SmokeTest
) {
165 class LayerTreeHostNoMessageLoopDelegatedLayer
166 : public LayerTreeHostNoMessageLoopTest
,
167 public DelegatedFrameResourceCollectionClient
{
169 void RunTestWithoutMessageLoop() override
{
170 resource_collection_
= new DelegatedFrameResourceCollection
;
171 frame_provider_
= new DelegatedFrameProvider(
172 resource_collection_
.get(), CreateFrameDataWithResource(998));
174 root_layer_
= Layer::Create();
176 FakeDelegatedRendererLayer::Create(frame_provider_
.get());
177 delegated_layer_
->SetBounds(size_
);
178 delegated_layer_
->SetIsDrawable(true);
179 root_layer_
->AddChild(delegated_layer_
);
181 SetupLayerTreeHost();
186 // Prepare and draw second frame.
187 frame_provider_
->SetFrameData(CreateFrameDataWithResource(999));
190 // Resource from first frame should be returned.
191 CheckReturnedResource(1u);
193 TearDownLayerTreeHost();
194 delegated_layer_
= NULL
;
195 frame_provider_
= NULL
;
197 // Resource from second frame should be returned.
198 CheckReturnedResource(1u);
199 resource_collection_
= NULL
;
202 // DelegatedFrameResourceCollectionClient overrides.
203 void UnusedResourcesAreAvailable() override
{}
206 scoped_ptr
<DelegatedFrameData
> CreateFrameDataWithResource(
207 ResourceProvider::ResourceId resource_id
) {
208 scoped_ptr
<DelegatedFrameData
> frame(new DelegatedFrameData
);
209 gfx::Rect
frame_rect(size_
);
211 scoped_ptr
<RenderPass
> root_pass(RenderPass::Create());
213 RenderPassId(1, 1), frame_rect
, frame_rect
, gfx::Transform());
214 frame
->render_pass_list
.push_back(root_pass
.Pass());
216 TransferableResource resource
;
217 resource
.id
= resource_id
;
218 resource
.mailbox_holder
.texture_target
= GL_TEXTURE_2D
;
219 resource
.mailbox_holder
.mailbox
= gpu::Mailbox::Generate();
220 frame
->resource_list
.push_back(resource
);
225 void CheckReturnedResource(size_t expected_num
) {
226 ReturnedResourceArray returned_resources
;
227 resource_collection_
->TakeUnusedResourcesForChildCompositor(
228 &returned_resources
);
229 EXPECT_EQ(expected_num
, returned_resources
.size());
232 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
233 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
234 scoped_refptr
<DelegatedRendererLayer
> delegated_layer_
;
237 TEST_F(LayerTreeHostNoMessageLoopDelegatedLayer
, SingleDelegatedLayer
) {