Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions shell/common/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,14 @@ void Shell::OnFrameRasterized(const FrameTiming& timing) {
},
fml::TimeDelta::FromMilliseconds(kBatchTimeInMilliseconds));
}

{
std::scoped_lock<std::mutex> lock(on_frame_rasterized_callbacks_mutex_);
for (auto callback : on_frame_rasterized_callbacks_) {
callback();
}
on_frame_rasterized_callbacks_.clear();
}
}

fml::Milliseconds Shell::GetFrameBudget() {
Expand Down Expand Up @@ -1749,6 +1757,11 @@ void Shell::RegisterImageDecoder(ImageGeneratorFactory factory,
});
}

void Shell::AddOnFrameRasterizedCallback(const fml::closure& callback) {
std::scoped_lock<std::mutex> lock(on_frame_rasterized_callbacks_mutex_);
on_frame_rasterized_callbacks_.push_back(callback);
}

bool Shell::OnServiceProtocolGetSkSLs(
const ServiceProtocol::Handler::ServiceProtocolMap& params,
rapidjson::Document* response) {
Expand Down
5 changes: 5 additions & 0 deletions shell/common/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ class Shell final : public PlatformView::Delegate,
/// @see `CreateCompatibleGenerator`
void RegisterImageDecoder(ImageGeneratorFactory factory, int32_t priority);

void AddOnFrameRasterizedCallback(const fml::closure& callback);

// |Engine::Delegate|
const std::shared_ptr<PlatformMessageHandler>& GetPlatformMessageHandler()
const override;
Expand Down Expand Up @@ -474,6 +476,9 @@ class Shell final : public PlatformView::Delegate,
// Used to communicate the right frame bounds via service protocol.
double device_pixel_ratio_ = 0.0;

std::mutex on_frame_rasterized_callbacks_mutex_;
std::vector<fml::closure> on_frame_rasterized_callbacks_;

// How many frames have been timed since last report.
size_t UnreportedFramesCount() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,15 +1609,16 @@ - (void)setupKeyboardAnimationVsyncClient {
flutterViewController.get()->_viewportMetrics.physical_view_inset_bottom =
flutterViewController.get()
.keyboardAnimationView.layer.presentationLayer.frame.origin.y;
[flutterViewController updateViewportMetrics];
[flutterViewController keyboardAnimationUpdateViewportMetricsInFrameRasterizedCallback];
}
} else {
fml::TimeDelta timeElapsed = recorder.get()->GetVsyncTargetTime() -
fml::TimeDelta frameInterval = recorder->GetVsyncTargetTime() - recorder->GetVsyncStartTime();
fml::TimeDelta timeElapsed = recorder.get()->GetVsyncTargetTime() + frameInterval -
flutterViewController.get().keyboardAnimationStartTime;

flutterViewController.get()->_viewportMetrics.physical_view_inset_bottom =
[[flutterViewController keyboardSpringAnimation] curveFunction:timeElapsed.ToSecondsF()];
[flutterViewController updateViewportMetrics];
[flutterViewController keyboardAnimationUpdateViewportMetricsInFrameRasterizedCallback];
}
};
flutter::Shell& shell = [_engine.get() shell];
Expand All @@ -1630,6 +1631,26 @@ - (void)setupKeyboardAnimationVsyncClient {
[_keyboardAnimationVSyncClient await];
}

- (void)keyboardAnimationUpdateViewportMetricsInFrameRasterizedCallback {
fml::closure platformTask = [weakSelf = [self getWeakPtr]] {
if (!weakSelf) {
return;
}
fml::scoped_nsobject<FlutterViewController> flutterViewController(
[(FlutterViewController*)weakSelf.get() retain]);
if (!flutterViewController) {
return;
}
[flutterViewController updateViewportMetrics];
};

flutter::Shell& shell = [_engine.get() shell];
fml::closure onFrameRasterizedCallback = [&shell, platformTask] {
shell.GetTaskRunners().GetPlatformTaskRunner()->PostTask(platformTask);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this also needs to be synchronized. The rasterizer needs to wait until the platform task to finish before starting next frame.

};
shell.AddOnFrameRasterizedCallback(onFrameRasterizedCallback);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think add the callback here might be a frame late?
What I was thinking was the callback will be some sort of delegate method and runs every frame. The delegate then decide if the method body is a no-op (when there is no keyboard animation), or synchronously runs the keyboard animation on platform thread.

}

- (void)invalidateKeyboardAnimationVSyncClient {
[_keyboardAnimationVSyncClient invalidate];
[_keyboardAnimationVSyncClient release];
Expand Down