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