Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob61c9a04bfbe56c0f202c041b2b5a33b90ba19bdd
1 // Copyright 2011 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 "cc/test/layer_tree_test.h"
7 #include "base/command_line.h"
8 #include "cc/animation/animation.h"
9 #include "cc/animation/animation_registrar.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/base/thread_impl.h"
13 #include "cc/input/input_handler.h"
14 #include "cc/layers/content_layer.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_impl.h"
17 #include "cc/test/animation_test_common.h"
18 #include "cc/test/fake_layer_tree_host_client.h"
19 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/occlusion_tracker_test_common.h"
21 #include "cc/test/tiled_layer_test_common.h"
22 #include "cc/trees/layer_tree_host_impl.h"
23 #include "cc/trees/single_thread_proxy.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "ui/gfx/size_conversions.h"
27 namespace cc {
29 TestHooks::TestHooks()
30 : fake_client_(
31 new FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
33 TestHooks::~TestHooks() {}
35 bool TestHooks::PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
36 LayerTreeHostImpl::FrameData* frame_data,
37 bool result) {
38 return true;
41 bool TestHooks::CanActivatePendingTree() {
42 return true;
45 scoped_ptr<OutputSurface> TestHooks::CreateOutputSurface() {
46 return CreateFakeOutputSurface();
49 scoped_refptr<cc::ContextProvider> TestHooks::
50 OffscreenContextProviderForMainThread() {
51 return fake_client_->OffscreenContextProviderForMainThread();
54 scoped_refptr<cc::ContextProvider> TestHooks::
55 OffscreenContextProviderForCompositorThread() {
56 return fake_client_->OffscreenContextProviderForCompositorThread();
59 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
60 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
61 public:
62 static scoped_ptr<LayerTreeHostImplForTesting> Create(
63 TestHooks* test_hooks,
64 const LayerTreeSettings& settings,
65 LayerTreeHostImplClient* host_impl_client,
66 Proxy* proxy,
67 RenderingStatsInstrumentation* stats_instrumentation) {
68 return make_scoped_ptr(
69 new LayerTreeHostImplForTesting(test_hooks,
70 settings,
71 host_impl_client,
72 proxy,
73 stats_instrumentation));
76 protected:
77 LayerTreeHostImplForTesting(
78 TestHooks* test_hooks,
79 const LayerTreeSettings& settings,
80 LayerTreeHostImplClient* host_impl_client,
81 Proxy* proxy,
82 RenderingStatsInstrumentation* stats_instrumentation)
83 : LayerTreeHostImpl(settings,
84 host_impl_client,
85 proxy,
86 stats_instrumentation),
87 test_hooks_(test_hooks) {}
89 virtual void BeginCommit() OVERRIDE {
90 LayerTreeHostImpl::BeginCommit();
91 test_hooks_->BeginCommitOnThread(this);
94 virtual void CommitComplete() OVERRIDE {
95 LayerTreeHostImpl::CommitComplete();
96 test_hooks_->CommitCompleteOnThread(this);
98 if (!settings().impl_side_painting)
99 test_hooks_->TreeActivatedOnThread(this);
102 virtual bool PrepareToDraw(FrameData* frame, gfx::Rect damage_rect) OVERRIDE {
103 bool result = LayerTreeHostImpl::PrepareToDraw(frame, damage_rect);
104 if (!test_hooks_->PrepareToDrawOnThread(this, frame, result))
105 result = false;
106 return result;
109 virtual void DrawLayers(FrameData* frame,
110 base::TimeTicks frame_begin_time) OVERRIDE {
111 LayerTreeHostImpl::DrawLayers(frame, frame_begin_time);
112 test_hooks_->DrawLayersOnThread(this);
115 virtual bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) OVERRIDE {
116 bool result = LayerTreeHostImpl::SwapBuffers(frame);
117 test_hooks_->SwapBuffersOnThread(this, result);
118 return result;
121 virtual bool ActivatePendingTreeIfNeeded() OVERRIDE {
122 if (!pending_tree())
123 return false;
125 if (!test_hooks_->CanActivatePendingTree())
126 return false;
128 bool activated = LayerTreeHostImpl::ActivatePendingTreeIfNeeded();
129 if (activated)
130 test_hooks_->TreeActivatedOnThread(this);
131 return activated;
134 virtual bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface)
135 OVERRIDE {
136 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
137 test_hooks_->InitializedRendererOnThread(this, success);
138 return success;
141 virtual void SetVisible(bool visible) OVERRIDE {
142 LayerTreeHostImpl::SetVisible(visible);
143 test_hooks_->DidSetVisibleOnImplTree(this, visible);
146 virtual void AnimateLayers(base::TimeTicks monotonic_time,
147 base::Time wall_clock_time) OVERRIDE {
148 test_hooks_->WillAnimateLayers(this, monotonic_time);
149 LayerTreeHostImpl::AnimateLayers(monotonic_time, wall_clock_time);
150 test_hooks_->AnimateLayers(this, monotonic_time);
153 virtual void UpdateAnimationState(bool start_ready_animations) OVERRIDE {
154 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
155 bool has_unfinished_animation = false;
156 AnimationRegistrar::AnimationControllerMap::const_iterator iter =
157 active_animation_controllers().begin();
158 for (; iter != active_animation_controllers().end(); ++iter) {
159 if (iter->second->HasActiveAnimation()) {
160 has_unfinished_animation = true;
161 break;
164 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
167 virtual base::TimeDelta LowFrequencyAnimationInterval() const OVERRIDE {
168 return base::TimeDelta::FromMilliseconds(16);
171 private:
172 TestHooks* test_hooks_;
175 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
176 class LayerTreeHostForTesting : public cc::LayerTreeHost {
177 public:
178 static scoped_ptr<LayerTreeHostForTesting> Create(
179 TestHooks* test_hooks,
180 cc::LayerTreeHostClient* host_client,
181 const cc::LayerTreeSettings& settings,
182 scoped_ptr<cc::Thread> impl_thread) {
183 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
184 new LayerTreeHostForTesting(test_hooks, host_client, settings));
185 bool success = layer_tree_host->Initialize(impl_thread.Pass());
186 EXPECT_TRUE(success);
187 return layer_tree_host.Pass();
190 virtual scoped_ptr<cc::LayerTreeHostImpl> CreateLayerTreeHostImpl(
191 cc::LayerTreeHostImplClient* host_impl_client) OVERRIDE {
192 return LayerTreeHostImplForTesting::Create(
193 test_hooks_,
194 settings(),
195 host_impl_client,
196 proxy(),
197 rendering_stats_instrumentation()).PassAs<cc::LayerTreeHostImpl>();
200 virtual void SetNeedsCommit() OVERRIDE {
201 if (!test_started_)
202 return;
203 LayerTreeHost::SetNeedsCommit();
206 void set_test_started(bool started) { test_started_ = started; }
208 virtual void DidDeferCommit() OVERRIDE {
209 test_hooks_->DidDeferCommit();
212 private:
213 LayerTreeHostForTesting(TestHooks* test_hooks,
214 cc::LayerTreeHostClient* client,
215 const cc::LayerTreeSettings& settings)
216 : LayerTreeHost(client, settings),
217 test_hooks_(test_hooks),
218 test_started_(false) {}
220 TestHooks* test_hooks_;
221 bool test_started_;
224 // Implementation of LayerTreeHost callback interface.
225 class LayerTreeHostClientForTesting : public LayerTreeHostClient {
226 public:
227 static scoped_ptr<LayerTreeHostClientForTesting> Create(
228 TestHooks* test_hooks) {
229 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
231 virtual ~LayerTreeHostClientForTesting() {}
233 virtual void WillBeginFrame() OVERRIDE {}
235 virtual void DidBeginFrame() OVERRIDE {}
237 virtual void Animate(double monotonic_time) OVERRIDE {
238 test_hooks_->Animate(base::TimeTicks::FromInternalValue(
239 monotonic_time * base::Time::kMicrosecondsPerSecond));
242 virtual void Layout() OVERRIDE {
243 test_hooks_->Layout();
246 virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta,
247 float scale) OVERRIDE {
248 test_hooks_->ApplyScrollAndScale(scroll_delta, scale);
251 virtual scoped_ptr<OutputSurface> CreateOutputSurface() OVERRIDE {
252 return test_hooks_->CreateOutputSurface();
255 virtual void DidRecreateOutputSurface(bool succeeded) OVERRIDE {
256 test_hooks_->DidRecreateOutputSurface(succeeded);
259 virtual void WillRetryRecreateOutputSurface() OVERRIDE {
260 test_hooks_->WillRetryRecreateOutputSurface();
263 virtual scoped_ptr<InputHandler> CreateInputHandler() OVERRIDE {
264 return scoped_ptr<InputHandler>();
267 virtual void WillCommit() OVERRIDE {}
269 virtual void DidCommit() OVERRIDE {
270 test_hooks_->DidCommit();
273 virtual void DidCommitAndDrawFrame() OVERRIDE {
274 test_hooks_->DidCommitAndDrawFrame();
277 virtual void DidCompleteSwapBuffers() OVERRIDE {}
279 virtual void ScheduleComposite() OVERRIDE {
280 test_hooks_->ScheduleComposite();
283 virtual scoped_refptr<cc::ContextProvider>
284 OffscreenContextProviderForMainThread() OVERRIDE {
285 return test_hooks_->OffscreenContextProviderForMainThread();
288 virtual scoped_refptr<cc::ContextProvider>
289 OffscreenContextProviderForCompositorThread() OVERRIDE {
290 return test_hooks_->OffscreenContextProviderForCompositorThread();
293 private:
294 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
295 : test_hooks_(test_hooks) {}
297 TestHooks* test_hooks_;
300 LayerTreeTest::LayerTreeTest()
301 : beginning_(false),
302 end_when_begin_returns_(false),
303 timed_out_(false),
304 scheduled_(false),
305 schedule_when_set_visible_true_(false),
306 started_(false),
307 ended_(false),
308 timeout_seconds_(0),
309 impl_thread_(NULL),
310 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
311 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
313 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
314 // specified (for running in a debugger).
315 CommandLine* command_line = CommandLine::ForCurrentProcess();
316 if (!command_line->HasSwitch("cc-layer-tree-test-no-timeout"))
317 timeout_seconds_ = 5;
320 LayerTreeTest::~LayerTreeTest() {}
322 void LayerTreeTest::EndTest() {
323 // For the case where we EndTest during BeginTest(), set a flag to indicate
324 // that the test should end the second BeginTest regains control.
325 if (beginning_) {
326 end_when_begin_returns_ = true;
327 } else if (proxy()) {
328 // Racy timeouts and explicit EndTest calls might have cleaned up
329 // the tree host. Should check proxy first.
330 proxy()->MainThread()->PostTask(
331 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
335 void LayerTreeTest::EndTestAfterDelay(int delay_milliseconds) {
336 proxy()->MainThread()->PostTask(
337 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_));
340 void LayerTreeTest::PostAddAnimationToMainThread(
341 Layer* layer_to_receive_animation) {
342 proxy()->MainThread()->PostTask(
343 base::Bind(&LayerTreeTest::DispatchAddAnimation,
344 main_thread_weak_ptr_,
345 base::Unretained(layer_to_receive_animation)));
348 void LayerTreeTest::PostAddInstantAnimationToMainThread(
349 Layer* layer_to_receive_animation) {
350 proxy()->MainThread()->PostTask(
351 base::Bind(&LayerTreeTest::DispatchAddInstantAnimation,
352 main_thread_weak_ptr_,
353 base::Unretained(layer_to_receive_animation)));
356 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
357 proxy()->MainThread()->PostTask(
358 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
359 main_thread_weak_ptr_));
362 void LayerTreeTest::PostAcquireLayerTextures() {
363 proxy()->MainThread()->PostTask(
364 base::Bind(&LayerTreeTest::DispatchAcquireLayerTextures,
365 main_thread_weak_ptr_));
368 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
369 proxy()->MainThread()->PostTask(
370 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
371 main_thread_weak_ptr_));
374 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
375 proxy()->MainThread()->PostTask(
376 base::Bind(&LayerTreeTest::DispatchSetVisible,
377 main_thread_weak_ptr_,
378 visible));
381 void LayerTreeTest::DoBeginTest() {
382 client_ = LayerTreeHostClientForTesting::Create(this);
384 scoped_ptr<cc::Thread> impl_ccthread(NULL);
385 if (impl_thread_) {
386 impl_ccthread = cc::ThreadImpl::CreateForDifferentThread(
387 impl_thread_->message_loop_proxy());
389 layer_tree_host_ = LayerTreeHostForTesting::Create(this,
390 client_.get(),
391 settings_,
392 impl_ccthread.Pass());
393 ASSERT_TRUE(layer_tree_host_);
395 started_ = true;
396 beginning_ = true;
397 SetupTree();
398 layer_tree_host_->SetSurfaceReady();
399 BeginTest();
400 beginning_ = false;
401 if (end_when_begin_returns_)
402 RealEndTest();
404 // Allow commits to happen once BeginTest() has had a chance to post tasks
405 // so that those tasks will happen before the first commit.
406 if (layer_tree_host_) {
407 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())->
408 set_test_started(true);
412 void LayerTreeTest::SetupTree() {
413 if (!layer_tree_host_->root_layer()) {
414 scoped_refptr<Layer> root_layer = Layer::Create();
415 root_layer->SetAnchorPoint(gfx::PointF());
416 root_layer->SetBounds(gfx::Size(1, 1));
417 root_layer->SetIsDrawable(true);
418 layer_tree_host_->SetRootLayer(root_layer);
421 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
422 gfx::Size device_root_bounds = gfx::ToCeiledSize(
423 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
424 layer_tree_host_->SetViewportSize(device_root_bounds);
427 void LayerTreeTest::Timeout() {
428 timed_out_ = true;
429 EndTest();
432 void LayerTreeTest::ScheduleComposite() {
433 if (!started_ || scheduled_)
434 return;
435 scheduled_ = true;
436 proxy()->MainThread()->PostTask(
437 base::Bind(&LayerTreeTest::DispatchComposite, main_thread_weak_ptr_));
440 void LayerTreeTest::RealEndTest() {
441 ended_ = true;
443 if (layer_tree_host_ && proxy()->CommitPendingForTesting()) {
444 proxy()->MainThread()->PostTask(
445 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
446 return;
449 MessageLoop::current()->Quit();
452 void LayerTreeTest::DispatchAddInstantAnimation(
453 Layer* layer_to_receive_animation) {
454 DCHECK(!proxy() || proxy()->IsMainThread());
456 if (layer_to_receive_animation) {
457 AddOpacityTransitionToLayer(layer_to_receive_animation,
460 0.5,
461 false);
465 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation) {
466 DCHECK(!proxy() || proxy()->IsMainThread());
468 if (layer_to_receive_animation) {
469 AddOpacityTransitionToLayer(layer_to_receive_animation,
470 0.000001,
472 0.5,
473 true);
477 void LayerTreeTest::DispatchSetNeedsCommit() {
478 DCHECK(!proxy() || proxy()->IsMainThread());
480 if (layer_tree_host_)
481 layer_tree_host_->SetNeedsCommit();
484 void LayerTreeTest::DispatchAcquireLayerTextures() {
485 DCHECK(!proxy() || proxy()->IsMainThread());
487 if (layer_tree_host_)
488 layer_tree_host_->AcquireLayerTextures();
491 void LayerTreeTest::DispatchSetNeedsRedraw() {
492 DCHECK(!proxy() || proxy()->IsMainThread());
494 if (layer_tree_host_)
495 layer_tree_host_->SetNeedsRedraw();
498 void LayerTreeTest::DispatchSetVisible(bool visible) {
499 DCHECK(!proxy() || proxy()->IsMainThread());
501 if (!layer_tree_host_)
502 return;
504 layer_tree_host_->SetVisible(visible);
506 // If the LTH is being made visible and a previous ScheduleComposite() was
507 // deferred because the LTH was not visible, re-schedule the composite now.
508 if (layer_tree_host_->visible() && schedule_when_set_visible_true_)
509 ScheduleComposite();
512 void LayerTreeTest::DispatchComposite() {
513 scheduled_ = false;
515 if (!layer_tree_host_)
516 return;
518 // If the LTH is not visible, defer the composite until the LTH is made
519 // visible.
520 if (!layer_tree_host_->visible()) {
521 schedule_when_set_visible_true_ = true;
522 return;
525 schedule_when_set_visible_true_ = false;
526 base::TimeTicks now = base::TimeTicks::Now();
527 layer_tree_host_->UpdateAnimations(now);
528 layer_tree_host_->Composite(now);
531 void LayerTreeTest::RunTest(bool threaded) {
532 if (threaded) {
533 impl_thread_.reset(new base::Thread("LayerTreeTest"));
534 ASSERT_TRUE(impl_thread_->Start());
537 main_ccthread_ = cc::ThreadImpl::CreateForCurrentThread();
539 // Spend less time waiting for vsync because the output is mocked out.
540 settings_.refresh_rate = 200.0;
541 InitializeSettings(&settings_);
543 main_ccthread_->PostTask(
544 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
546 if (timeout_seconds_) {
547 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
548 main_ccthread_->PostDelayedTask(
549 timeout_.callback(),
550 base::TimeDelta::FromSeconds(timeout_seconds_));
553 MessageLoop::current()->Run();
554 if (layer_tree_host_ && layer_tree_host_->root_layer())
555 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
556 layer_tree_host_.reset();
558 timeout_.Cancel();
560 ASSERT_FALSE(layer_tree_host_.get());
561 client_.reset();
562 if (timed_out_) {
563 FAIL() << "Test timed out";
564 return;
566 AfterTest();
569 } // namespace cc