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"
40 TestHooks::TestHooks() {}
42 TestHooks::~TestHooks() {}
44 DrawResult
TestHooks::PrepareToDrawOnThread(
45 LayerTreeHostImpl
* host_impl
,
46 LayerTreeHostImpl::FrameData
* frame_data
,
47 DrawResult draw_result
) {
51 void TestHooks::CreateResourceAndTileTaskWorkerPool(
52 LayerTreeHostImpl
* host_impl
,
53 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
54 scoped_ptr
<ResourcePool
>* resource_pool
,
55 scoped_ptr
<ResourcePool
>* staging_resource_pool
) {
56 host_impl
->LayerTreeHostImpl::CreateResourceAndTileTaskWorkerPool(
57 tile_task_worker_pool
, resource_pool
, staging_resource_pool
);
60 // Adapts ThreadProxy for test. Injects test hooks for testing.
61 class ThreadProxyForTest
: public ThreadProxy
{
63 static scoped_ptr
<Proxy
> Create(
64 TestHooks
* test_hooks
,
66 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
67 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
68 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
69 return make_scoped_ptr(new ThreadProxyForTest(
74 external_begin_frame_source
.Pass()));
77 ~ThreadProxyForTest() override
{}
80 TestHooks
* test_hooks_
;
82 void ScheduledActionSendBeginMainFrame() override
{
83 test_hooks_
->ScheduledActionWillSendBeginMainFrame();
84 ThreadProxy::ScheduledActionSendBeginMainFrame();
85 test_hooks_
->ScheduledActionSendBeginMainFrame();
88 DrawResult
ScheduledActionDrawAndSwapIfPossible() override
{
89 DrawResult result
= ThreadProxy::ScheduledActionDrawAndSwapIfPossible();
90 test_hooks_
->ScheduledActionDrawAndSwapIfPossible();
94 void ScheduledActionAnimate() override
{
95 ThreadProxy::ScheduledActionAnimate();
96 test_hooks_
->ScheduledActionAnimate();
99 void ScheduledActionCommit() override
{
100 ThreadProxy::ScheduledActionCommit();
101 test_hooks_
->ScheduledActionCommit();
104 void ScheduledActionBeginOutputSurfaceCreation() override
{
105 ThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
106 test_hooks_
->ScheduledActionBeginOutputSurfaceCreation();
109 void ScheduledActionPrepareTiles() override
{
110 ThreadProxy::ScheduledActionPrepareTiles();
111 test_hooks_
->ScheduledActionPrepareTiles();
114 void ScheduledActionInvalidateOutputSurface() override
{
115 ThreadProxy::ScheduledActionInvalidateOutputSurface();
116 test_hooks_
->ScheduledActionInvalidateOutputSurface();
119 void SendBeginMainFrameNotExpectedSoon() override
{
120 ThreadProxy::SendBeginMainFrameNotExpectedSoon();
121 test_hooks_
->SendBeginMainFrameNotExpectedSoon();
125 TestHooks
* test_hooks
,
127 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
128 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
129 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
)
130 : ThreadProxy(host
, main_task_runner
,
132 external_begin_frame_source
.Pass()),
133 test_hooks_(test_hooks
) {}
136 // Adapts ThreadProxy for test. Injects test hooks for testing.
137 class SingleThreadProxyForTest
: public SingleThreadProxy
{
139 static scoped_ptr
<Proxy
> Create(
140 TestHooks
* test_hooks
,
142 LayerTreeHostSingleThreadClient
* client
,
143 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
144 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
145 return make_scoped_ptr(new SingleThreadProxyForTest(
146 test_hooks
, host
, client
, main_task_runner
,
147 external_begin_frame_source
.Pass()));
150 ~SingleThreadProxyForTest() override
{}
153 TestHooks
* test_hooks_
;
155 void ScheduledActionSendBeginMainFrame() override
{
156 test_hooks_
->ScheduledActionWillSendBeginMainFrame();
157 SingleThreadProxy::ScheduledActionSendBeginMainFrame();
158 test_hooks_
->ScheduledActionSendBeginMainFrame();
161 DrawResult
ScheduledActionDrawAndSwapIfPossible() override
{
163 SingleThreadProxy::ScheduledActionDrawAndSwapIfPossible();
164 test_hooks_
->ScheduledActionDrawAndSwapIfPossible();
168 void ScheduledActionAnimate() override
{
169 SingleThreadProxy::ScheduledActionAnimate();
170 test_hooks_
->ScheduledActionAnimate();
173 void ScheduledActionCommit() override
{
174 SingleThreadProxy::ScheduledActionCommit();
175 test_hooks_
->ScheduledActionCommit();
178 void ScheduledActionBeginOutputSurfaceCreation() override
{
179 SingleThreadProxy::ScheduledActionBeginOutputSurfaceCreation();
180 test_hooks_
->ScheduledActionBeginOutputSurfaceCreation();
183 void ScheduledActionPrepareTiles() override
{
184 SingleThreadProxy::ScheduledActionPrepareTiles();
185 test_hooks_
->ScheduledActionPrepareTiles();
188 void ScheduledActionInvalidateOutputSurface() override
{
189 SingleThreadProxy::ScheduledActionInvalidateOutputSurface();
190 test_hooks_
->ScheduledActionInvalidateOutputSurface();
193 void SendBeginMainFrameNotExpectedSoon() override
{
194 SingleThreadProxy::SendBeginMainFrameNotExpectedSoon();
195 test_hooks_
->SendBeginMainFrameNotExpectedSoon();
198 SingleThreadProxyForTest(
199 TestHooks
* test_hooks
,
201 LayerTreeHostSingleThreadClient
* client
,
202 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
203 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
)
204 : SingleThreadProxy(host
, client
, main_task_runner
,
205 external_begin_frame_source
.Pass()),
206 test_hooks_(test_hooks
) {}
209 // Adapts LayerTreeHostImpl for test. Runs real code, then invokes test hooks.
210 class LayerTreeHostImplForTesting
: public LayerTreeHostImpl
{
212 static scoped_ptr
<LayerTreeHostImplForTesting
> Create(
213 TestHooks
* test_hooks
,
214 const LayerTreeSettings
& settings
,
215 LayerTreeHostImplClient
* host_impl_client
,
217 SharedBitmapManager
* shared_bitmap_manager
,
218 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
219 TaskGraphRunner
* task_graph_runner
,
220 RenderingStatsInstrumentation
* stats_instrumentation
) {
221 return make_scoped_ptr(new LayerTreeHostImplForTesting(
222 test_hooks
, settings
, host_impl_client
, proxy
, shared_bitmap_manager
,
223 gpu_memory_buffer_manager
, task_graph_runner
, stats_instrumentation
));
227 LayerTreeHostImplForTesting(
228 TestHooks
* test_hooks
,
229 const LayerTreeSettings
& settings
,
230 LayerTreeHostImplClient
* host_impl_client
,
232 SharedBitmapManager
* shared_bitmap_manager
,
233 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
234 TaskGraphRunner
* task_graph_runner
,
235 RenderingStatsInstrumentation
* stats_instrumentation
)
236 : LayerTreeHostImpl(settings
,
239 stats_instrumentation
,
240 shared_bitmap_manager
,
241 gpu_memory_buffer_manager
,
244 test_hooks_(test_hooks
),
245 block_notify_ready_to_activate_for_testing_(false),
246 notify_ready_to_activate_was_blocked_(false) {}
248 void CreateResourceAndTileTaskWorkerPool(
249 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
250 scoped_ptr
<ResourcePool
>* resource_pool
,
251 scoped_ptr
<ResourcePool
>* staging_resource_pool
) override
{
252 test_hooks_
->CreateResourceAndTileTaskWorkerPool(
253 this, tile_task_worker_pool
, resource_pool
, staging_resource_pool
);
256 void WillBeginImplFrame(const BeginFrameArgs
& args
) override
{
257 LayerTreeHostImpl::WillBeginImplFrame(args
);
258 test_hooks_
->WillBeginImplFrameOnThread(this, args
);
261 void DidFinishImplFrame() override
{
262 LayerTreeHostImpl::DidFinishImplFrame();
263 test_hooks_
->DidFinishImplFrameOnThread(this);
266 void BeginMainFrameAborted(CommitEarlyOutReason reason
) override
{
267 LayerTreeHostImpl::BeginMainFrameAborted(reason
);
268 test_hooks_
->BeginMainFrameAbortedOnThread(this, reason
);
271 void BeginCommit() override
{
272 LayerTreeHostImpl::BeginCommit();
273 test_hooks_
->BeginCommitOnThread(this);
276 void CommitComplete() override
{
277 LayerTreeHostImpl::CommitComplete();
278 test_hooks_
->CommitCompleteOnThread(this);
281 DrawResult
PrepareToDraw(FrameData
* frame
) override
{
282 DrawResult draw_result
= LayerTreeHostImpl::PrepareToDraw(frame
);
283 return test_hooks_
->PrepareToDrawOnThread(this, frame
, draw_result
);
286 void DrawLayers(FrameData
* frame
) override
{
287 LayerTreeHostImpl::DrawLayers(frame
);
288 test_hooks_
->DrawLayersOnThread(this);
291 bool SwapBuffers(const LayerTreeHostImpl::FrameData
& frame
) override
{
292 bool result
= LayerTreeHostImpl::SwapBuffers(frame
);
293 test_hooks_
->SwapBuffersOnThread(this, result
);
297 void DidSwapBuffersComplete() override
{
298 LayerTreeHostImpl::DidSwapBuffersComplete();
299 test_hooks_
->SwapBuffersCompleteOnThread(this);
302 void ReclaimResources(const CompositorFrameAck
* ack
) override
{
303 LayerTreeHostImpl::ReclaimResources(ack
);
306 void NotifyReadyToActivate() override
{
307 if (block_notify_ready_to_activate_for_testing_
) {
308 notify_ready_to_activate_was_blocked_
= true;
310 LayerTreeHostImpl::NotifyReadyToActivate();
311 test_hooks_
->NotifyReadyToActivateOnThread(this);
315 void NotifyReadyToDraw() override
{
316 LayerTreeHostImpl::NotifyReadyToDraw();
317 test_hooks_
->NotifyReadyToDrawOnThread(this);
320 void NotifyAllTileTasksCompleted() override
{
321 LayerTreeHostImpl::NotifyAllTileTasksCompleted();
322 test_hooks_
->NotifyAllTileTasksCompleted(this);
325 void BlockNotifyReadyToActivateForTesting(bool block
) override
{
326 CHECK(proxy()->ImplThreadTaskRunner())
327 << "Not supported for single-threaded mode.";
328 block_notify_ready_to_activate_for_testing_
= block
;
329 if (!block
&& notify_ready_to_activate_was_blocked_
) {
330 NotifyReadyToActivate();
331 notify_ready_to_activate_was_blocked_
= false;
335 void ActivateSyncTree() override
{
336 test_hooks_
->WillActivateTreeOnThread(this);
337 LayerTreeHostImpl::ActivateSyncTree();
338 DCHECK(!pending_tree());
339 test_hooks_
->DidActivateTreeOnThread(this);
342 bool InitializeRenderer(scoped_ptr
<OutputSurface
> output_surface
) override
{
343 bool success
= LayerTreeHostImpl::InitializeRenderer(output_surface
.Pass());
344 test_hooks_
->InitializedRendererOnThread(this, success
);
348 void SetVisible(bool visible
) override
{
349 LayerTreeHostImpl::SetVisible(visible
);
350 test_hooks_
->DidSetVisibleOnImplTree(this, visible
);
353 void AnimateLayers(base::TimeTicks monotonic_time
) override
{
354 test_hooks_
->WillAnimateLayers(this, monotonic_time
);
355 LayerTreeHostImpl::AnimateLayers(monotonic_time
);
356 test_hooks_
->AnimateLayers(this, monotonic_time
);
359 void UpdateAnimationState(bool start_ready_animations
) override
{
360 LayerTreeHostImpl::UpdateAnimationState(start_ready_animations
);
361 bool has_unfinished_animation
= false;
362 AnimationRegistrar
* registrar
=
363 animation_registrar() ? animation_registrar()
364 : animation_host()->animation_registrar();
365 for (const auto& it
:
366 registrar
->active_animation_controllers_for_testing()) {
367 if (it
.second
->HasActiveAnimation()) {
368 has_unfinished_animation
= true;
372 test_hooks_
->UpdateAnimationState(this, has_unfinished_animation
);
375 void NotifyTileStateChanged(const Tile
* tile
) override
{
376 LayerTreeHostImpl::NotifyTileStateChanged(tile
);
377 test_hooks_
->NotifyTileStateChangedOnThread(this, tile
);
381 TestHooks
* test_hooks_
;
382 bool block_notify_ready_to_activate_for_testing_
;
383 bool notify_ready_to_activate_was_blocked_
;
386 // Implementation of LayerTreeHost callback interface.
387 class LayerTreeHostClientForTesting
: public LayerTreeHostClient
,
388 public LayerTreeHostSingleThreadClient
{
390 static scoped_ptr
<LayerTreeHostClientForTesting
> Create(
391 TestHooks
* test_hooks
) {
392 return make_scoped_ptr(new LayerTreeHostClientForTesting(test_hooks
));
394 ~LayerTreeHostClientForTesting() override
{}
396 void WillBeginMainFrame() override
{ test_hooks_
->WillBeginMainFrame(); }
398 void DidBeginMainFrame() override
{ test_hooks_
->DidBeginMainFrame(); }
400 void BeginMainFrame(const BeginFrameArgs
& args
) override
{
401 test_hooks_
->BeginMainFrame(args
);
404 void Layout() override
{ test_hooks_
->Layout(); }
406 void ApplyViewportDeltas(const gfx::Vector2dF
& inner_delta
,
407 const gfx::Vector2dF
& outer_delta
,
408 const gfx::Vector2dF
& elastic_overscroll_delta
,
410 float top_controls_delta
) override
{
411 test_hooks_
->ApplyViewportDeltas(inner_delta
, outer_delta
,
412 elastic_overscroll_delta
, page_scale
,
416 void RequestNewOutputSurface() override
{
417 test_hooks_
->RequestNewOutputSurface();
420 void DidInitializeOutputSurface() override
{
421 test_hooks_
->DidInitializeOutputSurface();
424 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) override
{
425 test_hooks_
->SendBeginFramesToChildren(args
);
428 void DidFailToInitializeOutputSurface() override
{
429 test_hooks_
->DidFailToInitializeOutputSurface();
430 RequestNewOutputSurface();
433 void WillCommit() override
{ test_hooks_
->WillCommit(); }
435 void DidCommit() override
{ test_hooks_
->DidCommit(); }
437 void DidCommitAndDrawFrame() override
{
438 test_hooks_
->DidCommitAndDrawFrame();
441 void DidCompleteSwapBuffers() override
{
442 test_hooks_
->DidCompleteSwapBuffers();
445 void DidPostSwapBuffers() override
{}
446 void DidAbortSwapBuffers() override
{}
447 void ScheduleComposite() override
{ test_hooks_
->ScheduleComposite(); }
448 void DidCompletePageScaleAnimation() override
{}
449 void BeginMainFrameNotExpectedSoon() override
{}
451 void RecordFrameTimingEvents(
452 scoped_ptr
<FrameTimingTracker::CompositeTimingSet
> composite_events
,
453 scoped_ptr
<FrameTimingTracker::MainFrameTimingSet
> main_frame_events
)
457 explicit LayerTreeHostClientForTesting(TestHooks
* test_hooks
)
458 : test_hooks_(test_hooks
) {}
460 TestHooks
* test_hooks_
;
463 // Adapts LayerTreeHost for test. Injects LayerTreeHostImplForTesting.
464 class LayerTreeHostForTesting
: public LayerTreeHost
{
466 static scoped_ptr
<LayerTreeHostForTesting
> Create(
467 TestHooks
* test_hooks
,
468 LayerTreeHostClientForTesting
* client
,
469 SharedBitmapManager
* shared_bitmap_manager
,
470 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
,
471 TaskGraphRunner
* task_graph_runner
,
472 const LayerTreeSettings
& settings
,
473 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
474 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
475 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
476 LayerTreeHost::InitParams params
;
477 params
.client
= client
;
478 params
.shared_bitmap_manager
= shared_bitmap_manager
;
479 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager
;
480 params
.task_graph_runner
= task_graph_runner
;
481 params
.settings
= &settings
;
482 scoped_ptr
<LayerTreeHostForTesting
> layer_tree_host(
483 new LayerTreeHostForTesting(test_hooks
, ¶ms
));
484 if (impl_task_runner
.get()) {
485 layer_tree_host
->InitializeForTesting(
486 ThreadProxyForTest::Create(test_hooks
,
487 layer_tree_host
.get(),
490 external_begin_frame_source
.Pass()));
492 layer_tree_host
->InitializeForTesting(
493 SingleThreadProxyForTest::Create(
495 layer_tree_host
.get(),
498 external_begin_frame_source
.Pass()));
500 return layer_tree_host
.Pass();
503 scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
504 LayerTreeHostImplClient
* host_impl_client
) override
{
505 return LayerTreeHostImplForTesting::Create(
506 test_hooks_
, settings(), host_impl_client
, proxy(),
507 shared_bitmap_manager(), gpu_memory_buffer_manager(),
508 task_graph_runner(), rendering_stats_instrumentation());
511 void SetNeedsCommit() override
{
514 LayerTreeHost::SetNeedsCommit();
517 void set_test_started(bool started
) { test_started_
= started
; }
520 LayerTreeHostForTesting(TestHooks
* test_hooks
,
521 LayerTreeHost::InitParams
* params
)
522 : LayerTreeHost(params
), test_hooks_(test_hooks
), test_started_(false) {}
524 TestHooks
* test_hooks_
;
528 LayerTreeTest::LayerTreeTest()
529 : output_surface_(nullptr),
530 external_begin_frame_source_(nullptr),
532 end_when_begin_returns_(false),
537 delegating_renderer_(false),
538 verify_property_trees_(true),
540 weak_factory_(this) {
541 main_thread_weak_ptr_
= weak_factory_
.GetWeakPtr();
543 // Tests should timeout quickly unless --cc-layer-tree-test-no-timeout was
544 // specified (for running in a debugger).
545 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
546 if (!command_line
->HasSwitch(switches::kCCLayerTreeTestNoTimeout
))
547 timeout_seconds_
= 5;
550 LayerTreeTest::~LayerTreeTest() {}
552 void LayerTreeTest::EndTest() {
557 // For the case where we EndTest during BeginTest(), set a flag to indicate
558 // that the test should end the second BeginTest regains control.
560 end_when_begin_returns_
= true;
562 main_task_runner_
->PostTask(
564 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
568 void LayerTreeTest::EndTestAfterDelayMs(int delay_milliseconds
) {
569 main_task_runner_
->PostDelayedTask(
571 base::Bind(&LayerTreeTest::EndTest
, main_thread_weak_ptr_
),
572 base::TimeDelta::FromMilliseconds(delay_milliseconds
));
575 void LayerTreeTest::PostAddAnimationToMainThread(
576 Layer
* layer_to_receive_animation
) {
577 main_task_runner_
->PostTask(
579 base::Bind(&LayerTreeTest::DispatchAddAnimation
, main_thread_weak_ptr_
,
580 base::Unretained(layer_to_receive_animation
), 0.000004));
583 void LayerTreeTest::PostAddInstantAnimationToMainThread(
584 Layer
* layer_to_receive_animation
) {
585 main_task_runner_
->PostTask(
587 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
588 main_thread_weak_ptr_
,
589 base::Unretained(layer_to_receive_animation
),
593 void LayerTreeTest::PostAddLongAnimationToMainThread(
594 Layer
* layer_to_receive_animation
) {
595 main_task_runner_
->PostTask(
597 base::Bind(&LayerTreeTest::DispatchAddAnimation
,
598 main_thread_weak_ptr_
,
599 base::Unretained(layer_to_receive_animation
),
603 void LayerTreeTest::PostAddAnimationToMainThreadPlayer(
604 AnimationPlayer
* player_to_receive_animation
) {
605 main_task_runner_
->PostTask(
607 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer
,
608 main_thread_weak_ptr_
,
609 base::Unretained(player_to_receive_animation
), 0.000004));
612 void LayerTreeTest::PostAddInstantAnimationToMainThreadPlayer(
613 AnimationPlayer
* player_to_receive_animation
) {
614 main_task_runner_
->PostTask(
616 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer
,
617 main_thread_weak_ptr_
,
618 base::Unretained(player_to_receive_animation
), 0.0));
621 void LayerTreeTest::PostAddLongAnimationToMainThreadPlayer(
622 AnimationPlayer
* player_to_receive_animation
) {
623 main_task_runner_
->PostTask(
625 base::Bind(&LayerTreeTest::DispatchAddAnimationToPlayer
,
626 main_thread_weak_ptr_
,
627 base::Unretained(player_to_receive_animation
), 1.0));
630 void LayerTreeTest::PostSetDeferCommitsToMainThread(bool defer_commits
) {
631 main_task_runner_
->PostTask(
633 base::Bind(&LayerTreeTest::DispatchSetDeferCommits
,
634 main_thread_weak_ptr_
, defer_commits
));
637 void LayerTreeTest::PostSetNeedsCommitToMainThread() {
638 main_task_runner_
->PostTask(FROM_HERE
,
639 base::Bind(&LayerTreeTest::DispatchSetNeedsCommit
,
640 main_thread_weak_ptr_
));
643 void LayerTreeTest::PostSetNeedsUpdateLayersToMainThread() {
644 main_task_runner_
->PostTask(
646 base::Bind(&LayerTreeTest::DispatchSetNeedsUpdateLayers
,
647 main_thread_weak_ptr_
));
650 void LayerTreeTest::PostSetNeedsRedrawToMainThread() {
651 main_task_runner_
->PostTask(FROM_HERE
,
652 base::Bind(&LayerTreeTest::DispatchSetNeedsRedraw
,
653 main_thread_weak_ptr_
));
656 void LayerTreeTest::PostSetNeedsRedrawRectToMainThread(
657 const gfx::Rect
& damage_rect
) {
658 main_task_runner_
->PostTask(
660 base::Bind(&LayerTreeTest::DispatchSetNeedsRedrawRect
,
661 main_thread_weak_ptr_
,
665 void LayerTreeTest::PostSetVisibleToMainThread(bool visible
) {
666 main_task_runner_
->PostTask(
669 &LayerTreeTest::DispatchSetVisible
, main_thread_weak_ptr_
, visible
));
672 void LayerTreeTest::PostSetNextCommitForcesRedrawToMainThread() {
673 main_task_runner_
->PostTask(
675 base::Bind(&LayerTreeTest::DispatchSetNextCommitForcesRedraw
,
676 main_thread_weak_ptr_
));
679 void LayerTreeTest::PostCompositeImmediatelyToMainThread() {
680 main_task_runner_
->PostTask(
682 base::Bind(&LayerTreeTest::DispatchCompositeImmediately
,
683 main_thread_weak_ptr_
));
686 void LayerTreeTest::WillBeginTest() {
687 layer_tree_host_
->SetLayerTreeHostClientReady();
690 void LayerTreeTest::DoBeginTest() {
691 client_
= LayerTreeHostClientForTesting::Create(this);
693 scoped_ptr
<FakeExternalBeginFrameSource
> external_begin_frame_source
;
694 if (settings_
.use_external_begin_frame_source
) {
695 external_begin_frame_source
.reset(new FakeExternalBeginFrameSource(
696 settings_
.renderer_settings
.refresh_rate
));
697 external_begin_frame_source_
= external_begin_frame_source
.get();
700 DCHECK(!impl_thread_
|| impl_thread_
->task_runner().get());
701 layer_tree_host_
= LayerTreeHostForTesting::Create(
702 this, client_
.get(), shared_bitmap_manager_
.get(),
703 gpu_memory_buffer_manager_
.get(), task_graph_runner_
.get(), settings_
,
704 base::ThreadTaskRunnerHandle::Get(),
705 impl_thread_
? impl_thread_
->task_runner() : NULL
,
706 external_begin_frame_source
.Pass());
707 ASSERT_TRUE(layer_tree_host_
);
715 if (end_when_begin_returns_
)
718 // Allow commits to happen once BeginTest() has had a chance to post tasks
719 // so that those tasks will happen before the first commit.
720 if (layer_tree_host_
) {
721 static_cast<LayerTreeHostForTesting
*>(layer_tree_host_
.get())
722 ->set_test_started(true);
726 void LayerTreeTest::SetupTree() {
727 if (!layer_tree_host_
->root_layer()) {
728 scoped_refptr
<Layer
> root_layer
= Layer::Create(layer_settings_
);
729 root_layer
->SetBounds(gfx::Size(1, 1));
730 root_layer
->SetIsDrawable(true);
731 layer_tree_host_
->SetRootLayer(root_layer
);
734 gfx::Size root_bounds
= layer_tree_host_
->root_layer()->bounds();
735 gfx::Size device_root_bounds
= gfx::ToCeiledSize(
736 gfx::ScaleSize(root_bounds
, layer_tree_host_
->device_scale_factor()));
737 layer_tree_host_
->SetViewportSize(device_root_bounds
);
740 void LayerTreeTest::Timeout() {
745 void LayerTreeTest::RealEndTest() {
746 // TODO(mithro): Make this method only end when not inside an impl frame.
747 if (layer_tree_host_
&& !timed_out_
&&
748 proxy()->MainFrameWillHappenForTesting()) {
749 main_task_runner_
->PostTask(
751 base::Bind(&LayerTreeTest::RealEndTest
, main_thread_weak_ptr_
));
755 base::MessageLoop::current()->Quit();
758 void LayerTreeTest::DispatchAddAnimation(Layer
* layer_to_receive_animation
,
759 double animation_duration
) {
760 DCHECK(!proxy() || proxy()->IsMainThread());
762 if (layer_to_receive_animation
) {
763 AddOpacityTransitionToLayer(
764 layer_to_receive_animation
, animation_duration
, 0, 0.5, true);
768 void LayerTreeTest::DispatchAddAnimationToPlayer(
769 AnimationPlayer
* player_to_receive_animation
,
770 double animation_duration
) {
771 DCHECK(!proxy() || proxy()->IsMainThread());
773 if (player_to_receive_animation
) {
774 AddOpacityTransitionToPlayer(player_to_receive_animation
,
775 animation_duration
, 0, 0.5, true);
779 void LayerTreeTest::DispatchSetDeferCommits(bool defer_commits
) {
780 DCHECK(!proxy() || proxy()->IsMainThread());
782 if (layer_tree_host_
)
783 layer_tree_host_
->SetDeferCommits(defer_commits
);
786 void LayerTreeTest::DispatchSetNeedsCommit() {
787 DCHECK(!proxy() || proxy()->IsMainThread());
789 if (layer_tree_host_
)
790 layer_tree_host_
->SetNeedsCommit();
793 void LayerTreeTest::DispatchSetNeedsUpdateLayers() {
794 DCHECK(!proxy() || proxy()->IsMainThread());
796 if (layer_tree_host_
)
797 layer_tree_host_
->SetNeedsUpdateLayers();
800 void LayerTreeTest::DispatchSetNeedsRedraw() {
801 DCHECK(!proxy() || proxy()->IsMainThread());
803 if (layer_tree_host_
)
804 layer_tree_host_
->SetNeedsRedraw();
807 void LayerTreeTest::DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
) {
808 DCHECK(!proxy() || proxy()->IsMainThread());
810 if (layer_tree_host_
)
811 layer_tree_host_
->SetNeedsRedrawRect(damage_rect
);
814 void LayerTreeTest::DispatchSetVisible(bool visible
) {
815 DCHECK(!proxy() || proxy()->IsMainThread());
816 if (layer_tree_host_
)
817 layer_tree_host_
->SetVisible(visible
);
820 void LayerTreeTest::DispatchSetNextCommitForcesRedraw() {
821 DCHECK(!proxy() || proxy()->IsMainThread());
823 if (layer_tree_host_
)
824 layer_tree_host_
->SetNextCommitForcesRedraw();
827 void LayerTreeTest::DispatchCompositeImmediately() {
828 DCHECK(!proxy() || proxy()->IsMainThread());
829 if (layer_tree_host_
)
830 layer_tree_host_
->Composite(base::TimeTicks::Now());
833 void LayerTreeTest::RunTest(bool threaded
, bool delegating_renderer
) {
835 impl_thread_
.reset(new base::Thread("Compositor"));
836 ASSERT_TRUE(impl_thread_
->Start());
839 main_task_runner_
= base::ThreadTaskRunnerHandle::Get();
841 shared_bitmap_manager_
.reset(new TestSharedBitmapManager
);
842 gpu_memory_buffer_manager_
.reset(new TestGpuMemoryBufferManager
);
843 task_graph_runner_
.reset(new TestTaskGraphRunner
);
845 delegating_renderer_
= delegating_renderer
;
847 // Spend less time waiting for BeginFrame because the output is
849 settings_
.renderer_settings
.refresh_rate
= 200.0;
850 settings_
.background_animation_rate
= 200.0;
851 settings_
.verify_property_trees
= verify_property_trees_
;
852 InitializeSettings(&settings_
);
853 InitializeLayerSettings(&layer_settings_
);
855 main_task_runner_
->PostTask(
857 base::Bind(&LayerTreeTest::DoBeginTest
, base::Unretained(this)));
859 if (timeout_seconds_
) {
860 timeout_
.Reset(base::Bind(&LayerTreeTest::Timeout
, base::Unretained(this)));
861 main_task_runner_
->PostDelayedTask(
864 base::TimeDelta::FromSeconds(timeout_seconds_
));
867 base::MessageLoop::current()->Run();
868 DestroyLayerTreeHost();
872 ASSERT_FALSE(layer_tree_host_
.get());
875 FAIL() << "Test timed out";
881 void LayerTreeTest::RequestNewOutputSurface() {
882 layer_tree_host_
->SetOutputSurface(CreateOutputSurface());
885 scoped_ptr
<OutputSurface
> LayerTreeTest::CreateOutputSurface() {
886 scoped_ptr
<FakeOutputSurface
> output_surface
= CreateFakeOutputSurface();
887 DCHECK_EQ(delegating_renderer_
,
888 output_surface
->capabilities().delegated_rendering
);
889 output_surface_
= output_surface
.get();
891 if (settings_
.use_external_begin_frame_source
) {
892 DCHECK(external_begin_frame_source_
);
893 DCHECK(external_begin_frame_source_
->is_ready());
895 return output_surface
.Pass();
898 scoped_ptr
<FakeOutputSurface
> LayerTreeTest::CreateFakeOutputSurface() {
899 if (delegating_renderer_
)
900 return FakeOutputSurface::CreateDelegating3d();
902 return FakeOutputSurface::Create3d();
905 TestWebGraphicsContext3D
* LayerTreeTest::TestContext() {
906 return static_cast<TestContextProvider
*>(output_surface_
->context_provider())
910 int LayerTreeTest::LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
)
912 if (impl
->pending_tree())
913 return impl
->pending_tree()->source_frame_number();
914 if (impl
->active_tree())
915 return impl
->active_tree()->source_frame_number();
916 // Source frames start at 0, so this is invalid.
920 void LayerTreeTest::DestroyLayerTreeHost() {
921 if (layer_tree_host_
&& layer_tree_host_
->root_layer())
922 layer_tree_host_
->root_layer()->SetLayerTreeHost(NULL
);
923 layer_tree_host_
= nullptr;
926 LayerTreeHost
* LayerTreeTest::layer_tree_host() {
927 // We check for a null proxy here as we sometimes ask for the layer tree host
928 // when the proxy does not exist, often for checking settings after a test has
929 // completed. For example, LTHPixelResourceTest::RunPixelResourceTest. See
930 // elsewhere in this file for other examples.
931 DCHECK(!proxy() || proxy()->IsMainThread() || proxy()->IsMainThreadBlocked());
932 return layer_tree_host_
.get();