Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blobe45b48060a769112c7136764d9b678c506747350
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 "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "cc/animation/animation.h"
12 #include "cc/animation/animation_registrar.h"
13 #include "cc/animation/layer_animation_controller.h"
14 #include "cc/animation/timing_function.h"
15 #include "cc/base/switches.h"
16 #include "cc/input/input_handler.h"
17 #include "cc/layers/layer.h"
18 #include "cc/layers/layer_impl.h"
19 #include "cc/test/animation_test_common.h"
20 #include "cc/test/begin_frame_args_test.h"
21 #include "cc/test/fake_external_begin_frame_source.h"
22 #include "cc/test/fake_layer_tree_host_client.h"
23 #include "cc/test/fake_output_surface.h"
24 #include "cc/test/test_context_provider.h"
25 #include "cc/test/test_gpu_memory_buffer_manager.h"
26 #include "cc/test/test_shared_bitmap_manager.h"
27 #include "cc/test/test_task_graph_runner.h"
28 #include "cc/trees/layer_tree_host_client.h"
29 #include "cc/trees/layer_tree_host_impl.h"
30 #include "cc/trees/layer_tree_host_single_thread_client.h"
31 #include "cc/trees/layer_tree_impl.h"
32 #include "cc/trees/single_thread_proxy.h"
33 #include "cc/trees/thread_proxy.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "ui/gfx/geometry/size_conversions.h"
37 namespace cc {
39 TestHooks::TestHooks() {}
41 TestHooks::~TestHooks() {}
43 DrawResult TestHooks::PrepareToDrawOnThread(
44 LayerTreeHostImpl* host_impl,
45 LayerTreeHostImpl::FrameData* frame_data,
46 DrawResult draw_result) {
47 return draw_result;
50 void TestHooks::CreateResourceAndTileTaskWorkerPool(
51 LayerTreeHostImpl* host_impl,
52 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
53 scoped_ptr<ResourcePool>* resource_pool,
54 scoped_ptr<ResourcePool>* staging_resource_pool) {
55 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
56 tile_task_worker_pool, resource_pool, staging_resource_pool);
59 // Adapts ThreadProxy for test. Injects test hooks for testing.
60 class ThreadProxyForTest : public ThreadProxy {
61 public:
62 static scoped_ptr<Proxy> Create(
63 TestHooks* test_hooks,
64 LayerTreeHost* host,
65 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
66 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
67 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
68 return make_scoped_ptr(new ThreadProxyForTest(
69 test_hooks,
70 host,
71 main_task_runner,
72 impl_task_runner,
73 external_begin_frame_source.Pass()));
76 ~ThreadProxyForTest() override {}
78 private:
79 TestHooks* test_hooks_;
81 void ScheduledActionSendBeginMainFrame() override {
82 test_hooks_->ScheduledActionWillSendBeginMainFrame();
83 ThreadProxy::ScheduledActionSendBeginMainFrame();
84 test_hooks_->ScheduledActionSendBeginMainFrame();
87 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
88 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
89 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
90 return result;
93 void ScheduledActionAnimate() override {
94 ThreadProxy::ScheduledActionAnimate();
95 test_hooks_->ScheduledActionAnimate();
98 void ScheduledActionCommit() override {
99 ThreadProxy::ScheduledActionCommit();
100 test_hooks_->ScheduledActionCommit();
103 void ScheduledActionBeginOutputSurfaceCreation() override {
104 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
105 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
108 void ScheduledActionPrepareTiles() override {
109 ThreadProxy::ScheduledActionPrepareTiles();
110 test_hooks_->ScheduledActionPrepareTiles();
113 void ScheduledActionInvalidateOutputSurface() override {
114 ThreadProxy::ScheduledActionInvalidateOutputSurface();
115 test_hooks_->ScheduledActionInvalidateOutputSurface();
118 void SendBeginMainFrameNotExpectedSoon() override {
119 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
120 test_hooks_->SendBeginMainFrameNotExpectedSoon();
123 ThreadProxyForTest(
124 TestHooks* test_hooks,
125 LayerTreeHost* host,
126 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
127 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
128 scoped_ptr<BeginFrameSource> external_begin_frame_source)
129 : ThreadProxy(host, main_task_runner,
130 impl_task_runner,
131 external_begin_frame_source.Pass()),
132 test_hooks_(test_hooks) {}
135 // Adapts ThreadProxy for test. Injects test hooks for testing.
136 class SingleThreadProxyForTest : public SingleThreadProxy {
137 public:
138 static scoped_ptr<Proxy> Create(
139 TestHooks* test_hooks,
140 LayerTreeHost* host,
141 LayerTreeHostSingleThreadClient* client,
142 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
143 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
144 return make_scoped_ptr(new SingleThreadProxyForTest(
145 test_hooks, host, client, main_task_runner,
146 external_begin_frame_source.Pass()));
149 ~SingleThreadProxyForTest() override {}
151 private:
152 TestHooks* test_hooks_;
154 void ScheduledActionSendBeginMainFrame() override {
155 test_hooks_->ScheduledActionWillSendBeginMainFrame();
156 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
157 test_hooks_->ScheduledActionSendBeginMainFrame();
160 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
161 DrawResult result =
162 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
163 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
164 return result;
167 void ScheduledActionAnimate() override {
168 SingleThreadProxy::ScheduledActionAnimate();
169 test_hooks_->ScheduledActionAnimate();
172 void ScheduledActionCommit() override {
173 SingleThreadProxy::ScheduledActionCommit();
174 test_hooks_->ScheduledActionCommit();
177 void ScheduledActionBeginOutputSurfaceCreation() override {
178 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
179 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
182 void ScheduledActionPrepareTiles() override {
183 SingleThreadProxy::ScheduledActionPrepareTiles();
184 test_hooks_->ScheduledActionPrepareTiles();
187 void ScheduledActionInvalidateOutputSurface() override {
188 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
189 test_hooks_->ScheduledActionInvalidateOutputSurface();
192 void SendBeginMainFrameNotExpectedSoon() override {
193 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
194 test_hooks_->SendBeginMainFrameNotExpectedSoon();
197 SingleThreadProxyForTest(
198 TestHooks* test_hooks,
199 LayerTreeHost* host,
200 LayerTreeHostSingleThreadClient* client,
201 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
202 scoped_ptr<BeginFrameSource> external_begin_frame_source)
203 : SingleThreadProxy(host, client, main_task_runner,
204 external_begin_frame_source.Pass()),
205 test_hooks_(test_hooks) {}
208 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
209 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
210 public:
211 static scoped_ptr<LayerTreeHostImplForTesting> Create(
212 TestHooks* test_hooks,
213 const LayerTreeSettings& settings,
214 LayerTreeHostImplClient* host_impl_client,
215 Proxy* proxy,
216 SharedBitmapManager* shared_bitmap_manager,
217 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
218 TaskGraphRunner* task_graph_runner,
219 RenderingStatsInstrumentation* stats_instrumentation) {
220 return make_scoped_ptr(new LayerTreeHostImplForTesting(
221 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
222 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
225 protected:
226 LayerTreeHostImplForTesting(
227 TestHooks* test_hooks,
228 const LayerTreeSettings& settings,
229 LayerTreeHostImplClient* host_impl_client,
230 Proxy* proxy,
231 SharedBitmapManager* shared_bitmap_manager,
232 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
233 TaskGraphRunner* task_graph_runner,
234 RenderingStatsInstrumentation* stats_instrumentation)
235 : LayerTreeHostImpl(settings,
236 host_impl_client,
237 proxy,
238 stats_instrumentation,
239 shared_bitmap_manager,
240 gpu_memory_buffer_manager,
241 task_graph_runner,
243 test_hooks_(test_hooks),
244 block_notify_ready_to_activate_for_testing_(false),
245 notify_ready_to_activate_was_blocked_(false) {}
247 void CreateResourceAndTileTaskWorkerPool(
248 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
249 scoped_ptr<ResourcePool>* resource_pool,
250 scoped_ptr<ResourcePool>* staging_resource_pool) override {
251 test_hooks_->CreateResourceAndTileTaskWorkerPool(
252 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
255 void WillBeginImplFrame(const BeginFrameArgs& args) override {
256 LayerTreeHostImpl::WillBeginImplFrame(args);
257 test_hooks_->WillBeginImplFrameOnThread(this, args);
260 void DidFinishImplFrame() override {
261 LayerTreeHostImpl::DidFinishImplFrame();
262 test_hooks_->DidFinishImplFrameOnThread(this);
265 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
266 LayerTreeHostImpl::BeginMainFrameAborted(reason);
267 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
270 void BeginCommit() override {
271 LayerTreeHostImpl::BeginCommit();
272 test_hooks_->BeginCommitOnThread(this);
275 void CommitComplete() override {
276 LayerTreeHostImpl::CommitComplete();
277 test_hooks_->CommitCompleteOnThread(this);
280 DrawResult PrepareToDraw(FrameData* frame) override {
281 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
282 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
285 void DrawLayers(FrameData* frame) override {
286 LayerTreeHostImpl::DrawLayers(frame);
287 test_hooks_->DrawLayersOnThread(this);
290 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
291 bool result = LayerTreeHostImpl::SwapBuffers(frame);
292 test_hooks_->SwapBuffersOnThread(this, result);
293 return result;
296 void DidSwapBuffersComplete() override {
297 LayerTreeHostImpl::DidSwapBuffersComplete();
298 test_hooks_->SwapBuffersCompleteOnThread(this);
301 void ReclaimResources(const CompositorFrameAck* ack) override {
302 LayerTreeHostImpl::ReclaimResources(ack);
305 void NotifyReadyToActivate() override {
306 if (block_notify_ready_to_activate_for_testing_) {
307 notify_ready_to_activate_was_blocked_ = true;
308 } else {
309 LayerTreeHostImpl::NotifyReadyToActivate();
310 test_hooks_->NotifyReadyToActivateOnThread(this);
314 void NotifyReadyToDraw() override {
315 LayerTreeHostImpl::NotifyReadyToDraw();
316 test_hooks_->NotifyReadyToDrawOnThread(this);
319 void NotifyAllTileTasksCompleted() override {
320 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
321 test_hooks_->NotifyAllTileTasksCompleted(this);
324 void BlockNotifyReadyToActivateForTesting(bool block) override {
325 CHECK(proxy()->ImplThreadTaskRunner())
326 << "Not supported for single-threaded mode.";
327 block_notify_ready_to_activate_for_testing_ = block;
328 if (!block && notify_ready_to_activate_was_blocked_) {
329 NotifyReadyToActivate();
330 notify_ready_to_activate_was_blocked_ = false;
334 void ActivateSyncTree() override {
335 test_hooks_->WillActivateTreeOnThread(this);
336 LayerTreeHostImpl::ActivateSyncTree();
337 DCHECK(!pending_tree());
338 test_hooks_->DidActivateTreeOnThread(this);
341 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
342 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
343 test_hooks_->InitializedRendererOnThread(this, success);
344 return success;
347 void SetVisible(bool visible) override {
348 LayerTreeHostImpl::SetVisible(visible);
349 test_hooks_->DidSetVisibleOnImplTree(this, visible);
352 void AnimateLayers(base::TimeTicks monotonic_time) override {
353 test_hooks_->WillAnimateLayers(this, monotonic_time);
354 LayerTreeHostImpl::AnimateLayers(monotonic_time);
355 test_hooks_->AnimateLayers(this, monotonic_time);
358 void UpdateAnimationState(bool start_ready_animations) override {
359 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
360 bool has_unfinished_animation = false;
361 for (const auto& it :
362 animation_registrar()->active_animation_controllers_for_testing()) {
363 if (it.second->HasActiveAnimation()) {
364 has_unfinished_animation = true;
365 break;
368 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
371 void NotifyTileStateChanged(const Tile* tile) override {
372 LayerTreeHostImpl::NotifyTileStateChanged(tile);
373 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
376 private:
377 TestHooks* test_hooks_;
378 bool block_notify_ready_to_activate_for_testing_;
379 bool notify_ready_to_activate_was_blocked_;
382 // Implementation of LayerTreeHost callback interface.
383 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
384 public LayerTreeHostSingleThreadClient {
385 public:
386 static scoped_ptr<LayerTreeHostClientForTesting> Create(
387 TestHooks* test_hooks) {
388 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
390 ~LayerTreeHostClientForTesting() override {}
392 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
394 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
396 void BeginMainFrame(const BeginFrameArgs& args) override {
397 test_hooks_->BeginMainFrame(args);
400 void Layout() override { test_hooks_->Layout(); }
402 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
403 const gfx::Vector2dF& outer_delta,
404 const gfx::Vector2dF& elastic_overscroll_delta,
405 float page_scale,
406 float top_controls_delta) override {
407 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
408 elastic_overscroll_delta, page_scale,
409 top_controls_delta);
412 void RequestNewOutputSurface() override {
413 test_hooks_->RequestNewOutputSurface();
416 void DidInitializeOutputSurface() override {
417 test_hooks_->DidInitializeOutputSurface();
420 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
421 test_hooks_->SendBeginFramesToChildren(args);
424 void DidFailToInitializeOutputSurface() override {
425 test_hooks_->DidFailToInitializeOutputSurface();
426 RequestNewOutputSurface();
429 void WillCommit() override { test_hooks_->WillCommit(); }
431 void DidCommit() override { test_hooks_->DidCommit(); }
433 void DidCommitAndDrawFrame() override {
434 test_hooks_->DidCommitAndDrawFrame();
437 void DidCompleteSwapBuffers() override {
438 test_hooks_->DidCompleteSwapBuffers();
441 void DidPostSwapBuffers() override {}
442 void DidAbortSwapBuffers() override {}
443 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
444 void DidCompletePageScaleAnimation() override {}
445 void BeginMainFrameNotExpectedSoon() override {}
447 void RecordFrameTimingEvents(
448 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
449 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
450 override {}
452 private:
453 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
454 : test_hooks_(test_hooks) {}
456 TestHooks* test_hooks_;
459 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
460 class LayerTreeHostForTesting : public LayerTreeHost {
461 public:
462 static scoped_ptr<LayerTreeHostForTesting> Create(
463 TestHooks* test_hooks,
464 LayerTreeHostClientForTesting* client,
465 SharedBitmapManager* shared_bitmap_manager,
466 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
467 TaskGraphRunner* task_graph_runner,
468 const LayerTreeSettings& settings,
469 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
470 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
471 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
472 LayerTreeHost::InitParams params;
473 params.client = client;
474 params.shared_bitmap_manager = shared_bitmap_manager;
475 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
476 params.task_graph_runner = task_graph_runner;
477 params.settings = &settings;
478 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
479 new LayerTreeHostForTesting(test_hooks, &params));
480 if (impl_task_runner.get()) {
481 layer_tree_host->InitializeForTesting(
482 ThreadProxyForTest::Create(test_hooks,
483 layer_tree_host.get(),
484 main_task_runner,
485 impl_task_runner,
486 external_begin_frame_source.Pass()));
487 } else {
488 layer_tree_host->InitializeForTesting(
489 SingleThreadProxyForTest::Create(
490 test_hooks,
491 layer_tree_host.get(),
492 client,
493 main_task_runner,
494 external_begin_frame_source.Pass()));
496 return layer_tree_host.Pass();
499 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
500 LayerTreeHostImplClient* host_impl_client) override {
501 return LayerTreeHostImplForTesting::Create(
502 test_hooks_, settings(), host_impl_client, proxy(),
503 shared_bitmap_manager(), gpu_memory_buffer_manager(),
504 task_graph_runner(), rendering_stats_instrumentation());
507 void SetNeedsCommit() override {
508 if (!test_started_)
509 return;
510 LayerTreeHost::SetNeedsCommit();
513 void set_test_started(bool started) { test_started_ = started; }
515 private:
516 LayerTreeHostForTesting(TestHooks* test_hooks,
517 LayerTreeHost::InitParams* params)
518 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
520 TestHooks* test_hooks_;
521 bool test_started_;
524 LayerTreeTest::LayerTreeTest()
525 : output_surface_(nullptr),
526 external_begin_frame_source_(nullptr),
527 beginning_(false),
528 end_when_begin_returns_(false),
529 timed_out_(false),
530 scheduled_(false),
531 started_(false),
532 ended_(false),
533 delegating_renderer_(false),
534 verify_property_trees_(false),
535 timeout_seconds_(0),
536 weak_factory_(this) {
537 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
539 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
540 // specified (for running in a debugger).
541 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
542 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
543 timeout_seconds_ = 5;
546 LayerTreeTest::~LayerTreeTest() {}
548 void LayerTreeTest::EndTest() {
549 if (ended_)
550 return;
551 ended_ = true;
553 // For the case where we EndTest during BeginTest(), set a flag to indicate
554 // that the test should end the second BeginTest regains control.
555 if (beginning_) {
556 end_when_begin_returns_ = true;
557 } else {
558 main_task_runner_->PostTask(
559 FROM_HERE,
560 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
564 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
565 main_task_runner_->PostDelayedTask(
566 FROM_HERE,
567 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
568 base::TimeDelta::FromMilliseconds(delay_milliseconds));
571 void LayerTreeTest::PostAddAnimationToMainThread(
572 Layer* layer_to_receive_animation) {
573 main_task_runner_->PostTask(
574 FROM_HERE,
575 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
576 base::Unretained(layer_to_receive_animation), 0.000004));
579 void LayerTreeTest::PostAddInstantAnimationToMainThread(
580 Layer* layer_to_receive_animation) {
581 main_task_runner_->PostTask(
582 FROM_HERE,
583 base::Bind(&LayerTreeTest::DispatchAddAnimation,
584 main_thread_weak_ptr_,
585 base::Unretained(layer_to_receive_animation),
586 0.0));
589 void LayerTreeTest::PostAddLongAnimationToMainThread(
590 Layer* layer_to_receive_animation) {
591 main_task_runner_->PostTask(
592 FROM_HERE,
593 base::Bind(&LayerTreeTest::DispatchAddAnimation,
594 main_thread_weak_ptr_,
595 base::Unretained(layer_to_receive_animation),
596 1.0));
599 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
600 main_task_runner_->PostTask(
601 FROM_HERE,
602 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
603 main_thread_weak_ptr_, defer_commits));
606 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
607 main_task_runner_->PostTask(FROM_HERE,
608 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
609 main_thread_weak_ptr_));
612 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
613 main_task_runner_->PostTask(
614 FROM_HERE,
615 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
616 main_thread_weak_ptr_));
619 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
620 main_task_runner_->PostTask(FROM_HERE,
621 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
622 main_thread_weak_ptr_));
625 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
626 const gfx::Rect& damage_rect) {
627 main_task_runner_->PostTask(
628 FROM_HERE,
629 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
630 main_thread_weak_ptr_,
631 damage_rect));
634 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
635 main_task_runner_->PostTask(
636 FROM_HERE,
637 base::Bind(
638 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
641 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
642 main_task_runner_->PostTask(
643 FROM_HERE,
644 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
645 main_thread_weak_ptr_));
648 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
649 main_task_runner_->PostTask(
650 FROM_HERE,
651 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
652 main_thread_weak_ptr_));
655 void LayerTreeTest::WillBeginTest() {
656 layer_tree_host_->SetLayerTreeHostClientReady();
659 void LayerTreeTest::DoBeginTest() {
660 client_ = LayerTreeHostClientForTesting::Create(this);
662 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
663 if (settings_.use_external_begin_frame_source) {
664 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
665 settings_.renderer_settings.refresh_rate));
666 external_begin_frame_source_ = external_begin_frame_source.get();
669 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
670 layer_tree_host_ = LayerTreeHostForTesting::Create(
671 this, client_.get(), shared_bitmap_manager_.get(),
672 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
673 base::ThreadTaskRunnerHandle::Get(),
674 impl_thread_ ? impl_thread_->task_runner() : NULL,
675 external_begin_frame_source.Pass());
676 ASSERT_TRUE(layer_tree_host_);
678 started_ = true;
679 beginning_ = true;
680 SetupTree();
681 WillBeginTest();
682 BeginTest();
683 beginning_ = false;
684 if (end_when_begin_returns_)
685 RealEndTest();
687 // Allow commits to happen once BeginTest() has had a chance to post tasks
688 // so that those tasks will happen before the first commit.
689 if (layer_tree_host_) {
690 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
691 ->set_test_started(true);
695 void LayerTreeTest::SetupTree() {
696 if (!layer_tree_host_->root_layer()) {
697 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
698 root_layer->SetBounds(gfx::Size(1, 1));
699 root_layer->SetIsDrawable(true);
700 layer_tree_host_->SetRootLayer(root_layer);
703 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
704 gfx::Size device_root_bounds = gfx::ToCeiledSize(
705 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
706 layer_tree_host_->SetViewportSize(device_root_bounds);
709 void LayerTreeTest::Timeout() {
710 timed_out_ = true;
711 EndTest();
714 void LayerTreeTest::RealEndTest() {
715 // TODO(mithro): Make this method only end when not inside an impl frame.
716 if (layer_tree_host_ && !timed_out_ &&
717 proxy()->MainFrameWillHappenForTesting()) {
718 main_task_runner_->PostTask(
719 FROM_HERE,
720 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
721 return;
724 base::MessageLoop::current()->Quit();
727 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
728 double animation_duration) {
729 DCHECK(!proxy() || proxy()->IsMainThread());
731 if (layer_to_receive_animation) {
732 AddOpacityTransitionToLayer(
733 layer_to_receive_animation, animation_duration, 0, 0.5, true);
737 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
738 DCHECK(!proxy() || proxy()->IsMainThread());
740 if (layer_tree_host_)
741 layer_tree_host_->SetDeferCommits(defer_commits);
744 void LayerTreeTest::DispatchSetNeedsCommit() {
745 DCHECK(!proxy() || proxy()->IsMainThread());
747 if (layer_tree_host_)
748 layer_tree_host_->SetNeedsCommit();
751 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
752 DCHECK(!proxy() || proxy()->IsMainThread());
754 if (layer_tree_host_)
755 layer_tree_host_->SetNeedsUpdateLayers();
758 void LayerTreeTest::DispatchSetNeedsRedraw() {
759 DCHECK(!proxy() || proxy()->IsMainThread());
761 if (layer_tree_host_)
762 layer_tree_host_->SetNeedsRedraw();
765 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
766 DCHECK(!proxy() || proxy()->IsMainThread());
768 if (layer_tree_host_)
769 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
772 void LayerTreeTest::DispatchSetVisible(bool visible) {
773 DCHECK(!proxy() || proxy()->IsMainThread());
774 if (layer_tree_host_)
775 layer_tree_host_->SetVisible(visible);
778 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
779 DCHECK(!proxy() || proxy()->IsMainThread());
781 if (layer_tree_host_)
782 layer_tree_host_->SetNextCommitForcesRedraw();
785 void LayerTreeTest::DispatchCompositeImmediately() {
786 DCHECK(!proxy() || proxy()->IsMainThread());
787 if (layer_tree_host_)
788 layer_tree_host_->Composite(base::TimeTicks::Now());
791 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
792 if (threaded) {
793 impl_thread_.reset(new base::Thread("Compositor"));
794 ASSERT_TRUE(impl_thread_->Start());
797 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
799 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
800 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
801 task_graph_runner_.reset(new TestTaskGraphRunner);
803 delegating_renderer_ = delegating_renderer;
805 // Spend less time waiting for BeginFrame because the output is
806 // mocked out.
807 settings_.renderer_settings.refresh_rate = 200.0;
808 settings_.background_animation_rate = 200.0;
809 settings_.verify_property_trees = verify_property_trees_;
810 InitializeSettings(&settings_);
811 InitializeLayerSettings(&layer_settings_);
813 main_task_runner_->PostTask(
814 FROM_HERE,
815 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
817 if (timeout_seconds_) {
818 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
819 main_task_runner_->PostDelayedTask(
820 FROM_HERE,
821 timeout_.callback(),
822 base::TimeDelta::FromSeconds(timeout_seconds_));
825 base::MessageLoop::current()->Run();
826 DestroyLayerTreeHost();
828 timeout_.Cancel();
830 ASSERT_FALSE(layer_tree_host_.get());
831 client_ = nullptr;
832 if (timed_out_) {
833 FAIL() << "Test timed out";
834 return;
836 AfterTest();
839 void LayerTreeTest::RequestNewOutputSurface() {
840 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
843 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
844 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
845 DCHECK_EQ(delegating_renderer_,
846 output_surface->capabilities().delegated_rendering);
847 output_surface_ = output_surface.get();
849 if (settings_.use_external_begin_frame_source) {
850 DCHECK(external_begin_frame_source_);
851 DCHECK(external_begin_frame_source_->is_ready());
853 return output_surface.Pass();
856 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
857 if (delegating_renderer_)
858 return FakeOutputSurface::CreateDelegating3d();
859 else
860 return FakeOutputSurface::Create3d();
863 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
864 return static_cast<TestContextProvider*>(output_surface_->context_provider())
865 ->TestContext3d();
868 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
869 const {
870 if (impl->pending_tree())
871 return impl->pending_tree()->source_frame_number();
872 if (impl->active_tree())
873 return impl->active_tree()->source_frame_number();
874 // Source frames start at 0, so this is invalid.
875 return -1;
878 void LayerTreeTest::DestroyLayerTreeHost() {
879 if (layer_tree_host_ && layer_tree_host_->root_layer())
880 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
881 layer_tree_host_ = nullptr;
884 LayerTreeHost* LayerTreeTest::layer_tree_host() {
885 // We check for a null proxy here as we sometimes ask for the layer tree host
886 // when the proxy does not exist, often for checking settings after a test has
887 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
888 // elsewhere in this file for other examples.
889 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
890 return layer_tree_host_.get();
893 } // namespace cc