Debian Codegraph @

src/gpu/graphite/PaintParams.cpp

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 #include "src/gpu/graphite/PaintParams.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkShader.h"
12 #include "src/core/SkBlendModeBlender.h"
13 #include "src/core/SkBlenderBase.h"
14 #include "src/core/SkColorSpacePriv.h"
15 #include "src/core/SkImageInfoPriv.h"
16 #include "src/effects/colorfilters/SkColorFilterBase.h"
17 #include "src/gpu/Blend.h"
18 #include "src/gpu/DitherUtils.h"
19 #include "src/gpu/graphite/ContextUtils.h"
20 #include "src/gpu/graphite/KeyContext.h"
21 #include "src/gpu/graphite/KeyHelpers.h"
22 #include "src/gpu/graphite/PaintParamsKey.h"
23 #include "src/gpu/graphite/PipelineData.h"
24 #include "src/gpu/graphite/RecorderPriv.h"
25
26 namespace skgpu::graphite {
27
28 namespace {
29
30 // This should be kept in sync w/ SkPaintPriv::ShouldDither and PaintOption::shouldDither
31 bool should_dither(const PaintParams& p, SkColorType dstCT) {
32 // The paint dither flag can veto.
33 if (!p.dither()) {
34 return false;
35 }
36
37 if (dstCT == kUnknown_SkColorType) {
38 return false;
39 }
40
41 // We always dither 565 or 4444 when requested.
42 if (dstCT == kRGB_565_SkColorType || dstCT == kARGB_4444_SkColorType) {
43 return true;
44 }
45
46 // Otherwise, dither is only needed for non-const paints.
47 return p.imageShader() || (p.shader() && !as_SB(p.shader())->isConstant());
48 }
49
50 bool blendmode_depends_on_dst(SkBlendMode blendMode, bool srcIsOpaque) {
51 if (blendMode == SkBlendMode::kSrc || blendMode == SkBlendMode::kClear) {
52 // src and clear blending never depends on dst
53 return false;
54 }
55
56 if (blendMode == SkBlendMode::kSrcOver || blendMode == SkBlendMode::kDstOut) {
57 // src-over depends on dst if src is transparent (a != 1)
58 // dst-out simplifies to kClear if a == 1
59 return !srcIsOpaque;
60 }
61
62 return true;
63 }
64
65 std::pair<const SkBlender*, SkBlendMode> get_final_blend(const SkBlender* blender) {
66 if (!blender) {
67 return {nullptr, SkBlendMode::kSrcOver};
68 }
69
70 auto optionalBlendMode = as_BB(blender)->asBlendMode();
71 if (optionalBlendMode.has_value()) {
72 return {nullptr, *optionalBlendMode};
73 } else {
74 return {blender, SkBlendMode::kSrc};
75 }
76 }
77
78 SkEnumBitMask<DstUsage> get_dst_usage(const Caps* caps,
79 TextureFormat targetFormat,
80 const PaintParams& paint,
81 Coverage rendererCoverage,
82 const SkShader* clipShader,
83 const NonMSAAClip& nonMSAAClip) {
84 SkEnumBitMask<DstUsage> dstUsage;
85 Coverage effectiveCoverage = rendererCoverage;
86 if (clipShader || !nonMSAAClip.isEmpty()) {
87 // Analytic AA is being added through clipping (outside of any geometry the Renderer is
88 // in control of).
89 dstUsage = DstUsage::kDependsOnDst;
90 if (effectiveCoverage == Coverage::kNone) {
91 effectiveCoverage = Coverage::kSingleChannel;
92 }
93 } else if (rendererCoverage != Coverage::kNone) {
94 // Analytic AA is added through the Renderer
95 dstUsage = DstUsage::kDependsOnDst | DstUsage::kDstOnlyUsedByRenderer;
96 } else {
97 // kDependsOnDst as a result of shading/blending will be handled in toKey() as it depends on
98 // the analyzed opacity of the shader.
99 dstUsage = DstUsage::kNone;
100 }
101
102 if (paint.finalBlender()) {
103 // Runtime blenders always require the dst in the shader
104 dstUsage |= DstUsage::kDstReadRequired;
105 } else {
106 SkBlendMode finalBlendMode = paint.finalBlendMode();
107 if (!CanUseHardwareBlending(caps, targetFormat, finalBlendMode, effectiveCoverage)) {
108 dstUsage |= DstUsage::kDstReadRequired;
109 }
110 // Even if we're using HW blending, we flag advanced blends because they might need barriers
111 if (finalBlendMode > SkBlendMode::kLastCoeffMode) {
112 dstUsage |= DstUsage::kAdvancedBlend;
113 }
114 }
115
116 return dstUsage;
117 }
118
119 } // anonymous namespace
120
121 PaintParams::PaintParams(const SkPaint& paint,
122 const SimpleImage* imageOverride,
123 const SkBlender* primitiveBlender,
124 bool skipColorXform,
125 bool ignoreShader)
126 : fColor(paint.getColor4f())
127 , fFinalBlend(get_final_blend(paint.getBlender()))
128 , fShader(ignoreShader ? nullptr : paint.getShader())
129 , fImageShader(imageOverride)
130 , fColorFilter(paint.getColorFilter())
131 , fPrimitiveBlender(primitiveBlender)
132 , fSkipColorXform(skipColorXform)
133 , fDither(paint.isDither()) {
134 if (!fPrimitiveBlender) {
135 // NOTE: We can still have an alpha-only fImageShader and still want to try simplifying the
136 // paint's shader to a solid color for the alpha image's colorization.
137 SkColor4f constantColor;
138 if (fShader && as_SB(fShader)->isConstant(&constantColor)) {
139 // The original fColor and `constantColor` are un-premul sRGB, but we need to preserve
140 // the paint's alpha.
141 float origA = fColor.fA;
142 fColor = constantColor;
143 fColor.fA *= origA;
144 fShader = nullptr;
145 }
146 // We can't apply the color filter to the color if a shader modifies it, including when the
147 // image shader is alpha-only. The image's per-pixel alpha modulates the paint color
148 // *before* the color filter is evaluated.
149 if (!fShader && !fImageShader && fColorFilter) {
150 fColor = fColorFilter->filterColor4f(fColor,
151 sk_srgb_singleton(),
152 sk_srgb_singleton());
153 fColorFilter = nullptr;
154 }
155 }
156 }
157
158 PaintParams::PaintParams(const SkPaint& paint,
159 const SkBlender* primitiveBlender,
160 bool skipColorXform,
161 bool ignoreShader)
162 : PaintParams(paint,
163 /*imageOverride=*/nullptr,
164 primitiveBlender,
165 skipColorXform,
166 ignoreShader) {}
167
168 PaintParams::PaintParams(const SkPaint& paint, const SimpleImage& imageOverride, float xtraAlpha)
169 : PaintParams(paint,
170 &imageOverride,
171 /*primitiveBlender=*/nullptr,
172 /*skipColorXform=*/false,
173 // For color images, the paint's original shader is ignored.
174 /*ignoreShader=*/!SkColorTypeIsAlphaOnly(imageOverride.fImage->colorType())) {
175 // Multiply in the extra alpha that's allowed to be set on an ImageSetEntry. Accepting it here
176 // avoids needing to modify the SkPaint providing the base color.
177 fColor.fA *= xtraAlpha;
178 }
179
180 PaintParams::PaintParams(const SkColor4f& color, SkBlendMode finalBlendMode)
181 : fColor(color)
182 , fFinalBlend({nullptr, finalBlendMode})
183 , fShader(nullptr)
184 , fImageShader(nullptr)
185 , fColorFilter(nullptr)
186 , fPrimitiveBlender(nullptr)
187 , fSkipColorXform(false)
188 , fDither(false) {}
189
190 SkColor4f PaintParams::Color4fPrepForDst(SkColor4f srcColor, const SkColorInfo& dstColorInfo) {
191 // xform from sRGB to the destination colorspace
192 SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
193 dstColorInfo.colorSpace(), kUnpremul_SkAlphaType);
194
195 SkColor4f result = srcColor;
196 steps.apply(result.vec());
197 return result;
198 }
199
200 ///////////////////////////////////////////////////////////////////////////////////////////////////
201
202 ShadingParams::ShadingParams(const Caps* caps,
203 const PaintParams& paint,
204 const NonMSAAClip& nonMSAAClip,
205 const SkShader* clipShader,
206 Coverage coverage,
207 TextureFormat targetFormat)
208 : fPaint(paint)
209 , fNonMSAAClip(nonMSAAClip)
210 , fClipShader(clipShader)
211 , fDstUsage(get_dst_usage(caps, targetFormat, paint, coverage, fClipShader, fNonMSAAClip))
212 #if defined(SK_DEBUG)
213 , fDstUsageNoCoverage(get_dst_usage(caps, targetFormat, paint,
214 Coverage::kNone, fClipShader, fNonMSAAClip))
215 , fRendererCoverage(coverage)
216 #endif
217 {}
218
219 bool ShadingParams::addPaintColorToKey(const KeyContext& keyContext) const {
220 const auto& simpleImage = fPaint.imageShader();
221 if (simpleImage) {
222 // There is an implicit image shader, match handling of SkModifyPaintForDrawImageRect
223 if (fPaint.shader()) {
224 // Alpha-only images for drawImageRect() get colorized with the paint's shader. This
225 // differs from alpha-only image shaders that might be encountered within an SkShader
226 // graph, which get colorized by the paint's opaque color.
227 SkASSERT(SkColorTypeIsAlphaOnly(simpleImage->fImage->colorType()));
228 Blend(keyContext,
229 /* addBlendToKey */ [&] () -> void {
230 AddFixedBlendMode(keyContext, SkBlendMode::kDstIn);
231 },
232 /* addSrcToKey = */ [&] () -> void {
233 // Since colorization is handled here, disable paint color-colorization later.
234 AddToKey(keyContext.withExtraFlags(
235 KeyGenFlags::kDisableAlphaOnlyImageColorization),
236 *simpleImage);
237 },
238 /* addDstToKey = */ [&] () -> void {
239 AddToKey(keyContext, fPaint.shader());
240 });
241 return false; // Colorizing with an alpha-only texture probably isn't opaque
242 } else {
243 // Encode the image structure directly, which includes handling alpha-only images that
244 // combine with the paint's color (RGB1) stored on `keyContext`.
245 AddToKey(keyContext, *simpleImage);
246 return simpleImage->fImage->isOpaque();
247 }
248 } else if (fPaint.shader()) {
249 AddToKey(keyContext, fPaint.shader());
250 return fPaint.shader()->isOpaque();
251 } else {
252 RGBPaintColorBlock::AddBlock(keyContext);
253 return true; // rgb1, always opaque
254 }
255 }
256
257 /**
258 * Primitive blend blocks are used to blend either the paint color or the output of another shader
259 * with a primitive color emitted by certain draw geometry calls (drawVertices, drawAtlas, etc.).
260 * Dst: primitiveColor Src: Paint color/shader output
261 */
262 bool ShadingParams::handlePrimitiveColor(const KeyContext& keyContext) const {
263 // If no primitive blending is required, simply add the paint color.
264 if (!fPaint.primitiveBlender()) {
265 return this->addPaintColorToKey(keyContext);
266 }
267
268 // If no color space conversion is required and the primitive blend mode is kDst, the src
269 // branch of the blend does not matter and we can simply emit the primitive color.
270 std::optional<SkBlendMode> primBlend = as_BB(fPaint.primitiveBlender())->asBlendMode();
271 const bool canSkipBlendStep = fPaint.skipPrimitiveColorXform() &&
272 primBlend == SkBlendMode::kDst;
273
274 if (canSkipBlendStep) {
275 AddPrimitiveColor(keyContext, fPaint.skipPrimitiveColorXform());
276 return false;
277 }
278
279 bool srcIsOpaque = false;
280 Blend(keyContext,
281 /* addBlendToKey= */ [&] () -> void {
282 AddToKey(keyContext, fPaint.primitiveBlender());
283 },
284 /* addSrcToKey= */ [&] () -> void {
285 srcIsOpaque = this->addPaintColorToKey(keyContext);
286 },
287 /* addDstToKey= */ [&] () -> void {
288 AddPrimitiveColor(keyContext, fPaint.skipPrimitiveColorXform());
289 });
290 if (primBlend.has_value() && srcIsOpaque) {
291 // If the input paint/shader is opaque, the result is only opaque if the primitive blend
292 // mode is kSrc or kSrcOver. All other modes can introduce transparency.
293 return *primBlend == SkBlendMode::kSrc || *primBlend == SkBlendMode::kSrcOver;
294 }
295
296 // If the input was already transparent, or if it's a runtime/complex blend mode,
297 // the result cannot be considered opaque.
298 return false;
299 }
300
301 // Apply the paint's alpha value.
302 bool ShadingParams::handlePaintAlpha(const KeyContext& keyContext) const {
303 if (!fPaint.shader() && !fPaint.imageShader() && !fPaint.primitiveBlender()) {
304 // If there is no shader and no primitive blending the input to the colorFilter stage
305 // is just the premultiplied paint color.
306 SkPMColor4f paintColor = PaintParams::Color4fPrepForDst(fPaint.color(),
307 keyContext.dstColorInfo()).premul();
308 SolidColorShaderBlock::AddBlock(keyContext, paintColor);
309 return fPaint.color().isOpaque();
310 }
311
312 if (!fPaint.color().isOpaque()) {
313 Blend(keyContext,
314 /* addBlendToKey= */ [&] () -> void {
315 AddFixedBlendMode(keyContext, SkBlendMode::kSrcIn);
316 },
317 /* addSrcToKey= */ [&]() -> void {
318 this->handlePrimitiveColor(keyContext);
319 },
320 /* addDstToKey= */ [&]() -> void {
321 AlphaOnlyPaintColorBlock::AddBlock(keyContext);
322 });
323 // The result is guaranteed to be non-opaque because we're blending with fColor's alpha.
324 return false;
325 } else {
326 return this->handlePrimitiveColor(keyContext);
327 }
328 }
329
330 bool ShadingParams::handleColorFilter(const KeyContext& keyContext) const {
331 if (fPaint.colorFilter()) {
332 bool srcIsOpaque = false;
333 Compose(keyContext,
334 /* addInnerToKey= */ [&]() -> void {
335 srcIsOpaque = this->handlePaintAlpha(keyContext);
336 },
337 /* addOuterToKey= */ [&]() -> void {
338 AddToKey(keyContext, fPaint.colorFilter());
339 });
340 return srcIsOpaque && fPaint.colorFilter()->isAlphaUnchanged();
341 } else {
342 return this->handlePaintAlpha(keyContext);
343 }
344 }
345
346 bool ShadingParams::handleDithering(const KeyContext& keyContext) const {
347
348 #ifndef SK_IGNORE_GPU_DITHER
349 SkColorType ct = keyContext.dstColorInfo().colorType();
350 if (should_dither(fPaint, ct)) {
351 bool srcIsOpaque = false;
352 Compose(keyContext,
353 /* addInnerToKey= */ [&]() -> void {
354 srcIsOpaque = this->handleColorFilter(keyContext);
355 },
356 /* addOuterToKey= */ [&]() -> void {
357 AddDitherBlock(keyContext, ct);
358 });
359 return srcIsOpaque;
360 } else
361 #endif
362 {
363 return this->handleColorFilter(keyContext);
364 }
365 }
366
367 void ShadingParams::handleClipping(const KeyContext& keyContext) const {
368 if (!fNonMSAAClip.isEmpty()) {
369 const AnalyticClip& analyticClip = fNonMSAAClip.fAnalyticClip;
370 SkPoint radiusPair;
371 SkRect analyticBounds;
372 if (!analyticClip.isEmpty()) {
373 float radius = analyticClip.fRadius + 0.5f;
374 // N.B.: Because the clip data is normally used with depth-based clipping,
375 // the shape is inverted from its usual state. We re-invert here to
376 // match what the shader snippet expects.
377 radiusPair = {(analyticClip.fInverted) ? radius : -radius, 1.0f/radius};
378 analyticBounds = analyticClip.fBounds.makeOutset(0.5f).asSkRect();
379 } else {
380 // This will generate no analytic clip.
381 radiusPair = { -0.5f, 1.f };
382 analyticBounds = { 0, 0, 0, 0 };
383 }
384
385 const AtlasClip& atlasClip = fNonMSAAClip.fAtlasClip;
386 SkISize maskSize = atlasClip.fMaskBounds.size();
387 SkRect texMaskBounds = SkRect::MakeXYWH(atlasClip.fOutPos.x(), atlasClip.fOutPos.y(),
388 maskSize.width(), maskSize.height());
389 // Outset bounds to capture some of the padding (necessary for inverse clip)
390 texMaskBounds.outset(0.5f, 0.5f);
391 SkPoint texCoordOffset = SkPoint::Make(atlasClip.fOutPos.x() - atlasClip.fMaskBounds.left(),
392 atlasClip.fOutPos.y() - atlasClip.fMaskBounds.top());
393
394 NonMSAAClipBlock::NonMSAAClipData data(
395 analyticBounds,
396 radiusPair,
397 analyticClip.edgeSelectRect(),
398 texCoordOffset,
399 texMaskBounds,
400 atlasClip.fAtlasTexture);
401 if (fClipShader) {
402 // For both an analytic clip and clip shader, we need to compose them together into
403 // a single clipping root node.
404 Blend(keyContext,
405 /* addBlendToKey= */ [&]() -> void {
406 AddFixedBlendMode(keyContext, SkBlendMode::kModulate);
407 },
408 /* addSrcToKey= */ [&]() -> void {
409 NonMSAAClipBlock::AddBlock(keyContext, data);
410 },
411 /* addDstToKey= */ [&]() -> void {
412 AddToKey(keyContext, fClipShader);
413 });
414 } else {
415 // Without a clip shader, the analytic clip can be the clipping root node.
416 NonMSAAClipBlock::AddBlock(keyContext, data);
417 }
418 } else if (fClipShader) {
419 // Since there's no analytic clip, the clipping root node can be fClipShader directly.
420 AddToKey(keyContext, fClipShader);
421 }
422 }
423
424 std::optional<ShadingParams::Result> ShadingParams::toKey(const KeyContext& keyContext) const {
425 // Root Node 0 is the source color, which is the output of all effects post dithering
426 bool isOpaque = this->handleDithering(keyContext);
427
428 // Root Node 1 is the final blender
429 bool paintDependsOnDst = false;
430 if (fPaint.finalBlender()) {
431 AddToKey(keyContext, fPaint.finalBlender());
432 // Cannot inspect runtime blenders to pessimistically assume they will always use the dst.
433 paintDependsOnDst = true;
434 } else {
435 if (!(fDstUsage & DstUsage::kDstReadRequired)) {
436 // With no shader blending, be as explicit as possible about the final blend
437 AddFixedBlendMode(keyContext, fPaint.finalBlendMode());
438 // Blend modes can be analyzed to determine if specific src colors depend on the dst.
439 paintDependsOnDst = blendmode_depends_on_dst(fPaint.finalBlendMode(), isOpaque);
440 } else {
441 // With shader blending, use AddBlendMode() to select the more universal blend functions
442 // when possible. Technically we could always use a fixed blend mode but would then
443 // over-generate when encountering certain classes of blends. This is most problematic
444 // on devices that wouldn't support dual-source blending, so help them out by at least
445 // not requiring lots of pipelines.
446 AddBlendMode(keyContext, fPaint.finalBlendMode());
447 // TODO(b/478239991): With shader-based blending, always treat the paint as depending
448 // on the dst. Almost all blend modes that trigger a dst read wouldn't have returned
449 // true from `blendmode_depends_on_dst`, but the exception is kSrc triggering a dst
450 // read when the renderer has coverage and there's no dual-src blending support. In that
451 // case we can't support an inner fill operation w/o coverage that shares the same
452 // paint key (it would be expecting a fixed blend mode node here).
453 paintDependsOnDst = true;
454 }
455 }
456
457 // Optional Root Node 2 is the clip
458 this->handleClipping(keyContext);
459
460 SkEnumBitMask<DstUsage> dstUsage = fDstUsage;
461 if (paintDependsOnDst) {
462 // Must add kDependsOnDst and remove kDstOnlyUsedByRenderer
463 dstUsage |= DstUsage::kDependsOnDst;
464 dstUsage &= ~DstUsage::kDstOnlyUsedByRenderer;
465 }
466
467 // If dstUsage is not kNone, then kDependsOnDst must be set (all other bits only apply *because*
468 // the shading depends on dst).
469 SkASSERT(dstUsage == DstUsage::kNone || (dstUsage & DstUsage::kDependsOnDst));
470
471 // If dstUsage is kNone, then there cannot be renderer coverage, analytic clipping, or the paint
472 // depending on the dst color.
473 SkASSERT(dstUsage != DstUsage::kNone || !(paintDependsOnDst ||
474 fClipShader ||
475 !fNonMSAAClip.isEmpty() ||
476 fRendererCoverage != Coverage::kNone));
477
478 // If kDstOnlyUsedByRenderer is set, the paint shouldn't depend on the dst and the dst usage
479 // when the Renderer has Coverage::kNone should equal kNone
480 SkASSERT(!(dstUsage & DstUsage::kDstOnlyUsedByRenderer) ||
481 (!paintDependsOnDst && fDstUsageNoCoverage == DstUsage::kNone));
482 UniquePaintParamsID paintID =
483 keyContext.recorder()->priv().shaderCodeDictionary()->findOrCreate(
484 keyContext.paintParamsKeyBuilder());
485 if (!paintID.isValid()) {
486 return {};
487 } else {
488 return Result{paintID, dstUsage};
489 }
490 }
491
492 } // namespace skgpu::graphite