• 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(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     fRequireOrderedRecordings = options.fRequireOrderedRecordings;
54     fSetBackendLabels = options.fSetBackendLabels;
55 }
56 
capabilities() const57 sk_sp<SkCapabilities> Caps::capabilities() const { return fCapabilities; }
58 
getDepthAttachmentDimensions(const TextureInfo & textureInfo,const SkISize colorAttachmentDimensions) const59 SkISize Caps::getDepthAttachmentDimensions(const TextureInfo& textureInfo,
60                                            const SkISize colorAttachmentDimensions) const {
61     return colorAttachmentDimensions;
62 }
63 
isTexturable(const TextureInfo & info) const64 bool Caps::isTexturable(const TextureInfo& info) const {
65     if (info.numSamples() > 1) {
66         return false;
67     }
68     return this->onIsTexturable(info);
69 }
70 
makeSamplerKey(const SamplerDesc & samplerDesc) const71 GraphiteResourceKey Caps::makeSamplerKey(const SamplerDesc& samplerDesc) const {
72     GraphiteResourceKey samplerKey;
73     static const ResourceType kType = GraphiteResourceKey::GenerateResourceType();
74     GraphiteResourceKey::Builder builder(&samplerKey, kType, /*data32Count=*/1, Shareable::kYes);
75 
76     // The default impl. of this method adds no additional backend information to the key.
77     builder[0] = samplerDesc.desc();
78 
79     builder.finish();
80     return samplerKey;
81 }
82 
areColorTypeAndTextureInfoCompatible(SkColorType ct,const TextureInfo & info) const83 bool Caps::areColorTypeAndTextureInfoCompatible(SkColorType ct, const TextureInfo& info) const {
84     // TODO: add SkTextureCompressionType handling
85     // (can be handled by setting up the colorTypeInfo instead?)
86 
87     return SkToBool(this->getColorTypeInfo(ct, info));
88 }
89 
color_type_fallback(SkColorType ct)90 static inline SkColorType color_type_fallback(SkColorType ct) {
91     switch (ct) {
92         // kRGBA_8888 is our default fallback for many color types that may not have renderable
93         // backend formats.
94         case kAlpha_8_SkColorType:
95         case kRGB_565_SkColorType:
96         case kARGB_4444_SkColorType:
97         case kBGRA_8888_SkColorType:
98         case kRGBA_1010102_SkColorType:
99         case kBGRA_1010102_SkColorType:
100         case kRGBA_F16_SkColorType:
101         case kRGBA_F16Norm_SkColorType:
102             return kRGBA_8888_SkColorType;
103         case kA16_float_SkColorType:
104             return kRGBA_F16_SkColorType;
105         case kGray_8_SkColorType:
106         case kRGB_F16F16F16x_SkColorType:
107         case kRGB_101010x_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 
getSubRunControl(bool useSDFTForSmallText) const161 sktext::gpu::SubRunControl Caps::getSubRunControl(bool useSDFTForSmallText) const {
162 #if !defined(SK_DISABLE_SDF_TEXT)
163     return sktext::gpu::SubRunControl{
164             this->shaderCaps()->supportsDistanceFieldText(),
165             useSDFTForSmallText,
166             true, /*ableToUsePerspectiveSDFT*/
167             this->minDistanceFieldFontSize(),
168             this->glyphsAsPathsFontSize(),
169             true /*forcePathAA*/};
170 #else
171     return sktext::gpu::SubRunControl{/*forcePathAA=*/true};
172 #endif
173 }
174 
175 } // namespace skgpu::graphite
176