blob: a295c0409e67042c412460a7b9eb3d637fe24902 [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
8#include "GrVkTexture.h"
Robert Phillipsf95b1752017-08-31 08:56:07 -04009
10#include "GrTexturePriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrVkGpu.h"
12#include "GrVkImageView.h"
egdaniel50ead532016-07-13 14:23:26 -070013#include "GrVkTextureRenderTarget.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkUtil.h"
15
jvanverthfd359ca2016-03-18 11:57:24 -070016#include "vk/GrVkTypes.h"
17
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);
34}
35
36GrVkTexture::GrVkTexture(GrVkGpu* gpu,
37 Wrapped,
38 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070039 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040040 sk_sp<GrVkImageLayout> layout,
egdanielb2df0c22016-05-13 11:30:37 -070041 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040042 GrMipMapsStatus mipMapsStatus,
Greg Daniel2268ad22018-11-15 09:27:38 -050043 GrBackendObjectOwnership ownership,
Brian Salomon796693a2018-12-05 14:36:35 -050044 GrIOType ioType,
Greg Daniel2268ad22018-11-15 09:27:38 -050045 bool purgeImmediately)
Brian Salomon60dd8c72018-07-30 10:24:13 -040046 : GrSurface(gpu, desc)
47 , GrVkImage(info, std::move(layout), ownership)
Brian Salomone632dfc2018-08-01 13:01:16 -040048 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040049 , fTextureView(view) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040050 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Brian Salomon796693a2018-12-05 14:36:35 -050051 if (ioType == kRead_GrIOType) {
52 this->setReadOnly();
53 }
Greg Daniel2268ad22018-11-15 09:27:38 -050054 this->registerWithCacheWrapped(purgeImmediately);
Greg Daniel164a9f02016-02-22 09:56:40 -050055}
56
57// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
58GrVkTexture::GrVkTexture(GrVkGpu* gpu,
59 const GrSurfaceDesc& desc,
egdanielb2df0c22016-05-13 11:30:37 -070060 const GrVkImageInfo& info,
Greg Daniel52e16d92018-04-10 09:34:07 -040061 sk_sp<GrVkImageLayout> layout,
egdanielb2df0c22016-05-13 11:30:37 -070062 const GrVkImageView* view,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040063 GrMipMapsStatus mipMapsStatus,
64 GrBackendObjectOwnership ownership)
Brian Salomon60dd8c72018-07-30 10:24:13 -040065 : GrSurface(gpu, desc)
66 , GrVkImage(info, layout, ownership)
Brian Salomone632dfc2018-08-01 13:01:16 -040067 , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus)
Brian Salomon60dd8c72018-07-30 10:24:13 -040068 , fTextureView(view) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040069 SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount));
Greg Daniel164a9f02016-02-22 09:56:40 -050070}
71
Greg Daniel475eb702018-09-28 14:16:50 -040072sk_sp<GrVkTexture> GrVkTexture::MakeNewTexture(GrVkGpu* gpu, SkBudgeted budgeted,
73 const GrSurfaceDesc& desc,
74 const GrVkImage::ImageDesc& imageDesc,
75 GrMipMapsStatus mipMapsStatus) {
Greg Daniel164a9f02016-02-22 09:56:40 -050076 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT);
77
egdanielb2df0c22016-05-13 11:30:37 -070078 GrVkImageInfo info;
79 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
Greg Daniel164a9f02016-02-22 09:56:40 -050080 return nullptr;
81 }
82
Greg Daniel7e000222018-12-03 10:08:21 -050083 const GrVkImageView* imageView = GrVkImageView::Create(
84 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
85 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -070086 if (!imageView) {
87 GrVkImage::DestroyImageInfo(gpu, &info);
88 return nullptr;
89 }
Greg Daniel52e16d92018-04-10 09:34:07 -040090 sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
Greg Daniel164a9f02016-02-22 09:56:40 -050091
Greg Daniel52e16d92018-04-10 09:34:07 -040092 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, budgeted, desc, info, std::move(layout),
93 imageView, mipMapsStatus));
Greg Daniel164a9f02016-02-22 09:56:40 -050094}
95
bungeman6bd52842016-10-27 09:30:08 -070096sk_sp<GrVkTexture> GrVkTexture::MakeWrappedTexture(GrVkGpu* gpu,
97 const GrSurfaceDesc& desc,
Greg Daniel1591c382017-08-17 15:37:20 -040098 GrWrapOwnership wrapOwnership,
Brian Salomon796693a2018-12-05 14:36:35 -050099 GrIOType ioType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500100 bool purgeImmediately,
Greg Daniel52e16d92018-04-10 09:34:07 -0400101 const GrVkImageInfo& info,
102 sk_sp<GrVkImageLayout> layout) {
jvanverthfd359ca2016-03-18 11:57:24 -0700103 // Wrapped textures require both image and allocation (because they can be mapped)
Greg Daniel52e16d92018-04-10 09:34:07 -0400104 SkASSERT(VK_NULL_HANDLE != info.fImage && VK_NULL_HANDLE != info.fAlloc.fMemory);
Greg Daniel164a9f02016-02-22 09:56:40 -0500105
Greg Daniel7e000222018-12-03 10:08:21 -0500106 const GrVkImageView* imageView = GrVkImageView::Create(
107 gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount,
108 info.fYcbcrConversionInfo);
egdanielb2df0c22016-05-13 11:30:37 -0700109 if (!imageView) {
jvanverthfd359ca2016-03-18 11:57:24 -0700110 return nullptr;
111 }
112
Greg Daniel52e16d92018-04-10 09:34:07 -0400113 GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kValid
114 : GrMipMapsStatus::kNotAllocated;
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400115
Greg Daniel1591c382017-08-17 15:37:20 -0400116 GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership
117 ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned;
Greg Daniel52e16d92018-04-10 09:34:07 -0400118 return sk_sp<GrVkTexture>(new GrVkTexture(gpu, kWrapped, desc, info, std::move(layout),
Brian Salomon796693a2018-12-05 14:36:35 -0500119 imageView, mipMapsStatus, ownership, ioType,
Greg Daniel2268ad22018-11-15 09:27:38 -0500120 purgeImmediately));
Greg Daniel164a9f02016-02-22 09:56:40 -0500121}
122
123GrVkTexture::~GrVkTexture() {
124 // either release or abandon should have been called by the owner of this object.
125 SkASSERT(!fTextureView);
126}
127
128void GrVkTexture::onRelease() {
129 // we create this and don't hand it off, so we should always destroy it
130 if (fTextureView) {
131 fTextureView->unref(this->getVkGpu());
132 fTextureView = nullptr;
133 }
134
kkinnunen2e6055b2016-04-22 01:48:29 -0700135 this->releaseImage(this->getVkGpu());
Greg Daniel164a9f02016-02-22 09:56:40 -0500136
137 INHERITED::onRelease();
138}
139
140void GrVkTexture::onAbandon() {
141 if (fTextureView) {
142 fTextureView->unrefAndAbandon();
143 fTextureView = nullptr;
144 }
145
146 this->abandonImage();
147 INHERITED::onAbandon();
148}
149
Robert Phillipsb67821d2017-12-13 15:00:45 -0500150GrBackendTexture GrVkTexture::getBackendTexture() const {
Greg Daniel52e16d92018-04-10 09:34:07 -0400151 return GrBackendTexture(this->width(), this->height(), fInfo, this->grVkImageLayout());
Robert Phillipsb67821d2017-12-13 15:00:45 -0500152}
153
Greg Daniel164a9f02016-02-22 09:56:40 -0500154GrVkGpu* GrVkTexture::getVkGpu() const {
155 SkASSERT(!this->wasDestroyed());
156 return static_cast<GrVkGpu*>(this->getGpu());
157}
jvanverth62340062016-04-26 08:01:44 -0700158
Brian Osman2b23c4b2018-06-01 12:25:08 -0400159const GrVkImageView* GrVkTexture::textureView() {
160 return fTextureView;
brianosmanf05ab1b2016-05-12 11:01:10 -0700161}
162