| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 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 skgpu_graphite_GlobalCache_DEFINED |
| 9 |
#define skgpu_graphite_GlobalCache_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRefCnt.h" |
| 12 |
#include "include/gpu/graphite/ContextOptions.h" |
| 13 |
#include "include/private/base/SkTArray.h" |
| 14 |
#include "src/base/SkSpinlock.h" |
| 15 |
#include "src/core/SkLRUCache.h" |
| 16 |
#include "src/gpu/ResourceKey.h" |
| 17 |
#include "src/gpu/graphite/GraphicsPipeline.h" |
| 18 |
#include "src/gpu/graphite/Log.h" |
| 19 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 20 |
|
| 21 |
#include <functional> |
| 22 |
|
| 23 |
namespace skgpu::graphite { |
| 24 |
|
| 25 |
class Caps; |
| 26 |
class ComputePipeline; |
| 27 |
class GraphicsPipeline; |
| 28 |
class Resource; |
| 29 |
class ResourceProvider; |
| 30 |
class Sampler; |
| 31 |
class ShaderCodeDictionary; |
| 32 |
|
| 33 |
struct SamplerDesc; |
| 34 |
|
| 35 |
/** |
| 36 |
* GlobalCache holds GPU resources that should be shared by every Recorder. The common requirement |
| 37 |
* of these resources are they are static/read-only, have long lifetimes, and are likely to be used |
| 38 |
* by multiple Recorders. The canonical example of this are pipelines. |
| 39 |
* |
| 40 |
* GlobalCache is thread safe, but intentionally splits queries and storing operations so that they |
| 41 |
* are not atomic. The pattern is to query for a resource, which has a high likelihood of a cache |
| 42 |
* hit. If it's not found, the Recorder creates the resource on its own, without locking the |
| 43 |
* GlobalCache. After the resource is created, it is added to the GlobalCache, atomically returning |
| 44 |
* the winning Resource in the event of a race between Recorders for the same UniqueKey. |
| 45 |
*/ |
| 46 |
class GlobalCache { |
| 47 |
public: |
| 48 |
GlobalCache(); |
| 49 |
~GlobalCache(); |
| 50 |
|
| 51 |
void deleteResources(); |
| 52 |
|
| 53 |
// Find a cached GraphicsPipeline that matches the associated key. |
| 54 |
sk_sp<GraphicsPipeline> findGraphicsPipeline( |
| 55 |
const UniqueKey&, |
| 56 |
SkEnumBitMask<PipelineCreationFlags> = PipelineCreationFlags::kNone, |
| 57 |
uint32_t* compilationID = nullptr) SK_EXCLUDES(fSpinLock); |
| 58 |
|
| 59 |
// Associate the given pipeline with the key. If the key has already had a separate pipeline |
| 60 |
// associated with the key, that pipeline is returned and the passed-in pipeline is discarded. |
| 61 |
// Otherwise, the passed-in pipeline is held by the GlobalCache and also returned back. |
| 62 |
std::pair<sk_sp<GraphicsPipeline>, bool> addGraphicsPipeline( |
| 63 |
const UniqueKey&, sk_sp<GraphicsPipeline>) SK_EXCLUDES(fSpinLock); |
| 64 |
|
| 65 |
void purgePipelinesNotUsedSince( |
| 66 |
StdSteadyClock::time_point purgeTime) SK_EXCLUDES(fSpinLock); |
| 67 |
|
| 68 |
void reportPrecompileStats() SK_EXCLUDES(fSpinLock); |
| 69 |
void reportCacheStats() SK_EXCLUDES(fSpinLock); |
| 70 |
|
| 71 |
#if defined(GPU_TEST_UTILS) |
| 72 |
int numGraphicsPipelines() const SK_EXCLUDES(fSpinLock); |
| 73 |
void resetGraphicsPipelines() SK_EXCLUDES(fSpinLock); |
| 74 |
void forEachGraphicsPipeline( |
| 75 |
const std::function<void(const UniqueKey&, const GraphicsPipeline*)>& fn) |
| 76 |
SK_EXCLUDES(fSpinLock); |
| 77 |
uint16_t getEpoch() const SK_EXCLUDES(fSpinLock); |
| 78 |
void forceNextEpochOverflow() SK_EXCLUDES(fSpinLock); |
| 79 |
#endif |
| 80 |
|
| 81 |
struct PipelineStats { |
| 82 |
#if defined(GPU_TEST_UTILS) |
| 83 |
int fGraphicsCacheHits = 0; |
| 84 |
int fGraphicsCacheMisses = 0; |
| 85 |
int fGraphicsCacheAdditions = 0; |
| 86 |
int fGraphicsRaces = 0; |
| 87 |
int fGraphicsPurges = 0; |
| 88 |
#endif |
| 89 |
// Normally compiled Pipelines that were skipped bc of a preexisting Precompiled Pipeline |
| 90 |
uint32_t fNormalPreemptedByPrecompile = 0; |
| 91 |
// Precompiled Pipelines that made it into the cache |
| 92 |
uint32_t fUnpreemptedPrecompilePipelines = 0; |
| 93 |
// Precompiled Pipelines that were purged from the cache prior to use |
| 94 |
uint32_t fPurgedUnusedPrecompiledPipelines = 0; |
| 95 |
// The number of Pipelines requested since the last call to reportCacheStats |
| 96 |
uint32_t fPipelineUsesInEpoch = 0; |
| 97 |
}; |
| 98 |
|
| 99 |
PipelineStats getStats() const SK_EXCLUDES(fSpinLock); |
| 100 |
|
| 101 |
// Find and add operations for ComputePipelines, with the same pattern as GraphicsPipelines. |
| 102 |
sk_sp<ComputePipeline> findComputePipeline(const UniqueKey&) SK_EXCLUDES(fSpinLock); |
| 103 |
sk_sp<ComputePipeline> addComputePipeline(const UniqueKey&, |
| 104 |
sk_sp<ComputePipeline>) SK_EXCLUDES(fSpinLock); |
| 105 |
|
| 106 |
// The GlobalCache holds a ref on the given Resource until the cache is destroyed, keeping it |
| 107 |
// alive for the lifetime of the SharedContext. This should be used only for Resources that are |
| 108 |
// immutable after initialization so that anyone can use the resource without synchronization |
| 109 |
// or reference tracking. |
| 110 |
void addStaticResource(sk_sp<Resource>) SK_EXCLUDES(fSpinLock); |
| 111 |
|
| 112 |
// Note: we change the names here to better reflect the internal view of the Callbacks |
| 113 |
using PipelineCallbackContext = ContextOptions::PipelineCallbackContext; |
| 114 |
using PipelineCallback = ContextOptions::PipelineCachingCallback; |
| 115 |
using DeprecatedPipelineCallback = ContextOptions::PipelineCallback; |
| 116 |
|
| 117 |
void setPipelineCallback(PipelineCallbackContext, |
| 118 |
PipelineCallback, |
| 119 |
DeprecatedPipelineCallback); |
| 120 |
|
| 121 |
bool hasPipelineCallback() const { return fPipelineCallback || fDeprecatedPipelineCallback; } |
| 122 |
|
| 123 |
void invokePipelineCallback(ContextOptions::PipelineCacheOp, |
| 124 |
const GraphicsPipeline*, |
| 125 |
sk_sp<SkData> serializedKey = nullptr); |
| 126 |
|
| 127 |
// Returns a cached Sampler matching the sampler description, assuming that `desc` is a dynamic |
| 128 |
// sampler and does not have any immutable sampler information. The number of dynamic samplers |
| 129 |
// is small enough that they are created once during context initialization and then shared. |
| 130 |
// |
| 131 |
// The returned Sampler is kept alive by the GlobalCache's static resource collection for the |
| 132 |
// lifetime of the GlobalCache. |
| 133 |
const Sampler* getDynamicSampler(SamplerDesc desc) const SK_EXCLUDES(fSpinLock) { |
| 134 |
SkAutoSpinlock lock{fSpinLock}; |
| 135 |
SkASSERT(!desc.isImmutable() && desc.asSpan().size() == 1 && fDynamicSamplers[desc.desc()]); |
| 136 |
return fDynamicSamplers[desc.desc()]; |
| 137 |
} |
| 138 |
|
| 139 |
bool initializeDynamicSamplers(ResourceProvider*, const Caps*) SK_EXCLUDES(fSpinLock); |
| 140 |
|
| 141 |
#if defined(GPU_TEST_UTILS) |
| 142 |
struct StaticVertexCopyRanges { |
| 143 |
uint32_t fOffset; |
| 144 |
uint32_t fUnalignedSize; |
| 145 |
uint32_t fSize; |
| 146 |
uint32_t fRequiredAlignment; |
| 147 |
}; |
| 148 |
void testingOnly_SetStaticVertexInfo(skia_private::TArray<StaticVertexCopyRanges>, |
| 149 |
const Buffer*) SK_EXCLUDES(fSpinLock); |
| 150 |
SkSpan<const StaticVertexCopyRanges> getStaticVertexCopyRanges() const SK_EXCLUDES(fSpinLock); |
| 151 |
sk_sp<Buffer> getStaticVertexBuffer() SK_EXCLUDES(fSpinLock); |
| 152 |
#endif |
| 153 |
|
| 154 |
private: |
| 155 |
static constexpr int kNumDynamicSamplers = 1 << SamplerDesc::kImmutableSamplerInfoShift; |
| 156 |
|
| 157 |
// Remove the GraphicsPipeline from the cache, if possible. This does nothing if the pipeline |
| 158 |
// is not held in the cache. This removes based on actual pipeline object, not by key. When |
| 159 |
// pipeline compilation has transient failures, it is possible for multiple GraphicsPipelines to |
| 160 |
// be created that have the same key. |
| 161 |
void removeGraphicsPipeline(const GraphicsPipeline*) SK_REQUIRES(fSpinLock); |
| 162 |
|
| 163 |
struct KeyHash { |
| 164 |
uint32_t operator()(const UniqueKey& key) const { return key.hash(); } |
| 165 |
}; |
| 166 |
|
| 167 |
static void LogPurge(void* context, const UniqueKey& key, sk_sp<GraphicsPipeline>* p); |
| 168 |
struct PurgeCB { |
| 169 |
void operator()(void* context, const UniqueKey& k, sk_sp<GraphicsPipeline>* p) const { |
| 170 |
LogPurge(context, k, p); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
using GraphicsPipelineCache = SkLRUCache<UniqueKey, sk_sp<GraphicsPipeline>, KeyHash, PurgeCB>; |
| 175 |
using ComputePipelineCache = SkLRUCache<UniqueKey, sk_sp<ComputePipeline>, KeyHash>; |
| 176 |
|
| 177 |
// TODO: can we do something better given this should have write-seldom/read-often behavior? |
| 178 |
mutable SkSpinlock fSpinLock; |
| 179 |
|
| 180 |
// GraphicsPipelines and ComputePipelines are expensive to create, likely to be used by multiple |
| 181 |
// Recorders, and are ideally pre-compiled on process startup so thread write-contention is |
| 182 |
// expected to be low. For these reasons we store pipelines globally instead of per-Recorder. |
| 183 |
GraphicsPipelineCache fGraphicsPipelineCache SK_GUARDED_BY(fSpinLock); |
| 184 |
ComputePipelineCache fComputePipelineCache SK_GUARDED_BY(fSpinLock); |
| 185 |
|
| 186 |
skia_private::TArray<sk_sp<Resource>> fStaticResource SK_GUARDED_BY(fSpinLock); |
| 187 |
|
| 188 |
PipelineCallbackContext fPipelineCallbackContext = nullptr; |
| 189 |
|
| 190 |
PipelineCallback fPipelineCallback = nullptr; |
| 191 |
DeprecatedPipelineCallback fDeprecatedPipelineCallback = nullptr; |
| 192 |
|
| 193 |
PipelineStats fStats SK_GUARDED_BY(fSpinLock); |
| 194 |
|
| 195 |
// An epoch is the span of time between calls to PrecompileContext::reportPipelineStats. |
| 196 |
// Every Pipeline will be marked with the epoch in which it was created and then updated |
| 197 |
// for each epoch in which it was used. |
| 198 |
uint16_t fEpochCounter SK_GUARDED_BY(fSpinLock) = 1; |
| 199 |
|
| 200 |
// Lookup table for dynamically created samplers. Each sampler represents a specific combination |
| 201 |
// of tile modes and sampling options. The array is indexed by a bitmask generated from these |
| 202 |
// properties. The actual Sampler objects are owned by `fStaticResource`. |
| 203 |
std::array<const Sampler*, kNumDynamicSamplers> fDynamicSamplers SK_GUARDED_BY(fSpinLock); |
| 204 |
|
| 205 |
#if defined(GPU_TEST_UTILS) |
| 206 |
skia_private::TArray<StaticVertexCopyRanges> fStaticVertexInfo SK_GUARDED_BY(fSpinLock); |
| 207 |
const Buffer* fStaticVertexBuffer SK_GUARDED_BY(fSpinLock); |
| 208 |
#endif |
| 209 |
}; |
| 210 |
|
| 211 |
} // namespace skgpu::graphite |
| 212 |
|
| 213 |
#endif // skgpu_graphite_GlobalCache_DEFINED |