xref: /aosp_15_r20/external/skia/src/gpu/graphite/ResourceProvider.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 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_ResourceProvider_DEFINED
9 #define skgpu_graphite_ResourceProvider_DEFINED
10 
11 #include "include/core/SkSize.h"
12 #include "include/core/SkTileMode.h"
13 #include "src/core/SkLRUCache.h"
14 #include "src/gpu/ResourceKey.h"
15 #include "src/gpu/graphite/CommandBuffer.h"
16 #include "src/gpu/graphite/GraphicsPipeline.h"
17 #include "src/gpu/graphite/ResourceCache.h"
18 #include "src/gpu/graphite/ResourceTypes.h"
19 
20 struct AHardwareBuffer;
21 struct SkSamplingOptions;
22 class SkTraceMemoryDump;
23 
24 namespace skgpu {
25 class SingleOwner;
26 }
27 
28 namespace SkSL {
29     class Compiler;
30 }
31 
32 namespace skgpu::graphite {
33 
34 class BackendTexture;
35 class Buffer;
36 class Caps;
37 class ComputePipeline;
38 class ComputePipelineDesc;
39 class GlobalCache;
40 class GraphicsPipelineDesc;
41 class GraphiteResourceKey;
42 class ResourceCache;
43 class RuntimeEffectDictionary;
44 class ShaderCodeDictionary;
45 class Sampler;
46 class SharedContext;
47 class Texture;
48 class TextureInfo;
49 
50 class ResourceProvider {
51 public:
52     virtual ~ResourceProvider();
53 
54     // The runtime effect dictionary provides a link between SkCodeSnippetIds referenced in the
55     // paint key and the current SkRuntimeEffect that provides the SkSL for that id.
56     sk_sp<GraphicsPipeline> findOrCreateGraphicsPipeline(
57             const RuntimeEffectDictionary*,
58             const GraphicsPipelineDesc&,
59             const RenderPassDesc&,
60             SkEnumBitMask<PipelineCreationFlags> = PipelineCreationFlags::kNone);
61 
62     sk_sp<ComputePipeline> findOrCreateComputePipeline(const ComputePipelineDesc&);
63 
64     sk_sp<Texture> findOrCreateScratchTexture(SkISize,
65                                               const TextureInfo&,
66                                               std::string_view label,
67                                               skgpu::Budgeted);
68 
69     sk_sp<Texture> createWrappedTexture(const BackendTexture&, std::string_view label);
70 
71     sk_sp<Texture> findOrCreateDepthStencilAttachment(SkISize dimensions,
72                                                       const TextureInfo&);
73 
74     sk_sp<Texture> findOrCreateDiscardableMSAAAttachment(SkISize dimensions,
75                                                          const TextureInfo&);
76 
77     sk_sp<Buffer> findOrCreateBuffer(size_t size,
78                                      BufferType type,
79                                      AccessPattern,
80                                      std::string_view label);
81 
82     sk_sp<Sampler> findOrCreateCompatibleSampler(const SamplerDesc&);
83 
84     BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&);
85     void deleteBackendTexture(const BackendTexture&);
86 
proxyCache()87     ProxyCache* proxyCache() { return fResourceCache->proxyCache(); }
88 
getResourceCacheLimit()89     size_t getResourceCacheLimit() const { return fResourceCache->getMaxBudget(); }
getResourceCacheCurrentBudgetedBytes()90     size_t getResourceCacheCurrentBudgetedBytes() const {
91         return fResourceCache->currentBudgetedBytes();
92     }
getResourceCacheCurrentPurgeableBytes()93     size_t getResourceCacheCurrentPurgeableBytes() const {
94         return fResourceCache->currentPurgeableBytes();
95     }
96 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump)97     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
98         fResourceCache->dumpMemoryStatistics(traceMemoryDump);
99     }
100 
101     void freeGpuResources();
102     void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime);
103 
104 #if defined(GPU_TEST_UTILS)
resourceCache()105     ResourceCache* resourceCache() { return fResourceCache.get(); }
sharedContext()106     const SharedContext* sharedContext() { return fSharedContext; }
107 #endif
108 
109 #ifdef SK_BUILD_FOR_ANDROID
110     virtual BackendTexture createBackendTexture(AHardwareBuffer*,
111                                                 bool isRenderable,
112                                                 bool isProtectedContent,
113                                                 SkISize dimensions,
114                                                 bool fromAndroidWindow) const;
115 #endif
116 
117 protected:
118     ResourceProvider(SharedContext* sharedContext,
119                      SingleOwner* singleOwner,
120                      uint32_t recorderID,
121                      size_t resourceBudget);
122 
123     SharedContext* fSharedContext;
124     // Each ResourceProvider owns one local cache; for some resources it also refers out to the
125     // global cache of the SharedContext, which is assumed to outlive the ResourceProvider.
126     sk_sp<ResourceCache> fResourceCache;
127 
128 private:
129     virtual sk_sp<GraphicsPipeline> createGraphicsPipeline(
130             const RuntimeEffectDictionary*,
131             const GraphicsPipelineDesc&,
132             const RenderPassDesc&,
133             SkEnumBitMask<PipelineCreationFlags>) = 0;
134     virtual sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) = 0;
135     virtual sk_sp<Texture> createTexture(SkISize,
136                                          const TextureInfo&,
137                                          skgpu::Budgeted) = 0;
138     virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) = 0;
139     virtual sk_sp<Sampler> createSampler(const SamplerDesc&) = 0;
140 
141     sk_sp<Texture> findOrCreateTextureWithKey(SkISize dimensions,
142                                               const TextureInfo& info,
143                                               const GraphiteResourceKey& key,
144                                               std::string_view label,
145                                               skgpu::Budgeted);
146 
147     virtual sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) = 0;
148 
149     virtual BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) = 0;
150 #ifdef SK_BUILD_FOR_ANDROID
151     virtual BackendTexture onCreateBackendTexture(AHardwareBuffer*,
152                                                   bool isRenderable,
153                                                   bool isProtectedContent,
154                                                   SkISize dimensions,
155                                                   bool fromAndroidWindow) const;
156 #endif
157     virtual void onDeleteBackendTexture(const BackendTexture&) = 0;
158 
onFreeGpuResources()159     virtual void onFreeGpuResources() {}
onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime)160     virtual void onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {}
161 };
162 
163 } // namespace skgpu::graphite
164 
165 #endif // skgpu_graphite_ResourceProvider_DEFINED
166