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 TestGpuMemoryBufferManager
;
25 class TestWebGraphicsContext3D
;
27 // Used by test stubs to notify the test when something interesting happens.
28 class TestHooks
: public AnimationDelegate
{
31 ~TestHooks() override
;
33 void ReadSettings(const LayerTreeSettings
& settings
);
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 DidFinishImplFrameOnThread(LayerTreeHostImpl
* host_impl
) {}
43 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
44 CommitEarlyOutReason reason
) {}
45 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
46 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
47 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
48 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
49 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
51 virtual DrawResult
PrepareToDrawOnThread(
52 LayerTreeHostImpl
* host_impl
,
53 LayerTreeHostImpl::FrameData
* frame_data
,
54 DrawResult draw_result
);
55 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
56 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
57 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
58 virtual void NotifyReadyToActivateOnThread(LayerTreeHostImpl
* host_impl
) {}
59 virtual void NotifyReadyToDrawOnThread(LayerTreeHostImpl
* host_impl
) {}
60 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
62 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
63 base::TimeTicks monotonic_time
) {}
64 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
65 bool has_unfinished_animation
) {}
66 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
67 base::TimeTicks monotonic_time
) {}
68 virtual void ApplyViewportDeltas(
69 const gfx::Vector2dF
& inner_delta
,
70 const gfx::Vector2dF
& outer_delta
,
71 const gfx::Vector2dF
& elastic_overscroll_delta
,
73 float top_controls_delta
) {}
74 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
75 virtual void WillBeginMainFrame() {}
76 virtual void DidBeginMainFrame() {}
77 virtual void Layout() {}
78 virtual void DidInitializeOutputSurface() {}
79 virtual void DidFailToInitializeOutputSurface() {}
80 virtual void DidAddAnimation() {}
81 virtual void WillCommit() {}
82 virtual void DidCommit() {}
83 virtual void DidCommitAndDrawFrame() {}
84 virtual void DidCompleteSwapBuffers() {}
85 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
87 virtual void ScheduleComposite() {}
89 // Hooks for SchedulerClient.
90 virtual void ScheduledActionWillSendBeginMainFrame() {}
91 virtual void ScheduledActionSendBeginMainFrame() {}
92 virtual void ScheduledActionDrawAndSwapIfPossible() {}
93 virtual void ScheduledActionAnimate() {}
94 virtual void ScheduledActionCommit() {}
95 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
96 virtual void ScheduledActionPrepareTiles() {}
97 virtual void ScheduledActionInvalidateOutputSurface() {}
98 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
99 virtual void SendBeginMainFrameNotExpectedSoon() {}
101 // Implementation of AnimationDelegate:
102 void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
103 Animation::TargetProperty target_property
,
104 int group
) override
{}
105 void NotifyAnimationFinished(base::TimeTicks monotonic_time
,
106 Animation::TargetProperty target_property
,
107 int group
) override
{}
109 virtual void RequestNewOutputSurface() = 0;
113 class LayerTreeHostClientForTesting
;
116 // The LayerTreeTests runs with the main loop running. It instantiates a single
117 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
118 // LayerTreeHostClientForTesting.
120 // BeginTest() is called once the main message loop is running and the layer
121 // tree host is initialized.
123 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
124 // LayerTreeTest methods of similar names. To track the commit process, override
127 // The test continues until someone calls EndTest. EndTest can be called on any
128 // thread, but be aware that ending the test is an asynchronous process.
129 class LayerTreeTest
: public testing::Test
, public TestHooks
{
131 ~LayerTreeTest() override
;
133 virtual void EndTest();
134 void EndTestAfterDelayMs(int delay_milliseconds
);
136 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
137 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
138 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
139 void PostSetDeferCommitsToMainThread(bool defer_commits
);
140 void PostSetNeedsCommitToMainThread();
141 void PostSetNeedsUpdateLayersToMainThread();
142 void PostSetNeedsRedrawToMainThread();
143 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
144 void PostSetVisibleToMainThread(bool visible
);
145 void PostSetNextCommitForcesRedrawToMainThread();
146 void PostCompositeImmediatelyToMainThread();
151 bool verify_property_trees() const { return verify_property_trees_
; }
152 void set_verify_property_trees(bool verify_property_trees
) {
153 verify_property_trees_
= verify_property_trees
;
156 const LayerSettings
& layer_settings() { return layer_settings_
; }
161 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
162 virtual void InitializeLayerSettings(LayerSettings
* layer_settings
) {}
166 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
167 double animation_duration
);
168 void DispatchSetDeferCommits(bool defer_commits
);
169 void DispatchSetNeedsCommit();
170 void DispatchSetNeedsUpdateLayers();
171 void DispatchSetNeedsRedraw();
172 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
173 void DispatchSetVisible(bool visible
);
174 void DispatchSetNextCommitForcesRedraw();
175 void DispatchDidAddAnimation();
176 void DispatchCompositeImmediately();
178 virtual void AfterTest() = 0;
179 virtual void WillBeginTest();
180 virtual void BeginTest() = 0;
181 virtual void SetupTree();
183 virtual void RunTest(bool threaded
,
184 bool delegating_renderer
,
185 bool impl_side_painting
);
186 virtual void RunTestWithImplSidePainting();
188 bool HasImplThread() { return !!impl_thread_
; }
189 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
191 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
192 : main_task_runner_
.get();
194 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
195 return main_task_runner_
.get();
197 Proxy
* proxy() const {
198 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
200 TaskGraphRunner
* task_graph_runner() const {
201 return task_graph_runner_
.get();
204 bool TestEnded() const { return ended_
; }
206 LayerTreeHost
* layer_tree_host();
207 bool delegating_renderer() const { return delegating_renderer_
; }
208 FakeOutputSurface
* output_surface() { return output_surface_
; }
209 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
211 void DestroyLayerTreeHost();
213 // By default, output surface recreation is synchronous.
214 void RequestNewOutputSurface() override
;
215 // Override this for pixel tests, where you need a real output surface.
216 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
217 // Override this for unit tests, which should not produce pixel output.
218 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
220 TestWebGraphicsContext3D
* TestContext();
222 TestGpuMemoryBufferManager
* GetTestGpuMemoryBufferManager() {
223 return gpu_memory_buffer_manager_
.get();
227 LayerTreeSettings settings_
;
228 LayerSettings layer_settings_
;
230 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
231 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
232 FakeOutputSurface
* output_surface_
;
233 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
236 bool end_when_begin_returns_
;
241 bool delegating_renderer_
;
242 bool verify_property_trees_
;
244 int timeout_seconds_
;
246 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
247 scoped_ptr
<base::Thread
> impl_thread_
;
248 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
249 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
250 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
251 base::CancelableClosure timeout_
;
252 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
253 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
254 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
259 #define SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
260 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_MainThreadPaint) { \
261 RunTest(false, false, false); \
263 class SingleThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
265 #define SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
266 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_ImplSidePaint) { \
267 RunTest(false, false, true); \
269 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
271 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
272 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
273 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
275 #define SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
276 TEST_F(TEST_FIXTURE_NAME, \
277 RunSingleThread_DelegatingRenderer_MainThreadPaint) { \
278 RunTest(false, true, false); \
280 class SingleThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
282 #define SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
283 TEST_F(TEST_FIXTURE_NAME, \
284 RunSingleThread_DelegatingRenderer_ImplSidePaint) { \
285 RunTest(false, true, true); \
287 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
289 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
290 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
291 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
293 #define SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
294 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
295 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
297 #define SINGLE_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
298 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
299 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
301 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
302 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
303 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
305 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
306 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
307 RunTest(true, false, false); \
309 class MultiThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
311 #define MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
312 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
313 RunTest(true, false, true); \
315 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
317 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
318 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
319 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
321 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
322 TEST_F(TEST_FIXTURE_NAME, \
323 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
324 RunTest(true, true, false); \
326 class MultiThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
328 #define MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
329 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
330 RunTest(true, true, true); \
332 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
334 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
335 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
336 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
338 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
339 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
340 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
342 #define MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
343 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
344 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
346 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
347 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
348 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
350 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
352 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
353 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
355 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
356 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
357 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
359 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
360 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
361 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
363 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
365 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
366 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
368 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F( \
370 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
371 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
373 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
374 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
375 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
377 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
378 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
379 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
381 #define SINGLE_AND_MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
382 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
383 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
385 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
386 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
387 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
389 // Some tests want to control when notify ready for activation occurs,
390 // but this is not supported in the single-threaded case.
391 #define SINGLE_AND_MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
392 SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
393 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
395 #endif // CC_TEST_LAYER_TREE_TEST_H_