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