| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrVkTexture.h" | 8 #include "GrVkTexture.h" |
| 9 #include "GrVkGpu.h" | 9 #include "GrVkGpu.h" |
| 10 #include "GrVkImageView.h" | 10 #include "GrVkImageView.h" |
| 11 #include "GrTexturePriv.h" | 11 #include "GrTexturePriv.h" |
| 12 #include "GrVkUtil.h" | 12 #include "GrVkUtil.h" |
| 13 | 13 |
| 14 #include "vk/GrVkTypes.h" | 14 #include "vk/GrVkTypes.h" |
| 15 | 15 |
| 16 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) | 16 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) |
| 17 | 17 |
| 18 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. | 18 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. |
| 19 GrVkTexture::GrVkTexture(GrVkGpu* gpu, | 19 GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 20 SkBudgeted budgeted, | 20 SkBudgeted budgeted, |
| 21 const GrSurfaceDesc& desc, | 21 const GrSurfaceDesc& desc, |
| 22 const GrVkImage::Resource* imageResource, | 22 const GrVkImage::Resource* imageResource, |
| 23 const GrVkImageView* view) | 23 const GrVkImageView* view) |
| 24 : GrSurface(gpu, desc) | 24 : GrSurface(gpu, desc) |
| 25 , GrVkImage(imageResource) | 25 , GrVkImage(imageResource) |
| 26 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) | 26 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) |
| 27 , fTextureView(view) { | 27 , fTextureView(view) |
| 28 , fLinearTextureView(nullptr) { |
| 28 this->registerWithCache(budgeted); | 29 this->registerWithCache(budgeted); |
| 29 } | 30 } |
| 30 | 31 |
| 31 GrVkTexture::GrVkTexture(GrVkGpu* gpu, | 32 GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 32 Wrapped, | 33 Wrapped, |
| 33 const GrSurfaceDesc& desc, | 34 const GrSurfaceDesc& desc, |
| 34 const GrVkImage::Resource* imageResource, | 35 const GrVkImage::Resource* imageResource, |
| 35 const GrVkImageView* view) | 36 const GrVkImageView* view) |
| 36 : GrSurface(gpu, desc) | 37 : GrSurface(gpu, desc) |
| 37 , GrVkImage(imageResource) | 38 , GrVkImage(imageResource) |
| 38 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) | 39 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) |
| 39 , fTextureView(view) { | 40 , fTextureView(view) |
| 41 , fLinearTextureView(nullptr) { |
| 40 this->registerWithCacheWrapped(); | 42 this->registerWithCacheWrapped(); |
| 41 } | 43 } |
| 42 | 44 |
| 43 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. | 45 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. |
| 44 GrVkTexture::GrVkTexture(GrVkGpu* gpu, | 46 GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
| 45 const GrSurfaceDesc& desc, | 47 const GrSurfaceDesc& desc, |
| 46 const GrVkImage::Resource* imageResource, | 48 const GrVkImage::Resource* imageResource, |
| 47 const GrVkImageView* view) | 49 const GrVkImageView* view) |
| 48 : GrSurface(gpu, desc) | 50 : GrSurface(gpu, desc) |
| 49 , GrVkImage(imageResource) | 51 , GrVkImage(imageResource) |
| 50 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) | 52 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) |
| 51 , fTextureView(view) {} | 53 , fTextureView(view) |
| 54 , fLinearTextureView(nullptr) {} |
| 52 | 55 |
| 53 | 56 |
| 54 template<typename ResourceType> | 57 template<typename ResourceType> |
| 55 GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu, | 58 GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu, |
| 56 ResourceType type, | 59 ResourceType type, |
| 57 const GrSurfaceDesc& desc, | 60 const GrSurfaceDesc& desc, |
| 58 VkFormat format, | 61 VkFormat format, |
| 59 const GrVkImage::Resource* imageResource) { | 62 const GrVkImage::Resource* imageResource) { |
| 60 VkImage image = imageResource->fImage; | 63 VkImage image = imageResource->fImage; |
| 61 | 64 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 } | 122 } |
| 120 // Create() will increment the refCount of the image resource if it succeeds | 123 // Create() will increment the refCount of the image resource if it succeeds |
| 121 imageResource->unref(gpu); | 124 imageResource->unref(gpu); |
| 122 | 125 |
| 123 return texture; | 126 return texture; |
| 124 } | 127 } |
| 125 | 128 |
| 126 GrVkTexture::~GrVkTexture() { | 129 GrVkTexture::~GrVkTexture() { |
| 127 // either release or abandon should have been called by the owner of this ob
ject. | 130 // either release or abandon should have been called by the owner of this ob
ject. |
| 128 SkASSERT(!fTextureView); | 131 SkASSERT(!fTextureView); |
| 132 SkASSERT(!fLinearTextureView); |
| 129 } | 133 } |
| 130 | 134 |
| 131 void GrVkTexture::onRelease() { | 135 void GrVkTexture::onRelease() { |
| 132 // we create this and don't hand it off, so we should always destroy it | 136 // we create this and don't hand it off, so we should always destroy it |
| 133 if (fTextureView) { | 137 if (fTextureView) { |
| 134 fTextureView->unref(this->getVkGpu()); | 138 fTextureView->unref(this->getVkGpu()); |
| 135 fTextureView = nullptr; | 139 fTextureView = nullptr; |
| 136 } | 140 } |
| 137 | 141 |
| 142 if (fLinearTextureView) { |
| 143 fLinearTextureView->unref(this->getVkGpu()); |
| 144 fLinearTextureView = nullptr; |
| 145 } |
| 146 |
| 138 this->releaseImage(this->getVkGpu()); | 147 this->releaseImage(this->getVkGpu()); |
| 139 | 148 |
| 140 INHERITED::onRelease(); | 149 INHERITED::onRelease(); |
| 141 } | 150 } |
| 142 | 151 |
| 143 void GrVkTexture::onAbandon() { | 152 void GrVkTexture::onAbandon() { |
| 144 if (fTextureView) { | 153 if (fTextureView) { |
| 145 fTextureView->unrefAndAbandon(); | 154 fTextureView->unrefAndAbandon(); |
| 146 fTextureView = nullptr; | 155 fTextureView = nullptr; |
| 147 } | 156 } |
| 148 | 157 |
| 158 if (fLinearTextureView) { |
| 159 fLinearTextureView->unrefAndAbandon(); |
| 160 fLinearTextureView = nullptr; |
| 161 } |
| 162 |
| 149 this->abandonImage(); | 163 this->abandonImage(); |
| 150 INHERITED::onAbandon(); | 164 INHERITED::onAbandon(); |
| 151 } | 165 } |
| 152 | 166 |
| 153 GrBackendObject GrVkTexture::getTextureHandle() const { | 167 GrBackendObject GrVkTexture::getTextureHandle() const { |
| 154 // Currently just passing back the pointer to the Resource as the handle | 168 // Currently just passing back the pointer to the Resource as the handle |
| 155 return (GrBackendObject)&fResource; | 169 return (GrBackendObject)&fResource; |
| 156 } | 170 } |
| 157 | 171 |
| 158 GrVkGpu* GrVkTexture::getVkGpu() const { | 172 GrVkGpu* GrVkTexture::getVkGpu() const { |
| 159 SkASSERT(!this->wasDestroyed()); | 173 SkASSERT(!this->wasDestroyed()); |
| 160 return static_cast<GrVkGpu*>(this->getGpu()); | 174 return static_cast<GrVkGpu*>(this->getGpu()); |
| 161 } | 175 } |
| 162 | 176 |
| 177 const GrVkImageView* GrVkTexture::textureView(bool allowSRGB) { |
| 178 VkFormat linearFormat; |
| 179 if (allowSRGB || !GrVkFormatIsSRGB(fResource->fFormat, &linearFormat)) { |
| 180 return fTextureView; |
| 181 } |
| 182 |
| 183 if (!fLinearTextureView) { |
| 184 fLinearTextureView = GrVkImageView::Create(this->getVkGpu(), fResource->
fImage, |
| 185 linearFormat, GrVkImageView::
kColor_Type, |
| 186 fResource->fLevelCount); |
| 187 SkASSERT(fLinearTextureView); |
| 188 } |
| 189 |
| 190 return fLinearTextureView; |
| 191 } |
| 192 |
| 163 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) { | 193 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) { |
| 164 if (mipLevels == 1) { | 194 if (mipLevels == 1) { |
| 165 // don't need to do anything for a 1x1 texture | 195 // don't need to do anything for a 1x1 texture |
| 166 return false; | 196 return false; |
| 167 } | 197 } |
| 168 | 198 |
| 169 const GrVkImage::Resource* oldResource = fResource; | 199 const GrVkImage::Resource* oldResource = fResource; |
| 170 | 200 |
| 171 // We shouldn't realloc something that doesn't belong to us | 201 // We shouldn't realloc something that doesn't belong to us |
| 172 if (GrVkImage::Resource::kBorrowed_Flag & oldResource->fFlags) { | 202 if (GrVkImage::Resource::kBorrowed_Flag & oldResource->fFlags) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 203 VkImage image = imageResource->fImage; | 233 VkImage image = imageResource->fImage; |
| 204 const GrVkImageView* textureView = GrVkImageView::Create(gpu, image, imageRe
source->fFormat, | 234 const GrVkImageView* textureView = GrVkImageView::Create(gpu, image, imageRe
source->fFormat, |
| 205 GrVkImageView::kCol
or_Type, mipLevels); | 235 GrVkImageView::kCol
or_Type, mipLevels); |
| 206 if (!textureView) { | 236 if (!textureView) { |
| 207 imageResource->unref(gpu); | 237 imageResource->unref(gpu); |
| 208 return false; | 238 return false; |
| 209 } | 239 } |
| 210 | 240 |
| 211 oldResource->unref(gpu); | 241 oldResource->unref(gpu); |
| 212 oldView->unref(gpu); | 242 oldView->unref(gpu); |
| 243 if (fLinearTextureView) { |
| 244 fLinearTextureView->unref(gpu); |
| 245 fLinearTextureView = nullptr; |
| 246 } |
| 213 fResource = imageResource; | 247 fResource = imageResource; |
| 214 fTextureView = textureView; | 248 fTextureView = textureView; |
| 215 fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED; | 249 fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 216 this->texturePriv().setMaxMipMapLevel(mipLevels); | 250 this->texturePriv().setMaxMipMapLevel(mipLevels); |
| 217 | 251 |
| 218 return true; | 252 return true; |
| 219 } | 253 } |
| OLD | NEW |