Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
18#include "src/gpu/GpuRefCnt.h"
25
26#include <cinttypes>
27
28class GrVkGpu;
29class GrVkImageView;
30
31class GrVkImage : public GrAttachment {
32private:
33 class Resource;
34
35public:
38 int sampleCnt,
40
43 int numSamples,
46 GrMemoryless memoryless);
47
51 uint32_t mipLevels,
52 GrRenderable renderable,
53 int numSamples,
54 skgpu::Budgeted budgeted,
56
59 const GrVkImageInfo&,
61 UsageFlags attachmentUsages,
64 std::string_view label,
65 bool forSecondaryCB = false);
66
67 ~GrVkImage() override;
68
69 VkImage image() const {
70 // Should only be called when we have a real fResource object, i.e. never when being used as
71 // a RT in an external secondary command buffer.
72 SkASSERT(fResource);
73 return fInfo.fImage;
74 }
75 const skgpu::VulkanAlloc& alloc() const {
76 // Should only be called when we have a real fResource object, i.e. never when being used as
77 // a RT in an external secondary command buffer.
78 SkASSERT(fResource);
79 return fInfo.fAlloc;
80 }
81 const GrVkImageInfo& vkImageInfo() const { return fInfo; }
82 VkFormat imageFormat() const { return fInfo.fFormat; }
83 GrBackendFormat backendFormat() const override {
84 bool usesDRMModifier =
86 if (fResource && this->ycbcrConversionInfo().isValid()) {
87 SkASSERT(this->imageFormat() == this->ycbcrConversionInfo().fFormat);
88 return GrBackendFormats::MakeVk(this->ycbcrConversionInfo(), usesDRMModifier);
89 }
91 return GrBackendFormats::MakeVk(this->imageFormat(), usesDRMModifier);
92 }
93 uint32_t mipLevels() const { return fInfo.fLevelCount; }
95 // Should only be called when we have a real fResource object, i.e. never when being used as
96 // a RT in an external secondary command buffer.
97 SkASSERT(fResource);
98 return fInfo.fYcbcrConversionInfo;
99 }
104
105 const GrVkImageView* framebufferView() const { return fFramebufferView.get(); }
106 const GrVkImageView* textureView() const { return fTextureView.get(); }
107
108 // So that we don't need to rewrite descriptor sets each time, we keep cached input descriptor
109 // sets on the attachment and simply reuse those descriptor sets for this attachment only. These
110 // calls will fail if the attachment does not support being used as an input attachment. These
111 // calls do not ref the GrVkDescriptorSet so they called will need to manually ref them if they
112 // need to be kept alive.
114 // Input descripotr set used when needing to read a resolve attachment to load data into a
115 // discardable msaa attachment.
117
118 const Resource* resource() const {
119 SkASSERT(fResource);
120 return fResource;
121 }
122 bool isLinearTiled() const {
123 // Should only be called when we have a real fResource object, i.e. never when being used as
124 // a RT in an external secondary command buffer.
125 SkASSERT(fResource);
127 }
128 bool isBorrowed() const { return fIsBorrowed; }
129
130 sk_sp<skgpu::MutableTextureState> getMutableState() const { return fMutableState; }
131
135
136 void setImageLayoutAndQueueIndex(const GrVkGpu* gpu,
137 VkImageLayout newLayout,
138 VkAccessFlags dstAccessMask,
139 VkPipelineStageFlags dstStageMask,
140 bool byRegion,
141 uint32_t newQueueFamilyIndex);
142
143 void setImageLayout(const GrVkGpu* gpu,
144 VkImageLayout newLayout,
145 VkAccessFlags dstAccessMask,
146 VkPipelineStageFlags dstStageMask,
147 bool byRegion) {
148 this->setImageLayoutAndQueueIndex(gpu, newLayout, dstAccessMask, dstStageMask, byRegion,
150 }
151
152 uint32_t currentQueueFamilyIndex() const {
154 }
155
156 void setQueueFamilyIndex(uint32_t queueFamilyIndex) {
157 skgpu::MutableTextureStates::SetVkQueueFamilyIndex(fMutableState.get(), queueFamilyIndex);
158 }
159
160 // Returns the image to its original queue family and changes the layout to present if the queue
161 // family is not external or foreign.
162 void prepareForPresent(GrVkGpu* gpu);
163
164 // Returns the image to its original queue family
165 void prepareForExternal(GrVkGpu* gpu);
166
167 // This simply updates our tracking of the image layout and does not actually do any gpu work.
168 // This is only used for mip map generation where we are manually changing the layouts as we
169 // blit each layer, and then at the end need to update our tracking.
171 // Should only be called when we have a real fResource object, i.e. never when being used as
172 // a RT in an external secondary command buffer.
173 SkASSERT(fResource);
174 skgpu::MutableTextureStates::SetVkImageLayout(fMutableState.get(), newLayout);
175 }
176
201
202 static bool InitImageInfo(GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*);
203 // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo
204 static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*);
205
206 // These match the definitions in SkImage, for whence they came
207 typedef void* ReleaseCtx;
208 typedef void (*ReleaseProc)(ReleaseCtx);
209
211
212 // Helpers to use for setting the layout of the VkImage
215
216#if defined(GR_TEST_UTILS)
217 void setCurrentQueueFamilyToGraphicsQueue(GrVkGpu* gpu);
218#endif
219
220private:
221 static sk_sp<GrVkImage> Make(GrVkGpu* gpu,
223 UsageFlags attachmentUsages,
224 int sampleCnt,
226 uint32_t mipLevels,
231
232 GrVkImage(GrVkGpu* gpu,
235 const GrVkImageInfo&,
240 std::string_view label);
241
242 GrVkImage(GrVkGpu* gpu,
245 const GrVkImageInfo&,
251 bool forSecondaryCB,
252 std::string_view label);
253
254 void init(GrVkGpu*, bool forSecondaryCB);
255
256 void onRelease() override;
257 void onAbandon() override;
258
259 void releaseImage();
260 bool hasResource() const { return fResource; }
261
262 GrVkGpu* getVkGpu() const;
263
264 GrVkImageInfo fInfo;
265 uint32_t fInitialQueueFamily;
267
268 sk_sp<const GrVkImageView> fFramebufferView;
269 sk_sp<const GrVkImageView> fTextureView;
270
271 bool fIsBorrowed;
272
273 // Descriptor set used when this is used as an input attachment for reading the dst in blending.
274 gr_rp<const GrVkDescriptorSet> fCachedBlendingInputDescSet;
275 // Descriptor set used when this is used as an input attachment for loading an msaa attachment.
276 gr_rp<const GrVkDescriptorSet> fCachedMSAALoadInputDescSet;
277
278 class Resource : public GrTextureResource {
279 public:
280 explicit Resource(const GrVkGpu* gpu)
281 : fGpu(gpu)
282 , fImage(VK_NULL_HANDLE) {
283 fAlloc.fMemory = VK_NULL_HANDLE;
284 fAlloc.fOffset = 0;
285 }
286
287 Resource(const GrVkGpu* gpu,
288 VkImage image,
290 VkImageTiling tiling)
291 : fGpu(gpu)
292 , fImage(image)
293 , fAlloc(alloc) {}
294
295 ~Resource() override {}
296
297#ifdef SK_TRACE_MANAGED_RESOURCES
298 void dumpInfo() const override {
299 SkDebugf("GrVkImage: %" PRIdPTR " (%d refs)\n", (intptr_t)fImage, this->getRefCnt());
300 }
301#endif
302
303#ifdef SK_DEBUG
304 const GrManagedResource* asVkImageResource() const override { return this; }
305#endif
306
307 private:
308 void freeGPUData() const override;
309
310 const GrVkGpu* fGpu;
311 VkImage fImage;
312 skgpu::VulkanAlloc fAlloc;
313
315 };
316
317 // for wrapped textures
318 class BorrowedResource : public Resource {
319 public:
320 BorrowedResource(const GrVkGpu* gpu, VkImage image, const skgpu::VulkanAlloc& alloc,
321 VkImageTiling tiling)
322 : Resource(gpu, image, alloc, tiling) {
323 }
324 private:
325 void freeGPUData() const override;
326 };
327
328 Resource* fResource;
329
330 friend class GrVkRenderTarget;
331};
332
333#endif
GrWrapCacheable
Definition GrTypesPriv.h:84
GrWrapOwnership
Definition GrTypesPriv.h:76
GrMemoryless
GrBackendObjectOwnership
#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,...)
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
int numSamples() const
UsageFlags supportedUsages() const
SkISize dimensions() const
Definition GrSurface.h:27
bool isProtected() const
Definition GrSurface.h:87
const GrVkImageView * framebufferView() const
Definition GrVkImage.h:105
void prepareForPresent(GrVkGpu *gpu)
static sk_sp< GrVkImage > MakeStencil(GrVkGpu *gpu, SkISize dimensions, int sampleCnt, VkFormat format)
Definition GrVkImage.cpp:21
const GrVkImageView * textureView() const
Definition GrVkImage.h:106
void setQueueFamilyIndex(uint32_t queueFamilyIndex)
Definition GrVkImage.h:156
void(* ReleaseProc)(ReleaseCtx)
Definition GrVkImage.h:208
static VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout)
uint32_t mipLevels() const
Definition GrVkImage.h:93
bool isLinearTiled() const
Definition GrVkImage.h:122
void setImageLayout(const GrVkGpu *gpu, VkImageLayout newLayout, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask, bool byRegion)
Definition GrVkImage.h:143
static void DestroyImageInfo(const GrVkGpu *gpu, GrVkImageInfo *)
const GrVkYcbcrConversionInfo & ycbcrConversionInfo() const
Definition GrVkImage.h:94
GrBackendFormat backendFormat() const override
Definition GrVkImage.h:83
void onAbandon() override
bool supportsInputAttachmentUsage() const
Definition GrVkImage.h:101
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:132
VkFormat imageFormat() const
Definition GrVkImage.h:82
sk_sp< skgpu::MutableTextureState > getMutableState() const
Definition GrVkImage.h:130
VkImageUsageFlags vkUsageFlags()
Definition GrVkImage.h:100
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)
static VkPipelineStageFlags LayoutToPipelineSrcStageFlags(const VkImageLayout layout)
const Resource * resource() const
Definition GrVkImage.h:118
void prepareForExternal(GrVkGpu *gpu)
~GrVkImage() override
const skgpu::VulkanAlloc & alloc() const
Definition GrVkImage.h:75
void updateImageLayout(VkImageLayout newLayout)
Definition GrVkImage.h:170
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:128
gr_rp< const GrVkDescriptorSet > inputDescSetForMSAALoad(GrVkGpu *gpu)
uint32_t currentQueueFamilyIndex() const
Definition GrVkImage.h:152
void * ReleaseCtx
Definition GrVkImage.h:207
gr_rp< const GrVkDescriptorSet > inputDescSetForBlending(GrVkGpu *gpu)
void setResourceRelease(sk_sp< RefCntedReleaseProc > releaseHelper)
const GrVkImageInfo & vkImageInfo() const
Definition GrVkImage.h:81
void setImageLayoutAndQueueIndex(const GrVkGpu *gpu, VkImageLayout newLayout, VkAccessFlags dstAccessMask, VkPipelineStageFlags dstStageMask, bool byRegion, uint32_t newQueueFamilyIndex)
VkImage image() const
Definition GrVkImage.h:69
void onRelease() override
static bool InitImageInfo(GrVkGpu *gpu, const ImageDesc &imageDesc, GrVkImageInfo *)
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
GrVkYcbcrConversionInfo fYcbcrConversionInfo
Definition GrVkTypes.h:36
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
VkImageUsageFlags fImageUsageFlags
Definition GrVkTypes.h:31
VkImageTiling fImageTiling
Definition GrVkTypes.h:28
VkImageUsageFlags fUsageFlags
Definition GrVkImage.h:185
VkImageType fImageType
Definition GrVkImage.h:178
GrProtected fIsProtected
Definition GrVkImage.h:187
VkImageTiling fImageTiling
Definition GrVkImage.h:184
VkFlags VkPipelineStageFlags
VkImageLayout
@ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
VkFlags VkAccessFlags
VkFlags VkImageUsageFlags
VkImageTiling
@ VK_IMAGE_TILING_OPTIMAL
@ VK_IMAGE_TILING_LINEAR
@ VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT
@ VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
VkImageType
@ VK_IMAGE_TYPE_2D
uint32_t VkFlags
Definition vulkan_core.h:97
#define VK_NULL_HANDLE
Definition vulkan_core.h:46
VkFormat
@ VK_FORMAT_UNDEFINED
#define VK_QUEUE_FAMILY_IGNORED