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 #ifndef CC_TEST_LAYER_TREE_TEST_H_
6 #define CC_TEST_LAYER_TREE_TEST_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/threading/thread.h"
10 #include "cc/animation/animation_delegate.h"
11 #include "cc/trees/layer_tree_host.h"
12 #include "cc/trees/layer_tree_host_impl.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 class AnimationPlayer
;
17 class FakeExternalBeginFrameSource
;
18 class FakeLayerTreeHostClient
;
19 class FakeOutputSurface
;
22 class LayerTreeHostClient
;
23 class LayerTreeHostImpl
;
24 class TestContextProvider
;
25 class TestGpuMemoryBufferManager
;
26 class TestWebGraphicsContext3D
;
28 // Used by test stubs to notify the test when something interesting happens.
29 class TestHooks
: public AnimationDelegate
{
32 ~TestHooks() override
;
34 void ReadSettings(const LayerTreeSettings
& settings
);
36 virtual void CreateResourceAndTileTaskWorkerPool(
37 LayerTreeHostImpl
* host_impl
,
38 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
39 scoped_ptr
<ResourcePool
>* resource_pool
,
40 scoped_ptr
<ResourcePool
>* staging_resource_pool
);
41 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
42 const BeginFrameArgs
& args
) {}
43 virtual void DidFinishImplFrameOnThread(LayerTreeHostImpl
* host_impl
) {}
44 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
45 CommitEarlyOutReason reason
) {}
46 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
47 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
48 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
49 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
50 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
52 virtual DrawResult
PrepareToDrawOnThread(
53 LayerTreeHostImpl
* host_impl
,
54 LayerTreeHostImpl::FrameData
* frame_data
,
55 DrawResult draw_result
);
56 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
57 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
58 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
59 virtual void NotifyReadyToActivateOnThread(LayerTreeHostImpl
* host_impl
) {}
60 virtual void NotifyReadyToDrawOnThread(LayerTreeHostImpl
* host_impl
) {}
61 virtual void NotifyAllTileTasksCompleted(LayerTreeHostImpl
* host_impl
) {}
62 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
64 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
65 base::TimeTicks monotonic_time
) {}
66 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
67 bool has_unfinished_animation
) {}
68 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
69 base::TimeTicks monotonic_time
) {}
70 virtual void ApplyViewportDeltas(
71 const gfx::Vector2dF
& inner_delta
,
72 const gfx::Vector2dF
& outer_delta
,
73 const gfx::Vector2dF
& elastic_overscroll_delta
,
75 float top_controls_delta
) {}
76 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
77 virtual void WillBeginMainFrame() {}
78 virtual void DidBeginMainFrame() {}
79 virtual void Layout() {}
80 virtual void DidInitializeOutputSurface() {}
81 virtual void DidFailToInitializeOutputSurface() {}
82 virtual void DidAddAnimation() {}
83 virtual void WillCommit() {}
84 virtual void DidCommit() {}
85 virtual void DidCommitAndDrawFrame() {}
86 virtual void DidCompleteSwapBuffers() {}
87 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
89 virtual void ScheduleComposite() {}
91 // Hooks for SchedulerClient.
92 virtual void ScheduledActionWillSendBeginMainFrame() {}
93 virtual void ScheduledActionSendBeginMainFrame() {}
94 virtual void ScheduledActionDrawAndSwapIfPossible() {}
95 virtual void ScheduledActionAnimate() {}
96 virtual void ScheduledActionCommit() {}
97 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
98 virtual void ScheduledActionPrepareTiles() {}
99 virtual void ScheduledActionInvalidateOutputSurface() {}
100 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
101 virtual void SendBeginMainFrameNotExpectedSoon() {}
103 // Implementation of AnimationDelegate:
104 void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
105 Animation::TargetProperty target_property
,
106 int group
) override
{}
107 void NotifyAnimationFinished(base::TimeTicks monotonic_time
,
108 Animation::TargetProperty target_property
,
109 int group
) override
{}
111 virtual void RequestNewOutputSurface() = 0;
115 class LayerTreeHostClientForTesting
;
118 // The LayerTreeTests runs with the main loop running. It instantiates a single
119 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
120 // LayerTreeHostClientForTesting.
122 // BeginTest() is called once the main message loop is running and the layer
123 // tree host is initialized.
125 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
126 // LayerTreeTest methods of similar names. To track the commit process, override
129 // The test continues until someone calls EndTest. EndTest can be called on any
130 // thread, but be aware that ending the test is an asynchronous process.
131 class LayerTreeTest
: public testing::Test
, public TestHooks
{
133 ~LayerTreeTest() override
;
135 virtual void EndTest();
136 void EndTestAfterDelayMs(int delay_milliseconds
);
138 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
139 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
140 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
141 void PostAddAnimationToMainThreadPlayer(
142 AnimationPlayer
* player_to_receive_animation
);
143 void PostAddInstantAnimationToMainThreadPlayer(
144 AnimationPlayer
* player_to_receive_animation
);
145 void PostAddLongAnimationToMainThreadPlayer(
146 AnimationPlayer
* player_to_receive_animation
);
147 void PostSetDeferCommitsToMainThread(bool defer_commits
);
148 void PostSetNeedsCommitToMainThread();
149 void PostSetNeedsUpdateLayersToMainThread();
150 void PostSetNeedsRedrawToMainThread();
151 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
152 void PostSetVisibleToMainThread(bool visible
);
153 void PostSetNextCommitForcesRedrawToMainThread();
154 void PostCompositeImmediatelyToMainThread();
159 bool verify_property_trees() const { return verify_property_trees_
; }
160 void set_verify_property_trees(bool verify_property_trees
) {
161 verify_property_trees_
= verify_property_trees
;
164 const LayerSettings
& layer_settings() { return layer_settings_
; }
169 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
170 virtual void InitializeLayerSettings(LayerSettings
* layer_settings
) {}
174 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
175 double animation_duration
);
176 virtual void DispatchAddAnimationToPlayer(
177 AnimationPlayer
* player_to_receive_animation
,
178 double animation_duration
);
179 void DispatchSetDeferCommits(bool defer_commits
);
180 void DispatchSetNeedsCommit();
181 void DispatchSetNeedsUpdateLayers();
182 void DispatchSetNeedsRedraw();
183 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
184 void DispatchSetVisible(bool visible
);
185 void DispatchSetNextCommitForcesRedraw();
186 void DispatchDidAddAnimation();
187 void DispatchCompositeImmediately();
189 virtual void AfterTest() = 0;
190 virtual void WillBeginTest();
191 virtual void BeginTest() = 0;
192 virtual void SetupTree();
194 virtual void RunTest(bool threaded
, bool delegating_renderer
);
196 bool HasImplThread() { return !!impl_thread_
; }
197 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
199 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
200 : main_task_runner_
.get();
202 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
203 return main_task_runner_
.get();
205 Proxy
* proxy() const {
206 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
208 TaskGraphRunner
* task_graph_runner() const {
209 return task_graph_runner_
.get();
212 bool TestEnded() const { return ended_
; }
214 LayerTreeHost
* layer_tree_host();
215 bool delegating_renderer() const { return delegating_renderer_
; }
216 FakeOutputSurface
* output_surface() { return output_surface_
; }
217 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
219 void DestroyLayerTreeHost();
221 // By default, output surface recreation is synchronous.
222 void RequestNewOutputSurface() override
;
223 // Override this for pixel tests, where you need a real output surface.
224 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
225 // Override this for unit tests, which should not produce pixel output.
226 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
228 TestWebGraphicsContext3D
* TestContext();
230 TestGpuMemoryBufferManager
* GetTestGpuMemoryBufferManager() {
231 return gpu_memory_buffer_manager_
.get();
235 LayerTreeSettings settings_
;
236 LayerSettings layer_settings_
;
238 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
239 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
240 FakeOutputSurface
* output_surface_
;
241 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
244 bool end_when_begin_returns_
;
249 bool delegating_renderer_
;
250 bool verify_property_trees_
;
252 int timeout_seconds_
;
254 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
255 scoped_ptr
<base::Thread
> impl_thread_
;
256 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
257 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
258 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
259 base::CancelableClosure timeout_
;
260 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
261 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
262 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
267 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
268 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
269 RunTest(false, false); \
271 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
273 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
274 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
275 RunTest(false, true); \
277 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
279 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
280 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
281 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
283 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
284 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer) { \
285 RunTest(true, false); \
287 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
289 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
290 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer) { \
291 RunTest(true, true); \
293 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
295 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
296 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
297 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
299 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
300 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
301 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
303 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
304 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
305 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
307 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
308 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
309 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
311 // Some tests want to control when notify ready for activation occurs,
312 // but this is not supported in the single-threaded case.
313 #define MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
314 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
316 #endif // CC_TEST_LAYER_TREE_TEST_H_