xref: /aosp_15_r20/external/skia/src/gpu/graphite/dawn/DawnResourceProvider.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 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_graphite_DawnResourceProvider_DEFINED
9 #define skgpu_graphite_DawnResourceProvider_DEFINED
10 
11 #include "include/gpu/graphite/dawn/DawnTypes.h"
12 #include "src/core/SkLRUCache.h"
13 #include "src/core/SkTHash.h"
14 #include "src/gpu/graphite/PipelineData.h"
15 #include "src/gpu/graphite/ResourceProvider.h"
16 
17 namespace skgpu::graphite {
18 
19 class DawnGraphicsPipeline;
20 class DawnSampler;
21 class DawnSharedContext;
22 class DawnTexture;
23 class DawnBuffer;
24 class DawnCommandBuffer;
25 
26 class DawnResourceProvider final : public ResourceProvider {
27 public:
28     template <size_t NumEntries>
29     using BindGroupKey = FixedSizeKey<2 * NumEntries>;
30 
31     static constexpr size_t kNumUniformEntries = 4;
32 
33     DawnResourceProvider(SharedContext* sharedContext,
34                          SingleOwner*,
35                          uint32_t recorderID,
36                          size_t resourceBudget);
37     ~DawnResourceProvider() override;
38 
39     sk_sp<DawnTexture> findOrCreateDiscardableMSAALoadTexture(SkISize dimensions,
40                                                               const TextureInfo& msaaInfo);
41 
42     wgpu::RenderPipeline findOrCreateBlitWithDrawPipeline(const RenderPassDesc& renderPassDesc);
43 
44     sk_sp<DawnBuffer> findOrCreateDawnBuffer(size_t size,
45                                              BufferType type,
46                                              AccessPattern,
47                                              std::string_view label);
48 
49     const wgpu::BindGroupLayout& getOrCreateUniformBuffersBindGroupLayout();
50     const wgpu::BindGroupLayout& getOrCreateSingleTextureSamplerBindGroupLayout();
51 
52     // Find the cached bind group or create a new one based on the bound buffers and their
53     // binding sizes (boundBuffersAndSizes) for these uniforms (in order):
54     // - Intrinsic constants.
55     // - Render step uniforms.
56     // - Paint uniforms.
57     const wgpu::BindGroup& findOrCreateUniformBuffersBindGroup(
58             const std::array<std::pair<const DawnBuffer*, uint32_t>, kNumUniformEntries>&
59                     boundBuffersAndSizes);
60 
61     // Find or create a bind group containing the given sampler & texture.
62     const wgpu::BindGroup& findOrCreateSingleTextureSamplerBindGroup(const DawnSampler* sampler,
63                                                                      const DawnTexture* texture);
64 
65     // Find the cached bind buffer info, or create a new one for the given intrinsic values.
66     BindBufferInfo findOrCreateIntrinsicBindBufferInfo(DawnCommandBuffer* cb,
67                                                        UniformDataBlock intrinsicValues);
68 
69 private:
70     sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*,
71                                                    const GraphicsPipelineDesc&,
72                                                    const RenderPassDesc&,
73                                                    SkEnumBitMask<PipelineCreationFlags>) override;
74     sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override;
75 
76     sk_sp<Texture> createTexture(SkISize, const TextureInfo&, skgpu::Budgeted) override;
77     sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) override;
78 
79     sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) override;
80 
81     sk_sp<Sampler> createSampler(const SamplerDesc&) override;
82 
83     BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override;
84     void onDeleteBackendTexture(const BackendTexture&) override;
85 
86     const wgpu::Buffer& getOrCreateNullBuffer();
87 
88     DawnSharedContext* dawnSharedContext() const;
89 
90     void onFreeGpuResources() override;
91     void onPurgeResourcesNotUsedSince(StdSteadyClock::time_point purgeTime) override;
92 
93     skia_private::THashMap<uint32_t, wgpu::RenderPipeline> fBlitWithDrawPipelines;
94 
95     wgpu::BindGroupLayout fUniformBuffersBindGroupLayout;
96     wgpu::BindGroupLayout fSingleTextureSamplerBindGroupLayout;
97 
98     wgpu::Buffer fNullBuffer;
99 
100     template <size_t NumEntries>
101     using BindGroupCache = SkLRUCache<BindGroupKey<NumEntries>,
102                                       wgpu::BindGroup,
103                                       typename BindGroupKey<NumEntries>::Hash>;
104 
105     BindGroupCache<kNumUniformEntries> fUniformBufferBindGroupCache;
106     BindGroupCache<1> fSingleTextureSamplerBindGroups;
107 
108     class IntrinsicBuffer;
109     class IntrinsicConstantsManager;
110     std::unique_ptr<IntrinsicConstantsManager> fIntrinsicConstantsManager;
111 };
112 
113 }  // namespace skgpu::graphite
114 
115 #endif  // skgpu_graphite_DawnResourceProvider_DEFINED
116