Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ffbe2a4

Browse files
authored
[testing] Move test vsync waiters to their own TUs (#14456)
This move makes it easier to add more vsync waiters.
1 parent 0c24f3d commit ffbe2a4

File tree

6 files changed

+110
-66
lines changed

6 files changed

+110
-66
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ FILE: ../../../flutter/shell/common/surface.cc
568568
FILE: ../../../flutter/shell/common/surface.h
569569
FILE: ../../../flutter/shell/common/switches.cc
570570
FILE: ../../../flutter/shell/common/switches.h
571+
FILE: ../../../flutter/shell/common/test_vsync_waiters.cc
572+
FILE: ../../../flutter/shell/common/test_vsync_waiters.h
571573
FILE: ../../../flutter/shell/common/thread_host.cc
572574
FILE: ../../../flutter/shell/common/thread_host.h
573575
FILE: ../../../flutter/shell/common/vsync_waiter.cc

shell/common/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ if (current_toolchain == host_toolchain) {
166166
"shell_test.cc",
167167
"shell_test.h",
168168
"shell_unittests.cc",
169+
"test_vsync_waiters.cc",
170+
"test_vsync_waiters.h",
169171
]
170172

171173
deps = [

shell/common/shell_test.cc

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -289,46 +289,6 @@ void ShellTest::AddNativeCallback(std::string name,
289289
native_resolver_->AddNativeCallback(std::move(name), callback);
290290
}
291291

292-
void ShellTestVsyncClock::SimulateVSync() {
293-
std::scoped_lock lock(mutex_);
294-
if (vsync_issued_ >= vsync_promised_.size()) {
295-
vsync_promised_.emplace_back();
296-
}
297-
FML_CHECK(vsync_issued_ < vsync_promised_.size());
298-
vsync_promised_[vsync_issued_].set_value(vsync_issued_);
299-
vsync_issued_ += 1;
300-
}
301-
302-
std::future<int> ShellTestVsyncClock::NextVSync() {
303-
std::scoped_lock lock(mutex_);
304-
vsync_promised_.emplace_back();
305-
return vsync_promised_.back().get_future();
306-
}
307-
308-
void ShellTestVsyncWaiter::AwaitVSync() {
309-
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
310-
auto vsync_future = clock_.NextVSync();
311-
312-
auto async_wait = std::async([&vsync_future, this]() {
313-
vsync_future.wait();
314-
315-
// Post the `FireCallback` to the Platform thread so earlier Platform tasks
316-
// (specifically, the `VSyncFlush` call) will be finished before
317-
// `FireCallback` is executed. This is only needed for our unit tests.
318-
//
319-
// Without this, the repeated VSYNC signals in `VSyncFlush` may start both
320-
// the current frame in the UI thread and the next frame in the secondary
321-
// callback (both of them are waiting for VSYNCs). That breaks the unit
322-
// test's assumption that each frame's VSYNC must be issued by different
323-
// `VSyncFlush` call (which resets the `will_draw_new_frame` bit).
324-
//
325-
// For example, HandlesActualIphoneXsInputEvents will fail without this.
326-
task_runners_.GetPlatformTaskRunner()->PostTask([this]() {
327-
FireCallback(fml::TimePoint::Now(), fml::TimePoint::Now());
328-
});
329-
});
330-
}
331-
332292
ShellTestPlatformView::ShellTestPlatformView(PlatformView::Delegate& delegate,
333293
TaskRunners task_runners,
334294
bool simulate_vsync)

shell/common/shell_test.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flutter/lib/ui/window/platform_message.h"
1414
#include "flutter/shell/common/run_configuration.h"
1515
#include "flutter/shell/common/shell.h"
16+
#include "flutter/shell/common/test_vsync_waiters.h"
1617
#include "flutter/shell/common/thread_host.h"
1718
#include "flutter/shell/gpu/gpu_surface_gl_delegate.h"
1819
#include "flutter/testing/test_dart_native_resolver.h"
@@ -88,32 +89,6 @@ class ShellTest : public ThreadTest {
8889
FML_DISALLOW_COPY_AND_ASSIGN(ShellTest);
8990
};
9091

91-
class ShellTestVsyncClock {
92-
public:
93-
/// Simulate that a vsync signal is triggered.
94-
void SimulateVSync();
95-
96-
/// A future that will return the index the next vsync signal.
97-
std::future<int> NextVSync();
98-
99-
private:
100-
std::mutex mutex_;
101-
std::vector<std::promise<int>> vsync_promised_;
102-
size_t vsync_issued_ = 0;
103-
};
104-
105-
class ShellTestVsyncWaiter : public VsyncWaiter {
106-
public:
107-
ShellTestVsyncWaiter(TaskRunners task_runners, ShellTestVsyncClock& clock)
108-
: VsyncWaiter(std::move(task_runners)), clock_(clock) {}
109-
110-
protected:
111-
void AwaitVSync() override;
112-
113-
private:
114-
ShellTestVsyncClock& clock_;
115-
};
116-
11792
class ShellTestPlatformView : public PlatformView, public GPUSurfaceGLDelegate {
11893
public:
11994
ShellTestPlatformView(PlatformView::Delegate& delegate,

shell/common/test_vsync_waiters.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2013 The Flutter 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+
#define FML_USED_ON_EMBEDDER
6+
7+
#include "flutter/shell/common/shell_test.h"
8+
9+
#include "flutter/flow/layers/layer_tree.h"
10+
#include "flutter/flow/layers/transform_layer.h"
11+
#include "flutter/fml/make_copyable.h"
12+
#include "flutter/fml/mapping.h"
13+
#include "flutter/runtime/dart_vm.h"
14+
#include "flutter/shell/gpu/gpu_surface_gl.h"
15+
#include "flutter/testing/testing.h"
16+
17+
namespace flutter {
18+
namespace testing {
19+
20+
void ShellTestVsyncClock::SimulateVSync() {
21+
std::scoped_lock lock(mutex_);
22+
if (vsync_issued_ >= vsync_promised_.size()) {
23+
vsync_promised_.emplace_back();
24+
}
25+
FML_CHECK(vsync_issued_ < vsync_promised_.size());
26+
vsync_promised_[vsync_issued_].set_value(vsync_issued_);
27+
vsync_issued_ += 1;
28+
}
29+
30+
std::future<int> ShellTestVsyncClock::NextVSync() {
31+
std::scoped_lock lock(mutex_);
32+
vsync_promised_.emplace_back();
33+
return vsync_promised_.back().get_future();
34+
}
35+
36+
void ShellTestVsyncWaiter::AwaitVSync() {
37+
FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread());
38+
auto vsync_future = clock_.NextVSync();
39+
40+
auto async_wait = std::async([&vsync_future, this]() {
41+
vsync_future.wait();
42+
43+
// Post the `FireCallback` to the Platform thread so earlier Platform tasks
44+
// (specifically, the `VSyncFlush` call) will be finished before
45+
// `FireCallback` is executed. This is only needed for our unit tests.
46+
//
47+
// Without this, the repeated VSYNC signals in `VSyncFlush` may start both
48+
// the current frame in the UI thread and the next frame in the secondary
49+
// callback (both of them are waiting for VSYNCs). That breaks the unit
50+
// test's assumption that each frame's VSYNC must be issued by different
51+
// `VSyncFlush` call (which resets the `will_draw_new_frame` bit).
52+
//
53+
// For example, HandlesActualIphoneXsInputEvents will fail without this.
54+
task_runners_.GetPlatformTaskRunner()->PostTask([this]() {
55+
FireCallback(fml::TimePoint::Now(), fml::TimePoint::Now());
56+
});
57+
});
58+
}
59+
60+
} // namespace testing
61+
} // namespace flutter

shell/common/test_vsync_waiters.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Flutter 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+
#define FML_USED_ON_EMBEDDER
6+
7+
#ifndef FLUTTER_SHELL_COMMON_TEST_VSYNC_WAITERS_H_
8+
#define FLUTTER_SHELL_COMMON_TEST_VSYNC_WAITERS_H_
9+
10+
#include "flutter/shell/common/shell.h"
11+
12+
namespace flutter {
13+
namespace testing {
14+
15+
class ShellTestVsyncClock {
16+
public:
17+
/// Simulate that a vsync signal is triggered.
18+
void SimulateVSync();
19+
20+
/// A future that will return the index the next vsync signal.
21+
std::future<int> NextVSync();
22+
23+
private:
24+
std::mutex mutex_;
25+
std::vector<std::promise<int>> vsync_promised_;
26+
size_t vsync_issued_ = 0;
27+
};
28+
29+
class ShellTestVsyncWaiter : public VsyncWaiter {
30+
public:
31+
ShellTestVsyncWaiter(TaskRunners task_runners, ShellTestVsyncClock& clock)
32+
: VsyncWaiter(std::move(task_runners)), clock_(clock) {}
33+
34+
protected:
35+
void AwaitVSync() override;
36+
37+
private:
38+
ShellTestVsyncClock& clock_;
39+
};
40+
41+
} // namespace testing
42+
} // namespace flutter
43+
44+
#endif // FLUTTER_SHELL_COMMON_TEST_VSYNC_WAITERS_H_

0 commit comments

Comments
 (0)