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