blob: ee02b7555dfe2e8b91404a4bcc521943d4c21d3e [file] [log] [blame]
Robert Phillips26f3aeb2020-09-16 10:57:32 -04001/*
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
10GrThreadSafeUniquelyKeyedProxyViewCache::GrThreadSafeUniquelyKeyedProxyViewCache() {}
11
12GrThreadSafeUniquelyKeyedProxyViewCache::~GrThreadSafeUniquelyKeyedProxyViewCache() {
13 fUniquelyKeyedProxyViews.foreach([](const GrUniqueKey&k, Entry** v) { delete *v; });
14}
15
16#if GR_TEST_UTILS
17int GrThreadSafeUniquelyKeyedProxyViewCache::numEntries() const {
18 SkAutoSpinlock lock{fSpinLock};
19
20 return fUniquelyKeyedProxyViews.count();
21}
22
23size_t GrThreadSafeUniquelyKeyedProxyViewCache::approxBytesUsed() const {
24 SkAutoSpinlock lock{fSpinLock};
25
26 return fUniquelyKeyedProxyViews.approxBytesUsed();
27}
28#endif
29
30void GrThreadSafeUniquelyKeyedProxyViewCache::dropAllRefs() {
31 SkAutoSpinlock lock{fSpinLock};
32
33 fUniquelyKeyedProxyViews.reset();
34}
35
36GrSurfaceProxyView 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
47GrSurfaceProxyView 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
60GrSurfaceProxyView GrThreadSafeUniquelyKeyedProxyViewCache::add(const GrUniqueKey& key,
61 const GrSurfaceProxyView& view) {
62 SkAutoSpinlock lock{fSpinLock};
63
64 return this->internalAdd(key, view);
65}