Flutter Engine
The Flutter Engine
Classes | Public Member Functions | List of all members
sk_gpu_test::MemoryCache Class Reference

#include <MemoryCache.h>

Inheritance diagram for sk_gpu_test::MemoryCache:
GrContextOptions::PersistentCache

Public Member Functions

 MemoryCache ()=default
 
 MemoryCache (const MemoryCache &)=delete
 
MemoryCacheoperator= (const MemoryCache &)=delete
 
void reset ()
 
sk_sp< SkDataload (const SkData &key) override
 
void store (const SkData &key, const SkData &data, const SkString &description) override
 
int numCacheMisses () const
 
int numCacheStores () const
 
void resetCacheStats ()
 
void writeShadersToDisk (const char *path, GrBackendApi backend)
 
template<typename Fn >
void foreach (Fn &&fn)
 
- Public Member Functions inherited from GrContextOptions::PersistentCache
virtual ~PersistentCache ()=default
 
virtual sk_sp< SkDataload (const SkData &key)=0
 
virtual void store (const SkData &, const SkData &)
 
virtual void store (const SkData &key, const SkData &data, const SkString &)
 

Additional Inherited Members

- Protected Member Functions inherited from GrContextOptions::PersistentCache
 PersistentCache ()=default
 
 PersistentCache (const PersistentCache &)=delete
 
PersistentCacheoperator= (const PersistentCache &)=delete
 

Detailed Description

This class can be used to maintain an in memory record of all programs cached by GrContext. It can be shared by multiple GrContexts so long as those GrContexts are created with the same options and will have the same GrCaps (e.g. same backend, same GL context creation parameters, ...).

Definition at line 25 of file MemoryCache.h.

Constructor & Destructor Documentation

◆ MemoryCache() [1/2]

sk_gpu_test::MemoryCache::MemoryCache ( )
default

◆ MemoryCache() [2/2]

sk_gpu_test::MemoryCache::MemoryCache ( const MemoryCache )
delete

Member Function Documentation

◆ foreach()

template<typename Fn >
void sk_gpu_test::MemoryCache::foreach ( Fn &&  fn)
inline

Definition at line 47 of file MemoryCache.h.

47 {
48 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
49 fn(it->first.fKey, it->second.fData, it->second.fDescription, it->second.fHitCount);
50 }
51 }

◆ load()

sk_sp< SkData > sk_gpu_test::MemoryCache::load ( const SkData key)
overridevirtual

Returns the data for the key if it exists in the cache, otherwise returns null.

Implements GrContextOptions::PersistentCache.

Definition at line 39 of file MemoryCache.cpp.

39 {
40 auto result = fMap.find(key);
41 if (result == fMap.end()) {
42 if (LOG_MEMORY_CACHE) {
43 SkDebugf("Load Key: %s\n\tNot Found.\n\n", data_to_str(key).c_str());
44 }
45 ++fCacheMissCnt;
46 return nullptr;
47 }
48 if (LOG_MEMORY_CACHE) {
49 SkDebugf("Load Key: %s\n\tFound Data: %s\n\n", data_to_str(key).c_str(),
50 data_to_str(*result->second.fData).c_str());
51 }
52 result->second.fHitCount++;
53 return result->second.fData;
54}
static SkString data_to_str(const SkData &data)
Definition: MemoryCache.cpp:21
#define LOG_MEMORY_CACHE
Definition: MemoryCache.cpp:19
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
const char * c_str() const
Definition: SkString.h:133
GAsyncResult * result

◆ numCacheMisses()

int sk_gpu_test::MemoryCache::numCacheMisses ( ) const
inline

Definition at line 37 of file MemoryCache.h.

37{ return fCacheMissCnt; }

◆ numCacheStores()

int sk_gpu_test::MemoryCache::numCacheStores ( ) const
inline

Definition at line 38 of file MemoryCache.h.

38{ return fCacheStoreCnt; }

◆ operator=()

MemoryCache & sk_gpu_test::MemoryCache::operator= ( const MemoryCache )
delete

◆ reset()

void sk_gpu_test::MemoryCache::reset ( )
inline

Definition at line 30 of file MemoryCache.h.

30 {
31 this->resetCacheStats();
32 fMap.clear();
33 }

◆ resetCacheStats()

void sk_gpu_test::MemoryCache::resetCacheStats ( )
inline

Definition at line 39 of file MemoryCache.h.

