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

Commit e229eb6

Browse files
committed
Move PersistentCache to flow/
PersistentCache no longer manages its version internally. It is the client's responsibility to inject a version directory at its discretion. shell/gpu/ no longer depends on shell/common after this, so there is no need to permute it for product / non-product configurations. BUG: fxb/52961, flutter/flutter#53399
1 parent b77e45c commit e229eb6

23 files changed

+494
-388
lines changed

assets/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ source_set("assets") {
1212
]
1313

1414
deps = [
15-
"//flutter/common",
1615
"//flutter/fml",
1716
]
1817

flow/BUILD.gn

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ source_set_maybe_fuchsia_legacy("flow") {
5454
"matrix_decomposition.h",
5555
"paint_utils.cc",
5656
"paint_utils.h",
57+
"persistent_cache.cc",
58+
"persistent_cache.h",
5759
"raster_cache.cc",
5860
"raster_cache.h",
5961
"raster_cache_key.cc",
@@ -73,8 +75,9 @@ source_set_maybe_fuchsia_legacy("flow") {
7375
public_configs = [ "//flutter:config" ]
7476

7577
deps = [
76-
"//flutter/common",
78+
"//flutter/assets",
7779
"//flutter/fml",
80+
"//third_party/rapidjson",
7881
"//third_party/skia",
7982
]
8083

@@ -151,6 +154,7 @@ if (enable_unittests) {
151154
"layers/transform_layer_unittests.cc",
152155
"matrix_decomposition_unittests.cc",
153156
"mutators_stack_unittests.cc",
157+
"persistent_cache_unittests.cc",
154158
"raster_cache_unittests.cc",
155159
"rtree_unittests.cc",
156160
"skia_gpu_object_unittests.cc",
@@ -161,6 +165,7 @@ if (enable_unittests) {
161165

162166
deps = [
163167
":flow_fixtures",
168+
"//flutter/assets",
164169
"//flutter/fml",
165170
"//flutter/testing:skia",
166171
"//flutter/testing:testing_lib",

shell/common/persistent_cache.cc renamed to flow/persistent_cache.cc

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#include "flutter/shell/common/persistent_cache.h"
5+
#include "flutter/flow/persistent_cache.h"
66

77
#include <future>
88
#include <memory>
99
#include <string>
1010
#include <string_view>
1111

12-
#include "rapidjson/document.h"
13-
#include "third_party/skia/include/utils/SkBase64.h"
14-
1512
#include "flutter/fml/base32.h"
1613
#include "flutter/fml/file.h"
1714
#include "flutter/fml/logging.h"
1815
#include "flutter/fml/make_copyable.h"
1916
#include "flutter/fml/mapping.h"
2017
#include "flutter/fml/paths.h"
2118
#include "flutter/fml/trace_event.h"
22-
#include "flutter/shell/version/version.h"
19+
#include "third_party/rapidjson/include/rapidjson/document.h"
20+
#include "third_party/skia/include/utils/SkBase64.h"
2321

2422
namespace flutter {
2523

2624
std::string PersistentCache::cache_base_path_;
25+
std::string PersistentCache::cache_version_;
2726

2827
std::shared_ptr<AssetManager> PersistentCache::asset_manager_;
2928

@@ -75,10 +74,14 @@ void PersistentCache::ResetCacheForProcess() {
7574
strategy_set_ = false;
7675
}
7776

78-
void PersistentCache::SetCacheDirectoryPath(std::string path) {
77+
void PersistentCache::SetCacheDirectoryPath(const std::string& path) {
7978
cache_base_path_ = path;
8079
}
8180

81+
void PersistentCache::SetCacheVersion(const std::string& version) {
82+
cache_version_ = version;
83+
}
84+
8285
bool PersistentCache::Purge() {
8386
// Make sure that this is called after the worker task runner setup so all the
8487
// file system modifications would happen on that single thread to avoid
@@ -100,31 +103,29 @@ bool PersistentCache::Purge() {
100103

101104
namespace {
102105

103-
constexpr char kEngineComponent[] = "flutter_engine";
104-
105-
static void FreeOldCacheDirectory(const fml::UniqueFD& cache_base_dir) {
106-
fml::UniqueFD engine_dir =
107-
fml::OpenDirectoryReadOnly(cache_base_dir, kEngineComponent);
108-
if (!engine_dir.is_valid()) {
109-
return;
110-
}
111-
fml::VisitFiles(engine_dir, [](const fml::UniqueFD& directory,
112-
const std::string& filename) {
113-
if (filename != GetFlutterEngineVersion()) {
114-
auto dir = fml::OpenDirectory(directory, filename.c_str(), false,
115-
fml::FilePermission::kReadWrite);
116-
if (dir.is_valid()) {
117-
fml::RemoveDirectoryRecursively(directory, filename.c_str());
118-
}
119-
}
120-
return true;
121-
});
106+
static void FreeOldCacheDirectory(const fml::UniqueFD& cache_base_dir,
107+
const std::string& cache_version) {
108+
fml::VisitFiles(
109+
cache_base_dir, [cache_version](const fml::UniqueFD& directory,
110+
const std::string& filename) {
111+
if (filename != cache_version) {
112+
auto dir = fml::OpenDirectory(directory, filename.c_str(), false,
113+
fml::FilePermission::kReadWrite);
114+
if (dir.is_valid()) {
115+
fml::RemoveDirectoryRecursively(directory, filename.c_str());
116+
}
117+
}
118+
return true;
119+
});
122120
}
123121

124122
static std::shared_ptr<fml::UniqueFD> MakeCacheDirectory(
125123
const std::string& global_cache_base_path,
124+
const std::string& cache_version,
126125
bool read_only,
127126
bool cache_sksl) {
127+
FML_CHECK(cache_version.length()) << "Cache version unspecified";
128+
128129
fml::UniqueFD cache_base_dir;
129130
if (global_cache_base_path.length()) {
130131
cache_base_dir = fml::OpenDirectory(global_cache_base_path.c_str(), false,
@@ -134,9 +135,8 @@ static std::shared_ptr<fml::UniqueFD> MakeCacheDirectory(
134135
}
135136

136137
if (cache_base_dir.is_valid()) {
137-
FreeOldCacheDirectory(cache_base_dir);
138-
std::vector<std::string> components = {
139-
kEngineComponent, GetFlutterEngineVersion(), "skia", GetSkiaVersion()};
138+
FreeOldCacheDirectory(cache_base_dir, cache_version);
139+
std::vector<std::string> components = {cache_version};
140140
if (cache_sksl) {
141141
components.push_back(PersistentCache::kSkSLSubdirName);
142142
}
@@ -198,9 +198,9 @@ std::vector<PersistentCache::SkSLCache> PersistentCache::LoadSkSLs() {
198198
mapping = asset_manager_->GetAsMapping(kAssetFileName);
199199
}
200200
if (mapping == nullptr) {
201-
FML_LOG(INFO) << "No sksl asset found.";
201+
FML_DLOG(INFO) << "No sksl asset found.";
202202
} else {
203-
FML_LOG(INFO) << "Found sksl asset. Loading SkSLs from it...";
203+
FML_DLOG(INFO) << "Found sksl asset. Loading SkSLs from it...";
204204
rapidjson::Document json_doc;
205205
rapidjson::ParseResult parse_result =
206206
json_doc.Parse(reinterpret_cast<const char*>(mapping->GetMapping()),
@@ -225,9 +225,14 @@ std::vector<PersistentCache::SkSLCache> PersistentCache::LoadSkSLs() {
225225

226226
PersistentCache::PersistentCache(bool read_only)
227227
: is_read_only_(read_only),
228-
cache_directory_(MakeCacheDirectory(cache_base_path_, read_only, false)),
229-
sksl_cache_directory_(
230-
MakeCacheDirectory(cache_base_path_, read_only, true)) {
228+
cache_directory_(MakeCacheDirectory(cache_base_path_,
229+
cache_version_,
230+
read_only,
231+
false)),
232+
sksl_cache_directory_(MakeCacheDirectory(cache_base_path_,
233+
cache_version_,
234+
read_only,
235+
true)) {
231236
if (!IsValid()) {
232237
FML_LOG(WARNING) << "Could not acquire the persistent cache directory. "
233238
"Caching of GPU resources on disk is disabled.";
@@ -341,7 +346,7 @@ void PersistentCache::DumpSkp(const SkData& data) {
341346
auto ticks = fml::TimePoint::Now().ToEpochDelta().ToNanoseconds();
342347
name_stream << "shader_dump_" << std::to_string(ticks) << ".skp";
343348
std::string file_name = name_stream.str();
344-
FML_LOG(INFO) << "Dumping " << file_name;
349+
FML_DLOG(INFO) << "Dumping " << file_name;
345350
auto mapping = std::make_unique<fml::DataMapping>(
346351
std::vector<uint8_t>{data.bytes(), data.bytes() + data.size()});
347352
PersistentCacheStore(GetWorkerTaskRunner(), cache_directory_,

shell/common/persistent_cache.h renamed to flow/persistent_cache.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#ifndef FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
6-
#define FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
5+
#ifndef FLUTTER_FLOW_PERSISTENT_CACHE_H_
6+
#define FLUTTER_FLOW_PERSISTENT_CACHE_H_
77

88
#include <memory>
99
#include <mutex>
@@ -35,7 +35,11 @@ class PersistentCache : public GrContextOptions::PersistentCache {
3535

3636
// This must be called before |GetCacheForProcess|. Otherwise, it won't
3737
// affect the cache directory returned by |GetCacheForProcess|.
38-
static void SetCacheDirectoryPath(std::string path);
38+
static void SetCacheDirectoryPath(const std::string& path);
39+
40+
// This must be called before |GetCacheForProcess|. Otherwise, it won't
41+
// affect the cache directory returned by |GetCacheForProcess|.
42+
static void SetCacheVersion(const std::string& version);
3943

4044
// Convert a binary SkData key into a Base32 encoded string.
4145
//
@@ -83,6 +87,7 @@ class PersistentCache : public GrContextOptions::PersistentCache {
8387

8488
private:
8589
static std::string cache_base_path_;
90+
static std::string cache_version_;
8691

8792
static std::shared_ptr<AssetManager> asset_manager_;
8893

@@ -125,4 +130,4 @@ class PersistentCache : public GrContextOptions::PersistentCache {
125130

126131
} // namespace flutter
127132

128-
#endif // FLUTTER_SHELL_COMMON_PERSISTENT_CACHE_H_
133+
#endif // FLUTTER_FLOW_PERSISTENT_CACHE_H_

0 commit comments

Comments
 (0)