[win32] support high frame rates (flutter/engine#28927)
This commit is contained in:
@@ -4,13 +4,31 @@
|
||||
|
||||
#include "flutter/fml/platform/win/message_loop_win.h"
|
||||
|
||||
#include <VersionHelpers.h>
|
||||
#include <timeapi.h>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
|
||||
constexpr uint32_t kHighResolutionTimer = 1; // 1 ms
|
||||
constexpr uint32_t kLowResolutionTimer = 15; // 15 ms
|
||||
|
||||
namespace fml {
|
||||
|
||||
MessageLoopWin::MessageLoopWin()
|
||||
: timer_(CreateWaitableTimer(NULL, FALSE, NULL)) {
|
||||
FML_CHECK(timer_.is_valid());
|
||||
// Flutter uses timers to schedule frames. By default, Windows timers do
|
||||
// not have the precision to reliably schedule frame rates greater than
|
||||
// 60hz. We can increase the precision, but on versions of Windows before
|
||||
// 10, this would globally increase timer precision leading to increased
|
||||
// resource usage. This would be particularly problematic on a laptop or
|
||||
// mobile device.
|
||||
if (IsWindows10OrGreater()) {
|
||||
timer_resolution_ = kHighResolutionTimer;
|
||||
} else {
|
||||
timer_resolution_ = kLowResolutionTimer;
|
||||
}
|
||||
timeBeginPeriod(timer_resolution_);
|
||||
}
|
||||
|
||||
MessageLoopWin::~MessageLoopWin() = default;
|
||||
@@ -27,6 +45,7 @@ void MessageLoopWin::Run() {
|
||||
void MessageLoopWin::Terminate() {
|
||||
running_ = false;
|
||||
WakeUp(fml::TimePoint::Now());
|
||||
timeEndPeriod(timer_resolution_);
|
||||
}
|
||||
|
||||
void MessageLoopWin::WakeUp(fml::TimePoint time_point) {
|
||||
|
||||
@@ -25,6 +25,7 @@ class MessageLoopWin : public MessageLoopImpl {
|
||||
|
||||
bool running_;
|
||||
fml::UniqueObject<HANDLE, UniqueHandleTraits> timer_;
|
||||
uint32_t timer_resolution_;
|
||||
|
||||
MessageLoopWin();
|
||||
|
||||
|
||||
@@ -21,13 +21,28 @@
|
||||
#if defined(WINUWP)
|
||||
#include "flutter/shell/platform/windows/accessibility_bridge_delegate_winuwp.h"
|
||||
#else
|
||||
#include <dwmapi.h>
|
||||
#include "flutter/shell/platform/windows/accessibility_bridge_delegate_win32.h"
|
||||
#endif // defined(WINUWP)
|
||||
|
||||
// winbase.h defines GetCurrentTime as a macro.
|
||||
#undef GetCurrentTime
|
||||
|
||||
namespace flutter {
|
||||
|
||||
namespace {
|
||||
|
||||
// Lifted from vsync_waiter_fallback.cc
|
||||
static std::chrono::nanoseconds SnapToNextTick(
|
||||
std::chrono::nanoseconds value,
|
||||
std::chrono::nanoseconds tick_phase,
|
||||
std::chrono::nanoseconds tick_interval) {
|
||||
std::chrono::nanoseconds offset = (tick_phase - value) % tick_interval;
|
||||
if (offset != std::chrono::nanoseconds::zero())
|
||||
offset = offset + tick_interval;
|
||||
return value + offset;
|
||||
}
|
||||
|
||||
// Creates and returns a FlutterRendererConfig that renders to the view (if any)
|
||||
// of a FlutterWindowsEngine, using OpenGL (via ANGLE).
|
||||
// The user_data received by the render callbacks refers to the
|
||||
@@ -262,6 +277,10 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
|
||||
auto host = static_cast<FlutterWindowsEngine*>(user_data);
|
||||
return host->HandlePlatformMessage(engine_message);
|
||||
};
|
||||
args.vsync_callback = [](void* user_data, intptr_t baton) -> void {
|
||||
auto host = static_cast<FlutterWindowsEngine*>(user_data);
|
||||
host->OnVsync(baton);
|
||||
};
|
||||
args.on_pre_engine_restart_callback = [](void* user_data) {
|
||||
auto host = static_cast<FlutterWindowsEngine*>(user_data);
|
||||
host->view()->OnPreEngineRestart();
|
||||
@@ -307,6 +326,19 @@ bool FlutterWindowsEngine::RunWithEntrypoint(const char* entrypoint) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Configure device frame rate displayed via devtools.
|
||||
FlutterEngineDisplay display = {};
|
||||
display.struct_size = sizeof(FlutterEngineDisplay);
|
||||
display.display_id = 0;
|
||||
display.single_display = true;
|
||||
display.refresh_rate =
|
||||
1.0 / (static_cast<double>(FrameInterval().count()) / 1000000000.0);
|
||||
|
||||
std::vector<FlutterEngineDisplay> displays = {display};
|
||||
embedder_api_.NotifyDisplayUpdate(engine_,
|
||||
kFlutterEngineDisplaysUpdateTypeStartup,
|
||||
displays.data(), displays.size());
|
||||
|
||||
SendSystemSettings();
|
||||
|
||||
return true;
|
||||
@@ -328,6 +360,36 @@ void FlutterWindowsEngine::SetView(FlutterWindowsView* view) {
|
||||
view_ = view;
|
||||
}
|
||||
|
||||
void FlutterWindowsEngine::OnVsync(intptr_t baton) {
|
||||
std::chrono::nanoseconds current_time =
|
||||
std::chrono::nanoseconds(embedder_api_.GetCurrentTime());
|
||||
std::chrono::nanoseconds frame_interval = FrameInterval();
|
||||
auto next = SnapToNextTick(current_time, start_time_, frame_interval);
|
||||
embedder_api_.OnVsync(engine_, baton, next.count(),
|
||||
(next + frame_interval).count());
|
||||
}
|
||||
|
||||
std::chrono::nanoseconds FlutterWindowsEngine::FrameInterval() {
|
||||
if (frame_interval_override_.has_value()) {
|
||||
return frame_interval_override_.value();
|
||||
}
|
||||
uint64_t interval = 16600000;
|
||||
|
||||
#ifndef WINUWP
|
||||
DWM_TIMING_INFO timing_info = {};
|
||||
timing_info.cbSize = sizeof(timing_info);
|
||||
HRESULT result = DwmGetCompositionTimingInfo(NULL, &timing_info);
|
||||
if (result == S_OK && timing_info.rateRefresh.uiDenominator > 0 &&
|
||||
timing_info.rateRefresh.uiNumerator > 0) {
|
||||
interval = static_cast<double>(timing_info.rateRefresh.uiDenominator *
|
||||
1000000000.0) /
|
||||
static_cast<double>(timing_info.rateRefresh.uiNumerator);
|
||||
}
|
||||
#endif
|
||||
|
||||
return std::chrono::nanoseconds(interval);
|
||||
}
|
||||
|
||||
// Returns the currently configured Plugin Registrar.
|
||||
FlutterDesktopPluginRegistrarRef FlutterWindowsEngine::GetRegistrar() {
|
||||
return plugin_registrar_.get();
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_WINDOWS_FLUTTER_WINDOWS_ENGINE_H_
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
@@ -149,6 +150,9 @@ class FlutterWindowsEngine {
|
||||
// given |texture_id|.
|
||||
bool MarkExternalTextureFrameAvailable(int64_t texture_id);
|
||||
|
||||
// Invoke on the embedder's vsync callback to schedule a frame.
|
||||
void OnVsync(intptr_t baton);
|
||||
|
||||
// Dispatches a semantics action to the specified semantics node.
|
||||
bool DispatchSemanticsAction(uint64_t id,
|
||||
FlutterSemanticsAction action,
|
||||
@@ -158,7 +162,9 @@ class FlutterWindowsEngine {
|
||||
void UpdateSemanticsEnabled(bool enabled);
|
||||
|
||||
// Returns true if the semantics tree is enabled.
|
||||
bool semantics_enabled() const { return semantics_enabled_; }
|
||||
bool semantics_enabled() const {
|
||||
return semantics_enabled_;
|
||||
}
|
||||
|
||||
// Returns the native accessibility node with the given id.
|
||||
gfx::NativeViewAccessible GetNativeAccessibleFromId(AccessibilityNodeId id);
|
||||
@@ -220,6 +226,16 @@ class FlutterWindowsEngine {
|
||||
FlutterDesktopOnPluginRegistrarDestroyed
|
||||
plugin_registrar_destruction_callback_ = nullptr;
|
||||
|
||||
// The approximate time between vblank events.
|
||||
std::chrono::nanoseconds FrameInterval();
|
||||
|
||||
// The start time used to align frames.
|
||||
std::chrono::nanoseconds start_time_ = std::chrono::nanoseconds::zero();
|
||||
|
||||
// An override of the frame interval used by EngineModifier for testing.
|
||||
std::optional<std::chrono::nanoseconds> frame_interval_override_ =
|
||||
std::nullopt;
|
||||
|
||||
bool semantics_enabled_ = false;
|
||||
|
||||
std::shared_ptr<AccessibilityBridge> accessibility_bridge_;
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
#include "flutter/shell/platform/windows/testing/engine_modifier.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
// winbase.h defines GetCurrentTime as a macro.
|
||||
#undef GetCurrentTime
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
@@ -63,6 +66,7 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
|
||||
EXPECT_NE(args->platform_message_callback, nullptr);
|
||||
EXPECT_NE(args->custom_task_runners, nullptr);
|
||||
EXPECT_EQ(args->custom_dart_entrypoint, nullptr);
|
||||
EXPECT_NE(args->vsync_callback, nullptr);
|
||||
|
||||
return kSuccess;
|
||||
}));
|
||||
@@ -93,6 +97,29 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
|
||||
return kSuccess;
|
||||
}));
|
||||
|
||||
// And it should send display info.
|
||||
bool notify_display_update_called = false;
|
||||
modifier.SetFrameInterval(16600000); // 60 fps.
|
||||
modifier.embedder_api().NotifyDisplayUpdate = MOCK_ENGINE_PROC(
|
||||
NotifyDisplayUpdate,
|
||||
([¬ify_display_update_called, engine_instance = engine.get()](
|
||||
FLUTTER_API_SYMBOL(FlutterEngine) raw_engine,
|
||||
const FlutterEngineDisplaysUpdateType update_type,
|
||||
const FlutterEngineDisplay* embedder_displays,
|
||||
size_t display_count) {
|
||||
EXPECT_EQ(update_type, kFlutterEngineDisplaysUpdateTypeStartup);
|
||||
EXPECT_EQ(display_count, 1);
|
||||
|
||||
FlutterEngineDisplay display = embedder_displays[0];
|
||||
|
||||
EXPECT_EQ(display.display_id, 0);
|
||||
EXPECT_EQ(display.single_display, true);
|
||||
EXPECT_EQ(std::floor(display.refresh_rate), 60.0);
|
||||
|
||||
notify_display_update_called = true;
|
||||
return kSuccess;
|
||||
}));
|
||||
|
||||
// Set the AngleSurfaceManager to !nullptr to test ANGLE rendering.
|
||||
modifier.SetSurfaceManager(reinterpret_cast<AngleSurfaceManager*>(1));
|
||||
|
||||
@@ -101,6 +128,7 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
|
||||
EXPECT_TRUE(run_called);
|
||||
EXPECT_TRUE(update_locales_called);
|
||||
EXPECT_TRUE(settings_message_sent);
|
||||
EXPECT_TRUE(notify_display_update_called);
|
||||
|
||||
// Ensure that deallocation doesn't call the actual Shutdown with the bogus
|
||||
// engine pointer that the overridden Run returned.
|
||||
@@ -108,10 +136,44 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) {
|
||||
modifier.ReleaseSurfaceManager();
|
||||
}
|
||||
|
||||
TEST(FlutterWindowsEngine, ConfiguresFrameVsync) {
|
||||
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
|
||||
EngineModifier modifier(engine.get());
|
||||
bool on_vsync_called = false;
|
||||
|
||||
modifier.embedder_api().GetCurrentTime =
|
||||
MOCK_ENGINE_PROC(GetCurrentTime, ([]() -> uint64_t { return 1; }));
|
||||
modifier.embedder_api().OnVsync = MOCK_ENGINE_PROC(
|
||||
OnVsync,
|
||||
([&on_vsync_called, engine_instance = engine.get()](
|
||||
FLUTTER_API_SYMBOL(FlutterEngine) engine, intptr_t baton,
|
||||
uint64_t frame_start_time_nanos, uint64_t frame_target_time_nanos) {
|
||||
EXPECT_EQ(baton, 1);
|
||||
EXPECT_EQ(frame_start_time_nanos, 16600000);
|
||||
EXPECT_EQ(frame_target_time_nanos, 33200000);
|
||||
on_vsync_called = true;
|
||||
return kSuccess;
|
||||
}));
|
||||
modifier.SetStartTime(0);
|
||||
modifier.SetFrameInterval(16600000);
|
||||
|
||||
engine->OnVsync(1);
|
||||
|
||||
EXPECT_TRUE(on_vsync_called);
|
||||
}
|
||||
|
||||
TEST(FlutterWindowsEngine, RunWithoutANGLEUsesSoftware) {
|
||||
std::unique_ptr<FlutterWindowsEngine> engine = GetTestEngine();
|
||||
EngineModifier modifier(engine.get());
|
||||
|
||||
modifier.embedder_api().NotifyDisplayUpdate =
|
||||
MOCK_ENGINE_PROC(NotifyDisplayUpdate,
|
||||
([engine_instance = engine.get()](
|
||||
FLUTTER_API_SYMBOL(FlutterEngine) raw_engine,
|
||||
const FlutterEngineDisplaysUpdateType update_type,
|
||||
const FlutterEngineDisplay* embedder_displays,
|
||||
size_t display_count) { return kSuccess; }));
|
||||
|
||||
// The engine should be run with expected configuration values.
|
||||
bool run_called = false;
|
||||
modifier.embedder_api().Run = MOCK_ENGINE_PROC(
|
||||
|
||||
@@ -78,6 +78,13 @@ std::unique_ptr<FlutterWindowsEngine> GetTestEngine() {
|
||||
key_event_logs.push_back(kKeyEventFromEmbedder);
|
||||
callback(test_response);
|
||||
});
|
||||
modifier.embedder_api().NotifyDisplayUpdate =
|
||||
MOCK_ENGINE_PROC(NotifyDisplayUpdate,
|
||||
([engine_instance = engine.get()](
|
||||
FLUTTER_API_SYMBOL(FlutterEngine) raw_engine,
|
||||
const FlutterEngineDisplaysUpdateType update_type,
|
||||
const FlutterEngineDisplay* embedder_displays,
|
||||
size_t display_count) { return kSuccess; }));
|
||||
|
||||
MockEmbedderApiForKeyboard(modifier, key_response_controller);
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "flutter/shell/platform/windows/flutter_windows_engine.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace flutter {
|
||||
|
||||
// A test utility class providing the ability to access and alter various
|
||||
@@ -35,6 +37,20 @@ class EngineModifier {
|
||||
engine_->surface_manager_.reset(surface_manager);
|
||||
}
|
||||
|
||||
/// Reset the start_time field that is used to align vsync events.
|
||||
void SetStartTime(uint64_t start_time_nanos) {
|
||||
engine_->start_time_ = std::chrono::nanoseconds(start_time_nanos);
|
||||
}
|
||||
|
||||
/// Override the frame interval to the provided nanosecond interval.
|
||||
///
|
||||
/// This will prevent the windows engine from delegating to dwm to
|
||||
/// discover the true frame interval, which can vary across machines.
|
||||
void SetFrameInterval(uint64_t frame_interval_nanos) {
|
||||
engine_->frame_interval_override_ =
|
||||
std::optional<std::chrono::nanoseconds>(frame_interval_nanos);
|
||||
}
|
||||
|
||||
// Explicitly releases the SurfaceManager being used by the
|
||||
// FlutterWindowsEngine instance. This should be used if SetSurfaceManager is
|
||||
// used to explicitly set to a non-null value (but not a valid object) to test
|
||||
|
||||
@@ -189,6 +189,10 @@ void MockEmbedderApiForKeyboard(
|
||||
return kSuccess;
|
||||
};
|
||||
modifier.embedder_api().Shutdown = [](auto engine) { return kSuccess; };
|
||||
modifier.embedder_api().NotifyDisplayUpdate =
|
||||
[](auto engine, const FlutterEngineDisplaysUpdateType update_type,
|
||||
const FlutterEngineDisplay* embedder_displays,
|
||||
size_t display_count) { return kSuccess; };
|
||||
}
|
||||
|
||||
void MockMessageQueue::PushBack(const Win32Message* message) {
|
||||
|
||||
Reference in New Issue
Block a user