blob: 312e6795505baaf3917e3db7738a10123ca0a33f [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"
13#include "src/gpu/mtl/GrMtlCopyPipelineState.h"
14#include "src/gpu/mtl/GrMtlPipelineStateBuilder.h"
Timothy Liange30739a2018-07-31 10:51:17 -040015
16#import <metal/metal.h>
17
18class GrMtlGpu;
19
20class GrMtlResourceProvider {
21public:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050022 GrMtlResourceProvider(GrMtlGpu* gpu);
Timothy Liange30739a2018-07-31 10:51:17 -040023
24 GrMtlCopyPipelineState* findOrCreateCopyPipelineState(MTLPixelFormat dstPixelFormat,
25 id<MTLFunction> vertexFunction,
26 id<MTLFunction> fragmentFunction,
27 MTLVertexDescriptor* vertexDescriptor);
28
Jim Van Verth1223e7f2019-02-28 17:38:35 -050029 GrMtlPipelineState* findOrCreateCompatiblePipelineState(
30 GrRenderTarget*, GrSurfaceOrigin,
31 const GrPipeline&,
32 const GrPrimitiveProcessor&,
33 const GrTextureProxy* const primProcProxies[],
34 GrPrimitiveType);
35
Timothy Liange30739a2018-07-31 10:51:17 -040036private:
Jim Van Verth1223e7f2019-02-28 17:38:35 -050037#ifdef SK_DEBUG
38#define GR_PIPELINE_STATE_CACHE_STATS
39#endif
40
41 class PipelineStateCache : public ::SkNoncopyable {
42 public:
43 PipelineStateCache(GrMtlGpu* gpu);
44 ~PipelineStateCache();
45
46 GrMtlPipelineState* refPipelineState(GrRenderTarget*, GrSurfaceOrigin,
47 const GrPrimitiveProcessor&,
48 const GrTextureProxy* const primProcProxies[],
49 const GrPipeline&,
50 GrPrimitiveType);
51
52 private:
53 enum {
54 // We may actually have kMaxEntries+1 PipelineStates in context because we create a new
55 // PipelineState before evicting from the cache.
56 kMaxEntries = 128,
57 };
58
59 struct Entry;
60
61 struct DescHash {
62 uint32_t operator()(const GrProgramDesc& desc) const {
63 return SkOpts::hash_fn(desc.asKey(), desc.keyLength(), 0);
64 }
65 };
66
67 SkLRUCache<const GrMtlPipelineStateBuilder::Desc, std::unique_ptr<Entry>, DescHash> fMap;
68
69 GrMtlGpu* fGpu;
70
71#ifdef GR_PIPELINE_STATE_CACHE_STATS
72 int fTotalRequests;
73 int fCacheMisses;
74#endif
75 };
76
Timothy Liange30739a2018-07-31 10:51:17 -040077 SkTArray<std::unique_ptr<GrMtlCopyPipelineState>> fCopyPipelineStateCache;
78
79 GrMtlGpu* fGpu;
Jim Van Verth1223e7f2019-02-28 17:38:35 -050080
81 // Cache of GrMtlPipelineStates
82 std::unique_ptr<PipelineStateCache> fPipelineStateCache;
Timothy Liange30739a2018-07-31 10:51:17 -040083};
84
85#endif