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> findOrCreateNonShareableTexture(SkISize, 65 const TextureInfo&, 66 std::string_view label, 67 Budgeted); 68 sk_sp<Texture> findOrCreateScratchTexture(SkISize, 69 const TextureInfo&, 70 std::string_view label, 71 const ResourceCache::ScratchResourceSet& unavailable); 72 73 sk_sp<Texture> createWrappedTexture(const BackendTexture&, std::string_view label); 74 75 sk_sp<Texture> findOrCreateDepthStencilAttachment(SkISize dimensions, 76 const TextureInfo&); 77 78 sk_sp<Texture> findOrCreateDiscardableMSAAAttachment(SkISize dimensions, 79 const TextureInfo&); 80 81 sk_sp<Buffer> findOrCreateBuffer(size_t size, 82 BufferType type, 83 AccessPattern, 84 std::string_view label); 85 86 sk_sp<Sampler> findOrCreateCompatibleSampler(const SamplerDesc&); 87 88 BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&); 89 void deleteBackendTexture(const BackendTexture&); 90 proxyCache()91 ProxyCache* proxyCache() { return fResourceCache->proxyCache(); } 92 setResourceCacheLimit(size_t bytes)93 void setResourceCacheLimit(size_t bytes) { return fResourceCache->setMaxBudget(bytes); } getResourceCacheLimit()94 size_t getResourceCacheLimit() const { return fResourceCache->getMaxBudget(); } getResourceCacheCurrentBudgetedBytes()95 size_t getResourceCacheCurrentBudgetedBytes() const { 96 return fResourceCache->currentBudgetedBytes(); 97 } getResourceCacheCurrentPurgeableBytes()98 size_t getResourceCacheCurrentPurgeableBytes() const { 99 return fResourceCache->currentPurgeableBytes(); 100 } 101 dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump)102 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { 103 fResourceCache->dumpMemoryStatistics(traceMemoryDump); 104 } 105 106 void freeGpuResources(); 107 void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime); forceProcessReturnedResources()108 void forceProcessReturnedResources() { fResourceCache->forceProcessReturnedResources(); } 109 110 #if defined(GPU_TEST_UTILS) resourceCache()111 ResourceCache* resourceCache() { return fResourceCache.get(); } sharedContext()112 const SharedContext* sharedContext() { return fSharedContext; } 113 #endif 114 115 #ifdef SK_BUILD_FOR_ANDROID 116 virtual BackendTexture createBackendTexture(AHardwareBuffer*, 117 bool isRenderable, 118 bool isProtectedContent, 119 SkISize dimensions, 120 bool fromAndroidWindow) const; 121 #endif 122 123 protected: 124 ResourceProvider(SharedContext* sharedContext, 125 SingleOwner* singleOwner, 126 uint32_t recorderID, 127 size_t resourceBudget); 128 129 SharedContext* fSharedContext; 130 // Each ResourceProvider owns one local cache; for some resources it also refers out to the 131 // global cache of the SharedContext, which is assumed to outlive the ResourceProvider. 132 sk_sp<ResourceCache> fResourceCache; 133 134 private: 135 virtual sk_sp<GraphicsPipeline> createGraphicsPipeline( 136 const RuntimeEffectDictionary*, 137 const UniqueKey&, 138 const GraphicsPipelineDesc&, 139 const RenderPassDesc&, 140 SkEnumBitMask<PipelineCreationFlags>, 141 uint32_t compilationID) = 0; 142 virtual sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) = 0; 143 virtual sk_sp<Texture> createTexture(SkISize, const TextureInfo&) = 0; 144 virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) = 0; 145 virtual sk_sp<Sampler> createSampler(const SamplerDesc&) = 0; 146 147 sk_sp<Texture> findOrCreateTexture(SkISize dimensions, 148 const TextureInfo& info, 149 std::string_view label, 150 Budgeted, 151 Shareable, 152 const ResourceCache::ScratchResourceSet* = nullptr); 153 154 virtual sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) = 0; 155 156 virtual BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) = 0; 157 #ifdef SK_BUILD_FOR_ANDROID 158 virtual BackendTexture onCreateBackendTexture(AHardwareBuffer*, 159 bool isRenderable, 160 bool isProtectedContent, 161 SkISize dimensions, 162 bool fromAndroidWindow) const; 163 #endif 164 virtual void onDeleteBackendTexture(const BackendTexture&) = 0; 165 onFreeGpuResources()166 virtual void onFreeGpuResources() {} onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime)167 virtual void onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) {} 168 }; 169 170 } // namespace skgpu::graphite 171 172 #endif // skgpu_graphite_ResourceProvider_DEFINED 173