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