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" |
| 9 | #include "GrVkGpu.h" |
| 10 | #include "GrVkImageView.h" |
| 11 | #include "GrVkUtil.h" |
| 12 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 13 | #include "vk/GrVkTypes.h" |
| 14 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 15 | #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 16 | |
| 17 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 18 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 19 | const GrSurfaceDesc& desc, |
| 20 | GrGpuResource::LifeCycle lifeCycle, |
| 21 | const GrVkImage::Resource* imageResource, |
| 22 | const GrVkImageView* view) |
| 23 | : GrSurface(gpu, lifeCycle, desc) |
| 24 | , GrVkImage(imageResource) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 25 | , INHERITED(gpu, lifeCycle, desc, kSampler2D_GrSLType, |
| 26 | false) // false because we don't upload MIP data in Vk yet |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 27 | , fTextureView(view) { |
| 28 | this->registerWithCache(); |
| 29 | } |
| 30 | |
| 31 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
| 32 | GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 33 | const GrSurfaceDesc& desc, |
| 34 | GrGpuResource::LifeCycle lifeCycle, |
| 35 | const GrVkImage::Resource* imageResource, |
| 36 | const GrVkImageView* view, |
| 37 | Derived) |
| 38 | : GrSurface(gpu, lifeCycle, desc) |
| 39 | , GrVkImage(imageResource) |
cdalton | 9c3f143 | 2016-03-11 10:07:37 -0800 | [diff] [blame] | 40 | , INHERITED(gpu, lifeCycle, desc, kSampler2D_GrSLType, |
| 41 | false) // false because we don't upload MIP data in Vk yet |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 42 | , fTextureView(view) {} |
| 43 | |
| 44 | |
| 45 | GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu, |
| 46 | const GrSurfaceDesc& desc, |
| 47 | GrGpuResource::LifeCycle lifeCycle, |
| 48 | VkFormat format, |
| 49 | const GrVkImage::Resource* imageResource) { |
| 50 | VkImage image = imageResource->fImage; |
| 51 | const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, |
| 52 | GrVkImageView::kColor_Type); |
| 53 | if (!imageView) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | return new GrVkTexture(gpu, desc, lifeCycle, imageResource, imageView); |
| 58 | } |
| 59 | |
| 60 | GrVkTexture* GrVkTexture::CreateNewTexture(GrVkGpu* gpu, const GrSurfaceDesc& desc, |
| 61 | GrGpuResource::LifeCycle lifeCycle, |
| 62 | const GrVkImage::ImageDesc& imageDesc) { |
| 63 | SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
| 64 | |
| 65 | const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, imageDesc); |
| 66 | if (!imageResource) { |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | GrVkTexture* texture = Create(gpu, desc, lifeCycle, imageDesc.fFormat, imageResource); |
| 71 | // Create() will increment the refCount of the image resource if it succeeds |
| 72 | imageResource->unref(gpu); |
| 73 | |
| 74 | return texture; |
| 75 | } |
| 76 | |
| 77 | GrVkTexture* GrVkTexture::CreateWrappedTexture(GrVkGpu* gpu, const GrSurfaceDesc& desc, |
| 78 | GrGpuResource::LifeCycle lifeCycle, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 79 | VkFormat format, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 80 | const GrVkTextureInfo* info) { |
| 81 | SkASSERT(info); |
| 82 | // Wrapped textures require both image and allocation (because they can be mapped) |
| 83 | SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 84 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 85 | GrVkImage::Resource::Flags flags = (VK_IMAGE_TILING_LINEAR == info->fImageTiling) |
| 86 | ? Resource::kLinearTiling_Flag : Resource::kNo_Flags; |
| 87 | |
jvanverth | fe170d2 | 2016-03-22 13:15:44 -0700 | [diff] [blame] | 88 | const GrVkImage::Resource* imageResource; |
| 89 | if (kBorrowed_LifeCycle == lifeCycle) { |
| 90 | imageResource = new GrVkImage::BorrowedResource(info->fImage, info->fAlloc, flags); |
| 91 | } else { |
| 92 | imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flags); |
| 93 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 94 | if (!imageResource) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | |
| 98 | GrVkTexture* texture = Create(gpu, desc, lifeCycle, format, imageResource); |
| 99 | if (texture) { |
| 100 | texture->fCurrentLayout = info->fImageLayout; |
| 101 | } |
| 102 | // Create() will increment the refCount of the image resource if it succeeds |
| 103 | imageResource->unref(gpu); |
| 104 | |
| 105 | return texture; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | GrVkTexture::~GrVkTexture() { |
| 109 | // either release or abandon should have been called by the owner of this object. |
| 110 | SkASSERT(!fTextureView); |
| 111 | } |
| 112 | |
| 113 | void GrVkTexture::onRelease() { |
| 114 | // we create this and don't hand it off, so we should always destroy it |
| 115 | if (fTextureView) { |
| 116 | fTextureView->unref(this->getVkGpu()); |
| 117 | fTextureView = nullptr; |
| 118 | } |
| 119 | |
| 120 | if (this->shouldFreeResources()) { |
| 121 | this->releaseImage(this->getVkGpu()); |
| 122 | } else { |
| 123 | this->abandonImage(); |
| 124 | } |
| 125 | |
| 126 | INHERITED::onRelease(); |
| 127 | } |
| 128 | |
| 129 | void GrVkTexture::onAbandon() { |
| 130 | if (fTextureView) { |
| 131 | fTextureView->unrefAndAbandon(); |
| 132 | fTextureView = nullptr; |
| 133 | } |
| 134 | |
| 135 | this->abandonImage(); |
| 136 | INHERITED::onAbandon(); |
| 137 | } |
| 138 | |
| 139 | GrBackendObject GrVkTexture::getTextureHandle() const { |
| 140 | // Currently just passing back the pointer to the Resource as the handle |
| 141 | return (GrBackendObject)&fResource; |
| 142 | } |
| 143 | |
| 144 | GrVkGpu* GrVkTexture::getVkGpu() const { |
| 145 | SkASSERT(!this->wasDestroyed()); |
| 146 | return static_cast<GrVkGpu*>(this->getGpu()); |
| 147 | } |