blob: c1e88f867ece661be987c7a1e415545b98144e50 [file] [log] [blame]
Timothy Liange30739a2018-07-31 10:51:17 -04001/*
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 Verthbbf85f92019-06-20 12:38:38 -040011#include "include/private/SkSpinlock.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/SkTArray.h"
13#include "src/core/SkLRUCache.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040014#include "src/gpu/mtl/GrMtlDepthStencil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040016#include "src/gpu/mtl/GrMtlSampler.h"
Timothy Liange30739a2018-07-31 10:51:17 -040017
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020018#import <Metal/Metal.h>
Timothy Liange30739a2018-07-31 10:51:17 -040019
20class GrMtlGpu;
Jim Van Verthbbf85f92019-06-20 12:38:38 -040021class GrMtlCommandBuffer;
Timothy Liange30739a2018-07-31 10:51:17 -040022
23class GrMtlResourceProvider {
24public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050025 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040026
Jim Van Verth1223e7f2019-02-28 17:38:35 -050027 GrMtlPipelineState* findOrCreateCompatiblePipelineState(
Robert Phillips65a77752019-10-02 15:22:24 -040028 GrRenderTarget*, int numSamples, GrSurfaceOrigin,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050029 const GrPipeline&,
30 const GrPrimitiveProcessor&,
31 const GrTextureProxy* const primProcProxies[],
32 GrPrimitiveType);
33
Jim Van Verth75c53262019-04-26 12:23:51 -040034 // 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 Verthd44c0de2019-07-30 18:25:32 +000041 id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset);
42 void addBufferCompletionHandler(GrMtlCommandBuffer* cmdBuffer);
Jim Van Verth35a67eb2019-05-03 10:58:40 -040043
Jim Van Verthcf23f582019-05-22 09:46:57 -040044 // Destroy any cached resources. To be called before releasing the MtlDevice.
45 void destroyResources();
46
Timothy Liange30739a2018-07-31 10:51:17 -040047private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050048#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 Verthcf23f582019-05-22 09:46:57 -040057 void release();
Robert Phillips65a77752019-10-02 15:22:24 -040058 GrMtlPipelineState* refPipelineState(GrRenderTarget*, int numSamples, GrSurfaceOrigin,
Jim Van Verth1223e7f2019-02-28 17:38:35 -050059 const GrPrimitiveProcessor&,
60 const GrTextureProxy* const primProcProxies[],
61 const GrPipeline&,
62 GrPrimitiveType);
63
64 private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050065 struct Entry;
66
67 struct DescHash {
68 uint32_t operator()(const GrProgramDesc& desc) const {
69 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
70 }
71 };
72
73 SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
74
75 GrMtlGpu* fGpu;
76
77#ifdef GR_PIPELINE_STATE_CACHE_STATS
78 int fTotalRequests;
79 int fCacheMisses;
80#endif
81 };
82
Jim Van Verthbbf85f92019-06-20 12:38:38 -040083 // Buffer allocator
84 class BufferSuballocator : public SkRefCnt {
85 public:
Jim Van Verthd44c0de2019-07-30 18:25:32 +000086 BufferSuballocator(id<MTLDevice> device, size_t size);
Jim Van Verthbbf85f92019-06-20 12:38:38 -040087 ~BufferSuballocator() {
88 fBuffer = nil;
89 fTotalSize = 0;
90 }
91
92 id<MTLBuffer> getAllocation(size_t size, size_t* offset);
93 void addCompletionHandler(GrMtlCommandBuffer* cmdBuffer);
94 size_t size() { return fTotalSize; }
95
96 private:
97 id<MTLBuffer> fBuffer;
98 size_t fTotalSize;
Jim Van Verth5e8f3892019-07-01 15:11:29 -040099 size_t fHead SK_GUARDED_BY(fMutex); // where we start allocating
100 size_t fTail SK_GUARDED_BY(fMutex); // where we start deallocating
Jim Van Verthbbf85f92019-06-20 12:38:38 -0400101 SkSpinlock fMutex;
102 };
Jim Van Verthd44c0de2019-07-30 18:25:32 +0000103 static constexpr size_t kBufferSuballocatorStartSize = 1024*1024;
Jim Van Verthbbf85f92019-06-20 12:38:38 -0400104
Timothy Liange30739a2018-07-31 10:51:17 -0400105 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -0500106
107 // Cache of GrMtlPipelineStates
108 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Jim Van Verth75c53262019-04-26 12:23:51 -0400109
110 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers;
111 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates;
Jim Van Verth35a67eb2019-05-03 10:58:40 -0400112
Jim Van Verthd44c0de2019-07-30 18:25:32 +0000113 // This is ref-counted because we might delete the GrContext before the command buffer
114 // finishes. The completion handler will retain a reference to this so it won't get
Jim Van Verthbbf85f92019-06-20 12:38:38 -0400115 // deleted along with the GrContext.
Jim Van Verthd44c0de2019-07-30 18:25:32 +0000116 sk_sp<BufferSuballocator> fBufferSuballocator;
Jim Van Verth22098812019-08-07 11:22:13 -0400117 size_t fBufferSuballocatorMaxSize;
Timothy Liange30739a2018-07-31 10:51:17 -0400118};
119
120#endif