blob: 1257c287fefd31b4f600996fd6208d25fa879c07 [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"
Greg Daniel793c9e82019-10-29 10:07:08 -040011#include "src/gpu/vk/GrVkDescriptorSet.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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 Daniel164a9f02016-02-22 09:56:40 -050016
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/vk/GrVkTypes.h"
jvanverthfd359ca2016-03-18 11:57:24 -070018
Greg Daniel164a9f02016-02-22 09:56:40 -050019#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.
22GrVkTexture::GrVkTexture(GrVkGpu* gpu,
kkinnunen2e6055b2016-04-22 01:48:29 -070023 SkBudgeted budgeted,
Greg Daniel164a9f02016-02-22 09:56:40 -050024 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070025 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040026 sk_sp<GrVkImageLayout> layout,
Greg Daniel834f1202017-10-09 15:06:20 -040027 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040028 GrMipMapsStatus mipMapsStatus)
Brian Salomona9c22572019-08-05 12:57:09 -040029 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected)
Brian Salomon60dd8c72018-07-30 10:24:13 -040030 , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned)
Brian Salomona9c22572019-08-05 12:57:09 -040031 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected,
32 GrTextureType::k2D, mipMapsStatus)
Greg Daniel793c9e82019-10-29 10:07:08 -040033 , fTextureView(view)
34 , fDescSetCache(kMaxCachedDescSets) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040035 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Greg Daniela7b7fe02019-09-26 15:18:32 -040036 // We don't support creating external GrVkTextures
37 SkASSERT(!info.fYcbcrConversionInfo.isValid() || !info.fYcbcrConversionInfo.fExternalFormat);
kkinnunen2e6055b2016-04-22 01:48:29 -070038 this->registerWithCache(budgeted);
Jim Van Verthe3671012019-09-18 09:53:31 -040039 if (GrVkFormatIsCompressed(info.fFormat)) {
Jim Van Verth1676cb92019-01-15 13:24:45 -050040 this->setReadOnly();
41 }
kkinnunen2e6055b2016-04-22 01:48:29 -070042}
43
Brian Salomonfa2ebea2019-01-24 15:58:58 -050044GrVkTexture::GrVkTexture(GrVkGpu* gpu, const GrSurfaceDesc& desc, const GrVkImageInfo& info,
45 sk_sp<GrVkImageLayout> layout, const GrVkImageView* view,
46 GrMipMapsStatus mipMapsStatus, GrBackendObjectOwnership ownership,
Greg Daniela7b7fe02019-09-26 15:18:32 -040047 GrWrapCacheable cacheable, GrIOType ioType, bool isExternal)
Brian Salomona9c22572019-08-05 12:57:09 -040048 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected)
Brian Salomon60dd8c72018-07-30 10:24:13 -040049 , GrVkImage(info, std::move(layout), ownership)
Brian Salomona9c22572019-08-05 12:57:09 -040050 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected,
Greg Daniela7b7fe02019-09-26 15:18:32 -040051 isExternal ? GrTextureType::kExternal : GrTextureType::k2D, mipMapsStatus)
Greg Daniel793c9e82019-10-29 10:07:08 -040052 , fTextureView(view)
53 , fDescSetCache(kMaxCachedDescSets) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040054 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Brian Salomonc67c31c2018-12-06 10:00:03 -050055 if (ioType == kRead_GrIOType) {
56 this->setReadOnly();
57 }
Brian Salomonfa2ebea2019-01-24 15:58:58 -050058 this->registerWithCacheWrapped(cacheable);
Greg Daniel164a9f02016-02-22 09:56:40 -050059}
60
61// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
62GrVkTexture::GrVkTexture(GrVkGpu* gpu,
63 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070064 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040065 sk_sp<GrVkImageLayout> layout,
egdanielb2df0c22016-05-13 11:30:37 -070066 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040067 GrMipMapsStatus mipMapsStatus,
68 GrBackendObjectOwnership ownership)
Brian Salomona9c22572019-08-05 12:57:09 -040069 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected)
Brian Salomon60dd8c72018-07-30 10:24:13 -040070 , GrVkImage(info, layout, ownership)
Brian Salomona9c22572019-08-05 12:57:09 -040071 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, info.fProtected,
72 GrTextureType::k2D, mipMapsStatus)
Greg Daniel793c9e82019-10-29 10:07:08 -040073 , fTextureView(view)
74 , fDescSetCache(kMaxCachedDescSets) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040075 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Greg Daniela7b7fe02019-09-26 15:18:32 -040076 // Since this ctor is only called from GrVkTextureRenderTarget, we can't have a ycbcr conversion
77 // since we don't support that on render targets.
78 SkASSERT(!info.fYcbcrConversionInfo.isValid());
Greg Daniel164a9f02016-02-22 09:56:40 -050079}
80
Greg Daniel475eb702018-09-28 14:16:50 -040081sk_sp<GrVkTexture> GrVkTexture::MakeNewTexture(GrVkGpu* gpu, SkBudgeted budgeted,
82 const GrSurfaceDesc& desc,
83 const GrVkImage::ImageDesc& imageDesc,
84 GrMipMapsStatus mipMapsStatus) {
Greg Daniel164a9f02016-02-22 09:56:40 -050085 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT);
86
egdanielb2df0c22016-05-13 11:30:37 -070087 GrVkImageInfo info;
88 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050089 return nullptr;
90 }
91
Greg Daniel7e000222018-12-03 10:08:21 -050092 const GrVkImageView* imageView = GrVkImageView::Create(
93 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
94 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -070095 if (!imageView) {
96 GrVkImage::DestroyImageInfo(gpu, &info);
97 return nullptr;
98 }
Greg Daniel52e16d92018-04-10 09:34:07 -040099 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500100
Greg Daniel52e16d92018-04-10 09:34:07 -0400101 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, budgeted, desc, info, std::move(layout),
102 imageView, mipMapsStatus));
Greg Daniel164a9f02016-02-22 09:56:40 -0500103}
104
Brian Salomone8a766b2019-07-19 14:24:36 -0400105sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(GrVkGpu* gpu,
106 const GrSurfaceDesc& desc,
Greg Daniel1591c382017-08-17 15:37:20 -0400107 GrWrapOwnership wrapOwnership,
Brian Salomone8a766b2019-07-19 14:24:36 -0400108 GrWrapCacheable cacheable,
109 GrIOType ioType,
Greg Daniel52e16d92018-04-10 09:34:07 -0400110 const GrVkImageInfo& info,
111 sk_sp<GrVkImageLayout> layout) {
Jim Van Verth658d4992019-07-11 14:07:53 -0400112 // Adopted textures require both image and allocation because we're responsible for freeing
113 SkASSERT(VK_NULL_HANDLE != info.fImage &&
114 (kBorrow_GrWrapOwnership == wrapOwnership || VK_NULL_HANDLE != info.fAlloc.fMemory));
Greg Daniel164a9f02016-02-22 09:56:40 -0500115
Greg Daniel7e000222018-12-03 10:08:21 -0500116 const GrVkImageView* imageView = GrVkImageView::Create(
117 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
118 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -0700119 if (!imageView) {
jvanverthfd359ca2016-03-18 11:57:24 -0700120 return nullptr;
121 }
122
Greg Daniel52e16d92018-04-10 09:34:07 -0400123 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kValid
124 : GrMipMapsStatus::kNotAllocated;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400125
Greg Daniel1591c382017-08-17 15:37:20 -0400126 GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership
127 ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned;
Greg Daniela7b7fe02019-09-26 15:18:32 -0400128 bool isExternal = info.fYcbcrConversionInfo.isValid() &&
129 (info.fYcbcrConversionInfo.fExternalFormat != 0);
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500130 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, desc, info, std::move(layout), imageView,
Greg Daniela7b7fe02019-09-26 15:18:32 -0400131 mipMapsStatus, ownership, cacheable, ioType,
132 isExternal));
Greg Daniel164a9f02016-02-22 09:56:40 -0500133}
134
135GrVkTexture::~GrVkTexture() {
136 // either release or abandon should have been called by the owner of this object.
137 SkASSERT(!fTextureView);
138}
139
140void GrVkTexture::onRelease() {
Brian Salomone80b8092019-03-08 13:25:19 -0500141 // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to
142 // decide who will handle them. If the resource is still tied to a command buffer we let it
143 // handle them. Otherwise, we handle them.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500144 if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) {
Brian Salomone80b8092019-03-08 13:25:19 -0500145 this->removeFinishIdleProcs();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500146 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500147
Greg Daniel164a9f02016-02-22 09:56:40 -0500148 // we create this and don't hand it off, so we should always destroy it
149 if (fTextureView) {
150 fTextureView->unref(this->getVkGpu());
151 fTextureView = nullptr;
152 }
153
Greg Daniel793c9e82019-10-29 10:07:08 -0400154 fDescSetCache.reset();
155
kkinnunen2e6055b2016-04-22 01:48:29 -0700156 this->releaseImage(this->getVkGpu());
Greg Daniel164a9f02016-02-22 09:56:40 -0500157
158 INHERITED::onRelease();
159}
160
Greg Daniel793c9e82019-10-29 10:07:08 -0400161struct GrVkTexture::DescriptorCacheEntry {
162 DescriptorCacheEntry(const GrVkDescriptorSet* fDescSet, GrVkGpu* gpu)
163 : fDescriptorSet(fDescSet), fGpu(gpu) {}
164 ~DescriptorCacheEntry() {
165 if (fDescriptorSet) {
166 fDescriptorSet->recycle(fGpu);
167 }
168 }
169
170 const GrVkDescriptorSet* fDescriptorSet;
171 GrVkGpu* fGpu;
172};
173
Greg Daniel164a9f02016-02-22 09:56:40 -0500174void GrVkTexture::onAbandon() {
Brian Salomone80b8092019-03-08 13:25:19 -0500175 // We're about to be severed from our GrVkResource. If there are "finish" idle procs we have to
176 // decide who will handle them. If the resource is still tied to a command buffer we let it
177 // handle them. Otherwise, we handle them.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500178 if (this->hasResource() && this->resource()->isOwnedByCommandBuffer()) {
Brian Salomone80b8092019-03-08 13:25:19 -0500179 this->removeFinishIdleProcs();
Brian Salomon9bc76d92019-01-24 12:18:33 -0500180 }
181
Brian Salomon614c1a82018-12-19 15:42:06 -0500182 // we create this and don't hand it off, so we should always destroy it
Greg Daniel164a9f02016-02-22 09:56:40 -0500183 if (fTextureView) {
184 fTextureView->unrefAndAbandon();
185 fTextureView = nullptr;
186 }
187
Greg Daniel793c9e82019-10-29 10:07:08 -0400188 fDescSetCache.foreach ([](std::unique_ptr<DescriptorCacheEntry>* entry) {
189 (*entry)->fDescriptorSet->unrefAndAbandon();
190 (*entry)->fDescriptorSet = nullptr;
191 });
192 fDescSetCache.reset();
193
Greg Daniel164a9f02016-02-22 09:56:40 -0500194 this->abandonImage();
195 INHERITED::onAbandon();
196}
197
Robert Phillipsb67821d2017-12-13 15:00:45 -0500198GrBackendTexture GrVkTexture::getBackendTexture() const {
Brian Salomon4456a0d2019-07-18 15:05:11 -0400199 return GrBackendTexture(this->width(), this->height(), fInfo, this->grVkImageLayout());
Robert Phillipsb67821d2017-12-13 15:00:45 -0500200}
201
Greg Daniel164a9f02016-02-22 09:56:40 -0500202GrVkGpu* GrVkTexture::getVkGpu() const {
203 SkASSERT(!this->wasDestroyed());
204 return static_cast<GrVkGpu*>(this->getGpu());
205}
jvanverth62340062016-04-26 08:01:44 -0700206
Brian Osman2b23c4b2018-06-01 12:25:08 -0400207const GrVkImageView* GrVkTexture::textureView() {
208 return fTextureView;
brianosmanf05ab1b2016-05-12 11:01:10 -0700209}
210
Brian Salomone80b8092019-03-08 13:25:19 -0500211void GrVkTexture::addIdleProc(sk_sp<GrRefCntedCallback> idleProc, IdleState type) {
212 INHERITED::addIdleProc(idleProc, type);
213 if (type == IdleState::kFinished) {
214 if (auto* resource = this->resource()) {
215 resource->addIdleProc(this, std::move(idleProc));
216 }
Brian Salomon614c1a82018-12-19 15:42:06 -0500217 }
218}
219
Brian Salomone80b8092019-03-08 13:25:19 -0500220void GrVkTexture::callIdleProcsOnBehalfOfResource() {
221 // If we got here then the resource is being removed from its last command buffer and the
222 // texture is idle in the cache. Any kFlush idle procs should already have been called. So
223 // the texture and resource should have the same set of procs.
224 SkASSERT(this->resource());
225 SkASSERT(this->resource()->idleProcCnt() == fIdleProcs.count());
226#ifdef SK_DEBUG
227 for (int i = 0; i < fIdleProcs.count(); ++i) {
228 SkASSERT(fIdleProcs[i] == this->resource()->idleProc(i));
229 }
230#endif
231 fIdleProcs.reset();
232 this->resource()->resetIdleProcs();
233}
234
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400235void GrVkTexture::willRemoveLastRef() {
Brian Salomone80b8092019-03-08 13:25:19 -0500236 if (!fIdleProcs.count()) {
Brian Salomon614c1a82018-12-19 15:42:06 -0500237 return;
238 }
239 // This is called when the GrTexture is purgeable. However, we need to check whether the
240 // Resource is still owned by any command buffers. If it is then it will call the proc.
Brian Salomon9bc76d92019-01-24 12:18:33 -0500241 auto* resource = this->hasResource() ? this->resource() : nullptr;
Brian Salomone80b8092019-03-08 13:25:19 -0500242 bool callFinishProcs = !resource || !resource->isOwnedByCommandBuffer();
243 if (callFinishProcs) {
244 // Everything must go!
245 fIdleProcs.reset();
246 resource->resetIdleProcs();
247 } else {
248 // The procs that should be called on flush but not finish are those that are owned
249 // by the GrVkTexture and not the Resource. We do this by copying the resource's array
250 // and thereby dropping refs to procs we own but the resource does not.
251 SkASSERT(resource);
252 fIdleProcs.reset(resource->idleProcCnt());
253 for (int i = 0; i < fIdleProcs.count(); ++i) {
254 fIdleProcs[i] = resource->idleProc(i);
Brian Salomonb2c5dae2019-03-04 10:25:17 -0500255 }
Brian Salomon9bc76d92019-01-24 12:18:33 -0500256 }
Brian Salomone80b8092019-03-08 13:25:19 -0500257}
258
259void GrVkTexture::removeFinishIdleProcs() {
260 // This should only be called by onRelease/onAbandon when we have already checked for a
261 // resource.
262 const auto* resource = this->resource();
263 SkASSERT(resource);
264 SkSTArray<4, sk_sp<GrRefCntedCallback>> procsToKeep;
265 int resourceIdx = 0;
266 // The idle procs that are common between the GrVkTexture and its Resource should be found in
267 // the same order.
268 for (int i = 0; i < fIdleProcs.count(); ++i) {
269 if (fIdleProcs[i] == resource->idleProc(resourceIdx)) {
270 ++resourceIdx;
271 } else {
272 procsToKeep.push_back(fIdleProcs[i]);
273 }
274 }
275 SkASSERT(resourceIdx == resource->idleProcCnt());
276 fIdleProcs = procsToKeep;
Brian Salomon614c1a82018-12-19 15:42:06 -0500277}
Greg Daniel793c9e82019-10-29 10:07:08 -0400278
279const GrVkDescriptorSet* GrVkTexture::cachedSingleDescSet(const GrSamplerState& state) {
280 if (std::unique_ptr<DescriptorCacheEntry>* e = fDescSetCache.find(state)) {
281 return (*e)->fDescriptorSet;
282 }
283 return nullptr;
284}
285
286void GrVkTexture::addDescriptorSetToCache(const GrVkDescriptorSet* descSet,
287 const GrSamplerState& state) {
288 SkASSERT(!fDescSetCache.find(state));
289 descSet->ref();
290 fDescSetCache.insert(state,
291 std::unique_ptr<DescriptorCacheEntry>(
292 new DescriptorCacheEntry(descSet, this->getVkGpu())));
293}
294