Debian Codegraph @

src/gpu/graphite/Caps.cpp

raw
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 #include "src/gpu/graphite/Caps.h"
8
9 #include "include/core/SkCapabilities.h"
10 #include "include/gpu/ShaderErrorHandler.h"
11 #include "include/gpu/graphite/ContextOptions.h"
12 #include "include/gpu/graphite/TextureInfo.h"
13 #include "include/private/base/SkTo.h"
14 #include "src/gpu/graphite/ContextOptionsPriv.h"
15 #include "src/gpu/graphite/ResourceTypes.h"
16 #include "src/gpu/graphite/TextureInfoPriv.h"
17 #include "src/sksl/SkSLUtil.h"
18
19 #include <algorithm>
20
21 namespace skgpu::graphite {
22
23 Caps::Caps()
24 : fShaderCaps(std::make_unique<SkSL::ShaderCaps>())
25 , fCapabilities(new SkCapabilities()) {}
26
27 Caps::~Caps() {}
28
29 void Caps::finishInitialization(const ContextOptions& options) {
30 fCapabilities->initSkCaps(fShaderCaps.get());
31
32 fMaxInternalSampleCount = options.fInternalMultisampleCount;
33
34 if (options.fShaderErrorHandler) {
35 fShaderErrorHandler = options.fShaderErrorHandler;
36 } else {
37 fShaderErrorHandler = DefaultShaderErrorHandler();
38 }
39
40 #if defined(GPU_TEST_UTILS)
41 if (options.fOptionsPriv) {
42 fMaxTextureSize = std::min(fMaxTextureSize, options.fOptionsPriv->fMaxTextureSizeOverride);
43 fRequestedPathRendererStrategy = options.fOptionsPriv->fPathRendererStrategy;
44 }
45 #endif
46 fGlyphCacheTextureMaximumBytes = options.fGlyphCacheTextureMaximumBytes;
47 fMinMSAAPathSize = options.fMinimumPathSizeForMSAA;
48 fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
49 fGlyphsAsPathsFontSize = options.fGlyphsAsPathsFontSize;
50 fMaxPathAtlasTextureSize = options.fMaxPathAtlasTextureSize;
51 fAllowMultipleAtlasTextures = options.fAllowMultipleAtlasTextures;
52 fSupportBilerpFromGlyphAtlas = options.fSupportBilerpFromGlyphAtlas;
53 fRequireOrderedRecordings = options.fRequireOrderedRecordings;
54 fSetBackendLabels = options.fSetBackendLabels;
55 }
56
57 sk_sp<SkCapabilities> Caps::capabilities() const { return fCapabilities; }
58
59 SkISize Caps::getDepthAttachmentDimensions(const TextureInfo& textureInfo,
60 const SkISize colorAttachmentDimensions) const {
61 return colorAttachmentDimensions;
62 }
63
64 bool Caps::isTexturable(const TextureInfo& info) const {
65 if (info.sampleCount() > SampleCount::k1) {
66 return false;
67 }
68 return this->onIsTexturable(info);
69 }
70
71 bool Caps::areColorTypeAndTextureInfoCompatible(SkColorType ct, const TextureInfo& info) const {
72 // TODO: add SkTextureCompressionType handling
73 // (can be handled by setting up the colorTypeInfo instead?)
74
75 return SkToBool(this->getColorTypeInfo(ct, info));
76 }
77
78 static inline SkColorType color_type_fallback(SkColorType ct) {
79 switch (ct) {
80 // kRGBA_8888 is our default fallback for many color types that may not have renderable
81 // backend formats.
82 case kAlpha_8_SkColorType:
83 case kRGB_565_SkColorType:
84 case kARGB_4444_SkColorType:
85 case kBGRA_8888_SkColorType:
86 case kRGBA_1010102_SkColorType:
87 case kBGRA_1010102_SkColorType:
88 case kRGBA_F16_SkColorType:
89 case kRGBA_F16Norm_SkColorType:
90 return kRGBA_8888_SkColorType;
91 case kA16_float_SkColorType:
92 return kRGBA_F16_SkColorType;
93 case kGray_8_SkColorType:
94 case kRGB_F16F16F16x_SkColorType:
95 case kRGB_101010x_SkColorType:
96 return kRGB_888x_SkColorType;
97 default:
98 return kUnknown_SkColorType;
99 }
100 }
101
102 const Caps::ColorTypeInfo* Caps::getColorTypeInfo(SkColorType ct, const TextureInfo& info) const {
103 if (!info.isValid()) {
104 return nullptr;
105 }
106
107 for (const ColorTypeInfo& colorInfo : this->getColorTypeInfos(info)) {
108 if (colorInfo.fColorType == ct) {
109 return &colorInfo;
110 }
111 }
112 return nullptr;
113 }
114
115 SkColorType Caps::getDefaultColorType(const TextureInfo& info) const {
116 if (!info.isValid()) {
117 return kUnknown_SkColorType;
118 }
119
120 const bool isRenderable = this->isRenderable(info);
121 for (const ColorTypeInfo& colorInfo : this->getColorTypeInfos(info)) {
122 if (!isRenderable || (colorInfo.fFlags & ColorTypeInfo::kRenderable_Flag)) {
123 return colorInfo.fColorType;
124 }
125 }
126 return kUnknown_SkColorType;
127 }
128
129 SkColorType Caps::getRenderableColorType(SkColorType ct) const {
130 do {
131 auto texInfo = this->getDefaultSampledTextureInfo(ct,
132 Mipmapped::kNo,
133 Protected::kNo,
134 Renderable::kYes);
135 // We continue to the fallback color type if there is no default renderable format
136 if (texInfo.isValid() && this->isRenderable(texInfo)) {
137 return ct;
138 }
139 ct = color_type_fallback(ct);
140 } while (ct != kUnknown_SkColorType);
141 return kUnknown_SkColorType;
142 }
143
144 SampleCount Caps::getCompatibleMSAASampleCount(const TextureInfo& info) const {
145 if (info.sampleCount() > SampleCount::k1) {
146 // Use the inherent sample count since it's already MSAA
147 return info.sampleCount();
148 } else if (!this->avoidMSAA()) {
149 // The max internal sample count may be higher than what is universally supported for
150 // every renderable TextureFormat, but unless avoidMSAA() was true, this should bottom out
151 // at SampleCount::k4.
152 TextureFormat format = TextureInfoPriv::ViewFormat(info);
153 for (SampleCount s = fMaxInternalSampleCount;
154 s > SampleCount::k1;
155 s = static_cast<SampleCount>((uint8_t)s >> 1)) {
156 if (this->isSampleCountSupported(format, s)) {
157 return s;
158 }
159 }
160 }
161
162 // If we got here, MSAA has been disabled somehow (by ContextOption, driver workaround, or
163 // no support for a particular TextureFormat).
164 return SampleCount::k1;
165 }
166
167 skgpu::Swizzle Caps::getReadSwizzle(SkColorType ct, const TextureInfo& info) const {
168 // TODO: add SkTextureCompressionType handling
169 // (can be handled by setting up the colorTypeInfo instead?)
170
171 auto colorTypeInfo = this->getColorTypeInfo(ct, info);
172 if (!colorTypeInfo) {
173 SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
174 return {};
175 }
176
177 return colorTypeInfo->fReadSwizzle;
178 }
179
180 skgpu::Swizzle Caps::getWriteSwizzle(SkColorType ct, const TextureInfo& info) const {
181 auto colorTypeInfo = this->getColorTypeInfo(ct, info);
182 if (!colorTypeInfo) {
183 SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
184 return {};
185 }
186
187 return colorTypeInfo->fWriteSwizzle;
188 }
189
190 std::pair<SkColorType, bool /*isRGBFormat*/> Caps::supportedTransferColorType(
191 SkColorType colorType,
192 const TextureInfo& textureInfo) const {
193 // NOTE: Compressed textures can't be read back, and external format textures can't be read or
194 // written to. However, this is not checked here. Instead that is expected to be handled by
195 // supports[Read|Write]Pixels().
196 const ColorTypeInfo* colorInfo = this->getColorTypeInfo(colorType, textureInfo);
197 if (colorInfo) {
198 const TextureFormat format = TextureInfoPriv::ViewFormat(textureInfo);
199 const bool rgbRequiresIntervention =
200 TextureFormatChannelMask(format) == kRGB_SkColorChannelFlags &&
201 colorInfo->fTransferColorType != kRGB_565_SkColorType;
202 return {colorInfo->fTransferColorType, rgbRequiresIntervention};
203 } else {
204 return {kUnknown_SkColorType, false};
205 }
206 }
207
208 DstReadStrategy Caps::getDstReadStrategy() const {
209 // TODO(b/238757201; b/383769988): Dst reads are currently only supported by FB fetch and
210 // texture copy.
211 if (this->shaderCaps()->fFBFetchSupport) {
212 return DstReadStrategy::kFramebufferFetch;
213 } else {
214 return DstReadStrategy::kTextureCopy;
215 }
216 }
217
218 sktext::gpu::SubRunControl Caps::getSubRunControl(bool useSDFTForSmallText) const {
219 #if !defined(SK_DISABLE_SDF_TEXT)
220 return sktext::gpu::SubRunControl{
221 this->shaderCaps()->supportsDistanceFieldText(),
222 useSDFTForSmallText,
223 true, /*ableToUsePerspectiveSDFT*/
224 this->minDistanceFieldFontSize(),
225 this->glyphsAsPathsFontSize(),
226 true /*forcePathAA*/};
227 #else
228 return sktext::gpu::SubRunControl{/*forcePathAA=*/true};
229 #endif
230 }
231
232 } // namespace skgpu::graphite