Remove chrome/browser/chromeos/system/syslogs_provider.h/cc
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_no_message_loop.cc
blob3e87b556db430974259f4a72fe930191058d43c1
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"
26 namespace cc {
27 namespace {
29 class NoMessageLoopOutputSurface : public OutputSurface {
30 public:
31 NoMessageLoopOutputSurface() : OutputSurface(TestContextProvider::Create()) {}
32 ~NoMessageLoopOutputSurface() override {}
34 // OutputSurface overrides.
35 void SwapBuffers(CompositorFrame* frame) override {
36 DCHECK(client_);
37 client_->DidSwapBuffers();
38 client_->DidSwapBuffersComplete();
42 class LayerTreeHostNoMessageLoopTest
43 : public testing::Test,
44 public base::DelegateSimpleThread::Delegate,
45 public LayerTreeHostClient,
46 public LayerTreeHostSingleThreadClient {
47 public:
48 LayerTreeHostNoMessageLoopTest()
49 : did_initialize_output_surface_(false),
50 did_commit_(false),
51 did_commit_and_draw_frame_(false),
52 size_(100, 100),
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,
64 float page_scale,
65 float top_controls_delta) override {}
66 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
67 float page_scale,
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 {}
86 void RunTest() {
87 no_loop_thread_.Start();
88 no_loop_thread_.Join();
91 // base::DelegateSimpleThread::Delegate override.
92 void Run() override {
93 ASSERT_FALSE(base::MessageLoopProxy::current().get());
94 RunTestWithoutMessageLoop();
95 EXPECT_FALSE(base::MessageLoopProxy::current().get());
98 protected:
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_);
111 void Composite() {
112 did_commit_ = false;
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_;
131 bool did_commit_;
132 bool did_commit_and_draw_frame_;
133 gfx::Size size_;
135 private:
136 base::DelegateSimpleThread no_loop_thread_;
139 class LayerTreeHostNoMessageLoopSmokeTest
140 : public LayerTreeHostNoMessageLoopTest {
141 protected:
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();
156 Composite();
157 TearDownLayerTreeHost();
161 TEST_F(LayerTreeHostNoMessageLoopSmokeTest, SmokeTest) {
162 RunTest();
165 class LayerTreeHostNoMessageLoopDelegatedLayer
166 : public LayerTreeHostNoMessageLoopTest,
167 public DelegatedFrameResourceCollectionClient {
168 protected:
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();
175 delegated_layer_ =
176 FakeDelegatedRendererLayer::Create(frame_provider_.get());
177 delegated_layer_->SetBounds(size_);
178 delegated_layer_->SetIsDrawable(true);
179 root_layer_->AddChild(delegated_layer_);
181 SetupLayerTreeHost();
183 // Draw first frame.
184 Composite();
186 // Prepare and draw second frame.
187 frame_provider_->SetFrameData(CreateFrameDataWithResource(999));
188 Composite();
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 {}
205 private:
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());
212 root_pass->SetNew(
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);
222 return frame.Pass();
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) {
238 RunTest();
241 } // namespace
242 } // namespace cc