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/test/fake_delegated_renderer_layer.h"
17 #include "cc/test/test_context_provider.h"
18 #include "cc/trees/layer_tree_host.h"
19 #include "cc/trees/layer_tree_host_client.h"
20 #include "cc/trees/layer_tree_host_single_thread_client.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/gfx/frame_time.h"
28 class NoMessageLoopOutputSurface
: public OutputSurface
{
30 NoMessageLoopOutputSurface() : OutputSurface(TestContextProvider::Create()) {}
31 ~NoMessageLoopOutputSurface() override
{}
33 // OutputSurface overrides.
34 void SwapBuffers(CompositorFrame
* frame
) override
{
36 client_
->DidSwapBuffers();
37 client_
->DidSwapBuffersComplete();
41 class LayerTreeHostNoMessageLoopTest
42 : public testing::Test
,
43 public base::DelegateSimpleThread::Delegate
,
44 public LayerTreeHostClient
,
45 public LayerTreeHostSingleThreadClient
{
47 LayerTreeHostNoMessageLoopTest()
48 : did_initialize_output_surface_(false),
50 did_commit_and_draw_frame_(false),
52 no_loop_thread_(this, "LayerTreeHostNoMessageLoopTest") {}
53 virtual ~LayerTreeHostNoMessageLoopTest() {}
55 // LayerTreeHostClient overrides.
56 void WillBeginMainFrame(int frame_id
) override
{}
57 void BeginMainFrame(const BeginFrameArgs
& args
) override
{}
58 void DidBeginMainFrame() override
{}
59 void Layout() override
{}
60 void ApplyViewportDeltas(const gfx::Vector2d
& inner_delta
,
61 const gfx::Vector2d
& outer_delta
,
63 float top_controls_delta
) override
{}
64 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
66 float top_controls_delta
) override
{}
67 void RequestNewOutputSurface(bool fallback
) override
{
68 layer_tree_host_
->SetOutputSurface(
69 make_scoped_ptr
<OutputSurface
>(new NoMessageLoopOutputSurface
));
71 void DidInitializeOutputSurface() override
{
72 did_initialize_output_surface_
= true;
74 void WillCommit() override
{}
75 void DidCommit() override
{ did_commit_
= true; }
76 void DidCommitAndDrawFrame() override
{ did_commit_and_draw_frame_
= true; }
77 void DidCompleteSwapBuffers() override
{}
79 // LayerTreeHostSingleThreadClient overrides.
80 void DidPostSwapBuffers() override
{}
81 void DidAbortSwapBuffers() override
{}
84 no_loop_thread_
.Start();
85 no_loop_thread_
.Join();
88 // base::DelegateSimpleThread::Delegate override.
90 ASSERT_FALSE(base::MessageLoopProxy::current().get());
91 RunTestWithoutMessageLoop();
92 EXPECT_FALSE(base::MessageLoopProxy::current().get());
96 virtual void RunTestWithoutMessageLoop() = 0;
98 void SetupLayerTreeHost() {
99 LayerTreeSettings settings
;
100 settings
.single_thread_proxy_scheduler
= false;
101 layer_tree_host_
= LayerTreeHost::CreateSingleThreaded(
102 this, this, NULL
, NULL
, settings
, NULL
);
103 layer_tree_host_
->SetViewportSize(size_
);
104 layer_tree_host_
->SetRootLayer(root_layer_
);
109 did_commit_and_draw_frame_
= false;
110 layer_tree_host_
->Composite(gfx::FrameTime::Now());
111 EXPECT_TRUE(did_initialize_output_surface_
);
112 EXPECT_TRUE(did_commit_
);
113 EXPECT_TRUE(did_commit_and_draw_frame_
);
116 void TearDownLayerTreeHost() {
117 // Explicit teardown to make failures easier to debug.
118 layer_tree_host_
= nullptr;
119 root_layer_
= nullptr;
122 // All protected member variables are accessed only on |no_loop_thread_|.
123 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
124 scoped_refptr
<Layer
> root_layer_
;
126 bool did_initialize_output_surface_
;
128 bool did_commit_and_draw_frame_
;
132 base::DelegateSimpleThread no_loop_thread_
;
135 class LayerTreeHostNoMessageLoopSmokeTest
136 : public LayerTreeHostNoMessageLoopTest
{
138 void RunTestWithoutMessageLoop() override
{
139 gfx::Size
size(100, 100);
141 // Set up root layer.
143 scoped_refptr
<SolidColorLayer
> solid_color_layer
=
144 SolidColorLayer::Create();
145 solid_color_layer
->SetBackgroundColor(SK_ColorRED
);
146 solid_color_layer
->SetBounds(size_
);
147 solid_color_layer
->SetIsDrawable(true);
148 root_layer_
= solid_color_layer
;
151 SetupLayerTreeHost();
153 TearDownLayerTreeHost();
157 TEST_F(LayerTreeHostNoMessageLoopSmokeTest
, SmokeTest
) {
161 class LayerTreeHostNoMessageLoopDelegatedLayer
162 : public LayerTreeHostNoMessageLoopTest
,
163 public DelegatedFrameResourceCollectionClient
{
165 void RunTestWithoutMessageLoop() override
{
166 resource_collection_
= new DelegatedFrameResourceCollection
;
167 frame_provider_
= new DelegatedFrameProvider(
168 resource_collection_
.get(), CreateFrameDataWithResource(998));
170 root_layer_
= Layer::Create();
172 FakeDelegatedRendererLayer::Create(frame_provider_
.get());
173 delegated_layer_
->SetBounds(size_
);
174 delegated_layer_
->SetIsDrawable(true);
175 root_layer_
->AddChild(delegated_layer_
);
177 SetupLayerTreeHost();
182 // Prepare and draw second frame.
183 frame_provider_
->SetFrameData(CreateFrameDataWithResource(999));
186 // Resource from first frame should be returned.
187 CheckReturnedResource(1u);
189 TearDownLayerTreeHost();
190 delegated_layer_
= NULL
;
191 frame_provider_
= NULL
;
193 // Resource from second frame should be returned.
194 CheckReturnedResource(1u);
195 resource_collection_
= NULL
;
198 // DelegatedFrameResourceCollectionClient overrides.
199 void UnusedResourcesAreAvailable() override
{}
202 scoped_ptr
<DelegatedFrameData
> CreateFrameDataWithResource(
203 ResourceProvider::ResourceId resource_id
) {
204 scoped_ptr
<DelegatedFrameData
> frame(new DelegatedFrameData
);
205 gfx::Rect
frame_rect(size_
);
207 scoped_ptr
<RenderPass
> root_pass(RenderPass::Create());
209 RenderPassId(1, 1), frame_rect
, frame_rect
, gfx::Transform());
210 frame
->render_pass_list
.push_back(root_pass
.Pass());
212 TransferableResource resource
;
213 resource
.id
= resource_id
;
214 resource
.mailbox_holder
.texture_target
= GL_TEXTURE_2D
;
215 resource
.mailbox_holder
.mailbox
= gpu::Mailbox::Generate();
216 frame
->resource_list
.push_back(resource
);
221 void CheckReturnedResource(size_t expected_num
) {
222 ReturnedResourceArray returned_resources
;
223 resource_collection_
->TakeUnusedResourcesForChildCompositor(
224 &returned_resources
);
225 EXPECT_EQ(expected_num
, returned_resources
.size());
228 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
229 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
230 scoped_refptr
<DelegatedRendererLayer
> delegated_layer_
;
233 TEST_F(LayerTreeHostNoMessageLoopDelegatedLayer
, SingleDelegatedLayer
) {