diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.cc index f1f11c6b91..15023ead54 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.cc @@ -37,13 +37,20 @@ FlatlandConnection::~FlatlandConnection() = default; // This method is called from the raster thread. void FlatlandConnection::Present() { - // TODO(fxbug.dev/64201): Consider a more complex presentation loop that - // accumulates Present calls until OnNextFrameBegin. - if (present_credits_ == 0) - return; + if (present_credits_ > 0) { + DoPresent(); + } else { + present_pending_ = true; + } +} +// This method is called from the raster thread. +void FlatlandConnection::DoPresent() { + FML_CHECK(present_credits_ > 0); --present_credits_; + fuchsia::ui::composition::PresentArgs present_args; + // TODO(fxbug.dev/64201): compute a better presentation time; present_args.set_requested_presentation_time(0); present_args.set_acquire_fences(std::move(acquire_fences_)); present_args.set_release_fences(std::move(previous_present_release_fences_)); @@ -100,7 +107,12 @@ void FlatlandConnection::OnNextFrameBegin( fuchsia::ui::composition::OnNextFrameBeginValues values) { present_credits_ += values.additional_present_credits(); - { + if (present_pending_ && present_credits_ > 0) { + DoPresent(); + present_pending_ = false; + } + + if (present_credits_ > 0) { std::scoped_lock lock(threadsafe_state_.mutex_); if (threadsafe_state_.fire_callback_) { fml::TimePoint now = fml::TimePoint::Now(); diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.h b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.h index a3ed4cc11a..2a1e2707be 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.h +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/flatland_connection.h @@ -67,6 +67,7 @@ class FlatlandConnection final { void OnNextFrameBegin( fuchsia::ui::composition::OnNextFrameBeginValues values); void OnFramePresented(fuchsia::scenic::scheduling::FramePresentedInfo info); + void DoPresent(); fuchsia::ui::composition::FlatlandPtr flatland_; @@ -77,6 +78,7 @@ class FlatlandConnection final { on_frame_presented_event on_frame_presented_callback_; uint32_t present_credits_ = 1; + bool present_pending_ = false; // This struct contains state that is accessed from both from the UI thread // (in AwaitVsync) and the raster thread (in OnNextFrameBegin). You should diff --git a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_connection_unittests.cc b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_connection_unittests.cc index 6e02365489..f6e81e412c 100644 --- a/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_connection_unittests.cc +++ b/engine/src/flutter/shell/platform/fuchsia/flutter/tests/flatland_connection_unittests.cc @@ -271,4 +271,151 @@ TEST_F(FlatlandConnectionTest, OutOfOrderAwait) { EXPECT_TRUE(await_vsync_callback_fired); } +TEST_F(FlatlandConnectionTest, PresentCreditExhaustion) { + // Set up callbacks which allow sensing of how many presents were handled. + size_t num_presents_called = 0u; + size_t num_release_fences = 0u; + size_t num_acquire_fences = 0u; + + auto reset_test_counters = [&num_presents_called, &num_acquire_fences, + &num_release_fences]() { + num_presents_called = 0u; + num_release_fences = 0u; + num_acquire_fences = 0u; + }; + + fake_flatland().SetPresentHandler( + [&num_presents_called, &num_acquire_fences, &num_release_fences]( + fuchsia::ui::composition::PresentArgs present_args) { + num_presents_called++; + num_acquire_fences = present_args.acquire_fences().size(); + num_release_fences = present_args.release_fences().size(); + }); + + // Create the FlatlandConnection but don't pump the loop. No FIDL calls are + // completed yet. + on_frame_presented_event on_frame_presented = [](auto...) {}; + flutter_runner::FlatlandConnection flatland_connection( + GetCurrentTestName(), TakeFlatlandHandle(), []() { FAIL(); }, + std::move(on_frame_presented), 1, fml::TimeDelta::Zero()); + EXPECT_EQ(num_presents_called, 0u); + + // Pump the loop. Nothing is called. + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 0u); + + // Simulate an AwaitVsync that comes after the first call. + flatland_connection.AwaitVsync([](fml::TimePoint, fml::TimePoint) {}); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 0u); + + // This test uses a fire callback that triggers Present() with a single + // acquire and release fence in order to approximate the behavior of the real + // flutter fire callback and let us drive presents with ONFBs + auto fire_callback = [&flatland_connection](fml::TimePoint frame_start, + fml::TimePoint frame_end) { + zx::event acquire_fence; + zx::event::create(0, &acquire_fence); + zx::event release_fence; + zx::event::create(0, &release_fence); + flatland_connection.EnqueueAcquireFence(std::move(acquire_fence)); + flatland_connection.EnqueueReleaseFence(std::move(release_fence)); + flatland_connection.Present(); + }; + + // Call Await Vsync with a callback that triggers Present, but this should not + // present until ONFB is delivered below. + reset_test_counters(); + flatland_connection.AwaitVsync(fire_callback); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 0u); + + // Call ONFB ands supply 0 present credits. This causes `Present` to be + // called and consumes the one and only present credit we start with. + OnNextFrameBegin(0); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 1u); + EXPECT_EQ(num_acquire_fences, 1u); + EXPECT_EQ(num_release_fences, 0u); + + // Do it again, but this time we should not get a present because the client + // has exhausted its present credits. + reset_test_counters(); + flatland_connection.AwaitVsync(fire_callback); + OnNextFrameBegin(0); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 0u); + + // Supply a present credit but dont set a new fire callback. Fire callback + // from previous ONFB should fire and trigger a Present() + reset_test_counters(); + OnNextFrameBegin(1); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 1u); + EXPECT_EQ(num_acquire_fences, 1u); + EXPECT_EQ(num_release_fences, 1u); + + // From here on we are testing handling of a race condition where a fire + // callback is fired but another ONFB arrives before the present from the + // first fire callback comes in, causing present_credits to be negative + // within Present(). + + uint num_onfb = 5; + uint num_deferred_callbacks = 0; + // This callback will accumulate num_onfb+1 calls before firing all + // of their presents at once. + auto accumulating_fire_callback = [&](fml::TimePoint frame_start, + fml::TimePoint frame_end) { + num_deferred_callbacks++; + if (num_deferred_callbacks > num_onfb) { + fml::TimePoint now = fml::TimePoint::Now(); + for (uint i = 0; i < num_onfb + 1; i++) { + fire_callback(now, now); + num_deferred_callbacks--; + } + } + }; + + reset_test_counters(); + for (uint i = 0; i < num_onfb; i++) { + flatland_connection.AwaitVsync(accumulating_fire_callback); + // only supply a present credit on the first call. Since Presents are being + // deferred this credit will not be used up, but we need a credit to call + // the accumulating_fire_callback + OnNextFrameBegin(i == 0 ? 1 : 0); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 0u); + } + + // This is the num_onfb+1 call to accumulating_fire_callback which triggers + // all of the "racing" presents to fire. the first one should be fired, + // but the other num_onfb Presents should be deferred. + flatland_connection.AwaitVsync(accumulating_fire_callback); + OnNextFrameBegin(0); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 1u); + EXPECT_EQ(num_acquire_fences, 1u); + EXPECT_EQ(num_release_fences, 1u); + + // Supply a present credit, but pass an empty lambda to AwaitVsync so + // that it doesnt call Present(). Should get a deferred present with + // all the accumulate acuire fences + reset_test_counters(); + flatland_connection.AwaitVsync([](fml::TimePoint, fml::TimePoint) {}); + OnNextFrameBegin(1); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 1u); + EXPECT_EQ(num_acquire_fences, num_onfb); + EXPECT_EQ(num_release_fences, 1u); + + // Pump another frame to check that release fences accumulate as expected + reset_test_counters(); + flatland_connection.AwaitVsync(fire_callback); + OnNextFrameBegin(1); + loop().RunUntilIdle(); + EXPECT_EQ(num_presents_called, 1u); + EXPECT_EQ(num_acquire_fences, 1u); + EXPECT_EQ(num_release_fences, num_onfb); +} + } // namespace flutter_runner::testing