Embedder API for setting the persistent cache path (flutter/engine#7915)
Fixes https://github.com/flutter/flutter/issues/28278
This commit is contained in:
@@ -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<fml::UniqueFD>(
|
||||
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<fml::UniqueFD>(
|
||||
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.";
|
||||
|
||||
@@ -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<fml::TaskRunner> task_runner);
|
||||
@@ -28,6 +30,7 @@ class PersistentCache : public GrContextOptions::PersistentCache {
|
||||
void RemoveWorkerTaskRunner(fml::RefPtr<fml::TaskRunner> task_runner);
|
||||
|
||||
private:
|
||||
static std::string cache_base_path_;
|
||||
std::shared_ptr<fml::UniqueFD> cache_directory_;
|
||||
mutable std::mutex worker_task_runners_mutex_;
|
||||
std::multiset<fml::RefPtr<fml::TaskRunner>> worker_task_runners_
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user