Skip to content

Commit 87edd94

Browse files
authored
Add read-only persistent cache (#8049)
Some clients (e.g., embedded devices) prefer generating persistent cache files for the specific device beforehand, and ship them as readonly files in OTA packages.
1 parent 4c94049 commit 87edd94

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

shell/common/persistent_cache.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,21 @@ static std::string SkKeyToFilePath(const SkData& data) {
3535
return encode_result.second;
3636
}
3737

38+
bool PersistentCache::gIsReadOnly = false;
39+
3840
PersistentCache* PersistentCache::GetCacheForProcess() {
3941
static std::unique_ptr<PersistentCache> gPersistentCache;
4042
static std::once_flag once = {};
41-
std::call_once(once, []() { gPersistentCache.reset(new PersistentCache()); });
43+
std::call_once(
44+
once, []() { gPersistentCache.reset(new PersistentCache(gIsReadOnly)); });
4245
return gPersistentCache.get();
4346
}
4447

4548
void PersistentCache::SetCacheDirectoryPath(std::string path) {
4649
cache_base_path_ = path;
4750
}
4851

49-
PersistentCache::PersistentCache() {
52+
PersistentCache::PersistentCache(bool read_only) : is_read_only_(read_only) {
5053
fml::UniqueFD cache_base_dir;
5154
if (cache_base_path_.length()) {
5255
cache_base_dir = fml::OpenDirectory(cache_base_path_.c_str(), false,
@@ -60,7 +63,8 @@ PersistentCache::PersistentCache() {
6063
CreateDirectory(cache_base_dir,
6164
{"flutter_engine", blink::GetFlutterEngineVersion(),
6265
"skia", blink::GetSkiaVersion()},
63-
fml::FilePermission::kReadWrite));
66+
read_only ? fml::FilePermission::kRead
67+
: fml::FilePermission::kReadWrite));
6468
}
6569
if (!IsValid()) {
6670
FML_LOG(WARNING) << "Could not acquire the persistent cache directory. "
@@ -130,6 +134,10 @@ static void PersistentCacheStore(fml::RefPtr<fml::TaskRunner> worker,
130134

131135
// |GrContextOptions::PersistentCache|
132136
void PersistentCache::store(const SkData& key, const SkData& data) {
137+
if (is_read_only_) {
138+
return;
139+
}
140+
133141
if (!IsValid()) {
134142
return;
135143
}

shell/common/persistent_cache.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ namespace shell {
1919

2020
class PersistentCache : public GrContextOptions::PersistentCache {
2121
public:
22+
// Mutable static switch that can be set before GetCacheForProcess. If true,
23+
// we'll only read existing caches but not generate new ones. Some clients
24+
// (e.g., embedded devices) prefer generating persistent cache files for the
25+
// specific device beforehand, and ship them as readonly files in OTA
26+
// packages.
27+
static bool gIsReadOnly;
28+
2229
static PersistentCache* GetCacheForProcess();
2330

2431
static void SetCacheDirectoryPath(std::string path);
@@ -31,14 +38,16 @@ class PersistentCache : public GrContextOptions::PersistentCache {
3138

3239
private:
3340
static std::string cache_base_path_;
41+
42+
const bool is_read_only_;
3443
std::shared_ptr<fml::UniqueFD> cache_directory_;
3544
mutable std::mutex worker_task_runners_mutex_;
3645
std::multiset<fml::RefPtr<fml::TaskRunner>> worker_task_runners_
3746
FML_GUARDED_BY(worker_task_runners_mutex_);
3847

3948
bool IsValid() const;
4049

41-
PersistentCache();
50+
PersistentCache(bool read_only = false);
4251

4352
// |GrContextOptions::PersistentCache|
4453
sk_sp<SkData> load(const SkData& key) override;

shell/platform/embedder/embedder.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,10 @@ FlutterEngineResult FlutterEngineRun(size_t version,
348348
shell::PersistentCache::SetCacheDirectoryPath(persistent_cache_path);
349349
}
350350

351+
if (SAFE_ACCESS(args, is_persistent_cache_read_only, false)) {
352+
shell::PersistentCache::gIsReadOnly = true;
353+
}
354+
351355
fml::CommandLine command_line;
352356
if (SAFE_ACCESS(args, command_line_argc, 0) != 0 &&
353357
SAFE_ACCESS(args, command_line_argv, nullptr) != nullptr) {

shell/platform/embedder/embedder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ typedef struct {
549549
// Flutter application (such as compiled shader programs used by Skia).
550550
// This is optional. The string must be NULL terminated.
551551
const char* persistent_cache_path;
552+
553+
// If true, we'll only read the existing cache, but not write new ones.
554+
bool is_persistent_cache_read_only;
555+
552556
// A callback that gets invoked by the engine when it attempts to wait for a
553557
// platform vsync event. The engine will give the platform a baton that needs
554558
// to be returned back to the engine via |FlutterEngineOnVsync|. All batons

0 commit comments

Comments
 (0)