Flutter Engine
The Flutter Engine
TextureInfo.cpp
Go to the documentation of this file.
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
9
10#ifdef SK_DAWN
12#endif
13
14#ifdef SK_METAL
15namespace skgpu::graphite {
16 // Including Metal types/headers here is tricky. This is defined in MtlGraphiteUtils.mm
19}
20#endif
21
22#ifdef SK_VULKAN
24#endif
25
26namespace skgpu::graphite {
27
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
41 fDawnSpec = that.fDawnSpec;
42 break;
43#endif
44#ifdef SK_METAL
46 fMtlSpec = that.fMtlSpec;
47 break;
48#endif
49#ifdef SK_VULKAN
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
62bool 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
83 return fDawnSpec == that.fDawnSpec;
84#endif
85#ifdef SK_METAL
87 return fMtlSpec == that.fMtlSpec;
88#endif
89#ifdef SK_VULKAN
91 return fVkSpec == that.fVkSpec;
92#endif
93 default:
94 return false;
95 }
96}
97
98bool 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
116 return fDawnSpec.isCompatible(that.fDawnSpec);
117#endif
118#ifdef SK_METAL
120 return fMtlSpec.isCompatible(that.fMtlSpec);
121#endif
122#ifdef SK_VULKAN
124 return fVkSpec.isCompatible(that.fVkSpec);
125#endif
126 default:
127 return false;
128 }
129}
130
131#ifdef SK_DAWN
132bool 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
142 SkString ret;
143 switch (fBackend) {
144#ifdef SK_DAWN
146 ret.appendf("Dawn(%s,", fDawnSpec.toString().c_str());
147 break;
148#endif
149#ifdef SK_METAL
151 ret.appendf("Metal(%s,", fMtlSpec.toString().c_str());
152 break;
153#endif
154#ifdef SK_VULKAN
156 ret.appendf("Vulkan(%s,", fVkSpec.toString().c_str());
157 break;
158#endif
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
175 // For renderpass attachments, the string will contain the view format and sample count only
176 switch (fBackend) {
177#ifdef SK_DAWN
179 return SkStringPrintf("Dawn(f=%u,s=%u)",
180 static_cast<unsigned int>(fDawnSpec.fViewFormat), fSampleCount);
181#endif
182#ifdef SK_METAL
184 return SkStringPrintf("Metal(f=%u,s=%u)",
185 static_cast<unsigned int>(fMtlSpec.fFormat), fSampleCount);
186#endif
187#ifdef SK_VULKAN
189 return SkStringPrintf("Vulkan(f%u,s=%u)",
190 static_cast<unsigned int>(fVkSpec.fFormat), fSampleCount);
191#endif
193 return SkStringPrintf("Mock(s=%u)", fSampleCount);
194 default:
195 return SkString("Invalid");
196 }
197}
198
199size_t TextureInfo::bytesPerPixel() const {
200 if (!this->isValid()) {
201 return 0;
202 }
203
204 switch (fBackend) {
205#ifdef SK_DAWN
207 return DawnFormatBytesPerBlock(this->dawnTextureSpec().getViewFormat());
208#endif
209#ifdef SK_METAL
211 return MtlFormatBytesPerBlock(this->mtlTextureSpec().fFormat);
212#endif
213#ifdef SK_VULKAN
215 return VkFormatBytesPerBlock(this->vulkanTextureSpec().fFormat);
216#endif
217 default:
218 return 0;
219 }
220}
221
223 if (!this->isValid()) {
225 }
226
227 switch (fBackend) {
228#ifdef SK_DAWN
230 return DawnFormatToCompressionType(this->dawnTextureSpec().getViewFormat());
231#endif
232#ifdef SK_METAL
234 return MtlFormatToCompressionType(this->mtlTextureSpec().fFormat);
235#endif
236#ifdef SK_VULKAN
238 return VkFormatToCompressionType(this->vulkanTextureSpec().fFormat);
239#endif
240 default:
242 }
243}
244
245} // namespace skgpu::graphite
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
SkTextureCompressionType
void void void appendf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:550
SkTextureCompressionType compressionType() const
TextureInfo & operator=(const TextureInfo &)
Definition: TextureInfo.cpp:28
bool isCompatible(const TextureInfo &that) const
Definition: TextureInfo.cpp:98
BackendApi backend() const
Definition: TextureInfo.h:76
bool operator==(const TextureInfo &) const
Definition: TextureInfo.cpp:62
SkString toRPAttachmentString() const
unsigned int MtlPixelFormat
SkTextureCompressionType DawnFormatToCompressionType(wgpu::TextureFormat format)
Definition: DawnUtils.cpp:55
DawnTextureInfo DawnTextureSpecToTextureInfo(const DawnTextureSpec &dawnSpec, uint32_t sampleCount, Mipmapped mipmapped)
size_t MtlFormatBytesPerBlock(MtlPixelFormat format)
size_t DawnFormatBytesPerBlock(wgpu::TextureFormat format)
Definition: DawnUtils.cpp:21
SkTextureCompressionType MtlFormatToCompressionType(MtlPixelFormat format)
static constexpr size_t VkFormatBytesPerBlock(VkFormat vkFormat)
static constexpr SkTextureCompressionType VkFormatToCompressionType(VkFormat vkFormat)