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 | |
| 8 | #include "GrVkTexture.h" |
Robert Phillips | f95b175 | 2017-08-31 08:56:07 -0400 | [diff] [blame] | 9 | |
| 10 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 11 | #include "GrVkGpu.h" |
| 12 | #include "GrVkImageView.h" |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 13 | #include "GrVkTextureRenderTarget.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrVkUtil.h" |
| 15 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 16 | #include "vk/GrVkTypes.h" |
| 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 | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 28 | : GrSurface(gpu, desc) |
| 29 | , GrVkImage(info, std::move(layout), GrBackendObjectOwnership::kOwned) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 30 | , INHERITED(gpu, desc, 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); |
| 34 | } |
| 35 | |
| 36 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 37 | Wrapped, |
| 38 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 39 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 40 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 41 | const GrVkImageView* view, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 42 | GrMipMapsStatus mipMapsStatus, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 43 | GrBackendObjectOwnership ownership, |
Brian Salomon | 796693a | 2018-12-05 14:36:35 -0500 | [diff] [blame^] | 44 | GrIOType ioType, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 45 | bool purgeImmediately) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 46 | : GrSurface(gpu, desc) |
| 47 | , GrVkImage(info, std::move(layout), ownership) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 48 | , INHERITED(gpu, desc, GrTextureType::k2D, mipMapsStatus) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 49 | , fTextureView(view) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 50 | SkASSERT((GrMipMapsStatus::kNotAllocated == mipMapsStatus) == (1 == info.fLevelCount)); |
Brian Salomon | 796693a | 2018-12-05 14:36:35 -0500 | [diff] [blame^] | 51 | if (ioType == kRead_GrIOType) { |
| 52 | this->setReadOnly(); |
| 53 | } |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 54 | this->registerWithCacheWrapped(purgeImmediately); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 58 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 59 | const GrSurfaceDesc& desc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 60 | const GrVkImageInfo& info, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 61 | sk_sp<GrVkImageLayout> layout, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 62 | const GrVkImageView* view, |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 63 | GrMipMapsStatus mipMapsStatus, |
| 64 | GrBackendObjectOwnership ownership) |
Brian Salomon | 60dd8c7 | 2018-07-30 10:24:13 -0400 | [diff] [blame] | 65 | : GrSurface(gpu, desc) |
| 66 | , GrVkImage(info, layout, ownership) |
Brian Salomon | e632dfc | 2018-08-01 13:01:16 -0400 | [diff] [blame] | 67 | , INHERITED(gpu, desc, 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 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [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 | 796693a | 2018-12-05 14:36:35 -0500 | [diff] [blame^] | 99 | GrIOType ioType, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 100 | bool purgeImmediately, |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 101 | const GrVkImageInfo& info, |
| 102 | sk_sp<GrVkImageLayout> layout) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 103 | // Wrapped textures require both image and allocation (because they can be mapped) |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 104 | SkASSERT(VK_NULL_HANDLE != info.fImage && VK_NULL_HANDLE != info.fAlloc.fMemory); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 105 | |
Greg Daniel | 7e00022 | 2018-12-03 10:08:21 -0500 | [diff] [blame] | 106 | const GrVkImageView* imageView = GrVkImageView::Create( |
| 107 | gpu, info.fImage, info.fFormat, GrVkImageView::kColor_Type, info.fLevelCount, |
| 108 | info.fYcbcrConversionInfo); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 109 | if (!imageView) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 110 | return nullptr; |
| 111 | } |
| 112 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 113 | GrMipMapsStatus mipMapsStatus = info.fLevelCount > 1 ? GrMipMapsStatus::kValid |
| 114 | : GrMipMapsStatus::kNotAllocated; |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 115 | |
Greg Daniel | 1591c38 | 2017-08-17 15:37:20 -0400 | [diff] [blame] | 116 | GrBackendObjectOwnership ownership = kBorrow_GrWrapOwnership == wrapOwnership |
| 117 | ? GrBackendObjectOwnership::kBorrowed : GrBackendObjectOwnership::kOwned; |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 118 | return sk_sp<GrVkTexture>(new GrVkTexture(gpu, kWrapped, desc, info, std::move(layout), |
Brian Salomon | 796693a | 2018-12-05 14:36:35 -0500 | [diff] [blame^] | 119 | imageView, mipMapsStatus, ownership, ioType, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 120 | purgeImmediately)); |
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() { |
| 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 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 135 | this->releaseImage(this->getVkGpu()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | |
| 137 | INHERITED::onRelease(); |
| 138 | } |
| 139 | |
| 140 | void GrVkTexture::onAbandon() { |
| 141 | if (fTextureView) { |
| 142 | fTextureView->unrefAndAbandon(); |
| 143 | fTextureView = nullptr; |
| 144 | } |
| 145 | |
| 146 | this->abandonImage(); |
| 147 | INHERITED::onAbandon(); |
| 148 | } |
| 149 | |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 150 | GrBackendTexture GrVkTexture::getBackendTexture() const { |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 151 | return GrBackendTexture(this->width(), this->height(), fInfo, this->grVkImageLayout()); |
Robert Phillips | b67821d | 2017-12-13 15:00:45 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 154 | GrVkGpu* GrVkTexture::getVkGpu() const { |
| 155 | SkASSERT(!this->wasDestroyed()); |
| 156 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 157 | } |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 158 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 159 | const GrVkImageView* GrVkTexture::textureView() { |
| 160 | return fTextureView; |
brianosman | f05ab1b | 2016-05-12 11:01:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |