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

Commit ff25f35

Browse files
committed
re-implement VsyncWaiter - all tests and rebased
1 parent aa5c033 commit ff25f35

17 files changed

+673
-176
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,6 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.cc
13661366
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_recorder.h
13671367
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_waiter.cc
13681368
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_waiter.h
1369-
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vsync_waiter_unittests.cc
13701369
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vulkan_surface.cc
13711370
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vulkan_surface.h
13721371
FILE: ../../../flutter/shell/platform/fuchsia/flutter/vulkan_surface_pool.cc

shell/common/animator.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void Animator::BeginFrame(
124124
// If we still don't have valid continuation, the pipeline is currently
125125
// full because the consumer is being too slow. Try again at the next
126126
// frame interval.
127+
TRACE_EVENT0("flutter", "PipelineFull");
127128
RequestFrame();
128129
return;
129130
}

shell/platform/fuchsia/flutter/BUILD.gn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ executable("flutter_runner_unittests") {
456456
"tests/engine_unittests.cc",
457457
"tests/flutter_runner_product_configuration_unittests.cc",
458458
"tests/vsync_recorder_unittests.cc",
459-
"vsync_waiter_unittests.cc",
459+
"tests/vsync_waiter_unittests.cc",
460+
"tests/fake_session_connection.h",
460461
]
461462

462463
# This is needed for //third_party/googletest for linking zircon symbols.

