Flutter Engine
The Flutter Engine
Caps.h
Go to the documentation of this file.
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#ifndef skgpu_graphite_Caps_DEFINED
9#define skgpu_graphite_Caps_DEFINED
10
11#include <optional>
12#include <string>
13#include <string_view>
14
19#include "src/gpu/ResourceKey.h"
20#include "src/gpu/Swizzle.h"
24
25#if defined(GRAPHITE_TEST_UTILS)
27#endif
28
29enum class SkBlendMode;
31class SkCapabilities;
32
33namespace SkSL { struct ShaderCaps; }
34
35namespace skgpu { class ShaderErrorHandler; }
36
37namespace skgpu::graphite {
38
39enum class BufferType : int;
40struct ContextOptions;
41class ComputePipelineDesc;
42class GraphicsPipelineDesc;
43class GraphiteResourceKey;
44class RendererProvider;
45struct RenderPassDesc;
46class TextureInfo;
47
49 // The required data layout rules for the contents of a uniform buffer.
51
52 // The required data layout rules for the contents of a storage buffer.
54
55 // Whether combined texture-sampler types are supported. Backends that do not support
56 // combined image samplers (i.e. sampler2D) require a texture and sampler object to be bound
57 // separately and their binding indices explicitly specified in the shader text.
59
60 // Whether buffer, texture, and sampler resource bindings use distinct index ranges.
62};
63
65 kNone,
69};
70
71class Caps {
72public:
73 virtual ~Caps();
74
75 const SkSL::ShaderCaps* shaderCaps() const { return fShaderCaps.get(); }
76
78
79#if defined(GRAPHITE_TEST_UTILS)
80 std::string_view deviceName() const { return fDeviceName; }
81
82 PathRendererStrategy requestedPathRendererStrategy() const {
83 return fRequestedPathRendererStrategy;
84 }
85#endif
86
88 Mipmapped mipmapped,
90 Renderable) const = 0;
91
93 Mipmapped mipmapped) const = 0;
94
96 Mipmapped mipmapped,
97 Protected) const = 0;
98
99 virtual TextureInfo getDefaultMSAATextureInfo(const TextureInfo& singleSampledInfo,
100 Discardable discardable) const = 0;
101
103 uint32_t sampleCount,
104 Protected) const = 0;
105
107
108 // Get required depth attachment dimensions for a givin color attachment info and dimensions.
110 const SkISize colorAttachmentDimensions) const;
111
113 const RenderPassDesc&) const = 0;
115
116 // Returns a GraphiteResourceKey based upon a SamplerDesc with any additional information that
117 // backends append within their implementation. By default, simply returns a key based upon
118 // the SamplerDesc with no extra info.
119 // TODO: Rather than going through a GraphiteResourceKey, migrate to having a cache of samplers
120 // keyed off of SamplerDesc to minimize heap allocations.
121 virtual GraphiteResourceKey makeSamplerKey(const SamplerDesc& samplerDesc) const;
122
123 // Backends can optionally override this method to return meaningful sampler conversion info.
124 // By default, simply return a default ImmutableSamplerInfo.
126 return {};
127 }
128
129 virtual bool extractGraphicsDescs(const UniqueKey&,
132 const RendererProvider*) const { return false; }
133
135 virtual uint32_t channelMask(const TextureInfo&) const = 0;
136
137 bool isTexturable(const TextureInfo&) const;
138 virtual bool isRenderable(const TextureInfo&) const = 0;
139 virtual bool isStorage(const TextureInfo&) const = 0;
140
141 int maxTextureSize() const { return fMaxTextureSize; }
143
144 virtual void buildKeyForTexture(SkISize dimensions,
145 const TextureInfo&,
147 Shareable,
148 GraphiteResourceKey*) const = 0;
149
152 }
153
154 // Returns the required alignment in bytes for the offset into a uniform buffer when binding it
155 // to a draw.
157
158 // Returns the required alignment in bytes for the offset into a storage buffer when binding it
159 // to a draw.
161
162 // Returns the required alignment in bytes for the offset and size of copies involving a buffer.
164
165 // Returns the aligned rowBytes when transfering to or from a Texture
166 size_t getAlignedTextureDataRowBytes(size_t rowBytes) const {
168 }
169
170 /**
171 * Backends may have restrictions on what types of textures support Device::writePixels().
172 * If this returns false then the caller should implement a fallback where a temporary texture
173 * is created, pixels are written to it, and then that is copied or drawn into the the surface.
174 */
175 virtual bool supportsWritePixels(const TextureInfo& textureInfo) const = 0;
176
177 /**
178 * Backends may have restrictions on what types of textures support Device::readPixels().
179 * If this returns false then the caller should implement a fallback where a temporary texture
180 * is created, the original texture is copied or drawn into it, and then pixels read from
181 * the temporary texture.
182 */
183 virtual bool supportsReadPixels(const TextureInfo& textureInfo) const = 0;
184
185 /**
186 * Given a dst pixel config and a src color type what color type must the caller coax the
187 * the data into in order to use writePixels.
188 *
189 * We currently don't have an SkColorType for a 3 channel RGB format. Additionally the current
190 * implementation of raster pipeline requires power of 2 channels, so it is not easy to add such
191 * an SkColorType. Thus we need to check for data that is 3 channels using the isRGBFormat
192 * return value and handle it manually
193 */
194 virtual std::pair<SkColorType, bool /*isRGB888Format*/> supportedWritePixelsColorType(
195 SkColorType dstColorType,
196 const TextureInfo& dstTextureInfo,
197 SkColorType srcColorType) const = 0;
198
199 /**
200 * Given a src surface's color type and its texture info as well as a color type the caller
201 * would like read into, this provides a legal color type that the caller can use for
202 * readPixels. The returned color type may differ from the passed dstColorType, in
203 * which case the caller must convert the read pixel data (see GrConvertPixels). When converting
204 * to dstColorType the swizzle in the returned struct should be applied. The caller must check
205 * the returned color type for kUnknown.
206 *
207 * We currently don't have an SkColorType for a 3 channel RGB format. Additionally the current
208 * implementation of raster pipeline requires power of 2 channels, so it is not easy to add such
209 * an SkColorType. Thus we need to check for data that is 3 channels using the isRGBFormat
210 * return value and handle it manually
211 */
212 virtual std::pair<SkColorType, bool /*isRGBFormat*/> supportedReadPixelsColorType(
213 SkColorType srcColorType,
214 const TextureInfo& srcTextureInfo,
215 SkColorType dstColorType) const = 0;
216
217 /**
218 * Checks whether the passed color type is renderable. If so, the same color type is passed
219 * back. If not, provides an alternative (perhaps lower bit depth and/or unorm instead of float)
220 * color type that is supported or kUnknown if there no renderable fallback format.
221 */
223
225
226 bool protectedSupport() const { return fProtectedSupport; }
227
228 // Supports BackendSemaphores
229 bool semaphoreSupport() const { return fSemaphoreSupport; }
230
231 // If false then calling Context::submit with SyncToCpu::kYes is an error.
232 bool allowCpuSync() const { return fAllowCpuSync; }
233
234 // Returns whether storage buffers are supported.
236
237 // Returns whether storage buffers are preferred over uniform buffers, when both will yield
238 // correct results.
240
241 // Returns whether a draw buffer can be mapped.
243
244#if defined(GRAPHITE_TEST_UTILS)
245 bool drawBufferCanBeMappedForReadback() const { return fDrawBufferCanBeMappedForReadback; }
246#endif
247
248 // Returns whether using Buffer::asyncMap() must be used to map buffers. map() may only be
249 // called after asyncMap() is called and will fail if the asynchronous map is not complete. This
250 // excludes premapped buffers for which map() can be called freely until the first unmap() call.
252
253 // Returns whether multisampled render to single sampled is supported.
255
256 // Returns whether compute shaders are supported.
257 bool computeSupport() const { return fComputeSupport; }
258
259 /**
260 * Returns true if the given backend supports importing AHardwareBuffers. This will only
261 * ever be supported on Android devices with API level >= 26.
262 */
264
265 // Returns the skgpu::Swizzle to use when sampling or reading back from a texture with the
266 // passed in SkColorType and TextureInfo.
268
269 // Returns the skgpu::Swizzle to use when writing colors to a surface with the passed in
270 // SkColorType and TextureInfo.
272
274
275 // Returns what method of dst read is required for a draw using the dst color.
277
280
283
286
288
289 // When uploading to a full compressed texture do we need to pad the size out to a multiple of
290 // the block width and height.
293 }
294
295 sktext::gpu::SDFTControl getSDFTControl(bool useSDFTForSmallText) const;
296
297 bool setBackendLabels() const { return fSetBackendLabels; }
298
299protected:
300 Caps();
301
302 // Subclasses must call this at the end of their init method in order to do final processing on
303 // the caps.
305
306#if defined(GRAPHITE_TEST_UTILS)
307 void setDeviceName(const char* n) {
308 fDeviceName = n;
309 }
310#endif
311
312 // There are only a few possible valid sample counts (1, 2, 4, 8, 16). So we can key on those 5
313 // options instead of the actual sample value.
314 static inline uint32_t SamplesToKey(uint32_t numSamples) {
315 switch (numSamples) {
316 case 1:
317 return 0;
318 case 2:
319 return 1;
320 case 4:
321 return 2;
322 case 8:
323 return 3;
324 case 16:
325 return 4;
326 default:
328 }
329 }
330
331 // ColorTypeInfo for a specific format. Used in format tables.
333 ColorTypeInfo() = default;
334 ColorTypeInfo(SkColorType ct, SkColorType transferCt, uint32_t flags,
335 skgpu::Swizzle readSwizzle, skgpu::Swizzle writeSwizzle)
336 : fColorType(ct)
337 , fTransferColorType(transferCt)
338 , fFlags(flags)
339 , fReadSwizzle(readSwizzle)
340 , fWriteSwizzle(writeSwizzle) {}
341
344 enum {
346 // Does Graphite itself support rendering to this colorType & format pair. Renderability
347 // still additionally depends on if the format itself is renderable.
349 };
350 uint32_t fFlags = 0;
351
354 };
355
362
363 std::unique_ptr<SkSL::ShaderCaps> fShaderCaps;
364
366 bool fProtectedSupport = false;
367 bool fSemaphoreSupport = false;
368 bool fAllowCpuSync = true;
374
375 bool fComputeSupport = false;
378
379#if defined(GRAPHITE_TEST_UTILS)
380 bool fDrawBufferCanBeMappedForReadback = true;
381#endif
382
384
385 //////////////////////////////////////////////////////////////////////////////////////////
386 // Client-provided Caps
387
388 /**
389 * If present, use this object to report shader compilation failures. If not, report failures
390 * via SkDebugf and assert.
391 */
393
394#if defined(GRAPHITE_TEST_UTILS)
395 std::string fDeviceName;
396 int fMaxTextureAtlasSize = 2048;
397 PathRendererStrategy fRequestedPathRendererStrategy;
398#endif
399 size_t fGlyphCacheTextureMaximumBytes = 2048 * 1024 * 4;
400
403
405
408
409 // Set based on client options
411
412 bool fSetBackendLabels = false;
413
414private:
415 virtual bool onIsTexturable(const TextureInfo&) const = 0;
416 virtual const ColorTypeInfo* getColorTypeInfo(SkColorType, const TextureInfo&) const = 0;
417
418 sk_sp<SkCapabilities> fCapabilities;
419};
420
421} // namespace skgpu::graphite
422
423#endif // skgpu_graphite_Caps_DEFINED
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition: SkAlign.h:33
#define SkUNREACHABLE
Definition: SkAssert.h:135
SkBlendMode
Definition: SkBlendMode.h:38
SkColorType
Definition: SkColorType.h:19
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
SkTextureCompressionType
size_t fRequiredStorageBufferAlignment
Definition: Caps.h:359
virtual bool extractGraphicsDescs(const UniqueKey &, GraphicsPipelineDesc *, RenderPassDesc *, const RendererProvider *) const
Definition: Caps.h:129
bool fDrawBufferCanBeMapped
Definition: Caps.h:371
virtual bool onIsTexturable(const TextureInfo &) const =0
virtual ImmutableSamplerInfo getImmutableSamplerInfo(const TextureProxy *) const
Definition: Caps.h:125
size_t requiredUniformBufferAlignment() const
Definition: Caps.h:156
virtual TextureInfo getDefaultDepthStencilTextureInfo(SkEnumBitMask< DepthStencilFlags >, uint32_t sampleCount, Protected) const =0
virtual GraphiteResourceKey makeSamplerKey(const SamplerDesc &samplerDesc) const
Definition: Caps.cpp:73
bool semaphoreSupport() const
Definition: Caps.h:229
virtual uint32_t channelMask(const TextureInfo &) const =0
skgpu::ShaderErrorHandler * shaderErrorHandler() const
Definition: Caps.h:273
const ResourceBindingRequirements & resourceBindingRequirements() const
Definition: Caps.h:150
virtual const ColorTypeInfo * getColorTypeInfo(SkColorType, const TextureInfo &) const =0
skgpu::Swizzle getReadSwizzle(SkColorType, const TextureInfo &) const
Definition: Caps.cpp:129
bool fSupportsAHardwareBufferImages
Definition: Caps.h:376
virtual TextureInfo getDefaultMSAATextureInfo(const TextureInfo &singleSampledInfo, Discardable discardable) const =0
bool fSemaphoreSupport
Definition: Caps.h:367
bool drawBufferCanBeMapped() const
Definition: Caps.h:242
bool requireOrderedRecordings() const
Definition: Caps.h:287
virtual TextureInfo getDefaultCompressedTextureInfo(SkTextureCompressionType, Mipmapped mipmapped, Protected) const =0
size_t fRequiredUniformBufferAlignment
Definition: Caps.h:358
float fMinDistanceFieldFontSize
Definition: Caps.h:401
bool allowMultipleAtlasTextures() const
Definition: Caps.h:284
bool isTexturable(const TextureInfo &) const
Definition: Caps.cpp:66
bool fBufferMapsAreAsync
Definition: Caps.h:372
bool fStorageBufferPreferred
Definition: Caps.h:370
size_t requiredStorageBufferAlignment() const
Definition: Caps.h:160
virtual TextureInfo getDefaultStorageTextureInfo(SkColorType) const =0
bool fClampToBorderSupport
Definition: Caps.h:365
bool fSupportBilerpFromGlyphAtlas
Definition: Caps.h:407
bool computeSupport() const
Definition: Caps.h:257
sktext::gpu::SDFTControl getSDFTControl(bool useSDFTForSmallText) const
Definition: Caps.cpp:161
virtual std::pair< SkColorType, bool > supportedWritePixelsColorType(SkColorType dstColorType, const TextureInfo &dstTextureInfo, SkColorType srcColorType) const =0
bool supportsAHardwareBufferImages() const
Definition: Caps.h:263
virtual bool supportsReadPixels(const TextureInfo &textureInfo) const =0
bool fMSAARenderToSingleSampledSupport
Definition: Caps.h:373
int fMaxPathAtlasTextureSize
Definition: Caps.h:404
bool supportBilerpFromGlyphAtlas() const
Definition: Caps.h:285
bool fullCompressedUploadSizeMustAlignToBlockDims() const
Definition: Caps.h:291
virtual SkISize getDepthAttachmentDimensions(const TextureInfo &, const SkISize colorAttachmentDimensions) const
Definition: Caps.cpp:61
virtual UniqueKey makeGraphicsPipelineKey(const GraphicsPipelineDesc &, const RenderPassDesc &) const =0
bool bufferMapsAreAsync() const
Definition: Caps.h:251
bool protectedSupport() const
Definition: Caps.h:226
bool setBackendLabels() const
Definition: Caps.h:297
virtual bool isStorage(const TextureInfo &) const =0
bool fComputeSupport
Definition: Caps.h:375
size_t getAlignedTextureDataRowBytes(size_t rowBytes) const
Definition: Caps.h:166
size_t fGlyphCacheTextureMaximumBytes
Definition: Caps.h:399
skgpu::Swizzle getWriteSwizzle(SkColorType, const TextureInfo &) const
Definition: Caps.cpp:142
bool storageBufferPreferred() const
Definition: Caps.h:239
float fGlyphsAsPathsFontSize
Definition: Caps.h:402
virtual UniqueKey makeComputePipelineKey(const ComputePipelineDesc &) const =0
SkColorType getRenderableColorType(SkColorType) const
Definition: Caps.cpp:114
bool fFullCompressedUploadSizeMustAlignToBlockDims
Definition: Caps.h:377
std::unique_ptr< SkSL::ShaderCaps > fShaderCaps
Definition: Caps.h:363
virtual TextureInfo getTextureInfoForSampledCopy(const TextureInfo &textureInfo, Mipmapped mipmapped) const =0
int defaultMSAASamplesCount() const
Definition: Caps.h:142
bool fStorageBufferSupport
Definition: Caps.h:369
sk_sp< SkCapabilities > capabilities() const
Definition: Caps.cpp:59
float glyphsAsPathsFontSize() const
Definition: Caps.h:279
DstReadRequirement getDstReadRequirement() const
Definition: Caps.cpp:152
static uint32_t SamplesToKey(uint32_t numSamples)
Definition: Caps.h:314
virtual bool isRenderable(const TextureInfo &) const =0
virtual ~Caps()
Definition: Caps.cpp:27
virtual TextureInfo getDefaultSampledTextureInfo(SkColorType, Mipmapped mipmapped, Protected, Renderable) const =0
bool fSetBackendLabels
Definition: Caps.h:412
bool clampToBorderSupport() const
Definition: Caps.h:224
bool areColorTypeAndTextureInfoCompatible(SkColorType, const TextureInfo &) const
Definition: Caps.cpp:85
bool fRequireOrderedRecordings
Definition: Caps.h:410
virtual std::pair< SkColorType, bool > supportedReadPixelsColorType(SkColorType srcColorType, const TextureInfo &srcTextureInfo, SkColorType dstColorType) const =0
ShaderErrorHandler * fShaderErrorHandler
Definition: Caps.h:392
size_t glyphCacheTextureMaximumBytes() const
Definition: Caps.h:281
float minDistanceFieldFontSize() const
Definition: Caps.h:278
bool fProtectedSupport
Definition: Caps.h:366
void finishInitialization(const ContextOptions &)
Definition: Caps.cpp:29
bool msaaRenderToSingleSampledSupport() const
Definition: Caps.h:254
virtual bool supportsWritePixels(const TextureInfo &textureInfo) const =0
size_t fRequiredTransferBufferAlignment
Definition: Caps.h:360
virtual void buildKeyForTexture(SkISize dimensions, const TextureInfo &, ResourceType, Shareable, GraphiteResourceKey *) const =0
bool allowCpuSync() const
Definition: Caps.h:232
const SkSL::ShaderCaps * shaderCaps() const
Definition: Caps.h:75
bool storageBufferSupport() const
Definition: Caps.h:235
size_t requiredTransferBufferAlignment() const
Definition: Caps.h:163
int maxPathAtlasTextureSize() const
Definition: Caps.h:282
ResourceBindingRequirements fResourceBindingReqs
Definition: Caps.h:383
int fDefaultMSAASamples
Definition: Caps.h:357
size_t fTextureDataRowBytesAlignment
Definition: Caps.h:361
int maxTextureSize() const
Definition: Caps.h:141
bool fAllowMultipleAtlasTextures
Definition: Caps.h:406
FlutterSemanticsFlag flags
uint32_t ResourceType
DstReadRequirement
Definition: Caps.h:64
Definition: GpuTools.h:21
Renderable
Definition: GpuTypes.h:69
Mipmapped
Definition: GpuTypes.h:53
Protected
Definition: GpuTypes.h:61
Definition: SkSize.h:16
ColorTypeInfo(SkColorType ct, SkColorType transferCt, uint32_t flags, skgpu::Swizzle readSwizzle, skgpu::Swizzle writeSwizzle)
Definition: Caps.h:334