Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob93ddf120d4cdd968155e3d0f308d4d110c0c267a
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/content_layer.h"
18 #include "cc/layers/layer.h"
19 #include "cc/layers/layer_impl.h"
20 #include "cc/test/animation_test_common.h"
21 #include "cc/test/begin_frame_args_test.h"
22 #include "cc/test/fake_external_begin_frame_source.h"
23 #include "cc/test/fake_layer_tree_host_client.h"
24 #include "cc/test/fake_output_surface.h"
25 #include "cc/test/test_context_provider.h"
26 #include "cc/test/test_gpu_memory_buffer_manager.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "cc/test/test_task_graph_runner.h"
29 #include "cc/test/tiled_layer_test_common.h"
30 #include "cc/trees/layer_tree_host_client.h"
31 #include "cc/trees/layer_tree_host_impl.h"
32 #include "cc/trees/layer_tree_host_single_thread_client.h"
33 #include "cc/trees/layer_tree_impl.h"
34 #include "cc/trees/single_thread_proxy.h"
35 #include "cc/trees/thread_proxy.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "ui/gfx/frame_time.h"
38 #include "ui/gfx/geometry/size_conversions.h"
40 namespace cc {
42 TestHooks::TestHooks() {}
44 TestHooks::~TestHooks() {}
46 DrawResult TestHooks::PrepareToDrawOnThread(
47 LayerTreeHostImpl* host_impl,
48 LayerTreeHostImpl::FrameData* frame_data,
49 DrawResult draw_result) {
50 return draw_result;
53 void TestHooks::CreateResourceAndTileTaskWorkerPool(
54 LayerTreeHostImpl* host_impl,
55 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
56 scoped_ptr<ResourcePool>* resource_pool,
57 scoped_ptr<ResourcePool>* staging_resource_pool) {
58 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
59 tile_task_worker_pool, resource_pool, staging_resource_pool);
62 // Adapts ThreadProxy for test. Injects test hooks for testing.
63 class ThreadProxyForTest : public ThreadProxy {
64 public:
65 static scoped_ptr<Proxy> Create(
66 TestHooks* test_hooks,
67 LayerTreeHost* host,
68 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
69 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
70 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
71 return make_scoped_ptr(new ThreadProxyForTest(
72 test_hooks,
73 host,
74 main_task_runner,
75 impl_task_runner,
76 external_begin_frame_source.Pass()));
79 ~ThreadProxyForTest() override {}
81 private:
82 TestHooks* test_hooks_;
84 void WillBeginImplFrame(const BeginFrameArgs& args) override {
85 ThreadProxy::WillBeginImplFrame(args);
86 test_hooks_->WillBeginImplFrame(args);
89 void ScheduledActionSendBeginMainFrame() override {
90 test_hooks_->ScheduledActionWillSendBeginMainFrame();
91 ThreadProxy::ScheduledActionSendBeginMainFrame();
92 test_hooks_->ScheduledActionSendBeginMainFrame();
95 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
96 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
97 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
98 return result;
101 void ScheduledActionAnimate() override {
102 ThreadProxy::ScheduledActionAnimate();
103 test_hooks_->ScheduledActionAnimate();
106 void ScheduledActionCommit() override {
107 ThreadProxy::ScheduledActionCommit();
108 test_hooks_->ScheduledActionCommit();
111 void ScheduledActionBeginOutputSurfaceCreation() override {
112 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
113 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
116 void ScheduledActionPrepareTiles() override {
117 ThreadProxy::ScheduledActionPrepareTiles();
118 test_hooks_->ScheduledActionPrepareTiles();
121 void ScheduledActionInvalidateOutputSurface() override {
122 ThreadProxy::ScheduledActionInvalidateOutputSurface();
123 test_hooks_->ScheduledActionInvalidateOutputSurface();
126 ThreadProxyForTest(
127 TestHooks* test_hooks,
128 LayerTreeHost* host,
129 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
130 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
131 scoped_ptr<BeginFrameSource> external_begin_frame_source)
132 : ThreadProxy(host, main_task_runner,
133 impl_task_runner,
134 external_begin_frame_source.Pass()),
135 test_hooks_(test_hooks) {}
138 // Adapts ThreadProxy for test. Injects test hooks for testing.
139 class SingleThreadProxyForTest : public SingleThreadProxy {
140 public:
141 static scoped_ptr<Proxy> Create(
142 TestHooks* test_hooks,
143 LayerTreeHost* host,
144 LayerTreeHostSingleThreadClient* client,
145 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
146 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
147 return make_scoped_ptr(new SingleThreadProxyForTest(
148 test_hooks, host, client, main_task_runner,
149 external_begin_frame_source.Pass()));
152 ~SingleThreadProxyForTest() override {}
154 private:
155 TestHooks* test_hooks_;
157 void WillBeginImplFrame(const BeginFrameArgs& args) override {
158 SingleThreadProxy::WillBeginImplFrame(args);
159 test_hooks_->WillBeginImplFrame(args);
162 void ScheduledActionSendBeginMainFrame() override {
163 test_hooks_->ScheduledActionWillSendBeginMainFrame();
164 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
165 test_hooks_->ScheduledActionSendBeginMainFrame();
168 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
169 DrawResult result =
170 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
171 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
172 return result;
175 void ScheduledActionAnimate() override {
176 SingleThreadProxy::ScheduledActionAnimate();
177 test_hooks_->ScheduledActionAnimate();
180 void ScheduledActionCommit() override {
181 SingleThreadProxy::ScheduledActionCommit();
182 test_hooks_->ScheduledActionCommit();
185 void ScheduledActionBeginOutputSurfaceCreation() override {
186 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
187 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
190 void ScheduledActionPrepareTiles() override {
191 SingleThreadProxy::ScheduledActionPrepareTiles();
192 test_hooks_->ScheduledActionPrepareTiles();
195 SingleThreadProxyForTest(
196 TestHooks* test_hooks,
197 LayerTreeHost* host,
198 LayerTreeHostSingleThreadClient* client,
199 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
200 scoped_ptr<BeginFrameSource> external_begin_frame_source)
201 : SingleThreadProxy(host, client, main_task_runner,
202 external_begin_frame_source.Pass()),
203 test_hooks_(test_hooks) {}
206 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
207 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
208 public:
209 static scoped_ptr<LayerTreeHostImplForTesting> Create(
210 TestHooks* test_hooks,
211 const LayerTreeSettings& settings,
212 LayerTreeHostImplClient* host_impl_client,
213 Proxy* proxy,
214 SharedBitmapManager* shared_bitmap_manager,
215 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
216 TaskGraphRunner* task_graph_runner,
217 RenderingStatsInstrumentation* stats_instrumentation) {
218 return make_scoped_ptr(new LayerTreeHostImplForTesting(
219 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
220 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
223 protected:
224 LayerTreeHostImplForTesting(
225 TestHooks* test_hooks,
226 const LayerTreeSettings& settings,
227 LayerTreeHostImplClient* host_impl_client,
228 Proxy* proxy,
229 SharedBitmapManager* shared_bitmap_manager,
230 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
231 TaskGraphRunner* task_graph_runner,
232 RenderingStatsInstrumentation* stats_instrumentation)
233 : LayerTreeHostImpl(settings,
234 host_impl_client,
235 proxy,
236 stats_instrumentation,
237 shared_bitmap_manager,
238 gpu_memory_buffer_manager,
239 task_graph_runner,
241 test_hooks_(test_hooks),
242 block_notify_ready_to_activate_for_testing_(false),
243 notify_ready_to_activate_was_blocked_(false) {}
245 void CreateResourceAndTileTaskWorkerPool(
246 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
247 scoped_ptr<ResourcePool>* resource_pool,
248 scoped_ptr<ResourcePool>* staging_resource_pool) override {
249 test_hooks_->CreateResourceAndTileTaskWorkerPool(
250 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
253 void WillBeginImplFrame(const BeginFrameArgs& args) override {
254 LayerTreeHostImpl::WillBeginImplFrame(args);
255 test_hooks_->WillBeginImplFrameOnThread(this, args);
258 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
259 LayerTreeHostImpl::BeginMainFrameAborted(reason);
260 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
263 void BeginCommit() override {
264 LayerTreeHostImpl::BeginCommit();
265 test_hooks_->BeginCommitOnThread(this);
268 void CommitComplete() override {
269 LayerTreeHostImpl::CommitComplete();
270 test_hooks_->CommitCompleteOnThread(this);
273 DrawResult PrepareToDraw(FrameData* frame) override {
274 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
275 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
278 void DrawLayers(FrameData* frame, base::TimeTicks frame_begin_time) override {
279 LayerTreeHostImpl::DrawLayers(frame, frame_begin_time);
280 test_hooks_->DrawLayersOnThread(this);
283 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
284 bool result = LayerTreeHostImpl::SwapBuffers(frame);
285 test_hooks_->SwapBuffersOnThread(this, result);
286 return result;
289 void DidSwapBuffersComplete() override {
290 LayerTreeHostImpl::DidSwapBuffersComplete();
291 test_hooks_->SwapBuffersCompleteOnThread(this);
294 void ReclaimResources(const CompositorFrameAck* ack) override {
295 LayerTreeHostImpl::ReclaimResources(ack);
298 void NotifyReadyToActivate() override {
299 if (block_notify_ready_to_activate_for_testing_) {
300 notify_ready_to_activate_was_blocked_ = true;
301 } else {
302 LayerTreeHostImpl::NotifyReadyToActivate();
303 test_hooks_->NotifyReadyToActivateOnThread(this);
307 void NotifyReadyToDraw() override {
308 LayerTreeHostImpl::NotifyReadyToDraw();
309 test_hooks_->NotifyReadyToDrawOnThread(this);
312 void BlockNotifyReadyToActivateForTesting(bool block) override {
313 CHECK(settings().impl_side_painting);
314 CHECK(proxy()->ImplThreadTaskRunner())
315 << "Not supported for single-threaded mode.";
316 block_notify_ready_to_activate_for_testing_ = block;
317 if (!block && notify_ready_to_activate_was_blocked_) {
318 NotifyReadyToActivate();
319 notify_ready_to_activate_was_blocked_ = false;
323 void ActivateSyncTree() override {
324 test_hooks_->WillActivateTreeOnThread(this);
325 LayerTreeHostImpl::ActivateSyncTree();
326 DCHECK(!pending_tree());
327 test_hooks_->DidActivateTreeOnThread(this);
330 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
331 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
332 test_hooks_->InitializedRendererOnThread(this, success);
333 return success;
336 void SetVisible(bool visible) override {
337 LayerTreeHostImpl::SetVisible(visible);
338 test_hooks_->DidSetVisibleOnImplTree(this, visible);
341 void AnimateLayers(base::TimeTicks monotonic_time) override {
342 test_hooks_->WillAnimateLayers(this, monotonic_time);
343 LayerTreeHostImpl::AnimateLayers(monotonic_time);
344 test_hooks_->AnimateLayers(this, monotonic_time);
347 void UpdateAnimationState(bool start_ready_animations) override {
348 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
349 bool has_unfinished_animation = false;
350 for (const auto& it :
351 animation_registrar()->active_animation_controllers_for_testing()) {
352 if (it.second->HasActiveAnimation()) {
353 has_unfinished_animation = true;
354 break;
357 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
360 void NotifyTileStateChanged(const Tile* tile) override {
361 LayerTreeHostImpl::NotifyTileStateChanged(tile);
362 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
365 private:
366 TestHooks* test_hooks_;
367 bool block_notify_ready_to_activate_for_testing_;
368 bool notify_ready_to_activate_was_blocked_;
371 // Implementation of LayerTreeHost callback interface.
372 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
373 public LayerTreeHostSingleThreadClient {
374 public:
375 static scoped_ptr<LayerTreeHostClientForTesting> Create(
376 TestHooks* test_hooks) {
377 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
379 ~LayerTreeHostClientForTesting() override {}
381 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
383 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
385 void BeginMainFrame(const BeginFrameArgs& args) override {
386 test_hooks_->BeginMainFrame(args);
389 void Layout() override { test_hooks_->Layout(); }
391 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
392 const gfx::Vector2dF& outer_delta,
393 const gfx::Vector2dF& elastic_overscroll_delta,
394 float page_scale,
395 float top_controls_delta) override {
396 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
397 elastic_overscroll_delta, page_scale,
398 top_controls_delta);
400 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
401 float scale,
402 float top_controls_delta) override {
403 test_hooks_->ApplyViewportDeltas(scroll_delta,
404 scale,
405 top_controls_delta);
408 void RequestNewOutputSurface() override {
409 test_hooks_->RequestNewOutputSurface();
412 void DidInitializeOutputSurface() override {
413 test_hooks_->DidInitializeOutputSurface();
416 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
417 test_hooks_->SendBeginFramesToChildren(args);
420 void DidFailToInitializeOutputSurface() override {
421 test_hooks_->DidFailToInitializeOutputSurface();
422 RequestNewOutputSurface();
425 void WillCommit() override { test_hooks_->WillCommit(); }
427 void DidCommit() override { test_hooks_->DidCommit(); }
429 void DidCommitAndDrawFrame() override {
430 test_hooks_->DidCommitAndDrawFrame();
433 void DidCompleteSwapBuffers() override {
434 test_hooks_->DidCompleteSwapBuffers();
437 void DidPostSwapBuffers() override {}
438 void DidAbortSwapBuffers() override {}
439 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
440 void DidCompletePageScaleAnimation() override {}
441 void BeginMainFrameNotExpectedSoon() override {}
443 private:
444 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
445 : test_hooks_(test_hooks) {}
447 TestHooks* test_hooks_;
450 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
451 class LayerTreeHostForTesting : public LayerTreeHost {
452 public:
453 static scoped_ptr<LayerTreeHostForTesting> Create(
454 TestHooks* test_hooks,
455 LayerTreeHostClientForTesting* client,
456 SharedBitmapManager* shared_bitmap_manager,
457 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
458 TaskGraphRunner* task_graph_runner,
459 const LayerTreeSettings& settings,
460 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
461 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
462 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
463 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
464 new LayerTreeHostForTesting(test_hooks, client, shared_bitmap_manager,
465 gpu_memory_buffer_manager,
466 task_graph_runner, settings));
467 if (impl_task_runner.get()) {
468 layer_tree_host->InitializeForTesting(
469 ThreadProxyForTest::Create(test_hooks,
470 layer_tree_host.get(),
471 main_task_runner,
472 impl_task_runner,
473 external_begin_frame_source.Pass()));
474 } else {
475 layer_tree_host->InitializeForTesting(
476 SingleThreadProxyForTest::Create(
477 test_hooks,
478 layer_tree_host.get(),
479 client,
480 main_task_runner,
481 external_begin_frame_source.Pass()));
483 return layer_tree_host.Pass();
486 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
487 LayerTreeHostImplClient* host_impl_client) override {
488 return LayerTreeHostImplForTesting::Create(
489 test_hooks_, settings(), host_impl_client, proxy(),
490 shared_bitmap_manager_, gpu_memory_buffer_manager_, task_graph_runner_,
491 rendering_stats_instrumentation());
494 void SetNeedsCommit() override {
495 if (!test_started_)
496 return;
497 LayerTreeHost::SetNeedsCommit();
500 void set_test_started(bool started) { test_started_ = started; }
502 private:
503 LayerTreeHostForTesting(
504 TestHooks* test_hooks,
505 LayerTreeHostClient* client,
506 SharedBitmapManager* shared_bitmap_manager,
507 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
508 TaskGraphRunner* task_graph_runner,
509 const LayerTreeSettings& settings)
510 : LayerTreeHost(client, NULL, NULL, NULL, settings),
511 shared_bitmap_manager_(shared_bitmap_manager),
512 gpu_memory_buffer_manager_(gpu_memory_buffer_manager),
513 task_graph_runner_(task_graph_runner),
514 test_hooks_(test_hooks),
515 test_started_(false) {}
517 SharedBitmapManager* shared_bitmap_manager_;
518 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager_;
519 TaskGraphRunner* task_graph_runner_;
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_(true),
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();
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 if (layer_tree_host_ && !timed_out_ &&
716 proxy()->MainFrameWillHappenForTesting()) {
717 main_task_runner_->PostTask(
718 FROM_HERE,
719 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
720 return;
723 base::MessageLoop::current()->Quit();
726 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
727 double animation_duration) {
728 DCHECK(!proxy() || proxy()->IsMainThread());
730 if (layer_to_receive_animation) {
731 AddOpacityTransitionToLayer(
732 layer_to_receive_animation, animation_duration, 0, 0.5, true);
736 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
737 DCHECK(!proxy() || proxy()->IsMainThread());
739 if (layer_tree_host_)
740 layer_tree_host_->SetDeferCommits(defer_commits);
743 void LayerTreeTest::DispatchSetNeedsCommit() {
744 DCHECK(!proxy() || proxy()->IsMainThread());
746 if (layer_tree_host_)
747 layer_tree_host_->SetNeedsCommit();
750 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
751 DCHECK(!proxy() || proxy()->IsMainThread());
753 if (layer_tree_host_)
754 layer_tree_host_->SetNeedsUpdateLayers();
757 void LayerTreeTest::DispatchSetNeedsRedraw() {
758 DCHECK(!proxy() || proxy()->IsMainThread());
760 if (layer_tree_host_)
761 layer_tree_host_->SetNeedsRedraw();
764 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
765 DCHECK(!proxy() || proxy()->IsMainThread());
767 if (layer_tree_host_)
768 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
771 void LayerTreeTest::DispatchSetVisible(bool visible) {
772 DCHECK(!proxy() || proxy()->IsMainThread());
773 if (layer_tree_host_)
774 layer_tree_host_->SetVisible(visible);
777 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
778 DCHECK(!proxy() || proxy()->IsMainThread());
780 if (layer_tree_host_)
781 layer_tree_host_->SetNextCommitForcesRedraw();
784 void LayerTreeTest::DispatchCompositeImmediately() {
785 DCHECK(!proxy() || proxy()->IsMainThread());
786 if (layer_tree_host_)
787 layer_tree_host_->Composite(gfx::FrameTime::Now());
790 void LayerTreeTest::RunTest(bool threaded,
791 bool delegating_renderer,
792 bool impl_side_painting) {
793 if (threaded) {
794 impl_thread_.reset(new base::Thread("Compositor"));
795 ASSERT_TRUE(impl_thread_->Start());
798 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
800 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
801 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
802 task_graph_runner_.reset(new TestTaskGraphRunner);
804 delegating_renderer_ = delegating_renderer;
806 // Spend less time waiting for BeginFrame because the output is
807 // mocked out.
808 settings_.renderer_settings.refresh_rate = 200.0;
809 settings_.background_animation_rate = 200.0;
810 settings_.impl_side_painting = impl_side_painting;
811 settings_.verify_property_trees = verify_property_trees_;
812 InitializeSettings(&settings_);
814 main_task_runner_->PostTask(
815 FROM_HERE,
816 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
818 if (timeout_seconds_) {
819 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
820 main_task_runner_->PostDelayedTask(
821 FROM_HERE,
822 timeout_.callback(),
823 base::TimeDelta::FromSeconds(timeout_seconds_));
826 base::MessageLoop::current()->Run();
827 DestroyLayerTreeHost();
829 timeout_.Cancel();
831 ASSERT_FALSE(layer_tree_host_.get());
832 client_ = nullptr;
833 if (timed_out_) {
834 FAIL() << "Test timed out";
835 return;
837 AfterTest();
840 void LayerTreeTest::RunTestWithImplSidePainting() {
841 RunTest(true, false, true);
844 void LayerTreeTest::RequestNewOutputSurface() {
845 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
848 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
849 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
850 DCHECK_EQ(delegating_renderer_,
851 output_surface->capabilities().delegated_rendering);
852 output_surface_ = output_surface.get();
854 if (settings_.use_external_begin_frame_source) {
855 DCHECK(external_begin_frame_source_);
856 DCHECK(external_begin_frame_source_->is_ready());
858 return output_surface.Pass();
861 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
862 if (delegating_renderer_)
863 return FakeOutputSurface::CreateDelegating3d();
864 else
865 return FakeOutputSurface::Create3d();
868 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
869 return static_cast<TestContextProvider*>(output_surface_->context_provider())
870 ->TestContext3d();
873 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
874 const {
875 if (impl->pending_tree())
876 return impl->pending_tree()->source_frame_number();
877 if (impl->active_tree())
878 return impl->active_tree()->source_frame_number();
879 // Source frames start at 0, so this is invalid.
880 return -1;
883 void LayerTreeTest::DestroyLayerTreeHost() {
884 if (layer_tree_host_ && layer_tree_host_->root_layer())
885 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
886 layer_tree_host_ = nullptr;
889 LayerTreeHost* LayerTreeTest::layer_tree_host() {
890 // We check for a null proxy here as we sometimes ask for the layer tree host
891 // when the proxy does not exist, often for checking settings after a test has
892 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
893 // elsewhere in this file for other examples.
894 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
895 return layer_tree_host_.get();
898 } // namespace cc