1 /* 2 * Copyright 2018 Google Inc. 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 GrMtlResourceProvider_DEFINED 9 #define GrMtlResourceProvider_DEFINED 10 11 #include "include/private/SkSpinlock.h" 12 #include "include/private/SkTArray.h" 13 #include "src/core/SkLRUCache.h" 14 #include "src/core/SkTDynamicHash.h" 15 #include "src/gpu/GrProgramDesc.h" 16 #include "src/gpu/GrThreadSafePipelineBuilder.h" 17 #include "src/gpu/mtl/GrMtlDepthStencil.h" 18 #include "src/gpu/mtl/GrMtlPipeline.h" 19 #include "src/gpu/mtl/GrMtlPipelineStateBuilder.h" 20 #include "src/gpu/mtl/GrMtlSampler.h" 21 22 #import <Metal/Metal.h> 23 24 class GrMtlGpu; 25 class GrMtlCommandBuffer; 26 27 class GrMtlResourceProvider { 28 public: 29 GrMtlResourceProvider(GrMtlGpu* gpu); 30 31 GrMtlPipelineState* findOrCreateCompatiblePipelineState( 32 const GrProgramDesc&,const GrProgramInfo&, 33 GrThreadSafePipelineBuilder::Stats::ProgramCacheResult* stat = nullptr); 34 bool precompileShader(const SkData& key, const SkData& data); 35 36 // Finds or creates a compatible MTLDepthStencilState based on the GrStencilSettings. 37 GrMtlDepthStencil* findOrCreateCompatibleDepthStencilState(const GrStencilSettings&, 38 GrSurfaceOrigin); 39 40 // Finds or creates a compatible MTLSamplerState based on the GrSamplerState. 41 GrMtlSampler* findOrCreateCompatibleSampler(GrSamplerState); 42 43 const GrMtlRenderPipeline* findOrCreateMSAALoadPipeline(MTLPixelFormat colorFormat, 44 int sampleCount, 45 MTLPixelFormat stencilFormat); 46 47 // Destroy any cached resources. To be called before releasing the MtlDevice. 48 void destroyResources(); 49 50 #if GR_TEST_UTILS resetShaderCacheForTesting()51 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); } 52 #endif 53 54 private: 55 #ifdef SK_DEBUG 56 #define GR_PIPELINE_STATE_CACHE_STATS 57 #endif 58 59 class PipelineStateCache : public GrThreadSafePipelineBuilder { 60 public: 61 PipelineStateCache(GrMtlGpu* gpu); 62 ~PipelineStateCache() override; 63 64 void release(); 65 GrMtlPipelineState* refPipelineState(const GrProgramDesc&, const GrProgramInfo&, 66 Stats::ProgramCacheResult*); 67 bool precompileShader(const SkData& key, const SkData& data); 68 69 private: 70 GrMtlPipelineState* onRefPipelineState(const GrProgramDesc&, const GrProgramInfo&, 71 Stats::ProgramCacheResult*); 72 73 struct Entry; 74 75 struct DescHash { operatorDescHash76 uint32_t operator()(const GrProgramDesc& desc) const { 77 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); 78 } 79 }; 80 81 SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap; 82 83 GrMtlGpu* fGpu; 84 }; 85 86 GrMtlGpu* fGpu; 87 88 // Cache of GrMtlPipelineStates 89 std::unique_ptr<PipelineStateCache> fPipelineStateCache; 90 91 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers; 92 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates; 93 94 struct MSAALoadPipelineEntry { 95 sk_sp<const GrMtlRenderPipeline> fPipeline; 96 MTLPixelFormat fColorFormat; 97 int fSampleCount; 98 MTLPixelFormat fStencilFormat; 99 }; 100 id<MTLLibrary> fMSAALoadLibrary; 101 SkTArray<MSAALoadPipelineEntry> fMSAALoadPipelines; 102 }; 103 104 #endif 105