xref: /aosp_15_r20/external/skia/src/gpu/graphite/vk/VulkanResourceProvider.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_VulkanResourceProvider_DEFINED
9 #define skgpu_graphite_VulkanResourceProvider_DEFINED
10 
11 #include "src/gpu/graphite/ResourceProvider.h"
12 #include "src/gpu/graphite/vk/VulkanGraphicsPipeline.h"
13 
14 #include "include/gpu/vk/VulkanTypes.h"
15 #include "src/core/SkLRUCache.h"
16 #include "src/core/SkTHash.h"
17 #include "src/gpu/graphite/DescriptorData.h"
18 
19 #ifdef  SK_BUILD_FOR_ANDROID
20 extern "C" {
21     typedef struct AHardwareBuffer AHardwareBuffer;
22 }
23 #endif
24 
25 namespace skgpu::graphite {
26 
27 class VulkanCommandBuffer;
28 class VulkanDescriptorSet;
29 class VulkanFramebuffer;
30 class VulkanGraphicsPipeline;
31 class VulkanRenderPass;
32 class VulkanSharedContext;
33 class VulkanYcbcrConversion;
34 
35 class VulkanResourceProvider final : public ResourceProvider {
36 public:
37     static constexpr size_t kIntrinsicConstantSize = sizeof(float) * 8; // float4 + 2xfloat2
38     static constexpr size_t kLoadMSAAVertexBufferSize = sizeof(float) * 8; // 4 points of 2 floats
39 
40     using UniformBindGroupKey = FixedSizeKey<2 * VulkanGraphicsPipeline::kNumUniformBuffers>;
41 
42     VulkanResourceProvider(SharedContext* sharedContext,
43                            SingleOwner*,
44                            uint32_t recorderID,
45                            size_t resourceBudget,
46                            sk_sp<Buffer> intrinsicConstantUniformBuffer);
47 
48     ~VulkanResourceProvider() override;
49 
50     sk_sp<Buffer> refIntrinsicConstantBuffer() const;
51 
52     sk_sp<VulkanYcbcrConversion> findOrCreateCompatibleYcbcrConversion(
53             const VulkanYcbcrConversionInfo& ycbcrInfo) const;
54 
55 private:
56     const VulkanSharedContext* vulkanSharedContext() const;
57 
58     sk_sp<GraphicsPipeline> createGraphicsPipeline(const RuntimeEffectDictionary*,
59                                                    const GraphicsPipelineDesc&,
60                                                    const RenderPassDesc&,
61                                                    SkEnumBitMask<PipelineCreationFlags>) override;
62     sk_sp<ComputePipeline> createComputePipeline(const ComputePipelineDesc&) override;
63 
64     sk_sp<Texture> createTexture(SkISize,
65                                  const TextureInfo&,
66                                  skgpu::Budgeted) override;
67     sk_sp<Texture> onCreateWrappedTexture(const BackendTexture&) override;
68     sk_sp<Buffer> createBuffer(size_t size, BufferType type, AccessPattern) override;
69     sk_sp<Sampler> createSampler(const SamplerDesc&) override;
70 
71     sk_sp<VulkanFramebuffer> createFramebuffer(
72             const VulkanSharedContext*,
73             const skia_private::TArray<VkImageView>& attachmentViews,
74             const VulkanRenderPass&,
75             const int width,
76             const int height);
77 
78     BackendTexture onCreateBackendTexture(SkISize dimensions, const TextureInfo&) override;
79 #ifdef SK_BUILD_FOR_ANDROID
80     BackendTexture onCreateBackendTexture(AHardwareBuffer*,
81                                           bool isRenderable,
82                                           bool isProtectedContent,
83                                           SkISize dimensions,
84                                           bool fromAndroidWindow) const override;
85 #endif
86     void onDeleteBackendTexture(const BackendTexture&) override;
87 
88     sk_sp<VulkanDescriptorSet> findOrCreateDescriptorSet(SkSpan<DescriptorData>);
89 
90     sk_sp<VulkanDescriptorSet> findOrCreateUniformBuffersDescriptorSet(
91             SkSpan<DescriptorData> requestedDescriptors,
92             SkSpan<BindBufferInfo> bindUniformBufferInfo);
93 
94     sk_sp<VulkanGraphicsPipeline> findOrCreateLoadMSAAPipeline(const RenderPassDesc&);
95 
96     // Find or create a compatible (needed when creating a framebuffer and graphics pipeline) or
97     // full (needed when beginning a render pass from the command buffer) RenderPass.
98     sk_sp<VulkanRenderPass> findOrCreateRenderPass(const RenderPassDesc&, bool compatibleOnly);
99 
100     // Use a predetermined RenderPass key for finding/creating a RenderPass to avoid recreating it
101     sk_sp<VulkanRenderPass> findOrCreateRenderPassWithKnownKey(
102             const RenderPassDesc&, bool compatibleOnly, const GraphiteResourceKey& rpKey);
103 
104     VkPipelineCache pipelineCache();
105 
106     friend class VulkanCommandBuffer;
107     friend class VulkanGraphicsPipeline;
108     VkPipelineCache fPipelineCache = VK_NULL_HANDLE;
109 
110     // Each render pass will need buffer space to record rtAdjust information. To minimize costly
111     // allocation calls and searching of the resource cache, we find & store a uniform buffer upon
112     // resource provider creation. This way, render passes across all command buffers can simply
113     // update the value within this buffer as needed.
114     sk_sp<Buffer> fIntrinsicUniformBuffer;
115 
116     // The first value of the pair is a renderpass key. Graphics pipeline keys contain extra
117     // information that we do not need for identifying unique pipelines.
118     skia_private::TArray<std::pair<GraphiteResourceKey,
119                          sk_sp<VulkanGraphicsPipeline>>> fLoadMSAAPipelines;
120     // All of the following attributes are the same between all msaa load pipelines, so they only
121     // need to be created once and can then be stored.
122     VkShaderModule fMSAALoadVertShaderModule = VK_NULL_HANDLE;
123     VkShaderModule fMSAALoadFragShaderModule = VK_NULL_HANDLE;
124     VkPipelineShaderStageCreateInfo fMSAALoadShaderStageInfo[2];
125     VkPipelineLayout fMSAALoadPipelineLayout = VK_NULL_HANDLE;
126 
127     SkLRUCache<UniformBindGroupKey, sk_sp<VulkanDescriptorSet>,
128                UniformBindGroupKey::Hash> fUniformBufferDescSetCache;
129 };
130 
131 } // namespace skgpu::graphite
132 
133 #endif // skgpu_graphite_VulkanResourceProvider_DEFINED
134