Brian Salomon | 00a5eb8 | 2018-07-11 15:32:05 -0400 | [diff] [blame] | 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 "MemoryCache.h" |
| 9 | #include "SkBase64.h" |
| 10 | |
| 11 | // Change this to 1 to log cache hits/misses/stores using SkDebugf. |
| 12 | #define LOG_MEMORY_CACHE 0 |
| 13 | |
| 14 | static SkString data_to_str(const SkData& data) { |
| 15 | size_t encodeLength = SkBase64::Encode(data.data(), data.size(), nullptr); |
| 16 | SkString str; |
| 17 | str.resize(encodeLength); |
| 18 | SkBase64::Encode(data.data(), data.size(), str.writable_str()); |
| 19 | static constexpr size_t kMaxLength = 60; |
| 20 | static constexpr char kTail[] = "..."; |
| 21 | static const size_t kTailLen = strlen(kTail); |
| 22 | bool overlength = encodeLength > kMaxLength; |
| 23 | if (overlength) { |
| 24 | str = SkString(str.c_str(), kMaxLength - kTailLen); |
| 25 | str.append(kTail); |
| 26 | } |
| 27 | return str; |
| 28 | } |
| 29 | |
| 30 | namespace sk_gpu_test { |
| 31 | |
| 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).c_str()); |
| 44 | } |
| 45 | return result->second; |
| 46 | } |
| 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)] = SkData::MakeWithCopy(data.data(), data.size()); |
| 54 | } |
| 55 | |
| 56 | } // namespace sk_gpu_test |