[fuchsia] set vsync_offset based on config file (flutter/engine#18006)

In order to better support different products on Fuchsia, we should
change performance-sensitive attributes based on config files passed in.
This change does so for `vsync_offset`.
This commit is contained in:
Felipe Archondo
2020-05-01 11:45:53 -04:00
committed by GitHub
parent 6027990460
commit fbdf852d1d
16 changed files with 192 additions and 26 deletions

View File

@@ -1083,6 +1083,8 @@ FILE: ../../../flutter/shell/platform/fuchsia/flutter/compositor_context.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/engine.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/engine.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_fakes.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_intl.cc
FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_intl.h
FILE: ../../../flutter/shell/platform/fuchsia/flutter/fuchsia_intl_unittest.cc

View File

@@ -272,6 +272,8 @@ executable("flutter_runner_unittests") {
"accessibility_bridge.h",
"accessibility_bridge_unittest.cc",
"flutter_runner_fakes.h",
"flutter_runner_product_configuration.cc",
"flutter_runner_product_configuration.h",
"fuchsia_intl.cc",
"fuchsia_intl.h",
"fuchsia_intl_unittest.cc",
@@ -290,6 +292,7 @@ executable("flutter_runner_unittests") {
"task_observers.h",
"task_runner_adapter.cc",
"task_runner_adapter.h",
"tests/flutter_runner_product_configuration_unittests.cc",
"tests/vsync_recorder_unittests.cc",
"thread.cc",
"thread.h",

View File

@@ -35,6 +35,7 @@
#include "runtime/dart/utils/tempfs.h"
#include "runtime/dart/utils/vmo.h"
#include "flutter_runner_product_configuration.h"
#include "task_observers.h"
#include "task_runner_adapter.h"
#include "thread.h"
@@ -314,6 +315,13 @@ Application::Application(
}
}
// Load and use product-specific configuration, if it exists.
std::string json_string;
if (dart_utils::ReadFileToString(
"/config/data/frame_scheduling_performance_values", &json_string)) {
product_config_ = FlutterRunnerProductConfiguration(json_string);
}
#if defined(DART_PRODUCT)
settings_.enable_observatory = false;
#else
@@ -609,7 +617,8 @@ void Application::CreateView(
std::move(isolate_snapshot_), // isolate snapshot
scenic::ToViewToken(std::move(view_token)), // view token
std::move(fdio_ns_), // FDIO namespace
std::move(directory_request_) // outgoing request
std::move(directory_request_), // outgoing request
product_config_ // product configuration
));
}

View File

@@ -25,6 +25,7 @@
#include "flutter/common/settings.h"
#include "flutter/fml/macros.h"
#include "flutter_runner_product_configuration.h"
#include "thread.h"
#include "unique_fdio_ns.h"
@@ -77,6 +78,7 @@ class Application final : public Engine::Delegate,
private:
flutter::Settings settings_;
FlutterRunnerProductConfiguration product_config_;
TerminationCallback termination_callback_;
const std::string debug_label_;
UniqueFDIONS fdio_ns_ = UniqueFDIONSCreate();

View File

