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