Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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#include <memory>
9#include <mutex>
10#include <set>
11
12#include "flutter/assets/asset_manager.h"
13#include "flutter/fml/macros.h"
14#include "flutter/fml/task_runner.h"
15#include "flutter/fml/unique_fd.h"
17
18class GrDirectContext;
19
20namespace flutter {
21
22namespace testing {
23class ShellTest;
24}
25
26/// A cache of SkData that gets stored to disk.
27///
28/// This is mainly used for Shaders but is also written to by Dart. It is
29/// thread-safe for reading and writing from multiple threads.
31 public:
32 // Mutable static switch that can be set before GetCacheForProcess. If true,
33 // we'll only read existing caches but not generate new ones. Some clients
34 // (e.g., embedded devices) prefer generating persistent cache files for the
35 // specific device beforehand, and ship them as readonly files in OTA
36 // packages.
37 static bool gIsReadOnly;
38
40 static void ResetCacheForProcess();
41
42 // This must be called before |GetCacheForProcess|. Otherwise, it won't
43 // affect the cache directory returned by |GetCacheForProcess|.
44 static void SetCacheDirectoryPath(std::string path);
45
46 // Convert a binary SkData key into a Base32 encoded string.
47 //
48 // This is used to specify persistent cache filenames and service protocol
49 // json keys.
50 static std::string SkKeyToFilePath(const SkData& key);
51
52 // Allocate a MallocMapping containing the given key and value in the file
53 // format used by the cache.
54 static std::unique_ptr<fml::MallocMapping> BuildCacheObject(
55 const SkData& key,
56 const SkData& data);
57
58 // Header written into the files used to store cached Skia objects.
60 // A prefix used to identify the cache object file format.
61 static const uint32_t kSignature = 0xA869593F;
62 static const uint32_t kVersion1 = 1;
63
64 explicit CacheObjectHeader(uint32_t p_key_size) : key_size(p_key_size) {}
65
67 uint32_t version = kVersion1;
68 uint32_t key_size;
69 };
70
71 ~PersistentCache() override;
72
74
76
77 // Whether Skia tries to store any shader into this persistent cache after
78 // |ResetStoredNewShaders| is called. This flag is usually reset before each
79 // frame so we can know if Skia tries to compile new shaders in that frame.
80 bool StoredNewShaders() const { return stored_new_shaders_; }
81 void ResetStoredNewShaders() { stored_new_shaders_ = false; }
82 void DumpSkp(const SkData& data);
83 bool IsDumpingSkp() const { return is_dumping_skp_; }
84 void SetIsDumpingSkp(bool value) { is_dumping_skp_ = value; }
85
86 // Remove all files inside the persistent cache directory.
87 // Return whether the purge is successful.
88 bool Purge();
89
90 // |GrContextOptions::PersistentCache|
91 sk_sp<SkData> load(const SkData& key) override;
92
97
98 /// Load all the SkSL shader caches in the right directory.
99 std::vector<SkSLCache> LoadSkSLs() const;
100
101 //----------------------------------------------------------------------------
102 /// @brief Precompile SkSLs packaged with the application and gathered
103 /// during previous runs in the given context.
104 ///
105 /// @warning The context must be the rendering context. This context may be
106 /// destroyed during application suspension and subsequently
107 /// recreated. The SkSLs must be precompiled again in the new
108 /// context.
109 ///
110 /// @param context The rendering context to precompile shaders in.
111 ///
112 /// @return The number of SkSLs precompiled.
113 ///
114 size_t PrecompileKnownSkSLs(GrDirectContext* context) const;
115
116 // Return mappings for all skp's accessible through the AssetManager
117 std::vector<std::unique_ptr<fml::Mapping>> GetSkpsFromAssetManager() const;
118
119 /// Set the asset manager from which PersistentCache can load SkLSs. A nullptr
120 /// can be provided to clear the asset manager.
121 static void SetAssetManager(std::shared_ptr<AssetManager> value);
122 static std::shared_ptr<AssetManager> asset_manager() {
123 return asset_manager_;
124 }
125
126 static bool cache_sksl() { return cache_sksl_; }
127
128 static void SetCacheSkSL(bool value);
129
130 static void MarkStrategySet() { strategy_set_ = true; }
131
132 static constexpr char kSkSLSubdirName[] = "sksl";
133 static constexpr char kAssetFileName[] = "io.flutter.shaders.json";
134
135 private:
136 static std::string cache_base_path_;
137
138 static std::shared_ptr<AssetManager> asset_manager_;
139
140 static std::mutex instance_mutex_;
141 static std::unique_ptr<PersistentCache> gPersistentCache;
142
143 // Mutable static switch that can be set before GetCacheForProcess is called
144 // and GrContextOptions.fShaderCacheStrategy is set. If true, it means that
145 // we'll set `GrContextOptions::fShaderCacheStrategy` to `kSkSL`, and all the
146 // persistent cache should be stored and loaded from the "sksl" directory.
147 static std::atomic<bool> cache_sksl_;
148
149 // Guard flag to make sure that cache_sksl_ is not modified after
150 // strategy_set_ becomes true.
151 static std::atomic<bool> strategy_set_;
152
153 const bool is_read_only_;
154 const std::shared_ptr<fml::UniqueFD> cache_directory_;
155 const std::shared_ptr<fml::UniqueFD> sksl_cache_directory_;
156 mutable std::mutex worker_task_runners_mutex_;
157 std::multiset<fml::RefPtr<fml::TaskRunner>> worker_task_runners_;
158
159 bool stored_new_shaders_ = false;
160 bool is_dumping_skp_ = false;
161
162 static SkSLCache LoadFile(const fml::UniqueFD& dir,
163 const std::string& file_name,
164 bool need_key);
165
166 bool IsValid() const;
167
168 explicit PersistentCache(bool read_only = false);
169
170 // |GrContextOptions::PersistentCache|
171 void store(const SkData& key, const SkData& data) override;
172
173 fml::RefPtr<fml::TaskRunner> GetWorkerTaskRunner() const;
174
175 friend class testing::ShellTest;
176
178};
179
180} // namespace flutter
181
182#endif // FLUTTER_COMMON_GRAPHICS_PERSISTENT_CACHE_H_
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)
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.
void store(const SkData &key, const SkData &data) override
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