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 layer_tree_host_
= LayerTreeHost::CreateSingleThreaded(
108 this, this, nullptr, nullptr, settings
, nullptr, nullptr);
109 layer_tree_host_
->SetViewportSize(size_
);
110 layer_tree_host_
->SetRootLayer(root_layer_
);
115 did_commit_and_draw_frame_
= false;
116 layer_tree_host_
->Composite(gfx::FrameTime::Now());
117 EXPECT_TRUE(did_initialize_output_surface_
);
118 EXPECT_TRUE(did_commit_
);
119 EXPECT_TRUE(did_commit_and_draw_frame_
);
122 void TearDownLayerTreeHost() {
123 // Explicit teardown to make failures easier to debug.
124 layer_tree_host_
= nullptr;
125 root_layer_
= nullptr;
128 // All protected member variables are accessed only on |no_loop_thread_|.
129 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
130 scoped_refptr
<Layer
> root_layer_
;
132 bool did_initialize_output_surface_
;
134 bool did_commit_and_draw_frame_
;
138 base::DelegateSimpleThread no_loop_thread_
;
141 class LayerTreeHostNoMessageLoopSmokeTest
142 : public LayerTreeHostNoMessageLoopTest
{
144 void RunTestWithoutMessageLoop() override
{
145 gfx::Size
size(100, 100);
147 // Set up root layer.
149 scoped_refptr
<SolidColorLayer
> solid_color_layer
=
150 SolidColorLayer::Create();
151 solid_color_layer
->SetBackgroundColor(SK_ColorRED
);
152 solid_color_layer
->SetBounds(size_
);
153 solid_color_layer
->SetIsDrawable(true);
154 root_layer_
= solid_color_layer
;
157 SetupLayerTreeHost();
159 TearDownLayerTreeHost();
163 TEST_F(LayerTreeHostNoMessageLoopSmokeTest
, SmokeTest
) {
167 class LayerTreeHostNoMessageLoopDelegatedLayer
168 : public LayerTreeHostNoMessageLoopTest
,
169 public DelegatedFrameResourceCollectionClient
{
171 void RunTestWithoutMessageLoop() override
{
172 resource_collection_
= new DelegatedFrameResourceCollection
;
173 frame_provider_
= new DelegatedFrameProvider(
174 resource_collection_
.get(), CreateFrameDataWithResource(998));
176 root_layer_
= Layer::Create();
178 FakeDelegatedRendererLayer::Create(frame_provider_
.get());
179 delegated_layer_
->SetBounds(size_
);
180 delegated_layer_
->SetIsDrawable(true);
181 root_layer_
->AddChild(delegated_layer_
);
183 SetupLayerTreeHost();
188 // Prepare and draw second frame.
189 frame_provider_
->SetFrameData(CreateFrameDataWithResource(999));
192 // Resource from first frame should be returned.
193 CheckReturnedResource(1u);
195 TearDownLayerTreeHost();
196 delegated_layer_
= NULL
;
197 frame_provider_
= NULL
;
199 // Resource from second frame should be returned.
200 CheckReturnedResource(1u);
201 resource_collection_
= NULL
;
204 // DelegatedFrameResourceCollectionClient overrides.
205 void UnusedResourcesAreAvailable() override
{}
208 scoped_ptr
<DelegatedFrameData
> CreateFrameDataWithResource(
209 ResourceProvider::ResourceId resource_id
) {
210 scoped_ptr
<DelegatedFrameData
> frame(new DelegatedFrameData
);
211 gfx::Rect
frame_rect(size_
);
213 scoped_ptr
<RenderPass
> root_pass(RenderPass::Create());
215 RenderPassId(1, 1), frame_rect
, frame_rect
, gfx::Transform());
216 frame
->render_pass_list
.push_back(root_pass
.Pass());
218 TransferableResource resource
;
219 resource
.id
= resource_id
;
220 resource
.mailbox_holder
.texture_target
= GL_TEXTURE_2D
;
221 resource
.mailbox_holder
.mailbox
= gpu::Mailbox::Generate();
222 frame
->resource_list
.push_back(resource
);
227 void CheckReturnedResource(size_t expected_num
) {
228 ReturnedResourceArray returned_resources
;
229 resource_collection_
->TakeUnusedResourcesForChildCompositor(
230 &returned_resources
);
231 EXPECT_EQ(expected_num
, returned_resources
.size());
234 scoped_refptr
<DelegatedFrameResourceCollection
> resource_collection_
;
235 scoped_refptr
<DelegatedFrameProvider
> frame_provider_
;
236 scoped_refptr
<DelegatedRendererLayer
> delegated_layer_
;
239 TEST_F(LayerTreeHostNoMessageLoopDelegatedLayer
, SingleDelegatedLayer
) {