blob: 93ef2b14a4710c56f50d24b3cea2cc6a96d6f850 [file] [log] [blame]
Brian Salomon00a5eb82018-07-11 15:32:05 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/utils/SkBase64.h"
9#include "src/core/SkMD5.h"
10#include "src/gpu/GrPersistentCacheUtils.h"
11#include "tools/gpu/MemoryCache.h"
Brian Osmanbe2062c2019-04-15 09:26:13 -040012
13#if defined(SK_VULKAN)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/vk/GrVkGpu.h"
Brian Osmanbe2062c2019-04-15 09:26:13 -040015#endif
Brian Salomon00a5eb82018-07-11 15:32:05 -040016
17// Change this to 1 to log cache hits/misses/stores using SkDebugf.
18#define LOG_MEMORY_CACHE 0
19
20static 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
36namespace sk_gpu_test {
37
38sk_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(),
Brian Osman5aa11fb2019-04-08 16:40:36 -040049 data_to_str(*result->second.fData).c_str());
Brian Salomon00a5eb82018-07-11 15:32:05 -040050 }
Brian Osman5aa11fb2019-04-08 16:40:36 -040051 result->second.fHitCount++;
52 return result->second.fData;
Brian Salomon00a5eb82018-07-11 15:32:05 -040053}
54
Brian Osmanacf26502021-03-03 13:35:04 +000055void MemoryCache::store(const SkData& key, const SkData& data) {
Brian Salomon00a5eb82018-07-11 15:32:05 -040056 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 }
Brian Osman43f443f2020-06-05 11:11:36 -040060 ++fCacheStoreCnt;
Brian Osmanacf26502021-03-03 13:35:04 +000061 fMap[Key(key)] = Value(data);
Brian Osman5aa11fb2019-04-08 16:40:36 -040062}
63
64void MemoryCache::writeShadersToDisk(const char* path, GrBackendApi api) {
Brian Osmanbe2062c2019-04-15 09:26:13 -040065 if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) {
Brian Osman5aa11fb2019-04-08 16:40:36 -040066 return;
67 }
68
Brian Osman5aa11fb2019-04-08 16:40:36 -040069 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
70 SkMD5 hash;
Brian Osmanbe2062c2019-04-15 09:26:13 -040071 size_t bytesToHash = it->first.fKey->size();
72#if defined(SK_VULKAN)
73 if (GrBackendApi::kVulkan == api) {
74 // Vulkan stores two kinds of data in the cache (shaders and pipelines). The last four
75 // bytes of the key identify which one we have. We only want to extract shaders.
76 // Additionally, we don't want to hash the tag bytes, so we get the same keys as GL,
77 // which is good for cross-checking code generation and performance.
78 GrVkGpu::PersistentCacheKeyType vkKeyType;
79 SkASSERT(bytesToHash >= sizeof(vkKeyType));
80 bytesToHash -= sizeof(vkKeyType);
81 memcpy(&vkKeyType, it->first.fKey->bytes() + bytesToHash, sizeof(vkKeyType));
82 if (vkKeyType != GrVkGpu::kShader_PersistentCacheKeyType) {
83 continue;
84 }
85 }
86#endif
87 hash.write(it->first.fKey->bytes(), bytesToHash);
Brian Osman5aa11fb2019-04-08 16:40:36 -040088 SkMD5::Digest digest = hash.finish();
89 SkString md5;
90 for (int i = 0; i < 16; ++i) {
91 md5.appendf("%02x", digest.data[i]);
92 }
93
Brian Osmanbe2062c2019-04-15 09:26:13 -040094 SkSL::Program::Inputs inputsIgnored[kGrShaderTypeCount];
95 SkSL::String shaders[kGrShaderTypeCount];
96 const SkData* data = it->second.fData.get();
Brian Osmana085a412019-04-25 09:44:43 -040097 // Even with the SPIR-V switches, it seems like we must use .spv, or malisc tries to
98 // run glslang on the input.
99 const char* ext = GrBackendApi::kOpenGL == api ? "frag" : "spv";
Brian Osman9e4e4c72020-06-10 07:19:34 -0400100 SkReadBuffer reader(data->data(), data->size());
Brian Osman1facd5e2020-03-16 16:21:24 -0400101 GrPersistentCacheUtils::GetType(&reader); // Shader type tag
Brian Osmana66081d2019-09-03 14:59:26 -0400102 GrPersistentCacheUtils::UnpackCachedShaders(&reader, shaders,
Brian Osmana085a412019-04-25 09:44:43 -0400103 inputsIgnored, kGrShaderTypeCount);
Brian Osman5aa11fb2019-04-08 16:40:36 -0400104
Brian Osmanbe2062c2019-04-15 09:26:13 -0400105 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
Brian Osman5aa11fb2019-04-08 16:40:36 -0400106 SkFILEWStream file(filename.c_str());
Brian Osmanbe2062c2019-04-15 09:26:13 -0400107 file.write(shaders[kFragment_GrShaderType].c_str(), shaders[kFragment_GrShaderType].size());
Brian Osman5aa11fb2019-04-08 16:40:36 -0400108 }
Brian Salomon00a5eb82018-07-11 15:32:05 -0400109}
110
111} // namespace sk_gpu_test