1 /*
2 * Copyright 2021 Google LLC
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 "include/gpu/graphite/BackendTexture.h"
9
10 #include "src/gpu/MutableTextureStateRef.h"
11
12 namespace skgpu::graphite {
13
BackendTexture()14 BackendTexture::BackendTexture() {}
15
~BackendTexture()16 BackendTexture::~BackendTexture() {
17 if (!this->isValid()) {
18 return;
19 }
20 #ifdef SK_DAWN
21 if (this->backend() == BackendApi::kDawn) {
22 // Only one of fDawnTexture and fDawnTextureView can be non null.
23 SkASSERT(!(fDawn.fTexture && fDawn.fTextureView));
24 // Release reference.
25 fDawn.fTexture = nullptr;
26 fDawn.fTextureView = nullptr;
27 }
28 #endif
29 }
30
BackendTexture(const BackendTexture & that)31 BackendTexture::BackendTexture(const BackendTexture& that) {
32 *this = that;
33 }
34
operator =(const BackendTexture & that)35 BackendTexture& BackendTexture::operator=(const BackendTexture& that) {
36 bool valid = this->isValid();
37 if (!that.isValid()) {
38 fInfo = {};
39 return *this;
40 } else if (valid && this->backend() != that.backend()) {
41 valid = false;
42 }
43 fDimensions = that.fDimensions;
44 fInfo = that.fInfo;
45
46 switch (that.backend()) {
47 #ifdef SK_DAWN
48 case BackendApi::kDawn:
49 fDawn = that.fDawn;
50 break;
51 #endif
52 #ifdef SK_METAL
53 case BackendApi::kMetal:
54 fMtlTexture = that.fMtlTexture;
55 break;
56 #endif
57 #ifdef SK_VULKAN
58 case BackendApi::kVulkan:
59 // TODO: Actually fill this out
60 break;
61 #endif
62 default:
63 SK_ABORT("Unsupport Backend");
64 }
65 return *this;
66 }
67
operator ==(const BackendTexture & that) const68 bool BackendTexture::operator==(const BackendTexture& that) const {
69 if (!this->isValid() || !that.isValid()) {
70 return false;
71 }
72
73 if (fDimensions != that.fDimensions || fInfo != that.fInfo) {
74 return false;
75 }
76
77 switch (that.backend()) {
78 #ifdef SK_DAWN
79 case BackendApi::kDawn:
80 if (fDawn != that.fDawn) {
81 return false;
82 }
83 break;
84 #endif
85 #ifdef SK_METAL
86 case BackendApi::kMetal:
87 if (fMtlTexture != that.fMtlTexture) {
88 return false;
89 }
90 break;
91 #endif
92 #ifdef SK_VULKAN
93 case BackendApi::kVulkan:
94 // TODO: Actually fill this out
95 return false;
96 #endif
97 default:
98 SK_ABORT("Unsupport Backend");
99 }
100 return true;
101 }
102
setMutableState(const skgpu::MutableTextureState & newState)103 void BackendTexture::setMutableState(const skgpu::MutableTextureState& newState) {
104 fMutableState->set(newState);
105 }
106
107 #ifdef SK_DAWN
BackendTexture(wgpu::Texture texture)108 BackendTexture::BackendTexture(wgpu::Texture texture)
109 : fDimensions{static_cast<int32_t>(texture.GetWidth()),
110 static_cast<int32_t>(texture.GetHeight())}
111 , fInfo(DawnTextureInfo(texture))
112 , fDawn(std::move(texture)) {}
113
BackendTexture(SkISize dimensions,const DawnTextureInfo & info,wgpu::TextureView textureView)114 BackendTexture::BackendTexture(SkISize dimensions,
115 const DawnTextureInfo& info,
116 wgpu::TextureView textureView)
117 : fDimensions(dimensions)
118 , fInfo(info)
119 , fDawn(std::move(textureView)) {}
120
getDawnTexture() const121 wgpu::Texture BackendTexture::getDawnTexture() const {
122 if (this->isValid() && this->backend() == BackendApi::kDawn) {
123 return fDawn.fTexture;
124 }
125 return {};
126 }
127
getDawnTextureView() const128 wgpu::TextureView BackendTexture::getDawnTextureView() const {
129 if (this->isValid() && this->backend() == BackendApi::kDawn) {
130 return fDawn.fTextureView;
131 }
132 return {};
133 }
134 #endif
135
136 #ifdef SK_METAL
BackendTexture(SkISize dimensions,MtlHandle mtlTexture)137 BackendTexture::BackendTexture(SkISize dimensions, MtlHandle mtlTexture)
138 : fDimensions(dimensions)
139 , fInfo(MtlTextureInfo(mtlTexture))
140 , fMtlTexture(mtlTexture) {}
141
getMtlTexture() const142 MtlHandle BackendTexture::getMtlTexture() const {
143 if (this->isValid() && this->backend() == BackendApi::kMetal) {
144 return fMtlTexture;
145 }
146 return nullptr;
147 }
148 #endif // SK_METAL
149
150 #ifdef SK_VULKAN
BackendTexture(SkISize dimensions,const VulkanTextureInfo & info,VkImageLayout layout,uint32_t queueFamilyIndex,VkImage image)151 BackendTexture::BackendTexture(SkISize dimensions,
152 const VulkanTextureInfo& info,
153 VkImageLayout layout,
154 uint32_t queueFamilyIndex,
155 VkImage image)
156 : fDimensions(dimensions)
157 , fInfo(info)
158 , fMutableState(new MutableTextureStateRef(layout, queueFamilyIndex))
159 , fVkImage(image) {}
160
getVkImage() const161 VkImage BackendTexture::getVkImage() const {
162 if (this->isValid() && this->backend() == BackendApi::kVulkan) {
163 return fVkImage;
164 }
165 return VK_NULL_HANDLE;
166 }
167
getVkImageLayout() const168 VkImageLayout BackendTexture::getVkImageLayout() const {
169 if (this->isValid() && this->backend() == BackendApi::kVulkan) {
170 SkASSERT(fMutableState);
171 return fMutableState->getImageLayout();
172 }
173 return VK_IMAGE_LAYOUT_UNDEFINED;
174 }
175
getVkQueueFamilyIndex() const176 uint32_t BackendTexture::getVkQueueFamilyIndex() const {
177 if (this->isValid() && this->backend() == BackendApi::kVulkan) {
178 SkASSERT(fMutableState);
179 return fMutableState->getQueueFamilyIndex();
180 }
181 return 0;
182 }
183 #endif // SK_VULKAN
184
185 } // namespace skgpu::graphite
186
187