blob: cf19c8979937833940db38b4f165af8d29a65dac [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTArray.h"
12#include "src/core/SkLRUCache.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040013#include "src/gpu/mtl/GrMtlDepthStencil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040015#include "src/gpu/mtl/GrMtlSampler.h"
Timothy Liange30739a2018-07-31 10:51:17 -040016
17#import <metal/metal.h>
18
19class GrMtlGpu;
20
21class GrMtlResourceProvider {
22public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050023 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040024
Jim Van Verth1223e7f2019-02-28 17:38:35 -050025 GrMtlPipelineState* findOrCreateCompatiblePipelineState(
26 GrRenderTarget*, GrSurfaceOrigin,
27 const GrPipeline&,
28 const GrPrimitiveProcessor&,
29 const GrTextureProxy* const primProcProxies[],
30 GrPrimitiveType);
31
Jim Van Verth75c53262019-04-26 12:23:51 -040032 // 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.
37 GrMtlSampler* findOrCreateCompatibleSampler(const GrSamplerState&, uint32_t maxMipLevel);
38
Jim Van Verth35a67eb2019-05-03 10:58:40 -040039 id<MTLBuffer> getDynamicBuffer(size_t size, size_t* offset);
40
Jim Van Verthcf23f582019-05-22 09:46:57 -040041 // Destroy any cached resources. To be called before releasing the MtlDevice.
42 void destroyResources();
43
Timothy Liange30739a2018-07-31 10:51:17 -040044private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050045#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 Verthcf23f582019-05-22 09:46:57 -040054 void release();
Jim Van Verth1223e7f2019-02-28 17:38:35 -050055 GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin,
56 const GrPrimitiveProcessor&,
57 const GrTextureProxy* const primProcProxies[],
58 const GrPipeline&,
59 GrPrimitiveType);
60
61 private:
62 enum {
63 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
64 // PipelineState before evicting from the cache.
65 kMaxEntries = 128,
66 };
67
68 struct Entry;
69
70 struct DescHash {
71 uint32_t operator()(const GrProgramDesc& desc) const {
72 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
73 }
74 };
75
76 SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
77
78 GrMtlGpu* fGpu;
79
80#ifdef GR_PIPELINE_STATE_CACHE_STATS
81 int fTotalRequests;
82 int fCacheMisses;
83#endif
84 };
85
Timothy Liange30739a2018-07-31 10:51:17 -040086 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050087
88 // Cache of GrMtlPipelineStates
89 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Jim Van Verth75c53262019-04-26 12:23:51 -040090
91 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers;
92 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates;
Jim Van Verth35a67eb2019-05-03 10:58:40 -040093
94 // Buffer state
95 struct BufferState {
96 id<MTLBuffer> fAllocation;
97 size_t fAllocationSize;
98 size_t fNextOffset;
99 };
100 BufferState fBufferState;
Timothy Liange30739a2018-07-31 10:51:17 -0400101};
102
103#endif