Flutter Engine
The Flutter Engine
Classes | Public Member Functions | List of all members
skgpu::graphite::GlobalCache Class Reference

#include <GlobalCache.h>

Public Member Functions

 GlobalCache ()
 
 ~GlobalCache ()
 
void deleteResources ()
 
sk_sp< GraphicsPipelinefindGraphicsPipeline (const UniqueKey &) SK_EXCLUDES(fSpinLock)
 
sk_sp< GraphicsPipelineaddGraphicsPipeline (const UniqueKey &, sk_sp< GraphicsPipeline >) SK_EXCLUDES(fSpinLock)
 
sk_sp< ComputePipelinefindComputePipeline (const UniqueKey &) SK_EXCLUDES(fSpinLock)
 
sk_sp< ComputePipelineaddComputePipeline (const UniqueKey &, sk_sp< ComputePipeline >) SK_EXCLUDES(fSpinLock)
 
void addStaticResource (sk_sp< Resource >) SK_EXCLUDES(fSpinLock)
 

Detailed Description

GlobalCache holds GPU resources that should be shared by every Recorder. The common requirement of these resources are they are static/read-only, have long lifetimes, and are likely to be used by multiple Recorders. The canonical example of this are pipelines.

GlobalCache is thread safe, but intentionally splits queries and storing operations so that they are not atomic. The pattern is to query for a resource, which has a high likelihood of a cache hit. If it's not found, the Recorder creates the resource on its own, without locking the GlobalCache. After the resource is created, it is added to the GlobalCache, atomically returning the winning Resource in the event of a race between Recorders for the same UniqueKey.

Definition at line 37 of file GlobalCache.h.

Constructor & Destructor Documentation

◆ GlobalCache()

skgpu::graphite::GlobalCache::GlobalCache ( )

Definition at line 17 of file GlobalCache.cpp.

18 : fGraphicsPipelineCache(256) // TODO: find a good value for these limits
19 , fComputePipelineCache(256) {}

◆ ~GlobalCache()

skgpu::graphite::GlobalCache::~GlobalCache ( )

Definition at line 21 of file GlobalCache.cpp.

21 {
22 // These should have been cleared out earlier by deleteResources().
23 SkDEBUGCODE(SkAutoSpinlock lock{ fSpinLock });
24 SkASSERT(fGraphicsPipelineCache.count() == 0);
25 SkASSERT(fComputePipelineCache.count() == 0);
26 SkASSERT(fStaticResource.size() == 0);
27}
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()

Member Function Documentation

◆ addComputePipeline()

sk_sp< ComputePipeline > skgpu::graphite::GlobalCache::addComputePipeline ( const UniqueKey key,
sk_sp< ComputePipeline pipeline 
)

Definition at line 87 of file GlobalCache.cpp.

88 {
89 SkAutoSpinlock lock{fSpinLock};
90 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
91 if (!entry) {
92 entry = fComputePipelineCache.insert(key, std::move(pipeline));
93 }
94 return *entry;
95}

◆ addGraphicsPipeline()

sk_sp< GraphicsPipeline > skgpu::graphite::GlobalCache::addGraphicsPipeline ( const UniqueKey key,
sk_sp< GraphicsPipeline pipeline 
)

Definition at line 44 of file GlobalCache.cpp.

45 {
46 SkAutoSpinlock lock{fSpinLock};
47
48 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
49 if (!entry) {
50 // No equivalent pipeline was stored in the cache between a previous call to
51 // findGraphicsPipeline() that returned null (triggering the pipeline creation) and this
52 // later adding to the cache.
53 entry = fGraphicsPipelineCache.insert(key, std::move(pipeline));
54 } // else there was a race creating the same pipeline and this thread lost, so return the winner
55 return *entry;
56}

◆ addStaticResource()

void skgpu::graphite::GlobalCache::addStaticResource ( sk_sp< Resource resource)

Definition at line 97 of file GlobalCache.cpp.

97 {
98 SkAutoSpinlock lock{fSpinLock};
99 fStaticResource.push_back(std::move(resource));
100}
static SkString resource(SkPDFResourceType type, int index)

◆ deleteResources()

void skgpu::graphite::GlobalCache::deleteResources ( )

Definition at line 29 of file GlobalCache.cpp.

29 {
30 SkAutoSpinlock lock{ fSpinLock };
31
32 fGraphicsPipelineCache.reset();
33 fComputePipelineCache.reset();
34 fStaticResource.clear();
35}

◆ findComputePipeline()

sk_sp< ComputePipeline > skgpu::graphite::GlobalCache::findComputePipeline ( const UniqueKey key)

Definition at line 81 of file GlobalCache.cpp.

81 {
82 SkAutoSpinlock lock{fSpinLock};
83 sk_sp<ComputePipeline>* entry = fComputePipelineCache.find(key);
84 return entry ? *entry : nullptr;
85}

◆ findGraphicsPipeline()

sk_sp< GraphicsPipeline > skgpu::graphite::GlobalCache::findGraphicsPipeline ( const UniqueKey key)

Definition at line 37 of file GlobalCache.cpp.

37 {
38 SkAutoSpinlock lock{fSpinLock};
39
40 sk_sp<GraphicsPipeline>* entry = fGraphicsPipelineCache.find(key);
41 return entry ? *entry : nullptr;
42}

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