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 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() {}
101 virtual void ScheduledActionInvalidateOutputSurface() {}
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 PostSetDeferCommitsToMainThread(bool defer_commits
);
142 void PostSetNeedsCommitToMainThread();
143 void PostSetNeedsUpdateLayersToMainThread();
144 void PostSetNeedsRedrawToMainThread();
145 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
146 void PostSetVisibleToMainThread(bool visible
);
147 void PostSetNextCommitForcesRedrawToMainThread();
148 void PostCompositeImmediatelyToMainThread();
153 bool verify_property_trees() const { return verify_property_trees_
; }
154 void set_verify_property_trees(bool verify_property_trees
) {
155 verify_property_trees_
= verify_property_trees
;
161 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
165 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
166 double animation_duration
);
167 void DispatchSetDeferCommits(bool defer_commits
);
168 void DispatchSetNeedsCommit();
169 void DispatchSetNeedsUpdateLayers();
170 void DispatchSetNeedsRedraw();
171 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
172 void DispatchSetVisible(bool visible
);
173 void DispatchSetNextCommitForcesRedraw();
174 void DispatchDidAddAnimation();
175 void DispatchCompositeImmediately();
177 virtual void AfterTest() = 0;
178 virtual void WillBeginTest();
179 virtual void BeginTest() = 0;
180 virtual void SetupTree();
182 virtual void RunTest(bool threaded
,
183 bool delegating_renderer
,
184 bool impl_side_painting
);
185 virtual void RunTestWithImplSidePainting();
187 bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
188 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
190 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
191 : main_task_runner_
.get();
193 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
194 return main_task_runner_
.get();
196 Proxy
* proxy() const {
197 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
199 TaskGraphRunner
* task_graph_runner() const {
200 return task_graph_runner_
.get();
203 bool TestEnded() const { return ended_
; }
205 LayerTreeHost
* layer_tree_host();
206 bool delegating_renderer() const { return delegating_renderer_
; }
207 FakeOutputSurface
* output_surface() { return output_surface_
; }
208 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
210 void DestroyLayerTreeHost();
212 // By default, output surface recreation is synchronous.
213 void RequestNewOutputSurface() override
;
214 // Override this for pixel tests, where you need a real output surface.
215 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
216 // Override this for unit tests, which should not produce pixel output.
217 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
219 TestWebGraphicsContext3D
* TestContext();
222 LayerTreeSettings settings_
;
223 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
224 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
225 FakeOutputSurface
* output_surface_
;
226 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
229 bool end_when_begin_returns_
;
234 bool delegating_renderer_
;
235 bool verify_property_trees_
;
237 int timeout_seconds_
;
239 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
240 scoped_ptr
<base::Thread
> impl_thread_
;
241 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
242 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
243 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
244 base::CancelableClosure timeout_
;
245 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
246 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
247 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
252 #define SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
253 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_MainThreadPaint) { \
254 RunTest(false, false, false); \
256 class SingleThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
258 #define SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
259 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer_ImplSidePaint) { \
260 RunTest(false, false, true); \
262 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
264 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
265 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
266 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
268 #define SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
269 TEST_F(TEST_FIXTURE_NAME, \
270 RunSingleThread_DelegatingRenderer_MainThreadPaint) { \
271 RunTest(false, true, false); \
273 class SingleThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
275 #define SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
276 TEST_F(TEST_FIXTURE_NAME, \
277 RunSingleThread_DelegatingRenderer_ImplSidePaint) { \
278 RunTest(false, true, true); \
280 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
282 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
283 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
284 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
286 #define SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
287 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
288 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
290 #define SINGLE_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
291 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
292 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
294 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
295 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
296 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
298 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
299 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
300 RunTest(true, false, false); \
302 class MultiThreadDirectNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
304 #define MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
305 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
306 RunTest(true, false, true); \
308 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
310 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
311 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
312 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
314 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
315 TEST_F(TEST_FIXTURE_NAME, \
316 RunMultiThread_DelegatingRenderer_MainThreadPaint) { \
317 RunTest(true, true, false); \
319 class MultiThreadDelegatingNoImplNeedsSemicolon##TEST_FIXTURE_NAME {}
321 #define MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
322 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
323 RunTest(true, true, true); \
325 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
327 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
328 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
329 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
331 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
332 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
333 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
335 #define MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
336 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
337 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
339 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
340 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
341 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
343 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
345 SINGLE_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
346 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
348 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME) \
349 SINGLE_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
350 MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
352 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
353 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
354 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
356 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
358 SINGLE_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
359 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
361 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F( \
363 SINGLE_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
364 MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
366 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
367 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
368 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
370 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
371 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
372 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
374 #define SINGLE_AND_MULTI_THREAD_IMPL_TEST_F(TEST_FIXTURE_NAME) \
375 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME); \
376 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_IMPL_TEST_F(TEST_FIXTURE_NAME)
378 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
379 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
380 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
382 // Some tests want to control when notify ready for activation occurs,
383 // but this is not supported in the single-threaded case.
384 #define SINGLE_AND_MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
385 SINGLE_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
386 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
388 #endif // CC_TEST_LAYER_TREE_TEST_H_