MemoryCache.cpp 3.9 KB

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