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 "cc/animation/animation.h"
9 #include "cc/animation/animation_registrar.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/base/switches.h"
13 #include "cc/input/input_handler.h"
14 #include "cc/layers/content_layer.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_impl.h"
17 #include "cc/test/animation_test_common.h"
18 #include "cc/test/fake_layer_tree_host_client.h"
19 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/test_context_provider.h"
21 #include "cc/test/test_shared_bitmap_manager.h"
22 #include "cc/test/tiled_layer_test_common.h"
23 #include "cc/trees/layer_tree_host_client.h"
24 #include "cc/trees/layer_tree_host_impl.h"
25 #include "cc/trees/layer_tree_host_single_thread_client.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "cc/trees/single_thread_proxy.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "ui/gfx/frame_time.h"
30 #include "ui/gfx/size_conversions.h"
34 TestHooks::TestHooks() {}
36 TestHooks::~TestHooks() {}
38 DrawSwapReadbackResult::DrawResult
TestHooks::PrepareToDrawOnThread(
39 LayerTreeHostImpl
* host_impl
,
40 LayerTreeHostImpl::FrameData
* frame_data
,
41 DrawSwapReadbackResult::DrawResult draw_result
) {
45 base::TimeDelta
TestHooks::LowFrequencyAnimationInterval() const {
46 return base::TimeDelta::FromMilliseconds(16);
49 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
50 class LayerTreeHostImplForTesting
: public LayerTreeHostImpl
{
52 static scoped_ptr
<LayerTreeHostImplForTesting
> Create(
53 TestHooks
* test_hooks
,
54 const LayerTreeSettings
& settings
,
55 LayerTreeHostImplClient
* host_impl_client
,
57 SharedBitmapManager
* manager
,
58 RenderingStatsInstrumentation
* stats_instrumentation
) {
59 return make_scoped_ptr(
60 new LayerTreeHostImplForTesting(test_hooks
,
65 stats_instrumentation
));
69 LayerTreeHostImplForTesting(
70 TestHooks
* test_hooks
,
71 const LayerTreeSettings
& settings
,
72 LayerTreeHostImplClient
* host_impl_client
,
74 SharedBitmapManager
* manager
,
75 RenderingStatsInstrumentation
* stats_instrumentation
)
76 : LayerTreeHostImpl(settings
,
79 stats_instrumentation
,
82 test_hooks_(test_hooks
),
83 block_notify_ready_to_activate_for_testing_(false),
84 notify_ready_to_activate_was_blocked_(false) {}
86 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) OVERRIDE
{
87 LayerTreeHostImpl::WillBeginImplFrame(args
);
88 test_hooks_
->WillBeginImplFrameOnThread(this, args
);
91 virtual void BeginMainFrameAborted(bool did_handle
) OVERRIDE
{
92 LayerTreeHostImpl::BeginMainFrameAborted(did_handle
);
93 test_hooks_
->BeginMainFrameAbortedOnThread(this, did_handle
);
96 virtual void BeginCommit() OVERRIDE
{
97 LayerTreeHostImpl::BeginCommit();
98 test_hooks_
->BeginCommitOnThread(this);
101 virtual void CommitComplete() OVERRIDE
{
102 LayerTreeHostImpl::CommitComplete();
103 test_hooks_
->CommitCompleteOnThread(this);
105 if (!settings().impl_side_painting
) {
106 test_hooks_
->WillActivateTreeOnThread(this);
107 test_hooks_
->DidActivateTreeOnThread(this);
111 virtual DrawSwapReadbackResult::DrawResult
PrepareToDraw(
113 const gfx::Rect
& damage_rect
) OVERRIDE
{
114 DrawSwapReadbackResult::DrawResult draw_result
=
115 LayerTreeHostImpl::PrepareToDraw(frame
, damage_rect
);
116 return test_hooks_
->PrepareToDrawOnThread(this, frame
, draw_result
);
119 virtual void DrawLayers(FrameData
* frame
,
120 base::TimeTicks frame_begin_time
) OVERRIDE
{
121 LayerTreeHostImpl::DrawLayers(frame
, frame_begin_time
);
122 test_hooks_
->DrawLayersOnThread(this);
125 virtual bool SwapBuffers(const LayerTreeHostImpl::FrameData
& frame
) OVERRIDE
{
126 bool result
= LayerTreeHostImpl::SwapBuffers(frame
);
127 test_hooks_
->SwapBuffersOnThread(this, result
);
131 virtual void DidSwapBuffersComplete() OVERRIDE
{
132 LayerTreeHostImpl::DidSwapBuffersComplete();
133 test_hooks_
->SwapBuffersCompleteOnThread(this);
136 virtual void ReclaimResources(const CompositorFrameAck
* ack
) OVERRIDE
{
137 LayerTreeHostImpl::ReclaimResources(ack
);
140 virtual void UpdateVisibleTiles() OVERRIDE
{
141 LayerTreeHostImpl::UpdateVisibleTiles();
142 test_hooks_
->UpdateVisibleTilesOnThread(this);
145 virtual void NotifyReadyToActivate() OVERRIDE
{
146 if (block_notify_ready_to_activate_for_testing_
)
147 notify_ready_to_activate_was_blocked_
= true;
149 client_
->NotifyReadyToActivate();
152 virtual void BlockNotifyReadyToActivateForTesting(bool block
) OVERRIDE
{
153 block_notify_ready_to_activate_for_testing_
= block
;
154 if (!block
&& notify_ready_to_activate_was_blocked_
) {
155 NotifyReadyToActivate();
156 notify_ready_to_activate_was_blocked_
= false;
160 virtual void ActivatePendingTree() OVERRIDE
{
161 test_hooks_
->WillActivateTreeOnThread(this);
162 LayerTreeHostImpl::ActivatePendingTree();
163 DCHECK(!pending_tree());
164 test_hooks_
->DidActivateTreeOnThread(this);
167 virtual bool InitializeRenderer(scoped_ptr
<OutputSurface
> output_surface
)
169 bool success
= LayerTreeHostImpl::InitializeRenderer(output_surface
.Pass());
170 test_hooks_
->InitializedRendererOnThread(this, success
);
174 virtual void SetVisible(bool visible
) OVERRIDE
{
175 LayerTreeHostImpl::SetVisible(visible
);
176 test_hooks_
->DidSetVisibleOnImplTree(this, visible
);
179 virtual void AnimateLayers(base::TimeTicks monotonic_time
) OVERRIDE
{
180 test_hooks_
->WillAnimateLayers(this, monotonic_time
);
181 LayerTreeHostImpl::AnimateLayers(monotonic_time
);
182 test_hooks_
->AnimateLayers(this, monotonic_time
);
185 virtual void UpdateAnimationState(bool start_ready_animations
) OVERRIDE
{
186 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations
);
187 bool has_unfinished_animation
= false;
188 AnimationRegistrar::AnimationControllerMap::const_iterator iter
=
189 active_animation_controllers().begin();
190 for (; iter
!= active_animation_controllers().end(); ++iter
) {
191 if (iter
->second
->HasActiveAnimation()) {
192 has_unfinished_animation
= true;
196 test_hooks_
->UpdateAnimationState(this, has_unfinished_animation
);
199 virtual base::TimeDelta
LowFrequencyAnimationInterval() const OVERRIDE
{
200 return test_hooks_
->LowFrequencyAnimationInterval();
204 TestHooks
* test_hooks_
;
205 bool block_notify_ready_to_activate_for_testing_
;
206 bool notify_ready_to_activate_was_blocked_
;
209 // Implementation of LayerTreeHost callback interface.
210 class LayerTreeHostClientForTesting
: public LayerTreeHostClient
,
211 public LayerTreeHostSingleThreadClient
{
213 static scoped_ptr
<LayerTreeHostClientForTesting
> Create(
214 TestHooks
* test_hooks
) {
215 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks
));
217 virtual ~LayerTreeHostClientForTesting() {}
219 virtual void WillBeginMainFrame(int frame_id
) OVERRIDE
{
220 test_hooks_
->WillBeginMainFrame();
223 virtual void DidBeginMainFrame() OVERRIDE
{
224 test_hooks_
->DidBeginMainFrame();
227 virtual void Animate(base::TimeTicks monotonic_time
) OVERRIDE
{
228 test_hooks_
->Animate(monotonic_time
);
231 virtual void Layout() OVERRIDE
{ test_hooks_
->Layout(); }
233 virtual void ApplyScrollAndScale(const gfx::Vector2d
& scroll_delta
,
234 float scale
) OVERRIDE
{
235 test_hooks_
->ApplyScrollAndScale(scroll_delta
, scale
);
238 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface(bool fallback
)
240 return test_hooks_
->CreateOutputSurface(fallback
);
243 virtual void DidInitializeOutputSurface() OVERRIDE
{
244 test_hooks_
->DidInitializeOutputSurface();
247 virtual void DidFailToInitializeOutputSurface() OVERRIDE
{
248 test_hooks_
->DidFailToInitializeOutputSurface();
251 virtual void WillCommit() OVERRIDE
{ test_hooks_
->WillCommit(); }
253 virtual void DidCommit() OVERRIDE
{ test_hooks_
->DidCommit(); }
255 virtual void DidCommitAndDrawFrame() OVERRIDE
{
256 test_hooks_
->DidCommitAndDrawFrame();
259 virtual void DidCompleteSwapBuffers() OVERRIDE
{
260 test_hooks_
->DidCompleteSwapBuffers();
263 virtual void ScheduleComposite() OVERRIDE
{
264 test_hooks_
->ScheduleComposite();
267 virtual void ScheduleAnimation() OVERRIDE
{
268 test_hooks_
->ScheduleAnimation();
271 virtual void DidPostSwapBuffers() OVERRIDE
{}
272 virtual void DidAbortSwapBuffers() OVERRIDE
{}
275 explicit LayerTreeHostClientForTesting(TestHooks
* test_hooks
)
276 : test_hooks_(test_hooks
) {}
278 TestHooks
* test_hooks_
;
281 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
282 class LayerTreeHostForTesting
: public LayerTreeHost
{
284 static scoped_ptr
<LayerTreeHostForTesting
> Create(
285 TestHooks
* test_hooks
,
286 LayerTreeHostClientForTesting
* client
,
287 const LayerTreeSettings
& settings
,
288 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
) {
289 scoped_ptr
<LayerTreeHostForTesting
> layer_tree_host(
290 new LayerTreeHostForTesting(test_hooks
, client
, settings
));
291 if (impl_task_runner
.get())
292 layer_tree_host
->InitializeThreaded(impl_task_runner
);
294 layer_tree_host
->InitializeSingleThreaded(client
);
295 return layer_tree_host
.Pass();
298 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
299 LayerTreeHostImplClient
* host_impl_client
) OVERRIDE
{
300 return LayerTreeHostImplForTesting::Create(
305 shared_bitmap_manager_
.get(),
306 rendering_stats_instrumentation()).PassAs
<LayerTreeHostImpl
>();
309 virtual void SetNeedsCommit() OVERRIDE
{
312 LayerTreeHost::SetNeedsCommit();
315 void set_test_started(bool started
) { test_started_
= started
; }
317 virtual void DidDeferCommit() OVERRIDE
{ test_hooks_
->DidDeferCommit(); }
320 LayerTreeHostForTesting(TestHooks
* test_hooks
,
321 LayerTreeHostClient
* client
,
322 const LayerTreeSettings
& settings
)
323 : LayerTreeHost(client
, NULL
, settings
),
324 shared_bitmap_manager_(new TestSharedBitmapManager()),
325 test_hooks_(test_hooks
),
326 test_started_(false) {}
328 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
329 TestHooks
* test_hooks_
;
333 LayerTreeTest::LayerTreeTest()
335 end_when_begin_returns_(false),
338 schedule_when_set_visible_true_(false),
341 delegating_renderer_(false),
343 weak_factory_(this) {
344 main_thread_weak_ptr_
= weak_factory_
.GetWeakPtr();
346 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
347 // specified (for running in a debugger).
348 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
349 if (!command_line
->HasSwitch(switches::kCCLayerTreeTestNoTimeout
))
350 timeout_seconds_
= 5;
353 LayerTreeTest::~LayerTreeTest() {}
355 void LayerTreeTest::EndTest() {
360 // For the case where we EndTest during BeginTest(), set a flag to indicate
361 // that the test should end the second BeginTest regains control.
363 end_when_begin_returns_
= true;
365 main_task_runner_
->PostTask(
367 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
371 void LayerTreeTest::EndTestAfterDelay(int delay_milliseconds
) {
372 main_task_runner_
->PostDelayedTask(
374 base::Bind(&LayerTreeTest::EndTest
, main_thread_weak_ptr_
),
375 base::TimeDelta::FromMilliseconds(delay_milliseconds
));
378 void LayerTreeTest::PostAddAnimationToMainThread(
379 Layer
* layer_to_receive_animation
) {
380 main_task_runner_
->PostTask(
382 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
383 main_thread_weak_ptr_
,
384 base::Unretained(layer_to_receive_animation
),
388 void LayerTreeTest::PostAddInstantAnimationToMainThread(
389 Layer
* layer_to_receive_animation
) {
390 main_task_runner_
->PostTask(
392 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
393 main_thread_weak_ptr_
,
394 base::Unretained(layer_to_receive_animation
),
398 void LayerTreeTest::PostAddLongAnimationToMainThread(
399 Layer
* layer_to_receive_animation
) {
400 main_task_runner_
->PostTask(
402 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
403 main_thread_weak_ptr_
,
404 base::Unretained(layer_to_receive_animation
),
408 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
409 main_task_runner_
->PostTask(FROM_HERE
,
410 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit
,
411 main_thread_weak_ptr_
));
414 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
415 main_task_runner_
->PostTask(
417 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers
,
418 main_thread_weak_ptr_
));
421 void LayerTreeTest::PostReadbackToMainThread() {
422 main_task_runner_
->PostTask(
424 base::Bind(&LayerTreeTest::DispatchReadback
, main_thread_weak_ptr_
));
427 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
428 main_task_runner_
->PostTask(FROM_HERE
,
429 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw
,
430 main_thread_weak_ptr_
));
433 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
434 const gfx::Rect
& damage_rect
) {
435 main_task_runner_
->PostTask(
437 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect
,
438 main_thread_weak_ptr_
,
442 void LayerTreeTest::PostSetVisibleToMainThread(bool visible
) {
443 main_task_runner_
->PostTask(
446 &LayerTreeTest::DispatchSetVisible
, main_thread_weak_ptr_
, visible
));
449 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
450 main_task_runner_
->PostTask(
452 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw
,
453 main_thread_weak_ptr_
));
456 void LayerTreeTest::WillBeginTest() {
457 layer_tree_host_
->SetLayerTreeHostClientReady();
460 void LayerTreeTest::DoBeginTest() {
461 client_
= LayerTreeHostClientForTesting::Create(this);
463 DCHECK(!impl_thread_
|| impl_thread_
->message_loop_proxy().get());
464 layer_tree_host_
= LayerTreeHostForTesting::Create(
468 impl_thread_
? impl_thread_
->message_loop_proxy() : NULL
);
469 ASSERT_TRUE(layer_tree_host_
);
477 if (end_when_begin_returns_
)
480 // Allow commits to happen once BeginTest() has had a chance to post tasks
481 // so that those tasks will happen before the first commit.
482 if (layer_tree_host_
) {
483 static_cast<LayerTreeHostForTesting
*>(layer_tree_host_
.get())
484 ->set_test_started(true);
488 void LayerTreeTest::SetupTree() {
489 if (!layer_tree_host_
->root_layer()) {
490 scoped_refptr
<Layer
> root_layer
= Layer::Create();
491 root_layer
->SetAnchorPoint(gfx::PointF());
492 root_layer
->SetBounds(gfx::Size(1, 1));
493 root_layer
->SetIsDrawable(true);
494 layer_tree_host_
->SetRootLayer(root_layer
);
497 gfx::Size root_bounds
= layer_tree_host_
->root_layer()->bounds();
498 gfx::Size device_root_bounds
= gfx::ToCeiledSize(
499 gfx::ScaleSize(root_bounds
, layer_tree_host_
->device_scale_factor()));
500 layer_tree_host_
->SetViewportSize(device_root_bounds
);
503 void LayerTreeTest::Timeout() {
508 void LayerTreeTest::ScheduleComposite() {
509 if (!started_
|| scheduled_
)
512 main_task_runner_
->PostTask(
514 base::Bind(&LayerTreeTest::DispatchComposite
, main_thread_weak_ptr_
));
517 void LayerTreeTest::RealEndTest() {
518 if (layer_tree_host_
&& proxy()->CommitPendingForTesting()) {
519 main_task_runner_
->PostTask(
521 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
525 base::MessageLoop::current()->Quit();
528 void LayerTreeTest::DispatchAddAnimation(Layer
* layer_to_receive_animation
,
529 double animation_duration
) {
530 DCHECK(!proxy() || proxy()->IsMainThread());
532 if (layer_to_receive_animation
) {
533 AddOpacityTransitionToLayer(
534 layer_to_receive_animation
, animation_duration
, 0, 0.5, true);
538 void LayerTreeTest::DispatchSetNeedsCommit() {
539 DCHECK(!proxy() || proxy()->IsMainThread());
541 if (layer_tree_host_
)
542 layer_tree_host_
->SetNeedsCommit();
545 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
546 DCHECK(!proxy() || proxy()->IsMainThread());
548 if (layer_tree_host_
)
549 layer_tree_host_
->SetNeedsUpdateLayers();
552 void LayerTreeTest::DispatchReadback() {
553 DCHECK(!proxy() || proxy()->IsMainThread());
555 if (layer_tree_host_
) {
557 layer_tree_host()->CompositeAndReadback(&pixels
, gfx::Rect(0, 0, 1, 1));
561 void LayerTreeTest::DispatchSetNeedsRedraw() {
562 DCHECK(!proxy() || proxy()->IsMainThread());
564 if (layer_tree_host_
)
565 layer_tree_host_
->SetNeedsRedraw();
568 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
) {
569 DCHECK(!proxy() || proxy()->IsMainThread());
571 if (layer_tree_host_
)
572 layer_tree_host_
->SetNeedsRedrawRect(damage_rect
);
575 void LayerTreeTest::DispatchSetVisible(bool visible
) {
576 DCHECK(!proxy() || proxy()->IsMainThread());
578 if (!layer_tree_host_
)
581 layer_tree_host_
->SetVisible(visible
);
583 // If the LTH is being made visible and a previous ScheduleComposite() was
584 // deferred because the LTH was not visible, re-schedule the composite now.
585 if (layer_tree_host_
->visible() && schedule_when_set_visible_true_
)
589 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
590 DCHECK(!proxy() || proxy()->IsMainThread());
592 if (layer_tree_host_
)
593 layer_tree_host_
->SetNextCommitForcesRedraw();
596 void LayerTreeTest::DispatchComposite() {
599 if (!layer_tree_host_
)
602 // If the LTH is not visible, defer the composite until the LTH is made
604 if (!layer_tree_host_
->visible()) {
605 schedule_when_set_visible_true_
= true;
609 schedule_when_set_visible_true_
= false;
610 base::TimeTicks now
= gfx::FrameTime::Now();
611 layer_tree_host_
->Composite(now
);
614 void LayerTreeTest::RunTest(bool threaded
,
615 bool delegating_renderer
,
616 bool impl_side_painting
) {
618 impl_thread_
.reset(new base::Thread("Compositor"));
619 ASSERT_TRUE(impl_thread_
->Start());
622 main_task_runner_
= base::MessageLoopProxy::current();
624 delegating_renderer_
= delegating_renderer
;
626 // Spend less time waiting for BeginFrame because the output is
628 settings_
.refresh_rate
= 200.0;
629 if (impl_side_painting
) {
631 << "Don't run single thread + impl side painting, it doesn't exist.";
632 settings_
.impl_side_painting
= true;
634 InitializeSettings(&settings_
);
636 main_task_runner_
->PostTask(
638 base::Bind(&LayerTreeTest::DoBeginTest
, base::Unretained(this)));
640 if (timeout_seconds_
) {
641 timeout_
.Reset(base::Bind(&LayerTreeTest::Timeout
, base::Unretained(this)));
642 main_task_runner_
->PostDelayedTask(
645 base::TimeDelta::FromSeconds(timeout_seconds_
));
648 base::MessageLoop::current()->Run();
649 DestroyLayerTreeHost();
653 ASSERT_FALSE(layer_tree_host_
.get());
656 FAIL() << "Test timed out";
662 void LayerTreeTest::RunTestWithImplSidePainting() {
663 RunTest(true, false, true);
666 scoped_ptr
<OutputSurface
> LayerTreeTest::CreateOutputSurface(bool fallback
) {
667 scoped_ptr
<FakeOutputSurface
> output_surface
=
668 CreateFakeOutputSurface(fallback
);
669 output_surface_
= output_surface
.get();
670 return output_surface
.PassAs
<OutputSurface
>();
673 scoped_ptr
<FakeOutputSurface
> LayerTreeTest::CreateFakeOutputSurface(
675 if (delegating_renderer_
)
676 return FakeOutputSurface::CreateDelegating3d();
678 return FakeOutputSurface::Create3d();
681 TestWebGraphicsContext3D
* LayerTreeTest::TestContext() {
682 return static_cast<TestContextProvider
*>(
683 output_surface_
->context_provider().get())->TestContext3d();
686 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
)
688 if (impl
->pending_tree())
689 return impl
->pending_tree()->source_frame_number();
690 if (impl
->active_tree())
691 return impl
->active_tree()->source_frame_number();
692 // Source frames start at 0, so this is invalid.
696 void LayerTreeTest::DestroyLayerTreeHost() {
697 if (layer_tree_host_
&& layer_tree_host_
->root_layer())
698 layer_tree_host_
->root_layer()->SetLayerTreeHost(NULL
);
699 layer_tree_host_
.reset();