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 NotifyAllTileTasksCompleted(LayerTreeHostImpl
* host_impl
) {}
61 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
63 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
64 base::TimeTicks monotonic_time
) {}
65 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
66 bool has_unfinished_animation
) {}
67 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
68 base::TimeTicks monotonic_time
) {}
69 virtual void ApplyViewportDeltas(
70 const gfx::Vector2dF
& inner_delta
,
71 const gfx::Vector2dF
& outer_delta
,
72 const gfx::Vector2dF
& elastic_overscroll_delta
,
74 float top_controls_delta
) {}
75 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
76 virtual void WillBeginMainFrame() {}
77 virtual void DidBeginMainFrame() {}
78 virtual void Layout() {}
79 virtual void DidInitializeOutputSurface() {}
80 virtual void DidFailToInitializeOutputSurface() {}
81 virtual void DidAddAnimation() {}
82 virtual void WillCommit() {}
83 virtual void DidCommit() {}
84 virtual void DidCommitAndDrawFrame() {}
85 virtual void DidCompleteSwapBuffers() {}
86 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
88 virtual void ScheduleComposite() {}
90 // Hooks for SchedulerClient.
91 virtual void ScheduledActionWillSendBeginMainFrame() {}
92 virtual void ScheduledActionSendBeginMainFrame() {}
93 virtual void ScheduledActionDrawAndSwapIfPossible() {}
94 virtual void ScheduledActionAnimate() {}
95 virtual void ScheduledActionCommit() {}
96 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
97 virtual void ScheduledActionPrepareTiles() {}
98 virtual void ScheduledActionInvalidateOutputSurface() {}
99 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
100 virtual void SendBeginMainFrameNotExpectedSoon() {}
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
;
157 const LayerSettings
& layer_settings() { return layer_settings_
; }
162 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
163 virtual void InitializeLayerSettings(LayerSettings
* layer_settings
) {}
167 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
168 double animation_duration
);
169 void DispatchSetDeferCommits(bool defer_commits
);
170 void DispatchSetNeedsCommit();
171 void DispatchSetNeedsUpdateLayers();
172 void DispatchSetNeedsRedraw();
173 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
174 void DispatchSetVisible(bool visible
);
175 void DispatchSetNextCommitForcesRedraw();
176 void DispatchDidAddAnimation();
177 void DispatchCompositeImmediately();
179 virtual void AfterTest() = 0;
180 virtual void WillBeginTest();
181 virtual void BeginTest() = 0;
182 virtual void SetupTree();
184 virtual void RunTest(bool threaded
, bool delegating_renderer
);
186 bool HasImplThread() { return !!impl_thread_
; }
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
;
198 TaskGraphRunner
* task_graph_runner() const {
199 return task_graph_runner_
.get();
202 bool TestEnded() const { return ended_
; }
204 LayerTreeHost
* layer_tree_host();
205 bool delegating_renderer() const { return delegating_renderer_
; }
206 FakeOutputSurface
* output_surface() { return output_surface_
; }
207 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
209 void DestroyLayerTreeHost();
211 // By default, output surface recreation is synchronous.
212 void RequestNewOutputSurface() override
;
213 // Override this for pixel tests, where you need a real output surface.
214 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
215 // Override this for unit tests, which should not produce pixel output.
216 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
218 TestWebGraphicsContext3D
* TestContext();
220 TestGpuMemoryBufferManager
* GetTestGpuMemoryBufferManager() {
221 return gpu_memory_buffer_manager_
.get();
225 LayerTreeSettings settings_
;
226 LayerSettings layer_settings_
;
228 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
229 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
230 FakeOutputSurface
* output_surface_
;
231 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
234 bool end_when_begin_returns_
;
239 bool delegating_renderer_
;
240 bool verify_property_trees_
;
242 int timeout_seconds_
;
244 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
245 scoped_ptr
<base::Thread
> impl_thread_
;
246 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
247 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
248 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
249 base::CancelableClosure timeout_
;
250 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
251 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
252 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
257 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
258 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
259 RunTest(false, false); \
261 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
263 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
264 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
265 RunTest(false, true); \
267 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
269 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
270 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
271 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
273 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
274 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer) { \
275 RunTest(true, false); \
277 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
279 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
280 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer) { \
281 RunTest(true, true); \
283 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
285 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
286 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
287 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
289 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
290 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
291 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
293 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
294 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
295 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
297 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
298 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
299 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
301 // Some tests want to control when notify ready for activation occurs,
302 // but this is not supported in the single-threaded case.
303 #define MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
304 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
306 #endif // CC_TEST_LAYER_TREE_TEST_H_