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 | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 27 | int GrThreadSafeUniquelyKeyedProxyViewCache::count() const { |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 28 | SkAutoSpinlock lock{fSpinLock}; |
| 29 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 30 | return fUniquelyKeyedProxyViewMap.count(); |
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 | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame^] | 45 | void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllUniqueRefs(GrResourceCache* resourceCache) { |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 46 | SkAutoSpinlock lock{fSpinLock}; |
| 47 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame^] | 48 | // Iterate from LRU to MRU |
| 49 | Entry* cur = fUniquelyKeyedProxyViewList.tail(); |
| 50 | Entry* prev = cur ? cur->fPrev : nullptr; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 51 | |
| 52 | while (cur) { |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame^] | 53 | if (resourceCache && !resourceCache->overBudget()) { |
| 54 | return; |
| 55 | } |
| 56 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 57 | if (cur->fView.proxy()->unique()) { |
| 58 | fUniquelyKeyedProxyViewMap.remove(cur->fKey); |
| 59 | fUniquelyKeyedProxyViewList.remove(cur); |
| 60 | this->recycleEntry(cur); |
| 61 | } |
| 62 | |
Robert Phillips | 187b04b | 2020-09-22 12:18:16 -0400 | [diff] [blame^] | 63 | cur = prev; |
| 64 | prev = cur ? cur->fPrev : nullptr; |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 65 | } |
Robert Phillips | 12d06a3 | 2020-09-16 12:31:34 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 68 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::find(const GrUniqueKey& key) { |
| 69 | SkAutoSpinlock lock{fSpinLock}; |
| 70 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 71 | Entry* tmp = fUniquelyKeyedProxyViewMap.find(key); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 72 | if (tmp) { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 73 | SkASSERT(fUniquelyKeyedProxyViewList.isInList(tmp)); |
| 74 | // make the sought out entry the MRU |
| 75 | fUniquelyKeyedProxyViewList.remove(tmp); |
| 76 | fUniquelyKeyedProxyViewList.addToHead(tmp); |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 77 | return tmp->fView; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return {}; |
| 81 | } |
| 82 | |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 83 | GrThreadSafeUniquelyKeyedProxyViewCache::Entry* |
| 84 | GrThreadSafeUniquelyKeyedProxyViewCache::getEntry(const GrUniqueKey& key, |
| 85 | const GrSurfaceProxyView& view) { |
| 86 | Entry* entry; |
| 87 | |
| 88 | if (fFreeEntryList) { |
| 89 | entry = fFreeEntryList; |
| 90 | fFreeEntryList = entry->fNext; |
| 91 | entry->fNext = nullptr; |
| 92 | |
| 93 | entry->fKey = key; |
| 94 | entry->fView = view; |
| 95 | } else { |
| 96 | entry = fEntryAllocator.make<Entry>(key, view); |
| 97 | } |
| 98 | |
| 99 | fUniquelyKeyedProxyViewList.addToHead(entry); // make 'entry' the MRU |
| 100 | fUniquelyKeyedProxyViewMap.add(entry); |
| 101 | return entry; |
| 102 | } |
| 103 | |
| 104 | void GrThreadSafeUniquelyKeyedProxyViewCache::recycleEntry(Entry* dead) { |
| 105 | SkASSERT(!dead->fPrev && !dead->fNext && !dead->fList); |
| 106 | |
| 107 | dead->fKey.reset(); |
| 108 | dead->fView.reset(); |
| 109 | |
| 110 | dead->fNext = fFreeEntryList; |
| 111 | fFreeEntryList = dead; |
| 112 | } |
| 113 | |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 114 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::internalAdd( |
| 115 | const GrUniqueKey& key, |
| 116 | const GrSurfaceProxyView& view) { |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 117 | Entry* tmp = fUniquelyKeyedProxyViewMap.find(key); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 118 | if (!tmp) { |
Robert Phillips | f3e2b3c | 2020-09-18 14:07:43 -0400 | [diff] [blame] | 119 | tmp = this->getEntry(key, view); |
Robert Phillips | 4559368 | 2020-09-18 16:16:33 -0400 | [diff] [blame] | 120 | |
| 121 | SkASSERT(fUniquelyKeyedProxyViewMap.find(key)); |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Robert Phillips | 752f7e1 | 2020-09-18 12:28:59 -0400 | [diff] [blame] | 124 | return tmp->fView; |
Robert Phillips | 26f3aeb | 2020-09-16 10:57:32 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::add(const GrUniqueKey& key, |
| 128 | const GrSurfaceProxyView& view) { |
| 129 | SkAutoSpinlock lock{fSpinLock}; |
| 130 | |
| 131 | return this->internalAdd(key, view); |
| 132 | } |