Update V8 to version 4.6.62.
[chromium-blink-merge.git] / cc / test / layer_tree_test.cc
blob7823ef192719c87d5ac682ce79118a1ac31929de
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 scoped_ptr<ResourcePool>* staging_resource_pool) {
112 host_impl->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
113 tile_task_worker_pool, resource_pool, staging_resource_pool);
116 // Adapts ThreadProxy for test. Injects test hooks for testing.
117 class ThreadProxyForTest : public ThreadProxy {
118 public:
119 static scoped_ptr<Proxy> Create(
120 TestHooks* test_hooks,
121 LayerTreeHost* host,
122 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
123 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
124 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
125 return make_scoped_ptr(new ThreadProxyForTest(
126 test_hooks,
127 host,
128 main_task_runner,
129 impl_task_runner,
130 external_begin_frame_source.Pass()));
133 ~ThreadProxyForTest() override {}
135 private:
136 TestHooks* test_hooks_;
138 void ScheduledActionSendBeginMainFrame() override {
139 test_hooks_->ScheduledActionWillSendBeginMainFrame();
140 ThreadProxy::ScheduledActionSendBeginMainFrame();
141 test_hooks_->ScheduledActionSendBeginMainFrame();
144 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
145 DrawResult result = ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
146 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
147 return result;
150 void ScheduledActionAnimate() override {
151 ThreadProxy::ScheduledActionAnimate();
152 test_hooks_->ScheduledActionAnimate();
155 void ScheduledActionCommit() override {
156 ThreadProxy::ScheduledActionCommit();
157 test_hooks_->ScheduledActionCommit();
160 void ScheduledActionBeginOutputSurfaceCreation() override {
161 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
162 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
165 void ScheduledActionPrepareTiles() override {
166 ThreadProxy::ScheduledActionPrepareTiles();
167 test_hooks_->ScheduledActionPrepareTiles();
170 void ScheduledActionInvalidateOutputSurface() override {
171 ThreadProxy::ScheduledActionInvalidateOutputSurface();
172 test_hooks_->ScheduledActionInvalidateOutputSurface();
175 void SendBeginMainFrameNotExpectedSoon() override {
176 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
177 test_hooks_->SendBeginMainFrameNotExpectedSoon();
180 ThreadProxyForTest(
181 TestHooks* test_hooks,
182 LayerTreeHost* host,
183 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
184 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
185 scoped_ptr<BeginFrameSource> external_begin_frame_source)
186 : ThreadProxy(host, main_task_runner,
187 impl_task_runner,
188 external_begin_frame_source.Pass()),
189 test_hooks_(test_hooks) {}
192 // Adapts ThreadProxy for test. Injects test hooks for testing.
193 class SingleThreadProxyForTest : public SingleThreadProxy {
194 public:
195 static scoped_ptr<Proxy> Create(
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 return make_scoped_ptr(new SingleThreadProxyForTest(
202 test_hooks, host, client, main_task_runner,
203 external_begin_frame_source.Pass()));
206 ~SingleThreadProxyForTest() override {}
208 private:
209 TestHooks* test_hooks_;
211 void ScheduledActionSendBeginMainFrame() override {
212 test_hooks_->ScheduledActionWillSendBeginMainFrame();
213 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
214 test_hooks_->ScheduledActionSendBeginMainFrame();
217 DrawResult ScheduledActionDrawAndSwapIfPossible() override {
218 DrawResult result =
219 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
220 test_hooks_->ScheduledActionDrawAndSwapIfPossible();
221 return result;
224 void ScheduledActionAnimate() override {
225 SingleThreadProxy::ScheduledActionAnimate();
226 test_hooks_->ScheduledActionAnimate();
229 void ScheduledActionCommit() override {
230 SingleThreadProxy::ScheduledActionCommit();
231 test_hooks_->ScheduledActionCommit();
234 void ScheduledActionBeginOutputSurfaceCreation() override {
235 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
236 test_hooks_->ScheduledActionBeginOutputSurfaceCreation();
239 void ScheduledActionPrepareTiles() override {
240 SingleThreadProxy::ScheduledActionPrepareTiles();
241 test_hooks_->ScheduledActionPrepareTiles();
244 void ScheduledActionInvalidateOutputSurface() override {
245 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
246 test_hooks_->ScheduledActionInvalidateOutputSurface();
249 void SendBeginMainFrameNotExpectedSoon() override {
250 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
251 test_hooks_->SendBeginMainFrameNotExpectedSoon();
254 SingleThreadProxyForTest(
255 TestHooks* test_hooks,
256 LayerTreeHost* host,
257 LayerTreeHostSingleThreadClient* client,
258 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
259 scoped_ptr<BeginFrameSource> external_begin_frame_source)
260 : SingleThreadProxy(host, client, main_task_runner,
261 external_begin_frame_source.Pass()),
262 test_hooks_(test_hooks) {}
265 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
266 class LayerTreeHostImplForTesting : public LayerTreeHostImpl {
267 public:
268 static scoped_ptr<LayerTreeHostImplForTesting> Create(
269 TestHooks* test_hooks,
270 const LayerTreeSettings& settings,
271 LayerTreeHostImplClient* host_impl_client,
272 Proxy* proxy,
273 SharedBitmapManager* shared_bitmap_manager,
274 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
275 TaskGraphRunner* task_graph_runner,
276 RenderingStatsInstrumentation* stats_instrumentation) {
277 return make_scoped_ptr(new LayerTreeHostImplForTesting(
278 test_hooks, settings, host_impl_client, proxy, shared_bitmap_manager,
279 gpu_memory_buffer_manager, task_graph_runner, stats_instrumentation));
282 protected:
283 LayerTreeHostImplForTesting(
284 TestHooks* test_hooks,
285 const LayerTreeSettings& settings,
286 LayerTreeHostImplClient* host_impl_client,
287 Proxy* proxy,
288 SharedBitmapManager* shared_bitmap_manager,
289 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
290 TaskGraphRunner* task_graph_runner,
291 RenderingStatsInstrumentation* stats_instrumentation)
292 : LayerTreeHostImpl(settings,
293 host_impl_client,
294 proxy,
295 stats_instrumentation,
296 shared_bitmap_manager,
297 gpu_memory_buffer_manager,
298 task_graph_runner,
300 test_hooks_(test_hooks),
301 block_notify_ready_to_activate_for_testing_(false),
302 notify_ready_to_activate_was_blocked_(false) {}
304 void CreateResourceAndTileTaskWorkerPool(
305 scoped_ptr<TileTaskWorkerPool>* tile_task_worker_pool,
306 scoped_ptr<ResourcePool>* resource_pool,
307 scoped_ptr<ResourcePool>* staging_resource_pool) override {
308 test_hooks_->CreateResourceAndTileTaskWorkerPool(
309 this, tile_task_worker_pool, resource_pool, staging_resource_pool);
312 void WillBeginImplFrame(const BeginFrameArgs& args) override {
313 LayerTreeHostImpl::WillBeginImplFrame(args);
314 test_hooks_->WillBeginImplFrameOnThread(this, args);
317 void DidFinishImplFrame() override {
318 LayerTreeHostImpl::DidFinishImplFrame();
319 test_hooks_->DidFinishImplFrameOnThread(this);
322 void BeginMainFrameAborted(CommitEarlyOutReason reason) override {
323 LayerTreeHostImpl::BeginMainFrameAborted(reason);
324 test_hooks_->BeginMainFrameAbortedOnThread(this, reason);
327 void BeginCommit() override {
328 LayerTreeHostImpl::BeginCommit();
329 test_hooks_->BeginCommitOnThread(this);
332 void CommitComplete() override {
333 LayerTreeHostImpl::CommitComplete();
334 test_hooks_->CommitCompleteOnThread(this);
337 DrawResult PrepareToDraw(FrameData* frame) override {
338 DrawResult draw_result = LayerTreeHostImpl::PrepareToDraw(frame);
339 return test_hooks_->PrepareToDrawOnThread(this, frame, draw_result);
342 void DrawLayers(FrameData* frame) override {
343 LayerTreeHostImpl::DrawLayers(frame);
344 test_hooks_->DrawLayersOnThread(this);
347 bool SwapBuffers(const LayerTreeHostImpl::FrameData& frame) override {
348 bool result = LayerTreeHostImpl::SwapBuffers(frame);
349 test_hooks_->SwapBuffersOnThread(this, result);
350 return result;
353 void DidSwapBuffersComplete() override {
354 LayerTreeHostImpl::DidSwapBuffersComplete();
355 test_hooks_->SwapBuffersCompleteOnThread(this);
358 void ReclaimResources(const CompositorFrameAck* ack) override {
359 LayerTreeHostImpl::ReclaimResources(ack);
362 void NotifyReadyToActivate() override {
363 if (block_notify_ready_to_activate_for_testing_) {
364 notify_ready_to_activate_was_blocked_ = true;
365 } else {
366 LayerTreeHostImpl::NotifyReadyToActivate();
367 test_hooks_->NotifyReadyToActivateOnThread(this);
371 void NotifyReadyToDraw() override {
372 LayerTreeHostImpl::NotifyReadyToDraw();
373 test_hooks_->NotifyReadyToDrawOnThread(this);
376 void NotifyAllTileTasksCompleted() override {
377 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
378 test_hooks_->NotifyAllTileTasksCompleted(this);
381 void BlockNotifyReadyToActivateForTesting(bool block) override {
382 CHECK(proxy()->ImplThreadTaskRunner())
383 << "Not supported for single-threaded mode.";
384 block_notify_ready_to_activate_for_testing_ = block;
385 if (!block && notify_ready_to_activate_was_blocked_) {
386 NotifyReadyToActivate();
387 notify_ready_to_activate_was_blocked_ = false;
391 void ActivateSyncTree() override {
392 test_hooks_->WillActivateTreeOnThread(this);
393 LayerTreeHostImpl::ActivateSyncTree();
394 DCHECK(!pending_tree());
395 test_hooks_->DidActivateTreeOnThread(this);
398 bool InitializeRenderer(scoped_ptr<OutputSurface> output_surface) override {
399 bool success = LayerTreeHostImpl::InitializeRenderer(output_surface.Pass());
400 test_hooks_->InitializedRendererOnThread(this, success);
401 return success;
404 void SetVisible(bool visible) override {
405 LayerTreeHostImpl::SetVisible(visible);
406 test_hooks_->DidSetVisibleOnImplTree(this, visible);
409 void AnimateLayers(base::TimeTicks monotonic_time) override {
410 test_hooks_->WillAnimateLayers(this, monotonic_time);
411 LayerTreeHostImpl::AnimateLayers(monotonic_time);
412 test_hooks_->AnimateLayers(this, monotonic_time);
415 void UpdateAnimationState(bool start_ready_animations) override {
416 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations);
417 bool has_unfinished_animation = false;
418 AnimationRegistrar* registrar =
419 animation_registrar() ? animation_registrar()
420 : animation_host()->animation_registrar();
421 for (const auto& it :
422 registrar->active_animation_controllers_for_testing()) {
423 if (it.second->HasActiveAnimation()) {
424 has_unfinished_animation = true;
425 break;
428 test_hooks_->UpdateAnimationState(this, has_unfinished_animation);
431 void NotifyTileStateChanged(const Tile* tile) override {
432 LayerTreeHostImpl::NotifyTileStateChanged(tile);
433 test_hooks_->NotifyTileStateChangedOnThread(this, tile);
436 private:
437 TestHooks* test_hooks_;
438 bool block_notify_ready_to_activate_for_testing_;
439 bool notify_ready_to_activate_was_blocked_;
442 // Implementation of LayerTreeHost callback interface.
443 class LayerTreeHostClientForTesting : public LayerTreeHostClient,
444 public LayerTreeHostSingleThreadClient {
445 public:
446 static scoped_ptr<LayerTreeHostClientForTesting> Create(
447 TestHooks* test_hooks) {
448 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks));
450 ~LayerTreeHostClientForTesting() override {}
452 void WillBeginMainFrame() override { test_hooks_->WillBeginMainFrame(); }
454 void DidBeginMainFrame() override { test_hooks_->DidBeginMainFrame(); }
456 void BeginMainFrame(const BeginFrameArgs& args) override {
457 test_hooks_->BeginMainFrame(args);
460 void Layout() override { test_hooks_->Layout(); }
462 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta,
463 const gfx::Vector2dF& outer_delta,
464 const gfx::Vector2dF& elastic_overscroll_delta,
465 float page_scale,
466 float top_controls_delta) override {
467 test_hooks_->ApplyViewportDeltas(inner_delta, outer_delta,
468 elastic_overscroll_delta, page_scale,
469 top_controls_delta);
472 void RequestNewOutputSurface() override {
473 test_hooks_->RequestNewOutputSurface();
476 void DidInitializeOutputSurface() override {
477 test_hooks_->DidInitializeOutputSurface();
480 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
481 test_hooks_->SendBeginFramesToChildren(args);
484 void DidFailToInitializeOutputSurface() override {
485 test_hooks_->DidFailToInitializeOutputSurface();
486 RequestNewOutputSurface();
489 void WillCommit() override { test_hooks_->WillCommit(); }
491 void DidCommit() override { test_hooks_->DidCommit(); }
493 void DidCommitAndDrawFrame() override {
494 test_hooks_->DidCommitAndDrawFrame();
497 void DidCompleteSwapBuffers() override {
498 test_hooks_->DidCompleteSwapBuffers();
501 void DidPostSwapBuffers() override {}
502 void DidAbortSwapBuffers() override {}
503 void ScheduleComposite() override { test_hooks_->ScheduleComposite(); }
504 void DidCompletePageScaleAnimation() override {}
505 void BeginMainFrameNotExpectedSoon() override {}
507 void RecordFrameTimingEvents(
508 scoped_ptr<FrameTimingTracker::CompositeTimingSet> composite_events,
509 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> main_frame_events)
510 override {}
512 private:
513 explicit LayerTreeHostClientForTesting(TestHooks* test_hooks)
514 : test_hooks_(test_hooks) {}
516 TestHooks* test_hooks_;
519 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
520 class LayerTreeHostForTesting : public LayerTreeHost {
521 public:
522 static scoped_ptr<LayerTreeHostForTesting> Create(
523 TestHooks* test_hooks,
524 LayerTreeHostClientForTesting* client,
525 SharedBitmapManager* shared_bitmap_manager,
526 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager,
527 TaskGraphRunner* task_graph_runner,
528 const LayerTreeSettings& settings,
529 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
530 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner,
531 scoped_ptr<BeginFrameSource> external_begin_frame_source) {
532 LayerTreeHost::InitParams params;
533 params.client = client;
534 params.shared_bitmap_manager = shared_bitmap_manager;
535 params.gpu_memory_buffer_manager = gpu_memory_buffer_manager;
536 params.task_graph_runner = task_graph_runner;
537 params.settings = &settings;
538 scoped_ptr<LayerTreeHostForTesting> layer_tree_host(
539 new LayerTreeHostForTesting(test_hooks, &params));
540 if (impl_task_runner.get()) {
541 layer_tree_host->InitializeForTesting(
542 ThreadProxyForTest::Create(test_hooks,
543 layer_tree_host.get(),
544 main_task_runner,
545 impl_task_runner,
546 external_begin_frame_source.Pass()));
547 } else {
548 layer_tree_host->InitializeForTesting(
549 SingleThreadProxyForTest::Create(
550 test_hooks,
551 layer_tree_host.get(),
552 client,
553 main_task_runner,
554 external_begin_frame_source.Pass()));
556 return layer_tree_host.Pass();
559 scoped_ptr<LayerTreeHostImpl> CreateLayerTreeHostImpl(
560 LayerTreeHostImplClient* host_impl_client) override {
561 return LayerTreeHostImplForTesting::Create(
562 test_hooks_, settings(), host_impl_client, proxy(),
563 shared_bitmap_manager(), gpu_memory_buffer_manager(),
564 task_graph_runner(), rendering_stats_instrumentation());
567 void SetNeedsCommit() override {
568 if (!test_started_)
569 return;
570 LayerTreeHost::SetNeedsCommit();
573 void set_test_started(bool started) { test_started_ = started; }
575 private:
576 LayerTreeHostForTesting(TestHooks* test_hooks,
577 LayerTreeHost::InitParams* params)
578 : LayerTreeHost(params), test_hooks_(test_hooks), test_started_(false) {}
580 TestHooks* test_hooks_;
581 bool test_started_;
584 LayerTreeTest::LayerTreeTest()
585 : output_surface_(nullptr),
586 external_begin_frame_source_(nullptr),
587 beginning_(false),
588 end_when_begin_returns_(false),
589 timed_out_(false),
590 scheduled_(false),
591 started_(false),
592 ended_(false),
593 delegating_renderer_(false),
594 verify_property_trees_(true),
595 timeout_seconds_(0),
596 weak_factory_(this) {
597 main_thread_weak_ptr_ = weak_factory_.GetWeakPtr();
599 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
600 // specified (for running in a debugger).
601 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
602 if (!command_line->HasSwitch(switches::kCCLayerTreeTestNoTimeout))
603 timeout_seconds_ = 5;
606 LayerTreeTest::~LayerTreeTest() {}
608 void LayerTreeTest::EndTest() {
609 if (ended_)
610 return;
611 ended_ = true;
613 // For the case where we EndTest during BeginTest(), set a flag to indicate
614 // that the test should end the second BeginTest regains control.
615 if (beginning_) {
616 end_when_begin_returns_ = true;
617 } else {
618 main_task_runner_->PostTask(
619 FROM_HERE,
620 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
624 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds) {
625 main_task_runner_->PostDelayedTask(
626 FROM_HERE,
627 base::Bind(&LayerTreeTest::EndTest, main_thread_weak_ptr_),
628 base::TimeDelta::FromMilliseconds(delay_milliseconds));
631 void LayerTreeTest::PostAddAnimationToMainThread(
632 Layer* layer_to_receive_animation) {
633 main_task_runner_->PostTask(
634 FROM_HERE,
635 base::Bind(&LayerTreeTest::DispatchAddAnimation, main_thread_weak_ptr_,
636 base::Unretained(layer_to_receive_animation), 0.000004));
639 void LayerTreeTest::PostAddInstantAnimationToMainThread(
640 Layer* layer_to_receive_animation) {
641 main_task_runner_->PostTask(
642 FROM_HERE,
643 base::Bind(&LayerTreeTest::DispatchAddAnimation,
644 main_thread_weak_ptr_,
645 base::Unretained(layer_to_receive_animation),
646 0.0));
649 void LayerTreeTest::PostAddLongAnimationToMainThread(
650 Layer* layer_to_receive_animation) {
651 main_task_runner_->PostTask(
652 FROM_HERE,
653 base::Bind(&LayerTreeTest::DispatchAddAnimation,
654 main_thread_weak_ptr_,
655 base::Unretained(layer_to_receive_animation),
656 1.0));
659 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
660 AnimationPlayer* player_to_receive_animation) {
661 main_task_runner_->PostTask(
662 FROM_HERE,
663 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
664 main_thread_weak_ptr_,
665 base::Unretained(player_to_receive_animation), 0.000004));
668 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
669 AnimationPlayer* player_to_receive_animation) {
670 main_task_runner_->PostTask(
671 FROM_HERE,
672 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
673 main_thread_weak_ptr_,
674 base::Unretained(player_to_receive_animation), 0.0));
677 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
678 AnimationPlayer* player_to_receive_animation) {
679 main_task_runner_->PostTask(
680 FROM_HERE,
681 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer,
682 main_thread_weak_ptr_,
683 base::Unretained(player_to_receive_animation), 1.0));
686 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits) {
687 main_task_runner_->PostTask(
688 FROM_HERE,
689 base::Bind(&LayerTreeTest::DispatchSetDeferCommits,
690 main_thread_weak_ptr_, defer_commits));
693 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
694 main_task_runner_->PostTask(FROM_HERE,
695 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit,
696 main_thread_weak_ptr_));
699 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
700 main_task_runner_->PostTask(
701 FROM_HERE,
702 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers,
703 main_thread_weak_ptr_));
706 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
707 main_task_runner_->PostTask(FROM_HERE,
708 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw,
709 main_thread_weak_ptr_));
712 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
713 const gfx::Rect& damage_rect) {
714 main_task_runner_->PostTask(
715 FROM_HERE,
716 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect,
717 main_thread_weak_ptr_,
718 damage_rect));
721 void LayerTreeTest::PostSetVisibleToMainThread(bool visible) {
722 main_task_runner_->PostTask(
723 FROM_HERE,
724 base::Bind(
725 &LayerTreeTest::DispatchSetVisible, main_thread_weak_ptr_, visible));
728 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
729 main_task_runner_->PostTask(
730 FROM_HERE,
731 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw,
732 main_thread_weak_ptr_));
735 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
736 main_task_runner_->PostTask(
737 FROM_HERE,
738 base::Bind(&LayerTreeTest::DispatchCompositeImmediately,
739 main_thread_weak_ptr_));
742 void LayerTreeTest::WillBeginTest() {
743 layer_tree_host_->SetLayerTreeHostClientReady();
746 void LayerTreeTest::DoBeginTest() {
747 client_ = LayerTreeHostClientForTesting::Create(this);
749 scoped_ptr<FakeExternalBeginFrameSource> external_begin_frame_source;
750 if (settings_.use_external_begin_frame_source) {
751 external_begin_frame_source.reset(new FakeExternalBeginFrameSource(
752 settings_.renderer_settings.refresh_rate));
753 external_begin_frame_source_ = external_begin_frame_source.get();
756 DCHECK(!impl_thread_ || impl_thread_->task_runner().get());
757 layer_tree_host_ = LayerTreeHostForTesting::Create(
758 this, client_.get(), shared_bitmap_manager_.get(),
759 gpu_memory_buffer_manager_.get(), task_graph_runner_.get(), settings_,
760 base::ThreadTaskRunnerHandle::Get(),
761 impl_thread_ ? impl_thread_->task_runner() : NULL,
762 external_begin_frame_source.Pass());
763 ASSERT_TRUE(layer_tree_host_);
765 started_ = true;
766 beginning_ = true;
767 SetupTree();
768 WillBeginTest();
769 BeginTest();
770 beginning_ = false;
771 if (end_when_begin_returns_)
772 RealEndTest();
774 // Allow commits to happen once BeginTest() has had a chance to post tasks
775 // so that those tasks will happen before the first commit.
776 if (layer_tree_host_) {
777 static_cast<LayerTreeHostForTesting*>(layer_tree_host_.get())
778 ->set_test_started(true);
782 void LayerTreeTest::SetupTree() {
783 if (!layer_tree_host_->root_layer()) {
784 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
785 root_layer->SetBounds(gfx::Size(1, 1));
786 root_layer->SetIsDrawable(true);
787 layer_tree_host_->SetRootLayer(root_layer);
790 gfx::Size root_bounds = layer_tree_host_->root_layer()->bounds();
791 gfx::Size device_root_bounds = gfx::ToCeiledSize(
792 gfx::ScaleSize(root_bounds, layer_tree_host_->device_scale_factor()));
793 layer_tree_host_->SetViewportSize(device_root_bounds);
796 void LayerTreeTest::Timeout() {
797 timed_out_ = true;
798 EndTest();
801 void LayerTreeTest::RealEndTest() {
802 // TODO(mithro): Make this method only end when not inside an impl frame.
803 if (layer_tree_host_ && !timed_out_ &&
804 proxy()->MainFrameWillHappenForTesting()) {
805 main_task_runner_->PostTask(
806 FROM_HERE,
807 base::Bind(&LayerTreeTest::RealEndTest, main_thread_weak_ptr_));
808 return;
811 base::MessageLoop::current()->Quit();
814 void LayerTreeTest::DispatchAddAnimation(Layer* layer_to_receive_animation,
815 double animation_duration) {
816 DCHECK(!proxy() || proxy()->IsMainThread());
818 if (layer_to_receive_animation) {
819 AddOpacityTransitionToLayer(
820 layer_to_receive_animation, animation_duration, 0, 0.5, true);
824 void LayerTreeTest::DispatchAddAnimationToPlayer(
825 AnimationPlayer* player_to_receive_animation,
826 double animation_duration) {
827 DCHECK(!proxy() || proxy()->IsMainThread());
829 if (player_to_receive_animation) {
830 AddOpacityTransitionToPlayer(player_to_receive_animation,
831 animation_duration, 0, 0.5, true);
835 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits) {
836 DCHECK(!proxy() || proxy()->IsMainThread());
838 if (layer_tree_host_)
839 layer_tree_host_->SetDeferCommits(defer_commits);
842 void LayerTreeTest::DispatchSetNeedsCommit() {
843 DCHECK(!proxy() || proxy()->IsMainThread());
845 if (layer_tree_host_)
846 layer_tree_host_->SetNeedsCommit();
849 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
850 DCHECK(!proxy() || proxy()->IsMainThread());
852 if (layer_tree_host_)
853 layer_tree_host_->SetNeedsUpdateLayers();
856 void LayerTreeTest::DispatchSetNeedsRedraw() {
857 DCHECK(!proxy() || proxy()->IsMainThread());
859 if (layer_tree_host_)
860 layer_tree_host_->SetNeedsRedraw();
863 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect) {
864 DCHECK(!proxy() || proxy()->IsMainThread());
866 if (layer_tree_host_)
867 layer_tree_host_->SetNeedsRedrawRect(damage_rect);
870 void LayerTreeTest::DispatchSetVisible(bool visible) {
871 DCHECK(!proxy() || proxy()->IsMainThread());
872 if (layer_tree_host_)
873 layer_tree_host_->SetVisible(visible);
876 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
877 DCHECK(!proxy() || proxy()->IsMainThread());
879 if (layer_tree_host_)
880 layer_tree_host_->SetNextCommitForcesRedraw();
883 void LayerTreeTest::DispatchCompositeImmediately() {
884 DCHECK(!proxy() || proxy()->IsMainThread());
885 if (layer_tree_host_)
886 layer_tree_host_->Composite(base::TimeTicks::Now());
889 void LayerTreeTest::RunTest(bool threaded, bool delegating_renderer) {
890 if (threaded) {
891 impl_thread_.reset(new base::Thread("Compositor"));
892 ASSERT_TRUE(impl_thread_->Start());
895 main_task_runner_ = base::ThreadTaskRunnerHandle::Get();
897 shared_bitmap_manager_.reset(new TestSharedBitmapManager);
898 gpu_memory_buffer_manager_.reset(new TestGpuMemoryBufferManager);
899 task_graph_runner_.reset(new TestTaskGraphRunner);
901 delegating_renderer_ = delegating_renderer;
903 // Spend less time waiting for BeginFrame because the output is
904 // mocked out.
905 settings_.renderer_settings.refresh_rate = 200.0;
906 settings_.background_animation_rate = 200.0;
907 settings_.verify_property_trees = verify_property_trees_;
908 InitializeSettings(&settings_);
909 InitializeLayerSettings(&layer_settings_);
911 main_task_runner_->PostTask(
912 FROM_HERE,
913 base::Bind(&LayerTreeTest::DoBeginTest, base::Unretained(this)));
915 if (timeout_seconds_) {
916 timeout_.Reset(base::Bind(&LayerTreeTest::Timeout, base::Unretained(this)));
917 main_task_runner_->PostDelayedTask(
918 FROM_HERE,
919 timeout_.callback(),
920 base::TimeDelta::FromSeconds(timeout_seconds_));
923 base::MessageLoop::current()->Run();
924 DestroyLayerTreeHost();
926 timeout_.Cancel();
928 ASSERT_FALSE(layer_tree_host_.get());
929 client_ = nullptr;
930 if (timed_out_) {
931 FAIL() << "Test timed out";
932 return;
934 AfterTest();
937 void LayerTreeTest::RequestNewOutputSurface() {
938 layer_tree_host_->SetOutputSurface(CreateOutputSurface());
941 scoped_ptr<OutputSurface> LayerTreeTest::CreateOutputSurface() {
942 scoped_ptr<FakeOutputSurface> output_surface = CreateFakeOutputSurface();
943 DCHECK_EQ(delegating_renderer_,
944 output_surface->capabilities().delegated_rendering);
945 output_surface_ = output_surface.get();
947 if (settings_.use_external_begin_frame_source) {
948 DCHECK(external_begin_frame_source_);
949 DCHECK(external_begin_frame_source_->is_ready());
951 return output_surface.Pass();
954 scoped_ptr<FakeOutputSurface> LayerTreeTest::CreateFakeOutputSurface() {
955 if (delegating_renderer_)
956 return FakeOutputSurface::CreateDelegating3d();
957 else
958 return FakeOutputSurface::Create3d();
961 TestWebGraphicsContext3D* LayerTreeTest::TestContext() {
962 return static_cast<TestContextProvider*>(output_surface_->context_provider())
963 ->TestContext3d();
966 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl)
967 const {
968 if (impl->pending_tree())
969 return impl->pending_tree()->source_frame_number();
970 if (impl->active_tree())
971 return impl->active_tree()->source_frame_number();
972 // Source frames start at 0, so this is invalid.
973 return -1;
976 void LayerTreeTest::DestroyLayerTreeHost() {
977 if (layer_tree_host_ && layer_tree_host_->root_layer())
978 layer_tree_host_->root_layer()->SetLayerTreeHost(NULL);
979 layer_tree_host_ = nullptr;
982 LayerTreeHost* LayerTreeTest::layer_tree_host() {
983 // We check for a null proxy here as we sometimes ask for the layer tree host
984 // when the proxy does not exist, often for checking settings after a test has
985 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
986 // elsewhere in this file for other examples.
987 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
988 return layer_tree_host_.get();
991 } // namespace cc