Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / cc / test / layer_tree_test.h
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.
4
5 #ifndef CC_TEST_LAYER_TREE_TEST_H_
6 #define CC_TEST_LAYER_TREE_TEST_H_
7
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"
14
15 namespace Webkit {
16 class WebGraphicsContext3D;
17 }
18
19 namespace cc {
20 class FakeLayerTreeHostClient;
21 class FakeOutputSurface;
22 class LayerImpl;
23 class LayerTreeHost;
24 class LayerTreeHostClient;
25 class LayerTreeHostImpl;
26 class TestContextProvider;
27 class TestWebGraphicsContext3D;
28
29 // Used by test stubs to notify the test when something interesting happens.
30 class TestHooks : public AnimationDelegate {
31  public:
32   TestHooks();
33   virtual ~TestHooks();
34
35   void ReadSettings(const LayerTreeSettings& settings);
36
37   virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
38                                           const BeginFrameArgs& args) {}
39   virtual void DidBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
40                                          const BeginFrameArgs& args) {}
41   virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
42                                              bool did_handle) {}
43   virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) {}
44   virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) {}
45   virtual void WillActivateTreeOnThread(LayerTreeHostImpl* host_impl) {}
46   virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) {}
47   virtual void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
48                                            bool success) {}
49   virtual bool PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
50                                      LayerTreeHostImpl::FrameData* frame_data,
51                                      bool result);
52   virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) {}
53   virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) {}
54   virtual void SwapBuffersCompleteOnThread(LayerTreeHostImpl* host_impl) {}
55   virtual void UpdateVisibleTilesOnThread(LayerTreeHostImpl* host_impl) {}
56   virtual void AnimateLayers(LayerTreeHostImpl* host_impl,
57                              base::TimeTicks monotonic_time) {}
58   virtual void UpdateAnimationState(LayerTreeHostImpl* host_impl,
59                                     bool has_unfinished_animation) {}
60   virtual void WillAnimateLayers(LayerTreeHostImpl* host_impl,
61                                  base::TimeTicks monotonic_time) {}
62   virtual void ApplyScrollAndScale(gfx::Vector2d scroll_delta, float scale) {}
63   virtual void Animate(base::TimeTicks monotonic_time) {}
64   virtual void WillBeginMainFrame() {}
65   virtual void DidBeginMainFrame() {}
66   virtual void Layout() {}
67   virtual void DidInitializeOutputSurface(bool succeeded) {}
68   virtual void DidFailToInitializeOutputSurface() {}
69   virtual void DidAddAnimation() {}
70   virtual void WillCommit() {}
71   virtual void DidCommit() {}
72   virtual void DidCommitAndDrawFrame() {}
73   virtual void DidCompleteSwapBuffers() {}
74   virtual void ScheduleComposite() {}
75   virtual void ScheduleAnimation() {}
76   virtual void DidDeferCommit() {}
77   virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
78                                        bool visible) {}
79   virtual base::TimeDelta LowFrequencyAnimationInterval() const;
80
81   // Implementation of AnimationDelegate:
82   virtual void NotifyAnimationStarted(double wall_clock_time,
83                                       base::TimeTicks monotonic_time,
84                                       Animation::TargetProperty target_property)
85       OVERRIDE {}
86   virtual void NotifyAnimationFinished(
87       double wall_clock_time,
88       base::TimeTicks monotonic_time,
89       Animation::TargetProperty target_property) OVERRIDE {}
90
91   virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) = 0;
92   virtual scoped_refptr<ContextProvider> OffscreenContextProvider() = 0;
93 };
94
95 class BeginTask;
96 class LayerTreeHostClientForTesting;
97 class TimeoutTask;
98
99 // The LayerTreeTests runs with the main loop running. It instantiates a single
100 // LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
101 // LayerTreeHostClientForTesting.
102 //
103 // BeginTest() is called once the main message loop is running and the layer
104 // tree host is initialized.
105 //
106 // Key stages of the drawing loop, e.g. drawing or commiting, redirect to
107 // LayerTreeTest methods of similar names. To track the commit process, override
108 // these functions.
109 //
110 // The test continues until someone calls EndTest. EndTest can be called on any
111 // thread, but be aware that ending the test is an asynchronous process.
112 class LayerTreeTest : public testing::Test, public TestHooks {
113  public:
114   virtual ~LayerTreeTest();
115
116   virtual void EndTest();
117   void EndTestAfterDelay(int delay_milliseconds);
118
119   void PostAddAnimationToMainThread(Layer* layer_to_receive_animation);
120   void PostAddInstantAnimationToMainThread(Layer* layer_to_receive_animation);
121   void PostAddLongAnimationToMainThread(Layer* layer_to_receive_animation);
122   void PostSetNeedsCommitToMainThread();
123   void PostSetNeedsUpdateLayersToMainThread();
124   void PostReadbackToMainThread();
125   void PostAcquireLayerTextures();
126   void PostSetNeedsRedrawToMainThread();
127   void PostSetNeedsRedrawRectToMainThread(const gfx::Rect& damage_rect);
128   void PostSetVisibleToMainThread(bool visible);
129   void PostSetNextCommitForcesRedrawToMainThread();
130
131   void DoBeginTest();
132   void Timeout();
133
134  protected:
135   LayerTreeTest();
136
137   virtual void InitializeSettings(LayerTreeSettings* settings) {}
138
139   virtual void ScheduleComposite() OVERRIDE;
140
141   void RealEndTest();
142
143   virtual void DispatchAddAnimation(Layer* layer_to_receive_animation,
144                                     double animation_duration);
145   void DispatchSetNeedsCommit();
146   void DispatchSetNeedsUpdateLayers();
147   void DispatchReadback();
148   void DispatchAcquireLayerTextures();
149   void DispatchSetNeedsRedraw();
150   void DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect);
151   void DispatchSetVisible(bool visible);
152   void DispatchSetNextCommitForcesRedraw();
153   void DispatchComposite();
154   void DispatchDidAddAnimation();
155
156   virtual void AfterTest() = 0;
157   virtual void WillBeginTest();
158   virtual void BeginTest() = 0;
159   virtual void SetupTree();
160
161   virtual void RunTest(bool threaded,
162                        bool delegating_renderer,
163                        bool impl_side_painting);
164   virtual void RunTestWithImplSidePainting();
165
166   bool HasImplThread() { return proxy() ? proxy()->HasImplThread() : false; }
167   base::SingleThreadTaskRunner* ImplThreadTaskRunner() {
168     DCHECK(proxy());
169     return proxy()->ImplThreadTaskRunner() ? proxy()->ImplThreadTaskRunner()
170                                            : main_task_runner_.get();
171   }
172   base::SingleThreadTaskRunner* MainThreadTaskRunner() {
173     return main_task_runner_.get();
174   }
175   Proxy* proxy() const {
176     return layer_tree_host_ ? layer_tree_host_->proxy() : NULL;
177   }
178
179   bool TestEnded() const { return ended_; }
180
181   LayerTreeHost* layer_tree_host() { return layer_tree_host_.get(); }
182   bool delegating_renderer() const { return delegating_renderer_; }
183   FakeOutputSurface* output_surface() { return output_surface_; }
184   int LastCommittedSourceFrameNumber(LayerTreeHostImpl* impl) const;
185
186   virtual scoped_ptr<OutputSurface> CreateOutputSurface(bool fallback) OVERRIDE;
187   virtual scoped_refptr<ContextProvider> OffscreenContextProvider() OVERRIDE;
188
189   virtual scoped_ptr<FakeOutputSurface> CreateFakeOutputSurfaceForTest(
190       bool fallback);
191
192   TestWebGraphicsContext3D* TestContext();
193
194
195  private:
196   LayerTreeSettings settings_;
197   scoped_ptr<LayerTreeHostClientForTesting> client_;
198   scoped_ptr<LayerTreeHost> layer_tree_host_;
199   FakeOutputSurface* output_surface_;
200
201   bool beginning_;
202   bool end_when_begin_returns_;
203   bool timed_out_;
204   bool scheduled_;
205   bool schedule_when_set_visible_true_;
206   bool started_;
207   bool ended_;
208   bool delegating_renderer_;
209
210   int timeout_seconds_;
211
212   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
213   scoped_ptr<base::Thread> impl_thread_;
214   base::CancelableClosure timeout_;
215   scoped_refptr<TestContextProvider> compositor_contexts_;
216   base::WeakPtr<LayerTreeTest> main_thread_weak_ptr_;
217   base::WeakPtrFactory<LayerTreeTest> weak_factory_;
218 };
219
220 }  // namespace cc
221
222 #define SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
223   TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DirectRenderer) {   \
224     RunTest(false, false, false);                               \
225   }                                                             \
226   class SingleThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
227
228 #define SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
229   TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) {   \
230     RunTest(false, true, false);                                    \
231   }                                                                 \
232   class SingleThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
233
234 #define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME)            \
235   SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
236   SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
237
238 #define MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)        \
239   TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_MainThreadPaint) { \
240     RunTest(true, false, false);                                             \
241   }
242
243 #define MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)                 \
244   MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)                \
245       TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DirectRenderer_ImplSidePaint) { \
246     RunTest(true, false, true);                                                \
247   }                                                                            \
248   class MultiThreadDirectNeedsSemicolon##TEST_FIXTURE_NAME {}
249
250 #define MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) \
251   TEST_F(TEST_FIXTURE_NAME,                                               \
252          RunMultiThread_DelegatingRenderer_MainThreadPaint) {             \
253     RunTest(true, true, false);                                           \
254   }
255
256 #define MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)          \
257   MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME) TEST_F( \
258       TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer_ImplSidePaint) { \
259     RunTest(true, true, true);                                              \
260   }                                                                         \
261   class MultiThreadDelegatingNeedsSemicolon##TEST_FIXTURE_NAME {}
262
263 #define MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME)            \
264   MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
265   MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
266
267 #define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)            \
268   MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
269   MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
270
271 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F( \
272     TEST_FIXTURE_NAME)                                         \
273   SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME);     \
274   MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
275
276 #define SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
277   SINGLE_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME);                \
278   MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME)
279
280 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F( \
281     TEST_FIXTURE_NAME)                                             \
282   SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME);     \
283   MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
284
285 #define SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME) \
286   SINGLE_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME);                \
287   MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
288
289 #define SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(TEST_FIXTURE_NAME)            \
290   SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME); \
291   SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(TEST_FIXTURE_NAME)
292
293 #define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)            \
294   SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(TEST_FIXTURE_NAME); \
295   SINGLE_AND_MULTI_THREAD_DELEGATING_RENDERER_TEST_F(TEST_FIXTURE_NAME)
296
297 #endif  // CC_TEST_LAYER_TREE_TEST_H_