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() override
{}
58 void BeginMainFrame(const BeginFrameArgs
& args
) override
{}
59 void BeginMainFrameNotExpectedSoon() override
{}
60 void DidBeginMainFrame() override
{}
61 void Layout() override
{}
62 void ApplyViewportDeltas(const gfx::Vector2dF
& inner_delta
,
63 const gfx::Vector2dF
& outer_delta
,
64 const gfx::Vector2dF
& elastic_overscroll_delta
,
66 float top_controls_delta
) override
{}
67 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
69 float top_controls_delta
) override
{}
70 void RequestNewOutputSurface() override
{
71 layer_tree_host_
->SetOutputSurface(
72 make_scoped_ptr
<OutputSurface
>(new NoMessageLoopOutputSurface
));
74 void DidInitializeOutputSurface() override
{
75 did_initialize_output_surface_
= true;
77 void DidFailToInitializeOutputSurface() override
{}
78 void WillCommit() override
{}
79 void DidCommit() override
{ did_commit_
= true; }
80 void DidCommitAndDrawFrame() override
{ did_commit_and_draw_frame_
= true; }
81 void DidCompleteSwapBuffers() override
{}
82 void DidCompletePageScaleAnimation() override
{}
84 // LayerTreeHostSingleThreadClient overrides.
85 void DidPostSwapBuffers() override
{}
86 void DidAbortSwapBuffers() override
{}
89 no_loop_thread_
.Start();
90 no_loop_thread_
.Join();
93 // base::DelegateSimpleThread::Delegate override.
95 ASSERT_FALSE(base::MessageLoopProxy::current().get());
96 RunTestWithoutMessageLoop();
97 EXPECT_FALSE(base::MessageLoopProxy::current().get());
101 virtual void RunTestWithoutMessageLoop() = 0;
103 void SetupLayerTreeHost() {
104 LayerTreeSettings settings
;
105 settings
.single_thread_proxy_scheduler
= false;
106 settings
.verify_property_trees
= true;
107 settings
.raster_enabled
= false;
108 layer_tree_host_
= LayerTreeHost::CreateSingleThreaded(
109 this, this, nullptr, nullptr, nullptr, settings
, nullptr, nullptr);
110 layer_tree_host_
->SetViewportSize(size_
);
111 layer_tree_host_
->SetRootLayer(root_layer_
);
116 did_commit_and_draw_frame_
= false;
117 layer_tree_host_
->Composite(gfx::FrameTime::Now());
118 EXPECT_TRUE(did_initialize_output_surface_
);
119 EXPECT_TRUE(did_commit_
);
120 EXPECT_TRUE(did_commit_and_draw_frame_
);
123 void TearDownLayerTreeHost() {
124 // Explicit teardown to make failures easier to debug.
125 layer_tree_host_
= nullptr;
126 root_layer_
= nullptr;
129 // All protected member variables are accessed only on |no_loop_thread_|.
130 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
131 scoped_refptr
<Layer
> root_layer_
;
133 bool did_initialize_output_surface_
;
135 bool did_commit_and_draw_frame_
;
139 base::DelegateSimpleThread no_loop_thread_
;
142 class LayerTreeHostNoMessageLoopSmokeTest
143 : public LayerTreeHostNoMessageLoopTest
{
145 void RunTestWithoutMessageLoop() override
{
146 gfx::Size
size(100, 100);
148 // Set up root layer.
150 scoped_refptr
<SolidColorLayer
> solid_color_layer
=
151 SolidColorLayer::Create();
152 solid_color_layer
->SetBackgroundColor(SK_ColorRED
);
153 solid_color_layer
->SetBounds(size_
);
154 solid_color_layer
->SetIsDrawable(true);
155 root_layer_
= solid_color_layer
;
158 SetupLayerTreeHost();
160 TearDownLayerTreeHost();
164 TEST_F(LayerTreeHostNoMessageLoopSmokeTest
, SmokeTest
) {
168 class LayerTreeHostNoMessageLoopDelegatedLayer
169 : public LayerTreeHostNoMessageLoopTest
,
170 public DelegatedFrameResourceCollectionClient
{
172 void RunTestWithoutMessageLoop() override
{
173 resource_collection_
= new DelegatedFrameResourceCollection
;
174 frame_provider_
= new DelegatedFrameProvider(
175 resource_collection_
.get(), CreateFrameDataWithResource(998));
177 root_layer_
= Layer::Create();
179 FakeDelegatedRendererLayer::Create(frame_provider_
.get());
180 delegated_layer_
->SetBounds(size_
);
181 delegated_layer_
->SetIsDrawable(true);
182 root_layer_
->AddChild(delegated_layer_
);
184 SetupLayerTreeHost();
189 // Prepare and draw second frame.
190 frame_provider_
->SetFrameData(CreateFrameDataWithResource(999));
193 // Resource from first frame should be returned.
194 CheckReturnedResource(1u);
196 TearDownLayerTreeHost();
197 delegated_layer_
= NULL
;
198 frame_provider_
= NULL
;
200 // Resource from second frame should be returned.
201 CheckReturnedResource(1u);
202 resource_collection_
= NULL
;
205 // DelegatedFrameResourceCollectionClient overrides.
206 void UnusedResourcesAreAvailable() override
{}
209 scoped_ptr
<DelegatedFrameData
> CreateFrameDataWithResource(
210 ResourceProvider::ResourceId resource_id
) {
211 scoped_ptr
<DelegatedFrameData
> frame(new DelegatedFrameData
);
212 gfx::Rect
frame_rect(size_
);
214 scoped_ptr
<RenderPass
> root_pass(RenderPass::Create());
216 RenderPassId(1, 1), frame_rect
, frame_rect
, gfx::Transform());
217 frame
->render_pass_list
.push_back(root_pass
.Pass());
219 TransferableResource resource
;
220 resource
.id
= resource_id
;
221 resource
.mailbox_holder
.texture_target
= GL_TEXTURE_2D
;
222 resource
.mailbox_holder
.mailbox
= gpu::Mailbox::Generate();
223 frame
->resource_list
.push_back(resource
);
228 void CheckReturnedResource(size_t expected_num
) {
229 ReturnedResourceArray returned_resources
;
230 resource_collection_
->TakeUnusedResourcesForChildCompositor(
231 &returned_resources
);
232 EXPECT_EQ(expected_num
, returned_resources
.size());
235 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
236 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
237 scoped_refptr
<DelegatedRendererLayer
> delegated_layer_
;
240 TEST_F(LayerTreeHostNoMessageLoopDelegatedLayer
, SingleDelegatedLayer
) {