blob: e23a0a3572cf1836a7e50793f5d4a84b95717fc3 [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"
Robert Phillips03e4c952019-11-26 16:20:22 -050014#include "src/gpu/GrProgramDesc.h"
Jim Van Verth534c8c52021-03-10 10:48:32 -050015#include "src/gpu/GrThreadSafePipelineBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040016#include "src/gpu/mtl/GrMtlDepthStencil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Jim Van Verth75c53262019-04-26 12:23:51 -040018#include "src/gpu/mtl/GrMtlSampler.h"
Timothy Liange30739a2018-07-31 10:51:17 -040019
Aaron O'Mullan829b6a02019-07-08 01:31:14 +020020#import <Metal/Metal.h>
Timothy Liange30739a2018-07-31 10:51:17 -040021
22class GrMtlGpu;
Jim Van Verthbbf85f92019-06-20 12:38:38 -040023class GrMtlCommandBuffer;
Timothy Liange30739a2018-07-31 10:51:17 -040024
25class GrMtlResourceProvider {
26public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050027 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040028
Jim Van Verth534c8c52021-03-10 10:48:32 -050029 GrMtlPipelineState* findOrCreateCompatiblePipelineState(
30 const GrProgramDesc&,const GrProgramInfo&,
31 GrThreadSafePipelineBuilder::Stats::ProgramCacheResult* stat = nullptr);
Jim Van Verth7d195ff2021-03-09 14:59:58 -050032 bool precompileShader(const SkData& key, const SkData& data);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050033
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.
Brian Salomonccb61422020-01-09 10:46:36 -050039 GrMtlSampler* findOrCreateCompatibleSampler(GrSamplerState);
Jim Van Verth75c53262019-04-26 12:23:51 -040040
Jim Van Verthcf23f582019-05-22 09:46:57 -040041 // Destroy any cached resources. To be called before releasing the MtlDevice.
42 void destroyResources();
43
John Stiles38b7d2f2020-06-24 12:13:31 -040044#if GR_TEST_UTILS
45 void resetShaderCacheForTesting() const { fPipelineStateCache->release(); }
46#endif
47
Timothy Liange30739a2018-07-31 10:51:17 -040048private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050049#ifdef SK_DEBUG
50#define GR_PIPELINE_STATE_CACHE_STATS
51#endif
52
Jim Van Verth534c8c52021-03-10 10:48:32 -050053 class PipelineStateCache : public GrThreadSafePipelineBuilder {
Jim Van Verth1223e7f2019-02-28 17:38:35 -050054 public:
55 PipelineStateCache(GrMtlGpu* gpu);
56 ~PipelineStateCache();
57
Jim Van Verthcf23f582019-05-22 09:46:57 -040058 void release();
Jim Van Verth534c8c52021-03-10 10:48:32 -050059 GrMtlPipelineState* refPipelineState(const GrProgramDesc&, const GrProgramInfo&,
60 Stats::ProgramCacheResult*);
Jim Van Verth7d195ff2021-03-09 14:59:58 -050061 bool precompileShader(const SkData& key, const SkData& data);
Jim Van Verth1223e7f2019-02-28 17:38:35 -050062
63 private:
Jim Van Verth534c8c52021-03-10 10:48:32 -050064 GrMtlPipelineState* onRefPipelineState(const GrProgramDesc&, const GrProgramInfo&,
65 Stats::ProgramCacheResult*);
66
Jim Van Verth1223e7f2019-02-28 17:38:35 -050067 struct Entry;
68
69 struct DescHash {
70 uint32_t operator()(const GrProgramDesc& desc) const {
71 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
72 }
73 };
74
Robert Phillips03e4c952019-11-26 16:20:22 -050075 SkLRUCache<const GrProgramDesc, std::unique_ptr<Entry>, DescHash> fMap;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050076
Robert Phillips03e4c952019-11-26 16:20:22 -050077 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050078 };
79
Timothy Liange30739a2018-07-31 10:51:17 -040080 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050081
82 // Cache of GrMtlPipelineStates
83 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Jim Van Verth75c53262019-04-26 12:23:51 -040084
85 SkTDynamicHash<GrMtlSampler, GrMtlSampler::Key> fSamplers;
86 SkTDynamicHash<GrMtlDepthStencil, GrMtlDepthStencil::Key> fDepthStencilStates;
Timothy Liange30739a2018-07-31 10:51:17 -040087};
88
89#endif