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" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 14 | #include "src/gpu/mtl/GrMtlDepthStencil.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/mtl/GrMtlPipelineStateBuilder.h" |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 16 | #include "src/gpu/mtl/GrMtlSampler.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 17 | |
| 18 | #import <metal/metal.h> |
| 19 | |
| 20 | class GrMtlGpu; |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame^] | 21 | class GrMtlCommandBuffer; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 22 | |
| 23 | class GrMtlResourceProvider { |
| 24 | public: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 25 | GrMtlResourceProvider(GrMtlGpu* gpu); |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 26 | |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 27 | GrMtlPipelineState* findOrCreateCompatiblePipelineState( |
| 28 | GrRenderTarget*, GrSurfaceOrigin, |
| 29 | const GrPipeline&, |
| 30 | const GrPrimitiveProcessor&, |
| 31 | const GrTextureProxy* const primProcProxies[], |
| 32 | GrPrimitiveType); |
| 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. |
| 39 | GrMtlSampler* findOrCreateCompatibleSampler(const GrSamplerState&, uint32_t maxMipLevel); |
| 40 | |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 41 | id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset); |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame^] | 42 | void addBufferCompletionHandler(GrMtlCommandBuffer* cmdBuffer); |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 43 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame] | 44 | // Destroy any cached resources. To be called before releasing the MtlDevice. |
| 45 | void destroyResources(); |
| 46 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 47 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 48 | #ifdef SK_DEBUG |
| 49 | #define GR_PIPELINE_STATE_CACHE_STATS |
| 50 | #endif |
| 51 | |
| 52 | class PipelineStateCache : public ::SkNoncopyable { |
| 53 | public: |
| 54 | PipelineStateCache(GrMtlGpu* gpu); |
| 55 | ~PipelineStateCache(); |
| 56 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame] | 57 | void release(); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 58 | GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin, |
| 59 | const GrPrimitiveProcessor&, |
| 60 | const GrTextureProxy* const primProcProxies[], |
| 61 | const GrPipeline&, |
| 62 | GrPrimitiveType); |
| 63 | |
| 64 | private: |
| 65 | enum { |
| 66 | // We may actually have kMaxEntries+1 PipelineStates in context because we create a new |
| 67 | // PipelineState before evicting from the cache. |
| 68 | kMaxEntries = 128, |
| 69 | }; |
| 70 | |
| 71 | struct Entry; |
| 72 | |
| 73 | struct DescHash { |
| 74 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 75 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap; |
| 80 | |
| 81 | GrMtlGpu* fGpu; |
| 82 | |
| 83 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 84 | int fTotalRequests; |
| 85 | int fCacheMisses; |
| 86 | #endif |
| 87 | }; |
| 88 | |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame^] | 89 | // Buffer allocator |
| 90 | class BufferSuballocator : public SkRefCnt { |
| 91 | public: |
| 92 | BufferSuballocator(id<MTLDevice> device, size_t size); |
| 93 | ~BufferSuballocator() { |
| 94 | fBuffer = nil; |
| 95 | fTotalSize = 0; |
| 96 | } |
| 97 | |
| 98 | id<MTLBuffer> getAllocation(size_t size, size_t* offset); |
| 99 | void addCompletionHandler(GrMtlCommandBuffer* cmdBuffer); |
| 100 | size_t size() { return fTotalSize; } |
| 101 | |
| 102 | private: |
| 103 | id<MTLBuffer> fBuffer; |
| 104 | size_t fTotalSize; |
| 105 | size_t fHead; // where we start allocating |
| 106 | size_t fTail; // where we start deallocating |
| 107 | SkSpinlock fMutex; |
| 108 | }; |
| 109 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 110 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 111 | |
| 112 | // Cache of GrMtlPipelineStates |
| 113 | std::unique_ptr<PipelineStateCache> fPipelineStateCache; |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 114 | |
| 115 | SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers; |
| 116 | SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates; |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 117 | |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame^] | 118 | // This is ref-counted because we might delete the GrContext before the command buffer |
| 119 | // finishes. The completion handler will retain a reference to this so it won't get |
| 120 | // deleted along with the GrContext. |
| 121 | sk_sp<BufferSuballocator> fBufferSuballocator; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | #endif |