Skip to content

Commit b972f75

Browse files
authored
Allow embedders to add per shell idle notification callbacks. (flutter#7427)
1 parent 844d27c commit b972f75

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

common/settings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ struct Settings {
7474
// The isolate is not current and may have already been destroyed when this
7575
// call is made.
7676
fml::closure root_isolate_shutdown_callback;
77+
// The callback made on the UI thread in an isolate scope when the engine
78+
// detects that the framework is idle. The VM also uses this time to perform
79+
// tasks suitable when idling. Due to this, embedders are still advised to be
80+
// as fast as possible in returning from this callback. Long running
81+
// operations in this callback do have the capability of introducing jank.
82+
fml::closure idle_notification_callback;
7783
bool enable_software_rendering = false;
7884
bool skia_deterministic_rendering_on_cpu = false;
7985
bool verbose_logging = false;

runtime/runtime_controller.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ RuntimeController::RuntimeController(
2424
fml::WeakPtr<GrContext> p_resource_context,
2525
fml::RefPtr<flow::SkiaUnrefQueue> p_unref_queue,
2626
std::string p_advisory_script_uri,
27-
std::string p_advisory_script_entrypoint)
27+
std::string p_advisory_script_entrypoint,
28+
fml::closure p_idle_notification_callback)
2829
: RuntimeController(p_client,
2930
p_vm,
3031
std::move(p_isolate_snapshot),
@@ -35,6 +36,7 @@ RuntimeController::RuntimeController(
3536
std::move(p_unref_queue),
3637
std::move(p_advisory_script_uri),
3738
std::move(p_advisory_script_entrypoint),
39+
p_idle_notification_callback,
3840
WindowData{/* default window data */}) {}
3941

4042
RuntimeController::RuntimeController(
@@ -48,6 +50,7 @@ RuntimeController::RuntimeController(
4850
fml::RefPtr<flow::SkiaUnrefQueue> p_unref_queue,
4951
std::string p_advisory_script_uri,
5052
std::string p_advisory_script_entrypoint,
53+
fml::closure idle_notification_callback,
5154
WindowData p_window_data)
5255
: client_(p_client),
5356
vm_(p_vm),
@@ -59,6 +62,7 @@ RuntimeController::RuntimeController(
5962
unref_queue_(p_unref_queue),
6063
advisory_script_uri_(p_advisory_script_uri),
6164
advisory_script_entrypoint_(p_advisory_script_entrypoint),
65+
idle_notification_callback_(idle_notification_callback),
6266
window_data_(std::move(p_window_data)),
6367
root_isolate_(
6468
DartIsolate::CreateRootIsolate(vm_,
@@ -120,6 +124,7 @@ std::unique_ptr<RuntimeController> RuntimeController::Clone() const {
120124
unref_queue_, //
121125
advisory_script_uri_, //
122126
advisory_script_entrypoint_, //
127+
idle_notification_callback_, //
123128
window_data_ //
124129
));
125130
}
@@ -201,7 +206,14 @@ bool RuntimeController::NotifyIdle(int64_t deadline) {
201206
}
202207

203208
tonic::DartState::Scope scope(root_isolate);
209+
204210
Dart_NotifyIdle(deadline);
211+
212+
// Idle notifications being in isolate scope are part of the contract.
213+
if (idle_notification_callback_) {
214+
TRACE_EVENT0("flutter", "EmbedderIdleNotification");
215+
idle_notification_callback_();
216+
}
205217
return true;
206218
}
207219

runtime/runtime_controller.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class RuntimeController final : public WindowClient {
3636
fml::WeakPtr<GrContext> resource_context,
3737
fml::RefPtr<flow::SkiaUnrefQueue> unref_queue,
3838
std::string advisory_script_uri,
39-
std::string advisory_script_entrypoint);
39+
std::string advisory_script_entrypoint,
40+
fml::closure idle_notification_callback);
4041

4142
~RuntimeController() override;
4243

@@ -125,6 +126,7 @@ class RuntimeController final : public WindowClient {
125126
fml::RefPtr<flow::SkiaUnrefQueue> unref_queue_;
126127
std::string advisory_script_uri_;
127128
std::string advisory_script_entrypoint_;
129+
fml::closure idle_notification_callback_;
128130
WindowData window_data_;
129131
std::weak_ptr<DartIsolate> root_isolate_;
130132
std::pair<bool, uint32_t> root_isolate_return_code_ = {false, 0};
@@ -139,6 +141,7 @@ class RuntimeController final : public WindowClient {
139141
fml::RefPtr<flow::SkiaUnrefQueue> unref_queue,
140142
std::string advisory_script_uri,
141143
std::string advisory_script_entrypoint,
144+
fml::closure idle_notification_callback,
142145
WindowData data);
143146

144147
Window* GetWindowIfAvailable();

shell/common/engine.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ Engine::Engine(Delegate& delegate,
5454
// object as its delegate. The delegate may be called in the constructor and
5555
// we want to be fully initilazed by that point.
5656
runtime_controller_ = std::make_unique<blink::RuntimeController>(
57-
*this, // runtime delegate
58-
&vm, // VM
59-
std::move(isolate_snapshot), // isolate snapshot
60-
std::move(shared_snapshot), // shared snapshot
61-
std::move(task_runners), // task runners
62-
std::move(snapshot_delegate), // snapshot delegate
63-
std::move(resource_context), // resource context
64-
std::move(unref_queue), // skia unref queue
65-
settings_.advisory_script_uri, // advisory script uri
66-
settings_.advisory_script_entrypoint // advisory script entrypoint
57+
*this, // runtime delegate
58+
&vm, // VM
59+
std::move(isolate_snapshot), // isolate snapshot
60+
std::move(shared_snapshot), // shared snapshot
61+
std::move(task_runners), // task runners
62+
std::move(snapshot_delegate), // snapshot delegate
63+
std::move(resource_context), // resource context
64+
std::move(unref_queue), // skia unref queue
65+
settings_.advisory_script_uri, // advisory script uri
66+
settings_.advisory_script_entrypoint, // advisory script entrypoint
67+
settings_.idle_notification_callback // idle notification callback
6768
);
6869
}
6970

0 commit comments

Comments
 (0)