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
2422namespace flutter {
2523
2624std::string PersistentCache::cache_base_path_;
25+ std::string PersistentCache::cache_version_;
2726
2827std::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+
8285bool 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
101104namespace {
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
124122static 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
226226PersistentCache::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_,
0 commit comments