xref: /aosp_15_r20/external/skia/src/gpu/graphite/Caps.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "src/gpu/graphite/Caps.h"
9 
10 #include "include/core/SkCapabilities.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkTextureCompressionType.h"
13 #include "include/gpu/ShaderErrorHandler.h"
14 #include "include/gpu/graphite/ContextOptions.h"
15 #include "include/gpu/graphite/TextureInfo.h"
16 #include "src/core/SkBlenderBase.h"
17 #include "src/gpu/graphite/GraphiteResourceKey.h"
18 #include "src/gpu/graphite/ResourceTypes.h"
19 #include "src/sksl/SkSLUtil.h"
20 
21 namespace skgpu::graphite {
22 
Caps()23 Caps::Caps()
24         : fShaderCaps(std::make_unique<SkSL::ShaderCaps>())
25         , fCapabilities(new SkCapabilities()) {}
26 
~Caps()27 Caps::~Caps() {}
28 
finishInitialization(const ContextOptions & options)29 void Caps::finishInitialization(const ContextOptions& options) {
30     fCapabilities->initSkCaps(fShaderCaps.get());
31 
32     fDefaultMSAASamples = 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         fMaxTextureAtlasSize = options.fOptionsPriv->fMaxTextureAtlasSize;
44         fRequestedPathRendererStrategy = options.fOptionsPriv->fPathRendererStrategy;
45     }
46 #endif
47     fGlyphCacheTextureMaximumBytes = options.fGlyphCacheTextureMaximumBytes;
48     fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
49     fGlyphsAsPathsFontSize = options.fGlyphsAsPathsFontSize;
50     fMaxPathAtlasTextureSize = options.fMaxPathAtlasTextureSize;
51     fAllowMultipleAtlasTextures = options.fAllowMultipleAtlasTextures;
52     fSupportBilerpFromGlyphAtlas = options.fSupportBilerpFromGlyphAtlas;
53     if (options.fDisableCachedGlyphUploads) {
54         fRequireOrderedRecordings = true;
55     }
56     fSetBackendLabels = options.fSetBackendLabels;
57 }
58 
capabilities() const59 sk_sp<SkCapabilities> Caps::capabilities() const { return fCapabilities; }
60 
getDepthAttachmentDimensions(const TextureInfo & textureInfo,const SkISize colorAttachmentDimensions) const61 SkISize Caps::getDepthAttachmentDimensions(const TextureInfo& textureInfo,
62                                            const SkISize colorAttachmentDimensions) const {
63     return colorAttachmentDimensions;
64 }
65 
isTexturable(const TextureInfo & info) const66 bool Caps::isTexturable(const TextureInfo& info) const {
67     if (info.numSamples() > 1) {
68         return false;
69     }
70     return this->onIsTexturable(info);
71 }
72 
makeSamplerKey(const SamplerDesc & samplerDesc) const73 GraphiteResourceKey Caps::makeSamplerKey(const SamplerDesc& samplerDesc) const {
74     GraphiteResourceKey samplerKey;
75     static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
76     GraphiteResourceKey::Builder builder(&samplerKey, kType, /*data32Count=*/1, Shareable::kYes);
77 
78     // The default impl. of this method adds no additional backend information to the key.
79     builder[0] = samplerDesc.desc();
80 
81     builder.finish();
82     return samplerKey;
83 }
84 
areColorTypeAndTextureInfoCompatible(SkColorType ct,const TextureInfo & info) const85 bool Caps::areColorTypeAndTextureInfoCompatible(SkColorType ct, const TextureInfo& info) const {
86     // TODO: add SkTextureCompressionType handling
87     // (can be handled by setting up the colorTypeInfo instead?)
88 
89     return SkToBool(this->getColorTypeInfo(ct, info));
90 }
91 
color_type_fallback(SkColorType ct)92 static inline SkColorType color_type_fallback(SkColorType ct) {
93     switch (ct) {
94         // kRGBA_8888 is our default fallback for many color types that may not have renderable
95         // backend formats.
96         case kAlpha_8_SkColorType:
97         case kRGB_565_SkColorType:
98         case kARGB_4444_SkColorType:
99         case kBGRA_8888_SkColorType:
100         case kRGBA_1010102_SkColorType:
101         case kBGRA_1010102_SkColorType:
102         case kRGBA_F16_SkColorType:
103         case kRGBA_F16Norm_SkColorType:
104             return kRGBA_8888_SkColorType;
105         case kA16_float_SkColorType:
106             return kRGBA_F16_SkColorType;
107         case kGray_8_SkColorType:
108         case kRGB_F16F16F16x_SkColorType:
109         case kRGB_101010x_SkColorType:
110             return kRGB_888x_SkColorType;
111         default:
112             return kUnknown_SkColorType;
113     }
114 }
115 
getRenderableColorType(SkColorType ct) const116 SkColorType Caps::getRenderableColorType(SkColorType ct) const {
117     do {
118         auto texInfo = this->getDefaultSampledTextureInfo(ct,
119                                                           Mipmapped::kNo,
120                                                           Protected::kNo,
121                                                           Renderable::kYes);
122         // We continue to the fallback color type if there is no default renderable format
123         if (texInfo.isValid() && this->isRenderable(texInfo)) {
124             return ct;
125         }
126         ct = color_type_fallback(ct);
127     } while (ct != kUnknown_SkColorType);
128     return kUnknown_SkColorType;
129 }
130 
getReadSwizzle(SkColorType ct,const TextureInfo & info) const131 skgpu::Swizzle Caps::getReadSwizzle(SkColorType ct, const TextureInfo& info) const {
132     // TODO: add SkTextureCompressionType handling
133     // (can be handled by setting up the colorTypeInfo instead?)
134 
135     auto colorTypeInfo = this->getColorTypeInfo(ct, info);
136     if (!colorTypeInfo) {
137         SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
138         return {};
139     }
140 
141     return colorTypeInfo->fReadSwizzle;
142 }
143 
getWriteSwizzle(SkColorType ct,const TextureInfo & info) const144 skgpu::Swizzle Caps::getWriteSwizzle(SkColorType ct, const TextureInfo& info) const {
145     auto colorTypeInfo = this->getColorTypeInfo(ct, info);
146     if (!colorTypeInfo) {
147         SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
148         return {};
149     }
150 
151     return colorTypeInfo->fWriteSwizzle;
152 }
153 
getDstReadRequirement() const154 DstReadRequirement Caps::getDstReadRequirement() const {
155     // TODO(b/238757201): Currently this only supports dst reads by FB fetch and texture copy.
156     if (this->shaderCaps()->fFBFetchSupport) {
157         return DstReadRequirement::kFramebufferFetch;
158     } else {
159         return DstReadRequirement::kTextureCopy;
160     }
161 }
162 
getSubRunControl(bool useSDFTForSmallText) const163 sktext::gpu::SubRunControl Caps::getSubRunControl(bool useSDFTForSmallText) const {
164 #if !defined(SK_DISABLE_SDF_TEXT)
165     return sktext::gpu::SubRunControl{
166             this->shaderCaps()->supportsDistanceFieldText(),
167             useSDFTForSmallText,
168             true, /*ableToUsePerspectiveSDFT*/
169             this->minDistanceFieldFontSize(),
170             this->glyphsAsPathsFontSize(),
171             true /*forcePathAA*/};
172 #else
173     return sktext::gpu::SubRunControl{/*forcePathAA=*/true};
174 #endif
175 }
176 
177 } // namespace skgpu::graphite
178