Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 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 | |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 11 | #include "include/private/SkSpinlock.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/private/SkTArray.h" |
| 13 | #include "src/core/SkLRUCache.h" |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrProgramDesc.h" |
Jim Van Verth | 534c8c5 | 2021-03-10 10:48:32 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrThreadSafePipelineBuilder.h" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 16 | #include "src/gpu/mtl/GrMtlDepthStencil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/mtl/GrMtlPipelineStateBuilder.h" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 18 | #include "src/gpu/mtl/GrMtlSampler.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 19 | |
Aaron O'Mullan | 829b6a0 | 2019-07-08 01:31:14 +0200 | [diff] [blame] | 20 | #import <Metal/Metal.h> |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 21 | |
| 22 | class GrMtlGpu; |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 23 | class GrMtlCommandBuffer; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 24 | |
| 25 | class GrMtlResourceProvider { |
| 26 | public: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 27 | GrMtlResourceProvider(GrMtlGpu* gpu); |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 28 | |
Jim Van Verth | 534c8c5 | 2021-03-10 10:48:32 -0500 | [diff] [blame] | 29 | GrMtlPipelineState* findOrCreateCompatiblePipelineState( |
| 30 | const GrProgramDesc&,const GrProgramInfo&, |
| 31 | GrThreadSafePipelineBuilder::Stats::ProgramCacheResult* stat = nullptr); |
Jim Van Verth | 7d195ff | 2021-03-09 14:59:58 -0500 | [diff] [blame] | 32 | bool precompileShader(const SkData& key, const SkData& data); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 33 | |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 34 | // Finds or creates a compatible MTLDepthStencilState based on the GrStencilSettings. |
| 35 | GrMtlDepthStencil* findOrCreateCompatibleDepthStencilState(const GrStencilSettings&, |
| 36 | GrSurfaceOrigin); |
| 37 | |
| 38 | // Finds or creates a compatible MTLSamplerState based on the GrSamplerState. |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 39 | GrMtlSampler* findOrCreateCompatibleSampler(GrSamplerState); |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 40 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame] | 41 | // Destroy any cached resources. To be called before releasing the MtlDevice. |
| 42 | void destroyResources(); |
| 43 | |
John Stiles | 38b7d2f | 2020-06-24 12:13:31 -0400 | [diff] [blame] | 44 | #if GR_TEST_UTILS |
| 45 | void resetShaderCacheForTesting() const { fPipelineStateCache->release(); } |
| 46 | #endif |
| 47 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 48 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 49 | #ifdef SK_DEBUG |
| 50 | #define GR_PIPELINE_STATE_CACHE_STATS |
| 51 | #endif |
| 52 | |
Jim Van Verth | 534c8c5 | 2021-03-10 10:48:32 -0500 | [diff] [blame] | 53 | class PipelineStateCache : public GrThreadSafePipelineBuilder { |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 54 | public: |
| 55 | PipelineStateCache(GrMtlGpu* gpu); |
| 56 | ~PipelineStateCache(); |
| 57 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame] | 58 | void release(); |
Jim Van Verth | 534c8c5 | 2021-03-10 10:48:32 -0500 | [diff] [blame] | 59 | GrMtlPipelineState* refPipelineState(const GrProgramDesc&, const GrProgramInfo&, |
| 60 | Stats::ProgramCacheResult*); |
Jim Van Verth | 7d195ff | 2021-03-09 14:59:58 -0500 | [diff] [blame] | 61 | bool precompileShader(const SkData& key, const SkData& data); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 62 | |
| 63 | private: |
Jim Van Verth | 534c8c5 | 2021-03-10 10:48:32 -0500 | [diff] [blame] | 64 | GrMtlPipelineState* onRefPipelineState(const GrProgramDesc&, const GrProgramInfo&, |
| 65 | Stats::ProgramCacheResult*); |
| 66 | |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 67 | struct Entry; |
| 68 | |
| 69 | struct DescHash { |
| 70 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 71 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 72 | } |
| 73 | }; |
| 74 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 75 | SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 76 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 77 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 80 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 81 | |
| 82 | // Cache of GrMtlPipelineStates |
| 83 | std::unique_ptr<PipelineStateCache> fPipelineStateCache; |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 84 | |
| 85 | SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers; |
| 86 | SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | #endif |