shell/platform/fuchsia/flutter/default_session_connection.cc

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@ DefaultSessionConnection::DefaultSessionConnection(
1717
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session,
1818
fml::closure session_error_callback,
1919
on_frame_presented_event on_frame_presented_callback,
20-
zx_handle_t vsync_event_handle,
2120
uint64_t max_frames_in_flight)
2221
: session_wrapper_(session.Bind(), nullptr),
2322
on_frame_presented_callback_(std::move(on_frame_presented_callback)),
24-
vsync_event_handle_(vsync_event_handle),
2523
kMaxFramesInFlight(max_frames_in_flight) {
2624
session_wrapper_.set_error_handler(
2725
[callback = session_error_callback](zx_status_t status) { callback(); });
2826

2927
// Set the |fuchsia::ui::scenic::OnFramePresented()| event handler that will
3028
// fire every time a set of one or more frames is presented.
3129
session_wrapper_.set_on_frame_presented_handler(
32-
[this, handle = vsync_event_handle_](
33-
fuchsia::scenic::scheduling::FramePresentedInfo info) {
30+
[this](fuchsia::scenic::scheduling::FramePresentedInfo info) {
3431
// Update Scenic's limit for our remaining frames in flight allowed.
3532
size_t num_presents_handled = info.presentation_infos.size();
3633
frames_in_flight_allowed_ = info.num_presents_allowed;
3734

3835
// A frame was presented: Update our |frames_in_flight| to match the
3936
// updated unfinalized present requests.
4037
frames_in_flight_ -= num_presents_handled;
41-
TRACE_EVENT2("gfx", "DSC::OnFramePresented", "frames_in_flight",
42-
frames_in_flight_, "max_frames_in_flight",
43-
kMaxFramesInFlight);
38+
TRACE_DURATION("gfx", "OnFramePresented", "frames_in_flight",
39+
frames_in_flight_, "max_frames_in_flight",
40+
kMaxFramesInFlight, "num_presents_handled",
41+
num_presents_handled);
4442
FML_DCHECK(frames_in_flight_ >= 0);
4543

4644
VsyncRecorder::GetInstance().UpdateFramePresentedInfo(
@@ -52,7 +50,10 @@ DefaultSessionConnection::DefaultSessionConnection(
5250
if (present_session_pending_) {
5351
PresentSession();
5452
}
55-
ToggleSignal(handle, true);
53+
54+
if (vsync_waiter_initialized_) {
55+
vsync_waiter_callback_();
56+
}
5657
} // callback
5758
);
5859

@@ -70,8 +71,6 @@ DefaultSessionConnection::DefaultSessionConnection(
7071
VsyncRecorder::GetInstance().UpdateNextPresentationInfo(
7172
std::move(info));
7273

73-
// Signal is initially high indicating availability of the session.
74-
ToggleSignal(vsync_event_handle_, true);
7574
initialized_ = true;
7675

7776
PresentSession();
@@ -101,7 +100,6 @@ void DefaultSessionConnection::Present() {
101100
FML_CHECK(frames_in_flight_ <= kMaxFramesInFlight);
102101

103102
present_session_pending_ = true;
104-
ToggleSignal(vsync_event_handle_, false);
105103
}
106104
}
107105

@@ -197,15 +195,12 @@ void DefaultSessionConnection::PresentSession() {
197195
});
198196
}
199197

200-
void DefaultSessionConnection::ToggleSignal(zx_handle_t handle, bool set) {
201-
const auto signal = VsyncWaiter::SessionPresentSignal;
202-
auto status = zx_object_signal(handle, // handle
203-
set ? 0 : signal, // clear mask
204-
set ? signal : 0 // set mask
205-
);
206-
if (status != ZX_OK) {
207-
FML_LOG(ERROR) << "Could not toggle vsync signal: " << set;
208-
}
198+
void DefaultSessionConnection::InitializeVsyncWaiterCallback(
199+
VsyncWaiterCallback callback) {
200+
FML_CHECK(!vsync_waiter_initialized_);
201+
202+
vsync_waiter_callback_ = callback;
203+
vsync_waiter_initialized_ = true;
209204
}
210205

211206
} // namespace flutter_runner

shell/platform/fuchsia/flutter/default_session_connection.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
#include "flutter/fml/closure.h"
1515
#include "flutter/fml/macros.h"
1616

17+
#include "session_connection.h"
18+
1719
namespace flutter_runner {
1820

1921
using on_frame_presented_event =
2022
std::function<void(fuchsia::scenic::scheduling::FramePresentedInfo)>;
2123

2224
// The component residing on the raster thread that is responsible for
2325
// maintaining the Scenic session connection and presenting node updates.
24-
class DefaultSessionConnection final : public flutter::SessionWrapper {
26+
class DefaultSessionConnection final
27+
: public flutter::SessionWrapper,
28+
public flutter_runner::SessionConnection {
2529
public:
2630
DefaultSessionConnection(
2731
std::string debug_label,
2832
fidl::InterfaceHandle<fuchsia::ui::scenic::Session> session,
2933
fml::closure session_error_callback,
3034
on_frame_presented_event on_frame_presented_callback,
31-
zx_handle_t vsync_event_handle,
3235
uint64_t max_frames_in_flight);
3336

3437
~DefaultSessionConnection();
@@ -45,6 +48,12 @@ class DefaultSessionConnection final : public flutter::SessionWrapper {
4548
std::deque<std::pair<fml::TimePoint, fml::TimePoint>>&
4649
future_presentation_infos);
4750

51+
void InitializeVsyncWaiterCallback(VsyncWaiterCallback callback) override;
52+
53+
bool CanRequestNewFrames() override {
54+
return frames_in_flight_ < kMaxFramesInFlight;
55+
}
56+
4857
private:
4958
scenic::Session session_wrapper_;
5059

@@ -80,6 +89,9 @@ class DefaultSessionConnection final : public flutter::SessionWrapper {
8089

8190
bool present_session_pending_ = false;
8291

92+
bool vsync_waiter_initialized_ = false;
93+
VsyncWaiterCallback vsync_waiter_callback_;
94+
8395
void PresentSession();
8496

8597
static void ToggleSignal(zx_handle_t handle, bool raise);

shell/platform/fuchsia/flutter/engine.cc

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ Engine::Engine(Delegate& delegate,
7676
#endif
7777
intercept_all_input_(product_config.get_intercept_all_input()),
7878
weak_factory_(this) {
79-
if (zx::event::create(0, &vsync_event_) != ZX_OK) {
80-
FML_DLOG(ERROR) << "Could not create the vsync event.";
81-
return;
82-
}
8379

8480
// Get the task runners from the managed threads. The current thread will be
8581
// used as the "platform" thread.
@@ -138,26 +134,27 @@ Engine::Engine(Delegate& delegate,
138134
view_token = std::move(view_token),
139135
view_ref_pair = std::move(view_ref_pair),
140136
max_frames_in_flight = product_config.get_max_frames_in_flight(),
141-
vsync_handle = vsync_event_.get(), &view_embedder_latch]() mutable {
142-
session_connection_.emplace(
137+
&view_embedder_latch]() mutable {
138+
FML_LOG(INFO) << "FELIPE: made shared";
139+
session_connection_ = std::make_shared<DefaultSessionConnection>(
143140
thread_label_, std::move(session),
144-
std::move(session_error_callback), [](auto) {}, vsync_handle,
141+
std::move(session_error_callback), [](auto) {},
145142
max_frames_in_flight);
146143
surface_producer_.emplace(session_connection_->get());
147144
#if defined(LEGACY_FUCHSIA_EMBEDDER)
148145
if (use_legacy_renderer_) {
149146
legacy_external_view_embedder_ =
150147
std::make_shared<flutter::SceneUpdateContext>(
151148
thread_label_, std::move(view_token),
152-
std::move(view_ref_pair), session_connection_.value(),
149+
std::move(view_ref_pair), *(session_connection_.get()),
153150
intercept_all_input_);
154151
} else
155152
#endif
156153
{
157154
external_view_embedder_ =
158155
std::make_shared<FuchsiaExternalViewEmbedder>(
159156
thread_label_, std::move(view_token),
160-
std::move(view_ref_pair), session_connection_.value(),
157+
std::move(view_ref_pair), *session_connection_.get(),
161158
surface_producer_.value(), intercept_all_input_);
162159
}
163160
view_embedder_latch.Signal();
@@ -232,8 +229,8 @@ Engine::Engine(Delegate& delegate,
232229
// Setup the callback that will instantiate the platform view.
233230
flutter::Shell::CreateCallback<flutter::PlatformView>
234231
on_create_platform_view = fml::MakeCopyable(
235-
[debug_label = thread_label_, view_ref = std::move(platform_view_ref),
236-
runner_services,
232+
[this, debug_label = thread_label_,
233+
view_ref = std::move(platform_view_ref), runner_services,
237234
parent_environment_service_provider =
238235
std::move(parent_environment_service_provider),
239236
session_listener_request = std::move(session_listener_request),
@@ -248,7 +245,6 @@ Engine::Engine(Delegate& delegate,
248245
on_create_surface_callback = std::move(on_create_surface_callback),
249246
external_view_embedder = GetExternalViewEmbedder(),
250247
vsync_offset = product_config.get_vsync_offset(),
251-
vsync_handle = vsync_event_.get(),
252248
keyboard_listener_request = std::move(keyboard_listener_request)](
253249
flutter::Shell& shell) mutable {
254250
return std::make_unique<flutter_runner::PlatformView>(
@@ -271,7 +267,7 @@ Engine::Engine(Delegate& delegate,
271267
std::move(on_create_surface_callback),
272268
external_view_embedder, // external view embedder
273269
std::move(vsync_offset), // vsync offset
274-
vsync_handle);
270+
session_connection_);
275271
});
276272

277273
// Setup the callback that will instantiate the rasterizer.
@@ -295,7 +291,7 @@ Engine::Engine(Delegate& delegate,
295291

296292
auto compositor_context =
297293
std::make_unique<flutter_runner::CompositorContext>(
298-
session_connection_.value(), surface_producer_.value(),
294+
*(session_connection_.get()), surface_producer_.value(),
299295
legacy_external_view_embedder_);
300296
return std::make_unique<flutter::Rasterizer>(
301297
shell, std::move(compositor_context));

shell/platform/fuchsia/flutter/engine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Engine final {
7474
const std::string thread_label_;
7575
std::array<Thread, 3> threads_;
7676

77-
std::optional<DefaultSessionConnection> session_connection_;
77+
std::shared_ptr<DefaultSessionConnection> session_connection_;
7878
std::optional<VulkanSurfaceProducer> surface_producer_;
7979
std::shared_ptr<FuchsiaExternalViewEmbedder> external_view_embedder_;
8080
#if defined(LEGACY_FUCHSIA_EMBEDDER)

shell/platform/fuchsia/flutter/platform_view.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PlatformView::PlatformView(
7474
OnCreateSurface on_create_surface_callback,
7575
std::shared_ptr<flutter::ExternalViewEmbedder> external_view_embedder,
7676
fml::TimeDelta vsync_offset,
77-
zx_handle_t vsync_event_handle)
77+
std::shared_ptr<SessionConnection> session_connection)
7878
: flutter::PlatformView(delegate, std::move(task_runners)),
7979
debug_label_(std::move(debug_label)),
8080
view_ref_(std::move(view_ref)),
@@ -90,7 +90,7 @@ PlatformView::PlatformView(
9090
external_view_embedder_(external_view_embedder),
9191
ime_client_(this),
9292
vsync_offset_(std::move(vsync_offset)),
93-
vsync_event_handle_(vsync_event_handle),
93+
session_connection_(session_connection),
9494
keyboard_listener_binding_(this, std::move(keyboard_listener_request)),
9595
weak_factory_(this) {
9696
// Register all error handlers.
@@ -692,7 +692,7 @@ void PlatformView::DeactivateIme() {
692692
// |flutter::PlatformView|
693693
std::unique_ptr<flutter::VsyncWaiter> PlatformView::CreateVSyncWaiter() {
694694
return std::make_unique<flutter_runner::VsyncWaiter>(
695-
debug_label_, vsync_event_handle_, task_runners_, vsync_offset_);
695+
session_connection_, task_runners_, vsync_offset_);
696696
}
697697

698698
// |flutter::PlatformView|

shell/platform/fuchsia/flutter/platform_view.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class PlatformView final : public flutter::PlatformView,
7272
OnCreateSurface on_create_surface_callback,
7373
std::shared_ptr<flutter::ExternalViewEmbedder> view_embedder,
7474
fml::TimeDelta vsync_offset,
75-
zx_handle_t vsync_event_handle);
75+
std::shared_ptr<SessionConnection> session_connection);
7676

7777
~PlatformView();
7878

@@ -218,7 +218,7 @@ class PlatformView final : public flutter::PlatformView,
218218
std::set<std::string /* channel */> unregistered_channels_;
219219

220220
fml::TimeDelta vsync_offset_;
221-
zx_handle_t vsync_event_handle_ = 0;
221+
std::shared_ptr<SessionConnection> session_connection_;
222222

223223
// The registered binding for serving the keyboard listener server endpoint.
224224
fidl::Binding<fuchsia::ui::input3::KeyboardListener>

shell/platform/fuchsia/flutter/platform_view_unittest.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ class PlatformViewBuilder {
264264
std::move(on_update_view_callback_),
265265
std::move(on_destroy_view_callback_),
266266
std::move(on_create_surface_callback_), view_embedder_,
267-
std::move(vsync_offset_), vsync_event_handle_);
267+
std::move(vsync_offset_), nullptr);
268268
}
269269

270270
private:
@@ -295,7 +295,6 @@ class PlatformViewBuilder {
295295
OnCreateSurface on_create_surface_callback_{nullptr};
296296
std::shared_ptr<flutter::ExternalViewEmbedder> view_embedder_{nullptr};
297297
fml::TimeDelta vsync_offset_{fml::TimeDelta::Zero()};
298-
zx_handle_t vsync_event_handle_{ZX_HANDLE_INVALID};
299298
};
300299

301300
} // namespace

0 commit comments

Comments
 (0)