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 GrVkImageInfo& info, |
23 const GrVkImageView* view) | 23 const GrVkImageView* view) |
24 : GrSurface(gpu, desc) | 24 : GrSurface(gpu, desc) |
25 , GrVkImage(imageResource) | 25 , GrVkImage(info, GrVkImage::kNot_Wrapped) |
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 , fLinearTextureView(nullptr) { |
29 this->registerWithCache(budgeted); | 29 this->registerWithCache(budgeted); |
30 } | 30 } |
31 | 31 |
32 GrVkTexture::GrVkTexture(GrVkGpu* gpu, | 32 GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
33 Wrapped, | 33 Wrapped, |
34 const GrSurfaceDesc& desc, | 34 const GrSurfaceDesc& desc, |
35 const GrVkImage::Resource* imageResource, | 35 const GrVkImageInfo& info, |
36 const GrVkImageView* view) | 36 const GrVkImageView* view, |
| 37 GrVkImage::Wrapped wrapped) |
37 : GrSurface(gpu, desc) | 38 : GrSurface(gpu, desc) |
38 , GrVkImage(imageResource) | 39 , GrVkImage(info, wrapped) |
39 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) | 40 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) |
40 , fTextureView(view) | 41 , fTextureView(view) |
41 , fLinearTextureView(nullptr) { | 42 , fLinearTextureView(nullptr) { |
42 this->registerWithCacheWrapped(); | 43 this->registerWithCacheWrapped(); |
43 } | 44 } |
44 | 45 |
45 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. | 46 // Because this class is virtually derived from GrSurface we must explicitly cal
l its constructor. |
46 GrVkTexture::GrVkTexture(GrVkGpu* gpu, | 47 GrVkTexture::GrVkTexture(GrVkGpu* gpu, |
47 const GrSurfaceDesc& desc, | 48 const GrSurfaceDesc& desc, |
48 const GrVkImage::Resource* imageResource, | 49 const GrVkImageInfo& info, |
49 const GrVkImageView* view) | 50 const GrVkImageView* view, |
| 51 GrVkImage::Wrapped wrapped) |
50 : GrSurface(gpu, desc) | 52 : GrSurface(gpu, desc) |
51 , GrVkImage(imageResource) | 53 , GrVkImage(info, wrapped) |
52 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) | 54 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped) |
53 , fTextureView(view) | 55 , fTextureView(view) |
54 , fLinearTextureView(nullptr) {} | 56 , fLinearTextureView(nullptr) { |
55 | |
56 | |
57 template<typename ResourceType> | |
58 GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu, | |
59 ResourceType type, | |
60 const GrSurfaceDesc& desc, | |
61 VkFormat format, | |
62 const GrVkImage::Resource* imageResource) { | |
63 VkImage image = imageResource->fImage; | |
64 | |
65 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, | |
66 GrVkImageView::kColor
_Type, | |
67 imageResource->fLevel
Count); | |
68 if (!imageView) { | |
69 return nullptr; | |
70 } | |
71 | |
72 return new GrVkTexture(gpu, type, desc, imageResource, imageView); | |
73 } | 57 } |
74 | 58 |
75 GrVkTexture* GrVkTexture::CreateNewTexture(GrVkGpu* gpu, SkBudgeted budgeted, | 59 GrVkTexture* GrVkTexture::CreateNewTexture(GrVkGpu* gpu, SkBudgeted budgeted, |
76 const GrSurfaceDesc& desc, | 60 const GrSurfaceDesc& desc, |
77 const GrVkImage::ImageDesc& imageDesc
) { | 61 const GrVkImage::ImageDesc& imageDesc
) { |
78 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); | 62 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); |
79 | 63 |
80 const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, im
ageDesc); | 64 GrVkImageInfo info; |
81 if (!imageResource) { | 65 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
82 return nullptr; | 66 return nullptr; |
83 } | 67 } |
84 | 68 |
85 GrVkTexture* texture = Create(gpu, budgeted, desc, imageDesc.fFormat, imageR
esource); | 69 const GrVkImageView* imageView = GrVkImageView::Create(gpu, info.fImage, inf
o.fFormat, |
86 // Create() will increment the refCount of the image resource if it succeeds | 70 GrVkImageView::kColor
_Type, |
87 imageResource->unref(gpu); | 71 info.fLevelCount); |
| 72 if (!imageView) { |
| 73 GrVkImage::DestroyImageInfo(gpu, &info); |
| 74 return nullptr; |
| 75 } |
88 | 76 |
89 return texture; | 77 return new GrVkTexture(gpu, budgeted, desc, info, imageView); |
90 } | 78 } |
91 | 79 |
92 GrVkTexture* GrVkTexture::CreateWrappedTexture(GrVkGpu* gpu, | 80 GrVkTexture* GrVkTexture::CreateWrappedTexture(GrVkGpu* gpu, |
93 const GrSurfaceDesc& desc, | 81 const GrSurfaceDesc& desc, |
94 GrWrapOwnership ownership, | 82 GrWrapOwnership ownership, |
95 VkFormat format, | 83 const GrVkImageInfo* info) { |
96 const GrVkTextureInfo* info) { | |
97 SkASSERT(info); | 84 SkASSERT(info); |
98 // Wrapped textures require both image and allocation (because they can be m
apped) | 85 // Wrapped textures require both image and allocation (because they can be m
apped) |
99 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); | 86 SkASSERT(VK_NULL_HANDLE != info->fImage && VK_NULL_HANDLE != info->fAlloc); |
100 | 87 |
101 GrVkImage::Resource::Flags flags = (VK_IMAGE_TILING_LINEAR == info->fImageTi
ling) | 88 const GrVkImageView* imageView = GrVkImageView::Create(gpu, info->fImage, in
fo->fFormat, |
102 ? Resource::kLinearTiling_Flag : Resource::
kNo_Flags; | 89 GrVkImageView::kColor
_Type, |
103 | 90 info->fLevelCount); |
104 const GrVkImage::Resource* imageResource; | 91 if (!imageView) { |
105 if (kBorrow_GrWrapOwnership == ownership) { | |
106 imageResource = new GrVkImage::BorrowedResource(info->fImage, | |
107 info->fAlloc, | |
108 info->fFormat, | |
109 info->fLevelCount, | |
110 flags); | |
111 } else { | |
112 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, info
->fFormat, | |
113 info->fLevelCount, flags); | |
114 } | |
115 if (!imageResource) { | |
116 return nullptr; | 92 return nullptr; |
117 } | 93 } |
118 | 94 |
119 GrVkTexture* texture = Create(gpu, kWrapped, desc, format, imageResource); | 95 GrVkImage::Wrapped wrapped = kBorrow_GrWrapOwnership == ownership ? GrVkImag
e::kBorrowed_Wrapped |
120 if (texture) { | 96 : GrVkImag
e::kAdopted_Wrapped; |
121 texture->fCurrentLayout = info->fImageLayout; | |
122 } | |
123 // Create() will increment the refCount of the image resource if it succeeds | |
124 imageResource->unref(gpu); | |
125 | 97 |
126 return texture; | 98 return new GrVkTexture(gpu, kWrapped, desc, *info, imageView, wrapped); |
127 } | 99 } |
128 | 100 |
129 GrVkTexture::~GrVkTexture() { | 101 GrVkTexture::~GrVkTexture() { |
130 // either release or abandon should have been called by the owner of this ob
ject. | 102 // either release or abandon should have been called by the owner of this ob
ject. |
131 SkASSERT(!fTextureView); | 103 SkASSERT(!fTextureView); |
132 SkASSERT(!fLinearTextureView); | 104 SkASSERT(!fLinearTextureView); |
133 } | 105 } |
134 | 106 |
135 void GrVkTexture::onRelease() { | 107 void GrVkTexture::onRelease() { |
136 // we create this and don't hand it off, so we should always destroy it | 108 // we create this and don't hand it off, so we should always destroy it |
(...skipping 21 matching lines...) Expand all Loading... |
158 if (fLinearTextureView) { | 130 if (fLinearTextureView) { |
159 fLinearTextureView->unrefAndAbandon(); | 131 fLinearTextureView->unrefAndAbandon(); |
160 fLinearTextureView = nullptr; | 132 fLinearTextureView = nullptr; |
161 } | 133 } |
162 | 134 |
163 this->abandonImage(); | 135 this->abandonImage(); |
164 INHERITED::onAbandon(); | 136 INHERITED::onAbandon(); |
165 } | 137 } |
166 | 138 |
167 GrBackendObject GrVkTexture::getTextureHandle() const { | 139 GrBackendObject GrVkTexture::getTextureHandle() const { |
168 // Currently just passing back the pointer to the Resource as the handle | 140 return (GrBackendObject)&fInfo; |
169 return (GrBackendObject)&fResource; | |
170 } | 141 } |
171 | 142 |
172 GrVkGpu* GrVkTexture::getVkGpu() const { | 143 GrVkGpu* GrVkTexture::getVkGpu() const { |
173 SkASSERT(!this->wasDestroyed()); | 144 SkASSERT(!this->wasDestroyed()); |
174 return static_cast<GrVkGpu*>(this->getGpu()); | 145 return static_cast<GrVkGpu*>(this->getGpu()); |
175 } | 146 } |
176 | 147 |
177 const GrVkImageView* GrVkTexture::textureView(bool allowSRGB) { | 148 const GrVkImageView* GrVkTexture::textureView(bool allowSRGB) { |
178 VkFormat linearFormat; | 149 VkFormat linearFormat; |
179 if (allowSRGB || !GrVkFormatIsSRGB(fResource->fFormat, &linearFormat)) { | 150 if (allowSRGB || !GrVkFormatIsSRGB(fInfo.fFormat, &linearFormat)) { |
180 return fTextureView; | 151 return fTextureView; |
181 } | 152 } |
182 | 153 |
183 if (!fLinearTextureView) { | 154 if (!fLinearTextureView) { |
184 fLinearTextureView = GrVkImageView::Create(this->getVkGpu(), fResource->
fImage, | 155 fLinearTextureView = GrVkImageView::Create(this->getVkGpu(), fInfo.fImag
e, |
185 linearFormat, GrVkImageView::
kColor_Type, | 156 linearFormat, GrVkImageView::
kColor_Type, |
186 fResource->fLevelCount); | 157 fInfo.fLevelCount); |
187 SkASSERT(fLinearTextureView); | 158 SkASSERT(fLinearTextureView); |
188 } | 159 } |
189 | 160 |
190 return fLinearTextureView; | 161 return fLinearTextureView; |
191 } | 162 } |
192 | 163 |
193 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) { | 164 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) { |
194 if (mipLevels == 1) { | 165 if (mipLevels == 1) { |
195 // don't need to do anything for a 1x1 texture | 166 // don't need to do anything for a 1x1 texture |
196 return false; | 167 return false; |
197 } | 168 } |
198 | 169 |
199 const GrVkImage::Resource* oldResource = fResource; | 170 const GrVkResource* oldResource = this->resource(); |
200 | 171 |
201 // We shouldn't realloc something that doesn't belong to us | 172 // We shouldn't realloc something that doesn't belong to us |
202 if (GrVkImage::Resource::kBorrowed_Flag & oldResource->fFlags) { | 173 if (fIsBorrowed) { |
203 return false; | 174 return false; |
204 } | 175 } |
205 | 176 |
206 // Does this even make sense for rendertargets? | 177 // Does this even make sense for rendertargets? |
207 bool renderTarget = SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag); | 178 bool renderTarget = SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag); |
208 | 179 |
209 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; | 180 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
210 if (renderTarget) { | 181 if (renderTarget) { |
211 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; | 182 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
212 } | 183 } |
213 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_
BIT; | 184 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_
BIT; |
214 | 185 |
215 GrVkImage::ImageDesc imageDesc; | 186 GrVkImage::ImageDesc imageDesc; |
216 imageDesc.fImageType = VK_IMAGE_TYPE_2D; | 187 imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
217 imageDesc.fFormat = oldResource->fFormat; | 188 imageDesc.fFormat = fInfo.fFormat; |
218 imageDesc.fWidth = fDesc.fWidth; | 189 imageDesc.fWidth = fDesc.fWidth; |
219 imageDesc.fHeight = fDesc.fHeight; | 190 imageDesc.fHeight = fDesc.fHeight; |
220 imageDesc.fLevels = mipLevels; | 191 imageDesc.fLevels = mipLevels; |
221 imageDesc.fSamples = 1; | 192 imageDesc.fSamples = 1; |
222 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; | 193 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
223 imageDesc.fUsageFlags = usageFlags; | 194 imageDesc.fUsageFlags = usageFlags; |
224 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; | 195 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
225 | 196 |
226 const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, im
ageDesc); | 197 GrVkImageInfo info; |
227 if (!imageResource) { | 198 if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) { |
228 return false; | 199 return nullptr; |
229 } | 200 } |
230 | 201 |
231 // have to create a new image view for new resource | 202 // have to create a new image view for new resource |
232 const GrVkImageView* oldView = fTextureView; | 203 const GrVkImageView* oldView = fTextureView; |
233 VkImage image = imageResource->fImage; | 204 VkImage image = info.fImage; |
234 const GrVkImageView* textureView = GrVkImageView::Create(gpu, image, imageRe
source->fFormat, | 205 const GrVkImageView* textureView = GrVkImageView::Create(gpu, image, info.fF
ormat, |
235 GrVkImageView::kCol
or_Type, mipLevels); | 206 GrVkImageView::kCol
or_Type, mipLevels); |
236 if (!textureView) { | 207 if (!textureView) { |
237 imageResource->unref(gpu); | 208 GrVkImage::DestroyImageInfo(gpu, &info); |
238 return false; | 209 return false; |
239 } | 210 } |
240 | 211 |
241 oldResource->unref(gpu); | 212 oldResource->unref(gpu); |
242 oldView->unref(gpu); | 213 oldView->unref(gpu); |
243 if (fLinearTextureView) { | 214 if (fLinearTextureView) { |
244 fLinearTextureView->unref(gpu); | 215 fLinearTextureView->unref(gpu); |
245 fLinearTextureView = nullptr; | 216 fLinearTextureView = nullptr; |
246 } | 217 } |
247 fResource = imageResource; | 218 |
| 219 this->setNewResource(info.fImage, info.fAlloc); |
248 fTextureView = textureView; | 220 fTextureView = textureView; |
249 fCurrentLayout = VK_IMAGE_LAYOUT_UNDEFINED; | 221 fInfo = info; |
250 this->texturePriv().setMaxMipMapLevel(mipLevels); | 222 this->texturePriv().setMaxMipMapLevel(mipLevels); |
251 | 223 |
252 return true; | 224 return true; |
253 } | 225 } |
OLD | NEW |