Embedder API for setting the persistent cache path (flutter/engine#7915)

Fixes https://github.com/flutter/flutter/issues/28278
This commit is contained in:
Jason Simmons
2019-02-21 17:30:17 -08:00
committed by GitHub
parent c416fe99ee
commit 4290d0349c
4 changed files with 36 additions and 10 deletions

View File

@@ -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.";

View File

@@ -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_

View File

@@ -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) {

View File

@@ -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