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/TextureInfo.h"
9
10 #ifdef SK_DAWN
11 #include "src/gpu/graphite/dawn/DawnUtilsPriv.h"
12 #endif
13
14 #ifdef SK_METAL
15 namespace skgpu::graphite {
16 // Including Metal types/headers here is tricky. This is defined in MtlGraphiteUtils.mm
17 size_t MtlFormatBytesPerBlock(MtlPixelFormat);
18 SkTextureCompressionType MtlFormatToCompressionType(MtlPixelFormat);
19 }
20 #endif
21
22 #ifdef SK_VULKAN
23 #include "src/gpu/vk/VulkanUtilsPriv.h"
24 #endif
25
26 namespace skgpu::graphite {
27
operator =(const TextureInfo & that)28 TextureInfo& TextureInfo::operator=(const TextureInfo& that) {
29 if (!that.isValid()) {
30 fValid = false;
31 return *this;
32 }
33 fBackend = that.fBackend;
34 fSampleCount = that.fSampleCount;
35 fMipmapped = that.fMipmapped;
36 fProtected = that.fProtected;
37
38 switch (that.backend()) {
39 #ifdef SK_DAWN
40 case BackendApi::kDawn:
41 fDawnSpec = that.fDawnSpec;
42 break;
43 #endif
44 #ifdef SK_METAL
45 case BackendApi::kMetal:
46 fMtlSpec = that.fMtlSpec;
47 break;
48 #endif
49 #ifdef SK_VULKAN
50 case BackendApi::kVulkan:
51 fVkSpec = that.fVkSpec;
52 break;
53 #endif
54 default:
55 SK_ABORT("Unsupport Backend");
56 }
57
58 fValid = true;
59 return *this;
60 }
61
operator ==(const TextureInfo & that) const62 bool TextureInfo::operator==(const TextureInfo& that) const {
63 if (!this->isValid() && !that.isValid()) {
64 return true;
65 }
66 if (!this->isValid() || !that.isValid()) {
67 return false;
68 }
69
70 if (fBackend != that.fBackend) {
71 return false;
72 }
73
74 if (fSampleCount != that.fSampleCount ||
75 fMipmapped != that.fMipmapped ||
76 fProtected != that.fProtected) {
77 return false;
78 }
79
80 switch (fBackend) {
81 #ifdef SK_DAWN
82 case BackendApi::kDawn:
83 return fDawnSpec == that.fDawnSpec;
84 #endif
85 #ifdef SK_METAL
86 case BackendApi::kMetal:
87 return fMtlSpec == that.fMtlSpec;
88 #endif
89 #ifdef SK_VULKAN
90 case BackendApi::kVulkan:
91 return fVkSpec == that.fVkSpec;
92 #endif
93 default:
94 return false;
95 }
96 }
97
isCompatible(const TextureInfo & that) const98 bool TextureInfo::isCompatible(const TextureInfo& that) const {
99 if (!this->isValid() || !that.isValid()) {
100 return false;
101 }
102
103 if (fSampleCount != that.fSampleCount ||
104 fMipmapped != that.fMipmapped ||
105 fProtected != that.fProtected) {
106 return false;
107 }
108
109 if (fBackend != that.fBackend) {
110 return false;
111 }
112
113 switch (fBackend) {
114 #ifdef SK_DAWN
115 case BackendApi::kDawn:
116 return fDawnSpec.isCompatible(that.fDawnSpec);
117 #endif
118 #ifdef SK_METAL
119 case BackendApi::kMetal:
120 return fMtlSpec.isCompatible(that.fMtlSpec);
121 #endif
122 #ifdef SK_VULKAN
123 case BackendApi::kVulkan:
124 return fVkSpec.isCompatible(that.fVkSpec);
125 #endif
126 default:
127 return false;
128 }
129 }
130
131 #ifdef SK_DAWN
getDawnTextureInfo(DawnTextureInfo * info) const132 bool TextureInfo::getDawnTextureInfo(DawnTextureInfo* info) const {
133 if (!this->isValid() || fBackend != BackendApi::kDawn) {
134 return false;
135 }
136 *info = DawnTextureSpecToTextureInfo(fDawnSpec, fSampleCount, fMipmapped);
137 return true;
138 }
139 #endif
140
toString() const141 SkString TextureInfo::toString() const {
142 SkString ret;
143 switch (fBackend) {
144 #ifdef SK_DAWN
145 case BackendApi::kDawn:
146 ret.appendf("Dawn(%s,", fDawnSpec.toString().c_str());
147 break;
148 #endif
149 #ifdef SK_METAL
150 case BackendApi::kMetal:
151 ret.appendf("Metal(%s,", fMtlSpec.toString().c_str());
152 break;
153 #endif
154 #ifdef SK_VULKAN
155 case BackendApi::kVulkan:
156 ret.appendf("Vulkan(%s,", fVkSpec.toString().c_str());
157 break;
158 #endif
159 case BackendApi::kMock:
160 ret += "Mock(";
161 break;
162 default:
163 ret += "Invalid(";
164 break;
165 }
166 ret.appendf("bytesPerPixel=%zu,sampleCount=%u,mipmapped=%d,protected=%d)",
167 this->bytesPerPixel(),
168 fSampleCount,
169 static_cast<int>(fMipmapped),
170 static_cast<int>(fProtected));
171 return ret;
172 }
173
toRPAttachmentString() const174 SkString TextureInfo::toRPAttachmentString() const {
175 // For renderpass attachments, the string will contain the view format and sample count only
176 switch (fBackend) {
177 #ifdef SK_DAWN
178 case BackendApi::kDawn:
179 return SkStringPrintf("Dawn(f=%u,s=%u)",
180 static_cast<unsigned int>(fDawnSpec.fViewFormat), fSampleCount);
181 #endif
182 #ifdef SK_METAL
183 case BackendApi::kMetal:
184 return SkStringPrintf("Metal(f=%u,s=%u)",
185 static_cast<unsigned int>(fMtlSpec.fFormat), fSampleCount);
186 #endif
187 #ifdef SK_VULKAN
188 case BackendApi::kVulkan:
189 return SkStringPrintf("Vulkan(f%u,s=%u)",
190 static_cast<unsigned int>(fVkSpec.fFormat), fSampleCount);
191 #endif
192 case BackendApi::kMock:
193 return SkStringPrintf("Mock(s=%u)", fSampleCount);
194 default:
195 return SkString("Invalid");
196 }
197 }
198
bytesPerPixel() const199 size_t TextureInfo::bytesPerPixel() const {
200 if (!this->isValid()) {
201 return 0;
202 }
203
204 switch (fBackend) {
205 #ifdef SK_DAWN
206 case BackendApi::kDawn:
207 return DawnFormatBytesPerBlock(this->dawnTextureSpec().getViewFormat());
208 #endif
209 #ifdef SK_METAL
210 case BackendApi::kMetal:
211 return MtlFormatBytesPerBlock(this->mtlTextureSpec().fFormat);
212 #endif
213 #ifdef SK_VULKAN
214 case BackendApi::kVulkan:
215 return VkFormatBytesPerBlock(this->vulkanTextureSpec().fFormat);
216 #endif
217 default:
218 return 0;
219 }
220 }
221
compressionType() const222 SkTextureCompressionType TextureInfo::compressionType() const {
223 if (!this->isValid()) {
224 return SkTextureCompressionType::kNone;
225 }
226
227 switch (fBackend) {
228 #ifdef SK_DAWN
229 case BackendApi::kDawn:
230 return DawnFormatToCompressionType(this->dawnTextureSpec().getViewFormat());
231 #endif
232 #ifdef SK_METAL
233 case BackendApi::kMetal:
234 return MtlFormatToCompressionType(this->mtlTextureSpec().fFormat);
235 #endif
236 #ifdef SK_VULKAN
237 case BackendApi::kVulkan:
238 return VkFormatToCompressionType(this->vulkanTextureSpec().fFormat);
239 #endif
240 default:
241 return SkTextureCompressionType::kNone;
242 }
243 }
244
245 } // namespace skgpu::graphite
246