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