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 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 11 | #include "GrBackendSurface.h" |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 12 | #include "GrTexture.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 13 | #include "GrTypesPriv.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 14 | #include "GrVkImageLayout.h" |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 15 | #include "GrVkResource.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 16 | #include "SkTypes.h" |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 17 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
| 19 | class GrVkGpu; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 20 | class GrVkTexture; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | |
| 22 | class GrVkImage : SkNoncopyable { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 23 | private: |
| 24 | class Resource; |
| 25 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 26 | public: |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 27 | GrVkImage(const GrVkImageInfo& info, sk_sp<GrVkImageLayout> layout, |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 28 | GrBackendObjectOwnership ownership, bool forSecondaryCB = false) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 29 | : fInfo(info) |
Greg Daniel | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 30 | , fInitialQueueFamily(info.fCurrentQueueFamily) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 31 | , fLayout(std::move(layout)) |
| 32 | , fIsBorrowed(GrBackendObjectOwnership::kBorrowed == ownership) { |
| 33 | SkASSERT(fLayout->getImageLayout() == fInfo.fImageLayout); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 34 | if (forSecondaryCB) { |
| 35 | fResource = nullptr; |
| 36 | } else if (fIsBorrowed) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 37 | fResource = new BorrowedResource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | } else { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 39 | fResource = new Resource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 40 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 41 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 42 | virtual ~GrVkImage(); |
| 43 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 44 | VkImage image() const { |
| 45 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 46 | // a RT in an external secondary command buffer. |
| 47 | SkASSERT(fResource); |
| 48 | return fInfo.fImage; |
| 49 | } |
| 50 | const GrVkAlloc& alloc() const { |
| 51 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 52 | // a RT in an external secondary command buffer. |
| 53 | SkASSERT(fResource); |
| 54 | return fInfo.fAlloc; |
| 55 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 56 | VkFormat imageFormat() const { return fInfo.fFormat; } |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 57 | GrBackendFormat getBackendFormat() const { |
Greg Daniel | 89df784 | 2019-02-21 12:40:21 -0500 | [diff] [blame] | 58 | if (fResource && this->ycbcrConversionInfo().isValid()) { |
| 59 | SkASSERT(this->imageFormat() == VK_FORMAT_UNDEFINED); |
| 60 | return GrBackendFormat::MakeVk(this->ycbcrConversionInfo()); |
| 61 | } |
| 62 | SkASSERT(this->imageFormat() != VK_FORMAT_UNDEFINED); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 63 | return GrBackendFormat::MakeVk(this->imageFormat()); |
| 64 | } |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 65 | uint32_t mipLevels() const { return fInfo.fLevelCount; } |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 66 | const GrVkYcbcrConversionInfo& ycbcrConversionInfo() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 67 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 68 | // a RT in an external secondary command buffer. |
| 69 | SkASSERT(fResource); |
Greg Daniel | 9a51a86 | 2018-11-30 10:18:14 -0500 | [diff] [blame] | 70 | return fInfo.fYcbcrConversionInfo; |
| 71 | } |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 72 | const Resource* resource() const { |
| 73 | SkASSERT(fResource); |
| 74 | return fResource; |
| 75 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 76 | bool isLinearTiled() const { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 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); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 80 | return SkToBool(VK_IMAGE_TILING_LINEAR == fInfo.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 81 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 82 | bool isBorrowed() const { return fIsBorrowed; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 83 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 84 | sk_sp<GrVkImageLayout> grVkImageLayout() const { return fLayout; } |
| 85 | |
| 86 | VkImageLayout currentLayout() const { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 87 | return fLayout->getImageLayout(); |
| 88 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 94 | bool byRegion, |
| 95 | bool releaseFamilyQueue = false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 96 | |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 97 | // This simply updates our tracking of the image layout and does not actually do any gpu work. |
| 98 | // This is only used for mip map generation where we are manually changing the layouts as we |
| 99 | // 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] | 100 | void updateImageLayout(VkImageLayout newLayout) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 101 | // Should only be called when we have a real fResource object, i.e. never when being used as |
| 102 | // a RT in an external secondary command buffer. |
| 103 | SkASSERT(fResource); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 104 | fLayout->setImageLayout(newLayout); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 105 | } |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 106 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 107 | struct ImageDesc { |
| 108 | VkImageType fImageType; |
| 109 | VkFormat fFormat; |
| 110 | uint32_t fWidth; |
| 111 | uint32_t fHeight; |
| 112 | uint32_t fLevels; |
| 113 | uint32_t fSamples; |
| 114 | VkImageTiling fImageTiling; |
| 115 | VkImageUsageFlags fUsageFlags; |
| 116 | VkFlags fMemProps; |
| 117 | |
| 118 | ImageDesc() |
| 119 | : fImageType(VK_IMAGE_TYPE_2D) |
| 120 | , fFormat(VK_FORMAT_UNDEFINED) |
| 121 | , fWidth(0) |
| 122 | , fHeight(0) |
| 123 | , fLevels(1) |
| 124 | , fSamples(1) |
| 125 | , fImageTiling(VK_IMAGE_TILING_OPTIMAL) |
| 126 | , fUsageFlags(0) |
| 127 | , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {} |
| 128 | }; |
| 129 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 130 | static bool InitImageInfo(const GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*); |
| 131 | // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo |
| 132 | static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 133 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 134 | // These match the definitions in SkImage, for whence they came |
| 135 | typedef void* ReleaseCtx; |
| 136 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 137 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 138 | void setResourceRelease(sk_sp<GrReleaseProcHelper> releaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 139 | |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 140 | // Helpers to use for setting the layout of the VkImage |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame] | 141 | static VkPipelineStageFlags LayoutToPipelineSrcStageFlags(const VkImageLayout layout); |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 142 | static VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
| 143 | |
Greg Daniel | 59dc148 | 2019-02-22 10:46:38 -0500 | [diff] [blame^] | 144 | #if GR_TEST_UTILS |
| 145 | void setCurrentQueueFamilyToGraphicsQueue(GrVkGpu* gpu); |
| 146 | #endif |
| 147 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 148 | protected: |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 149 | void releaseImage(GrVkGpu* gpu); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 150 | void abandonImage(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 151 | bool hasResource() const { return fResource; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 152 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 153 | GrVkImageInfo fInfo; |
Greg Daniel | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 154 | uint32_t fInitialQueueFamily; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 155 | sk_sp<GrVkImageLayout> fLayout; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 156 | bool fIsBorrowed; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 157 | |
| 158 | private: |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 159 | class Resource : public GrVkResource { |
| 160 | public: |
| 161 | Resource() |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 162 | : fImage(VK_NULL_HANDLE) { |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 163 | fAlloc.fMemory = VK_NULL_HANDLE; |
| 164 | fAlloc.fOffset = 0; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 165 | } |
| 166 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 167 | Resource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 168 | : fImage(image) |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 169 | , fAlloc(alloc) |
| 170 | , fImageTiling(tiling) {} |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 171 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 172 | ~Resource() override { |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 173 | SkASSERT(!fReleaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 174 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 175 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 176 | #ifdef SK_TRACE_VK_RESOURCES |
| 177 | void dumpInfo() const override { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 178 | SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 179 | } |
| 180 | #endif |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 181 | void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) { |
| 182 | fReleaseHelper = std::move(releaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 183 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 184 | |
| 185 | /** |
| 186 | * These are used to coordinate calling the idle proc between the GrVkTexture and the |
| 187 | * Resource. If the GrVkTexture becomes purgeable and if there are no command buffers |
| 188 | * referring to the Resource then it calls the proc. Otherwise, the Resource calls it |
| 189 | * when the last command buffer reference goes away and the GrVkTexture is purgeable. |
| 190 | */ |
| 191 | void setIdleProc(GrVkTexture* owner, GrTexture::IdleProc, void* context) const; |
| 192 | void removeOwningTexture() const; |
| 193 | |
| 194 | /** |
| 195 | * We track how many outstanding references this Resource has in command buffers and |
| 196 | * when the count reaches zero we call the idle proc. |
| 197 | */ |
| 198 | void notifyAddedToCommandBuffer() const override; |
| 199 | void notifyRemovedFromCommandBuffer() const override; |
| 200 | bool isOwnedByCommandBuffer() const { return fNumCommandBufferOwners > 0; } |
| 201 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 202 | protected: |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 203 | mutable sk_sp<GrReleaseProcHelper> fReleaseHelper; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 204 | |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 205 | void invokeReleaseProc() const { |
| 206 | if (fReleaseHelper) { |
| 207 | // Depending on the ref count of fReleaseHelper this may or may not actually trigger |
| 208 | // the ReleaseProc to be called. |
| 209 | fReleaseHelper.reset(); |
| 210 | } |
| 211 | } |
| 212 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 213 | private: |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 214 | void freeGPUData(GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 215 | void abandonGPUData() const override { |
Brian Salomon | 8cabb32 | 2019-02-22 10:44:19 -0500 | [diff] [blame] | 216 | this->invokeReleaseProc(); |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 217 | SkASSERT(!fReleaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 218 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 219 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 220 | VkImage fImage; |
| 221 | GrVkAlloc fAlloc; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 222 | VkImageTiling fImageTiling; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 223 | mutable int fNumCommandBufferOwners = 0; |
| 224 | mutable GrTexture::IdleProc* fIdleProc = nullptr; |
| 225 | mutable void* fIdleProcContext = nullptr; |
| 226 | mutable GrVkTexture* fOwningTexture = nullptr; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 227 | |
| 228 | typedef GrVkResource INHERITED; |
| 229 | }; |
| 230 | |
| 231 | // for wrapped textures |
| 232 | class BorrowedResource : public Resource { |
| 233 | public: |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 234 | BorrowedResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
| 235 | : Resource(image, alloc, tiling) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 236 | } |
| 237 | private: |
Ethan Nicholas | 8e265a7 | 2018-12-12 16:22:40 -0500 | [diff] [blame] | 238 | void freeGPUData(GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 239 | void abandonGPUData() const override; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 242 | Resource* fResource; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 243 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 244 | friend class GrVkRenderTarget; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | #endif |