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