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 // Creates the virtual viewport layer hierarchy under the given root_layer.
29 // Convenient overload of the method below that creates a scrolling layer as
30 // the outer viewport scroll layer.
31 void CreateVirtualViewportLayers(Layer
* root_layer
,
32 const gfx::Size
& inner_bounds
,
33 const gfx::Size
& outer_bounds
,
34 const gfx::Size
& scroll_bounds
,
36 const LayerSettings
& layer_settings
);
38 // Creates the virtual viewport layer hierarchy under the given root_layer.
39 // Uses the given scroll layer as the content "outer viewport scroll layer".
40 void CreateVirtualViewportLayers(Layer
* root_layer
,
41 scoped_refptr
<Layer
> outer_scroll_layer
,
42 const gfx::Size
& outer_bounds
,
43 const gfx::Size
& scroll_bounds
,
45 const LayerSettings
& layer_settings
);
47 // Used by test stubs to notify the test when something interesting happens.
48 class TestHooks
: public AnimationDelegate
{
51 ~TestHooks() override
;
53 void ReadSettings(const LayerTreeSettings
& settings
);
55 virtual void CreateResourceAndTileTaskWorkerPool(
56 LayerTreeHostImpl
* host_impl
,
57 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
58 scoped_ptr
<ResourcePool
>* resource_pool
);
59 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
60 const BeginFrameArgs
& args
) {}
61 virtual void DidFinishImplFrameOnThread(LayerTreeHostImpl
* host_impl
) {}
62 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
63 CommitEarlyOutReason reason
) {}
64 virtual void WillPrepareTiles(LayerTreeHostImpl
* host_impl
) {}
65 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
66 virtual void WillCommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
67 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
68 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
69 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
70 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
72 virtual DrawResult
PrepareToDrawOnThread(
73 LayerTreeHostImpl
* host_impl
,
74 LayerTreeHostImpl::FrameData
* frame_data
,
75 DrawResult draw_result
);
76 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
77 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
78 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
79 virtual void NotifyReadyToActivateOnThread(LayerTreeHostImpl
* host_impl
) {}
80 virtual void NotifyReadyToDrawOnThread(LayerTreeHostImpl
* host_impl
) {}
81 virtual void NotifyAllTileTasksCompleted(LayerTreeHostImpl
* host_impl
) {}
82 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
84 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
85 base::TimeTicks monotonic_time
) {}
86 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
87 bool has_unfinished_animation
) {}
88 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
89 base::TimeTicks monotonic_time
) {}
90 virtual void ApplyViewportDeltas(
91 const gfx::Vector2dF
& inner_delta
,
92 const gfx::Vector2dF
& outer_delta
,
93 const gfx::Vector2dF
& elastic_overscroll_delta
,
95 float top_controls_delta
) {}
96 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
97 virtual void WillBeginMainFrame() {}
98 virtual void DidBeginMainFrame() {}
99 virtual void Layout() {}
100 virtual void DidInitializeOutputSurface() {}
101 virtual void DidFailToInitializeOutputSurface() {}
102 virtual void DidAddAnimation() {}
103 virtual void WillCommit() {}
104 virtual void DidCommit() {}
105 virtual void DidCommitAndDrawFrame() {}
106 virtual void DidCompleteSwapBuffers() {}
107 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
109 virtual void ScheduleComposite() {}
110 virtual void DidSetNeedsUpdateLayers() {}
112 // Hooks for SchedulerClient.
113 virtual void ScheduledActionWillSendBeginMainFrame() {}
114 virtual void ScheduledActionSendBeginMainFrame() {}
115 virtual void ScheduledActionDrawAndSwapIfPossible() {}
116 virtual void ScheduledActionAnimate() {}
117 virtual void ScheduledActionCommit() {}
118 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
119 virtual void ScheduledActionPrepareTiles() {}
120 virtual void ScheduledActionInvalidateOutputSurface() {}
121 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
122 virtual void SendBeginMainFrameNotExpectedSoon() {}
124 // Implementation of AnimationDelegate:
125 void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
126 Animation::TargetProperty target_property
,
127 int group
) override
{}
128 void NotifyAnimationFinished(base::TimeTicks monotonic_time
,
129 Animation::TargetProperty target_property
,
130 int group
) override
{}
132 virtual void RequestNewOutputSurface() = 0;
136 class LayerTreeHostClientForTesting
;
139 // The LayerTreeTests runs with the main loop running. It instantiates a single
140 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
141 // LayerTreeHostClientForTesting.
143 // BeginTest() is called once the main message loop is running and the layer
144 // tree host is initialized.
146 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
147 // LayerTreeTest methods of similar names. To track the commit process, override
150 // The test continues until someone calls EndTest. EndTest can be called on any
151 // thread, but be aware that ending the test is an asynchronous process.
152 class LayerTreeTest
: public testing::Test
, public TestHooks
{
154 ~LayerTreeTest() override
;
156 virtual void EndTest();
157 void EndTestAfterDelayMs(int delay_milliseconds
);
159 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
160 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
161 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
162 void PostAddAnimationToMainThreadPlayer(
163 AnimationPlayer
* player_to_receive_animation
);
164 void PostAddInstantAnimationToMainThreadPlayer(
165 AnimationPlayer
* player_to_receive_animation
);
166 void PostAddLongAnimationToMainThreadPlayer(
167 AnimationPlayer
* player_to_receive_animation
);
168 void PostSetDeferCommitsToMainThread(bool defer_commits
);
169 void PostSetNeedsCommitToMainThread();
170 void PostSetNeedsUpdateLayersToMainThread();
171 void PostSetNeedsRedrawToMainThread();
172 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
173 void PostSetVisibleToMainThread(bool visible
);
174 void PostSetNextCommitForcesRedrawToMainThread();
175 void PostCompositeImmediatelyToMainThread();
180 bool verify_property_trees() const { return verify_property_trees_
; }
181 void set_verify_property_trees(bool verify_property_trees
) {
182 verify_property_trees_
= verify_property_trees
;
185 const LayerSettings
& layer_settings() { return layer_settings_
; }
190 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
191 virtual void InitializeLayerSettings(LayerSettings
* layer_settings
) {}
195 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
196 double animation_duration
);
197 virtual void DispatchAddAnimationToPlayer(
198 AnimationPlayer
* player_to_receive_animation
,
199 double animation_duration
);
200 void DispatchSetDeferCommits(bool defer_commits
);
201 void DispatchSetNeedsCommit();
202 void DispatchSetNeedsUpdateLayers();
203 void DispatchSetNeedsRedraw();
204 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
205 void DispatchSetVisible(bool visible
);
206 void DispatchSetNextCommitForcesRedraw();
207 void DispatchDidAddAnimation();
208 void DispatchCompositeImmediately();
210 virtual void AfterTest() = 0;
211 virtual void WillBeginTest();
212 virtual void BeginTest() = 0;
213 virtual void SetupTree();
215 virtual void RunTest(bool threaded
, bool delegating_renderer
);
217 bool HasImplThread() { return !!impl_thread_
; }
218 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
220 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
221 : main_task_runner_
.get();
223 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
224 return main_task_runner_
.get();
226 Proxy
* proxy() const {
227 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
229 TaskGraphRunner
* task_graph_runner() const {
230 return task_graph_runner_
.get();
233 bool TestEnded() const { return ended_
; }
235 LayerTreeHost
* layer_tree_host();
236 bool delegating_renderer() const { return delegating_renderer_
; }
237 FakeOutputSurface
* output_surface() { return output_surface_
; }
238 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
240 void DestroyLayerTreeHost();
242 // By default, output surface recreation is synchronous.
243 void RequestNewOutputSurface() override
;
244 // Override this for pixel tests, where you need a real output surface.
245 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
246 // Override this for unit tests, which should not produce pixel output.
247 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
249 TestWebGraphicsContext3D
* TestContext();
251 TestGpuMemoryBufferManager
* GetTestGpuMemoryBufferManager() {
252 return gpu_memory_buffer_manager_
.get();
256 LayerTreeSettings settings_
;
257 LayerSettings layer_settings_
;
259 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
260 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
261 FakeOutputSurface
* output_surface_
;
262 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
265 bool end_when_begin_returns_
;
270 bool delegating_renderer_
;
271 bool verify_property_trees_
;
273 int timeout_seconds_
;
275 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
276 scoped_ptr
<base::Thread
> impl_thread_
;
277 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
278 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
279 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
280 base::CancelableClosure timeout_
;
281 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
282 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
283 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
288 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
289 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
290 RunTest(false, false); \
292 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
294 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
295 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
296 RunTest(false, true); \
298 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
300 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
301 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
302 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
304 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
305 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer) { \
306 RunTest(true, false); \
308 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
310 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
311 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer) { \
312 RunTest(true, true); \
314 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
316 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
317 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
318 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
320 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
321 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
322 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
324 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
325 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
326 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
328 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
329 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
330 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
332 // Some tests want to control when notify ready for activation occurs,
333 // but this is not supported in the single-threaded case.
334 #define MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
335 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
337 #endif // CC_TEST_LAYER_TREE_TEST_H_