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 FakeExternalBeginFrameSource
;
17 class FakeLayerTreeHostClient
;
18 class FakeOutputSurface
;
21 class LayerTreeHostClient
;
22 class LayerTreeHostImpl
;
23 class TestContextProvider
;
24 class TestWebGraphicsContext3D
;
26 // Used by test stubs to notify the test when something interesting happens.
27 class TestHooks
: public AnimationDelegate
{
30 ~TestHooks() override
;
32 void ReadSettings(const LayerTreeSettings
& settings
);
34 virtual scoped_ptr
<Rasterizer
> CreateRasterizer(LayerTreeHostImpl
* host_impl
);
35 virtual void CreateResourceAndTileTaskWorkerPool(
36 LayerTreeHostImpl
* host_impl
,
37 scoped_ptr
<TileTaskWorkerPool
>* tile_task_worker_pool
,
38 scoped_ptr
<ResourcePool
>* resource_pool
,
39 scoped_ptr
<ResourcePool
>* staging_resource_pool
);
40 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
41 const BeginFrameArgs
& args
) {}
42 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
43 CommitEarlyOutReason reason
) {}
44 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
45 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
46 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
47 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
48 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
50 virtual DrawResult
PrepareToDrawOnThread(
51 LayerTreeHostImpl
* host_impl
,
52 LayerTreeHostImpl::FrameData
* frame_data
,
53 DrawResult draw_result
);
54 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
55 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
56 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
57 virtual void NotifyReadyToActivateOnThread(LayerTreeHostImpl
* host_impl
) {}
58 virtual void NotifyReadyToDrawOnThread(LayerTreeHostImpl
* host_impl
) {}
59 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
61 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
62 base::TimeTicks monotonic_time
) {}
63 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
64 bool has_unfinished_animation
) {}
65 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
66 base::TimeTicks monotonic_time
) {}
67 virtual void ApplyViewportDeltas(
68 const gfx::Vector2dF
& inner_delta
,
69 const gfx::Vector2dF
& outer_delta
,
70 const gfx::Vector2dF
& elastic_overscroll_delta
,
72 float top_controls_delta
) {}
73 virtual void ApplyViewportDeltas(const gfx::Vector2d
& scroll_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() {}
90 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
92 // Hooks for SchedulerClient.
93 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) {}
94 virtual void ScheduledActionWillSendBeginMainFrame() {}
95 virtual void ScheduledActionSendBeginMainFrame() {}
96 virtual void ScheduledActionDrawAndSwapIfPossible() {}
97 virtual void ScheduledActionAnimate() {}
98 virtual void ScheduledActionCommit() {}
99 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
100 virtual void ScheduledActionPrepareTiles() {}
102 // Implementation of AnimationDelegate:
103 void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
104 Animation::TargetProperty target_property
,
105 int group
) override
{}
106 void NotifyAnimationFinished(base::TimeTicks monotonic_time
,
107 Animation::TargetProperty target_property
,
108 int group
) override
{}
110 virtual void RequestNewOutputSurface() = 0;
114 class LayerTreeHostClientForTesting
;
117 // The LayerTreeTests runs with the main loop running. It instantiates a single
118 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
119 // LayerTreeHostClientForTesting.
121 // BeginTest() is called once the main message loop is running and the layer
122 // tree host is initialized.
124 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
125 // LayerTreeTest methods of similar names. To track the commit process, override
128 // The test continues until someone calls EndTest. EndTest can be called on any
129 // thread, but be aware that ending the test is an asynchronous process.
130 class LayerTreeTest
: public testing::Test
, public TestHooks
{
132 ~LayerTreeTest() override
;
134 virtual void EndTest();
135 void EndTestAfterDelayMs(int delay_milliseconds
);
137 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
138 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
139 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
140 void PostSetDeferCommitsToMainThread(bool defer_commits
);
141 void PostSetNeedsCommitToMainThread();
142 void PostSetNeedsUpdateLayersToMainThread();
143 void PostSetNeedsRedrawToMainThread();
144 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
145 void PostSetVisibleToMainThread(bool visible
);
146 void PostSetNextCommitForcesRedrawToMainThread();
147 void PostCompositeImmediatelyToMainThread();
152 bool verify_property_trees() const { return verify_property_trees_
; }
153 void set_verify_property_trees(bool verify_property_trees
) {
154 verify_property_trees_
= verify_property_trees
;
160 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
164 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
165 double animation_duration
);
166 void DispatchSetDeferCommits(bool defer_commits
);
167 void DispatchSetNeedsCommit();
168 void DispatchSetNeedsUpdateLayers();
169 void DispatchSetNeedsRedraw();
170 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
171 void DispatchSetVisible(bool visible
);
172 void DispatchSetNextCommitForcesRedraw();
173 void DispatchDidAddAnimation();
174 void DispatchCompositeImmediately();
176 virtual void AfterTest() = 0;
177 virtual void WillBeginTest();
178 virtual void BeginTest() = 0;
179 virtual void SetupTree();
181 virtual void RunTest(bool threaded
,
182 bool delegating_renderer
,
183 bool impl_side_painting
);
184 virtual void RunTestWithImplSidePainting();
186 bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
187 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
189 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
190 : main_task_runner_
.get();
192 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
193 return main_task_runner_
.get();
195 Proxy
* proxy() const {
196 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
199 bool TestEnded() const { return ended_
; }
201 LayerTreeHost
* layer_tree_host() { return layer_tree_host_
.get(); }
202 bool delegating_renderer() const { return delegating_renderer_
; }
203 FakeOutputSurface
* output_surface() { return output_surface_
; }
204 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
206 void DestroyLayerTreeHost();
208 // By default, output surface recreation is synchronous.
209 void RequestNewOutputSurface() override
;
210 // Override this for pixel tests, where you need a real output surface.
211 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
212 // Override this for unit tests, which should not produce pixel output.
213 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
215 TestWebGraphicsContext3D
* TestContext();
218 LayerTreeSettings settings_
;
219 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
220 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
221 FakeOutputSurface
* output_surface_
;
222 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
225 bool end_when_begin_returns_
;
230 bool delegating_renderer_
;
231 bool verify_property_trees_
;
233 int timeout_seconds_
;
235 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
236 scoped_ptr
<base::Thread
> impl_thread_
;
237 base::CancelableClosure timeout_
;
238 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
239 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
240 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
245 #define SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
246 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_MainThreadPaint) { \
247 RunTest(false, false, false); \
249 class SingleThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
251 #define SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
252 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_ImplSidePaint) { \
253 RunTest(false, false, true); \
255 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
257 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
258 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
259 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
261 #define SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
262 TEST_F(TEST_FIXTURE_NAME, \
263 RunSingleThread_DelegatingRenderer_MainThreadPaint) { \
264 RunTest(false, true, false); \
266 class SingleThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
268 #define SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
269 TEST_F(TEST_FIXTURE_NAME, \
270 RunSingleThread_DelegatingRenderer_ImplSidePaint) { \
271 RunTest(false, true, true); \
273 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
275 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
276 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
277 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
279 #define SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
280 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
281 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
283 #define SINGLE_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
284 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
285 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
287 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
288 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
289 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
291 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
292 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
293 RunTest(true, false, false); \
295 class MultiThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
297 #define MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
298 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
299 RunTest(true, false, true); \
301 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
303 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
304 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
305 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
307 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
308 TEST_F(TEST_FIXTURE_NAME, \
309 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
310 RunTest(true, true, false); \
312 class MultiThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
314 #define MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
315 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
316 RunTest(true, true, true); \
318 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
320 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
321 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
322 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
324 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
325 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
326 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
328 #define MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
329 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
330 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
332 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
333 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
334 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
336 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
338 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
339 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
341 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
342 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
343 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
345 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
346 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
347 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
349 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
351 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
352 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
354 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F( \
356 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
357 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
359 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
360 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
361 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
363 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
364 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
365 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
367 #define SINGLE_AND_MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
368 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
369 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
371 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
372 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
373 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
375 // Some tests want to control when notify ready for activation occurs,
376 // but this is not supported in the single-threaded case.
377 #define SINGLE_AND_MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
378 SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
379 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
381 #endif // CC_TEST_LAYER_TREE_TEST_H_