Add an embedder.h API for scheduling frame (flutter/engine#31304)

This commit is contained in:
ColdPaleLight
2022-02-18 05:24:10 +08:00
committed by GitHub
parent 8b133f69ea
commit 043d9d2e39
17 changed files with 157 additions and 0 deletions

View File

@@ -262,3 +262,11 @@ void canReceiveArgumentsWhenEngineRun(List<String> args) {
void canReceiveArgumentsWhenEngineSpawn(List<String> args) {
notifyNativeWhenEngineSpawn(args.length == 2 && args[0] == 'arg1' && args[1] == 'arg2');
}
@pragma('vm:entry-point')
void canScheduleFrameFromPlatform() {
PlatformDispatcher.instance.onBeginFrame = (Duration beginTime) {
nativeOnBeginFrame(beginTime.inMicroseconds);
};
notifyNative();
}

View File

@@ -83,6 +83,10 @@ void PlatformView::NotifyDestroyed() {
delegate_.OnPlatformViewDestroyed();
}
void PlatformView::ScheduleFrame() {
delegate_.OnPlatformViewScheduleFrame();
}
sk_sp<GrDirectContext> PlatformView::CreateResourceContext() const {
FML_DLOG(WARNING) << "This platform does not set up the resource "
"context on the IO thread for async texture uploads.";

View File

@@ -72,6 +72,12 @@ class PlatformView {
///
virtual void OnPlatformViewDestroyed() = 0;
//--------------------------------------------------------------------------
/// @brief Notifies the delegate that the platform needs to schedule a
/// frame to regenerate the layer tree and redraw the surface.
///
virtual void OnPlatformViewScheduleFrame() = 0;
//--------------------------------------------------------------------------
/// @brief Notifies the delegate that the specified callback needs to
/// be invoked after the rasterizer is done rendering the next
@@ -477,6 +483,12 @@ class PlatformView {
///
virtual void NotifyDestroyed();
//----------------------------------------------------------------------------
/// @brief Used by embedders to schedule a frame. In response to this
/// call, the framework may need to start generating a new frame.
///
void ScheduleFrame();
//----------------------------------------------------------------------------
/// @brief Used by the shell to obtain a Skia GPU context that is capable
/// of operating on the IO thread. The context must be in the same

View File

@@ -879,6 +879,19 @@ void Shell::OnPlatformViewDestroyed() {
rasterizer_->TeardownExternalViewEmbedder();
}
// |PlatformView::Delegate|
void Shell::OnPlatformViewScheduleFrame() {
TRACE_EVENT0("flutter", "Shell::OnPlatformViewScheduleFrame");
FML_DCHECK(is_setup_);
FML_DCHECK(task_runners_.GetPlatformTaskRunner()->RunsTasksOnCurrentThread());
task_runners_.GetUITaskRunner()->PostTask([engine = engine_->GetWeakPtr()]() {
if (engine) {
engine->ScheduleFrame();
}
});
}
// |PlatformView::Delegate|
void Shell::OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) {
FML_DCHECK(is_setup_);

View File

@@ -518,6 +518,9 @@ class Shell final : public PlatformView::Delegate,
// |PlatformView::Delegate|
void OnPlatformViewDestroyed() override;
// |PlatformView::Delegate|
void OnPlatformViewScheduleFrame() override;
// |PlatformView::Delegate|
void OnPlatformViewSetViewportMetrics(
const ViewportMetrics& metrics) override;

View File

@@ -55,6 +55,8 @@ class MockPlatformViewDelegate : public PlatformView::Delegate {
MOCK_METHOD0(OnPlatformViewDestroyed, void());
MOCK_METHOD0(OnPlatformViewScheduleFrame, void());
MOCK_METHOD1(OnPlatformViewSetNextFrameCallback,
void(const fml::closure& closure));
@@ -1828,6 +1830,36 @@ TEST_F(ShellTest, IsolateCanAccessPersistentIsolateData) {
DestroyShell(std::move(shell), std::move(task_runners));
}
TEST_F(ShellTest, CanScheduleFrameFromPlatform) {
Settings settings = CreateSettingsForFixture();
TaskRunners task_runners = GetTaskRunnersForFixture();
fml::AutoResetWaitableEvent latch;
AddNativeCallback(
"NotifyNative",
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) { latch.Signal(); }));
fml::AutoResetWaitableEvent check_latch;
AddNativeCallback("NativeOnBeginFrame",
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
check_latch.Signal();
}));
std::unique_ptr<Shell> shell =
CreateShell(std::move(settings), std::move(task_runners));
ASSERT_TRUE(shell->IsSetup());
auto configuration = RunConfiguration::InferFromSettings(settings);
configuration.SetEntrypoint("canScheduleFrameFromPlatform");
RunEngine(shell.get(), std::move(configuration));
// Wait for the application to attach the listener.
latch.Wait();
fml::TaskRunner::RunNowOrPostTask(
shell->GetTaskRunners().GetPlatformTaskRunner(),
[&shell]() { shell->GetPlatformView()->ScheduleFrame(); });
check_latch.Wait();
DestroyShell(std::move(shell), std::move(task_runners));
}
static void LogSkData(sk_sp<SkData> data, const char* title) {
FML_LOG(ERROR) << "---------- " << title;
std::ostringstream ostr;

View File

@@ -21,6 +21,7 @@ namespace {
class FakeDelegate : public PlatformView::Delegate {
void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
void OnPlatformViewDestroyed() override {}
void OnPlatformViewScheduleFrame() override {}
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {}
void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}

View File

@@ -90,6 +90,7 @@ namespace {
class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::Delegate {
void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
void OnPlatformViewDestroyed() override {}
void OnPlatformViewScheduleFrame() override {}
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {}
void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}

View File

@@ -74,6 +74,7 @@ namespace {
class MockDelegate : public PlatformView::Delegate {
void OnPlatformViewCreated(std::unique_ptr<Surface> surface) override {}
void OnPlatformViewDestroyed() override {}
void OnPlatformViewScheduleFrame() override {}
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) override {}
void OnPlatformViewSetViewportMetrics(const ViewportMetrics& metrics) override {}
void OnPlatformViewDispatchPlatformMessage(std::unique_ptr<PlatformMessage> message) override {}

View File

@@ -2550,6 +2550,18 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate(
}
}
FlutterEngineResult FlutterEngineScheduleFrame(FLUTTER_API_SYMBOL(FlutterEngine)
engine) {
if (engine == nullptr) {
return LOG_EMBEDDER_ERROR(kInvalidArguments, "Invalid engine handle.");
}
return reinterpret_cast<flutter::EmbedderEngine*>(engine)->ScheduleFrame()
? kSuccess
: LOG_EMBEDDER_ERROR(kInvalidArguments,
"Could not schedule frame.");
}
FlutterEngineResult FlutterEngineGetProcAddresses(
FlutterEngineProcTable* table) {
if (!table) {
@@ -2600,6 +2612,7 @@ FlutterEngineResult FlutterEngineGetProcAddresses(
SET_PROC(PostCallbackOnAllNativeThreads,
FlutterEnginePostCallbackOnAllNativeThreads);
SET_PROC(NotifyDisplayUpdate, FlutterEngineNotifyDisplayUpdate);
SET_PROC(ScheduleFrame, FlutterEngineScheduleFrame);
#undef SET_PROC
return kSuccess;

View File

@@ -2389,6 +2389,17 @@ FlutterEngineResult FlutterEngineNotifyDisplayUpdate(
const FlutterEngineDisplay* displays,
size_t display_count);
//------------------------------------------------------------------------------
/// @brief Schedule a new frame to redraw the content.
///
/// @param[in] engine A running engine instance.
///
/// @return the result of the call made to the engine.
///
FLUTTER_EXPORT
FlutterEngineResult FlutterEngineScheduleFrame(FLUTTER_API_SYMBOL(FlutterEngine)
engine);
#endif // !FLUTTER_ENGINE_NO_PROTOTYPES
// Typedefs for the function pointers in FlutterEngineProcTable.
@@ -2505,6 +2516,8 @@ typedef FlutterEngineResult (*FlutterEngineNotifyDisplayUpdateFnPtr)(
FlutterEngineDisplaysUpdateType update_type,
const FlutterEngineDisplay* displays,
size_t display_count);
typedef FlutterEngineResult (*FlutterEngineScheduleFrameFnPtr)(
FLUTTER_API_SYMBOL(FlutterEngine) engine);
/// Function-pointer-based versions of the APIs above.
typedef struct {
@@ -2549,6 +2562,7 @@ typedef struct {
FlutterEnginePostCallbackOnAllNativeThreadsFnPtr
PostCallbackOnAllNativeThreads;
FlutterEngineNotifyDisplayUpdateFnPtr NotifyDisplayUpdate;
FlutterEngineScheduleFrameFnPtr ScheduleFrame;
} FlutterEngineProcTable;
//------------------------------------------------------------------------------

View File

@@ -273,6 +273,19 @@ bool EmbedderEngine::PostTaskOnEngineManagedNativeThreads(
return true;
}
bool EmbedderEngine::ScheduleFrame() {
if (!IsValid()) {
return false;
}
auto platform_view = shell_->GetPlatformView();
if (!platform_view) {
return false;
}
platform_view->ScheduleFrame();
return true;
}
Shell& EmbedderEngine::GetShell() {
FML_DCHECK(shell_);
return *shell_.get();

View File

@@ -81,6 +81,8 @@ class EmbedderEngine {
bool PostTaskOnEngineManagedNativeThreads(
std::function<void(FlutterNativeThreadType)> closure) const;
bool ScheduleFrame();
Shell& GetShell();
private:

View File

@@ -991,3 +991,11 @@ void invalid_backingstore() {
};
PlatformDispatcher.instance.scheduleFrame();
}
@pragma('vm:entry-point')
void can_schedule_frame() {
PlatformDispatcher.instance.onBeginFrame = (Duration beginTime){
signalNativeCount(beginTime.inMicroseconds);
};
signalNativeTest();
}

View File

@@ -1744,6 +1744,34 @@ TEST_F(EmbedderTest, VsyncCallbackPostedIntoFuture) {
shutdown_latch.Wait();
}
TEST_F(EmbedderTest, CanScheduleFrame) {
auto& context = GetEmbedderContext(EmbedderTestContextType::kSoftwareContext);
EmbedderConfigBuilder builder(context);
builder.SetSoftwareRendererConfig();
builder.SetDartEntrypoint("can_schedule_frame");
fml::AutoResetWaitableEvent latch;
context.AddNativeCallback(
"SignalNativeTest",
CREATE_NATIVE_ENTRY(
[&latch](Dart_NativeArguments args) { latch.Signal(); }));
fml::AutoResetWaitableEvent check_latch;
context.AddNativeCallback(
"SignalNativeCount",
CREATE_NATIVE_ENTRY(
[&check_latch](Dart_NativeArguments args) { check_latch.Signal(); }));
auto engine = builder.LaunchEngine();
ASSERT_TRUE(engine.is_valid());
// Wait for the application to attach the listener.
latch.Wait();
ASSERT_EQ(FlutterEngineScheduleFrame(engine.get()), kSuccess);
check_latch.Wait();
}
} // namespace testing
} // namespace flutter

View File

@@ -85,6 +85,8 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate {
// |flutter::PlatformView::Delegate|
void OnPlatformViewDestroyed() {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewScheduleFrame() {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewSetViewportMetrics(

View File

@@ -84,6 +84,8 @@ class MockPlatformViewDelegate : public flutter::PlatformView::Delegate {
// |flutter::PlatformView::Delegate|
void OnPlatformViewDestroyed() {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewScheduleFrame() {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewSetNextFrameCallback(const fml::closure& closure) {}
// |flutter::PlatformView::Delegate|
void OnPlatformViewSetViewportMetrics(