blob: c457d099605c75f23d2e66a0b365a68d91a0758d [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#ifndef GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED
9#define GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED
10
11#include "include/private/SkSpinlock.h"
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040012#include "src/core/SkArenaAlloc.h"
Robert Phillips752f7e12020-09-18 12:28:59 -040013#include "src/core/SkTDynamicHash.h"
Robert Phillips26f3aeb2020-09-16 10:57:32 -040014#include "src/gpu/GrSurfaceProxyView.h"
15
16// Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
17// between the direct context and all the DDL recording contexts. This thread-safe cache
18// allows this sharing.
19//
20// In operation, each thread will first check if the threaded cache possesses the required texture.
21//
22// If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then
23// attempt to add it to the cache. If another thread had added it in the interim, the losing thread
24// will discard its work and use the texture the winning thread had created.
25//
26// If the thread in possession of the direct context doesn't find the needed texture it should
27// add a place holder view and then queue up the draw calls to complete it. In this way the
28// gpu-thread has precedence over the recording threads.
29//
30// The invariants for this cache differ a bit from those of the proxy and resource caches.
31// For this cache:
32//
33// only this cache knows the unique key - neither the proxy nor backing resource should
34// be discoverable in any other cache by the unique key
35// if a backing resource resides in the resource cache then there should be an entry in this
36// cache
37// an entry in this cache, however, doesn't guarantee that there is a corresponding entry in
38// the resource cache - although the entry here should be able to generate that entry
39// (i.e., be a lazy proxy)
40class GrThreadSafeUniquelyKeyedProxyViewCache {
41public:
42 GrThreadSafeUniquelyKeyedProxyViewCache();
43 ~GrThreadSafeUniquelyKeyedProxyViewCache();
44
45#if GR_TEST_UTILS
46 int numEntries() const SK_EXCLUDES(fSpinLock);
Robert Phillips752f7e12020-09-18 12:28:59 -040047 int count() const SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040048#endif
49
Robert Phillips752f7e12020-09-18 12:28:59 -040050 void dropAllRefs() SK_EXCLUDES(fSpinLock);
51 void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040052
53 GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
54
55 GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
56
57private:
58 struct Entry {
59 Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
60
61 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040062 GrUniqueKey fKey;
Robert Phillips26f3aeb2020-09-16 10:57:32 -040063 GrSurfaceProxyView fView;
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040064 Entry* fNext = nullptr;
Robert Phillips752f7e12020-09-18 12:28:59 -040065
66 // for SkTDynamicHash
67 static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
68 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
Robert Phillips26f3aeb2020-09-16 10:57:32 -040069 };
70
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040071 Entry* getEntry(const GrUniqueKey& key, const GrSurfaceProxyView& view) {
72 Entry* newEntry;
73 if (fFreeEntryList) {
74 newEntry = fFreeEntryList;
75 fFreeEntryList = newEntry->fNext;
76 newEntry->fNext = nullptr;
77
78 newEntry->fKey = key;
79 newEntry->fView = view;
80 } else {
81 newEntry = fEntryAllocator.make<Entry>(key, view);
82 }
83
84 return newEntry;
85 }
86
87 void recycleEntry(Entry* dead) {
88 dead->fKey.reset();
89 dead->fView.reset();
90 dead->fNext = fFreeEntryList;
91 fFreeEntryList = dead;
92 }
93
Robert Phillips26f3aeb2020-09-16 10:57:32 -040094 GrSurfaceProxyView internalAdd(const GrUniqueKey&,
95 const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
96
97 mutable SkSpinlock fSpinLock;
98
Robert Phillips752f7e12020-09-18 12:28:59 -040099 SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock);
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -0400100
101 // TODO: empirically determine this from the skps
102 static const int kInitialArenaSize = 64 * sizeof(Entry);
103
104 char fStorage[kInitialArenaSize];
105 SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize};
106 Entry* fFreeEntryList = nullptr;
Robert Phillips26f3aeb2020-09-16 10:57:32 -0400107};
108
109#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED