Skip to content

Commit

Permalink
Embedder API for setting the persistent cache path (flutter#7915)
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-simmons authored Feb 22, 2019
1 parent f1f19bb commit 043d92c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
32 changes: 22 additions & 10 deletions shell/common/persistent_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand All @@ -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.";
Expand Down
3 changes: 3 additions & 0 deletions shell/common/persistent_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ 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);

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_
Expand Down
7 changes: 7 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 043d92c

Please sign in to comment.