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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkTexture.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrTexturePriv.h" |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 11 | #include "src/gpu/vk/GrVkDescriptorSet.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/vk/GrVkGpu.h" |
| 13 | #include "src/gpu/vk/GrVkImageView.h" |
| 14 | #include "src/gpu/vk/GrVkTextureRenderTarget.h" |
| 15 | #include "src/gpu/vk/GrVkUtil.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 16 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/gpu/vk/GrVkTypes.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 18 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 19 | #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 20 | |
| 21 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 22 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 23 | SkBudgeted budgeted, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 24 | SkISize dimensions, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 25 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 26 | sk_sp<GrVkImageLayout> layout, |
Greg Daniel | 834f120 | 2017-10-09 15:06:20 -0400 | [diff] [blame] | 27 | const GrVkImageView* view, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 28 | GrMipMapsStatus mipMapsStatus) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 29 | : GrSurface(gpu, dimensions, info.fProtected) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 30 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 31 | , INHERITED(gpu, dimensions, info.fProtected, GrTextureType::k2D, mipMapsStatus) |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 32 | , fTextureView(view) |
| 33 | , fDescSetCache(kMaxCachedDescSets) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 34 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount)); |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 35 | // We don't support creating external GrVkTextures |
| 36 | SkASSERT(!info.fYcbcrConversionInfo.isValid() || !info.fYcbcrConversionInfo.fExternalFormat); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 37 | this->registerWithCache(budgeted); |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 38 | if (GrVkFormatIsCompressed(info.fFormat)) { |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 39 | this->setReadOnly(); |
| 40 | } |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 43 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, SkISize dimensions, const GrVkImageInfo& info, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 44 | sk_sp<GrVkImageLayout> layout, const GrVkImageView* view, |
| 45 | GrMipMapsStatus mipMapsStatus, GrBackendObjectOwnership ownership, |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 46 | GrWrapCacheable cacheable, GrIOType ioType, bool isExternal) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 47 | : GrSurface(gpu, dimensions, info.fProtected) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 48 | , GrVkImage(info, std::move(layout), ownership) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 49 | , INHERITED(gpu, dimensions, info.fProtected, |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 50 | isExternal ? GrTextureType::kExternal : GrTextureType::k2D, mipMapsStatus) |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 51 | , fTextureView(view) |
| 52 | , fDescSetCache(kMaxCachedDescSets) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 53 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount)); |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 54 | if (ioType == kRead_GrIOType) { |
| 55 | this->setReadOnly(); |
| 56 | } |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 57 | this->registerWithCacheWrapped(cacheable); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 61 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 62 | SkISize dimensions, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 63 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 64 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 65 | const GrVkImageView* view, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 66 | GrMipMapsStatus mipMapsStatus, |
| 67 | GrBackendObjectOwnership ownership) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 68 | : GrSurface(gpu, dimensions, info.fProtected) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 69 | , GrVkImage(info, layout, ownership) |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 70 | , INHERITED(gpu, dimensions, info.fProtected, GrTextureType::k2D, mipMapsStatus) |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 71 | , fTextureView(view) |
| 72 | , fDescSetCache(kMaxCachedDescSets) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 73 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount)); |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 74 | // Since this ctor is only called from GrVkTextureRenderTarget, we can't have a ycbcr conversion |
| 75 | // since we don't support that on render targets. |
| 76 | SkASSERT(!info.fYcbcrConversionInfo.isValid()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 79 | sk_sp<GrVkTexture> GrVkTexture::MakeNewTexture(GrVkGpu* gpu, SkBudgeted budgeted, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 80 | SkISize dimensions, |
Greg Daniel | 475eb70 | 2018-09-28 14:16:50 -0400 | [diff] [blame] | 81 | const GrVkImage::ImageDesc& imageDesc, |
| 82 | GrMipMapsStatus mipMapsStatus) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 83 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 84 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 85 | GrVkImageInfo info; |
| 86 | if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
| 89 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 90 | const GrVkImageView* imageView = GrVkImageView::Create( |
| 91 | gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount, |
| 92 | info.fYcbcrConversionInfo); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 93 | if (!imageView) { |
| 94 | GrVkImage::DestroyImageInfo(gpu, &info); |
| 95 | return nullptr; |
| 96 | } |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 97 | sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 99 | return sk_sp<GrVkTexture>(new GrVkTexture(gpu, budgeted, dimensions, info, std::move(layout), |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 100 | imageView, mipMapsStatus)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 103 | sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(GrVkGpu* gpu, |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 104 | SkISize dimensions, |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 105 | GrWrapOwnership wrapOwnership, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 106 | GrWrapCacheable cacheable, |
| 107 | GrIOType ioType, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 108 | const GrVkImageInfo& info, |
| 109 | sk_sp<GrVkImageLayout> layout) { |
Jim Van Verth | 658d499 | 2019-07-11 14:07:53 -0400 | [diff] [blame] | 110 | // Adopted textures require both image and allocation because we're responsible for freeing |
| 111 | SkASSERT(VK_NULL_HANDLE != info.fImage && |
| 112 | (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 113 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 114 | const GrVkImageView* imageView = GrVkImageView::Create( |
| 115 | gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount, |
| 116 | info.fYcbcrConversionInfo); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 117 | if (!imageView) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 118 | return nullptr; |
| 119 | } |
| 120 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 121 | GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kValid |
| 122 | : GrMipMapsStatus::kNotAllocated; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 123 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 124 | GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership |
| 125 | ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned; |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 126 | bool isExternal = info.fYcbcrConversionInfo.isValid() && |
| 127 | (info.fYcbcrConversionInfo.fExternalFormat != 0); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 128 | return sk_sp<GrVkTexture>(new GrVkTexture(gpu, dimensions, info, std::move(layout), imageView, |
Greg Daniel | a7b7fe0 | 2019-09-26 15:18:32 -0400 | [diff] [blame] | 129 | mipMapsStatus, ownership, cacheable, ioType, |
| 130 | isExternal)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | GrVkTexture::~GrVkTexture() { |
| 134 | // either release or abandon should have been called by the owner of this object. |
| 135 | SkASSERT(!fTextureView); |
| 136 | } |
| 137 | |
| 138 | void GrVkTexture::onRelease() { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 139 | // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to |
| 140 | // decide who will handle them. If the resource is still tied to a command buffer we let it |
| 141 | // handle them. Otherwise, we handle them. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 142 | if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 143 | this->removeFinishIdleProcs(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 144 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 145 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 146 | // we create this and don't hand it off, so we should always destroy it |
| 147 | if (fTextureView) { |
| 148 | fTextureView->unref(this->getVkGpu()); |
| 149 | fTextureView = nullptr; |
| 150 | } |
| 151 | |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 152 | fDescSetCache.reset(); |
| 153 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 154 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 155 | |
| 156 | INHERITED::onRelease(); |
| 157 | } |
| 158 | |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 159 | struct GrVkTexture::DescriptorCacheEntry { |
| 160 | DescriptorCacheEntry(const GrVkDescriptorSet* fDescSet, GrVkGpu* gpu) |
| 161 | : fDescriptorSet(fDescSet), fGpu(gpu) {} |
| 162 | ~DescriptorCacheEntry() { |
| 163 | if (fDescriptorSet) { |
| 164 | fDescriptorSet->recycle(fGpu); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | const GrVkDescriptorSet* fDescriptorSet; |
| 169 | GrVkGpu* fGpu; |
| 170 | }; |
| 171 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 172 | void GrVkTexture::onAbandon() { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 173 | // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to |
| 174 | // decide who will handle them. If the resource is still tied to a command buffer we let it |
| 175 | // handle them. Otherwise, we handle them. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 176 | if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 177 | this->removeFinishIdleProcs(); |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 180 | // we create this and don't hand it off, so we should always destroy it |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 181 | if (fTextureView) { |
Greg Daniel | f0e04f0 | 2019-12-04 15:17:54 -0500 | [diff] [blame] | 182 | fTextureView->unref(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 183 | fTextureView = nullptr; |
| 184 | } |
| 185 | |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 186 | fDescSetCache.reset(); |
| 187 | |
Greg Daniel | f0e04f0 | 2019-12-04 15:17:54 -0500 | [diff] [blame] | 188 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 189 | INHERITED::onAbandon(); |
| 190 | } |
| 191 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 192 | GrBackendTexture GrVkTexture::getBackendTexture() const { |
Brian Salomon | 4456a0d | 2019-07-18 15:05:11 -0400 | [diff] [blame] | 193 | return GrBackendTexture(this->width(), this->height(), fInfo, this->grVkImageLayout()); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 196 | GrVkGpu* GrVkTexture::getVkGpu() const { |
| 197 | SkASSERT(!this->wasDestroyed()); |
| 198 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 199 | } |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 200 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 201 | const GrVkImageView* GrVkTexture::textureView() { |
| 202 | return fTextureView; |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 205 | void GrVkTexture::addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState type) { |
| 206 | INHERITED::addIdleProc(idleProc, type); |
| 207 | if (type == IdleState::kFinished) { |
| 208 | if (auto* resource = this->resource()) { |
| 209 | resource->addIdleProc(this, std::move(idleProc)); |
| 210 | } |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 214 | void GrVkTexture::callIdleProcsOnBehalfOfResource() { |
| 215 | // If we got here then the resource is being removed from its last command buffer and the |
| 216 | // texture is idle in the cache. Any kFlush idle procs should already have been called. So |
| 217 | // the texture and resource should have the same set of procs. |
| 218 | SkASSERT(this->resource()); |
| 219 | SkASSERT(this->resource()->idleProcCnt() == fIdleProcs.count()); |
| 220 | #ifdef SK_DEBUG |
| 221 | for (int i = 0; i < fIdleProcs.count(); ++i) { |
| 222 | SkASSERT(fIdleProcs[i] == this->resource()->idleProc(i)); |
| 223 | } |
| 224 | #endif |
| 225 | fIdleProcs.reset(); |
| 226 | this->resource()->resetIdleProcs(); |
| 227 | } |
| 228 | |
Robert Phillips | bf8bf83 | 2019-08-30 13:13:44 -0400 | [diff] [blame] | 229 | void GrVkTexture::willRemoveLastRef() { |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 230 | if (!fIdleProcs.count()) { |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | // This is called when the GrTexture is purgeable. However, we need to check whether the |
| 234 | // Resource is still owned by any command buffers. If it is then it will call the proc. |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 235 | auto* resource = this->hasResource() ? this->resource() : nullptr; |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 236 | bool callFinishProcs = !resource || !resource->isOwnedByCommandBuffer(); |
| 237 | if (callFinishProcs) { |
| 238 | // Everything must go! |
| 239 | fIdleProcs.reset(); |
| 240 | resource->resetIdleProcs(); |
| 241 | } else { |
| 242 | // The procs that should be called on flush but not finish are those that are owned |
| 243 | // by the GrVkTexture and not the Resource. We do this by copying the resource's array |
| 244 | // and thereby dropping refs to procs we own but the resource does not. |
| 245 | SkASSERT(resource); |
| 246 | fIdleProcs.reset(resource->idleProcCnt()); |
| 247 | for (int i = 0; i < fIdleProcs.count(); ++i) { |
| 248 | fIdleProcs[i] = resource->idleProc(i); |
Brian Salomon | b2c5dae | 2019-03-04 10:25:17 -0500 | [diff] [blame] | 249 | } |
Brian Salomon | 9bc76d9 | 2019-01-24 12:18:33 -0500 | [diff] [blame] | 250 | } |
Brian Salomon | e80b809 | 2019-03-08 13:25:19 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void GrVkTexture::removeFinishIdleProcs() { |
| 254 | // This should only be called by onRelease/onAbandon when we have already checked for a |
| 255 | // resource. |
| 256 | const auto* resource = this->resource(); |
| 257 | SkASSERT(resource); |
| 258 | SkSTArray<4, sk_sp<GrRefCntedCallback>> procsToKeep; |
| 259 | int resourceIdx = 0; |
| 260 | // The idle procs that are common between the GrVkTexture and its Resource should be found in |
| 261 | // the same order. |
| 262 | for (int i = 0; i < fIdleProcs.count(); ++i) { |
| 263 | if (fIdleProcs[i] == resource->idleProc(resourceIdx)) { |
| 264 | ++resourceIdx; |
| 265 | } else { |
| 266 | procsToKeep.push_back(fIdleProcs[i]); |
| 267 | } |
| 268 | } |
| 269 | SkASSERT(resourceIdx == resource->idleProcCnt()); |
| 270 | fIdleProcs = procsToKeep; |
Brian Salomon | 614c1a8 | 2018-12-19 15:42:06 -0500 | [diff] [blame] | 271 | } |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 272 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 273 | const GrVkDescriptorSet* GrVkTexture::cachedSingleDescSet(GrSamplerState state) { |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 274 | if (std::unique_ptr<DescriptorCacheEntry>* e = fDescSetCache.find(state)) { |
| 275 | return (*e)->fDescriptorSet; |
| 276 | } |
| 277 | return nullptr; |
| 278 | } |
| 279 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 280 | void GrVkTexture::addDescriptorSetToCache(const GrVkDescriptorSet* descSet, GrSamplerState state) { |
Greg Daniel | 793c9e8 | 2019-10-29 10:07:08 -0400 | [diff] [blame] | 281 | SkASSERT(!fDescSetCache.find(state)); |
| 282 | descSet->ref(); |
| 283 | fDescSetCache.insert(state, |
| 284 | std::unique_ptr<DescriptorCacheEntry>( |
| 285 | new DescriptorCacheEntry(descSet, this->getVkGpu()))); |
| 286 | } |