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 | |
| 10 | GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache() {} |
| 11 | |
| 12 | GrThreadSafeUniquelyKeyedProxyViewCache::~GrThreadSafeUniquelyKeyedProxyViewCache() { |
| 13 | fUniquelyKeyedProxyViews.foreach([](const GrUniqueKey&k, Entry** v) { delete *v; }); |
| 14 | } |
| 15 | |
| 16 | #if GR_TEST_UTILS |
| 17 | int GrThreadSafeUniquelyKeyedProxyViewCache::numEntries() const { |
| 18 | SkAutoSpinlock lock{fSpinLock}; |
| 19 | |
| 20 | return fUniquelyKeyedProxyViews.count(); |
| 21 | } |
| 22 | |
| 23 | size_t GrThreadSafeUniquelyKeyedProxyViewCache::approxBytesUsed() const { |
| 24 | SkAutoSpinlock lock{fSpinLock}; |
| 25 | |
| 26 | return fUniquelyKeyedProxyViews.approxBytesUsed(); |
| 27 | } |
| 28 | #endif |
| 29 | |
| 30 | void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllRefs() { |
| 31 | SkAutoSpinlock lock{fSpinLock}; |
| 32 | |
| 33 | fUniquelyKeyedProxyViews.reset(); |
| 34 | } |
| 35 | |
| 36 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::find(const GrUniqueKey& key) { |
| 37 | SkAutoSpinlock lock{fSpinLock}; |
| 38 | |
| 39 | Entry** tmp = fUniquelyKeyedProxyViews.find(const_cast<GrUniqueKey&>(key)); |
| 40 | if (tmp) { |
| 41 | return (*tmp)->fView; |
| 42 | } |
| 43 | |
| 44 | return {}; |
| 45 | } |
| 46 | |
| 47 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::internalAdd( |
| 48 | const GrUniqueKey& key, |
| 49 | const GrSurfaceProxyView& view) { |
| 50 | Entry** tmp = fUniquelyKeyedProxyViews.find(const_cast<GrUniqueKey&>(key)); |
| 51 | if (!tmp) { |
| 52 | // TODO: block allocate here? |
| 53 | Entry* newT = new Entry(key, view); |
| 54 | tmp = fUniquelyKeyedProxyViews.set(newT->fKey, newT); |
| 55 | } |
| 56 | |
| 57 | return (*tmp)->fView; |
| 58 | } |
| 59 | |
| 60 | GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::add(const GrUniqueKey& key, |
| 61 | const GrSurfaceProxyView& view) { |
| 62 | SkAutoSpinlock lock{fSpinLock}; |
| 63 | |
| 64 | return this->internalAdd(key, view); |
| 65 | } |