Flutter Engine
The Flutter Engine
persistent_cache.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_COMMON_GRAPHICS_PERSISTENT_CACHE_H_
6#define FLUTTER_COMMON_GRAPHICS_PERSISTENT_CACHE_H_
7
8#if !SLIMPELLER
9
10#include <memory>
11#include <mutex>
12#include <set>
13
14#include "flutter/assets/asset_manager.h"
15#include "flutter/fml/macros.h"
16#include "flutter/fml/task_runner.h"
17#include "flutter/fml/unique_fd.h"
19
20class GrDirectContext;
21
22namespace flutter {
23
24namespace testing {
25class ShellTest;
26}
27
28/// A cache of SkData that gets stored to disk.
29///
30/// This is mainly used for Shaders but is also written to by Dart. It is
31/// thread-safe for reading and writing from multiple threads.
33 public:
34 // Mutable static switch that can be set before GetCacheForProcess. If true,
35 // we'll only read existing caches but not generate new ones. Some clients
36 // (e.g., embedded devices) prefer generating persistent cache files for the
37 // specific device beforehand, and ship them as readonly files in OTA
38 // packages.
39 static bool gIsReadOnly;
40
42 static void ResetCacheForProcess();
43
44 // This must be called before |GetCacheForProcess|. Otherwise, it won't
45 // affect the cache directory returned by |GetCacheForProcess|.
46 static void SetCacheDirectoryPath(std::string path);
47
48 // Convert a binary SkData key into a Base32 encoded string.
49 //
50 // This is used to specify persistent cache filenames and service protocol
51 // json keys.
52 static std::string SkKeyToFilePath(const SkData& key);
53
54 // Allocate a MallocMapping containing the given key and value in the file
55 // format used by the cache.
56 static std::unique_ptr<fml::MallocMapping> BuildCacheObject(
57 const SkData& key,
58 const SkData& data);
59
60 // Header written into the files used to store cached Skia objects.
62 // A prefix used to identify the cache object file format.
63 static const uint32_t kSignature = 0xA869593F;
64 static const uint32_t kVersion1 = 1;
65
66 explicit CacheObjectHeader(uint32_t p_key_size) : key_size(p_key_size) {}
67
69 uint32_t version = kVersion1;
70 uint32_t key_size;
71 };
72
73 ~PersistentCache() override;
74
76
78
79 // Whether Skia tries to store any shader into this persistent cache after
80 // |ResetStoredNewShaders| is called. This flag is usually reset before each
81 // frame so we can know if Skia tries to compile new shaders in that frame.
82 bool StoredNewShaders() const { return stored_new_shaders_; }
83 void ResetStoredNewShaders() { stored_new_shaders_ = false; }
84 void DumpSkp(const SkData& data);
85 bool IsDumpingSkp() const { return is_dumping_skp_; }
86 void SetIsDumpingSkp(bool value) { is_dumping_skp_ = value; }
87
88 // Remove all files inside the persistent cache directory.
89 // Return whether the purge is successful.
90 bool Purge();
91
92 // |GrContextOptions::PersistentCache|
93 sk_sp<SkData> load(const SkData& key) override;
94
95 struct SkSLCache {
98 };
99
100 /// Load all the SkSL shader caches in the right directory.
101 std::vector<SkSLCache> LoadSkSLs() const;
102
103 //----------------------------------------------------------------------------
104 /// @brief Precompile SkSLs packaged with the application and gathered
105 /// during previous runs in the given context.
106 ///
107 /// @warning The context must be the rendering context. This context may be
108 /// destroyed during application suspension and subsequently
109 /// recreated. The SkSLs must be precompiled again in the new
110 /// context.
111 ///
112 /// @param context The rendering context to precompile shaders in.
113 ///
114 /// @return The number of SkSLs precompiled.
115 ///
116 size_t PrecompileKnownSkSLs(GrDirectContext* context) const;
117
118 // Return mappings for all skp's accessible through the AssetManager
119 std::vector<std::unique_ptr<fml::Mapping>> GetSkpsFromAssetManager() const;
120
121 /// Set the asset manager from which PersistentCache can load SkLSs. A nullptr
122 /// can be provided to clear the asset manager.
123 static void SetAssetManager(std::shared_ptr<AssetManager> value);
124 static std::shared_ptr<AssetManager> asset_manager() {
125 return asset_manager_;
126 }
127
128 static bool cache_sksl() { return cache_sksl_; }
129
130 static void SetCacheSkSL(bool value);
131
132 static void MarkStrategySet() { strategy_set_ = true; }
133
134 static constexpr char kSkSLSubdirName[] = "sksl";
135 static constexpr char kAssetFileName[] = "io.flutter.shaders.json";
136
137 private:
138 static std::string cache_base_path_;
139
140 static std::shared_ptr<AssetManager> asset_manager_;
141
142 static std::mutex instance_mutex_;
143 static std::unique_ptr<PersistentCache> gPersistentCache;
144
145 // Mutable static switch that can be set before GetCacheForProcess is called
146 // and GrContextOptions.fShaderCacheStrategy is set. If true, it means that
147 // we'll set `GrContextOptions::fShaderCacheStrategy` to `kSkSL`, and all the
148 // persistent cache should be stored and loaded from the "sksl" directory.
149 static std::atomic<bool> cache_sksl_;
150
151 // Guard flag to make sure that cache_sksl_ is not modified after
152 // strategy_set_ becomes true.
153 static std::atomic<bool> strategy_set_;
154
155 const bool is_read_only_;
156 const std::shared_ptr<fml::UniqueFD> cache_directory_;
157 const std::shared_ptr<fml::UniqueFD> sksl_cache_directory_;
158 mutable std::mutex worker_task_runners_mutex_;
159 std::multiset<fml::RefPtr<fml::TaskRunner>> worker_task_runners_;
160
161 bool stored_new_shaders_ = false;
162 bool is_dumping_skp_ = false;
163
164 static SkSLCache LoadFile(const fml::UniqueFD& dir,
165 const std::string& file_name,
166 bool need_key);
167
168 bool IsValid() const;
169
170 explicit PersistentCache(bool read_only = false);
171
172 // |GrContextOptions::PersistentCache|
173 void store(const SkData& key, const SkData& data) override;
174
175 fml::RefPtr<fml::TaskRunner> GetWorkerTaskRunner() const;
176
177 friend class testing::ShellTest;
178
180};
181
182} // namespace flutter
183
184#endif // !SLIMPELLER
185
186#endif // FLUTTER_COMMON_GRAPHICS_PERSISTENT_CACHE_H_
Definition: SkData.h:25
static constexpr char kSkSLSubdirName[]
static std::shared_ptr< AssetManager > asset_manager()
static void SetAssetManager(std::shared_ptr< AssetManager > value)
void RemoveWorkerTaskRunner(const fml::RefPtr< fml::TaskRunner > &task_runner)
std::vector< std::unique_ptr< fml::Mapping > > GetSkpsFromAssetManager() const
static void SetCacheDirectoryPath(std::string path)
sk_sp< SkData > load(const SkData &key) override
static PersistentCache * GetCacheForProcess()
void DumpSkp(const SkData &data)
static constexpr char kAssetFileName[]
static void SetCacheSkSL(bool value)
static void ResetCacheForProcess()
void SetIsDumpingSkp(bool value)
void AddWorkerTaskRunner(const fml::RefPtr< fml::TaskRunner > &task_runner)
std::vector< SkSLCache > LoadSkSLs() const
Load all the SkSL shader caches in the right directory.
size_t PrecompileKnownSkSLs(GrDirectContext *context) const
Precompile SkSLs packaged with the application and gathered during previous runs in the given context...
static std::string SkKeyToFilePath(const SkData &key)
static std::unique_ptr< fml::MallocMapping > BuildCacheObject(const SkData &key, const SkData &data)
uint8_t value
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace Enable an endless trace buffer The default is a ring buffer This is useful when very old events need to viewed For during application launch Memory usage will continue to grow indefinitely however Start app with an specific route defined on the framework flutter assets dir
Definition: switches.h:145