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

Commit 24b2604

Browse files
committed
[asset_manager] add ability to get all files matching a pattern from
Asset Manager. Gives Asset Manager the ability to find files matching a specific pattern and uses that functionality to implement an entry point in PersistantCache which retreives mappings for all skp files in the Asset Bundle.
1 parent b046a42 commit 24b2604

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed

assets/asset_manager.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ std::unique_ptr<fml::Mapping> AssetManager::GetAsMapping(
5151
return nullptr;
5252
}
5353

54+
// |AssetResolver|
55+
std::vector<std::unique_ptr<fml::Mapping>> AssetManager::GetAsMappings(
56+
const std::string& asset_pattern) const {
57+
std::vector<std::unique_ptr<fml::Mapping>> mappings;
58+
if (asset_pattern.size() == 0) {
59+
return mappings;
60+
}
61+
TRACE_EVENT1("flutter", "AssetManager::GetAsMappings", "pattern",
62+
asset_pattern.c_str());
63+
for (const auto& resolver : resolvers_) {
64+
auto resolver_mappings = resolver->GetAsMappings(asset_pattern);
65+
mappings.insert(mappings.end(),
66+
std::make_move_iterator(resolver_mappings.begin()),
67+
std::make_move_iterator(resolver_mappings.end()));
68+
}
69+
return mappings;
70+
}
71+
5472
// |AssetResolver|
5573
bool AssetManager::IsValid() const {
5674
return resolvers_.size() > 0;

assets/asset_manager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class AssetManager final : public AssetResolver {
3737
std::unique_ptr<fml::Mapping> GetAsMapping(
3838
const std::string& asset_name) const override;
3939

40+
// |AssetResolver|
41+
std::vector<std::unique_ptr<fml::Mapping>> GetAsMappings(
42+
const std::string& asset_pattern) const override;
43+
4044
private:
4145
std::deque<std::unique_ptr<AssetResolver>> resolvers_;
4246

assets/asset_resolver.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ class AssetResolver {
4242
[[nodiscard]] virtual std::unique_ptr<fml::Mapping> GetAsMapping(
4343
const std::string& asset_name) const = 0;
4444

45+
// Same as GetAsMapping() but returns mappings for all files who's name
46+
// matches |pattern|. Returns empty vector if no matching assets are found
47+
[[nodiscard]] virtual std::vector<std::unique_ptr<fml::Mapping>>
48+
GetAsMappings(const std::string& asset_pattern) const = 0;
49+
4550
private:
4651
FML_DISALLOW_COPY_AND_ASSIGN(AssetResolver);
4752
};

assets/directory_asset_bundle.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "flutter/assets/directory_asset_bundle.h"
66

7+
#include <regex>
78
#include <utility>
89

910
#include "flutter/fml/eintr_wrapper.h"
@@ -53,4 +54,32 @@ std::unique_ptr<fml::Mapping> DirectoryAssetBundle::GetAsMapping(
5354
return mapping;
5455
}
5556

57+
std::vector<std::unique_ptr<fml::Mapping>> DirectoryAssetBundle::GetAsMappings(
58+
const std::string& asset_pattern) const {
59+
std::vector<std::unique_ptr<fml::Mapping>> mappings;
60+
if (!is_valid_) {
61+
FML_DLOG(WARNING) << "Asset bundle was not valid.";
62+
return mappings;
63+
}
64+
65+
std::regex asset_regex(asset_pattern);
66+
fml::FileVisitor visitor = [&](const fml::UniqueFD& directory,
67+
const std::string& filename) {
68+
if (std::regex_match(filename, asset_regex)) {
69+
auto mapping = std::make_unique<fml::FileMapping>(fml::OpenFile(
70+
directory, filename.c_str(), false, fml::FilePermission::kRead));
71+
72+
if (mapping && mapping->IsValid()) {
73+
mappings.push_back(std::move(mapping));
74+
} else {
75+
FML_LOG(ERROR) << "Mapping " << filename << " failed";
76+
}
77+
}
78+
return true;
79+
};
80+
fml::VisitFilesRecursively(descriptor_, visitor);
81+
82+
return mappings;
83+
}
84+
5685
} // namespace flutter

assets/directory_asset_bundle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class DirectoryAssetBundle : public AssetResolver {
3434
std::unique_ptr<fml::Mapping> GetAsMapping(
3535
const std::string& asset_name) const override;
3636

37+
// |AssetResolver|
38+
std::vector<std::unique_ptr<fml::Mapping>> GetAsMappings(
39+
const std::string& asset_pattern) const override;
40+
3741
FML_DISALLOW_COPY_AND_ASSIGN(DirectoryAssetBundle);
3842
};
3943

shell/common/persistent_cache.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,14 @@ void PersistentCache::SetAssetManager(std::shared_ptr<AssetManager> value) {
391391
asset_manager_ = value;
392392
}
393393

394+
std::vector<std::unique_ptr<fml::Mapping>>
395+
PersistentCache::GetSkpsFromAssetManager() const {
396+
if (!asset_manager_) {
397+
FML_LOG(ERROR)
398+
<< "PersistentCache::GetSkpsFromAssetManager: Asset manager not set!";
399+
return std::vector<std::unique_ptr<fml::Mapping>>();
400+
}
401+
return asset_manager_->GetAsMappings(".*\\.skp$");
402+
}
403+
394404
} // namespace flutter

