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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/SkTArray.h" |
| 12 | #include "src/core/SkLRUCache.h" |
| 13 | #include "src/gpu/mtl/GrMtlCopyPipelineState.h" |
| 14 | #include "src/gpu/mtl/GrMtlPipelineStateBuilder.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 15 | |
| 16 | #import <metal/metal.h> |
| 17 | |
| 18 | class GrMtlGpu; |
| 19 | |
| 20 | class GrMtlResourceProvider { |
| 21 | public: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 22 | GrMtlResourceProvider(GrMtlGpu* gpu); |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 23 | |
| 24 | GrMtlCopyPipelineState* findOrCreateCopyPipelineState(MTLPixelFormat dstPixelFormat, |
| 25 | id<MTLFunction> vertexFunction, |
| 26 | id<MTLFunction> fragmentFunction, |
| 27 | MTLVertexDescriptor* vertexDescriptor); |
| 28 | |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 29 | GrMtlPipelineState* findOrCreateCompatiblePipelineState( |
| 30 | GrRenderTarget*, GrSurfaceOrigin, |
| 31 | const GrPipeline&, |
| 32 | const GrPrimitiveProcessor&, |
| 33 | const GrTextureProxy* const primProcProxies[], |
| 34 | GrPrimitiveType); |
| 35 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 36 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 37 | #ifdef SK_DEBUG |
| 38 | #define GR_PIPELINE_STATE_CACHE_STATS |
| 39 | #endif |
| 40 | |
| 41 | class PipelineStateCache : public ::SkNoncopyable { |
| 42 | public: |
| 43 | PipelineStateCache(GrMtlGpu* gpu); |
| 44 | ~PipelineStateCache(); |
| 45 | |
| 46 | GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin, |
| 47 | const GrPrimitiveProcessor&, |
| 48 | const GrTextureProxy* const primProcProxies[], |
| 49 | const GrPipeline&, |
| 50 | GrPrimitiveType); |
| 51 | |
| 52 | private: |
| 53 | enum { |
| 54 | // We may actually have kMaxEntries+1 PipelineStates in context because we create a new |
| 55 | // PipelineState before evicting from the cache. |
| 56 | kMaxEntries = 128, |
| 57 | }; |
| 58 | |
| 59 | struct Entry; |
| 60 | |
| 61 | struct DescHash { |
| 62 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 63 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap; |
| 68 | |
| 69 | GrMtlGpu* fGpu; |
| 70 | |
| 71 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 72 | int fTotalRequests; |
| 73 | int fCacheMisses; |
| 74 | #endif |
| 75 | }; |
| 76 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 77 | SkTArray<std::unique_ptr<GrMtlCopyPipelineState>> fCopyPipelineStateCache; |
| 78 | |
| 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; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | #endif |