[fuchsia][flatland] Fix for AwaitVsync race (flutter/engine#28980)

* [fuchsia][flatland] regression test for AwaitVsync race problem

* [fuchsia][flatland] minimal fix for AwaitVsync race
This commit is contained in:
freiling
2021-10-04 17:54:04 -07:00
committed by GitHub
parent 858d6612e6
commit 6d0920a925
3 changed files with 139 additions and 12 deletions

View File

@@ -35,6 +35,7 @@ FlatlandConnection::FlatlandConnection(
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.
@@ -57,18 +58,36 @@ void FlatlandConnection::Present() {
acquire_fences_.clear();
}
// This method is called from the UI thread.
void FlatlandConnection::AwaitVsync(FireCallbackCallback callback) {
if (first_call) {
if (first_call_to_await_vsync_) {
fml::TimePoint now = fml::TimePoint::Now();
callback(now, now + kDefaultFlatlandPresentationInterval);
first_call = false;
first_call_to_await_vsync_ = false;
return;
}
fire_callback_ = callback;
{
std::scoped_lock<std::mutex> lock(threadsafe_state_.mutex_);
threadsafe_state_.fire_callback_ = callback;
if (threadsafe_state_.fire_callback_pending_) {
fml::TimePoint now = fml::TimePoint::Now();
// TODO(fxbug.dev/64201): Calculate correct frame times.
threadsafe_state_.fire_callback_(
now, now + kDefaultFlatlandPresentationInterval);
threadsafe_state_.fire_callback_ = nullptr;
threadsafe_state_.fire_callback_pending_ = false;
}
}
}
// This method is called from the UI thread.
void FlatlandConnection::AwaitVsyncForSecondaryCallback(
FireCallbackCallback callback) {}
FireCallbackCallback callback) {
fml::TimePoint now = fml::TimePoint::Now();
callback(now, now);
}
void FlatlandConnection::OnError(
fuchsia::ui::composition::FlatlandError error) {
@@ -76,26 +95,37 @@ void FlatlandConnection::OnError(
error_callback_();
}
// This method is called from the raster thread.
void FlatlandConnection::OnNextFrameBegin(
fuchsia::ui::composition::OnNextFrameBeginValues values) {
present_credits_ += values.additional_present_credits();
if (fire_callback_) {
fml::TimePoint now = fml::TimePoint::Now();
// TODO(fxbug.dev/64201): Calculate correct frame times.
fire_callback_(now, now + kDefaultFlatlandPresentationInterval);
{
std::scoped_lock<std::mutex> lock(threadsafe_state_.mutex_);
if (threadsafe_state_.fire_callback_) {
fml::TimePoint now = fml::TimePoint::Now();
// TODO(fxbug.dev/64201): Calculate correct frame times.
threadsafe_state_.fire_callback_(
now, now + kDefaultFlatlandPresentationInterval);
threadsafe_state_.fire_callback_ = nullptr;
} else {
threadsafe_state_.fire_callback_pending_ = true;
}
}
}
// This method is called from the raster thread.
void FlatlandConnection::OnFramePresented(
fuchsia::scenic::scheduling::FramePresentedInfo info) {
on_frame_presented_callback_(std::move(info));
}
// This method is called from the raster thread.
void FlatlandConnection::EnqueueAcquireFence(zx::event fence) {
acquire_fences_.push_back(std::move(fence));
}
// This method is called from the raster thread.
void FlatlandConnection::EnqueueReleaseFence(zx::event fence) {
current_present_release_fences_.push_back(std::move(fence));
}

View File

@@ -14,6 +14,7 @@
#include "vsync_waiter.h"
#include <cstdint>
#include <mutex>
#include <string>
namespace flutter_runner {
@@ -21,9 +22,9 @@ namespace flutter_runner {
using on_frame_presented_event =
std::function<void(fuchsia::scenic::scheduling::FramePresentedInfo)>;
// Assume a 60hz refresh rate.
// Set to zero for now for maximum simplicity until we have real values.
static constexpr fml::TimeDelta kDefaultFlatlandPresentationInterval =
fml::TimeDelta::FromSecondsF(1.0 / 60.0);
fml::TimeDelta::FromSecondsF(0);
// The component residing on the raster thread that is responsible for
// maintaining the Flatland instance connection and presenting updates.
@@ -42,6 +43,8 @@ class FlatlandConnection final {
void Present();
// Used to implement VsyncWaiter functionality.
// Note that these two methods are called from the UI thread while the
// rest of the methods on this class are called from the raster thread.
void AwaitVsync(FireCallbackCallback callback);
void AwaitVsyncForSecondaryCallback(FireCallbackCallback callback);
@@ -75,8 +78,16 @@ class FlatlandConnection final {
on_frame_presented_event on_frame_presented_callback_;
uint32_t present_credits_ = 1;
FireCallbackCallback fire_callback_;
bool first_call = true;
// This struct contains state that is accessed from both from the UI thread
// (in AwaitVsync) and the raster thread (in OnNextFrameBegin). You should
// always lock mutex_ before touching anything in this struct
struct {
std::mutex mutex_;
FireCallbackCallback fire_callback_;
bool fire_callback_pending_ = false;
} threadsafe_state_;
bool first_call_to_await_vsync_ = true;
std::vector<zx::event> acquire_fences_;
std::vector<zx::event> current_present_release_fences_;

View File

@@ -59,6 +59,15 @@ class FlatlandConnectionTest : public ::testing::Test {
return std::move(flatland_handle_);
}
// Syntactic sugar for OnNextFrameBegin
void OnNextFrameBegin(int num_present_credits) {
fuchsia::ui::composition::OnNextFrameBeginValues on_next_frame_begin_values;
on_next_frame_begin_values.set_additional_present_credits(
num_present_credits);
fake_flatland().FireOnNextFrameBeginEvent(
std::move(on_next_frame_begin_values));
}
private:
async::TestLoop loop_;
std::unique_ptr<async::LoopInterface> session_subloop_;
@@ -185,4 +194,81 @@ TEST_F(FlatlandConnectionTest, BasicPresent) {
EXPECT_EQ(release_fence_handle, first_release_fence_handle);
}
TEST_F(FlatlandConnectionTest, OutOfOrderAwait) {
// Set up callbacks which allow sensing of how many presents were handled.
size_t presents_called = 0u;
zx_handle_t release_fence_handle;
fake_flatland().SetPresentHandler([&presents_called,
&release_fence_handle](auto present_args) {
presents_called++;
release_fence_handle = present_args.release_fences().empty()
? ZX_HANDLE_INVALID
: present_args.release_fences().front().get();
});
// Set up a callback which allows sensing of how many vsync's
// (`OnFramePresented` events) were handled.
size_t vsyncs_handled = 0u;
on_frame_presented_event on_frame_presented = [&vsyncs_handled](auto...) {
vsyncs_handled++;
};
// Create the FlatlandConnection but don't pump the loop. No FIDL calls are
// completed yet.
flutter_runner::FlatlandConnection flatland_connection(
GetCurrentTestName(), TakeFlatlandHandle(), []() { FAIL(); },
std::move(on_frame_presented), 1, fml::TimeDelta::Zero());
EXPECT_EQ(presents_called, 0u);
EXPECT_EQ(vsyncs_handled, 0u);
// Pump the loop. Nothing is called.
loop().RunUntilIdle();
EXPECT_EQ(presents_called, 0u);
EXPECT_EQ(vsyncs_handled, 0u);
// Simulate an AwaitVsync that comes after the first call.
bool await_vsync_callback_fired = false;
AwaitVsyncChecked(flatland_connection, await_vsync_callback_fired,
kDefaultFlatlandPresentationInterval);
EXPECT_TRUE(await_vsync_callback_fired);
// Set the callback with AwaitVsync, callback should not be fired
await_vsync_callback_fired = false;
AwaitVsyncChecked(flatland_connection, await_vsync_callback_fired,
kDefaultFlatlandPresentationInterval);
EXPECT_FALSE(await_vsync_callback_fired);
// Fire the `OnNextFrameBegin` event. AwaitVsync callback should be fired.
await_vsync_callback_fired = false;
OnNextFrameBegin(1);
loop().RunUntilIdle();
EXPECT_TRUE(await_vsync_callback_fired);
// Second consecutive ONFB should not call the fire callback and should
// instead set it to be pending to fire on next AwaitVsync
await_vsync_callback_fired = false;
OnNextFrameBegin(1);
loop().RunUntilIdle();
EXPECT_FALSE(await_vsync_callback_fired);
// Now an AwaitVsync should immediately fire the pending callback
await_vsync_callback_fired = false;
AwaitVsyncChecked(flatland_connection, await_vsync_callback_fired,
kDefaultFlatlandPresentationInterval);
EXPECT_TRUE(await_vsync_callback_fired);
// With the pending callback fired, The new callback should be set for the
// next OnNextFrameBegin to call
await_vsync_callback_fired = false;
AwaitVsyncChecked(flatland_connection, await_vsync_callback_fired,
kDefaultFlatlandPresentationInterval);
EXPECT_FALSE(await_vsync_callback_fired);
// Now OnNextFrameBegin should fire the callback
await_vsync_callback_fired = false;
OnNextFrameBegin(1);
loop().RunUntilIdle();
EXPECT_TRUE(await_vsync_callback_fired);
}
} // namespace flutter_runner::testing