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, info->fAll
oc, flags); | 103 imageResource = new GrVkImage::BorrowedResource(info->fImage, info->fAll
oc, flags); |
91 } else { | 104 } else { |
92 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flag
s); | 105 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flag
s); |
93 } | 106 } |
94 if (!imageResource) { | 107 if (!imageResource) { |
95 return nullptr; | 108 return nullptr; |
96 } | 109 } |
97 | 110 |
98 GrVkTexture* texture = Create(gpu, desc, lifeCycle, format, imageResource); | 111 GrVkTexture* texture = Create(gpu, kWrapped, desc, format, imageResource); |
99 if (texture) { | 112 if (texture) { |
100 texture->fCurrentLayout = info->fImageLayout; | 113 texture->fCurrentLayout = info->fImageLayout; |
101 } | 114 } |
102 // Create() will increment the refCount of the image resource if it succeeds | 115 // Create() will increment the refCount of the image resource if it succeeds |
103 imageResource->unref(gpu); | 116 imageResource->unref(gpu); |
104 | 117 |
105 return texture; | 118 return texture; |
106 } | 119 } |
107 | 120 |
108 GrVkTexture::~GrVkTexture() { | 121 GrVkTexture::~GrVkTexture() { |
109 // either release or abandon should have been called by the owner of this ob
ject. | 122 // either release or abandon should have been called by the owner of this ob
ject. |
110 SkASSERT(!fTextureView); | 123 SkASSERT(!fTextureView); |
111 } | 124 } |
112 | 125 |
113 void GrVkTexture::onRelease() { | 126 void GrVkTexture::onRelease() { |
114 // we create this and don't hand it off, so we should always destroy it | 127 // we create this and don't hand it off, so we should always destroy it |
115 if (fTextureView) { | 128 if (fTextureView) { |
116 fTextureView->unref(this->getVkGpu()); | 129 fTextureView->unref(this->getVkGpu()); |
117 fTextureView = nullptr; | 130 fTextureView = nullptr; |
118 } | 131 } |
119 | 132 |
120 if (this->shouldFreeResources()) { | 133 this->releaseImage(this->getVkGpu()); |
121 this->releaseImage(this->getVkGpu()); | |
122 } else { | |
123 this->abandonImage(); | |
124 } | |
125 | 134 |
126 INHERITED::onRelease(); | 135 INHERITED::onRelease(); |
127 } | 136 } |
128 | 137 |
129 void GrVkTexture::onAbandon() { | 138 void GrVkTexture::onAbandon() { |
130 if (fTextureView) { | 139 if (fTextureView) { |
131 fTextureView->unrefAndAbandon(); | 140 fTextureView->unrefAndAbandon(); |
132 fTextureView = nullptr; | 141 fTextureView = nullptr; |
133 } | 142 } |
134 | 143 |
135 this->abandonImage(); | 144 this->abandonImage(); |
136 INHERITED::onAbandon(); | 145 INHERITED::onAbandon(); |
137 } | 146 } |
138 | 147 |
139 GrBackendObject GrVkTexture::getTextureHandle() const { | 148 GrBackendObject GrVkTexture::getTextureHandle() const { |
140 // Currently just passing back the pointer to the Resource as the handle | 149 // Currently just passing back the pointer to the Resource as the handle |
141 return (GrBackendObject)&fResource; | 150 return (GrBackendObject)&fResource; |
142 } | 151 } |
143 | 152 |
144 GrVkGpu* GrVkTexture::getVkGpu() const { | 153 GrVkGpu* GrVkTexture::getVkGpu() const { |
145 SkASSERT(!this->wasDestroyed()); | 154 SkASSERT(!this->wasDestroyed()); |
146 return static_cast<GrVkGpu*>(this->getGpu()); | 155 return static_cast<GrVkGpu*>(this->getGpu()); |
147 } | 156 } |
OLD | NEW |