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