cc: Use worker context for one-copy tile initialization.
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blobe81f1e3970c93f785dff69fed8bab8ee1180f630
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_host.h"
13 #include "cc/animation/animation_registrar.h"
14 #include "cc/animation/layer_animation_controller.h"
15 #include "cc/animation/timing_function.h"
16 #include "cc/base/switches.h"
17 #include "cc/input/input_handler.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/trees/layer_tree_host_client.h"
30 #include "cc/trees/layer_tree_host_impl.h"
31 #include "cc/trees/layer_tree_host_single_thread_client.h"
32 #include "cc/trees/layer_tree_impl.h"
33 #include "cc/trees/single_thread_proxy.h"
34 #include "cc/trees/thread_proxy.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "ui/gfx/geometry/size_conversions.h"
38 namespace cc {
40 void CreateVirtualViewportLayers(Layer* root_layer,
41 scoped_refptr<Layer> outer_scroll_layer,
42 const gfx::Size& inner_bounds,
43 const gfx::Size& outer_bounds,
44 LayerTreeHost* host,
45 const LayerSettings& layer_settings) {
46 scoped_refptr<Layer> inner_viewport_container_layer =
47 Layer::Create(layer_settings);
48 scoped_refptr<Layer> inner_viewport_scroll_layer =
49 Layer::Create(layer_settings);
50 scoped_refptr<Layer> outer_viewport_container_layer =
51 Layer::Create(layer_settings);
52 scoped_refptr<Layer> page_scale_layer = Layer::Create(layer_settings);
54 root_layer->AddChild(inner_viewport_container_layer);
55 inner_viewport_container_layer->AddChild(page_scale_layer);
56 page_scale_layer->AddChild(inner_viewport_scroll_layer);
57 inner_viewport_scroll_layer->AddChild(outer_viewport_container_layer);
58 outer_viewport_container_layer->AddChild(outer_scroll_layer);
60 inner_viewport_scroll_layer->SetScrollClipLayerId(
61 inner_viewport_container_layer->id());
62 outer_scroll_layer->SetScrollClipLayerId(
63 outer_viewport_container_layer->id());
65 inner_viewport_container_layer->SetBounds(inner_bounds);
66 inner_viewport_scroll_layer->SetBounds(outer_bounds);
67 outer_viewport_container_layer->SetBounds(outer_bounds);
69 inner_viewport_scroll_layer->SetIsContainerForFixedPositionLayers(true);
70 outer_scroll_layer->SetIsContainerForFixedPositionLayers(true);
71 host->RegisterViewportLayers(NULL,
72 root_layer,
73 inner_viewport_scroll_layer,
74 outer_scroll_layer);
77 void CreateVirtualViewportLayers(Layer* root_layer,
78 const gfx::Size& inner_bounds,
79 const gfx::Size& outer_bounds,
80 const gfx::Size& scroll_bounds,
81 LayerTreeHost* host,
82 const LayerSettings& layer_settings) {
83 scoped_refptr<Layer> outer_viewport_scroll_layer =
84 Layer::Create(layer_settings);
86 outer_viewport_scroll_layer->SetBounds(scroll_bounds);
87 outer_viewport_scroll_layer->SetIsDrawable(true);
88 CreateVirtualViewportLayers(root_layer,
89 outer_viewport_scroll_layer,
90 inner_bounds,
91 outer_bounds,
92 host,
93 layer_settings);
96 TestHooks::TestHooks() {}
98 TestHooks::~TestHooks() {}
100 DrawResult TestHooks::PrepareToDrawOnThread(
101 LayerTreeHostImpl* host_impl,
102 LayerTreeHostImpl::FrameData* frame_data,
103 DrawResult draw_result) {
104 return draw_result;
107 void TestHooks::CreateResourceAndTileTaskWorkerPool(
108 LayerTreeHostImpl* host_impl,
109 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
110 scoped_ptr<ResourcePool>* resource_pool) {
111 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
112 tile_task_worker_pool, resource_pool);
115 // Adapts ThreadProxy for test. Injects test hooks for testing.
116 class ThreadProxyForTest : public ThreadProxy {
117 public:
118 static scoped_ptr<Proxy> Create(
119 TestHooks* test_hooks,
120 LayerTreeHost* host,
121 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
122 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
123 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
124 return make_scoped_ptr(new ThreadProxyForTest(
125 test_hooks,
126 host,
127 main_task_runner,
128 impl_task_runner,
129 external_begin_frame_source.Pass()));
132 ~ThreadProxyForTest() override {}
134 private:
135 TestHooks* test_hooks_;
137 void ScheduledActionSendBeginMainFrame() override {
138 test_hooks_->ScheduledActionWillSendBeginMainFrame();
139 ThreadProxy::ScheduledActionSendBeginMainFrame();
140 test_hooks_->ScheduledActionSendBeginMainFrame();
143 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
144 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
145 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
146 return result;
149 void ScheduledActionAnimate() override {
150 ThreadProxy::ScheduledActionAnimate();
151 test_hooks_->ScheduledActionAnimate();
154 void ScheduledActionCommit() override {
155 ThreadProxy::ScheduledActionCommit();
156 test_hooks_->ScheduledActionCommit();
159 void ScheduledActionBeginOutputSurfaceCreation() override {
160 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
161 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
164 void ScheduledActionPrepareTiles() override {
165 ThreadProxy::ScheduledActionPrepareTiles();
166 test_hooks_->ScheduledActionPrepareTiles();
169 void ScheduledActionInvalidateOutputSurface() override {
170 ThreadProxy::ScheduledActionInvalidateOutputSurface();
171 test_hooks_->ScheduledActionInvalidateOutputSurface();
174 void SendBeginMainFrameNotExpectedSoon() override {
175 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
176 test_hooks_->SendBeginMainFrameNotExpectedSoon();
179 ThreadProxyForTest(
180 TestHooks* test_hooks,
181 LayerTreeHost* host,
182 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
183 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
184 scoped_ptr<BeginFrameSource> external_begin_frame_source)
185 : ThreadProxy(host, main_task_runner,
186 impl_task_runner,
187 external_begin_frame_source.Pass()),
188 test_hooks_(test_hooks) {}
191 // Adapts ThreadProxy for test. Injects test hooks for testing.
192 class SingleThreadProxyForTest : public SingleThreadProxy {
193 public:
194 static scoped_ptr<Proxy> Create(
195 TestHooks* test_hooks,
196 LayerTreeHost* host,
197 LayerTreeHostSingleThreadClient* client,
198 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
199 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
200 return make_scoped_ptr(new SingleThreadProxyForTest(
201 test_hooks, host, client, main_task_runner,
202 external_begin_frame_source.Pass()));
205 ~SingleThreadProxyForTest() override {}
207 private:
208 TestHooks* test_hooks_;
210 void ScheduledActionSendBeginMainFrame() override {
211 test_hooks_->ScheduledActionWillSendBeginMainFrame();
212 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
213 test_hooks_->ScheduledActionSendBeginMainFrame();
216 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
217 DrawResult result =
218 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
219 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
220 return result;
223 void ScheduledActionAnimate() override {
224 SingleThreadProxy::ScheduledActionAnimate();
225 test_hooks_->ScheduledActionAnimate();
228 void ScheduledActionCommit() override {
229 SingleThreadProxy::ScheduledActionCommit();
230 test_hooks_->ScheduledActionCommit();
233 void ScheduledActionBeginOutputSurfaceCreation() override {
234 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
235 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
238 void ScheduledActionPrepareTiles() override {
239 SingleThreadProxy::ScheduledActionPrepareTiles();
240 test_hooks_->ScheduledActionPrepareTiles();
243 void ScheduledActionInvalidateOutputSurface() override {
244 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
245 test_hooks_->ScheduledActionInvalidateOutputSurface();
248 void SendBeginMainFrameNotExpectedSoon() override {
249 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
250 test_hooks_->SendBeginMainFrameNotExpectedSoon();
253 SingleThreadProxyForTest(
254 TestHooks* test_hooks,
255 LayerTreeHost* host,
256 LayerTreeHostSingleThreadClient* client,
257 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
258 scoped_ptr<BeginFrameSource> external_begin_frame_source)
259 : SingleThreadProxy(host, client, main_task_runner,
260 external_begin_frame_source.Pass()),
261 test_hooks_(test_hooks) {}
264 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
265 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
266 public:
267 static scoped_ptr<LayerTreeHostImplForTesting> Create(
268 TestHooks* test_hooks,
269 const LayerTreeSettings& settings,
270 LayerTreeHostImplClient* host_impl_client,
271 Proxy* proxy,
272 SharedBitmapManager* shared_bitmap_manager,
273 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
274 TaskGraphRunner* task_graph_runner,
275 RenderingStatsInstrumentation* stats_instrumentation) {
276 return make_scoped_ptr(new LayerTreeHostImplForTesting(
277 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
278 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
281 protected:
282 LayerTreeHostImplForTesting(
283 TestHooks* test_hooks,
284 const LayerTreeSettings& settings,
285 LayerTreeHostImplClient* host_impl_client,
286 Proxy* proxy,
287 SharedBitmapManager* shared_bitmap_manager,
288 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
289 TaskGraphRunner* task_graph_runner,
290 RenderingStatsInstrumentation* stats_instrumentation)
291 : LayerTreeHostImpl(settings,
292 host_impl_client,
293 proxy,
294 stats_instrumentation,
295 shared_bitmap_manager,
296 gpu_memory_buffer_manager,
297 task_graph_runner,
299 test_hooks_(test_hooks),
300 block_notify_ready_to_activate_for_testing_(false),
301 notify_ready_to_activate_was_blocked_(false) {}
303 void CreateResourceAndTileTaskWorkerPool(
304 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
305 scoped_ptr<ResourcePool>* resource_pool) override {
306 test_hooks_->CreateResourceAndTileTaskWorkerPool(
307 this, tile_task_worker_pool, resource_pool);
310 void WillBeginImplFrame(const BeginFrameArgs& args) override {
311 LayerTreeHostImpl::WillBeginImplFrame(args);
312 test_hooks_->WillBeginImplFrameOnThread(this, args);
315 void DidFinishImplFrame() override {
316 LayerTreeHostImpl::DidFinishImplFrame();
317 test_hooks_->DidFinishImplFrameOnThread(this);
320 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
321 LayerTreeHostImpl::BeginMainFrameAborted(reason);
322 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
325 void BeginCommit() override {
326 LayerTreeHostImpl::BeginCommit();
327 test_hooks_->BeginCommitOnThread(this);
330 void CommitComplete() override {
331 LayerTreeHostImpl::CommitComplete();
332 test_hooks_->CommitCompleteOnThread(this);
335 DrawResult PrepareToDraw(FrameData* frame) override {
336 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
337 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
340 void DrawLayers(FrameData* frame) override {
341 LayerTreeHostImpl::DrawLayers(frame);
342 test_hooks_->DrawLayersOnThread(this);
345 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
346 bool result = LayerTreeHostImpl::SwapBuffers(frame);
347 test_hooks_->SwapBuffersOnThread(this, result);
348 return result;
351 void DidSwapBuffersComplete() override {
352 LayerTreeHostImpl::DidSwapBuffersComplete();
353 test_hooks_->SwapBuffersCompleteOnThread(this);
356 void ReclaimResources(const CompositorFrameAck* ack) override {
357 LayerTreeHostImpl::ReclaimResources(ack);
360 void NotifyReadyToActivate() override {
361 if (block_notify_ready_to_activate_for_testing_) {
362 notify_ready_to_activate_was_blocked_ = true;
363 } else {
364 LayerTreeHostImpl::NotifyReadyToActivate();
365 test_hooks_->NotifyReadyToActivateOnThread(this);
369 void NotifyReadyToDraw() override {
370 LayerTreeHostImpl::NotifyReadyToDraw();
371 test_hooks_->NotifyReadyToDrawOnThread(this);
374 void NotifyAllTileTasksCompleted() override {
375 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
376 test_hooks_->NotifyAllTileTasksCompleted(this);
379 void BlockNotifyReadyToActivateForTesting(bool block) override {
380 CHECK(proxy()->ImplThreadTaskRunner())
381 << "Not supported for single-threaded mode.";
382 block_notify_ready_to_activate_for_testing_ = block;
383 if (!block && notify_ready_to_activate_was_blocked_) {
384 NotifyReadyToActivate();
385 notify_ready_to_activate_was_blocked_ = false;
389 void ActivateSyncTree() override {
390 test_hooks_->WillActivateTreeOnThread(this);
391 LayerTreeHostImpl::ActivateSyncTree();
392 DCHECK(!pending_tree());
393 test_hooks_->DidActivateTreeOnThread(this);
396 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
397 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
398 test_hooks_->InitializedRendererOnThread(this, success);
399 return success;
402 void SetVisible(bool visible) override {
403 LayerTreeHostImpl::SetVisible(visible);
404 test_hooks_->DidSetVisibleOnImplTree(this, visible);
407 void AnimateLayers(base::TimeTicks monotonic_time) override {
408 test_hooks_->WillAnimateLayers(this, monotonic_time);
409 LayerTreeHostImpl::AnimateLayers(monotonic_time);
410 test_hooks_->AnimateLayers(this, monotonic_time);
413 void UpdateAnimationState(bool start_ready_animations) override {
414 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
415 bool has_unfinished_animation = false;
416 AnimationRegistrar* registrar =
417 animation_registrar() ? animation_registrar()
418 : animation_host()->animation_registrar();
419 for (const auto& it :
420 registrar->active_animation_controllers_for_testing()) {
421 if (it.second->HasActiveAnimation()) {
422 has_unfinished_animation = true;
423 break;
426 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
429 void NotifyTileStateChanged(const Tile* tile) override {
430 LayerTreeHostImpl::NotifyTileStateChanged(tile);
431 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
434 private:
435 TestHooks* test_hooks_;
436 bool block_notify_ready_to_activate_for_testing_;
437 bool notify_ready_to_activate_was_blocked_;
440 // Implementation of LayerTreeHost callback interface.
441 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
442 public LayerTreeHostSingleThreadClient {
443 public:
444 static scoped_ptr<LayerTreeHostClientForTesting> Create(
445 TestHooks* test_hooks) {
446 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
448 ~LayerTreeHostClientForTesting() override {}
450 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
452 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
454 void BeginMainFrame(const BeginFrameArgs& args) override {
455 test_hooks_->BeginMainFrame(args);
458 void Layout() override { test_hooks_->Layout(); }
460 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
461 const gfx::Vector2dF& outer_delta,
462 const gfx::Vector2dF& elastic_overscroll_delta,
463 float page_scale,
464 float top_controls_delta) override {
465 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
466 elastic_overscroll_delta, page_scale,
467 top_controls_delta);
470 void RequestNewOutputSurface() override {
471 test_hooks_->RequestNewOutputSurface();
474 void DidInitializeOutputSurface() override {
475 test_hooks_->DidInitializeOutputSurface();
478 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
479 test_hooks_->SendBeginFramesToChildren(args);
482 void DidFailToInitializeOutputSurface() override {
483 test_hooks_->DidFailToInitializeOutputSurface();
484 RequestNewOutputSurface();
487 void WillCommit() override { test_hooks_->WillCommit(); }
489 void DidCommit() override { test_hooks_->DidCommit(); }
491 void DidCommitAndDrawFrame() override {
492 test_hooks_->DidCommitAndDrawFrame();
495 void DidCompleteSwapBuffers() override {
496 test_hooks_->DidCompleteSwapBuffers();
499 void DidPostSwapBuffers() override {}
500 void DidAbortSwapBuffers() override {}
501 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
502 void DidCompletePageScaleAnimation() override {}
503 void BeginMainFrameNotExpectedSoon() override {}
505 void RecordFrameTimingEvents(
506 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
507 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
508 override {}
510 private:
511 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
512 : test_hooks_(test_hooks) {}
514 TestHooks* test_hooks_;
517 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
518 class LayerTreeHostForTesting : public LayerTreeHost {
519 public:
520 static scoped_ptr<LayerTreeHostForTesting> Create(
521 TestHooks* test_hooks,
522 LayerTreeHostClientForTesting* client,
523 SharedBitmapManager* shared_bitmap_manager,
524 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
525 TaskGraphRunner* task_graph_runner,
526 const LayerTreeSettings& settings,
527 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
528 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
529 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
530 LayerTreeHost::InitParams params;
531 params.client = client;
532 params.shared_bitmap_manager = shared_bitmap_manager;
533 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
534 params.task_graph_runner = task_graph_runner;
535 params.settings = &settings;
536 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
537 new LayerTreeHostForTesting(test_hooks, &params));
538 if (impl_task_runner.get()) {
539 layer_tree_host->InitializeForTesting(
540 ThreadProxyForTest::Create(test_hooks,
541 layer_tree_host.get(),
542 main_task_runner,
543 impl_task_runner,
544 external_begin_frame_source.Pass()));
545 } else {
546 layer_tree_host->InitializeForTesting(
547 SingleThreadProxyForTest::Create(
548 test_hooks,
549 layer_tree_host.get(),
550 client,
551 main_task_runner,
552 external_begin_frame_source.Pass()));
554 return layer_tree_host.Pass();
557 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
558 LayerTreeHostImplClient* host_impl_client) override {
559 return LayerTreeHostImplForTesting::Create(
560 test_hooks_, settings(), host_impl_client, proxy(),
561 shared_bitmap_manager(), gpu_memory_buffer_manager(),
562 task_graph_runner(), rendering_stats_instrumentation());
565 void SetNeedsCommit() override {
566 if (!test_started_)
567 return;
568 LayerTreeHost::SetNeedsCommit();
571 void set_test_started(bool started) { test_started_ = started; }
573 private:
574 LayerTreeHostForTesting(TestHooks* test_hooks,
575 LayerTreeHost::InitParams* params)
576 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
578 TestHooks* test_hooks_;
579 bool test_started_;
582 LayerTreeTest::LayerTreeTest()
583 : output_surface_(nullptr),
584 external_begin_frame_source_(nullptr),
585 beginning_(false),
586 end_when_begin_returns_(false),
587 timed_out_(false),
588 scheduled_(false),
589 started_(false),
590 ended_(false),
591 delegating_renderer_(false),
592 verify_property_trees_(true),
593 timeout_seconds_(0),
594 weak_factory_(this) {
595 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
597 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
598 // specified (for running in a debugger).
599 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
600 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
601 timeout_seconds_ = 5;
604 LayerTreeTest::~LayerTreeTest() {}
606 void LayerTreeTest::EndTest() {
607 if (ended_)
608 return;
609 ended_ = true;
611 // For the case where we EndTest during BeginTest(), set a flag to indicate
612 // that the test should end the second BeginTest regains control.
613 if (beginning_) {
614 end_when_begin_returns_ = true;
615 } else {
616 main_task_runner_->PostTask(
617 FROM_HERE,
618 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
622 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
623 main_task_runner_->PostDelayedTask(
624 FROM_HERE,
625 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
626 base::TimeDelta::FromMilliseconds(delay_milliseconds));
629 void LayerTreeTest::PostAddAnimationToMainThread(
630 Layer* layer_to_receive_animation) {
631 main_task_runner_->PostTask(
632 FROM_HERE,
633 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
634 base::Unretained(layer_to_receive_animation), 0.000004));
637 void LayerTreeTest::PostAddInstantAnimationToMainThread(
638 Layer* layer_to_receive_animation) {
639 main_task_runner_->PostTask(
640 FROM_HERE,
641 base::Bind(&LayerTreeTest::DispatchAddAnimation,
642 main_thread_weak_ptr_,
643 base::Unretained(layer_to_receive_animation),
644 0.0));
647 void LayerTreeTest::PostAddLongAnimationToMainThread(
648 Layer* layer_to_receive_animation) {
649 main_task_runner_->PostTask(
650 FROM_HERE,
651 base::Bind(&LayerTreeTest::DispatchAddAnimation,
652 main_thread_weak_ptr_,
653 base::Unretained(layer_to_receive_animation),
654 1.0));
657 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
658 AnimationPlayer* player_to_receive_animation) {
659 main_task_runner_->PostTask(
660 FROM_HERE,
661 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
662 main_thread_weak_ptr_,
663 base::Unretained(player_to_receive_animation), 0.000004));
666 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
667 AnimationPlayer* player_to_receive_animation) {
668 main_task_runner_->PostTask(
669 FROM_HERE,
670 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
671 main_thread_weak_ptr_,
672 base::Unretained(player_to_receive_animation), 0.0));
675 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
676 AnimationPlayer* player_to_receive_animation) {
677 main_task_runner_->PostTask(
678 FROM_HERE,
679 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
680 main_thread_weak_ptr_,
681 base::Unretained(player_to_receive_animation), 1.0));
684 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
685 main_task_runner_->PostTask(
686 FROM_HERE,
687 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
688 main_thread_weak_ptr_, defer_commits));
691 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
692 main_task_runner_->PostTask(FROM_HERE,
693 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
694 main_thread_weak_ptr_));
697 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
698 main_task_runner_->PostTask(
699 FROM_HERE,
700 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
701 main_thread_weak_ptr_));
704 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
705 main_task_runner_->PostTask(FROM_HERE,
706 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
707 main_thread_weak_ptr_));
710 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
711 const gfx::Rect& damage_rect) {
712 main_task_runner_->PostTask(
713 FROM_HERE,
714 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
715 main_thread_weak_ptr_,
716 damage_rect));
719 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
720 main_task_runner_->PostTask(
721 FROM_HERE,
722 base::Bind(
723 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
726 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
727 main_task_runner_->PostTask(
728 FROM_HERE,
729 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
730 main_thread_weak_ptr_));
733 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
734 main_task_runner_->PostTask(
735 FROM_HERE,
736 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
737 main_thread_weak_ptr_));
740 void LayerTreeTest::WillBeginTest() {
741 layer_tree_host_->SetLayerTreeHostClientReady();
744 void LayerTreeTest::DoBeginTest() {
745 client_ = LayerTreeHostClientForTesting::Create(this);
747 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
748 if (settings_.use_external_begin_frame_source) {
749 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
750 settings_.renderer_settings.refresh_rate));
751 external_begin_frame_source_ = external_begin_frame_source.get();
754 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
755 layer_tree_host_ = LayerTreeHostForTesting::Create(
756 this, client_.get(), shared_bitmap_manager_.get(),
757 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
758 base::ThreadTaskRunnerHandle::Get(),
759 impl_thread_ ? impl_thread_->task_runner() : NULL,
760 external_begin_frame_source.Pass());
761 ASSERT_TRUE(layer_tree_host_);
763 started_ = true;
764 beginning_ = true;
765 SetupTree();
766 WillBeginTest();
767 BeginTest();
768 beginning_ = false;
769 if (end_when_begin_returns_)
770 RealEndTest();
772 // Allow commits to happen once BeginTest() has had a chance to post tasks
773 // so that those tasks will happen before the first commit.
774 if (layer_tree_host_) {
775 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
776 ->set_test_started(true);
780 void LayerTreeTest::SetupTree() {
781 if (!layer_tree_host_->root_layer()) {
782 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
783 root_layer->SetBounds(gfx::Size(1, 1));
784 root_layer->SetIsDrawable(true);
785 layer_tree_host_->SetRootLayer(root_layer);
788 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
789 gfx::Size device_root_bounds = gfx::ToCeiledSize(
790 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
791 layer_tree_host_->SetViewportSize(device_root_bounds);
794 void LayerTreeTest::Timeout() {
795 timed_out_ = true;
796 EndTest();
799 void LayerTreeTest::RealEndTest() {
800 // TODO(mithro): Make this method only end when not inside an impl frame.
801 if (layer_tree_host_ && !timed_out_ &&
802 proxy()->MainFrameWillHappenForTesting()) {
803 main_task_runner_->PostTask(
804 FROM_HERE,
805 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
806 return;
809 base::MessageLoop::current()->Quit();
812 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
813 double animation_duration) {
814 DCHECK(!proxy() || proxy()->IsMainThread());
816 if (layer_to_receive_animation) {
817 AddOpacityTransitionToLayer(
818 layer_to_receive_animation, animation_duration, 0, 0.5, true);
822 void LayerTreeTest::DispatchAddAnimationToPlayer(
823 AnimationPlayer* player_to_receive_animation,
824 double animation_duration) {
825 DCHECK(!proxy() || proxy()->IsMainThread());
827 if (player_to_receive_animation) {
828 AddOpacityTransitionToPlayer(player_to_receive_animation,
829 animation_duration, 0, 0.5, true);
833 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
834 DCHECK(!proxy() || proxy()->IsMainThread());
836 if (layer_tree_host_)
837 layer_tree_host_->SetDeferCommits(defer_commits);
840 void LayerTreeTest::DispatchSetNeedsCommit() {
841 DCHECK(!proxy() || proxy()->IsMainThread());
843 if (layer_tree_host_)
844 layer_tree_host_->SetNeedsCommit();
847 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
848 DCHECK(!proxy() || proxy()->IsMainThread());
850 if (layer_tree_host_)
851 layer_tree_host_->SetNeedsUpdateLayers();
854 void LayerTreeTest::DispatchSetNeedsRedraw() {
855 DCHECK(!proxy() || proxy()->IsMainThread());
857 if (layer_tree_host_)
858 layer_tree_host_->SetNeedsRedraw();
861 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
862 DCHECK(!proxy() || proxy()->IsMainThread());
864 if (layer_tree_host_)
865 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
868 void LayerTreeTest::DispatchSetVisible(bool visible) {
869 DCHECK(!proxy() || proxy()->IsMainThread());
870 if (layer_tree_host_)
871 layer_tree_host_->SetVisible(visible);
874 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
875 DCHECK(!proxy() || proxy()->IsMainThread());
877 if (layer_tree_host_)
878 layer_tree_host_->SetNextCommitForcesRedraw();
881 void LayerTreeTest::DispatchCompositeImmediately() {
882 DCHECK(!proxy() || proxy()->IsMainThread());
883 if (layer_tree_host_)
884 layer_tree_host_->Composite(base::TimeTicks::Now());
887 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
888 if (threaded) {
889 impl_thread_.reset(new base::Thread("Compositor"));
890 ASSERT_TRUE(impl_thread_->Start());
893 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
895 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
896 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
897 task_graph_runner_.reset(new TestTaskGraphRunner);
899 delegating_renderer_ = delegating_renderer;
901 // Spend less time waiting for BeginFrame because the output is
902 // mocked out.
903 settings_.renderer_settings.refresh_rate = 200.0;
904 settings_.background_animation_rate = 200.0;
905 settings_.verify_property_trees = verify_property_trees_;
906 InitializeSettings(&settings_);
907 InitializeLayerSettings(&layer_settings_);
909 main_task_runner_->PostTask(
910 FROM_HERE,
911 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
913 if (timeout_seconds_) {
914 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
915 main_task_runner_->PostDelayedTask(
916 FROM_HERE,
917 timeout_.callback(),
918 base::TimeDelta::FromSeconds(timeout_seconds_));
921 base::MessageLoop::current()->Run();
922 DestroyLayerTreeHost();
924 timeout_.Cancel();
926 ASSERT_FALSE(layer_tree_host_.get());
927 client_ = nullptr;
928 if (timed_out_) {
929 FAIL() << "Test timed out";
930 return;
932 AfterTest();
935 void LayerTreeTest::RequestNewOutputSurface() {
936 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
939 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
940 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
941 DCHECK_EQ(delegating_renderer_,
942 output_surface->capabilities().delegated_rendering);
943 output_surface_ = output_surface.get();
945 if (settings_.use_external_begin_frame_source) {
946 DCHECK(external_begin_frame_source_);
947 DCHECK(external_begin_frame_source_->is_ready());
949 return output_surface.Pass();
952 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
953 if (delegating_renderer_)
954 return FakeOutputSurface::CreateDelegating3d();
955 else
956 return FakeOutputSurface::Create3d();
959 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
960 return static_cast<TestContextProvider*>(output_surface_->context_provider())
961 ->TestContext3d();
964 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
965 const {
966 if (impl->pending_tree())
967 return impl->pending_tree()->source_frame_number();
968 if (impl->active_tree())
969 return impl->active_tree()->source_frame_number();
970 // Source frames start at 0, so this is invalid.
971 return -1;
974 void LayerTreeTest::DestroyLayerTreeHost() {
975 if (layer_tree_host_ && layer_tree_host_->root_layer())
976 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
977 layer_tree_host_ = nullptr;
980 LayerTreeHost* LayerTreeTest::layer_tree_host() {
981 // We check for a null proxy here as we sometimes ask for the layer tree host
982 // when the proxy does not exist, often for checking settings after a test has
983 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
984 // elsewhere in this file for other examples.
985 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
986 return layer_tree_host_.get();
989 } // namespace cc