Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blobdad00450e7ba27fad94da6b50f13f8c3be1a17d9
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 ScheduledActionSendBeginMainFrame() override {
85 test_hooks_->ScheduledActionWillSendBeginMainFrame();
86 ThreadProxy::ScheduledActionSendBeginMainFrame();
87 test_hooks_->ScheduledActionSendBeginMainFrame();
90 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
91 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
92 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
93 return result;
96 void ScheduledActionAnimate() override {
97 ThreadProxy::ScheduledActionAnimate();
98 test_hooks_->ScheduledActionAnimate();
101 void ScheduledActionCommit() override {
102 ThreadProxy::ScheduledActionCommit();
103 test_hooks_->ScheduledActionCommit();
106 void ScheduledActionBeginOutputSurfaceCreation() override {
107 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
108 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
111 void ScheduledActionPrepareTiles() override {
112 ThreadProxy::ScheduledActionPrepareTiles();
113 test_hooks_->ScheduledActionPrepareTiles();
116 void ScheduledActionInvalidateOutputSurface() override {
117 ThreadProxy::ScheduledActionInvalidateOutputSurface();
118 test_hooks_->ScheduledActionInvalidateOutputSurface();
121 void SendBeginMainFrameNotExpectedSoon() override {
122 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
123 test_hooks_->SendBeginMainFrameNotExpectedSoon();
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 ScheduledActionSendBeginMainFrame() override {
158 test_hooks_->ScheduledActionWillSendBeginMainFrame();
159 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
160 test_hooks_->ScheduledActionSendBeginMainFrame();
163 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
164 DrawResult result =
165 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
166 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
167 return result;
170 void ScheduledActionAnimate() override {
171 SingleThreadProxy::ScheduledActionAnimate();
172 test_hooks_->ScheduledActionAnimate();
175 void ScheduledActionCommit() override {
176 SingleThreadProxy::ScheduledActionCommit();
177 test_hooks_->ScheduledActionCommit();
180 void ScheduledActionBeginOutputSurfaceCreation() override {
181 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
182 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
185 void ScheduledActionPrepareTiles() override {
186 SingleThreadProxy::ScheduledActionPrepareTiles();
187 test_hooks_->ScheduledActionPrepareTiles();
190 void ScheduledActionInvalidateOutputSurface() override {
191 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
192 test_hooks_->ScheduledActionInvalidateOutputSurface();
195 void SendBeginMainFrameNotExpectedSoon() override {
196 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
197 test_hooks_->SendBeginMainFrameNotExpectedSoon();
200 SingleThreadProxyForTest(
201 TestHooks* test_hooks,
202 LayerTreeHost* host,
203 LayerTreeHostSingleThreadClient* client,
204 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
205 scoped_ptr<BeginFrameSource> external_begin_frame_source)
206 : SingleThreadProxy(host, client, main_task_runner,
207 external_begin_frame_source.Pass()),
208 test_hooks_(test_hooks) {}
211 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
212 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
213 public:
214 static scoped_ptr<LayerTreeHostImplForTesting> Create(
215 TestHooks* test_hooks,
216 const LayerTreeSettings& settings,
217 LayerTreeHostImplClient* host_impl_client,
218 Proxy* proxy,
219 SharedBitmapManager* shared_bitmap_manager,
220 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
221 TaskGraphRunner* task_graph_runner,
222 RenderingStatsInstrumentation* stats_instrumentation) {
223 return make_scoped_ptr(new LayerTreeHostImplForTesting(
224 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
225 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
228 protected:
229 LayerTreeHostImplForTesting(
230 TestHooks* test_hooks,
231 const LayerTreeSettings& settings,
232 LayerTreeHostImplClient* host_impl_client,
233 Proxy* proxy,
234 SharedBitmapManager* shared_bitmap_manager,
235 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
236 TaskGraphRunner* task_graph_runner,
237 RenderingStatsInstrumentation* stats_instrumentation)
238 : LayerTreeHostImpl(settings,
239 host_impl_client,
240 proxy,
241 stats_instrumentation,
242 shared_bitmap_manager,
243 gpu_memory_buffer_manager,
244 task_graph_runner,
246 test_hooks_(test_hooks),
247 block_notify_ready_to_activate_for_testing_(false),
248 notify_ready_to_activate_was_blocked_(false) {}
250 void CreateResourceAndTileTaskWorkerPool(
251 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
252 scoped_ptr<ResourcePool>* resource_pool,
253 scoped_ptr<ResourcePool>* staging_resource_pool) override {
254 test_hooks_->CreateResourceAndTileTaskWorkerPool(
255 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
258 void WillBeginImplFrame(const BeginFrameArgs& args) override {
259 LayerTreeHostImpl::WillBeginImplFrame(args);
260 test_hooks_->WillBeginImplFrameOnThread(this, args);
263 void DidFinishImplFrame() override {
264 LayerTreeHostImpl::DidFinishImplFrame();
265 test_hooks_->DidFinishImplFrameOnThread(this);
268 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
269 LayerTreeHostImpl::BeginMainFrameAborted(reason);
270 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
273 void BeginCommit() override {
274 LayerTreeHostImpl::BeginCommit();
275 test_hooks_->BeginCommitOnThread(this);
278 void CommitComplete() override {
279 LayerTreeHostImpl::CommitComplete();
280 test_hooks_->CommitCompleteOnThread(this);
283 DrawResult PrepareToDraw(FrameData* frame) override {
284 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
285 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
288 void DrawLayers(FrameData* frame) override {
289 LayerTreeHostImpl::DrawLayers(frame);
290 test_hooks_->DrawLayersOnThread(this);
293 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
294 bool result = LayerTreeHostImpl::SwapBuffers(frame);
295 test_hooks_->SwapBuffersOnThread(this, result);
296 return result;
299 void DidSwapBuffersComplete() override {
300 LayerTreeHostImpl::DidSwapBuffersComplete();
301 test_hooks_->SwapBuffersCompleteOnThread(this);
304 void ReclaimResources(const CompositorFrameAck* ack) override {
305 LayerTreeHostImpl::ReclaimResources(ack);
308 void NotifyReadyToActivate() override {
309 if (block_notify_ready_to_activate_for_testing_) {
310 notify_ready_to_activate_was_blocked_ = true;
311 } else {
312 LayerTreeHostImpl::NotifyReadyToActivate();
313 test_hooks_->NotifyReadyToActivateOnThread(this);
317 void NotifyReadyToDraw() override {
318 LayerTreeHostImpl::NotifyReadyToDraw();
319 test_hooks_->NotifyReadyToDrawOnThread(this);
322 void BlockNotifyReadyToActivateForTesting(bool block) override {
323 CHECK(settings().impl_side_painting);
324 CHECK(proxy()->ImplThreadTaskRunner())
325 << "Not supported for single-threaded mode.";
326 block_notify_ready_to_activate_for_testing_ = block;
327 if (!block && notify_ready_to_activate_was_blocked_) {
328 NotifyReadyToActivate();
329 notify_ready_to_activate_was_blocked_ = false;
333 void ActivateSyncTree() override {
334 test_hooks_->WillActivateTreeOnThread(this);
335 LayerTreeHostImpl::ActivateSyncTree();
336 DCHECK(!pending_tree());
337 test_hooks_->DidActivateTreeOnThread(this);
340 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
341 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
342 test_hooks_->InitializedRendererOnThread(this, success);
343 return success;
346 void SetVisible(bool visible) override {
347 LayerTreeHostImpl::SetVisible(visible);
348 test_hooks_->DidSetVisibleOnImplTree(this, visible);
351 void AnimateLayers(base::TimeTicks monotonic_time) override {
352 test_hooks_->WillAnimateLayers(this, monotonic_time);
353 LayerTreeHostImpl::AnimateLayers(monotonic_time);
354 test_hooks_->AnimateLayers(this, monotonic_time);
357 void UpdateAnimationState(bool start_ready_animations) override {
358 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
359 bool has_unfinished_animation = false;
360 for (const auto& it :
361 animation_registrar()->active_animation_controllers_for_testing()) {
362 if (it.second->HasActiveAnimation()) {
363 has_unfinished_animation = true;
364 break;
367 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
370 void NotifyTileStateChanged(const Tile* tile) override {
371 LayerTreeHostImpl::NotifyTileStateChanged(tile);
372 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
375 private:
376 TestHooks* test_hooks_;
377 bool block_notify_ready_to_activate_for_testing_;
378 bool notify_ready_to_activate_was_blocked_;
381 // Implementation of LayerTreeHost callback interface.
382 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
383 public LayerTreeHostSingleThreadClient {
384 public:
385 static scoped_ptr<LayerTreeHostClientForTesting> Create(
386 TestHooks* test_hooks) {
387 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
389 ~LayerTreeHostClientForTesting() override {}
391 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
393 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
395 void BeginMainFrame(const BeginFrameArgs& args) override {
396 test_hooks_->BeginMainFrame(args);
399 void Layout() override { test_hooks_->Layout(); }
401 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
402 const gfx::Vector2dF& outer_delta,
403 const gfx::Vector2dF& elastic_overscroll_delta,
404 float page_scale,
405 float top_controls_delta) override {
406 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
407 elastic_overscroll_delta, page_scale,
408 top_controls_delta);
410 void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
411 float scale,
412 float top_controls_delta) override {
413 test_hooks_->ApplyViewportDeltas(scroll_delta,
414 scale,
415 top_controls_delta);
418 void RequestNewOutputSurface() override {
419 test_hooks_->RequestNewOutputSurface();
422 void DidInitializeOutputSurface() override {
423 test_hooks_->DidInitializeOutputSurface();
426 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
427 test_hooks_->SendBeginFramesToChildren(args);
430 void DidFailToInitializeOutputSurface() override {
431 test_hooks_->DidFailToInitializeOutputSurface();
432 RequestNewOutputSurface();
435 void WillCommit() override { test_hooks_->WillCommit(); }
437 void DidCommit() override { test_hooks_->DidCommit(); }
439 void DidCommitAndDrawFrame() override {
440 test_hooks_->DidCommitAndDrawFrame();
443 void DidCompleteSwapBuffers() override {
444 test_hooks_->DidCompleteSwapBuffers();
447 void DidPostSwapBuffers() override {}
448 void DidAbortSwapBuffers() override {}
449 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
450 void DidCompletePageScaleAnimation() override {}
451 void BeginMainFrameNotExpectedSoon() override {}
453 private:
454 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
455 : test_hooks_(test_hooks) {}
457 TestHooks* test_hooks_;
460 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
461 class LayerTreeHostForTesting : public LayerTreeHost {
462 public:
463 static scoped_ptr<LayerTreeHostForTesting> Create(
464 TestHooks* test_hooks,
465 LayerTreeHostClientForTesting* client,
466 SharedBitmapManager* shared_bitmap_manager,
467 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
468 TaskGraphRunner* task_graph_runner,
469 const LayerTreeSettings& settings,
470 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
471 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
472 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
473 LayerTreeHost::InitParams params;
474 params.client = client;
475 params.shared_bitmap_manager = shared_bitmap_manager;
476 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
477 params.task_graph_runner = task_graph_runner;
478 params.settings = &settings;
479 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
480 new LayerTreeHostForTesting(test_hooks, &params));
481 if (impl_task_runner.get()) {
482 layer_tree_host->InitializeForTesting(
483 ThreadProxyForTest::Create(test_hooks,
484 layer_tree_host.get(),
485 main_task_runner,
486 impl_task_runner,
487 external_begin_frame_source.Pass()));
488 } else {
489 layer_tree_host->InitializeForTesting(
490 SingleThreadProxyForTest::Create(
491 test_hooks,
492 layer_tree_host.get(),
493 client,
494 main_task_runner,
495 external_begin_frame_source.Pass()));
497 return layer_tree_host.Pass();
500 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
501 LayerTreeHostImplClient* host_impl_client) override {
502 return LayerTreeHostImplForTesting::Create(
503 test_hooks_, settings(), host_impl_client, proxy(),
504 shared_bitmap_manager(), gpu_memory_buffer_manager(),
505 task_graph_runner(), rendering_stats_instrumentation());
508 void SetNeedsCommit() override {
509 if (!test_started_)
510 return;
511 LayerTreeHost::SetNeedsCommit();
514 void set_test_started(bool started) { test_started_ = started; }
516 private:
517 LayerTreeHostForTesting(TestHooks* test_hooks,
518 LayerTreeHost::InitParams* params)
519 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
521 TestHooks* test_hooks_;
522 bool test_started_;
525 LayerTreeTest::LayerTreeTest()
526 : output_surface_(nullptr),
527 external_begin_frame_source_(nullptr),
528 beginning_(false),
529 end_when_begin_returns_(false),
530 timed_out_(false),
531 scheduled_(false),
532 started_(false),
533 ended_(false),
534 delegating_renderer_(false),
535 verify_property_trees_(true),
536 timeout_seconds_(0),
537 weak_factory_(this) {
538 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
540 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
541 // specified (for running in a debugger).
542 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
543 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
544 timeout_seconds_ = 5;
547 LayerTreeTest::~LayerTreeTest() {}
549 void LayerTreeTest::EndTest() {
550 if (ended_)
551 return;
552 ended_ = true;
554 // For the case where we EndTest during BeginTest(), set a flag to indicate
555 // that the test should end the second BeginTest regains control.
556 if (beginning_) {
557 end_when_begin_returns_ = true;
558 } else {
559 main_task_runner_->PostTask(
560 FROM_HERE,
561 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
565 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
566 main_task_runner_->PostDelayedTask(
567 FROM_HERE,
568 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
569 base::TimeDelta::FromMilliseconds(delay_milliseconds));
572 void LayerTreeTest::PostAddAnimationToMainThread(
573 Layer* layer_to_receive_animation) {
574 main_task_runner_->PostTask(
575 FROM_HERE,
576 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
577 base::Unretained(layer_to_receive_animation), 0.000004));
580 void LayerTreeTest::PostAddInstantAnimationToMainThread(
581 Layer* layer_to_receive_animation) {
582 main_task_runner_->PostTask(
583 FROM_HERE,
584 base::Bind(&LayerTreeTest::DispatchAddAnimation,
585 main_thread_weak_ptr_,
586 base::Unretained(layer_to_receive_animation),
587 0.0));
590 void LayerTreeTest::PostAddLongAnimationToMainThread(
591 Layer* layer_to_receive_animation) {
592 main_task_runner_->PostTask(
593 FROM_HERE,
594 base::Bind(&LayerTreeTest::DispatchAddAnimation,
595 main_thread_weak_ptr_,
596 base::Unretained(layer_to_receive_animation),
597 1.0));
600 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
601 main_task_runner_->PostTask(
602 FROM_HERE,
603 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
604 main_thread_weak_ptr_, defer_commits));
607 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
608 main_task_runner_->PostTask(FROM_HERE,
609 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
610 main_thread_weak_ptr_));
613 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
614 main_task_runner_->PostTask(
615 FROM_HERE,
616 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
617 main_thread_weak_ptr_));
620 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
621 main_task_runner_->PostTask(FROM_HERE,
622 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
623 main_thread_weak_ptr_));
626 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
627 const gfx::Rect& damage_rect) {
628 main_task_runner_->PostTask(
629 FROM_HERE,
630 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
631 main_thread_weak_ptr_,
632 damage_rect));
635 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
636 main_task_runner_->PostTask(
637 FROM_HERE,
638 base::Bind(
639 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
642 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
643 main_task_runner_->PostTask(
644 FROM_HERE,
645 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
646 main_thread_weak_ptr_));
649 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
650 main_task_runner_->PostTask(
651 FROM_HERE,
652 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
653 main_thread_weak_ptr_));
656 void LayerTreeTest::WillBeginTest() {
657 layer_tree_host_->SetLayerTreeHostClientReady();
660 void LayerTreeTest::DoBeginTest() {
661 client_ = LayerTreeHostClientForTesting::Create(this);
663 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
664 if (settings_.use_external_begin_frame_source) {
665 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
666 settings_.renderer_settings.refresh_rate));
667 external_begin_frame_source_ = external_begin_frame_source.get();
670 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
671 layer_tree_host_ = LayerTreeHostForTesting::Create(
672 this, client_.get(), shared_bitmap_manager_.get(),
673 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
674 base::ThreadTaskRunnerHandle::Get(),
675 impl_thread_ ? impl_thread_->task_runner() : NULL,
676 external_begin_frame_source.Pass());
677 ASSERT_TRUE(layer_tree_host_);
679 started_ = true;
680 beginning_ = true;
681 SetupTree();
682 WillBeginTest();
683 BeginTest();
684 beginning_ = false;
685 if (end_when_begin_returns_)
686 RealEndTest();
688 // Allow commits to happen once BeginTest() has had a chance to post tasks
689 // so that those tasks will happen before the first commit.
690 if (layer_tree_host_) {
691 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
692 ->set_test_started(true);
696 void LayerTreeTest::SetupTree() {
697 if (!layer_tree_host_->root_layer()) {
698 scoped_refptr<Layer> root_layer = Layer::Create();
699 root_layer->SetBounds(gfx::Size(1, 1));
700 root_layer->SetIsDrawable(true);
701 layer_tree_host_->SetRootLayer(root_layer);
704 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
705 gfx::Size device_root_bounds = gfx::ToCeiledSize(
706 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
707 layer_tree_host_->SetViewportSize(device_root_bounds);
710 void LayerTreeTest::Timeout() {
711 timed_out_ = true;
712 EndTest();
715 void LayerTreeTest::RealEndTest() {
716 // TODO(mithro): Make this method only end when not inside an impl frame.
717 if (layer_tree_host_ && !timed_out_ &&
718 proxy()->MainFrameWillHappenForTesting()) {
719 main_task_runner_->PostTask(
720 FROM_HERE,
721 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
722 return;
725 base::MessageLoop::current()->Quit();
728 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
729 double animation_duration) {
730 DCHECK(!proxy() || proxy()->IsMainThread());
732 if (layer_to_receive_animation) {
733 AddOpacityTransitionToLayer(
734 layer_to_receive_animation, animation_duration, 0, 0.5, true);
738 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
739 DCHECK(!proxy() || proxy()->IsMainThread());
741 if (layer_tree_host_)
742 layer_tree_host_->SetDeferCommits(defer_commits);
745 void LayerTreeTest::DispatchSetNeedsCommit() {
746 DCHECK(!proxy() || proxy()->IsMainThread());
748 if (layer_tree_host_)
749 layer_tree_host_->SetNeedsCommit();
752 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
753 DCHECK(!proxy() || proxy()->IsMainThread());
755 if (layer_tree_host_)
756 layer_tree_host_->SetNeedsUpdateLayers();
759 void LayerTreeTest::DispatchSetNeedsRedraw() {
760 DCHECK(!proxy() || proxy()->IsMainThread());
762 if (layer_tree_host_)
763 layer_tree_host_->SetNeedsRedraw();
766 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
767 DCHECK(!proxy() || proxy()->IsMainThread());
769 if (layer_tree_host_)
770 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
773 void LayerTreeTest::DispatchSetVisible(bool visible) {
774 DCHECK(!proxy() || proxy()->IsMainThread());
775 if (layer_tree_host_)
776 layer_tree_host_->SetVisible(visible);
779 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
780 DCHECK(!proxy() || proxy()->IsMainThread());
782 if (layer_tree_host_)
783 layer_tree_host_->SetNextCommitForcesRedraw();
786 void LayerTreeTest::DispatchCompositeImmediately() {
787 DCHECK(!proxy() || proxy()->IsMainThread());
788 if (layer_tree_host_)
789 layer_tree_host_->Composite(gfx::FrameTime::Now());
792 void LayerTreeTest::RunTest(bool threaded,
793 bool delegating_renderer,
794 bool impl_side_painting) {
795 if (threaded) {
796 impl_thread_.reset(new base::Thread("Compositor"));
797 ASSERT_TRUE(impl_thread_->Start());
800 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
802 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
803 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
804 task_graph_runner_.reset(new TestTaskGraphRunner);
806 delegating_renderer_ = delegating_renderer;
808 // Spend less time waiting for BeginFrame because the output is
809 // mocked out.
810 settings_.renderer_settings.refresh_rate = 200.0;
811 settings_.background_animation_rate = 200.0;
812 settings_.impl_side_painting = impl_side_painting;
813 settings_.verify_property_trees = verify_property_trees_;
814 InitializeSettings(&settings_);
816 main_task_runner_->PostTask(
817 FROM_HERE,
818 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
820 if (timeout_seconds_) {
821 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
822 main_task_runner_->PostDelayedTask(
823 FROM_HERE,
824 timeout_.callback(),
825 base::TimeDelta::FromSeconds(timeout_seconds_));
828 base::MessageLoop::current()->Run();
829 DestroyLayerTreeHost();
831 timeout_.Cancel();
833 ASSERT_FALSE(layer_tree_host_.get());
834 client_ = nullptr;
835 if (timed_out_) {
836 FAIL() << "Test timed out";
837 return;
839 AfterTest();
842 void LayerTreeTest::RunTestWithImplSidePainting() {
843 RunTest(true, false, true);
846 void LayerTreeTest::RequestNewOutputSurface() {
847 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
850 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
851 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
852 DCHECK_EQ(delegating_renderer_,
853 output_surface->capabilities().delegated_rendering);
854 output_surface_ = output_surface.get();
856 if (settings_.use_external_begin_frame_source) {
857 DCHECK(external_begin_frame_source_);
858 DCHECK(external_begin_frame_source_->is_ready());
860 return output_surface.Pass();
863 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
864 if (delegating_renderer_)
865 return FakeOutputSurface::CreateDelegating3d();
866 else
867 return FakeOutputSurface::Create3d();
870 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
871 return static_cast<TestContextProvider*>(output_surface_->context_provider())
872 ->TestContext3d();
875 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
876 const {
877 if (impl->pending_tree())
878 return impl->pending_tree()->source_frame_number();
879 if (impl->active_tree())
880 return impl->active_tree()->source_frame_number();
881 // Source frames start at 0, so this is invalid.
882 return -1;
885 void LayerTreeTest::DestroyLayerTreeHost() {
886 if (layer_tree_host_ && layer_tree_host_->root_layer())
887 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
888 layer_tree_host_ = nullptr;
891 LayerTreeHost* LayerTreeTest::layer_tree_host() {
892 // We check for a null proxy here as we sometimes ask for the layer tree host
893 // when the proxy does not exist, often for checking settings after a test has
894 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
895 // elsewhere in this file for other examples.
896 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
897 return layer_tree_host_.get();
900 } // namespace cc