shell/common/persistent_cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class PersistentCache : public GrContextOptions::PersistentCache {
7474
/// Load all the SkSL shader caches in the right directory.
7575
std::vector<SkSLCache> LoadSkSLs();
7676

77+
// Return mappings for all skp's accessible through the AssetManager
78+
std::vector<std::unique_ptr<fml::Mapping>> GetSkpsFromAssetManager() const;
79+
7780
/// Set the asset manager from which PersistentCache can load SkLSs. A nullptr
7881
/// can be provided to clear the asset manager.
7982
static void SetAssetManager(std::shared_ptr<AssetManager> value);

shell/common/shell_unittests.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <future>
1111
#include <memory>
1212

13+
#include "assets/directory_asset_bundle.h"
1314
#include "flutter/flow/layers/layer_tree.h"
1415
#include "flutter/flow/layers/picture_layer.h"
1516
#include "flutter/flow/layers/transform_layer.h"
@@ -2208,5 +2209,70 @@ TEST_F(ShellTest, EngineRootIsolateLaunchesDontTakeVMDataSettings) {
22082209
isolate_create_latch.Wait();
22092210
}
22102211

2212+
TEST_F(ShellTest, AssetManagerSingle) {
2213+
fml::ScopedTemporaryDirectory asset_dir;
2214+
fml::UniqueFD asset_dir_fd = fml::OpenDirectory(
2215+
asset_dir.path().c_str(), false, fml::FilePermission::kRead);
2216+
2217+
std::string filename = "test";
2218+
2219+
bool success = fml::WriteAtomically(asset_dir_fd, filename.c_str(),
2220+
fml::DataMapping(filename));
2221+
ASSERT_TRUE(success);
2222+
2223+
AssetManager asset_manager;
2224+
asset_manager.PushBack(
2225+
std::make_unique<DirectoryAssetBundle>(std::move(asset_dir_fd), false));
2226+
2227+
auto mapping = asset_manager.GetAsMapping(filename);
2228+
ASSERT_TRUE(mapping != nullptr);
2229+
2230+
std::string result(reinterpret_cast<const char*>(mapping->GetMapping()),
2231+
mapping->GetSize());
2232+
2233+
ASSERT_TRUE(result == filename);
2234+
}
2235+
TEST_F(ShellTest, AssetManagerMulti) {
2236+
fml::ScopedTemporaryDirectory asset_dir;
2237+
fml::UniqueFD asset_dir_fd = fml::OpenDirectory(
2238+
asset_dir.path().c_str(), false, fml::FilePermission::kRead);
2239+
2240+
std::vector<std::string> filenames = {
2241+
"good0",
2242+
"bad0",
2243+
"good1",
2244+
"bad1",
2245+
};
2246+
2247+
for (auto filename : filenames) {
2248+
bool success = fml::WriteAtomically(asset_dir_fd, filename.c_str(),
2249+
fml::DataMapping(filename));
2250+
ASSERT_TRUE(success);
2251+
}
2252+
2253+
AssetManager asset_manager;
2254+
asset_manager.PushBack(
2255+
std::make_unique<DirectoryAssetBundle>(std::move(asset_dir_fd), false));
2256+
2257+
auto mappings = asset_manager.GetAsMappings("(.*)");
2258+
ASSERT_TRUE(mappings.size() == 4);
2259+
2260+
std::vector<std::string> expected_results = {
2261+
"good0",
2262+
"good1",
2263+
};
2264+
2265+
mappings = asset_manager.GetAsMappings("(.*)good(.*)");
2266+
ASSERT_TRUE(mappings.size() == expected_results.size());
2267+
2268+
for (auto& mapping : mappings) {
2269+
std::string result(reinterpret_cast<const char*>(mapping->GetMapping()),
2270+
mapping->GetSize());
2271+
ASSERT_NE(
2272+
std::find(expected_results.begin(), expected_results.end(), result),
2273+
expected_results.end());
2274+
}
2275+
}
2276+
22112277
} // namespace testing
22122278
} // namespace flutter

0 commit comments

Comments
 (0)