Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8d2fadc

Browse files
dkwingsmtharryterkelsen
authored andcommitted
Reland 2 (part 1): Enforce the rule of calling FlutterView.Render (#47062)
This PR relands part of #45300, which was reverted in #46919 due to performance regression. Due to how little and trivial production code the original PR touches, I really couldn't figure out the exact line that caused it except through experimentation, which requires changes to be officially landed on the main branch. After this PR lands, I'll immediately fire a performance test. This PR contains the `Shell` refactor of the original PR. I made a slight change where the isolate snapshot is no longer returned through return value, but the parameter, in order to avoid the overhead of assigning. It is intentional to not contain any unit tests or other changes of the original PR. They will be landed shortly after this PR. Part of flutter/flutter#136826. ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
1 parent 97d7c7a commit 8d2fadc

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

shell/common/shell.cc

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,36 @@ void PerformInitializationTasks(Settings& settings) {
144144

145145
} // namespace
146146

147-
std::unique_ptr<Shell> Shell::Create(
148-
const PlatformData& platform_data,
149-
const TaskRunners& task_runners,
150-
Settings settings,
151-
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
152-
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
153-
bool is_gpu_disabled) {
154-
// This must come first as it initializes tracing.
155-
PerformInitializationTasks(settings);
156-
157-
TRACE_EVENT0("flutter", "Shell::Create");
158-
147+
std::pair<DartVMRef, fml::RefPtr<const DartSnapshot>>
148+
Shell::InferVmInitDataFromSettings(Settings& settings) {
159149
// Always use the `vm_snapshot` and `isolate_snapshot` provided by the
160150
// settings to launch the VM. If the VM is already running, the snapshot
161151
// arguments are ignored.
162152
auto vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
163153
auto isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
164154
auto vm = DartVMRef::Create(settings, vm_snapshot, isolate_snapshot);
165-
FML_CHECK(vm) << "Must be able to initialize the VM.";
166155

167156
// If the settings did not specify an `isolate_snapshot`, fall back to the
168157
// one the VM was launched with.
169158
if (!isolate_snapshot) {
170159
isolate_snapshot = vm->GetVMData()->GetIsolateSnapshot();
171160
}
161+
return {std::move(vm), isolate_snapshot};
162+
}
163+
164+
std::unique_ptr<Shell> Shell::Create(
165+
const PlatformData& platform_data,
166+
const TaskRunners& task_runners,
167+
Settings settings,
168+
const Shell::CreateCallback<PlatformView>& on_create_platform_view,
169+
const Shell::CreateCallback<Rasterizer>& on_create_rasterizer,
170+
bool is_gpu_disabled) {
171+
// This must come first as it initializes tracing.
172+
PerformInitializationTasks(settings);
173+
174+
TRACE_EVENT0("flutter", "Shell::Create");
175+
176+
auto [vm, isolate_snapshot] = InferVmInitDataFromSettings(settings);
172177
auto resource_cache_limit_calculator =
173178
std::make_shared<ResourceCacheLimitCalculator>(
174179
settings.resource_cache_max_bytes_threshold);

shell/common/shell.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,29 @@ class Shell final : public PlatformView::Delegate,
438438
const std::shared_ptr<fml::ConcurrentTaskRunner>
439439
GetConcurrentWorkerTaskRunner() const;
440440

441+
// Infer the VM ref and the isolate snapshot based on the settings.
442+
//
443+
// If the VM is already running, the settings are ignored, but the returned
444+
// isolate snapshot always prioritize what is specified by the settings, and
445+
// falls back to the one VM was launched with.
446+
//
447+
// This function is what Shell::Create uses to infer snapshot settings.
448+
//
449+
// TODO(dkwingsmt): Extracting this method is part of a bigger change. If the
450+
// entire change is not eventually landed, we should merge this method back
451+
// to Create. https://github.com/flutter/flutter/issues/136826
452+
static std::pair<DartVMRef, fml::RefPtr<const DartSnapshot>>
453+
InferVmInitDataFromSettings(Settings& settings);
454+
441455
private:
442456
using ServiceProtocolHandler =
443457
std::function<bool(const ServiceProtocol::Handler::ServiceProtocolMap&,
444458
rapidjson::Document*)>;
445459

446460
/// A collection of message channels (by name) that have sent at least one
447-
/// message from a non-platform thread. Used to prevent printing the error log
448-
/// more than once per channel, as a badly behaving plugin may send multiple
449-
/// messages per second indefinitely.
461+
/// message from a non-platform thread. Used to prevent printing the error
462+
/// log more than once per channel, as a badly behaving plugin may send
463+
/// multiple messages per second indefinitely.
450464
std::mutex misbehaving_message_channels_mutex_;
451465
std::set<std::string> misbehaving_message_channels_;
452466
const TaskRunners task_runners_;
@@ -497,19 +511,20 @@ class Shell final : public PlatformView::Delegate,
497511
bool frame_timings_report_scheduled_ = false;
498512

499513
// Vector of FrameTiming::kCount * n timestamps for n frames whose timings
500-
// have not been reported yet. Vector of ints instead of FrameTiming is stored
501-
// here for easier conversions to Dart objects.
514+
// have not been reported yet. Vector of ints instead of FrameTiming is
515+
// stored here for easier conversions to Dart objects.
502516
std::vector<int64_t> unreported_timings_;
503517

504-
/// Manages the displays. This class is thread safe, can be accessed from any
505-
/// of the threads.
518+
/// Manages the displays. This class is thread safe, can be accessed from
519+
/// any of the threads.
506520
std::unique_ptr<DisplayManager> display_manager_;
507521

508522
// protects expected_frame_size_ which is set on platform thread and read on
509523
// raster thread
510524
std::mutex resize_mutex_;
511525

512-
// used to discard wrong size layer tree produced during interactive resizing
526+
// used to discard wrong size layer tree produced during interactive
527+
// resizing
513528
std::unordered_map<int64_t, SkISize> expected_frame_sizes_;
514529

515530
// Used to communicate the right frame bounds via service protocol.
@@ -746,7 +761,8 @@ class Shell final : public PlatformView::Delegate,
746761

747762
// Service protocol handler
748763
//
749-
// The returned SkSLs are base64 encoded. Decode before storing them to files.
764+
// The returned SkSLs are base64 encoded. Decode before storing them to
765+
// files.
750766
bool OnServiceProtocolGetSkSLs(
751767
const ServiceProtocol::Handler::ServiceProtocolMap& params,
752768
rapidjson::Document* response);
@@ -767,8 +783,8 @@ class Shell final : public PlatformView::Delegate,
767783

768784
// Service protocol handler
769785
//
770-
// Forces the FontCollection to reload the font manifest. Used to support hot
771-
// reload for fonts.
786+
// Forces the FontCollection to reload the font manifest. Used to support
787+
// hot reload for fonts.
772788
bool OnServiceProtocolReloadAssetFonts(
773789
const ServiceProtocol::Handler::ServiceProtocolMap& params,
774790
rapidjson::Document* response);

0 commit comments

Comments
 (0)