Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/gpu/GrBackendSurface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/gpu/vk/GrVkTypes.h" |
| 14 | #include "include/private/GrTypesPriv.h" |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 15 | #include "include/private/GrVkTypesPriv.h" |
| 16 | #include "src/gpu/GrBackendSurfaceMutableStateImpl.h" |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrManagedResource.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 18 | #include "src/gpu/GrTexture.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | |
| 20 | class GrVkGpu; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 21 | class GrVkTexture; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 22 | |
| 23 | class GrVkImage : SkNoncopyable { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 24 | private: |
| 25 | class Resource; |
| 26 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | public: |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 28 | GrVkImage(const GrVkGpu* gpu, |
| 29 | const GrVkImageInfo& info, |
| 30 | sk_sp<GrBackendSurfaceMutableStateImpl> mutableState, |
| 31 | GrBackendObjectOwnership ownership, |
Greg Daniel | aa9d99f | 2020-06-02 11:10:41 -0400 | [diff] [blame] | 32 | bool forSecondaryCB = false); |
| 33 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 34 | virtual ~GrVkImage(); |
| 35 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 36 | VkImage image() const { |
| 37 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 38 | // a RT in an external secondary command buffer. |
| 39 | SkASSERT(fResource); |
| 40 | return fInfo.fImage; |
| 41 | } |
| 42 | const GrVkAlloc& alloc() const { |
| 43 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 44 | // a RT in an external secondary command buffer. |
| 45 | SkASSERT(fResource); |
| 46 | return fInfo.fAlloc; |
| 47 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 48 | VkFormat imageFormat() const { return fInfo.fFormat; } |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 49 | GrBackendFormat getBackendFormat() const { |
Greg Daniel | 89df784 | 2019-02-21 12:40:21 -0500 | [diff] [blame] | 50 | if (fResource && this->ycbcrConversionInfo().isValid()) { |
Sergey Ulanov | 2739fd2 | 2019-08-11 22:46:33 -0700 | [diff] [blame] | 51 | SkASSERT(this->imageFormat() == this->ycbcrConversionInfo().fFormat); |
Greg Daniel | 89df784 | 2019-02-21 12:40:21 -0500 | [diff] [blame] | 52 | return GrBackendFormat::MakeVk(this->ycbcrConversionInfo()); |
| 53 | } |
| 54 | SkASSERT(this->imageFormat() != VK_FORMAT_UNDEFINED); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 55 | return GrBackendFormat::MakeVk(this->imageFormat()); |
| 56 | } |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 57 | uint32_t mipLevels() const { return fInfo.fLevelCount; } |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 58 | const GrVkYcbcrConversionInfo& ycbcrConversionInfo() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 59 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 60 | // a RT in an external secondary command buffer. |
| 61 | SkASSERT(fResource); |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 62 | return fInfo.fYcbcrConversionInfo; |
| 63 | } |
Greg Daniel | bf7acb2 | 2020-08-21 13:32:51 -0400 | [diff] [blame] | 64 | bool supportsInputAttachmentUsage() const { |
| 65 | return fInfo.fImageUsageFlags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 66 | } |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 67 | const Resource* resource() const { |
| 68 | SkASSERT(fResource); |
| 69 | return fResource; |
| 70 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | bool isLinearTiled() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 72 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 73 | // a RT in an external secondary command buffer. |
| 74 | SkASSERT(fResource); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 75 | return SkToBool(VK_IMAGE_TILING_LINEAR == fInfo.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 76 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 77 | bool isBorrowed() const { return fIsBorrowed; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 78 | |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 79 | sk_sp<GrBackendSurfaceMutableStateImpl> getMutableState() const { return fMutableState; } |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 80 | |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 81 | VkImageLayout currentLayout() const { return fMutableState->getImageLayout(); } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 82 | |
Greg Daniel | 7f3408b | 2020-06-03 13:31:00 -0400 | [diff] [blame] | 83 | void setImageLayoutAndQueueIndex(const GrVkGpu* gpu, |
| 84 | VkImageLayout newLayout, |
| 85 | VkAccessFlags dstAccessMask, |
| 86 | VkPipelineStageFlags dstStageMask, |
| 87 | bool byRegion, |
| 88 | uint32_t newQueueFamilyIndex); |
| 89 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 90 | void setImageLayout(const GrVkGpu* gpu, |
| 91 | VkImageLayout newLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 92 | VkAccessFlags dstAccessMask, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 93 | VkPipelineStageFlags dstStageMask, |
Greg Daniel | 7f3408b | 2020-06-03 13:31:00 -0400 | [diff] [blame] | 94 | bool byRegion) { |
| 95 | this->setImageLayoutAndQueueIndex(gpu, newLayout, dstAccessMask, dstStageMask, byRegion, |
| 96 | VK_QUEUE_FAMILY_IGNORED); |
| 97 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 99 | uint32_t currentQueueFamilyIndex() const { return fMutableState->getQueueFamilyIndex(); } |
| 100 | |
| 101 | void setQueueFamilyIndex(uint32_t queueFamilyIndex) { |
| 102 | fMutableState->setQueueFamilyIndex(queueFamilyIndex); |
| 103 | } |
| 104 | |
Greg Daniel | bae7121 | 2019-03-01 15:24:35 -0500 | [diff] [blame] | 105 | // Returns the image to its original queue family and changes the layout to present if the queue |
| 106 | // family is not external or foreign. |
| 107 | void prepareForPresent(GrVkGpu* gpu); |
| 108 | |
Greg Daniel | 797efca | 2019-05-09 14:04:20 -0400 | [diff] [blame] | 109 | // Returns the image to its original queue family |
| 110 | void prepareForExternal(GrVkGpu* gpu); |
| 111 | |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 112 | // This simply updates our tracking of the image layout and does not actually do any gpu work. |
| 113 | // This is only used for mip map generation where we are manually changing the layouts as we |
| 114 | // blit each layer, and then at the end need to update our tracking. |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 115 | void updateImageLayout(VkImageLayout newLayout) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 116 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 117 | // a RT in an external secondary command buffer. |
| 118 | SkASSERT(fResource); |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 119 | fMutableState->setImageLayout(newLayout); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 120 | } |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 121 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 122 | struct ImageDesc { |
| 123 | VkImageType fImageType; |
| 124 | VkFormat fFormat; |
| 125 | uint32_t fWidth; |
| 126 | uint32_t fHeight; |
| 127 | uint32_t fLevels; |
| 128 | uint32_t fSamples; |
| 129 | VkImageTiling fImageTiling; |
| 130 | VkImageUsageFlags fUsageFlags; |
| 131 | VkFlags fMemProps; |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 132 | GrProtected fIsProtected; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 133 | |
| 134 | ImageDesc() |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 135 | : fImageType(VK_IMAGE_TYPE_2D) |
| 136 | , fFormat(VK_FORMAT_UNDEFINED) |
| 137 | , fWidth(0) |
| 138 | , fHeight(0) |
| 139 | , fLevels(1) |
| 140 | , fSamples(1) |
| 141 | , fImageTiling(VK_IMAGE_TILING_OPTIMAL) |
| 142 | , fUsageFlags(0) |
| 143 | , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) |
| 144 | , fIsProtected(GrProtected::kNo) {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 145 | }; |
| 146 | |
Greg Daniel | e643da6 | 2019-11-05 12:36:42 -0500 | [diff] [blame] | 147 | static bool InitImageInfo(GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 148 | // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo |
| 149 | static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 150 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 151 | // These match the definitions in SkImage, for whence they came |
| 152 | typedef void* ReleaseCtx; |
| 153 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 154 | |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 155 | void setResourceRelease(sk_sp<GrRefCntedCallback> releaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 156 | |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 157 | // Helpers to use for setting the layout of the VkImage |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 158 | static VkPipelineStageFlags LayoutToPipelineSrcStageFlags(const VkImageLayout layout); |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 159 | static VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
| 160 | |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame] | 161 | #if GR_TEST_UTILS |
| 162 | void setCurrentQueueFamilyToGraphicsQueue(GrVkGpu* gpu); |
| 163 | #endif |
| 164 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 165 | protected: |
Greg Daniel | 03535f4 | 2020-06-05 14:18:42 -0400 | [diff] [blame] | 166 | void releaseImage(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 167 | bool hasResource() const { return fResource; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 168 | |
Greg Daniel | 6c6caf4 | 2020-05-29 12:11:05 -0400 | [diff] [blame] | 169 | GrVkImageInfo fInfo; |
| 170 | uint32_t fInitialQueueFamily; |
| 171 | sk_sp<GrBackendSurfaceMutableStateImpl> fMutableState; |
| 172 | bool fIsBorrowed; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 173 | |
| 174 | private: |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 175 | class Resource : public GrTextureResource { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 176 | public: |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 177 | explicit Resource(const GrVkGpu* gpu) |
| 178 | : fGpu(gpu) |
| 179 | , fImage(VK_NULL_HANDLE) { |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 180 | fAlloc.fMemory = VK_NULL_HANDLE; |
| 181 | fAlloc.fOffset = 0; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 184 | Resource(const GrVkGpu* gpu, VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
| 185 | : fGpu(gpu) |
| 186 | , fImage(image) |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 187 | , fAlloc(alloc) |
| 188 | , fImageTiling(tiling) {} |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 189 | |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 190 | ~Resource() override {} |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 191 | |
Jim Van Verth | 3e19216 | 2020-03-10 16:23:16 -0400 | [diff] [blame] | 192 | #ifdef SK_TRACE_MANAGED_RESOURCES |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 193 | void dumpInfo() const override { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 194 | SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 195 | } |
| 196 | #endif |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 197 | |
Greg Daniel | d922f33 | 2020-04-27 11:21:36 -0400 | [diff] [blame] | 198 | #ifdef SK_DEBUG |
| 199 | const GrManagedResource* asVkImageResource() const override { return this; } |
| 200 | #endif |
| 201 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 202 | private: |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 203 | void freeGPUData() const override; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 204 | |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 205 | const GrVkGpu* fGpu; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 206 | VkImage fImage; |
| 207 | GrVkAlloc fAlloc; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 208 | VkImageTiling fImageTiling; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 209 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 210 | using INHERITED = GrTextureResource; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | // for wrapped textures |
| 214 | class BorrowedResource : public Resource { |
| 215 | public: |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 216 | BorrowedResource(const GrVkGpu* gpu, VkImage image, const GrVkAlloc& alloc, |
| 217 | VkImageTiling tiling) |
| 218 | : Resource(gpu, image, alloc, tiling) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 219 | } |
| 220 | private: |
Jim Van Verth | 5082df1 | 2020-03-11 16:14:51 -0400 | [diff] [blame] | 221 | void freeGPUData() const override; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 224 | Resource* fResource; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 225 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 226 | friend class GrVkRenderTarget; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | #endif |