diff --git a/engine/src/flutter/shell/common/persistent_cache.cc b/engine/src/flutter/shell/common/persistent_cache.cc index c3f6ebf025..bab0e7ac1b 100644 --- a/engine/src/flutter/shell/common/persistent_cache.cc +++ b/engine/src/flutter/shell/common/persistent_cache.cc @@ -17,6 +17,8 @@ namespace shell { +std::string PersistentCache::cache_base_path_; + static std::string SkKeyToFilePath(const SkData& data) { if (data.data() == nullptr || data.size() == 0) { return ""; @@ -40,16 +42,26 @@ PersistentCache* PersistentCache::GetCacheForProcess() { return gPersistentCache.get(); } -PersistentCache::PersistentCache() - : cache_directory_(std::make_shared( - CreateDirectory(fml::paths::GetCachesDirectory(), - { - "flutter_engine", // - blink::GetFlutterEngineVersion(), // - "skia", // - blink::GetSkiaVersion() // - }, - fml::FilePermission::kReadWrite))) { +void PersistentCache::SetCacheDirectoryPath(std::string path) { + cache_base_path_ = path; +} + +PersistentCache::PersistentCache() { + fml::UniqueFD cache_base_dir; + if (cache_base_path_.length()) { + cache_base_dir = fml::OpenDirectory(cache_base_path_.c_str(), false, + fml::FilePermission::kRead); + } else { + cache_base_dir = fml::paths::GetCachesDirectory(); + } + + if (cache_base_dir.is_valid()) { + cache_directory_ = std::make_shared( + CreateDirectory(cache_base_dir, + {"flutter_engine", blink::GetFlutterEngineVersion(), + "skia", blink::GetSkiaVersion()}, + fml::FilePermission::kReadWrite)); + } if (!IsValid()) { FML_LOG(WARNING) << "Could not acquire the persistent cache directory. " "Caching of GPU resources on disk is disabled."; diff --git a/engine/src/flutter/shell/common/persistent_cache.h b/engine/src/flutter/shell/common/persistent_cache.h index 32465a5c8c..cc65d6ad62 100644 --- a/engine/src/flutter/shell/common/persistent_cache.h +++ b/engine/src/flutter/shell/common/persistent_cache.h @@ -21,6 +21,8 @@ class PersistentCache : public GrContextOptions::PersistentCache { public: static PersistentCache* GetCacheForProcess(); + static void SetCacheDirectoryPath(std::string path); + ~PersistentCache() override; void AddWorkerTaskRunner(fml::RefPtr task_runner); @@ -28,6 +30,7 @@ class PersistentCache : public GrContextOptions::PersistentCache { void RemoveWorkerTaskRunner(fml::RefPtr task_runner); private: + static std::string cache_base_path_; std::shared_ptr cache_directory_; mutable std::mutex worker_task_runners_mutex_; std::multiset> worker_task_runners_ diff --git a/engine/src/flutter/shell/platform/embedder/embedder.cc b/engine/src/flutter/shell/platform/embedder/embedder.cc index f0f90c1256..6444104a1c 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.cc +++ b/engine/src/flutter/shell/platform/embedder/embedder.cc @@ -24,6 +24,7 @@ #include "flutter/fml/make_copyable.h" #include "flutter/fml/message_loop.h" #include "flutter/fml/paths.h" +#include "flutter/shell/common/persistent_cache.h" #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/switches.h" #include "flutter/shell/platform/embedder/embedder.h" @@ -327,6 +328,12 @@ FlutterEngineResult FlutterEngineRun(size_t version, icu_data_path = SAFE_ACCESS(args, icu_data_path, nullptr); } + if (SAFE_ACCESS(args, persistent_cache_path, nullptr) != nullptr) { + std::string persistent_cache_path = + SAFE_ACCESS(args, persistent_cache_path, nullptr); + shell::PersistentCache::SetCacheDirectoryPath(persistent_cache_path); + } + fml::CommandLine command_line; if (SAFE_ACCESS(args, command_line_argc, 0) != 0 && SAFE_ACCESS(args, command_line_argv, nullptr) != nullptr) { diff --git a/engine/src/flutter/shell/platform/embedder/embedder.h b/engine/src/flutter/shell/platform/embedder/embedder.h index 15c5e4922c..0bcd163d59 100644 --- a/engine/src/flutter/shell/platform/embedder/embedder.h +++ b/engine/src/flutter/shell/platform/embedder/embedder.h @@ -505,6 +505,10 @@ typedef struct { // |FlutterEngineRun| call is made. FlutterUpdateSemanticsCustomActionCallback update_semantics_custom_action_callback; + // Path to a directory used to store data that is cached across runs of a + // Flutter application (such as compiled shader programs used by Skia). + // This is optional. The string must be NULL terminated. + const char* persistent_cache_path; } FlutterProjectArgs; FLUTTER_EXPORT