• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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 skgpu_ResourceProvider_DEFINED
9 #define skgpu_ResourceProvider_DEFINED
10 
11 #include "experimental/graphite/src/CommandBuffer.h"
12 #include "experimental/graphite/src/GraphicsPipelineDesc.h"
13 #include "experimental/graphite/src/ResourceCache.h"
14 #include "experimental/graphite/src/ResourceTypes.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkTileMode.h"
17 #include "src/core/SkLRUCache.h"
18 #include "src/gpu/ResourceKey.h"
19 
20 struct SkSamplingOptions;
21 class SkShaderCodeDictionary;
22 
23 namespace skgpu {
24 
25 class BackendTexture;
26 class Buffer;
27 class Caps;
28 class GlobalCache;
29 class Gpu;
30 class GraphicsPipeline;
31 class Sampler;
32 class Texture;
33 class TextureInfo;
34 
35 class ResourceProvider {
36 public:
37     virtual ~ResourceProvider();
38 
39     virtual sk_sp<CommandBuffer> createCommandBuffer() = 0;
40 
41     sk_sp<GraphicsPipeline> findOrCreateGraphicsPipeline(const GraphicsPipelineDesc&,
42                                                          const RenderPassDesc&);
43 
44     sk_sp<Texture> findOrCreateTexture(SkISize, const TextureInfo&);
45     virtual sk_sp<Texture> createWrappedTexture(const BackendTexture&) = 0;
46 
47     sk_sp<Buffer> findOrCreateBuffer(size_t size, BufferType type, PrioritizeGpuReads);
48 
49     sk_sp<Sampler> findOrCreateCompatibleSampler(const SkSamplingOptions&,
50                                                  SkTileMode xTileMode,
51                                                  SkTileMode yTileMode);
52 
53     SkShaderCodeDictionary* shaderCodeDictionary() const;
54 
55 protected:
56     ResourceProvider(const Gpu* gpu, sk_sp<GlobalCache>, SingleOwner* singleOwner);
57 
58     const Gpu* fGpu;
59 
60 private:
61     virtual sk_sp<GraphicsPipeline> onCreateGraphicsPipeline(const GraphicsPipelineDesc&,
62                                                              const RenderPassDesc&) = 0;
63     virtual sk_sp<Texture> createTexture(SkISize, const TextureInfo&) = 0;
64     virtual sk_sp<Buffer> createBuffer(size_t size, BufferType type, PrioritizeGpuReads) = 0;
65 
66     virtual sk_sp<Sampler> createSampler(const SkSamplingOptions&,
67                                          SkTileMode xTileMode,
68                                          SkTileMode yTileMode) = 0;
69 
70     class GraphicsPipelineCache {
71     public:
72         GraphicsPipelineCache(ResourceProvider* resourceProvider);
73         ~GraphicsPipelineCache();
74 
75         void release();
76         sk_sp<GraphicsPipeline> refPipeline(const Caps* caps,
77                                             const GraphicsPipelineDesc&,
78                                             const RenderPassDesc&);
79 
80     private:
81         struct Entry;
82         struct KeyHash {
operatorKeyHash83             uint32_t operator()(const UniqueKey& key) const {
84                 return key.hash();
85             }
86         };
87         SkLRUCache<UniqueKey, std::unique_ptr<Entry>, KeyHash> fMap;
88 
89         ResourceProvider* fResourceProvider;
90     };
91 
92     ResourceCache fResourceCache;
93     sk_sp<GlobalCache> fGlobalCache;
94 
95     // Cache of GraphicsPipelines
96     // TODO: Move this onto GlobalCache
97     std::unique_ptr<GraphicsPipelineCache> fGraphicsPipelineCache;
98 };
99 
100 } // namespace skgpu
101 
102 #endif // skgpu_ResourceProvider_DEFINED
103