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 | |
| 11 | #include "GrVkResource.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 12 | |
| 13 | #include "GrTypesPriv.h" |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 14 | #include "GrVkImageLayout.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 15 | #include "SkTypes.h" |
| 16 | |
jvanverth | e50f3e7 | 2016-03-28 07:03:06 -0700 | [diff] [blame] | 17 | #include "vk/GrVkDefines.h" |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 18 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | |
| 20 | class GrVkGpu; |
| 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, |
| 28 | GrBackendObjectOwnership ownership) |
| 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 | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 34 | if (fIsBorrowed) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 35 | fResource = new BorrowedResource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 36 | } else { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 37 | fResource = new Resource(info.fImage, info.fAlloc, info.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 39 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 40 | virtual ~GrVkImage(); |
| 41 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 42 | VkImage image() const { return fInfo.fImage; } |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 43 | const GrVkAlloc& alloc() const { return fInfo.fAlloc; } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 44 | VkFormat imageFormat() const { return fInfo.fFormat; } |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 45 | uint32_t mipLevels() const { return fInfo.fLevelCount; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 46 | const Resource* resource() const { return fResource; } |
| 47 | bool isLinearTiled() const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 48 | return SkToBool(VK_IMAGE_TILING_LINEAR == fInfo.fImageTiling); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 49 | } |
Brian Osman | 13dddce | 2017-05-09 13:19:50 -0400 | [diff] [blame] | 50 | bool isBorrowed() const { return fIsBorrowed; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 51 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 52 | sk_sp<GrVkImageLayout> grVkImageLayout() const { return fLayout; } |
| 53 | |
| 54 | VkImageLayout currentLayout() const { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 55 | return fLayout->getImageLayout(); |
| 56 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 57 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 58 | void setImageLayout(const GrVkGpu* gpu, |
| 59 | VkImageLayout newLayout, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 60 | VkAccessFlags dstAccessMask, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 61 | VkPipelineStageFlags dstStageMask, |
Greg Daniel | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 62 | bool byRegion, |
| 63 | bool releaseFamilyQueue = false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 64 | |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 65 | // This simply updates our tracking of the image layout and does not actually do any gpu work. |
| 66 | // This is only used for mip map generation where we are manually changing the layouts as we |
| 67 | // 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] | 68 | void updateImageLayout(VkImageLayout newLayout) { |
| 69 | fLayout->setImageLayout(newLayout); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 70 | } |
Greg Daniel | 31cc731 | 2018-03-05 11:41:06 -0500 | [diff] [blame] | 71 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 72 | struct ImageDesc { |
| 73 | VkImageType fImageType; |
| 74 | VkFormat fFormat; |
| 75 | uint32_t fWidth; |
| 76 | uint32_t fHeight; |
| 77 | uint32_t fLevels; |
| 78 | uint32_t fSamples; |
| 79 | VkImageTiling fImageTiling; |
| 80 | VkImageUsageFlags fUsageFlags; |
| 81 | VkFlags fMemProps; |
| 82 | |
| 83 | ImageDesc() |
| 84 | : fImageType(VK_IMAGE_TYPE_2D) |
| 85 | , fFormat(VK_FORMAT_UNDEFINED) |
| 86 | , fWidth(0) |
| 87 | , fHeight(0) |
| 88 | , fLevels(1) |
| 89 | , fSamples(1) |
| 90 | , fImageTiling(VK_IMAGE_TILING_OPTIMAL) |
| 91 | , fUsageFlags(0) |
| 92 | , fMemProps(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) {} |
| 93 | }; |
| 94 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 95 | static bool InitImageInfo(const GrVkGpu* gpu, const ImageDesc& imageDesc, GrVkImageInfo*); |
| 96 | // Destroys the internal VkImage and VkDeviceMemory in the GrVkImageInfo |
| 97 | static void DestroyImageInfo(const GrVkGpu* gpu, GrVkImageInfo*); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 99 | // These match the definitions in SkImage, for whence they came |
| 100 | typedef void* ReleaseCtx; |
| 101 | typedef void (*ReleaseProc)(ReleaseCtx); |
| 102 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 103 | void setResourceRelease(sk_sp<GrReleaseProcHelper> releaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 104 | |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 105 | // Helpers to use for setting the layout of the VkImage |
Greg Daniel | f7828d0 | 2018-10-09 12:01:32 -0400 | [diff] [blame^] | 106 | static VkPipelineStageFlags LayoutToPipelineSrcStageFlags(const VkImageLayout layout); |
Greg Daniel | 6ddbafc | 2018-05-24 12:34:29 -0400 | [diff] [blame] | 107 | static VkAccessFlags LayoutToSrcAccessMask(const VkImageLayout layout); |
| 108 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 109 | protected: |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 110 | void releaseImage(const GrVkGpu* gpu); |
| 111 | void abandonImage(); |
| 112 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 113 | void setNewResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 114 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 115 | GrVkImageInfo fInfo; |
Greg Daniel | ecddbc0 | 2018-08-30 16:39:34 -0400 | [diff] [blame] | 116 | uint32_t fInitialQueueFamily; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 117 | sk_sp<GrVkImageLayout> fLayout; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 118 | bool fIsBorrowed; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 121 | class Resource : public GrVkResource { |
| 122 | public: |
| 123 | Resource() |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 124 | : fImage(VK_NULL_HANDLE) { |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 125 | fAlloc.fMemory = VK_NULL_HANDLE; |
| 126 | fAlloc.fOffset = 0; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 129 | Resource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 130 | : fImage(image) |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 131 | , fAlloc(alloc) |
| 132 | , fImageTiling(tiling) {} |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 133 | |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 134 | ~Resource() override { |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 135 | SkASSERT(!fReleaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 136 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 137 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 138 | #ifdef SK_TRACE_VK_RESOURCES |
| 139 | void dumpInfo() const override { |
egdaniel | a95220d | 2016-07-21 11:50:37 -0700 | [diff] [blame] | 140 | SkDebugf("GrVkImage: %d (%d refs)\n", fImage, this->getRefCnt()); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 141 | } |
| 142 | #endif |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 143 | void setRelease(sk_sp<GrReleaseProcHelper> releaseHelper) { |
| 144 | fReleaseHelper = std::move(releaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 145 | } |
| 146 | protected: |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 147 | mutable sk_sp<GrReleaseProcHelper> fReleaseHelper; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 148 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 149 | private: |
| 150 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 151 | void abandonGPUData() const override { |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 152 | SkASSERT(!fReleaseHelper); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 153 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 154 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 155 | VkImage fImage; |
| 156 | GrVkAlloc fAlloc; |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 157 | VkImageTiling fImageTiling; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 158 | |
| 159 | typedef GrVkResource INHERITED; |
| 160 | }; |
| 161 | |
| 162 | // for wrapped textures |
| 163 | class BorrowedResource : public Resource { |
| 164 | public: |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 165 | BorrowedResource(VkImage image, const GrVkAlloc& alloc, VkImageTiling tiling) |
| 166 | : Resource(image, alloc, tiling) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 167 | } |
| 168 | private: |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 169 | void invokeReleaseProc() const { |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 170 | if (fReleaseHelper) { |
| 171 | // Depending on the ref count of fReleaseHelper this may or may not actually trigger |
| 172 | // the ReleaseProc to be called. |
| 173 | fReleaseHelper.reset(); |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 177 | void freeGPUData(const GrVkGpu* gpu) const override; |
Greg Daniel | cef213c | 2017-04-21 11:52:27 -0400 | [diff] [blame] | 178 | void abandonGPUData() const override; |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Greg Daniel | 6a0176b | 2018-01-30 09:28:44 -0500 | [diff] [blame] | 181 | Resource* fResource; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 182 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 183 | friend class GrVkRenderTarget; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | #endif |