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/begin_frame_args_test.h"
19 #include "cc/test/fake_layer_tree_host_client.h"
20 #include "cc/test/fake_output_surface.h"
21 #include "cc/test/test_context_provider.h"
22 #include "cc/test/test_gpu_memory_buffer_manager.h"
23 #include "cc/test/test_shared_bitmap_manager.h"
24 #include "cc/test/tiled_layer_test_common.h"
25 #include "cc/trees/layer_tree_host_client.h"
26 #include "cc/trees/layer_tree_host_impl.h"
27 #include "cc/trees/layer_tree_host_single_thread_client.h"
28 #include "cc/trees/layer_tree_impl.h"
29 #include "cc/trees/single_thread_proxy.h"
30 #include "cc/trees/thread_proxy.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32 #include "ui/gfx/frame_time.h"
33 #include "ui/gfx/geometry/size_conversions.h"
37 TestHooks::TestHooks() {}
39 TestHooks::~TestHooks() {}
41 DrawResult
TestHooks::PrepareToDrawOnThread(
42 LayerTreeHostImpl
* host_impl
,
43 LayerTreeHostImpl::FrameData
* frame_data
,
44 DrawResult draw_result
) {
48 void TestHooks::CreateResourceAndTileTaskWorkerPool(
49 LayerTreeHostImpl
* host_impl
,
50 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
51 scoped_ptr
<ResourcePool
>* resource_pool
,
52 scoped_ptr
<ResourcePool
>* staging_resource_pool
) {
53 host_impl
->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
54 tile_task_worker_pool
, resource_pool
, staging_resource_pool
);
57 class ExternalBeginFrameSourceForTest
58 : public BeginFrameSourceMixIn
,
59 public NON_EXPORTED_BASE(base::NonThreadSafe
) {
61 explicit ExternalBeginFrameSourceForTest(double refresh_rate
)
62 : milliseconds_per_frame_(1000.0 / refresh_rate
),
64 weak_ptr_factory_(this) {
68 virtual ~ExternalBeginFrameSourceForTest() {
69 DCHECK(CalledOnValidThread());
72 void OnNeedsBeginFramesChange(bool needs_begin_frames
) override
{
73 DCHECK(CalledOnValidThread());
74 if (needs_begin_frames
) {
75 base::MessageLoop::current()->PostDelayedTask(
77 base::Bind(&ExternalBeginFrameSourceForTest::TestOnBeginFrame
,
78 weak_ptr_factory_
.GetWeakPtr()),
79 base::TimeDelta::FromMilliseconds(milliseconds_per_frame_
));
83 void SetClientReady() override
{
84 DCHECK(CalledOnValidThread());
88 bool is_ready() const {
92 void TestOnBeginFrame() {
93 DCHECK(CalledOnValidThread());
94 CallOnBeginFrame(CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
));
98 double milliseconds_per_frame_
;
100 base::WeakPtrFactory
<ExternalBeginFrameSourceForTest
> weak_ptr_factory_
;
103 // Adapts ThreadProxy for test. Injects test hooks for testing.
104 class ThreadProxyForTest
: public ThreadProxy
{
106 static scoped_ptr
<Proxy
> Create(
107 TestHooks
* test_hooks
,
109 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
110 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
111 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
112 return make_scoped_ptr(new ThreadProxyForTest(
117 external_begin_frame_source
.Pass()));
120 ~ThreadProxyForTest() override
{}
123 test_hooks_
->Layout();
127 TestHooks
* test_hooks_
;
129 void ScheduledActionSendBeginMainFrame() override
{
130 test_hooks_
->ScheduledActionWillSendBeginMainFrame();
131 ThreadProxy::ScheduledActionSendBeginMainFrame();
132 test_hooks_
->ScheduledActionSendBeginMainFrame();
135 DrawResult
ScheduledActionDrawAndSwapIfPossible() override
{
136 DrawResult result
= ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
137 test_hooks_
->ScheduledActionDrawAndSwapIfPossible();
141 void ScheduledActionAnimate() override
{
142 ThreadProxy::ScheduledActionAnimate();
143 test_hooks_
->ScheduledActionAnimate();
146 void ScheduledActionCommit() override
{
147 ThreadProxy::ScheduledActionCommit();
148 test_hooks_
->ScheduledActionCommit();
151 void ScheduledActionBeginOutputSurfaceCreation() override
{
152 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
153 test_hooks_
->ScheduledActionBeginOutputSurfaceCreation();
156 void ScheduledActionPrepareTiles() override
{
157 ThreadProxy::ScheduledActionPrepareTiles();
158 test_hooks_
->ScheduledActionPrepareTiles();
162 TestHooks
* test_hooks
,
164 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
165 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
166 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
)
167 : ThreadProxy(host
, main_task_runner
,
169 external_begin_frame_source
.Pass()),
170 test_hooks_(test_hooks
) {}
173 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
174 class LayerTreeHostImplForTesting
: public LayerTreeHostImpl
{
176 static scoped_ptr
<LayerTreeHostImplForTesting
> Create(
177 TestHooks
* test_hooks
,
178 const LayerTreeSettings
& settings
,
179 LayerTreeHostImplClient
* host_impl_client
,
181 SharedBitmapManager
* shared_bitmap_manager
,
182 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
183 RenderingStatsInstrumentation
* stats_instrumentation
) {
184 return make_scoped_ptr(
185 new LayerTreeHostImplForTesting(test_hooks
,
189 shared_bitmap_manager
,
190 gpu_memory_buffer_manager
,
191 stats_instrumentation
));
195 LayerTreeHostImplForTesting(
196 TestHooks
* test_hooks
,
197 const LayerTreeSettings
& settings
,
198 LayerTreeHostImplClient
* host_impl_client
,
200 SharedBitmapManager
* shared_bitmap_manager
,
201 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
202 RenderingStatsInstrumentation
* stats_instrumentation
)
203 : LayerTreeHostImpl(settings
,
206 stats_instrumentation
,
207 shared_bitmap_manager
,
208 gpu_memory_buffer_manager
,
210 test_hooks_(test_hooks
),
211 block_notify_ready_to_activate_for_testing_(false),
212 notify_ready_to_activate_was_blocked_(false) {}
214 void CreateResourceAndTileTaskWorkerPool(
215 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
216 scoped_ptr
<ResourcePool
>* resource_pool
,
217 scoped_ptr
<ResourcePool
>* staging_resource_pool
) override
{
218 test_hooks_
->CreateResourceAndTileTaskWorkerPool(
219 this, tile_task_worker_pool
, resource_pool
, staging_resource_pool
);
222 void WillBeginImplFrame(const BeginFrameArgs
& args
) override
{
223 LayerTreeHostImpl::WillBeginImplFrame(args
);
224 test_hooks_
->WillBeginImplFrameOnThread(this, args
);
227 void BeginMainFrameAborted(bool did_handle
) override
{
228 LayerTreeHostImpl::BeginMainFrameAborted(did_handle
);
229 test_hooks_
->BeginMainFrameAbortedOnThread(this, did_handle
);
232 void BeginCommit() override
{
233 LayerTreeHostImpl::BeginCommit();
234 test_hooks_
->BeginCommitOnThread(this);
237 void CommitComplete() override
{
238 LayerTreeHostImpl::CommitComplete();
239 test_hooks_
->CommitCompleteOnThread(this);
242 DrawResult
PrepareToDraw(FrameData
* frame
) override
{
243 DrawResult draw_result
= LayerTreeHostImpl::PrepareToDraw(frame
);
244 return test_hooks_
->PrepareToDrawOnThread(this, frame
, draw_result
);
247 void DrawLayers(FrameData
* frame
, base::TimeTicks frame_begin_time
) override
{
248 LayerTreeHostImpl::DrawLayers(frame
, frame_begin_time
);
249 test_hooks_
->DrawLayersOnThread(this);
252 bool SwapBuffers(const LayerTreeHostImpl::FrameData
& frame
) override
{
253 bool result
= LayerTreeHostImpl::SwapBuffers(frame
);
254 test_hooks_
->SwapBuffersOnThread(this, result
);
258 void DidSwapBuffersComplete() override
{
259 LayerTreeHostImpl::DidSwapBuffersComplete();
260 test_hooks_
->SwapBuffersCompleteOnThread(this);
263 void ReclaimResources(const CompositorFrameAck
* ack
) override
{
264 LayerTreeHostImpl::ReclaimResources(ack
);
267 void NotifyReadyToActivate() override
{
268 if (block_notify_ready_to_activate_for_testing_
) {
269 notify_ready_to_activate_was_blocked_
= true;
271 LayerTreeHostImpl::NotifyReadyToActivate();
272 test_hooks_
->NotifyReadyToActivateOnThread(this);
276 void NotifyReadyToDraw() override
{
277 LayerTreeHostImpl::NotifyReadyToDraw();
278 test_hooks_
->NotifyReadyToDrawOnThread(this);
281 void BlockNotifyReadyToActivateForTesting(bool block
) override
{
282 CHECK(settings().impl_side_painting
);
283 CHECK(proxy()->ImplThreadTaskRunner())
284 << "Not supported for single-threaded mode.";
285 block_notify_ready_to_activate_for_testing_
= block
;
286 if (!block
&& notify_ready_to_activate_was_blocked_
) {
287 NotifyReadyToActivate();
288 notify_ready_to_activate_was_blocked_
= false;
292 void ActivateSyncTree() override
{
293 test_hooks_
->WillActivateTreeOnThread(this);
294 LayerTreeHostImpl::ActivateSyncTree();
295 DCHECK(!pending_tree());
296 test_hooks_
->DidActivateTreeOnThread(this);
299 bool InitializeRenderer(scoped_ptr
<OutputSurface
> output_surface
) override
{
300 bool success
= LayerTreeHostImpl::InitializeRenderer(output_surface
.Pass());
301 test_hooks_
->InitializedRendererOnThread(this, success
);
305 void SetVisible(bool visible
) override
{
306 LayerTreeHostImpl::SetVisible(visible
);
307 test_hooks_
->DidSetVisibleOnImplTree(this, visible
);
310 void AnimateLayers(base::TimeTicks monotonic_time
) override
{
311 test_hooks_
->WillAnimateLayers(this, monotonic_time
);
312 LayerTreeHostImpl::AnimateLayers(monotonic_time
);
313 test_hooks_
->AnimateLayers(this, monotonic_time
);
316 void UpdateAnimationState(bool start_ready_animations
) override
{
317 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations
);
318 bool has_unfinished_animation
= false;
319 AnimationRegistrar::AnimationControllerMap::const_iterator iter
=
320 active_animation_controllers().begin();
321 for (; iter
!= active_animation_controllers().end(); ++iter
) {
322 if (iter
->second
->HasActiveAnimation()) {
323 has_unfinished_animation
= true;
327 test_hooks_
->UpdateAnimationState(this, has_unfinished_animation
);
330 void NotifyTileStateChanged(const Tile
* tile
) override
{
331 LayerTreeHostImpl::NotifyTileStateChanged(tile
);
332 test_hooks_
->NotifyTileStateChangedOnThread(this, tile
);
336 TestHooks
* test_hooks_
;
337 bool block_notify_ready_to_activate_for_testing_
;
338 bool notify_ready_to_activate_was_blocked_
;
341 // Implementation of LayerTreeHost callback interface.
342 class LayerTreeHostClientForTesting
: public LayerTreeHostClient
,
343 public LayerTreeHostSingleThreadClient
{
345 static scoped_ptr
<LayerTreeHostClientForTesting
> Create(
346 TestHooks
* test_hooks
) {
347 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks
));
349 ~LayerTreeHostClientForTesting() override
{}
351 void WillBeginMainFrame(int frame_id
) override
{
352 test_hooks_
->WillBeginMainFrame();
355 void DidBeginMainFrame() override
{ test_hooks_
->DidBeginMainFrame(); }
357 void BeginMainFrame(const BeginFrameArgs
& args
) override
{
358 test_hooks_
->BeginMainFrame(args
);
361 void Layout() override
{ test_hooks_
->Layout(); }
363 void ApplyViewportDeltas(const gfx::Vector2d
& inner_delta
,
364 const gfx::Vector2d
& outer_delta
,
365 const gfx::Vector2dF
& elastic_overscroll_delta
,
367 float top_controls_delta
) override
{
368 test_hooks_
->ApplyViewportDeltas(inner_delta
, outer_delta
,
369 elastic_overscroll_delta
, page_scale
,
372 void ApplyViewportDeltas(const gfx::Vector2d
& scroll_delta
,
374 float top_controls_delta
) override
{
375 test_hooks_
->ApplyViewportDeltas(scroll_delta
,
380 void RequestNewOutputSurface() override
{
381 test_hooks_
->RequestNewOutputSurface();
384 void DidInitializeOutputSurface() override
{
385 test_hooks_
->DidInitializeOutputSurface();
388 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) override
{
389 test_hooks_
->SendBeginFramesToChildren(args
);
392 void DidFailToInitializeOutputSurface() override
{
393 test_hooks_
->DidFailToInitializeOutputSurface();
394 RequestNewOutputSurface();
397 void WillCommit() override
{ test_hooks_
->WillCommit(); }
399 void DidCommit() override
{ test_hooks_
->DidCommit(); }
401 void DidCommitAndDrawFrame() override
{
402 test_hooks_
->DidCommitAndDrawFrame();
405 void DidCompleteSwapBuffers() override
{
406 test_hooks_
->DidCompleteSwapBuffers();
409 void DidPostSwapBuffers() override
{}
410 void DidAbortSwapBuffers() override
{}
411 void ScheduleComposite() override
{ test_hooks_
->ScheduleComposite(); }
414 explicit LayerTreeHostClientForTesting(TestHooks
* test_hooks
)
415 : test_hooks_(test_hooks
) {}
417 TestHooks
* test_hooks_
;
420 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
421 class LayerTreeHostForTesting
: public LayerTreeHost
{
423 static scoped_ptr
<LayerTreeHostForTesting
> Create(
424 TestHooks
* test_hooks
,
425 LayerTreeHostClientForTesting
* client
,
426 const LayerTreeSettings
& settings
,
427 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
428 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
429 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
430 scoped_ptr
<LayerTreeHostForTesting
> layer_tree_host(
431 new LayerTreeHostForTesting(test_hooks
, client
, settings
));
432 if (impl_task_runner
.get()) {
433 layer_tree_host
->InitializeForTesting(
434 ThreadProxyForTest::Create(test_hooks
,
435 layer_tree_host
.get(),
438 external_begin_frame_source
.Pass()));
440 layer_tree_host
->InitializeForTesting(SingleThreadProxy::Create(
441 layer_tree_host
.get(),
444 external_begin_frame_source
.Pass()));
446 return layer_tree_host
.Pass();
449 scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
450 LayerTreeHostImplClient
* host_impl_client
) override
{
451 return LayerTreeHostImplForTesting::Create(
456 shared_bitmap_manager_
.get(),
457 gpu_memory_buffer_manager_
.get(),
458 rendering_stats_instrumentation());
461 void SetNeedsCommit() override
{
464 LayerTreeHost::SetNeedsCommit();
467 void set_test_started(bool started
) { test_started_
= started
; }
469 void DidDeferCommit() override
{ test_hooks_
->DidDeferCommit(); }
472 LayerTreeHostForTesting(TestHooks
* test_hooks
,
473 LayerTreeHostClient
* client
,
474 const LayerTreeSettings
& settings
)
475 : LayerTreeHost(client
, NULL
, NULL
, settings
),
476 shared_bitmap_manager_(new TestSharedBitmapManager
),
477 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager
),
478 test_hooks_(test_hooks
),
479 test_started_(false) {}
481 scoped_ptr
<TestSharedBitmapManager
> shared_bitmap_manager_
;
482 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
483 TestHooks
* test_hooks_
;
487 LayerTreeTest::LayerTreeTest()
488 : output_surface_(nullptr),
489 external_begin_frame_source_(nullptr),
491 end_when_begin_returns_(false),
496 delegating_renderer_(false),
498 weak_factory_(this) {
499 main_thread_weak_ptr_
= weak_factory_
.GetWeakPtr();
501 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
502 // specified (for running in a debugger).
503 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
504 if (!command_line
->HasSwitch(switches::kCCLayerTreeTestNoTimeout
))
505 timeout_seconds_
= 5;
508 LayerTreeTest::~LayerTreeTest() {}
510 void LayerTreeTest::EndTest() {
515 // For the case where we EndTest during BeginTest(), set a flag to indicate
516 // that the test should end the second BeginTest regains control.
518 end_when_begin_returns_
= true;
520 main_task_runner_
->PostTask(
522 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
526 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds
) {
527 main_task_runner_
->PostDelayedTask(
529 base::Bind(&LayerTreeTest::EndTest
, main_thread_weak_ptr_
),
530 base::TimeDelta::FromMilliseconds(delay_milliseconds
));
533 void LayerTreeTest::PostAddAnimationToMainThread(
534 Layer
* layer_to_receive_animation
) {
535 main_task_runner_
->PostTask(
537 base::Bind(&LayerTreeTest::DispatchAddAnimation
, main_thread_weak_ptr_
,
538 base::Unretained(layer_to_receive_animation
), 0.000004));
541 void LayerTreeTest::PostAddInstantAnimationToMainThread(
542 Layer
* layer_to_receive_animation
) {
543 main_task_runner_
->PostTask(
545 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
546 main_thread_weak_ptr_
,
547 base::Unretained(layer_to_receive_animation
),
551 void LayerTreeTest::PostAddLongAnimationToMainThread(
552 Layer
* layer_to_receive_animation
) {
553 main_task_runner_
->PostTask(
555 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
556 main_thread_weak_ptr_
,
557 base::Unretained(layer_to_receive_animation
),
561 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
562 main_task_runner_
->PostTask(FROM_HERE
,
563 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit
,
564 main_thread_weak_ptr_
));
567 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
568 main_task_runner_
->PostTask(
570 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers
,
571 main_thread_weak_ptr_
));
574 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
575 main_task_runner_
->PostTask(FROM_HERE
,
576 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw
,
577 main_thread_weak_ptr_
));
580 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
581 const gfx::Rect
& damage_rect
) {
582 main_task_runner_
->PostTask(
584 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect
,
585 main_thread_weak_ptr_
,
589 void LayerTreeTest::PostSetVisibleToMainThread(bool visible
) {
590 main_task_runner_
->PostTask(
593 &LayerTreeTest::DispatchSetVisible
, main_thread_weak_ptr_
, visible
));
596 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
597 main_task_runner_
->PostTask(
599 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw
,
600 main_thread_weak_ptr_
));
603 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
604 main_task_runner_
->PostTask(
606 base::Bind(&LayerTreeTest::DispatchCompositeImmediately
,
607 main_thread_weak_ptr_
));
610 void LayerTreeTest::WillBeginTest() {
611 layer_tree_host_
->SetLayerTreeHostClientReady();
614 void LayerTreeTest::DoBeginTest() {
615 client_
= LayerTreeHostClientForTesting::Create(this);
617 scoped_ptr
<ExternalBeginFrameSourceForTest
> external_begin_frame_source
;
618 if (settings_
.use_external_begin_frame_source
&&
619 settings_
.throttle_frame_production
) {
620 external_begin_frame_source
.reset(new ExternalBeginFrameSourceForTest(
621 settings_
.renderer_settings
.refresh_rate
));
622 external_begin_frame_source_
= external_begin_frame_source
.get();
625 DCHECK(!impl_thread_
|| impl_thread_
->message_loop_proxy().get());
626 layer_tree_host_
= LayerTreeHostForTesting::Create(
630 base::MessageLoopProxy::current(),
631 impl_thread_
? impl_thread_
->message_loop_proxy() : NULL
,
632 external_begin_frame_source
.Pass());
633 ASSERT_TRUE(layer_tree_host_
);
641 if (end_when_begin_returns_
)
644 // Allow commits to happen once BeginTest() has had a chance to post tasks
645 // so that those tasks will happen before the first commit.
646 if (layer_tree_host_
) {
647 static_cast<LayerTreeHostForTesting
*>(layer_tree_host_
.get())
648 ->set_test_started(true);
652 void LayerTreeTest::SetupTree() {
653 if (!layer_tree_host_
->root_layer()) {
654 scoped_refptr
<Layer
> root_layer
= Layer::Create();
655 root_layer
->SetBounds(gfx::Size(1, 1));
656 root_layer
->SetIsDrawable(true);
657 layer_tree_host_
->SetRootLayer(root_layer
);
660 gfx::Size root_bounds
= layer_tree_host_
->root_layer()->bounds();
661 gfx::Size device_root_bounds
= gfx::ToCeiledSize(
662 gfx::ScaleSize(root_bounds
, layer_tree_host_
->device_scale_factor()));
663 layer_tree_host_
->SetViewportSize(device_root_bounds
);
666 void LayerTreeTest::Timeout() {
671 void LayerTreeTest::RealEndTest() {
672 if (layer_tree_host_
&& !timed_out_
&&
673 proxy()->MainFrameWillHappenForTesting()) {
674 main_task_runner_
->PostTask(
676 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
680 base::MessageLoop::current()->Quit();
683 void LayerTreeTest::DispatchAddAnimation(Layer
* layer_to_receive_animation
,
684 double animation_duration
) {
685 DCHECK(!proxy() || proxy()->IsMainThread());
687 if (layer_to_receive_animation
) {
688 AddOpacityTransitionToLayer(
689 layer_to_receive_animation
, animation_duration
, 0, 0.5, true);
693 void LayerTreeTest::DispatchSetNeedsCommit() {
694 DCHECK(!proxy() || proxy()->IsMainThread());
696 if (layer_tree_host_
)
697 layer_tree_host_
->SetNeedsCommit();
700 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
701 DCHECK(!proxy() || proxy()->IsMainThread());
703 if (layer_tree_host_
)
704 layer_tree_host_
->SetNeedsUpdateLayers();
707 void LayerTreeTest::DispatchSetNeedsRedraw() {
708 DCHECK(!proxy() || proxy()->IsMainThread());
710 if (layer_tree_host_
)
711 layer_tree_host_
->SetNeedsRedraw();
714 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
) {
715 DCHECK(!proxy() || proxy()->IsMainThread());
717 if (layer_tree_host_
)
718 layer_tree_host_
->SetNeedsRedrawRect(damage_rect
);
721 void LayerTreeTest::DispatchSetVisible(bool visible
) {
722 DCHECK(!proxy() || proxy()->IsMainThread());
723 if (layer_tree_host_
)
724 layer_tree_host_
->SetVisible(visible
);
727 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
728 DCHECK(!proxy() || proxy()->IsMainThread());
730 if (layer_tree_host_
)
731 layer_tree_host_
->SetNextCommitForcesRedraw();
734 void LayerTreeTest::DispatchCompositeImmediately() {
735 DCHECK(!proxy() || proxy()->IsMainThread());
736 if (layer_tree_host_
)
737 layer_tree_host_
->Composite(gfx::FrameTime::Now());
740 void LayerTreeTest::RunTest(bool threaded
,
741 bool delegating_renderer
,
742 bool impl_side_painting
) {
744 impl_thread_
.reset(new base::Thread("Compositor"));
745 ASSERT_TRUE(impl_thread_
->Start());
748 main_task_runner_
= base::MessageLoopProxy::current();
750 delegating_renderer_
= delegating_renderer
;
752 // Spend less time waiting for BeginFrame because the output is
754 settings_
.renderer_settings
.refresh_rate
= 200.0;
755 settings_
.background_animation_rate
= 200.0;
756 settings_
.impl_side_painting
= impl_side_painting
;
757 InitializeSettings(&settings_
);
759 main_task_runner_
->PostTask(
761 base::Bind(&LayerTreeTest::DoBeginTest
, base::Unretained(this)));
763 if (timeout_seconds_
) {
764 timeout_
.Reset(base::Bind(&LayerTreeTest::Timeout
, base::Unretained(this)));
765 main_task_runner_
->PostDelayedTask(
768 base::TimeDelta::FromSeconds(timeout_seconds_
));
771 base::MessageLoop::current()->Run();
772 DestroyLayerTreeHost();
776 ASSERT_FALSE(layer_tree_host_
.get());
779 FAIL() << "Test timed out";
785 void LayerTreeTest::RunTestWithImplSidePainting() {
786 RunTest(true, false, true);
789 void LayerTreeTest::RequestNewOutputSurface() {
790 layer_tree_host_
->SetOutputSurface(CreateOutputSurface());
793 scoped_ptr
<OutputSurface
> LayerTreeTest::CreateOutputSurface() {
794 scoped_ptr
<FakeOutputSurface
> output_surface
= CreateFakeOutputSurface();
795 DCHECK_EQ(delegating_renderer_
,
796 output_surface
->capabilities().delegated_rendering
);
797 output_surface_
= output_surface
.get();
799 if (settings_
.use_external_begin_frame_source
&&
800 settings_
.throttle_frame_production
) {
801 DCHECK(external_begin_frame_source_
);
802 DCHECK(external_begin_frame_source_
->is_ready());
804 return output_surface
.Pass();
807 scoped_ptr
<FakeOutputSurface
> LayerTreeTest::CreateFakeOutputSurface() {
808 if (delegating_renderer_
)
809 return FakeOutputSurface::CreateDelegating3d();
811 return FakeOutputSurface::Create3d();
814 TestWebGraphicsContext3D
* LayerTreeTest::TestContext() {
815 return static_cast<TestContextProvider
*>(output_surface_
->context_provider())
819 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
)
821 if (impl
->pending_tree())
822 return impl
->pending_tree()->source_frame_number();
823 if (impl
->active_tree())
824 return impl
->active_tree()->source_frame_number();
825 // Source frames start at 0, so this is invalid.
829 void LayerTreeTest::DestroyLayerTreeHost() {
830 if (layer_tree_host_
&& layer_tree_host_
->root_layer())
831 layer_tree_host_
->root_layer()->SetLayerTreeHost(NULL
);
832 layer_tree_host_
= nullptr;