@@ -16,6 +16,7 @@
#include "flutter/runtime/dart_vm_lifecycle.h"
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/run_configuration.h"
#include "flutter_runner_product_configuration.h"
#include "fuchsia_intl.h"
#include "platform_view.h"
#include "runtime/dart/utils/files.h"
@@ -57,7 +58,8 @@ Engine::Engine(Delegate& delegate,
fml::RefPtr<const flutter::DartSnapshot> isolate_snapshot,
fuchsia::ui::views::ViewToken view_token,
UniqueFDIONS fdio_ns,
fidl::InterfaceRequest<fuchsia::io::Directory> directory_request)
fidl::InterfaceRequest<fuchsia::io::Directory> directory_request,
FlutterRunnerProductConfiguration product_config)
: delegate_(delegate),
thread_label_(std::move(thread_label)),
settings_(std::move(settings)),
@@ -137,7 +139,8 @@ Engine::Engine(Delegate& delegate,
std::move(on_session_size_change_hint_callback),
on_enable_wireframe_callback =
std::move(on_enable_wireframe_callback),
vsync_handle = vsync_event_.get()](flutter::Shell& shell) mutable {
vsync_handle = vsync_event_.get(),
product_config = product_config](flutter::Shell& shell) mutable {
return std::make_unique<flutter_runner::PlatformView>(
shell, // delegate
debug_label, // debug label
@@ -150,8 +153,8 @@ Engine::Engine(Delegate& delegate,
std::move(on_session_metrics_change_callback),
std::move(on_session_size_change_hint_callback),
std::move(on_enable_wireframe_callback),
vsync_handle // vsync handle
);
vsync_handle, // vsync handle
product_config);
});
// Session can be terminated on the raster thread, but we must terminate
@@ -199,8 +202,7 @@ Engine::Engine(Delegate& delegate,
std::move(view_ref_pair), // scenic view ref/view ref control
std::move(session), // scenic session
on_session_error_callback, // session did encounter error
vsync_event // vsync event handle
);
vsync_event); // vsync event handle
}
return std::make_unique<flutter::Rasterizer>(

View File

@@ -15,6 +15,7 @@
#include "flutter/fml/macros.h"
#include "flutter/shell/common/shell.h"
#include "flutter_runner_product_configuration.h"
#include "isolate_configurator.h"
#include "thread.h"
@@ -37,7 +38,8 @@ class Engine final {
fml::RefPtr<const flutter::DartSnapshot> isolate_snapshot,
fuchsia::ui::views::ViewToken view_token,
UniqueFDIONS fdio_ns,
fidl::InterfaceRequest<fuchsia::io::Directory> directory_request);
fidl::InterfaceRequest<fuchsia::io::Directory> directory_request,
FlutterRunnerProductConfiguration product_config);
~Engine();
// Returns the Dart return code for the root isolate if one is present. This

View File

