Flutter Engine
The Flutter Engine
GrVkImage.h
Go to the documentation of this file.
1/*
2 * Copyright 2015 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 GrVkImage_DEFINED
9#define GrVkImage_DEFINED
10
19#include "src/gpu/GpuRefCnt.h"
26
27#include <cinttypes>
28
29class GrVkGpu;
30class GrVkImageView;
31
32class GrVkImage : public GrAttachment {
33private:
34 class Resource;
35
36public:
39 int sampleCnt,
41
44 int numSamples,
47 GrMemoryless memoryless);
48
52 uint32_t mipLevels,
53 GrRenderable renderable,
54 int numSamples,
55 skgpu::Budgeted budgeted,
57
60 const GrVkImageInfo&,
62 UsageFlags attachmentUsages,
65 std::string_view label,
66 bool forSecondaryCB = false);
67
68 ~GrVkImage() override;
69
70 VkImage image() const {
71 // Should only be called when we have a real fResource object, i.e. never when being used as
72 // a RT in an external secondary command buffer.
73 SkASSERT(fResource);
74 return fInfo.fImage;
75 }
76 const skgpu::VulkanAlloc& alloc() const {
77 // Should only be called when we have a real fResource object, i.e. never when being used as
78 // a RT in an external secondary command buffer.
79 SkASSERT(fResource);
80 return fInfo.fAlloc;
81 }
82 const GrVkImageInfo& vkImageInfo() const { return fInfo; }
83 VkFormat imageFormat() const { return fInfo.fFormat; }
84 GrBackendFormat backendFormat() const override {
85 bool usesDRMModifier =
87 if (fResource && this->ycbcrConversionInfo().isValid()) {
88 SkASSERT(this->imageFormat() == this->ycbcrConversionInfo().fFormat);
89 return GrBackendFormats::MakeVk(this->ycbcrConversionInfo(), usesDRMModifier);
90 }
92 return GrBackendFormats::MakeVk(this->imageFormat(), usesDRMModifier);
93 }
94 uint32_t mipLevels() const { return fInfo.fLevelCount; }
96 // Should only be called when we have a real fResource object, i.e. never when being used as
97 // a RT in an external secondary command buffer.
98 SkASSERT(fResource);
99 return fInfo.fYcbcrConversionInfo;
100 }
104 }
105
106 const GrVkImageView* framebufferView() const { return fFramebufferView.get(); }
107 const GrVkImageView* textureView() const { return fTextureView.get(); }
108
109 // So that we don't need to rewrite descriptor sets each time, we keep cached input descriptor
110 // sets on the attachment and simply reuse those descriptor sets for this attachment only. These
111 // calls will fail if the attachment does not support being used as an input attachment. These
112 // calls do not ref the GrVkDescriptorSet so they called will need to manually ref them if they
113 // need to be kept alive.
115 // Input descripotr set used when needing to read a resolve attachment to load data into a
116 // discardable msaa attachment.
118
119 const Resource* resource() const {
120 SkASSERT(fResource);
121 return fResource;
122 }
123 bool isLinearTiled() const {
124 // Should only be called when we have a real fResource object, i.e. never when being used as
125 // a RT in an external secondary command buffer.
126 SkASSERT(fResource);
128 }
129 bool isBorrowed() const { return fIsBorrowed; }
130
131 sk_sp<skgpu::MutableTextureState> getMutableState() const { return fMutableState; }
132
135 }
136
137 void setImageLayoutAndQueueIndex(const GrVkGpu* gpu,
138 VkImageLayout newLayout,
139 VkAccessFlags dstAccessMask,
140 VkPipelineStageFlags dstStageMask,
141 bool byRegion,
142 uint32_t newQueueFamilyIndex);
143
144 void setImageLayout(const GrVkGpu* gpu,
145 VkImageLayout newLayout,
146 VkAccessFlags dstAccessMask,
147 VkPipelineStageFlags dstStageMask,
148 bool byRegion) {
149 this->setImageLayoutAndQueueIndex(gpu, newLayout, dstAccessMask, dstStageMask, byRegion,
151 }
152
153 uint32_t currentQueueFamilyIndex() const {
155 }
156
157 void setQueueFamilyIndex(uint32_t queueFamilyIndex) {
158 skgpu::MutableTextureStates::SetVkQueueFamilyIndex(fMutableState.get(), queueFamilyIndex);
159 }
160
161 // Returns the image to its original queue family and changes the layout to present if the queue
162 // family is not external or foreign.
163 void prepareForPresent(GrVkGpu* gpu);
164
165 // Returns the image to its original queue family
166 void prepareForExternal(GrVkGpu* gpu);
167
168 // This simply updates our tracking of the image layout and does not actually do any gpu work.
169 // This is only used for mip map generation where we are manually changing the layouts as we
170 // blit each layer, and then at the end need to update our tracking.
172 // Should only be called when we have a real fResource object, i.e. never when being used as
173 // a RT in an external secondary command buffer.
174 SkASSERT(fResource);
175 skgpu::MutableTextureStates::SetVkImageLayout(fMutableState.get(), newLayout);
176 }
177
178 struct ImageDesc {
181 uint32_t fWidth;
182 uint32_t fHeight;
183 uint32_t fLevels;
184 uint32_t fSamples;
189
193 , fWidth(0)
194 , fHeight(0)
195 , fLevels(1)
196 , fSamples(1)
198 , fUsageFlags(0)
201 };
202
203 static bool InitImageInfo(GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*);
204 // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo
205 static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*);
206
207 // These match the definitions in SkImage, for whence they came
208 typedef void* ReleaseCtx;
209 typedef void (*ReleaseProc)(ReleaseCtx);
210
212
213 // Helpers to use for setting the layout of the VkImage
216
217#if defined(GR_TEST_UTILS)
218 void setCurrentQueueFamilyToGraphicsQueue(GrVkGpu* gpu);
219#endif
220
221private:
222 static sk_sp<GrVkImage> Make(GrVkGpu* gpu,
224 UsageFlags attachmentUsages,
225 int sampleCnt,
227 uint32_t mipLevels,
232
233 GrVkImage(GrVkGpu* gpu,
236 const GrVkImageInfo&,
241 std::string_view label);
242
243 GrVkImage(GrVkGpu* gpu,
246 const GrVkImageInfo&,
252 bool forSecondaryCB,
253 std::string_view label);
254
255 void init(GrVkGpu*, bool forSecondaryCB);
256
257 void onRelease() override;
258 void onAbandon() override;
259
260 void releaseImage();
261 bool hasResource() const { return fResource; }
262
263 GrVkGpu* getVkGpu() const;
264
265 GrVkImageInfo fInfo;
266 uint32_t fInitialQueueFamily;
268
269 sk_sp<const GrVkImageView> fFramebufferView;
270 sk_sp<const GrVkImageView> fTextureView;
271
272 bool fIsBorrowed;
273
274 // Descriptor set used when this is used as an input attachment for reading the dst in blending.
275 gr_rp<const GrVkDescriptorSet> fCachedBlendingInputDescSet;
276 // Descriptor set used when this is used as an input attachment for loading an msaa attachment.
277 gr_rp<const GrVkDescriptorSet> fCachedMSAALoadInputDescSet;
278
279 class Resource : public GrTextureResource {
280 public:
281 explicit Resource(const GrVkGpu* gpu)
282 : fGpu(gpu)
283 , fImage(VK_NULL_HANDLE) {
284 fAlloc.fMemory = VK_NULL_HANDLE;
285 fAlloc.fOffset = 0;
286 }
287
288 Resource(const GrVkGpu* gpu,
289 VkImage image,
291 VkImageTiling tiling)
292 : fGpu(gpu)
293 , fImage(image)
294 , fAlloc(alloc) {}
295
296 ~Resource() override {}
297
298#ifdef SK_TRACE_MANAGED_RESOURCES
299 void dumpInfo() const override {
300 SkDebugf("GrVkImage: %" PRIdPTR " (%d refs)\n", (intptr_t)fImage, this->getRefCnt());
301 }
302#endif
303
304#ifdef SK_DEBUG
305 const GrManagedResource* asVkImageResource() const override { return this; }
306#endif
307
308 private:
309 void freeGPUData() const override;
310
311 const GrVkGpu* fGpu;
312 VkImage fImage;
313 skgpu::VulkanAlloc fAlloc;
314
316 };
317
318 // for wrapped textures
319 class BorrowedResource : public Resource {
320 public:
321 BorrowedResource(const GrVkGpu* gpu, VkImage image, const skgpu::VulkanAlloc& alloc,
322 VkImageTiling tiling)
323 : Resource(gpu, image, alloc, tiling) {
324 }
325 private:
326 void freeGPUData() const override;
327 };
328
329 Resource* fResource;
330
331 friend class GrVkRenderTarget;
332};
333
334#endif
GrWrapCacheable
Definition: GrTypesPriv.h:85
GrWrapOwnership
Definition: GrTypesPriv.h:77
GrMemoryless
Definition: GrTypesPriv.h:123
GrBackendObjectOwnership
Definition: GrTypesPriv.h:496
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
@ kNo
Don't pre-clip the geometry before applying the (perspective) matrix.
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
int numSamples() const
Definition: GrAttachment.h:38
UsageFlags supportedUsages() const
Definition: GrAttachment.h:36
SkISize dimensions() const
Definition: GrSurface.h:27
bool isProtected() const
Definition: GrSurface.h:87
const GrVkImageView * framebufferView() const
Definition: GrVkImage.h:106
void prepareForPresent(GrVkGpu *gpu)
Definition: GrVkImage.cpp:589
static sk_sp< GrVkImage > MakeStencil(GrVkGpu *gpu, SkISize dimensions, int sampleCnt, VkFormat format)
Definition: GrVkImage.cpp:21
const GrVkImageView * textureView() const
Definition: GrVkImage.h:107
void setQueueFamilyIndex(uint32_t queueFamilyIndex)
Definition: GrVkImage.h:157
void(* ReleaseProc)(ReleaseCtx)
Definition: GrVkImage.h:209
static VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout)
Definition: GrVkImage.cpp:335
uint32_t mipLevels() const
Definition: GrVkImage.h:94
bool isLinearTiled() const
Definition: GrVkImage.h:123
void setImageLayout(const GrVkGpu *gpu, VkImageLayout newLayout, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask, bool byRegion)
Definition: GrVkImage.h:144
static void DestroyImageInfo(const GrVkGpu *gpu, GrVkImageInfo *)
Definition: GrVkImage.cpp:577
GrBackendFormat backendFormat() const override
Definition: GrVkImage.h:84
bool supportsInputAttachmentUsage() const
Definition: GrVkImage.h:102
static sk_sp< GrVkImage > MakeMSAA(GrVkGpu *gpu, SkISize dimensions, int numSamples, VkFormat format, GrProtected isProtected, GrMemoryless memoryless)
Definition: GrVkImage.cpp:39
VkImageLayout currentLayout() const
Definition: GrVkImage.h:133
VkFormat imageFormat() const
Definition: GrVkImage.h:83
sk_sp< skgpu::MutableTextureState > getMutableState() const
Definition: GrVkImage.h:131
VkImageUsageFlags vkUsageFlags()
Definition: GrVkImage.h:101
static sk_sp< GrVkImage > MakeWrapped(GrVkGpu *gpu, SkISize dimensions, const GrVkImageInfo &, sk_sp< skgpu::MutableTextureState >, UsageFlags attachmentUsages, GrWrapOwnership, GrWrapCacheable, std::string_view label, bool forSecondaryCB=false)
Definition: GrVkImage.cpp:179
const skgpu::VulkanYcbcrConversionInfo & ycbcrConversionInfo() const
Definition: GrVkImage.h:95
static VkPipelineStageFlags LayoutToPipelineSrcStageFlags(const VkImageLayout layout)
Definition: GrVkImage.cpp:312
const Resource * resource() const
Definition: GrVkImage.h:119
void prepareForExternal(GrVkGpu *gpu)
Definition: GrVkImage.cpp:601
~GrVkImage() override
Definition: GrVkImage.cpp:582
const skgpu::VulkanAlloc & alloc() const
Definition: GrVkImage.h:76
void updateImageLayout(VkImageLayout newLayout)
Definition: GrVkImage.h:171
static sk_sp< GrVkImage > MakeTexture(GrVkGpu *gpu, SkISize dimensions, VkFormat format, uint32_t mipLevels, GrRenderable renderable, int numSamples, skgpu::Budgeted budgeted, GrProtected isProtected)
Definition: GrVkImage.cpp:65
bool isBorrowed() const
Definition: GrVkImage.h:129
gr_rp< const GrVkDescriptorSet > inputDescSetForMSAALoad(GrVkGpu *gpu)
Definition: GrVkImage.cpp:691
uint32_t currentQueueFamilyIndex() const
Definition: GrVkImage.h:153
void * ReleaseCtx
Definition: GrVkImage.h:208
gr_rp< const GrVkDescriptorSet > inputDescSetForBlending(GrVkGpu *gpu)
Definition: GrVkImage.cpp:670
void setResourceRelease(sk_sp< RefCntedReleaseProc > releaseHelper)
Definition: GrVkImage.cpp:628
const GrVkImageInfo & vkImageInfo() const
Definition: GrVkImage.h:82
void setImageLayoutAndQueueIndex(const GrVkGpu *gpu, VkImageLayout newLayout, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask, bool byRegion, uint32_t newQueueFamilyIndex)
Definition: GrVkImage.cpp:379
VkImage image() const
Definition: GrVkImage.h:70
static bool InitImageInfo(GrVkGpu *gpu, const ImageDesc &imageDesc, GrVkImageInfo *)
Definition: GrVkImage.cpp:471
T * get() const
Definition: SkRefCnt.h:303
uint32_t uint32_t * format
SK_API GrBackendFormat MakeVk(VkFormat format, bool willUseDRMFormatModifiers=false)
void SetVkImageLayout(MutableTextureState *state, VkImageLayout layout)
SK_API uint32_t GetVkQueueFamilyIndex(const MutableTextureState &state)
void SetVkQueueFamilyIndex(MutableTextureState *state, uint32_t queueFamilyIndex)
SK_API VkImageLayout GetVkImageLayout(const MutableTextureState &state)
Budgeted
Definition: GpuTypes.h:35
Renderable
Definition: GpuTypes.h:69
Protected
Definition: GpuTypes.h:61
VkImage fImage
Definition: GrVkTypes.h:26
skgpu::VulkanAlloc fAlloc
Definition: GrVkTypes.h:27
VkFormat fFormat
Definition: GrVkTypes.h:30
uint32_t fLevelCount
Definition: GrVkTypes.h:33
skgpu::VulkanYcbcrConversionInfo fYcbcrConversionInfo
Definition: GrVkTypes.h:36
VkImageUsageFlags fImageUsageFlags
Definition: GrVkTypes.h:31
VkImageTiling fImageTiling
Definition: GrVkTypes.h:28
VkImageUsageFlags fUsageFlags
Definition: GrVkImage.h:186
VkImageType fImageType
Definition: GrVkImage.h:179
GrProtected fIsProtected
Definition: GrVkImage.h:188
VkImageTiling fImageTiling
Definition: GrVkImage.h:185
Definition: SkSize.h:16
VkFlags VkPipelineStageFlags
Definition: vulkan_core.h:2470
VkImageLayout
Definition: vulkan_core.h:1330
@ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
Definition: vulkan_core.h:2399
VkFlags VkAccessFlags
Definition: vulkan_core.h:2235
VkFlags VkImageUsageFlags
Definition: vulkan_core.h:2382
VkImageTiling
Definition: vulkan_core.h:1766
@ VK_IMAGE_TILING_OPTIMAL
Definition: vulkan_core.h:1767
@ VK_IMAGE_TILING_LINEAR
Definition: vulkan_core.h:1768
@ VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT
Definition: vulkan_core.h:1769
@ VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
Definition: vulkan_core.h:2359
VkImageType
Definition: vulkan_core.h:1773
@ VK_IMAGE_TYPE_2D
Definition: vulkan_core.h:1775
uint32_t VkFlags
Definition: vulkan_core.h:97
#define VK_NULL_HANDLE
Definition: vulkan_core.h:46
VkFormat
Definition: vulkan_core.h:1458
@ VK_FORMAT_UNDEFINED
Definition: vulkan_core.h:1459
#define VK_QUEUE_FAMILY_IGNORED
Definition: vulkan_core.h:127