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/ResourceCache.h" 17 #include "src/gpu/graphite/ResourceTypes.h" 18 19 struct AHardwareBuffer; 20 struct SkSamplingOptions; 21 class SkTraceMemoryDump; 22 23 namespace skgpu { 24 class SingleOwner; 25 } 26 27 namespace SkSL { 28 class Compiler; 29 } 30 31 namespace skgpu::graphite { 32 33 class BackendTexture; 34 class Buffer; 35 class Caps; 36 class ComputePipeline; 37 class ComputePipelineDesc; 38 class GlobalCache; 39 class GraphicsPipeline; 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(const RuntimeEffectDictionary*, 57 const GraphicsPipelineDesc&, 58 const RenderPassDesc&); 59 60 sk_sp<ComputePipeline> findOrCreateComputePipeline(const ComputePipelineDesc&); 61 62 sk_sp<Texture> findOrCreateScratchTexture(SkISize, 63 const TextureInfo&, 64 std::string_view label, 65 skgpu::Budgeted); 66 67 sk_sp<Texture> createWrappedTexture(const BackendTexture&, std::string_view label); 68 69 sk_sp<Texture> findOrCreateDepthStencilAttachment(SkISize dimensions, 70 const TextureInfo&); 71 72 sk_sp<Texture> findOrCreateDiscardableMSAAAttachment(SkISize dimensions, 73 const TextureInfo&); 74 75 sk_sp<Buffer> findOrCreateBuffer(size_t size, 76 BufferType type, 77 AccessPattern, 78 std::string_view label); 79 80 sk_sp<Sampler> findOrCreateCompatibleSampler(const SamplerDesc&); 81 82 BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&); 83 void deleteBackendTexture(const BackendTexture&); 84 proxyCache()85 ProxyCache* proxyCache() { return fResourceCache->proxyCache(); } 86 getResourceCacheLimit()87 size_t getResourceCacheLimit() const { return fResourceCache->getMaxBudget(); } getResourceCacheCurrentBudgetedBytes()88 size_t getResourceCacheCurrentBudgetedBytes() const { 89 return fResourceCache->currentBudgetedBytes(); 90 } 91 dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump)92 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { 93 fResourceCache->dumpMemoryStatistics(traceMemoryDump); 94 } 95 96 void freeGpuResources(); 97 void purgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime); 98 99 #if defined(GRAPHITE_TEST_UTILS) resourceCache()100 ResourceCache* resourceCache() { return fResourceCache.get(); } sharedContext()101 const SharedContext* sharedContext() { return fSharedContext; } 102 #endif 103 104 #ifdef SK_BUILD_FOR_ANDROID 105 virtual BackendTexture createBackendTexture(AHardwareBuffer*, 106 bool isRenderable, 107 bool isProtectedContent, 108 SkISize dimensions, 109 bool fromAndroidWindow) const; 110 #endif 111 112 protected: 113 ResourceProvider(SharedContext* sharedContext, 114 SingleOwner* singleOwner, 115 uint32_t recorderID, 116 size_t resourceBudget); 117 118 SharedContext* fSharedContext; 119 // Each ResourceProvider owns one local cache; for some resources it also refers out to the 120 // global cache of the SharedContext, which is assumed to outlive the ResourceProvider. 121 sk_sp<ResourceCache> fResourceCache; 122 123 private: 124 virtual sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*, 125 const GraphicsPipelineDesc&, 126 const RenderPassDesc&) = 0; 127 virtual sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) = 0; 128 virtual sk_sp<Texture> createTexture(SkISize, 129 const TextureInfo&, 130 skgpu::Budgeted) = 0; 131 virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) = 0; 132 virtual sk_sp<Sampler> createSampler(const SamplerDesc&) = 0; 133 134 sk_sp<Texture> findOrCreateTextureWithKey(SkISize dimensions, 135 const TextureInfo& info, 136 const GraphiteResourceKey& key, 137 std::string_view label, 138 skgpu::Budgeted); 139 140 virtual sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) = 0; 141 142 virtual BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) = 0; 143 #ifdef SK_BUILD_FOR_ANDROID 144 virtual BackendTexture onCreateBackendTexture(AHardwareBuffer*, 145 bool isRenderable, 146 bool isProtectedContent, 147 SkISize dimensions, 148 bool fromAndroidWindow) const; 149 #endif 150 virtual void onDeleteBackendTexture(const BackendTexture&) = 0; 151 }; 152 153 } // namespace skgpu::graphite 154 155 #endif // skgpu_graphite_ResourceProvider_DEFINED 156