@@ -50,6 +50,8 @@ template("flutter_runner") {
"compositor_context.h",
"engine.cc",
"engine.h",
"flutter_runner_product_configuration.cc",
"flutter_runner_product_configuration.h",
"fuchsia_intl.cc",
"fuchsia_intl.h",
"isolate_configurator.cc",

View File

@@ -0,0 +1,25 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter_runner_product_configuration.h"
#include "rapidjson/document.h"
namespace flutter_runner {
FlutterRunnerProductConfiguration::FlutterRunnerProductConfiguration(
std::string json_string) {
rapidjson::Document document;
document.Parse(json_string);
if (!document.IsObject())
return;
// Parse out all values we're expecting.
if (auto& val = document["vsync_offset_in_us"]; val.IsInt()) {
vsync_offset_ = fml::TimeDelta::FromMicroseconds(val.GetInt());
}
}
} // namespace flutter_runner

View File

@@ -0,0 +1,23 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_RUNNER_PRODUCT_CONFIGURATION_H_
#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_RUNNER_PRODUCT_CONFIGURATION_H_
#include "flutter/fml/time/time_delta.h"
namespace flutter_runner {
class FlutterRunnerProductConfiguration {
public:
FlutterRunnerProductConfiguration() {}
FlutterRunnerProductConfiguration(std::string path);
fml::TimeDelta get_vsync_offset() { return vsync_offset_; }
private:
fml::TimeDelta vsync_offset_ = fml::TimeDelta::Zero();
};
} // namespace flutter_runner
#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_RUNNER_PRODUCT_CONFIGURATION_H_

View File

@@ -12,6 +12,7 @@
#include "flutter/lib/ui/compositing/scene_host.h"
#include "flutter/lib/ui/window/pointer_data.h"
#include "flutter/lib/ui/window/window.h"
#include "flutter_runner_product_configuration.h"
#include "logging.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
@@ -91,7 +92,8 @@ PlatformView::PlatformView(
OnMetricsUpdate session_metrics_did_change_callback,
OnSizeChangeHint session_size_change_hint_callback,
OnEnableWireframe wireframe_enabled_callback,
zx_handle_t vsync_event_handle)
zx_handle_t vsync_event_handle,
FlutterRunnerProductConfiguration product_config)
: flutter::PlatformView(delegate, std::move(task_runners)),
debug_label_(std::move(debug_label)),
view_ref_(std::move(view_ref)),
@@ -103,7 +105,8 @@ PlatformView::PlatformView(
wireframe_enabled_callback_(std::move(wireframe_enabled_callback)),
ime_client_(this),
surface_(std::make_unique<Surface>(debug_label_)),
vsync_event_handle_(vsync_event_handle) {
vsync_event_handle_(vsync_event_handle),
product_config_(product_config) {
// Register all error handlers.
SetInterfaceErrorHandler(session_listener_binding_, "SessionListener");
SetInterfaceErrorHandler(ime_, "Input Method Editor");
@@ -561,7 +564,8 @@ void PlatformView::DeactivateIme() {
// |flutter::PlatformView|
std::unique_ptr<flutter::VsyncWaiter> PlatformView::CreateVSyncWaiter() {
return std::make_unique<flutter_runner::VsyncWaiter>(
debug_label_, vsync_event_handle_, task_runners_);
debug_label_, vsync_event_handle_, task_runners_,
product_config_.get_vsync_offset());
}
// |flutter::PlatformView|

View File

@@ -20,6 +20,7 @@
#include "lib/fidl/cpp/binding.h"
#include "lib/ui/scenic/cpp/id.h"
#include "flutter_runner_product_configuration.h"
#include "surface.h"
namespace flutter_runner {
@@ -53,7 +54,8 @@ class PlatformView final : public flutter::PlatformView,
OnMetricsUpdate session_metrics_did_change_callback,
OnSizeChangeHint session_size_change_hint_callback,
OnEnableWireframe wireframe_enabled_callback,
zx_handle_t vsync_event_handle);
zx_handle_t vsync_event_handle,
FlutterRunnerProductConfiguration product_config);
PlatformView(flutter::PlatformView::Delegate& delegate,
std::string debug_label,
flutter::TaskRunners task_runners,
@@ -115,6 +117,8 @@ class PlatformView final : public flutter::PlatformView,
std::set<std::string /* channel */> unregistered_channels_;
zx_handle_t vsync_event_handle_ = 0;
FlutterRunnerProductConfiguration product_config_;
void RegisterPlatformMessageHandlers();
void FlushViewportMetrics();

View File

@@ -110,7 +110,8 @@ TEST_F(PlatformViewTests, ChangesAccessibilitySettings) {
nullptr, // session_metrics_did_change_callback
nullptr, // session_size_change_hint_callback
nullptr, // on_enable_wireframe_callback,
0u // vsync_event_handle
0u, // vsync_event_handle
{} // product_config
);
RunLoopUntilIdle();
@@ -162,7 +163,8 @@ TEST_F(PlatformViewTests, EnableWireframeTest) {
nullptr, // session_metrics_did_change_callback
nullptr, // session_size_change_hint_callback
EnableWireframeCallback, // on_enable_wireframe_callback,
0u // vsync_event_handle
0u, // vsync_event_handle
{} // product_config
);
// Cast platform_view to its base view so we can have access to the public

View File

@@ -0,0 +1,71 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <gtest/gtest.h>
#include "flutter/fml/time/time_delta.h"
#include "flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h"
using namespace flutter_runner;
namespace flutter_runner_test {
class FlutterRunnerProductConfigurationTest : public testing::Test {};
TEST_F(FlutterRunnerProductConfigurationTest, ValidVsyncOffset) {
const std::string json_string = "{ \"vsync_offset_in_us\" : 9000 } ";
const fml::TimeDelta expected_offset = fml::TimeDelta::FromMicroseconds(9000);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
TEST_F(FlutterRunnerProductConfigurationTest, InvalidJsonString) {
const std::string json_string = "{ \"invalid json string\" }}} ";
const fml::TimeDelta expected_offset = fml::TimeDelta::FromMicroseconds(0);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
TEST_F(FlutterRunnerProductConfigurationTest, EmptyJsonString) {
const std::string json_string = "";
const fml::TimeDelta expected_offset = fml::TimeDelta::FromMicroseconds(0);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
TEST_F(FlutterRunnerProductConfigurationTest, EmptyVsyncOffset) {
const std::string json_string = "{ \"vsync_offset_in_us\" : } ";
const fml::TimeDelta expected_offset = fml::TimeDelta::FromMicroseconds(0);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
TEST_F(FlutterRunnerProductConfigurationTest, NegativeVsyncOffset) {
const std::string json_string = "{ \"vsync_offset_in_us\" : -15410 } ";
const fml::TimeDelta expected_offset =
fml::TimeDelta::FromMicroseconds(-15410);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
TEST_F(FlutterRunnerProductConfigurationTest, NonIntegerVsyncOffset) {
const std::string json_string = "{ \"vsync_offset_in_us\" : 3.14159 } ";
const fml::TimeDelta expected_offset = fml::TimeDelta::FromMicroseconds(0);
FlutterRunnerProductConfiguration product_config =
FlutterRunnerProductConfiguration(json_string);
EXPECT_EQ(product_config.get_vsync_offset(), expected_offset);
}
} // namespace flutter_runner_test

View File

@@ -14,16 +14,19 @@
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/trace_event.h"
#include "flutter_runner_product_configuration.h"
#include "vsync_recorder.h"
namespace flutter_runner {
VsyncWaiter::VsyncWaiter(std::string debug_label,
zx_handle_t session_present_handle,
flutter::TaskRunners task_runners)
flutter::TaskRunners task_runners,
fml::TimeDelta vsync_offset)
: flutter::VsyncWaiter(task_runners),
debug_label_(std::move(debug_label)),
session_wait_(session_present_handle, SessionPresentSignal),
vsync_offset_(vsync_offset),
weak_factory_(this),
weak_factory_ui_(nullptr) {
auto wait_handler = [&](async_dispatcher_t* dispatcher, //
@@ -50,6 +53,14 @@ VsyncWaiter::VsyncWaiter(std::string debug_label,
std::make_unique<fml::WeakPtrFactory<VsyncWaiter>>(this);
}));
session_wait_.set_handler(wait_handler);
if (vsync_offset_ >= fml::TimeDelta::FromSeconds(1)) {
FML_LOG(WARNING) << "Given vsync_offset is extremely high: "
<< vsync_offset_.ToMilliseconds() << "ms";
} else {
FML_LOG(INFO) << "Set vsync_offset to " << vsync_offset_.ToMicroseconds()
<< "us";
}
}
VsyncWaiter::~VsyncWaiter() {
@@ -132,7 +143,7 @@ void VsyncWaiter::AwaitVSync() {
fml::TimePoint next_vsync = SnapToNextPhase(now, last_presentation_time,
vsync_info.presentation_interval);
auto next_vsync_start_time = next_vsync - vsync_offset;
auto next_vsync_start_time = next_vsync - vsync_offset_;
if (now >= next_vsync_start_time)
next_vsync_start_time =

View File

@@ -9,8 +9,10 @@
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/fml/time/time_delta.h"
#include "flutter/fml/time/time_point.h"
#include "flutter/shell/common/vsync_waiter.h"
#include "flutter_runner_product_configuration.h"
namespace flutter_runner {
@@ -18,24 +20,24 @@ class VsyncWaiter final : public flutter::VsyncWaiter {
public:
static constexpr zx_signals_t SessionPresentSignal = ZX_EVENT_SIGNALED;
VsyncWaiter(std::string debug_label,
zx_handle_t session_present_handle,
flutter::TaskRunners task_runners,
fml::TimeDelta vsync_offset);
~VsyncWaiter() override;
static fml::TimePoint SnapToNextPhase(
const fml::TimePoint now,
const fml::TimePoint last_frame_presentation_time,
const fml::TimeDelta presentation_interval);
VsyncWaiter(std::string debug_label,
zx_handle_t session_present_handle,
flutter::TaskRunners task_runners);
~VsyncWaiter() override;
private:
const std::string debug_label_;
async::Wait session_wait_;
fml::WeakPtrFactory<VsyncWaiter> weak_factory_;
fml::TimeDelta vsync_offset_ = fml::TimeDelta::FromMicroseconds(0);
static constexpr fml::TimeDelta vsync_offset =
fml::TimeDelta::FromNanoseconds(0);
fml::WeakPtrFactory<VsyncWaiter> weak_factory_;
// For accessing the VsyncWaiter via the UI thread, necessary for the callback
// for AwaitVSync()

View File

@@ -14,6 +14,7 @@
#include "flutter/fml/time/time_point.h"
#include "flutter/shell/common/thread_host.h"
#include "flutter/shell/common/vsync_waiter.h"
#include "flutter/shell/platform/fuchsia/flutter/flutter_runner_product_configuration.h"
#include "flutter/shell/platform/fuchsia/flutter/task_runner_adapter.h"
#include "flutter/shell/platform/fuchsia/flutter/thread.h"
#include "flutter/shell/platform/fuchsia/flutter/vsync_waiter.h"
@@ -29,7 +30,8 @@ class VsyncWaiterTest : public testing::Test {
std::unique_ptr<flutter::VsyncWaiter> CreateVsyncWaiter(
flutter::TaskRunners task_runners) {
return std::make_unique<flutter_runner::VsyncWaiter>(
"VsyncWaiterTest", vsync_event_.get(), task_runners);
"VsyncWaiterTest", vsync_event_.get(), task_runners,
fml::TimeDelta::Zero());
}
void SignalVsyncEvent() {