39 {
40 fCacheMissCnt = 0;
41 fCacheStoreCnt = 0;
42 }

◆ store()

void sk_gpu_test::MemoryCache::store ( const SkData key,
const SkData data,
const SkString  
)
overridevirtual

Stores data in the cache, indexed by key. description provides a human-readable version of the key.

Reimplemented from GrContextOptions::PersistentCache.

Definition at line 56 of file MemoryCache.cpp.

56 {
57 if (LOG_MEMORY_CACHE) {
58 SkDebugf("Store Key: %s\n\tData: %s\n\n", data_to_str(key).c_str(),
59 data_to_str(data).c_str());
60 }
61 ++fCacheStoreCnt;
62 fMap[Key(key)] = Value(data, description);
63}
TArray< uint32_t > Key
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ writeShadersToDisk()

void sk_gpu_test::MemoryCache::writeShadersToDisk ( const char *  path,
GrBackendApi  backend 
)

Definition at line 65 of file MemoryCache.cpp.

65 {
66 if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) {
67 return;
68 }
69
70 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
71 SkMD5 hash;
72 size_t bytesToHash = it->first.fKey->size();
73#if defined(SK_VULKAN)
74 if (GrBackendApi::kVulkan == api) {
75 // Vulkan stores two kinds of data in the cache (shaders and pipelines). The last four
76 // bytes of the key identify which one we have. We only want to extract shaders.
77 // Additionally, we don't want to hash the tag bytes, so we get the same keys as GL,
78 // which is good for cross-checking code generation and performance.
80 SkASSERT(bytesToHash >= sizeof(vkKeyType));
81 bytesToHash -= sizeof(vkKeyType);
82 memcpy(&vkKeyType, it->first.fKey->bytes() + bytesToHash, sizeof(vkKeyType));
84 continue;
85 }
86 }
87#endif
88 hash.write(it->first.fKey->bytes(), bytesToHash);
89 SkMD5::Digest digest = hash.finish();
91
93 std::string shaders[kGrShaderTypeCount];
94 const SkData* data = it->second.fData.get();
95 const SkString& description = it->second.fDescription;
96 SkReadBuffer reader(data->data(), data->size());
97 GrPersistentCacheUtils::GetType(&reader); // Shader type tag
99 interfacesIgnored, kGrShaderTypeCount);
100
101 // Even with the SPIR-V switches, it seems like we must use .spv, or malisc tries to
102 // run glslang on the input.
103 {
104 const char* ext = GrBackendApi::kOpenGL == api ? "frag" : "frag.spv";
105 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
106 SkFILEWStream file(filename.c_str());
107 file.write(shaders[kFragment_GrShaderType].c_str(),
108 shaders[kFragment_GrShaderType].size());
109 }
110 {
111 const char* ext = GrBackendApi::kOpenGL == api ? "vert" : "vert.spv";
112 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
113 SkFILEWStream file(filename.c_str());
114 file.write(shaders[kVertex_GrShaderType].c_str(),
115 shaders[kVertex_GrShaderType].size());
116 }
117
118 if (!description.isEmpty()) {
119 const char* ext = "key";
120 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
121 SkFILEWStream file(filename.c_str());
122 file.write(description.c_str(), description.size());
123 }
124 }
125}
static SkMD5::Digest md5(const SkBitmap &bm)
Definition: CodecTest.cpp:77
@ kFragment_GrShaderType
Definition: GrTypesPriv.h:278
@ kVertex_GrShaderType
Definition: GrTypesPriv.h:277
static const int kGrShaderTypeCount
Definition: GrTypesPriv.h:282
#define SkASSERT(cond)
Definition: SkAssert.h:116
static uint32_t hash(const SkShaderBase::GradientInfo &v)
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
PersistentCacheKeyType
Definition: GrVkGpu.h:184
@ kShader_PersistentCacheKeyType
Definition: GrVkGpu.h:185
Definition: SkData.h:25
Definition: SkMD5.h:19
size_t size() const
Definition: SkString.h:131
bool isEmpty() const
Definition: SkString.h:130
bool UnpackCachedShaders(SkReadBuffer *reader, std::string shaders[], SkSL::Program::Interface interfaces[], int numInterfaces, ShaderMetadata *meta)
SkFourByteTag GetType(SkReadBuffer *reader)
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
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SkString toLowercaseHexString() const
Definition: SkMD5.cpp:116

The documentation for this class was generated from the following files: