blob: bd443e44598389995fbf8f8def5dc6c6c8cae483 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkTexture.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 Daniel164a9f02016-02-22 09:56:40 -050015
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/gpu/vk/GrVkTypes.h"
jvanverthfd359ca2016-03-18 11:57:24 -070017
Greg Daniel164a9f02016-02-22 09:56:40 -050018#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.
21GrVkTexture::GrVkTexture(GrVkGpu* gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -070022 SkBudgeted budgeted,
Greg Daniel164a9f02016-02-22 09:56:40 -050023 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070024 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040025 sk_sp<GrVkImageLayout> layout,
Greg Daniel834f1202017-10-09 15:06:20 -040026 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040027 GrMipMapsStatus mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040028 : GrSurface(gpu, desc)
29 , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned)
Brian Salomone632dfc2018-08-01 13:01:16 -040030 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040031 , fTextureView(view) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040032 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
kkinnunen2e6055b2016-04-22 01:48:29 -070033 this->registerWithCache(budgeted);
Jim Van Verth1676cb92019-01-15 13:24:45 -050034 if (GrPixelConfigIsCompressed(desc.fConfig)) {
35 this->setReadOnly();
36 }
kkinnunen2e6055b2016-04-22 01:48:29 -070037}
38
Brian Salomonfa2ebea2019-01-24 15:58:58 -050039GrVkTexture::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 Salomon60dd8c72018-07-30 10:24:13 -040043 : GrSurface(gpu, desc)
44 , GrVkImage(info, std::move(layout), ownership)
Brian Salomone632dfc2018-08-01 13:01:16 -040045 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040046 , fTextureView(view) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040047 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Brian Salomonc67c31c2018-12-06 10:00:03 -050048 if (ioType == kRead_GrIOType) {
49 this->setReadOnly();
50 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050051 this->registerWithCacheWrapped(cacheable);
Greg Daniel164a9f02016-02-22 09:56:40 -050052}
53
54// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
55GrVkTexture::GrVkTexture(GrVkGpu* gpu,
56 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070057 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040058 sk_sp<GrVkImageLayout> layout,
egdanielb2df0c22016-05-13 11:30:37 -070059 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040060 GrMipMapsStatus mipMapsStatus,
61 GrBackendObjectOwnership ownership)
Brian Salomon60dd8c72018-07-30 10:24:13 -040062 : GrSurface(gpu, desc)
63 , GrVkImage(info, layout, ownership)
Brian Salomone632dfc2018-08-01 13:01:16 -040064 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040065 , fTextureView(view) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040066 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Greg Daniel164a9f02016-02-22 09:56:40 -050067}
68
Greg Daniel475eb702018-09-28 14:16:50 -040069sk_sp<GrVkTexture> GrVkTexture::MakeNewTexture(GrVkGpu* gpu, SkBudgeted budgeted,
70 const GrSurfaceDesc& desc,
71 const GrVkImage::ImageDesc& imageDesc,
72 GrMipMapsStatus mipMapsStatus) {
Greg Daniel164a9f02016-02-22 09:56:40 -050073 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT);
74
egdanielb2df0c22016-05-13 11:30:37 -070075 GrVkImageInfo info;
76 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050077 return nullptr;
78 }
79
Greg Daniel7e000222018-12-03 10:08:21 -050080 const GrVkImageView* imageView = GrVkImageView::Create(
81 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
82 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -070083 if (!imageView) {
84 GrVkImage::DestroyImageInfo(gpu, &info);
85 return nullptr;
86 }
Greg Daniel52e16d92018-04-10 09:34:07 -040087 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
Greg Daniel164a9f02016-02-22 09:56:40 -050088
Greg Daniel52e16d92018-04-10 09:34:07 -040089 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, budgeted, desc, info, std::move(layout),
90 imageView, mipMapsStatus));
Greg Daniel164a9f02016-02-22 09:56:40 -050091}
92
Brian Salomonfa2ebea2019-01-24 15:58:58 -050093sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(GrVkGpu* gpu, const GrSurfaceDesc& desc,
Greg Daniel1591c382017-08-17 15:37:20 -040094 GrWrapOwnership wrapOwnership,
Brian Salomonfa2ebea2019-01-24 15:58:58 -050095 GrWrapCacheable cacheable, GrIOType ioType,
Greg Daniel52e16d92018-04-10 09:34:07 -040096 const GrVkImageInfo& info,
97 sk_sp<GrVkImageLayout> layout) {
jvanverthfd359ca2016-03-18 11:57:24 -070098 // Wrapped textures require both image and allocation (because they can be mapped)
Greg Daniel52e16d92018-04-10 09:34:07 -040099 SkASSERT(VK_NULL_HANDLE != info.fImage && VK_NULL_HANDLE != info.fAlloc.fMemory);
Greg Daniel164a9f02016-02-22 09:56:40 -0500100
Greg Daniel7e000222018-12-03 10:08:21 -0500101 const GrVkImageView* imageView = GrVkImageView::Create(
102 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
103 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -0700104 if (!imageView) {
jvanverthfd359ca2016-03-18 11:57:24 -0700105 return nullptr;
106 }
107
Greg Daniel52e16d92018-04-10 09:34:07 -0400108 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kValid
109 : GrMipMapsStatus::kNotAllocated;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400110
Greg Daniel1591c382017-08-17 15:37:20 -0400111 GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership
112 ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned;
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500113 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, desc, info, std::move(layout), imageView,
114 mipMapsStatus, ownership, cacheable, ioType));
Greg Daniel164a9f02016-02-22 09:56:40 -0500115}
116
117GrVkTexture::~GrVkTexture() {
118 // either release or abandon should have been called by the owner of this object.
119 SkASSERT(!fTextureView);
120}
121
122void GrVkTexture::onRelease() {
Brian Salomone80b8092019-03-08 13:25:19 -0500123 // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to
124 // decide who will handle them. If the resource is still tied to a command buffer we let it
125 // handle them. Otherwise, we handle them.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500126 if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) {
Brian Salomone80b8092019-03-08 13:25:19 -0500127 this->removeFinishIdleProcs();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500128 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500129
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 // we create this and don't hand it off, so we should always destroy it
131 if (fTextureView) {
132 fTextureView->unref(this->getVkGpu());
133 fTextureView = nullptr;
134 }
135
kkinnunen2e6055b2016-04-22 01:48:29 -0700136 this->releaseImage(this->getVkGpu());
Greg Daniel164a9f02016-02-22 09:56:40 -0500137
138 INHERITED::onRelease();
139}
140
141void GrVkTexture::onAbandon() {
Brian Salomone80b8092019-03-08 13:25:19 -0500142 // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to
143 // decide who will handle them. If the resource is still tied to a command buffer we let it
144 // handle them. Otherwise, we handle them.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500145 if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) {
Brian Salomone80b8092019-03-08 13:25:19 -0500146 this->removeFinishIdleProcs();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500147 }
148
Brian Salomon614c1a82018-12-19 15:42:06 -0500149 // we create this and don't hand it off, so we should always destroy it
Greg Daniel164a9f02016-02-22 09:56:40 -0500150 if (fTextureView) {
151 fTextureView->unrefAndAbandon();
152 fTextureView = nullptr;
153 }
154
155 this->abandonImage();
156 INHERITED::onAbandon();
157}
158
Robert Phillipsb67821d2017-12-13 15:00:45 -0500159GrBackendTexture GrVkTexture::getBackendTexture() const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400160 return GrBackendTexture(this->width(), this->height(), fInfo, this->grVkImageLayout());
Robert Phillipsb67821d2017-12-13 15:00:45 -0500161}
162
Greg Daniel164a9f02016-02-22 09:56:40 -0500163GrVkGpu* GrVkTexture::getVkGpu() const {
164 SkASSERT(!this->wasDestroyed());
165 return static_cast<GrVkGpu*>(this->getGpu());
166}
jvanverth62340062016-04-26 08:01:44 -0700167
Brian Osman2b23c4b2018-06-01 12:25:08 -0400168const GrVkImageView* GrVkTexture::textureView() {
169 return fTextureView;
brianosmanf05ab1b2016-05-12 11:01:10 -0700170}
171
Brian Salomone80b8092019-03-08 13:25:19 -0500172void GrVkTexture::addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState type) {
173 INHERITED::addIdleProc(idleProc, type);
174 if (type == IdleState::kFinished) {
175 if (auto* resource = this->resource()) {
176 resource->addIdleProc(this, std::move(idleProc));
177 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500178 }
179}
180
Brian Salomone80b8092019-03-08 13:25:19 -0500181void GrVkTexture::callIdleProcsOnBehalfOfResource() {
182 // If we got here then the resource is being removed from its last command buffer and the
183 // texture is idle in the cache. Any kFlush idle procs should already have been called. So
184 // the texture and resource should have the same set of procs.
185 SkASSERT(this->resource());
186 SkASSERT(this->resource()->idleProcCnt() == fIdleProcs.count());
187#ifdef SK_DEBUG
188 for (int i = 0; i < fIdleProcs.count(); ++i) {
189 SkASSERT(fIdleProcs[i] == this->resource()->idleProc(i));
190 }
191#endif
192 fIdleProcs.reset();
193 this->resource()->resetIdleProcs();
194}
195
Brian Salomon8cabb322019-02-22 10:44:19 -0500196void GrVkTexture::willRemoveLastRefOrPendingIO() {
Brian Salomone80b8092019-03-08 13:25:19 -0500197 if (!fIdleProcs.count()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500198 return;
199 }
200 // This is called when the GrTexture is purgeable. However, we need to check whether the
201 // Resource is still owned by any command buffers. If it is then it will call the proc.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500202 auto* resource = this->hasResource() ? this->resource() : nullptr;
Brian Salomone80b8092019-03-08 13:25:19 -0500203 bool callFinishProcs = !resource || !resource->isOwnedByCommandBuffer();
204 if (callFinishProcs) {
205 // Everything must go!
206 fIdleProcs.reset();
207 resource->resetIdleProcs();
208 } else {
209 // The procs that should be called on flush but not finish are those that are owned
210 // by the GrVkTexture and not the Resource. We do this by copying the resource's array
211 // and thereby dropping refs to procs we own but the resource does not.
212 SkASSERT(resource);
213 fIdleProcs.reset(resource->idleProcCnt());
214 for (int i = 0; i < fIdleProcs.count(); ++i) {
215 fIdleProcs[i] = resource->idleProc(i);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500216 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500217 }
Brian Salomone80b8092019-03-08 13:25:19 -0500218}
219
220void GrVkTexture::removeFinishIdleProcs() {
221 // This should only be called by onRelease/onAbandon when we have already checked for a
222 // resource.
223 const auto* resource = this->resource();
224 SkASSERT(resource);
225 SkSTArray<4, sk_sp<GrRefCntedCallback>> procsToKeep;
226 int resourceIdx = 0;
227 // The idle procs that are common between the GrVkTexture and its Resource should be found in
228 // the same order.
229 for (int i = 0; i < fIdleProcs.count(); ++i) {
230 if (fIdleProcs[i] == resource->idleProc(resourceIdx)) {
231 ++resourceIdx;
232 } else {
233 procsToKeep.push_back(fIdleProcs[i]);
234 }
235 }
236 SkASSERT(resourceIdx == resource->idleProcCnt());
237 fIdleProcs = procsToKeep;
Brian Salomon614c1a82018-12-19 15:42:06 -0500238}