Flutter Engine
The Flutter Engine
KeyHelpers.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 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_KeyHelpers_DEFINED
9#define skgpu_graphite_KeyHelpers_DEFINED
10
13#include "include/core/SkM44.h"
17#include "include/core/SkSpan.h"
27
28class SkColorFilter;
29class SkData;
30class SkRuntimeEffect;
31
32namespace skgpu::graphite {
33
34class DrawContext;
35class KeyContext;
36class PaintParamsKeyBuilder;
37class PipelineDataGatherer;
38class UniquePaintParamsID;
39enum class ReadSwizzle;
40
41// Types of logical "destinations" that a blender might blend with.
42enum class DstColorType {
43 // A color read from the framebuffer.
45 // A color provided by geometry.
47 // A color evaluated by a child shader.
49};
50
51/**
52 * The KeyHelpers can be used to manually construct an SkPaintParamsKey.
53 *
54 * TODO: If we restructure how the keys are made, we can utilize a single block type for the
55 * different blend blocks outlined below. The different Src/Dst pairings could instead be encoded
56 * as parent-child relationships.
57 */
58
60 static void AddBlock(const KeyContext&,
64 SkIPoint dstOffset);
65};
66
68 static void AddBlock(const KeyContext&,
71 const SkPMColor4f&);
72};
73
75 static void AddBlock(const KeyContext&,
78};
79
81 static void AddBlock(const KeyContext&,
84};
85
87 struct GradientData {
88 // The number of stops stored internal to this data structure before falling back to
89 // bitmap storage.
90 static constexpr int kNumInternalStorageStops = 8;
91
92 // This ctor is used during pre-compilation when we don't have enough information to
93 // extract uniform data. However, we must be able to provide enough data to make all the
94 // relevant decisions about which code snippets to use.
95 GradientData(SkShaderBase::GradientType, int numStops, bool useStorageBuffer);
96
97 // This ctor is used when extracting information from PaintParams. It must provide
98 // enough data to generate the uniform data the selected code snippet will require.
100 SkPoint point0, SkPoint point1,
101 float radius0, float radius1,
102 float bias, float scale,
104 int numStops,
105 const SkPMColor4f* colors,
106 const float* offsets,
107 const SkGradientBaseShader* shader,
108 sk_sp<TextureProxy> colorsAndOffsetsProxy,
109 bool useStorageBuffer,
111
112 bool operator==(const GradientData& rhs) const = delete;
113 bool operator!=(const GradientData& rhs) const = delete;
114
115 // Layout options.
118 float fRadii[2];
119
120 // Layout options for sweep gradient.
121 float fBias;
122 float fScale;
123
127
128 // For gradients w/ <= kNumInternalStorageStops stops we use fColors and fOffsets.
129 // The offsets are packed into a single float4 to save space when the layout is std140.
130 //
131 // Otherwise when storage buffers are preferred, we save the colors and offsets pointers
132 // to fSrcColors and fSrcOffsets so we can directly copy to the gatherer gradient buffer,
133 // else we pack the data into the fColorsAndOffsetsProxy texture.
138 const float* fSrcOffsets;
140
142 };
143
144 static void AddBlock(const KeyContext&,
147 const GradientData&);
148};
149
152 LMShaderData(const SkMatrix& localMatrix)
153 : fLocalMatrix(localMatrix) {
154 }
155
157 };
158
159 static void BeginBlock(const KeyContext&,
162 const LMShaderData&);
163};
164
166 struct ImageData {
168 SkTileMode tileModeX,
169 SkTileMode tileModeY,
170 SkISize imgSize,
171 SkRect subset,
172 ReadSwizzle readSwizzle);
173
179
181
182 // TODO: Currently this is only filled in when we're generating the key from an actual
183 // SkImageShader. In the pre-compile case we will need to create a Graphite promise
184 // image which holds the appropriate data.
186 };
187
188 static void AddBlock(const KeyContext&,
191 const ImageData&);
192};
193
195 struct ImageData {
197 SkTileMode tileModeX,
198 SkTileMode tileModeY,
199 SkISize imgSize,
200 SkRect subset);
201
206 SkISize fImgSizeUV; // Size of UV planes relative to Y's texel space
208 SkPoint fLinearFilterUVInset = { 0.50001f, 0.50001f };
212
213 // TODO: Currently these are only filled in when we're generating the key from an actual
214 // SkImageShader. In the pre-compile case we will need to create Graphite promise
215 // images which hold the appropriate data.
217 };
218
219 static void AddBlock(const KeyContext&,
222 const ImageData&);
223};
224
227 CoordClampData(SkRect subset) : fSubset(subset) {}
228
230 };
231
232 // The gatherer and data should be null or non-null together
233 static void BeginBlock(const KeyContext&,
236 const CoordClampData&);
237};
238
240 struct DitherData {
241 DitherData(float range, sk_sp<TextureProxy> proxy)
242 : fRange(range)
243 , fLUTProxy(std::move(proxy)) {}
244
245 float fRange;
247 };
248
249 static void AddBlock(const KeyContext&,
252 const DitherData&);
253};
254
256 enum class Type {
257 kFractalNoise,
258 kTurbulence,
259 };
260
263 SkVector baseFrequency,
264 int numOctaves,
265 SkISize stitchData)
266 : fType(type)
267 , fBaseFrequency(baseFrequency)
268 , fNumOctaves(numOctaves)
269 , fStitchData{ SkIntToFloat(stitchData.fWidth), SkIntToFloat(stitchData.fHeight) } {
270 }
271
272 bool stitching() const { return !fStitchData.isZero(); }
273
278
281 };
282
283 // The gatherer and data should be null or non-null together
284 static void AddBlock(const KeyContext&,
287 const PerlinNoiseData&);
288};
289
292};
293
295 static void AddBlock(const KeyContext&,
299};
300
302 static void AddBlock(const KeyContext&,
306};
307
309 static void BeginBlock(const KeyContext&,
312};
313
315 static void BeginBlock(const KeyContext&,
318};
319
323 bool inHSLA)
324 : fMatrix(matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
325 matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8],
326 matrix[10], matrix[11], matrix[12], matrix[13],
327 matrix[15], matrix[16], matrix[17], matrix[18])
328 , fTranslate{matrix[4], matrix[9], matrix[14], matrix[19]}
329 , fInHSLA(inHSLA) {
330 }
331
335 };
336
337 // The gatherer and matrixCFData should be null or non-null together
338 static void AddBlock(const KeyContext&,
341 const MatrixColorFilterData&);
342};
343
347
349 };
350
351 static void AddBlock(const KeyContext&,
354 const TableColorFilterData&);
355};
356
360 SkAlphaType srcAT,
361 const SkColorSpace* dst,
362 SkAlphaType dstAT);
365 };
366
367 static void AddBlock(const KeyContext&,
371};
372
373/**
374 * Blend mode color filters blend their input (as the dst color) with some given color (supplied
375 * via a uniform) as the src color.
376 */
381 const SkPMColor4f& srcColor);
382
384 struct ShaderData {
385 // This ctor is used during pre-compilation when we don't have enough information to
386 // extract uniform data.
388
389 // This ctor is used when extracting information from PaintParams.
391 sk_sp<const SkData> uniforms);
392
393 bool operator==(const ShaderData& rhs) const;
394 bool operator!=(const ShaderData& rhs) const { return !(*this == rhs); }
395
396 // Runtime shader data.
399 };
400
401 static void BeginBlock(const KeyContext&,
404 const ShaderData&);
405};
406
407void AddToKey(const KeyContext&,
410 const SkBlender*);
411
412/**
413 * Add implementation details, for the specified backend, of this SkColorFilter to the
414 * provided key.
415 *
416 * @param keyContext backend context for key creation
417 * @param builder builder for creating the key for this SkShader
418 * @param gatherer if non-null, storage for this colorFilter's data
419 * @param filter This function is a no-op if filter is null.
420 */
421void AddToKey(const KeyContext& keyContext,
423 PipelineDataGatherer* gatherer,
424 const SkColorFilter* filter);
425
426/**
427 * Add implementation details, for the specified backend, of this SkShader to the
428 * provided key.
429 *
430 * @param keyContext backend context for key creation
431 * @param builder builder for creating the key for this SkShader
432 * @param gatherer if non-null, storage for this colorFilter's data
433 * @param shader This function is a no-op if shader is null.
434 */
435void AddToKey(const KeyContext& keyContext,
437 PipelineDataGatherer* gatherer,
438 const SkShader* shader);
439
440// TODO(b/330864257) These visitation functions are redundant with AddToKey, except that they are
441// executed in the Device::drawGeometry() stack frame, whereas the keys are currently deferred until
442// DrawPass::Make. Image use needs to be detected in the draw frame to split tasks to match client
443// actions. Once paint keys are extracted in the draw frame, this can go away entirely.
447
448} // namespace skgpu::graphite
449
450#endif // skgpu_graphite_KeyHelpers_DEFINED
SkAlphaType
Definition: SkAlphaType.h:26
SkBlendMode
Definition: SkBlendMode.h:38
#define SkIntToFloat(x)
Definition: SkScalar.h:58
SkTileMode
Definition: SkTileMode.h:13
GLenum type
Definition: SkData.h:25
Definition: SkM44.h:150
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
dst
Definition: cp.py:12
list offsets
Definition: mskp_parser.py:37
void AddToKey(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, const SkBlender *blender)
void AddBlendModeColorFilter(const KeyContext &keyContext, PaintParamsKeyBuilder *builder, PipelineDataGatherer *gatherer, SkBlendMode bm, const SkPMColor4f &srcColor)
void NotifyImagesInUse(Recorder *recorder, DrawContext *drawContext, const SkBlender *blender)
Definition: ref_ptr.h:256
const Scalar scale
Definition: SkSize.h:16
bool isZero() const
Definition: SkPoint_impl.h:193
Definition: SkM44.h:98
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:139
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, SkBlendMode)
Definition: KeyHelpers.cpp:936
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:926
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:961
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, SkSpan< const float > coeffs)
Definition: KeyHelpers.cpp:948
ColorSpaceTransformData(const SkColorSpaceXformSteps &steps)
Definition: KeyHelpers.h:363
ColorSpaceTransformData(const SkColorSpace *src, SkAlphaType srcAT, const SkColorSpace *dst, SkAlphaType dstAT)
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const ColorSpaceTransformData &)
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:971
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const CoordClampData &)
Definition: KeyHelpers.cpp:853
DitherData(float range, sk_sp< TextureProxy > proxy)
Definition: KeyHelpers.h:241
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const DitherData &)
Definition: KeyHelpers.cpp:876
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, sk_sp< TextureProxy > dst, SkIPoint dstOffset)
Definition: KeyHelpers.cpp:169
bool operator!=(const GradientData &rhs) const =delete
SkPMColor4f fColors[kNumInternalStorageStops]
Definition: KeyHelpers.h:134
SkGradientShader::Interpolation fInterpolation
Definition: KeyHelpers.h:141
GradientData(SkShaderBase::GradientType, int numStops, bool useStorageBuffer)
Definition: KeyHelpers.cpp:339
SkV4 fOffsets[kNumInternalStorageStops/4]
Definition: KeyHelpers.h:135
bool operator==(const GradientData &rhs) const =delete
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const GradientData &)
Definition: KeyHelpers.cpp:410
ImageData(const SkSamplingOptions &sampling, SkTileMode tileModeX, SkTileMode tileModeY, SkISize imgSize, SkRect subset, ReadSwizzle readSwizzle)
Definition: KeyHelpers.cpp:659
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const ImageData &)
Definition: KeyHelpers.cpp:673
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const LMShaderData &)
Definition: KeyHelpers.cpp:498
MatrixColorFilterData(const float matrix[20], bool inHSLA)
Definition: KeyHelpers.h:322
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const MatrixColorFilterData &)
Definition: KeyHelpers.cpp:992
PerlinNoiseData(Type type, SkVector baseFrequency, int numOctaves, SkISize stitchData)
Definition: KeyHelpers.h:262
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const PerlinNoiseData &)
Definition: KeyHelpers.cpp:915
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *)
Definition: KeyHelpers.cpp:131
sk_sp< const SkRuntimeEffect > fEffect
Definition: KeyHelpers.h:397
bool operator!=(const ShaderData &rhs) const
Definition: KeyHelpers.h:394
ShaderData(sk_sp< const SkRuntimeEffect > effect)
bool operator==(const ShaderData &rhs) const
static void BeginBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const ShaderData &)
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const SkPMColor4f &)
Definition: KeyHelpers.cpp:102
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const TableColorFilterData &)
ImageData(const SkSamplingOptions &sampling, SkTileMode tileModeX, SkTileMode tileModeY, SkISize imgSize, SkRect subset)
Definition: KeyHelpers.cpp:771
static void AddBlock(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, const ImageData &)
Definition: KeyHelpers.cpp:804