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/ResourceTypes.h" 17 18 struct SkSamplingOptions; 19 20 namespace skgpu { 21 class SingleOwner; 22 } 23 24 namespace SkSL { 25 class Compiler; 26 } 27 28 namespace skgpu::graphite { 29 30 class BackendTexture; 31 class Buffer; 32 class Caps; 33 class ComputePipeline; 34 class ComputePipelineDesc; 35 class GlobalCache; 36 class GraphicsPipeline; 37 class GraphicsPipelineDesc; 38 class GraphiteResourceKey; 39 class ResourceCache; 40 class RuntimeEffectDictionary; 41 class ShaderCodeDictionary; 42 class Sampler; 43 class SharedContext; 44 class Texture; 45 class TextureInfo; 46 47 class ResourceProvider { 48 public: 49 virtual ~ResourceProvider(); 50 51 // The runtime effect dictionary provides a link between SkCodeSnippetIds referenced in the 52 // paint key and the current SkRuntimeEffect that provides the SkSL for that id. 53 sk_sp<GraphicsPipeline> findOrCreateGraphicsPipeline(const RuntimeEffectDictionary*, 54 const GraphicsPipelineDesc&, 55 const RenderPassDesc&); 56 57 sk_sp<ComputePipeline> findOrCreateComputePipeline(const ComputePipelineDesc&); 58 59 sk_sp<Texture> findOrCreateScratchTexture(SkISize, const TextureInfo&, skgpu::Budgeted); 60 virtual sk_sp<Texture> createWrappedTexture(const BackendTexture&) = 0; 61 62 sk_sp<Texture> findOrCreateDepthStencilAttachment(SkISize dimensions, 63 const TextureInfo&); 64 65 sk_sp<Texture> findOrCreateDiscardableMSAAAttachment(SkISize dimensions, 66 const TextureInfo&); 67 68 sk_sp<Buffer> findOrCreateBuffer(size_t size, BufferType type, PrioritizeGpuReads); 69 70 sk_sp<Sampler> findOrCreateCompatibleSampler(const SkSamplingOptions&, 71 SkTileMode xTileMode, 72 SkTileMode yTileMode); 73 skslCompiler()74 SkSL::Compiler* skslCompiler() { return fCompiler.get(); } 75 76 BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&); 77 void deleteBackendTexture(BackendTexture&); 78 79 #if GRAPHITE_TEST_UTILS resourceCache()80 ResourceCache* resourceCache() { return fResourceCache.get(); } sharedContext()81 const SharedContext* sharedContext() { return fSharedContext; } 82 #endif 83 84 protected: 85 ResourceProvider(SharedContext* sharedContext, 86 SingleOwner* singleOwner); 87 88 SharedContext* fSharedContext; 89 90 private: 91 virtual sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*, 92 const GraphicsPipelineDesc&, 93 const RenderPassDesc&) = 0; 94 virtual sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) = 0; 95 virtual sk_sp<Texture> createTexture(SkISize, const TextureInfo&, skgpu::Budgeted) = 0; 96 virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, PrioritizeGpuReads) = 0; 97 98 virtual sk_sp<Sampler> createSampler(const SkSamplingOptions&, 99 SkTileMode xTileMode, 100 SkTileMode yTileMode) = 0; 101 102 sk_sp<Texture> findOrCreateTextureWithKey(SkISize dimensions, 103 const TextureInfo& info, 104 const GraphiteResourceKey& key, 105 skgpu::Budgeted); 106 107 virtual BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) = 0; 108 virtual void onDeleteBackendTexture(BackendTexture&) = 0; 109 110 // Each ResourceProvider owns one local cache; for some resources it also refers out to the 111 // global cache of the SharedContext, which is assumed to outlive the ResourceProvider. 112 sk_sp<ResourceCache> fResourceCache; 113 114 // Compiler used for compiling SkSL into backend shader code. We only want to create the 115 // compiler once, as there is significant overhead to the first compile. 116 std::unique_ptr<SkSL::Compiler> fCompiler; 117 }; 118 119 } // namespace skgpu::graphite 120 121 #endif // skgpu_graphite_ResourceProvider_DEFINED 122