Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 "src/gpu/GrThreadSafeUniquelyKeyedProxyViewCache.h" |
| 9 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrResourceCache.h" |
| 11 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 12 | GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache() |
| 13 | : fFreeEntryList(nullptr) { |
| 14 | } |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 15 | |
| 16 | GrThreadSafeUniquelyKeyedProxyViewCache::~GrThreadSafeUniquelyKeyedProxyViewCache() { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 17 | this->dropAllRefs(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | #if GR_TEST_UTILS |
| 21 | int GrThreadSafeUniquelyKeyedProxyViewCache::numEntries() const { |
| 22 | SkAutoSpinlock lock{fSpinLock}; |
| 23 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 24 | return fUniquelyKeyedProxyViewMap.count(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 25 | } |
| 26 | |
Robert Phillips | c61c895 | 2020-09-22 14:24:43 -0400 | [diff] [blame] | 27 | size_t GrThreadSafeUniquelyKeyedProxyViewCache::approxBytesUsedForHash() const { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 28 | SkAutoSpinlock lock{fSpinLock}; |
| 29 | |
Robert Phillips | c61c895 | 2020-09-22 14:24:43 -0400 | [diff] [blame] | 30 | return fUniquelyKeyedProxyViewMap.approxBytesUsed(); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 31 | } |
| 32 | #endif |
| 33 | |
| 34 | void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllRefs() { |
| 35 | SkAutoSpinlock lock{fSpinLock}; |
| 36 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 37 | fUniquelyKeyedProxyViewMap.reset(); |
| 38 | while (auto tmp = fUniquelyKeyedProxyViewList.head()) { |
| 39 | fUniquelyKeyedProxyViewList.remove(tmp); |
| 40 | this->recycleEntry(tmp); |
| 41 | } |
| 42 | // TODO: should we empty out the fFreeEntryList and reset fEntryAllocator? |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Robert Phillips | 331699c | 2020-09-22 15:20:01 -0400 | [diff] [blame] | 45 | // TODO: add an atomic flag so we know when it is worthwhile to iterate |
| 46 | void GrThreadSafeUniquelyKeyedProxyViewCache::dropUniqueRefs(GrResourceCache* resourceCache) { |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 47 | SkAutoSpinlock lock{fSpinLock}; |
| 48 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 49 | // Iterate from LRU to MRU |
| 50 | Entry* cur = fUniquelyKeyedProxyViewList.tail(); |
| 51 | Entry* prev = cur ? cur->fPrev : nullptr; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 52 | |
| 53 | while (cur) { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 54 | if (resourceCache && !resourceCache->overBudget()) { |
| 55 | return; |
| 56 | } |
| 57 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 58 | if (cur->fView.proxy()->unique()) { |
| 59 | fUniquelyKeyedProxyViewMap.remove(cur->fKey); |
| 60 | fUniquelyKeyedProxyViewList.remove(cur); |
| 61 | this->recycleEntry(cur); |
| 62 | } |
| 63 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame] | 64 | cur = prev; |
| 65 | prev = cur ? cur->fPrev : nullptr; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 66 | } |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 69 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::find(const GrUniqueKey& key) { |
| 70 | SkAutoSpinlock lock{fSpinLock}; |
| 71 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 72 | Entry* tmp = fUniquelyKeyedProxyViewMap.find(key); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 73 | if (tmp) { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 74 | SkASSERT(fUniquelyKeyedProxyViewList.isInList(tmp)); |
| 75 | // make the sought out entry the MRU |
| 76 | fUniquelyKeyedProxyViewList.remove(tmp); |
| 77 | fUniquelyKeyedProxyViewList.addToHead(tmp); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 78 | return tmp->fView; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | return {}; |
| 82 | } |
| 83 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 84 | GrThreadSafeUniquelyKeyedProxyViewCache::Entry* |
| 85 | GrThreadSafeUniquelyKeyedProxyViewCache::getEntry(const GrUniqueKey& key, |
| 86 | const GrSurfaceProxyView& view) { |
| 87 | Entry* entry; |
| 88 | |
| 89 | if (fFreeEntryList) { |
| 90 | entry = fFreeEntryList; |
| 91 | fFreeEntryList = entry->fNext; |
| 92 | entry->fNext = nullptr; |
| 93 | |
| 94 | entry->fKey = key; |
| 95 | entry->fView = view; |
| 96 | } else { |
| 97 | entry = fEntryAllocator.make<Entry>(key, view); |
| 98 | } |
| 99 | |
| 100 | fUniquelyKeyedProxyViewList.addToHead(entry); // make 'entry' the MRU |
| 101 | fUniquelyKeyedProxyViewMap.add(entry); |
| 102 | return entry; |
| 103 | } |
| 104 | |
| 105 | void GrThreadSafeUniquelyKeyedProxyViewCache::recycleEntry(Entry* dead) { |
| 106 | SkASSERT(!dead->fPrev && !dead->fNext && !dead->fList); |
| 107 | |
| 108 | dead->fKey.reset(); |
| 109 | dead->fView.reset(); |
| 110 | |
| 111 | dead->fNext = fFreeEntryList; |
| 112 | fFreeEntryList = dead; |
| 113 | } |
| 114 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 115 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::internalAdd( |
| 116 | const GrUniqueKey& key, |
| 117 | const GrSurfaceProxyView& view) { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 118 | Entry* tmp = fUniquelyKeyedProxyViewMap.find(key); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 119 | if (!tmp) { |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 120 | tmp = this->getEntry(key, view); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 121 | |
| 122 | SkASSERT(fUniquelyKeyedProxyViewMap.find(key)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 125 | return tmp->fView; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::add(const GrUniqueKey& key, |
| 129 | const GrSurfaceProxyView& view) { |
| 130 | SkAutoSpinlock lock{fSpinLock}; |
| 131 | |
| 132 | return this->internalAdd(key, view); |
| 133 | } |