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 scoped_ptr
<ResourcePool
>* staging_resource_pool
);
60 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl
* host_impl
,
61 const BeginFrameArgs
& args
) {}
62 virtual void DidFinishImplFrameOnThread(LayerTreeHostImpl
* host_impl
) {}
63 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl
* host_impl
,
64 CommitEarlyOutReason reason
) {}
65 virtual void BeginCommitOnThread(LayerTreeHostImpl
* host_impl
) {}
66 virtual void CommitCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
67 virtual void WillActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
68 virtual void DidActivateTreeOnThread(LayerTreeHostImpl
* host_impl
) {}
69 virtual void InitializedRendererOnThread(LayerTreeHostImpl
* host_impl
,
71 virtual DrawResult
PrepareToDrawOnThread(
72 LayerTreeHostImpl
* host_impl
,
73 LayerTreeHostImpl::FrameData
* frame_data
,
74 DrawResult draw_result
);
75 virtual void DrawLayersOnThread(LayerTreeHostImpl
* host_impl
) {}
76 virtual void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) {}
77 virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl
* host_impl
) {}
78 virtual void NotifyReadyToActivateOnThread(LayerTreeHostImpl
* host_impl
) {}
79 virtual void NotifyReadyToDrawOnThread(LayerTreeHostImpl
* host_impl
) {}
80 virtual void NotifyAllTileTasksCompleted(LayerTreeHostImpl
* host_impl
) {}
81 virtual void NotifyTileStateChangedOnThread(LayerTreeHostImpl
* host_impl
,
83 virtual void AnimateLayers(LayerTreeHostImpl
* host_impl
,
84 base::TimeTicks monotonic_time
) {}
85 virtual void UpdateAnimationState(LayerTreeHostImpl
* host_impl
,
86 bool has_unfinished_animation
) {}
87 virtual void WillAnimateLayers(LayerTreeHostImpl
* host_impl
,
88 base::TimeTicks monotonic_time
) {}
89 virtual void ApplyViewportDeltas(
90 const gfx::Vector2dF
& inner_delta
,
91 const gfx::Vector2dF
& outer_delta
,
92 const gfx::Vector2dF
& elastic_overscroll_delta
,
94 float top_controls_delta
) {}
95 virtual void BeginMainFrame(const BeginFrameArgs
& args
) {}
96 virtual void WillBeginMainFrame() {}
97 virtual void DidBeginMainFrame() {}
98 virtual void Layout() {}
99 virtual void DidInitializeOutputSurface() {}
100 virtual void DidFailToInitializeOutputSurface() {}
101 virtual void DidAddAnimation() {}
102 virtual void WillCommit() {}
103 virtual void DidCommit() {}
104 virtual void DidCommitAndDrawFrame() {}
105 virtual void DidCompleteSwapBuffers() {}
106 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl
* host_impl
,
108 virtual void ScheduleComposite() {}
110 // Hooks for SchedulerClient.
111 virtual void ScheduledActionWillSendBeginMainFrame() {}
112 virtual void ScheduledActionSendBeginMainFrame() {}
113 virtual void ScheduledActionDrawAndSwapIfPossible() {}
114 virtual void ScheduledActionAnimate() {}
115 virtual void ScheduledActionCommit() {}
116 virtual void ScheduledActionBeginOutputSurfaceCreation() {}
117 virtual void ScheduledActionPrepareTiles() {}
118 virtual void ScheduledActionInvalidateOutputSurface() {}
119 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) {}
120 virtual void SendBeginMainFrameNotExpectedSoon() {}
122 // Implementation of AnimationDelegate:
123 void NotifyAnimationStarted(base::TimeTicks monotonic_time
,
124 Animation::TargetProperty target_property
,
125 int group
) override
{}
126 void NotifyAnimationFinished(base::TimeTicks monotonic_time
,
127 Animation::TargetProperty target_property
,
128 int group
) override
{}
130 virtual void RequestNewOutputSurface() = 0;
134 class LayerTreeHostClientForTesting
;
137 // The LayerTreeTests runs with the main loop running. It instantiates a single
138 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
139 // LayerTreeHostClientForTesting.
141 // BeginTest() is called once the main message loop is running and the layer
142 // tree host is initialized.
144 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
145 // LayerTreeTest methods of similar names. To track the commit process, override
148 // The test continues until someone calls EndTest. EndTest can be called on any
149 // thread, but be aware that ending the test is an asynchronous process.
150 class LayerTreeTest
: public testing::Test
, public TestHooks
{
152 ~LayerTreeTest() override
;
154 virtual void EndTest();
155 void EndTestAfterDelayMs(int delay_milliseconds
);
157 void PostAddAnimationToMainThread(Layer
* layer_to_receive_animation
);
158 void PostAddInstantAnimationToMainThread(Layer
* layer_to_receive_animation
);
159 void PostAddLongAnimationToMainThread(Layer
* layer_to_receive_animation
);
160 void PostAddAnimationToMainThreadPlayer(
161 AnimationPlayer
* player_to_receive_animation
);
162 void PostAddInstantAnimationToMainThreadPlayer(
163 AnimationPlayer
* player_to_receive_animation
);
164 void PostAddLongAnimationToMainThreadPlayer(
165 AnimationPlayer
* player_to_receive_animation
);
166 void PostSetDeferCommitsToMainThread(bool defer_commits
);
167 void PostSetNeedsCommitToMainThread();
168 void PostSetNeedsUpdateLayersToMainThread();
169 void PostSetNeedsRedrawToMainThread();
170 void PostSetNeedsRedrawRectToMainThread(const gfx::Rect
& damage_rect
);
171 void PostSetVisibleToMainThread(bool visible
);
172 void PostSetNextCommitForcesRedrawToMainThread();
173 void PostCompositeImmediatelyToMainThread();
178 bool verify_property_trees() const { return verify_property_trees_
; }
179 void set_verify_property_trees(bool verify_property_trees
) {
180 verify_property_trees_
= verify_property_trees
;
183 const LayerSettings
& layer_settings() { return layer_settings_
; }
188 virtual void InitializeSettings(LayerTreeSettings
* settings
) {}
189 virtual void InitializeLayerSettings(LayerSettings
* layer_settings
) {}
193 virtual void DispatchAddAnimation(Layer
* layer_to_receive_animation
,
194 double animation_duration
);
195 virtual void DispatchAddAnimationToPlayer(
196 AnimationPlayer
* player_to_receive_animation
,
197 double animation_duration
);
198 void DispatchSetDeferCommits(bool defer_commits
);
199 void DispatchSetNeedsCommit();
200 void DispatchSetNeedsUpdateLayers();
201 void DispatchSetNeedsRedraw();
202 void DispatchSetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
203 void DispatchSetVisible(bool visible
);
204 void DispatchSetNextCommitForcesRedraw();
205 void DispatchDidAddAnimation();
206 void DispatchCompositeImmediately();
208 virtual void AfterTest() = 0;
209 virtual void WillBeginTest();
210 virtual void BeginTest() = 0;
211 virtual void SetupTree();
213 virtual void RunTest(bool threaded
, bool delegating_renderer
);
215 bool HasImplThread() { return !!impl_thread_
; }
216 base::SingleThreadTaskRunner
* ImplThreadTaskRunner() {
218 return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
219 : main_task_runner_
.get();
221 base::SingleThreadTaskRunner
* MainThreadTaskRunner() {
222 return main_task_runner_
.get();
224 Proxy
* proxy() const {
225 return layer_tree_host_
? layer_tree_host_
->proxy() : NULL
;
227 TaskGraphRunner
* task_graph_runner() const {
228 return task_graph_runner_
.get();
231 bool TestEnded() const { return ended_
; }
233 LayerTreeHost
* layer_tree_host();
234 bool delegating_renderer() const { return delegating_renderer_
; }
235 FakeOutputSurface
* output_surface() { return output_surface_
; }
236 int LastCommittedSourceFrameNumber(LayerTreeHostImpl
* impl
) const;
238 void DestroyLayerTreeHost();
240 // By default, output surface recreation is synchronous.
241 void RequestNewOutputSurface() override
;
242 // Override this for pixel tests, where you need a real output surface.
243 virtual scoped_ptr
<OutputSurface
> CreateOutputSurface();
244 // Override this for unit tests, which should not produce pixel output.
245 virtual scoped_ptr
<FakeOutputSurface
> CreateFakeOutputSurface();
247 TestWebGraphicsContext3D
* TestContext();
249 TestGpuMemoryBufferManager
* GetTestGpuMemoryBufferManager() {
250 return gpu_memory_buffer_manager_
.get();
254 LayerTreeSettings settings_
;
255 LayerSettings layer_settings_
;
257 scoped_ptr
<LayerTreeHostClientForTesting
> client_
;
258 scoped_ptr
<LayerTreeHost
> layer_tree_host_
;
259 FakeOutputSurface
* output_surface_
;
260 FakeExternalBeginFrameSource
* external_begin_frame_source_
;
263 bool end_when_begin_returns_
;
268 bool delegating_renderer_
;
269 bool verify_property_trees_
;
271 int timeout_seconds_
;
273 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner_
;
274 scoped_ptr
<base::Thread
> impl_thread_
;
275 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
276 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
277 scoped_ptr
<TaskGraphRunner
> task_graph_runner_
;
278 base::CancelableClosure timeout_
;
279 scoped_refptr
<TestContextProvider
> compositor_contexts_
;
280 base::WeakPtr
<LayerTreeTest
> main_thread_weak_ptr_
;
281 base::WeakPtrFactory
<LayerTreeTest
> weak_factory_
;
286 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
287 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) { \
288 RunTest(false, false); \
290 class SingleThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
292 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
293 TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
294 RunTest(false, true); \
296 class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
298 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
299 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
300 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
302 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
303 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer) { \
304 RunTest(true, false); \
306 class MultiThreadDirectImplNeedsSemicolon##TEST_FIXTURE_NAME {}
308 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
309 TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer) { \
310 RunTest(true, true); \
312 class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
314 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
315 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
316 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
318 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
319 SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
320 MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
322 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
323 SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
324 MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
326 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
327 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
328 SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
330 // Some tests want to control when notify ready for activation occurs,
331 // but this is not supported in the single-threaded case.
332 #define MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
333 MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
335 #endif // CC_TEST_LAYER_TREE_TEST_H_