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_ResourceProvider_DEFINED 9 #define skgpu_ResourceProvider_DEFINED 10 11 #include "experimental/graphite/src/CommandBuffer.h" 12 #include "experimental/graphite/src/GraphicsPipelineDesc.h" 13 #include "experimental/graphite/src/ResourceTypes.h" 14 #include "include/core/SkSize.h" 15 #include "src/core/SkLRUCache.h" 16 17 namespace skgpu { 18 19 class Buffer; 20 class Gpu; 21 class GraphicsPipeline; 22 class Texture; 23 class TextureInfo; 24 25 class ResourceProvider { 26 public: 27 virtual ~ResourceProvider(); 28 29 virtual sk_sp<CommandBuffer> createCommandBuffer() = 0; 30 31 sk_sp<GraphicsPipeline> findOrCreateGraphicsPipeline(const GraphicsPipelineDesc&); 32 33 sk_sp<Texture> findOrCreateTexture(SkISize, const TextureInfo&); 34 35 sk_sp<Buffer> findOrCreateBuffer(size_t size, BufferType type, PrioritizeGpuReads); 36 37 protected: 38 ResourceProvider(const Gpu* gpu); 39 40 const Gpu* fGpu; 41 42 private: 43 virtual sk_sp<GraphicsPipeline> onCreateGraphicsPipeline(const GraphicsPipelineDesc&) = 0; 44 virtual sk_sp<Texture> createTexture(SkISize, const TextureInfo&) = 0; 45 virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, PrioritizeGpuReads) = 0; 46 47 class GraphicsPipelineCache { 48 public: 49 GraphicsPipelineCache(ResourceProvider* resourceProvider); 50 ~GraphicsPipelineCache(); 51 52 void release(); 53 sk_sp<GraphicsPipeline> refPipeline(const GraphicsPipelineDesc&); 54 55 private: 56 struct Entry; 57 58 struct DescHash { operatorDescHash59 uint32_t operator()(const GraphicsPipelineDesc& desc) const { 60 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); 61 } 62 }; 63 64 SkLRUCache<const GraphicsPipelineDesc, std::unique_ptr<Entry>, DescHash> fMap; 65 66 ResourceProvider* fResourceProvider; 67 }; 68 69 // Cache of GraphicsPipelines 70 std::unique_ptr<GraphicsPipelineCache> fGraphicsPipelineCache; 71 }; 72 73 } // namespace skgpu 74 75 #endif // skgpu_ResourceProvider_DEFINED 76