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