• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
setResourceCacheLimit(size_t bytes)89     void setResourceCacheLimit(size_t bytes) { return fResourceCache->setMaxBudget(bytes); }
getResourceCacheLimit()90     size_t getResourceCacheLimit() const { return fResourceCache->getMaxBudget(); }
getResourceCacheCurrentBudgetedBytes()91     size_t getResourceCacheCurrentBudgetedBytes() const {
92         return fResourceCache->currentBudgetedBytes();
93     }
getResourceCacheCurrentPurgeableBytes()94     size_t getResourceCacheCurrentPurgeableBytes() const {
95         return fResourceCache->currentPurgeableBytes();
96     }
97 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump)98     void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
99         fResourceCache->dumpMemoryStatistics(traceMemoryDump);
100     }
101 
102     void freeGpuResources();
103     void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime);
104 
105 #if defined(GPU_TEST_UTILS)
resourceCache()106     ResourceCache* resourceCache() { return fResourceCache.get(); }
sharedContext()107     const SharedContext* sharedContext() { return fSharedContext; }
108 #endif
109 
110 #ifdef SK_BUILD_FOR_ANDROID
111     virtual BackendTexture createBackendTexture(AHardwareBuffer*,
112                                                 bool isRenderable,
113                                                 bool isProtectedContent,
114                                                 SkISize dimensions,
115                                                 bool fromAndroidWindow) const;
116 #endif
117 
118 protected:
119     ResourceProvider(SharedContext* sharedContext,
120                      SingleOwner* singleOwner,
121                      uint32_t recorderID,
122                      size_t resourceBudget);
123 
124     SharedContext* fSharedContext;
125     // Each ResourceProvider owns one local cache; for some resources it also refers out to the
126     // global cache of the SharedContext, which is assumed to outlive the ResourceProvider.
127     sk_sp<ResourceCache> fResourceCache;
128 
129 private:
130     virtual sk_sp<GraphicsPipeline> createGraphicsPipeline(
131             const RuntimeEffectDictionary*,
132             const UniqueKey&,
133             const GraphicsPipelineDesc&,
134             const RenderPassDesc&,
135             SkEnumBitMask<PipelineCreationFlags>,
136             uint32_t compilationID) = 0;
137     virtual sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) = 0;
138     virtual sk_sp<Texture> createTexture(SkISize,
139                                          const TextureInfo&,
140                                          skgpu::Budgeted) = 0;
141     virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) = 0;
142     virtual sk_sp<Sampler> createSampler(const SamplerDesc&) = 0;
143 
144     sk_sp<Texture> findOrCreateTextureWithKey(SkISize dimensions,
145                                               const TextureInfo& info,
146                                               const GraphiteResourceKey& key,
147                                               std::string_view label,
148                                               skgpu::Budgeted);
149 
150     virtual sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) = 0;
151 
152     virtual BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) = 0;
153 #ifdef SK_BUILD_FOR_ANDROID
154     virtual BackendTexture onCreateBackendTexture(AHardwareBuffer*,
155                                                   bool isRenderable,
156                                                   bool isProtectedContent,
157                                                   SkISize dimensions,
158                                                   bool fromAndroidWindow) const;
159 #endif
160     virtual void onDeleteBackendTexture(const BackendTexture&) = 0;
161 
onFreeGpuResources()162     virtual void onFreeGpuResources() {}
onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime)163     virtual void onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {}
164 };
165 
166 } // namespace skgpu::graphite
167 
168 #endif // skgpu_graphite_ResourceProvider_DEFINED
169