blob: fbae73d812a69219de9b63a2831b132236d56119 [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 Phillips752f7e12020-09-18 12:28:59 -040012#include "src/core/SkTDynamicHash.h"
Robert Phillips26f3aeb2020-09-16 10:57:32 -040013#include "src/gpu/GrSurfaceProxyView.h"
14
15// Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
16// between the direct context and all the DDL recording contexts. This thread-safe cache
17// allows this sharing.
18//
19// In operation, each thread will first check if the threaded cache possesses the required texture.
20//
21// If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then
22// attempt to add it to the cache. If another thread had added it in the interim, the losing thread
23// will discard its work and use the texture the winning thread had created.
24//
25// If the thread in possession of the direct context doesn't find the needed texture it should
26// add a place holder view and then queue up the draw calls to complete it. In this way the
27// gpu-thread has precedence over the recording threads.
28//
29// The invariants for this cache differ a bit from those of the proxy and resource caches.
30// For this cache:
31//
32// only this cache knows the unique key - neither the proxy nor backing resource should
33// be discoverable in any other cache by the unique key
34// if a backing resource resides in the resource cache then there should be an entry in this
35// cache
36// an entry in this cache, however, doesn't guarantee that there is a corresponding entry in
37// the resource cache - although the entry here should be able to generate that entry
38// (i.e., be a lazy proxy)
39class GrThreadSafeUniquelyKeyedProxyViewCache {
40public:
41 GrThreadSafeUniquelyKeyedProxyViewCache();
42 ~GrThreadSafeUniquelyKeyedProxyViewCache();
43
44#if GR_TEST_UTILS
45 int numEntries() const SK_EXCLUDES(fSpinLock);
Robert Phillips752f7e12020-09-18 12:28:59 -040046 int count() const SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040047#endif
48
Robert Phillips752f7e12020-09-18 12:28:59 -040049 void dropAllRefs() SK_EXCLUDES(fSpinLock);
50 void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040051
52 GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
53
54 GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
55
56private:
57 struct Entry {
58 Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
59
60 // Note: the unique key is stored here bc it is never attached to a proxy or a GrTexture
Robert Phillips752f7e12020-09-18 12:28:59 -040061 const GrUniqueKey fKey;
Robert Phillips26f3aeb2020-09-16 10:57:32 -040062 GrSurfaceProxyView fView;
Robert Phillips752f7e12020-09-18 12:28:59 -040063
64 // for SkTDynamicHash
65 static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
66 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
Robert Phillips26f3aeb2020-09-16 10:57:32 -040067 };
68
69 GrSurfaceProxyView internalAdd(const GrUniqueKey&,
70 const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
71
72 mutable SkSpinlock fSpinLock;
73
Robert Phillips752f7e12020-09-18 12:28:59 -040074 SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViews SK_GUARDED_BY(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040075};
76
77#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED