• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(GRAPHITE_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             return kRGB_888x_SkColorType;
109         default:
110             return kUnknown_SkColorType;
111     }
112 }
113 
getRenderableColorType(SkColorType ct) const114 SkColorType Caps::getRenderableColorType(SkColorType ct) const {
115     do {
116         auto texInfo = this->getDefaultSampledTextureInfo(ct,
117                                                           Mipmapped::kNo,
118                                                           Protected::kNo,
119                                                           Renderable::kYes);
120         // We continue to the fallback color type if there is no default renderable format
121         if (texInfo.isValid() && this->isRenderable(texInfo)) {
122             return ct;
123         }
124         ct = color_type_fallback(ct);
125     } while (ct != kUnknown_SkColorType);
126     return kUnknown_SkColorType;
127 }
128 
getReadSwizzle(SkColorType ct,const TextureInfo & info) const129 skgpu::Swizzle Caps::getReadSwizzle(SkColorType ct, const TextureInfo& info) const {
130     // TODO: add SkTextureCompressionType handling
131     // (can be handled by setting up the colorTypeInfo instead?)
132 
133     auto colorTypeInfo = this->getColorTypeInfo(ct, info);
134     if (!colorTypeInfo) {
135         SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
136         return {};
137     }
138 
139     return colorTypeInfo->fReadSwizzle;
140 }
141 
getWriteSwizzle(SkColorType ct,const TextureInfo & info) const142 skgpu::Swizzle Caps::getWriteSwizzle(SkColorType ct, const TextureInfo& info) const {
143     auto colorTypeInfo = this->getColorTypeInfo(ct, info);
144     if (!colorTypeInfo) {
145         SkDEBUGFAILF("Illegal color type (%d) and format combination.", static_cast<int>(ct));
146         return {};
147     }
148 
149     return colorTypeInfo->fWriteSwizzle;
150 }
151 
getDstReadRequirement() const152 DstReadRequirement Caps::getDstReadRequirement() const {
153     // TODO(b/238757201): Currently this only supports dst reads by FB fetch and texture copy.
154     if (this->shaderCaps()->fFBFetchSupport) {
155         return DstReadRequirement::kFramebufferFetch;
156     } else {
157         return DstReadRequirement::kTextureCopy;
158     }
159 }
160 
getSDFTControl(bool useSDFTForSmallText) const161 sktext::gpu::SDFTControl Caps::getSDFTControl(bool useSDFTForSmallText) const {
162 #if !defined(SK_DISABLE_SDF_TEXT)
163     return sktext::gpu::SDFTControl{
164             this->shaderCaps()->supportsDistanceFieldText(),
165             useSDFTForSmallText,
166             true, /*ableToUsePerspectiveSDFT*/
167             this->minDistanceFieldFontSize(),
168             this->glyphsAsPathsFontSize()};
169 #else
170     return sktext::gpu::SDFTControl{};
171 #endif
172 }
173 
174 } // namespace skgpu::graphite
175