Simplify web_view.js
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_no_message_loop.cc
blobaa5a5080928bb483273090c91871a214d77c72bd
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"
25 namespace cc {
26 namespace {
28 class NoMessageLoopOutputSurface : public OutputSurface {
29 public:
30 NoMessageLoopOutputSurface() : OutputSurface(TestContextProvider::Create()) {}
31 ~NoMessageLoopOutputSurface() override {}
33 // OutputSurface overrides.
34 void SwapBuffers(CompositorFrame* frame) override {
35 DCHECK(client_);
36 client_->DidSwapBuffers();
37 client_->DidSwapBuffersComplete();
41 class LayerTreeHostNoMessageLoopTest
42 : public testing::Test,
43 public base::DelegateSimpleThread::Delegate,
44 public LayerTreeHostClient,
45 public LayerTreeHostSingleThreadClient {
46 public:
47 LayerTreeHostNoMessageLoopTest()
48 : did_initialize_output_surface_(false),
49 did_commit_(false),
50 did_commit_and_draw_frame_(false),
51 size_(100, 100),
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,
62 float page_scale,
63 float top_controls_delta) override {}
64 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
65 float page_scale,
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 {}
83 void RunTest() {
84 no_loop_thread_.Start();
85 no_loop_thread_.Join();
88 // base::DelegateSimpleThread::Delegate override.
89 void Run() override {
90 ASSERT_FALSE(base::MessageLoopProxy::current().get());
91 RunTestWithoutMessageLoop();
92 EXPECT_FALSE(base::MessageLoopProxy::current().get());
95 protected:
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_);
107 void Composite() {
108 did_commit_ = false;
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_;
127 bool did_commit_;
128 bool did_commit_and_draw_frame_;
129 gfx::Size size_;
131 private:
132 base::DelegateSimpleThread no_loop_thread_;
135 class LayerTreeHostNoMessageLoopSmokeTest
136 : public LayerTreeHostNoMessageLoopTest {
137 protected:
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();
152 Composite();
153 TearDownLayerTreeHost();
157 TEST_F(LayerTreeHostNoMessageLoopSmokeTest, SmokeTest) {
158 RunTest();
161 class LayerTreeHostNoMessageLoopDelegatedLayer
162 : public LayerTreeHostNoMessageLoopTest,
163 public DelegatedFrameResourceCollectionClient {
164 protected:
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();
171 delegated_layer_ =
172 FakeDelegatedRendererLayer::Create(frame_provider_.get());
173 delegated_layer_->SetBounds(size_);
174 delegated_layer_->SetIsDrawable(true);
175 root_layer_->AddChild(delegated_layer_);
177 SetupLayerTreeHost();
179 // Draw first frame.
180 Composite();
182 // Prepare and draw second frame.
183 frame_provider_->SetFrameData(CreateFrameDataWithResource(999));
184 Composite();
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 {}
201 private:
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());
208 root_pass->SetNew(
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);
218 return frame.Pass();
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) {
234 RunTest();
237 } // namespace
238 } // namespace cc