forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_tree_test.h
364 lines (317 loc) · 14.2 KB
/
layer_tree_test.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_TEST_LAYER_TREE_TEST_H_
#define CC_TEST_LAYER_TREE_TEST_H_
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "cc/animation/animation_delegate.h"
#include "cc/base/features.h"
#include "cc/test/property_tree_test_utils.h"
#include "cc/test/test_hooks.h"
#include "cc/test/test_task_graph_runner.h"
#include "cc/test/test_types.h"
#include "cc/trees/compositor_mode.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "components/viz/test/test_gpu_memory_buffer_manager.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace test {
class ScopedFeatureList;
}
} // namespace base
namespace viz {
class BeginFrameSource;
class TestContextProvider;
}
namespace cc {
class Animation;
class AnimationHost;
class LayerTreeHost;
class LayerTreeHostForTesting;
class LayerTreeTestLayerTreeFrameSinkClient;
class Proxy;
class TestLayerTreeFrameSink;
class TestTaskGraphRunner;
class LayerTreeHostClientForTesting;
// The LayerTreeTests runs with the main loop running. It instantiates a single
// LayerTreeHostForTesting and associated LayerTreeHostImplForTesting and
// LayerTreeHostClientForTesting.
//
// BeginTest() is called once the main message loop is running and the layer
// tree host is initialized.
//
// Key stages of the drawing loop, e.g. drawing or commiting, redirect to
// LayerTreeTest methods of similar names. To track the commit process, override
// these functions.
//
// The test continues until someone calls EndTest. EndTest can be called on any
// thread, but be aware that ending the test is an asynchronous process.
class LayerTreeTest : public testing::Test, public TestHooks {
public:
std::string TestTypeToString() {
switch (renderer_type_) {
case viz::RendererType::kGL:
return "GL";
case viz::RendererType::kSkiaGL:
return "Skia GL";
case viz::RendererType::kSkiaVk:
return "Skia Vulkan";
case viz::RendererType::kSkiaDawn:
return "Skia Dawn";
case viz::RendererType::kSoftware:
return "Software";
}
}
~LayerTreeTest() override;
virtual void EndTest();
void EndTestAfterDelayMs(int delay_milliseconds);
void PostAddNoDamageAnimationToMainThread(
Animation* animation_to_receive_animation);
void PostAddOpacityAnimationToMainThread(
Animation* animation_to_receive_animation);
void PostAddOpacityAnimationToMainThreadInstantly(
Animation* animation_to_receive_animation);
void PostAddOpacityAnimationToMainThreadDelayed(
Animation* animation_to_receive_animation);
void PostSetLocalSurfaceIdToMainThread(
const viz::LocalSurfaceId& local_surface_id);
void PostRequestNewLocalSurfaceIdToMainThread();
void PostGetDeferMainFrameUpdateToMainThread(
std::unique_ptr<ScopedDeferMainFrameUpdate>*
scoped_defer_main_frame_update);
void PostReturnDeferMainFrameUpdateToMainThread(
std::unique_ptr<ScopedDeferMainFrameUpdate>
scoped_defer_main_frame_update);
void PostDeferringCommitsStatusToMainThread(bool is_deferring_commits);
void PostSetNeedsCommitToMainThread();
void PostSetNeedsUpdateLayersToMainThread();
void PostSetNeedsRedrawToMainThread();
void PostSetNeedsRedrawRectToMainThread(const gfx::Rect& damage_rect);
void PostSetVisibleToMainThread(bool visible);
void PostSetNeedsCommitWithForcedRedrawToMainThread();
void PostCompositeImmediatelyToMainThread();
void PostNextCommitWaitsForActivationToMainThread();
void DoBeginTest();
void Timeout();
AnimationHost* animation_host() const { return animation_host_.get(); }
void SetUseLayerLists() { settings_.use_layer_lists = true; }
protected:
explicit LayerTreeTest(
viz::RendererType renderer_type = viz::RendererType::kGL);
void SkipAllocateInitialLocalSurfaceId();
const viz::LocalSurfaceId& GetCurrentLocalSurfaceId() const;
void GenerateNewLocalSurfaceId();
virtual void InitializeSettings(LayerTreeSettings* settings) {}
void RealEndTest();
std::unique_ptr<LayerTreeFrameSink>
ReleaseLayerTreeFrameSinkOnLayerTreeHost();
void SetVisibleOnLayerTreeHost(bool visible);
void SetInitialDeviceScaleFactor(float initial_device_scale_factor) {
initial_device_scale_factor_ = initial_device_scale_factor;
}
// Used when LayerTreeTest::SetupTree() creates the root layer. Not used if
// the root layer is created before LayerTreeTest::SetupTree() is called.
// The default is 1x1.
void SetInitialRootBounds(const gfx::Size& bounds) {
initial_root_bounds_ = bounds;
}
virtual void AfterTest() {}
virtual void WillBeginTest();
virtual void BeginTest() = 0;
virtual void SetupTree();
virtual void RunTest(CompositorMode mode);
bool HasImplThread() const { return !!impl_thread_; }
base::SingleThreadTaskRunner* ImplThreadTaskRunner() {
return impl_task_runner_.get();
}
base::SingleThreadTaskRunner* MainThreadTaskRunner() const {
return main_task_runner_.get();
}
Proxy* proxy();
TaskRunnerProvider* task_runner_provider() const;
TaskGraphRunner* task_graph_runner() const {
return task_graph_runner_.get();
}
bool TestEnded() const {
base::AutoLock hold(test_ended_lock_);
return ended_;
}
LayerTreeHost* layer_tree_host() const;
gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() {
return gpu_memory_buffer_manager_.get();
}
void DestroyLayerTreeHost();
// By default, output surface recreation is synchronous.
void RequestNewLayerTreeFrameSink() override;
// Override this to modify the TestContextProviders before they are bound
// and used. Override CreateLayerTreeFrameSink() instead if the test does not
// want to use TestContextProviders.
virtual void SetUpUnboundContextProviders(
viz::TestContextProvider* context_provider,
viz::TestContextProvider* worker_context_provider);
// Override this and call the base class to change what viz::ContextProviders
// will be used (such as for pixel tests). Or override it and create your own
// TestLayerTreeFrameSink to control how it is created.
virtual std::unique_ptr<TestLayerTreeFrameSink> CreateLayerTreeFrameSink(
const viz::RendererSettings& renderer_settings,
double refresh_rate,
scoped_refptr<viz::ContextProvider> compositor_context_provider,
scoped_refptr<viz::RasterContextProvider> worker_context_provider);
std::unique_ptr<viz::DisplayCompositorMemoryAndTaskController>
CreateDisplayControllerOnThread() override;
std::unique_ptr<viz::SkiaOutputSurface>
CreateDisplaySkiaOutputSurfaceOnThread(
viz::DisplayCompositorMemoryAndTaskController*) override;
// Override this and call the base class to change what viz::ContextProvider
// will be used, such as to prevent sharing the context with the
// LayerTreeFrameSink. Or override it and create your own OutputSurface to
// change what type of OutputSurface is used, such as a real OutputSurface for
// pixel tests or a software-compositing OutputSurface.
std::unique_ptr<viz::OutputSurface> CreateDisplayOutputSurfaceOnThread(
scoped_refptr<viz::ContextProvider> compositor_context_provider) override;
base::SingleThreadTaskRunner* image_worker_task_runner() const {
return image_worker_->task_runner().get();
}
void UseBeginFrameSource(viz::BeginFrameSource* begin_frame_source) {
begin_frame_source_ = begin_frame_source;
}
bool use_skia_renderer() const {
return renderer_type_ == viz::RendererType::kSkiaGL ||
renderer_type_ == viz::RendererType::kSkiaVk ||
renderer_type_ == viz::RendererType::kSkiaDawn;
}
bool use_software_renderer() const {
return renderer_type_ == viz::RendererType::kSoftware;
}
bool use_skia_vulkan() const {
return renderer_type_ == viz::RendererType::kSkiaVk;
}
bool use_d3d12() const {
#if BUILDFLAG(IS_WIN)
return renderer_type_ == viz::RendererType::kSkiaDawn;
#else
return false;
#endif
}
bool use_swangle() const;
const viz::RendererType renderer_type_;
const viz::DebugRendererSettings debug_settings_;
private:
virtual void DispatchAddNoDamageAnimation(
Animation* animation_to_receive_animation,
double animation_duration);
virtual void DispatchAddOpacityAnimation(
Animation* animation_to_receive_animation,
double animation_duration);
void DispatchSetLocalSurfaceId(const viz::LocalSurfaceId& local_surface_id);
void DispatchRequestNewLocalSurfaceId();
void DispatchGetDeferMainFrameUpdate(
std::unique_ptr<ScopedDeferMainFrameUpdate>*
scoped_defer_main_frame_update);
void DispatchReturnDeferMainFrameUpdate(
std::unique_ptr<ScopedDeferMainFrameUpdate>
scoped_defer_main_frame_update);
void DispatchDeferringCommitsStatus(bool is_deferring_commits);
void DispatchSetNeedsCommit();
void DispatchSetNeedsUpdateLayers();
void DispatchSetNeedsRedraw();
void DispatchSetNeedsRedrawRect(const gfx::Rect& damage_rect);
void DispatchSetVisible(bool visible);
void DispatchSetNeedsCommitWithForcedRedraw();
void DispatchDidAddAnimation();
void DispatchCompositeImmediately();
void DispatchNextCommitWaitsForActivation();
// |scoped_feature_list_| must be the first member to ensure that it is
// destroyed after any member that might be using it.
base::test::ScopedFeatureList scoped_feature_list_;
viz::TestGpuServiceHolder::ScopedResetter gpu_service_resetter_;
LayerTreeSettings settings_;
float initial_device_scale_factor_ = 1.f;
gfx::Size initial_root_bounds_;
CompositorMode mode_;
std::unique_ptr<LayerTreeHostClientForTesting> client_;
std::unique_ptr<LayerTreeHost> layer_tree_host_;
std::unique_ptr<AnimationHost> animation_host_;
bool beginning_ = false;
bool end_when_begin_returns_ = false;
bool timed_out_ = false;
bool scheduled_ = false;
bool started_ = false;
mutable base::Lock test_ended_lock_;
bool ended_ = false;
int timeout_seconds_ = 0;
raw_ptr<viz::BeginFrameSource> begin_frame_source_ = nullptr; // NOT OWNED.
std::unique_ptr<LayerTreeTestLayerTreeFrameSinkClient>
layer_tree_frame_sink_client_;
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_;
std::unique_ptr<base::Thread> impl_thread_;
std::unique_ptr<base::Thread> image_worker_;
std::unique_ptr<viz::TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
std::unique_ptr<TestTaskGraphRunner> task_graph_runner_;
base::CancelableOnceClosure timeout_;
scoped_refptr<viz::TestContextProvider> compositor_contexts_;
bool skip_allocate_initial_local_surface_id_ = false;
viz::ParentLocalSurfaceIdAllocator allocator_;
base::WeakPtr<LayerTreeTest> main_thread_weak_ptr_;
base::WeakPtrFactory<LayerTreeTest> weak_factory_{this};
};
} // namespace cc
// Do not change this macro to disable a test, it will disable half of
// the unit test suite. Instead, comment out the usage of this macro for
// a specific test name. eg.
// // TODO(crbug.com/abcd): Disabled for some reasons stated here.
// // SINGLE_AND_MULTI_THREAD_TEST_F(SomeRandomTest)
#define SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME) \
TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRenderer) { \
RunTest(CompositorMode::SINGLE_THREADED); \
} \
TEST_F(TEST_FIXTURE_NAME, RunSingleThread_DelegatingRendererUnifiedScroll) { \
base::test::ScopedFeatureList scoped_feature_list; \
scoped_feature_list.InitAndEnableFeature(features::kScrollUnification); \
RunTest(CompositorMode::SINGLE_THREADED); \
} \
class SingleThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
// Do not change this macro to disable a test, it will disable half of
// the unit test suite. Instead, comment out the usage of this macro for
// a specific test name. eg.
// // TODO(crbug.com/abcd): Disabled for some reasons stated here.
// // SINGLE_AND_MULTI_THREAD_TEST_F(SomeRandomTest)
#define MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRenderer) { \
RunTest(CompositorMode::THREADED); \
} \
TEST_F(TEST_FIXTURE_NAME, RunMultiThread_DelegatingRendererUnifiedScroll) { \
base::test::ScopedFeatureList scoped_feature_list; \
scoped_feature_list.InitAndEnableFeature(features::kScrollUnification); \
RunTest(CompositorMode::THREADED); \
} \
class MultiThreadDelegatingImplNeedsSemicolon##TEST_FIXTURE_NAME {}
// Do not change this macro to disable a test, it will disable half of
// the unit test suite. Instead, comment out the usage of this macro for
// a specific test name. eg.
// // TODO(crbug.com/abcd): Disabled for some reasons stated here.
// // SINGLE_AND_MULTI_THREAD_TEST_F(SomeRandomTest)
#define SINGLE_AND_MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME) \
SINGLE_THREAD_TEST_F(TEST_FIXTURE_NAME); \
MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
// Some tests want to control when notify ready for activation occurs,
// but this is not supported in the single-threaded case.
//
// Do not change this macro to disable a test, it will disable half of
// the unit test suite. Instead, comment out the usage of this macro for
// a specific test name. eg.
// // TODO(crbug.com/abcd): Disabled for some reasons stated here.
// // MULTI_THREAD_BLOCKNOTIFY_TEST_F(SomeRandomTest)
#define MULTI_THREAD_BLOCKNOTIFY_TEST_F(TEST_FIXTURE_NAME) \
MULTI_THREAD_TEST_F(TEST_FIXTURE_NAME)
#endif // CC_TEST_LAYER_TREE_TEST_H_