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 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 28 | GrMtlPipelineState* findOrCreateCompatiblePipelineState(GrRenderTarget*, |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 29 | const GrProgramInfo&); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 30 | |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 31 | // Finds or creates a compatible MTLDepthStencilState based on the GrStencilSettings. |
| 32 | GrMtlDepthStencil* findOrCreateCompatibleDepthStencilState(const GrStencilSettings&, |
| 33 | GrSurfaceOrigin); |
| 34 | |
| 35 | // Finds or creates a compatible MTLSamplerState based on the GrSamplerState. |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame^] | 36 | GrMtlSampler* findOrCreateCompatibleSampler(GrSamplerState); |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 37 | |
Jim Van Verth | d44c0de | 2019-07-30 18:25:32 +0000 | [diff] [blame] | 38 | id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset); |
| 39 | void addBufferCompletionHandler(GrMtlCommandBuffer* cmdBuffer); |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -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 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 44 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 45 | #ifdef SK_DEBUG |
| 46 | #define GR_PIPELINE_STATE_CACHE_STATS |
| 47 | #endif |
| 48 | |
| 49 | class PipelineStateCache : public ::SkNoncopyable { |
| 50 | public: |
| 51 | PipelineStateCache(GrMtlGpu* gpu); |
| 52 | ~PipelineStateCache(); |
| 53 | |
Jim Van Verth | cf23f58 | 2019-05-22 09:46:57 -0400 | [diff] [blame] | 54 | void release(); |
Robert Phillips | fcaae48 | 2019-11-07 10:17:03 -0500 | [diff] [blame] | 55 | GrMtlPipelineState* refPipelineState(GrRenderTarget*, const GrProgramInfo&); |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 56 | |
| 57 | private: |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 58 | struct Entry; |
| 59 | |
| 60 | struct DescHash { |
| 61 | uint32_t operator()(const GrProgramDesc& desc) const { |
| 62 | return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0); |
| 63 | } |
| 64 | }; |
| 65 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 66 | SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 67 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 68 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 69 | |
| 70 | #ifdef GR_PIPELINE_STATE_CACHE_STATS |
| 71 | int fTotalRequests; |
| 72 | int fCacheMisses; |
| 73 | #endif |
| 74 | }; |
| 75 | |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 76 | // Buffer allocator |
| 77 | class BufferSuballocator : public SkRefCnt { |
| 78 | public: |
Jim Van Verth | d44c0de | 2019-07-30 18:25:32 +0000 | [diff] [blame] | 79 | BufferSuballocator(id<MTLDevice> device, size_t size); |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 80 | ~BufferSuballocator() { |
| 81 | fBuffer = nil; |
| 82 | fTotalSize = 0; |
| 83 | } |
| 84 | |
| 85 | id<MTLBuffer> getAllocation(size_t size, size_t* offset); |
| 86 | void addCompletionHandler(GrMtlCommandBuffer* cmdBuffer); |
| 87 | size_t size() { return fTotalSize; } |
| 88 | |
| 89 | private: |
| 90 | id<MTLBuffer> fBuffer; |
| 91 | size_t fTotalSize; |
Jim Van Verth | 5e8f389 | 2019-07-01 15:11:29 -0400 | [diff] [blame] | 92 | size_t fHead SK_GUARDED_BY(fMutex); // where we start allocating |
| 93 | size_t fTail SK_GUARDED_BY(fMutex); // where we start deallocating |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 94 | SkSpinlock fMutex; |
| 95 | }; |
Jim Van Verth | d44c0de | 2019-07-30 18:25:32 +0000 | [diff] [blame] | 96 | static constexpr size_t kBufferSuballocatorStartSize = 1024*1024; |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 97 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 98 | GrMtlGpu* fGpu; |
Jim Van Verth | 1223e7f | 2019-02-28 17:38:35 -0500 | [diff] [blame] | 99 | |
| 100 | // Cache of GrMtlPipelineStates |
| 101 | std::unique_ptr<PipelineStateCache> fPipelineStateCache; |
Jim Van Verth | 75c5326 | 2019-04-26 12:23:51 -0400 | [diff] [blame] | 102 | |
| 103 | SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers; |
| 104 | SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates; |
Jim Van Verth | 35a67eb | 2019-05-03 10:58:40 -0400 | [diff] [blame] | 105 | |
Jim Van Verth | d44c0de | 2019-07-30 18:25:32 +0000 | [diff] [blame] | 106 | // This is ref-counted because we might delete the GrContext before the command buffer |
| 107 | // finishes. The completion handler will retain a reference to this so it won't get |
Jim Van Verth | bbf85f9 | 2019-06-20 12:38:38 -0400 | [diff] [blame] | 108 | // deleted along with the GrContext. |
Jim Van Verth | d44c0de | 2019-07-30 18:25:32 +0000 | [diff] [blame] | 109 | sk_sp<BufferSuballocator> fBufferSuballocator; |
Jim Van Verth | 2209881 | 2019-08-07 11:22:13 -0400 | [diff] [blame] | 110 | size_t fBufferSuballocatorMaxSize; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | #endif |