This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Move Assets loading from UI thread to IO thread #22077
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e643563
Move Assets loading from UI thread to IO thread
zhongwuzw f4c7d21
Remove RunNowOrPostTask
zhongwuzw 842b18e
Add unit test
zhongwuzw a1c9599
Merge branch 'master' into opt_asset_io
zhongwuzw 56730af
Update shell_unittests.cc
zhongwuzw 403db70
Merge branch 'master' into opt_asset_io
zhongwuzw 887789c
Fixes tests failure
zhongwuzw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| #include <vector> | ||
|
|
||
| #include "assets/directory_asset_bundle.h" | ||
| #include "flutter/assets/asset_resolver.h" | ||
| #include "flutter/common/graphics/persistent_cache.h" | ||
| #include "flutter/flow/layers/layer_tree.h" | ||
| #include "flutter/flow/layers/picture_layer.h" | ||
|
|
@@ -148,6 +149,83 @@ class TestAssetResolver : public AssetResolver { | |
| AssetResolver::AssetResolverType type_; | ||
| }; | ||
|
|
||
| class PlatformMessageResponseTest : public flutter::PlatformMessageResponse { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think both of these classes could be mocked with gmock which would be less code to maintain. |
||
| public: | ||
| void Complete(std::unique_ptr<fml::Mapping> data) override; | ||
|
|
||
| void CompleteEmpty() override; | ||
|
|
||
| private: | ||
| explicit PlatformMessageResponseTest(); | ||
|
|
||
| ~PlatformMessageResponseTest() override; | ||
|
|
||
| FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseTest); | ||
| }; | ||
|
|
||
| PlatformMessageResponseTest::PlatformMessageResponseTest() {} | ||
|
|
||
| PlatformMessageResponseTest::~PlatformMessageResponseTest() = default; | ||
|
|
||
| void PlatformMessageResponseTest::Complete(std::unique_ptr<fml::Mapping> data) { | ||
| } | ||
|
|
||
| void PlatformMessageResponseTest::CompleteEmpty() {} | ||
|
|
||
| class AssetTestResolver : public AssetResolver { | ||
| public: | ||
| AssetTestResolver(Shell* shell); | ||
|
|
||
| ~AssetTestResolver() override; | ||
|
|
||
| private: | ||
| Shell* shell_; | ||
| bool IsValid() const override; | ||
| // |AssetResolver| | ||
| AssetResolver::AssetResolverType GetType() const override; | ||
| // |AssetResolver| | ||
| std::unique_ptr<fml::Mapping> GetAsMapping( | ||
| const std::string& asset_name) const override; | ||
| void CheckQueue() const; | ||
| // |AssetResolver| | ||
| bool IsValidAfterAssetManagerChange() const override; | ||
| FML_DISALLOW_COPY_AND_ASSIGN(AssetTestResolver); | ||
| }; | ||
|
|
||
| AssetTestResolver::AssetTestResolver(Shell* shell) : shell_(shell){}; | ||
|
|
||
| AssetTestResolver::~AssetTestResolver() = default; | ||
|
|
||
| AssetResolver::AssetResolverType AssetTestResolver::GetType() const { | ||
| return AssetResolverType::kAssetManager; | ||
| } | ||
|
|
||
| void AssetTestResolver::CheckQueue() const { | ||
| const auto current_queue_id = fml::MessageLoop::GetCurrentTaskQueueId(); | ||
| const auto io_queue_id = | ||
| shell_->GetTaskRunners().GetIOTaskRunner()->GetTaskQueueId(); | ||
| ASSERT_TRUE(current_queue_id == io_queue_id); | ||
| } | ||
|
|
||
| // |AssetResolver| | ||
| std::unique_ptr<fml::Mapping> AssetTestResolver::GetAsMapping( | ||
| const std::string& asset_name) const { | ||
| if (asset_name.compare("TestFlutterLoadAssets.json") == 0) { | ||
| CheckQueue(); | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| // |AssetResolver| | ||
| bool AssetTestResolver::IsValid() const { | ||
| return true; | ||
| } | ||
|
|
||
| // |AssetResolver| | ||
| bool AssetTestResolver::IsValidAfterAssetManagerChange() const { | ||
| return false; | ||
| } | ||
|
|
||
| static bool ValidateShell(Shell* shell) { | ||
| if (!shell) { | ||
| return false; | ||
|
|
@@ -222,6 +300,38 @@ static void TestDartVmFlags(std::vector<const char*>& flags) { | |
| } | ||
| } | ||
|
|
||
| TEST_F(ShellTest, TestFlutterLoadAssetsOnIOThread) { | ||
| Settings settings = CreateSettingsForFixture(); | ||
| auto task_runner = CreateNewThread(); | ||
| ThreadHost thread_host("io.flutter.test." + GetCurrentTestName() + ".", | ||
| ThreadHost::Type::Platform | ThreadHost::Type::RASTER | | ||
| ThreadHost::Type::IO | ThreadHost::Type::UI); | ||
|
|
||
| TaskRunners task_runners("test", thread_host.ui_thread->GetTaskRunner(), | ||
| thread_host.raster_thread->GetTaskRunner(), | ||
| thread_host.ui_thread->GetTaskRunner(), | ||
| thread_host.io_thread->GetTaskRunner()); | ||
|
|
||
| std::unique_ptr<Shell> shell = | ||
| CreateShell(std::move(settings), std::move(task_runners)); | ||
|
|
||
| auto configuration = RunConfiguration::InferFromSettings(settings); | ||
| configuration.SetEntrypoint("emptyMain"); | ||
| configuration.AddAssetResolver( | ||
| std::make_unique<AssetTestResolver>(shell.get())); | ||
|
|
||
| RunEngine(shell.get(), std::move(configuration)); | ||
|
|
||
| std::string request_json = "TestFlutterLoadAssets.json"; | ||
| std::vector<uint8_t> data(request_json.begin(), request_json.end()); | ||
| auto platform_message = fml::MakeRefCounted<PlatformMessage>( | ||
| "flutter/assets", std::move(data), | ||
| fml::MakeRefCounted<flutter::testing::PlatformMessageResponseTest>()); | ||
| SendEnginePlatformMessage(shell.get(), std::move(platform_message)); | ||
|
|
||
| DestroyShell(std::move(shell), std::move(task_runners)); | ||
|
Comment on lines
+330
to
+332
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test has no assertion. |
||
| } | ||
|
|
||
| static void PostSync(const fml::RefPtr<fml::TaskRunner>& task_runner, | ||
| const fml::closure& task) { | ||
| fml::AutoResetWaitableEvent latch; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this early out should happen on the UI thread since it should be low cost. What line of this code is the heaviest? GetAsMapping? I'd only defer to the IO thread if we are going to hit that since posting to another thread incurs extra cost.