blob: a82b13a347856528db75337e347cb27afc517f7b [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 Phillips45593682020-09-18 16:16:33 -040014#include "src/core/SkTInternalLList.h"
Robert Phillips26f3aeb2020-09-16 10:57:32 -040015#include "src/gpu/GrSurfaceProxyView.h"
16
17// Ganesh creates a lot of utility textures (e.g., blurred-rrect masks) that need to be shared
18// between the direct context and all the DDL recording contexts. This thread-safe cache
19// allows this sharing.
20//
21// In operation, each thread will first check if the threaded cache possesses the required texture.
22//
23// If a DDL thread doesn't find a needed texture it will go off and create it on the cpu and then
24// attempt to add it to the cache. If another thread had added it in the interim, the losing thread
25// will discard its work and use the texture the winning thread had created.
26//
27// If the thread in possession of the direct context doesn't find the needed texture it should
28// add a place holder view and then queue up the draw calls to complete it. In this way the
29// gpu-thread has precedence over the recording threads.
30//
31// The invariants for this cache differ a bit from those of the proxy and resource caches.
32// For this cache:
33//
34// only this cache knows the unique key - neither the proxy nor backing resource should
35// be discoverable in any other cache by the unique key
36// if a backing resource resides in the resource cache then there should be an entry in this
37// cache
38// an entry in this cache, however, doesn't guarantee that there is a corresponding entry in
39// the resource cache - although the entry here should be able to generate that entry
40// (i.e., be a lazy proxy)
41class GrThreadSafeUniquelyKeyedProxyViewCache {
42public:
43 GrThreadSafeUniquelyKeyedProxyViewCache();
44 ~GrThreadSafeUniquelyKeyedProxyViewCache();
45
46#if GR_TEST_UTILS
47 int numEntries() const SK_EXCLUDES(fSpinLock);
Robert Phillips752f7e12020-09-18 12:28:59 -040048 int count() const SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040049#endif
50
Robert Phillips752f7e12020-09-18 12:28:59 -040051 void dropAllRefs() SK_EXCLUDES(fSpinLock);
52 void dropAllUniqueRefs() SK_EXCLUDES(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040053
54 GrSurfaceProxyView find(const GrUniqueKey&) SK_EXCLUDES(fSpinLock);
55
56 GrSurfaceProxyView add(const GrUniqueKey&, const GrSurfaceProxyView&) SK_EXCLUDES(fSpinLock);
57
58private:
59 struct Entry {
60 Entry(const GrUniqueKey& key, const GrSurfaceProxyView& view) : fKey(key), fView(view) {}
61
62 // 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 -040063 GrUniqueKey fKey;
Robert Phillips26f3aeb2020-09-16 10:57:32 -040064 GrSurfaceProxyView fView;
Robert Phillips45593682020-09-18 16:16:33 -040065
66 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Entry);
Robert Phillips752f7e12020-09-18 12:28:59 -040067
68 // for SkTDynamicHash
69 static const GrUniqueKey& GetKey(const Entry& e) { return e.fKey; }
70 static uint32_t Hash(const GrUniqueKey& key) { return key.hash(); }
Robert Phillips26f3aeb2020-09-16 10:57:32 -040071 };
72
Robert Phillips45593682020-09-18 16:16:33 -040073 Entry* getEntry(const GrUniqueKey&, const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
74 void recycleEntry(Entry*) SK_REQUIRES(fSpinLock);
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040075
Robert Phillips26f3aeb2020-09-16 10:57:32 -040076 GrSurfaceProxyView internalAdd(const GrUniqueKey&,
77 const GrSurfaceProxyView&) SK_REQUIRES(fSpinLock);
78
79 mutable SkSpinlock fSpinLock;
80
Robert Phillips45593682020-09-18 16:16:33 -040081 SkTDynamicHash<Entry, GrUniqueKey> fUniquelyKeyedProxyViewMap SK_GUARDED_BY(fSpinLock);
82 // The head of this list is the MRU
83 SkTInternalLList<Entry> fUniquelyKeyedProxyViewList SK_GUARDED_BY(fSpinLock);
Robert Phillipsf3e2b3c2020-09-18 14:07:43 -040084
85 // TODO: empirically determine this from the skps
86 static const int kInitialArenaSize = 64 * sizeof(Entry);
87
88 char fStorage[kInitialArenaSize];
89 SkArenaAlloc fEntryAllocator{fStorage, kInitialArenaSize, kInitialArenaSize};
Robert Phillips45593682020-09-18 16:16:33 -040090 Entry* fFreeEntryList SK_GUARDED_BY(fSpinLock);
Robert Phillips26f3aeb2020-09-16 10:57:32 -040091};
92
93#endif // GrThreadSafeUniquelyKeyedProxyViewCache_DEFINED