Debian Codegraph @

src/gpu/graphite/KeyHelpers.h

raw
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
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkBlendMode.h"
13 #include "include/core/SkM44.h"
14 #include "include/core/SkPoint3.h"
15 #include "include/core/SkSamplingOptions.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkSpan.h"
18 #include "include/core/SkTileMode.h"
19 #include "include/effects/SkGradient.h"
20 #include "include/gpu/graphite/Context.h"
21 #include "src/core/SkColorData.h"
22 #include "src/core/SkColorSpaceXformSteps.h"
23 #include "src/gpu/graphite/KeyContext.h"
24 #include "src/gpu/graphite/PaintParams.h"
25 #include "src/gpu/graphite/PaintParamsKey.h"
26 #include "src/gpu/graphite/ReadSwizzle.h"
27 #include "src/gpu/graphite/ResourceTypes.h"
28 #include "src/gpu/graphite/TextureProxy.h"
29 #include "src/shaders/SkShaderBase.h"
30 #include "src/shaders/gradients/SkGradientBaseShader.h"
31
32 class SkColorFilter;
33 class SkData;
34 class SkRuntimeEffect;
35
36 namespace skgpu::graphite {
37
38 class DrawContext;
39 class FloatStorageManager;
40 class PipelineDataGatherer;
41 class UniquePaintParamsID;
42 enum class ReadSwizzle;
43
44 /**
45 * The KeyHelpers can be used to manually construct an SkPaintParamsKey.
46 *
47 * TODO: If we restructure how the keys are made, we can utilize a single block type for the
48 * different blend blocks outlined below. The different Src/Dst pairings could instead be encoded
49 * as parent-child relationships.
50 */
51
52 struct SolidColorShaderBlock {
53 static void AddBlock(const KeyContext&, const SkPMColor4f&);
54 };
55
56 struct RGBPaintColorBlock {
57 static void AddBlock(const KeyContext&);
58 };
59
60 struct AlphaOnlyPaintColorBlock {
61 static void AddBlock(const KeyContext&);
62 };
63
64 struct GradientShaderBlocks {
65 struct GradientData {
66 // The number of stops stored internal to this data structure before falling back to
67 // bitmap storage.
68 static constexpr int kNumInternalStorageStops = 8;
69
70 // This ctor is used during pre-compilation when we don't have enough information to
71 // extract uniform data. However, we must be able to provide enough data to make all the
72 // relevant decisions about which code snippets to use.
73 GradientData(SkShaderBase::GradientType, int numStops, bool useStorageBuffer);
74
75 // This ctor is used when extracting information from PaintParams. It must provide
76 // enough data to generate the uniform data the selected code snippet will require.
77 GradientData(SkShaderBase::GradientType,
78 SkPoint point0, SkPoint point1,
79 float radius0, float radius1,
80 float bias, float scale,
81 SkTileMode,
82 int numStops,
83 const SkPMColor4f* colors,
84 const float* offsets,
85 const SkGradientBaseShader* shader,
86 sk_sp<TextureProxy> colorsAndOffsetsProxy,
87 bool useStorageBuffer,
88 const SkGradient::Interpolation&);
89
90 bool operator==(const GradientData& rhs) const = delete;
91 bool operator!=(const GradientData& rhs) const = delete;
92
93 // Layout options.
94 SkShaderBase::GradientType fType;
95 SkPoint fPoints[2];
96 float fRadii[2];
97
98 // Layout options for sweep gradient.
99 float fBias;
100 float fScale;
101
102 SkTileMode fTM;
103 int fNumStops;
104 bool fUseStorageBuffer;
105
106 // For gradients w/ <= kNumInternalStorageStops stops we use fColors and fOffsets.
107 // The offsets are packed into a single float4 to save space when the layout is std140.
108 //
109 // Otherwise when storage buffers are preferred, we save the colors and offsets pointers
110 // to fSrcColors and fSrcOffsets so we can directly copy to the gatherer gradient buffer,
111 // else we pack the data into the fColorsAndOffsetsProxy texture.
112 SkPMColor4f fColors[kNumInternalStorageStops];
113 SkV4 fOffsets[kNumInternalStorageStops / 4];
114 sk_sp<TextureProxy> fColorsAndOffsetsProxy;
115 const SkPMColor4f* fSrcColors;
116 const float* fSrcOffsets;
117 const SkGradientBaseShader* fSrcShader;
118
119 SkGradient::Interpolation fInterpolation;
120 };
121
122 static void AddBlock(const KeyContext&, const GradientData&);
123 };
124
125 struct LocalMatrixShaderBlock {
126 struct LMShaderData {
127 LMShaderData(const SkMatrix& localMatrix)
128 : fLocalMatrix(localMatrix) {}
129
130 // Local matrices are applied to coords.xy01, so a 4x4 matrix can be flattened to a 3x3
131 // for less data upload to the GPU at this point (as there will be no more coordinate
132 // space manipulation that might require the full 4x4).
133 const SkMatrix fLocalMatrix;
134 };
135
136 static void BeginBlock(const KeyContext&, const LMShaderData&);
137 };
138
139 struct ImageShaderBlock {
140 struct ImageData {
141 ImageData(const SkSamplingOptions& sampling,
142 SkTileMode tileModeX,
143 SkTileMode tileModeY,
144 SkISize imgSize,
145 SkRect subset,
146 ImmutableSamplerInfo immutableSamplerInfo = {});
147 SkSamplingOptions fSampling;
148 std::pair<SkTileMode, SkTileMode> fTileModes;
149 SkISize fImgSize;
150 SkRect fSubset;
151
152 // When we're generating the key from an actual SkImageShader fTextureProxy will be
153 // non-null. Otherwise, fImmutableSamplerInfo will be filled in.
154 sk_sp<TextureProxy> fTextureProxy;
155 ImmutableSamplerInfo fImmutableSamplerInfo;
156 };
157
158 static void AddBlock(const KeyContext&, const ImageData&);
159 };
160
161 struct YUVImageShaderBlock {
162 struct ImageData {
163 ImageData(const SkSamplingOptions& sampling,
164 SkTileMode tileModeX,
165 SkTileMode tileModeY,
166 SkISize imgSize,
167 SkRect subset);
168
169 SkSamplingOptions fSampling;
170 SkSamplingOptions fSamplingUV;
171 std::pair<SkTileMode, SkTileMode> fTileModes;
172 SkISize fImgSize;
173 SkISize fImgSizeUV; // Size of UV planes relative to Y's texel space
174 SkRect fSubset;
175 SkPoint fLinearFilterUVInset = { 0.50001f, 0.50001f };
176 SkV4 fChannelSelect[4];
177 float fAlphaParam = 0;
178 SkMatrix fYUVtoRGBMatrix;
179 SkPoint3 fYUVtoRGBTranslate;
180
181 // TODO: Currently these are only filled in when we're generating the key from an actual
182 // SkImageShader. In the pre-compile case we will need to create Graphite promise
183 // images which hold the appropriate data.
184 sk_sp<TextureProxy> fTextureProxies[4];
185 };
186
187 static void AddBlock(const KeyContext&, const ImageData&);
188 };
189
190 struct CoordNormalizeShaderBlock {
191 struct CoordNormalizeData {
192 CoordNormalizeData(SkSize dimensions)
193 : fInvDimensions(
194 SkSize::Make(1.0f / dimensions.width(), 1.0f / dimensions.height())) {}
195 SkSize fInvDimensions;
196 };
197
198 static void BeginBlock(const KeyContext&, const CoordNormalizeData&);
199 };
200
201 struct CoordClampShaderBlock {
202 struct CoordClampData {
203 CoordClampData(SkRect subset) : fSubset(subset) {}
204
205 SkRect fSubset;
206 };
207
208 static void BeginBlock(const KeyContext&, const CoordClampData&);
209 };
210
211 struct DitherShaderBlock {
212 struct DitherData {
213 DitherData(float range, sk_sp<TextureProxy> proxy)
214 : fRange(range)
215 , fLUTProxy(std::move(proxy)) {}
216
217 float fRange;
218 sk_sp<TextureProxy> fLUTProxy;
219 };
220
221 static void AddBlock(const KeyContext&, const DitherData&);
222 };
223
224 struct PerlinNoiseShaderBlock {
225 enum class Type {
226 kFractalNoise,
227 kTurbulence,
228 };
229
230 struct PerlinNoiseData {
231 PerlinNoiseData(Type type,
232 SkVector baseFrequency,
233 int numOctaves,
234 SkISize stitchData)
235 : fType(type)
236 , fBaseFrequency(baseFrequency)
237 , fNumOctaves(numOctaves)
238 , fStitchData{ SkIntToFloat(stitchData.fWidth), SkIntToFloat(stitchData.fHeight) } {
239 }
240
241 bool stitching() const { return !fStitchData.isZero(); }
242
243 Type fType;
244 SkVector fBaseFrequency;
245 int fNumOctaves;
246 SkVector fStitchData;
247
248 sk_sp<TextureProxy> fPermutationsProxy;
249 sk_sp<TextureProxy> fNoiseProxy;
250 };
251
252 static void AddBlock(const KeyContext&, const PerlinNoiseData&);
253 };
254
255 struct BlendComposeBlock {
256 static void BeginBlock(const KeyContext&);
257 };
258
259 struct PorterDuffBlenderBlock {
260 static void AddBlock(const KeyContext&, SkSpan<const float> coeffs);
261 };
262
263 struct HSLCBlenderBlock {
264 static void AddBlock(const KeyContext&, SkSpan<const float> coeffs);
265 };
266
267 struct ComposeBlock {
268 static void BeginBlock(const KeyContext&);
269 };
270
271 struct MatrixColorFilterBlock {
272 struct MatrixColorFilterData {
273 MatrixColorFilterData(const float matrix[20], bool inHSLA, bool clamp)
274 : fMatrix(matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
275 matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8],
276 matrix[10], matrix[11], matrix[12], matrix[13],
277 matrix[15], matrix[16], matrix[17], matrix[18])
278 , fTranslate{matrix[4], matrix[9], matrix[14], matrix[19]}
279 , fInHSLA(inHSLA)
280 , fClamp(clamp) {
281 }
282
283 SkM44 fMatrix;
284 SkV4 fTranslate;
285 bool fInHSLA;
286 bool fClamp;
287 };
288
289 static void AddBlock(const KeyContext&, const MatrixColorFilterData&);
290 };
291
292 struct TableColorFilterBlock {
293 struct TableColorFilterData {
294 TableColorFilterData(sk_sp<TextureProxy> proxy) : fTextureProxy(std::move(proxy)) {}
295
296 sk_sp<TextureProxy> fTextureProxy;
297 };
298
299 static void AddBlock(const KeyContext&, const TableColorFilterData&);
300 };
301
302 struct ColorSpaceTransformBlock {
303 struct ColorSpaceTransformData {
304 ColorSpaceTransformData(const SkColorSpace* src,
305 SkAlphaType srcAT,
306 const SkColorSpace* dst,
307 SkAlphaType dstAT);
308 ColorSpaceTransformData(const SkColorSpaceXformSteps& steps) { fSteps = steps; }
309 ColorSpaceTransformData(ReadSwizzle swizzle) : fReadSwizzle(swizzle) {
310 SkASSERT(fSteps.fFlags.mask() == 0); // By default, the colorspace should have no effect
311 }
312 SkColorSpaceXformSteps fSteps;
313 ReadSwizzle fReadSwizzle = ReadSwizzle::kRGBA;
314 };
315
316 static void AddBlock(const KeyContext&, const ColorSpaceTransformData&);
317 };
318
319 struct NonMSAAClipBlock {
320 struct NonMSAAClipData {
321 NonMSAAClipData(SkRect rect,
322 SkPoint radiusPlusHalf,
323 SkRect edgeSelect,
324 SkPoint texCoordOffset,
325 SkRect maskBounds,
326 sk_sp<TextureProxy> atlasTexture)
327 : fRect(rect)
328 , fRadiusPlusHalf(radiusPlusHalf)
329 , fEdgeSelect(edgeSelect)
330 , fTexCoordOffset(texCoordOffset)
331 , fMaskBounds(maskBounds)
332 , fAtlasTexture(std::move(atlasTexture)){}
333 // analytic clip
334 SkRect fRect; // bounds, outset by 0.5
335 SkPoint fRadiusPlusHalf; // abs() of .x is radius+0.5, if < 0 indicates inverse fill
336 // .y is 1/(radius+0.5)
337 SkRect fEdgeSelect; // 1 indicates a rounded corner on that side (LTRB), 0 otherwise
338
339 // atlas clip
340 SkPoint fTexCoordOffset; // translation from local coords to unnormalized texel coords
341 SkRect fMaskBounds; // bounds of mask area, in unnormalized texel coords
342
343 sk_sp<TextureProxy> fAtlasTexture;
344 };
345
346 static void AddBlock(const KeyContext&, const NonMSAAClipData&);
347 };
348
349 /**
350 * Adds a block that references the primitive color produced by the RenderStep and accounts for
351 * color space transformation.
352 */
353 void AddPrimitiveColor(const KeyContext&, bool skipColorXform);
354
355 /**
356 * Blend mode color filters blend their input (as the dst color) with some given color (supplied
357 * via a uniform) as the src color.
358 */
359 void AddBlendModeColorFilter(const KeyContext&,
360 SkBlendMode,
361 const SkPMColor4f& srcColor);
362
363 struct RuntimeEffectBlock {
364 struct ShaderData {
365 // This ctor is used during pre-compilation when we don't have enough information to
366 // extract uniform data.
367 ShaderData(sk_sp<const SkRuntimeEffect> effect);
368
369 // This ctor is used when extracting information from PaintParams.
370 ShaderData(sk_sp<const SkRuntimeEffect> effect,
371 sk_sp<const SkData> uniforms);
372
373 bool operator==(const ShaderData& rhs) const;
374 bool operator!=(const ShaderData& rhs) const { return !(*this == rhs); }
375
376 // Runtime shader data.
377 sk_sp<const SkRuntimeEffect> fEffect;
378 sk_sp<const SkData> fUniforms;
379 };
380
381 // On a false return, no block has been started
382 static bool BeginBlock(const KeyContext&, const ShaderData&);
383
384 // Add a no-op placeholder for an incorrect runtime effect
385 static void AddNoOpEffect(const KeyContext&, SkRuntimeEffect*);
386
387 // Add a post-amble for runtime effects that use the toLinearSrgb/fromLinearSrgb intrinsics
388 static void HandleIntrinsics(const KeyContext&, const SkRuntimeEffect*);
389 };
390
391 void AddToKey(const KeyContext&, const SkBlender*);
392
393 /**
394 * Add implementation details, for the specified backend, of this SkColorFilter to the
395 * provided key.
396 *
397 * @param keyContext backend context for key creation
398 * @param filter This function is a no-op if filter is null.
399 */
400 void AddToKey(const KeyContext& keyContext, const SkColorFilter* filter);
401
402 /**
403 * Add implementation details, for the specified backend, of this SkShader to the
404 * provided key.
405 *
406 * @param keyContext backend context for key creation
407 * @param shader This function is a no-op if shader is null.
408 */
409 void AddToKey(const KeyContext& keyContext, const SkShader* shader);
410
411 /**
412 * Add blocks to the key and data builders that represent the simple image shader, e.g. it is
413 * equivalent to:
414 *
415 * SkMatrix localMatrix = SkMatrix::Rect2Rect(imageShader.fSrcRect, imageShader.fDstRect);
416 * sk_sp<SkShader> skImageShader = SkImageShader::MakeSubset(imageShader.fImage,
417 * imageShader.fSubset,
418 * SkTileMode::kClamp,
419 * SkTileMode::kClamp,
420 * imageShader.fSamplingOptions,
421 * imageShader.fLocalMatrix);
422 * AddToKey(keyContext, skImageShader.get());
423 *
424 * with the benefit of not creating any SkShader objects or having to compute matrix inverses.
425 */
426 void AddToKey(const KeyContext& keyContext, const PaintParams::SimpleImage& imageShader);
427
428 // Add a fixed blend mode node for a specific SkBlendMode.
429 void AddFixedBlendMode(const KeyContext&, SkBlendMode);
430 // Add a blend mode node for an SkBlendMode that can vary
431 void AddBlendMode(const KeyContext&, SkBlendMode);
432 void AddDitherBlock(const KeyContext&, SkColorType);
433
434 template <typename AddBlendToKeyT, typename AddSrcToKeyT, typename AddDstToKeyT>
435 void Blend(const KeyContext& keyContext,
436 AddBlendToKeyT addBlendToKey,
437 AddSrcToKeyT addSrcToKey,
438 AddDstToKeyT addDstToKey) {
439 BlendComposeBlock::BeginBlock(keyContext);
440
441 addSrcToKey();
442
443 addDstToKey();
444
445 addBlendToKey();
446
447 keyContext.paintParamsKeyBuilder()->endBlock(); // BlendComposeBlock
448 }
449
450 template <typename AddInnerToKeyT, typename AddOuterToKeyT>
451 void Compose(const KeyContext& keyContext,
452 AddInnerToKeyT addInnerToKey,
453 AddOuterToKeyT addOuterToKey) {
454 ComposeBlock::BeginBlock(keyContext);
455
456 addInnerToKey();
457
458 addOuterToKey();
459
460 keyContext.paintParamsKeyBuilder()->endBlock(); // ComposeBlock
461 }
462
463 } // namespace skgpu::graphite
464
465 #endif // skgpu_graphite_KeyHelpers_DEFINED