Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Classes | Public Member Functions | List of all members
skgpu::graphite::ScratchResourceManager Class Reference

#include <ScratchResourceManager.h>

Classes

class  PendingUseListener
 

Public Member Functions

 ScratchResourceManager (ResourceProvider *resourceProvider, std::unique_ptr< ProxyReadCountMap >)
 
 ~ScratchResourceManager ()
 
sk_sp< TexturegetScratchTexture (SkISize, const TextureInfo &, std::string_view label)
 
void returnTexture (sk_sp< Texture >)
 
void pushScope ()
 
void popScope ()
 
void notifyResourcesConsumed ()
 
void markResourceInUse (PendingUseListener *listener)
 
int pendingReadCount (const TextureProxy *proxy) const
 
bool removePendingRead (const TextureProxy *proxy)
 

Detailed Description

ScratchResourceManager helps coordinate the reuse of resources within a Recording that would not otherwise be returned from the ResourceProvider/Cache because the Recorder is holds usage refs on the resources and they are typically not Shareable.

A ScratchResourceManager maintains a pool of resources that have been handed out for some use case and then been explicitly returned by the original holder. It is up to the callers to return resources in an optimal manner (for best reuse) and not use them after they've been returned for a later task's use. To help callers manage when they can return resources, the manager maintains a stack that corresponds with the depth-first traversal of the tasks during prepareResources() and provides hooks to register listeners that are invoked when tasks read or sample resources.

Once all uninstantiated resources are assigned and prepareResources() succeeds, the ScratchResourceManager can be discarded. The reuse within a Recording's task graph is fixed at that point and remains valid even if the recording is replayed.

Definition at line 75 of file ScratchResourceManager.h.

Constructor & Destructor Documentation

◆ ScratchResourceManager()

skgpu::graphite::ScratchResourceManager::ScratchResourceManager ( ResourceProvider resourceProvider,
std::unique_ptr< ProxyReadCountMap proxyCounts 
)

Definition at line 17 of file ScratchResourceManager.cpp.

19 : fResourceProvider(resourceProvider)
20 , fProxyReadCounts(std::move(proxyCounts)) {
21 SkASSERT(resourceProvider);
22 SkASSERT(fProxyReadCounts);
23}
#define SkASSERT(cond)
Definition: SkAssert.h:116

◆ ~ScratchResourceManager()

skgpu::graphite::ScratchResourceManager::~ScratchResourceManager ( )
default

Member Function Documentation

◆ getScratchTexture()

sk_sp< Texture > skgpu::graphite::ScratchResourceManager::getScratchTexture ( SkISize  dimensions,
const TextureInfo info,
std::string_view  label 
)

Definition at line 27 of file ScratchResourceManager.cpp.

29 {
30 for (ScratchTexture& st : fScratchTextures) {
31 if (st.fAvailable &&
32 st.fTexture->dimensions() == dimensions &&
33 st.fTexture->textureInfo() == info) {
34 // An exact match, reuse it.
35 st.fAvailable = false;
36 return st.fTexture;
37 }
38 }
39
40 // No texture was available so go out to the resource provider, which will hopefully find a
41 // cached resource that was freed up from a previous recording (or create a new one, if not).
42 // TODO(b/339496039): Always start with a fixed label like "ScratchTexture" and then concatenate
43 // the proxy label that's passed in onto the texture's label, including when reusing a texture.
44 sk_sp<Texture> newScratchTexture = fResourceProvider->findOrCreateScratchTexture(
45 dimensions, info, std::move(label), Budgeted::kYes);
46 if (newScratchTexture) {
47 fScratchTextures.push_back({newScratchTexture, /*fAvailable=*/false});
48 }
49 return newScratchTexture;
50}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
sk_sp< Texture > findOrCreateScratchTexture(SkISize, const TextureInfo &, std::string_view label, skgpu::Budgeted)

◆ markResourceInUse()

void skgpu::graphite::ScratchResourceManager::markResourceInUse ( PendingUseListener listener)

Definition at line 102 of file ScratchResourceManager.cpp.

102 {
103 // Should only be called inside a scope
104 SkASSERT(!fListenerStack.empty());
105 fListenerStack.push_back(listener);
106}

◆ notifyResourcesConsumed()

void skgpu::graphite::ScratchResourceManager::notifyResourcesConsumed ( )

Definition at line 85 of file ScratchResourceManager.cpp.

85 {
86 // Should only be called inside a scope
87 SkASSERT(!fListenerStack.empty());
88
89 int n = 0;
90 while (PendingUseListener* listener = fListenerStack.fromBack(n)) {
91 listener->onUseCompleted(this);
92 n++;
93 }
94 SkASSERT(n < fListenerStack.size() && fListenerStack.fromBack(n) == nullptr);
95 // Remove all non-null listeners that were just invoked, but do not remove the null entry that
96 // marks the start of this scope boundary.
97 if (n > 0) {
98 fListenerStack.pop_back_n(n);
99 }
100}

◆ pendingReadCount()

int skgpu::graphite::ScratchResourceManager::pendingReadCount ( const TextureProxy proxy) const
inline

Definition at line 164 of file ScratchResourceManager.h.

164 {
165 return fProxyReadCounts->get(proxy);
166 }

◆ popScope()

void skgpu::graphite::ScratchResourceManager::popScope ( )

Definition at line 69 of file ScratchResourceManager.cpp.

69 {
70 // Must have at least the null element to start the scope being popped
71 SkASSERT(!fListenerStack.empty());
72
73 // TODO: Assert that the current sublist is empty (i.e. the back element is a null pointer) but
74 // for now skip over them and leave them un-invoked to keep the unconsumed scratch resources
75 // out of the pool so they remain valid in later recordings.
76 int n = 0;
77 while (fListenerStack.fromBack(n)) {
78 n++;
79 }
80 SkASSERT(n < fListenerStack.size() && fListenerStack.fromBack(n) == nullptr);
81 // Remove all non-null listeners after the most recent null entry AND the null entry
82 fListenerStack.pop_back_n(n + 1);
83}

◆ pushScope()

void skgpu::graphite::ScratchResourceManager::pushScope ( )

Definition at line 64 of file ScratchResourceManager.cpp.

64 {
65 // Push a null pointer to mark the beginning of the list of listeners in the next depth
66 fListenerStack.push_back(nullptr);
67}

◆ removePendingRead()

bool skgpu::graphite::ScratchResourceManager::removePendingRead ( const TextureProxy proxy)
inline

Definition at line 169 of file ScratchResourceManager.h.

169 {
170 return fProxyReadCounts->decrement(proxy);
171 }

◆ returnTexture()

void skgpu::graphite::ScratchResourceManager::returnTexture ( sk_sp< Texture texture)

Definition at line 52 of file ScratchResourceManager.cpp.

52 {
53 for (ScratchTexture& st : fScratchTextures) {
54 if (st.fTexture.get() == texture.get()) {
55 SkASSERT(!st.fAvailable);
56 st.fAvailable = true;
57 return;
58 }
59 }
60 // Trying to return a resource that didn't come from getScratchTexture().
61 SkASSERT(false);
62}
FlTexture * texture

The documentation for this class was generated from the following files: