1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/utils/SkBase64.h"
9 #include "src/core/SkMD5.h"
10 #include "src/gpu/GrPersistentCacheUtils.h"
11 #include "tools/gpu/MemoryCache.h"
12
13 #if defined(SK_VULKAN)
14 #include "src/gpu/vk/GrVkGpu.h"
15 #endif
16
17 // Change this to 1 to log cache hits/misses/stores using SkDebugf.
18 #define LOG_MEMORY_CACHE 0
19
data_to_str(const SkData & data)20 static SkString data_to_str(const SkData& data) {
21 size_t encodeLength = SkBase64::Encode(data.data(), data.size(), nullptr);
22 SkString str;
23 str.resize(encodeLength);
24 SkBase64::Encode(data.data(), data.size(), str.writable_str());
25 static constexpr size_t kMaxLength = 60;
26 static constexpr char kTail[] = "...";
27 static const size_t kTailLen = strlen(kTail);
28 bool overlength = encodeLength > kMaxLength;
29 if (overlength) {
30 str = SkString(str.c_str(), kMaxLength - kTailLen);
31 str.append(kTail);
32 }
33 return str;
34 }
35
36 namespace sk_gpu_test {
37
load(const SkData & key)38 sk_sp<SkData> MemoryCache::load(const SkData& key) {
39 auto result = fMap.find(key);
40 if (result == fMap.end()) {
41 if (LOG_MEMORY_CACHE) {
42 SkDebugf("Load Key: %s\n\tNot Found.\n\n", data_to_str(key).c_str());
43 }
44 ++fCacheMissCnt;
45 return nullptr;
46 }
47 if (LOG_MEMORY_CACHE) {
48 SkDebugf("Load Key: %s\n\tFound Data: %s\n\n", data_to_str(key).c_str(),
49 data_to_str(*result->second.fData).c_str());
50 }
51 result->second.fHitCount++;
52 return result->second.fData;
53 }
54
store(const SkData & key,const SkData & data)55 void MemoryCache::store(const SkData& key, const SkData& data) {
56 if (LOG_MEMORY_CACHE) {
57 SkDebugf("Store Key: %s\n\tData: %s\n\n", data_to_str(key).c_str(),
58 data_to_str(data).c_str());
59 }
60 fMap[Key(key)] = Value(data);
61 }
62
writeShadersToDisk(const char * path,GrBackendApi api)63 void MemoryCache::writeShadersToDisk(const char* path, GrBackendApi api) {
64 if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) {
65 return;
66 }
67
68 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
69 SkMD5 hash;
70 size_t bytesToHash = it->first.fKey->size();
71 #if defined(SK_VULKAN)
72 if (GrBackendApi::kVulkan == api) {
73 // Vulkan stores two kinds of data in the cache (shaders and pipelines). The last four
74 // bytes of the key identify which one we have. We only want to extract shaders.
75 // Additionally, we don't want to hash the tag bytes, so we get the same keys as GL,
76 // which is good for cross-checking code generation and performance.
77 GrVkGpu::PersistentCacheKeyType vkKeyType;
78 SkASSERT(bytesToHash >= sizeof(vkKeyType));
79 bytesToHash -= sizeof(vkKeyType);
80 memcpy(&vkKeyType, it->first.fKey->bytes() + bytesToHash, sizeof(vkKeyType));
81 if (vkKeyType != GrVkGpu::kShader_PersistentCacheKeyType) {
82 continue;
83 }
84 }
85 #endif
86 hash.write(it->first.fKey->bytes(), bytesToHash);
87 SkMD5::Digest digest = hash.finish();
88 SkString md5;
89 for (int i = 0; i < 16; ++i) {
90 md5.appendf("%02x", digest.data[i]);
91 }
92
93 SkSL::Program::Inputs inputsIgnored[kGrShaderTypeCount];
94 SkSL::String shaders[kGrShaderTypeCount];
95 const SkData* data = it->second.fData.get();
96 // Even with the SPIR-V switches, it seems like we must use .spv, or malisc tries to
97 // run glslang on the input.
98 const char* ext = GrBackendApi::kOpenGL == api ? "frag" : "spv";
99 GrPersistentCacheUtils::UnpackCachedShaders(data, shaders,
100 inputsIgnored, kGrShaderTypeCount);
101
102 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
103 SkFILEWStream file(filename.c_str());
104 file.write(shaders[kFragment_GrShaderType].c_str(), shaders[kFragment_GrShaderType].size());
105 }
106 }
107
108 } // namespace sk_gpu_test
109