blob: 2cc4102823db9f855fd61bcad458001a985fa4ad [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
Brian Osman5aa11fb2019-04-08 16:40:36 -04008#include "GrPersistentCacheUtils.h"
Brian Salomon00a5eb82018-07-11 15:32:05 -04009#include "MemoryCache.h"
10#include "SkBase64.h"
Brian Osman5aa11fb2019-04-08 16:40:36 -040011#include "SkMD5.h"
Brian Osmanbe2062c2019-04-15 09:26:13 -040012
13#if defined(SK_VULKAN)
14#include "vk/GrVkGpu.h"
15#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
55void 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 }
Brian Osman5aa11fb2019-04-08 16:40:36 -040060 fMap[Key(key)] = Value(data);
61}
62
63void MemoryCache::writeShadersToDisk(const char* path, GrBackendApi api) {
Brian Osmanbe2062c2019-04-15 09:26:13 -040064 if (GrBackendApi::kOpenGL != api && GrBackendApi::kVulkan != api) {
Brian Osman5aa11fb2019-04-08 16:40:36 -040065 return;
66 }
67
Brian Osman5aa11fb2019-04-08 16:40:36 -040068 for (auto it = fMap.begin(); it != fMap.end(); ++it) {
69 SkMD5 hash;
Brian Osmanbe2062c2019-04-15 09:26:13 -040070 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);
Brian Osman5aa11fb2019-04-08 16:40:36 -040087 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
Brian Osmanbe2062c2019-04-15 09:26:13 -040093 SkSL::Program::Inputs inputsIgnored[kGrShaderTypeCount];
94 SkSL::String shaders[kGrShaderTypeCount];
95 const SkData* data = it->second.fData.get();
96 const char* ext;
97 if (GrBackendApi::kOpenGL == api) {
98 ext = "frag";
99 GrPersistentCacheUtils::UnpackCachedGLSL(data, inputsIgnored, shaders);
100 } else if (GrBackendApi::kVulkan == api) {
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 ext = "spv";
104 GrPersistentCacheUtils::UnpackCachedSPIRV(data, shaders, inputsIgnored);
105 }
Brian Osman5aa11fb2019-04-08 16:40:36 -0400106
Brian Osmanbe2062c2019-04-15 09:26:13 -0400107 SkString filename = SkStringPrintf("%s/%s.%s", path, md5.c_str(), ext);
Brian Osman5aa11fb2019-04-08 16:40:36 -0400108 SkFILEWStream file(filename.c_str());
Brian Osmanbe2062c2019-04-15 09:26:13 -0400109 file.write(shaders[kFragment_GrShaderType].c_str(), shaders[kFragment_GrShaderType].size());
Brian Osman5aa11fb2019-04-08 16:40:36 -0400110 }
Brian Salomon00a5eb82018-07-11 15:32:05 -0400111}
112
113} // namespace sk_gpu_test