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 #include "src/gpu/graphite/TextureInfoPriv.h"
11
12 namespace skgpu::graphite {
13
TextureInfo()14 TextureInfo::TextureInfo(){};
15 TextureInfo::~TextureInfo() = default;
16
assert_is_supported_backend(const BackendApi & backend)17 static inline void assert_is_supported_backend(const BackendApi& backend) {
18 SkASSERT(backend == BackendApi::kDawn ||
19 backend == BackendApi::kMetal ||
20 backend == BackendApi::kVulkan);
21 }
22
TextureInfo(const TextureInfo & that)23 TextureInfo::TextureInfo(const TextureInfo& that)
24 : fBackend(that.fBackend)
25 , fValid(that.fValid)
26 , fSampleCount(that.fSampleCount)
27 , fMipmapped(that.fMipmapped)
28 , fProtected(that.fProtected) {
29 if (!fValid) {
30 return;
31 }
32
33 assert_is_supported_backend(fBackend);
34 fTextureInfoData.reset();
35 that.fTextureInfoData->copyTo(fTextureInfoData);
36 }
37
operator =(const TextureInfo & that)38 TextureInfo& TextureInfo::operator=(const TextureInfo& that) {
39 if (this != &that) {
40 this->~TextureInfo();
41 new (this) TextureInfo(that);
42 }
43 return *this;
44 }
45
operator ==(const TextureInfo & that) const46 bool TextureInfo::operator==(const TextureInfo& that) const {
47 if (!this->isValid() && !that.isValid()) {
48 return true;
49 }
50 if (!this->isValid() || !that.isValid()) {
51 return false;
52 }
53
54 if (fBackend != that.fBackend) {
55 return false;
56 }
57
58 if (fSampleCount != that.fSampleCount ||
59 fMipmapped != that.fMipmapped ||
60 fProtected != that.fProtected) {
61 return false;
62 }
63 assert_is_supported_backend(fBackend);
64 return fTextureInfoData->equal(that.fTextureInfoData.get());
65 }
66
isCompatible(const TextureInfo & that) const67 bool TextureInfo::isCompatible(const TextureInfo& that) const {
68 if (!this->isValid() || !that.isValid()) {
69 return false;
70 }
71
72 if (fSampleCount != that.fSampleCount ||
73 fMipmapped != that.fMipmapped ||
74 fProtected != that.fProtected) {
75 return false;
76 }
77
78 if (fBackend != that.fBackend) {
79 return false;
80 }
81 assert_is_supported_backend(fBackend);
82 return fTextureInfoData->isCompatible(that.fTextureInfoData.get());
83 }
84
toString() const85 SkString TextureInfo::toString() const {
86 if (!fValid) {
87 return SkString("{}");
88 }
89
90 SkString ret;
91 switch (fBackend) {
92 case BackendApi::kDawn:
93 case BackendApi::kMetal:
94 case BackendApi::kVulkan:
95 ret = fTextureInfoData->toString();
96 break;
97 case BackendApi::kMock:
98 ret += "Mock(";
99 break;
100 case BackendApi::kUnsupported:
101 ret += "Unsupported(";
102 break;
103 }
104 ret.appendf("bytesPerPixel=%zu,sampleCount=%u,mipmapped=%d,protected=%d)",
105 this->bytesPerPixel(),
106 fSampleCount,
107 static_cast<int>(fMipmapped),
108 static_cast<int>(fProtected));
109 return ret;
110 }
111
toRPAttachmentString() const112 SkString TextureInfo::toRPAttachmentString() const {
113 if (!fValid) {
114 return SkString("{}");
115 }
116
117 // For renderpass attachments, the string will contain the view format and sample count only
118 switch (fBackend) {
119 case BackendApi::kDawn:
120 case BackendApi::kMetal:
121 case BackendApi::kVulkan:
122 return fTextureInfoData->toRPAttachmentString(fSampleCount);
123 case BackendApi::kMock:
124 return SkStringPrintf("Mock(s=%u)", fSampleCount);
125 case BackendApi::kUnsupported:
126 return SkString("Unsupported");
127 }
128 SkUNREACHABLE;
129 }
130
bytesPerPixel() const131 size_t TextureInfo::bytesPerPixel() const {
132 if (!this->isValid()) {
133 return 0;
134 }
135
136 switch (fBackend) {
137 case BackendApi::kDawn:
138 case BackendApi::kMetal:
139 case BackendApi::kVulkan:
140 return fTextureInfoData->bytesPerPixel();
141 default:
142 return 0;
143 }
144 }
145
compressionType() const146 SkTextureCompressionType TextureInfo::compressionType() const {
147 if (!this->isValid()) {
148 return SkTextureCompressionType::kNone;
149 }
150
151 switch (fBackend) {
152 case BackendApi::kDawn:
153 case BackendApi::kMetal:
154 case BackendApi::kVulkan:
155 return fTextureInfoData->compressionType();
156 default:
157 return SkTextureCompressionType::kNone;
158 }
159 }
160
isMemoryless() const161 bool TextureInfo::isMemoryless() const {
162 if (!this->isValid()) {
163 return false;
164 }
165
166 switch (fBackend) {
167 case BackendApi::kDawn:
168 case BackendApi::kMetal:
169 case BackendApi::kVulkan:
170 return fTextureInfoData->isMemoryless();
171 default:
172 return false;
173 }
174 }
175
~TextureInfoData()176 TextureInfoData::~TextureInfoData(){};
177
178 } // namespace skgpu::graphite
179