Debian Codegraph @

src/gpu/graphite/PaintParams.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_PaintParams_DEFINED
9 #define skgpu_graphite_PaintParams_DEFINED
10
11 #include "include/core/SkColor.h"
12 #include "include/core/SkPaint.h"
13 #include "src/base/SkEnumBitMask.h"
14 #include "src/gpu/graphite/Caps.h"
15 #include "src/gpu/graphite/Renderer.h"
16 #include "src/gpu/graphite/geom/NonMSAAClip.h"
17
18 class SkColorInfo;
19 class SkImage;
20 class SkShader;
21
22 namespace skgpu::graphite {
23
24 class DrawContext;
25 class KeyContext;
26 class FloatStorageManager;
27 class PaintParamsKeyBuilder;
28 class PipelineDataGatherer;
29 class Recorder;
30 class TextureProxy;
31 class UniquePaintParamsID;
32
33 // NOTE: Only represents the shading state of an SkPaint. Style and complex effects (mask filters,
34 // image filters, path effects) must be handled higher up. AA is not tracked since everything is
35 // assumed to be anti-aliased.
36 //
37 // The full shading of a draw is a combination of the PaintParams extracted from an SkPaint, any
38 // analytic NonMSAA clipping, analytic coverage from a RenderStep, and the target TextureFormat
39 // being rendered into. These additional parameters are aggregated together in ShadingParams.
40 //
41 // Both PaintParams and ShadingParams are meant to be short-lived objects used for processing a
42 // draw's effects into a UniquePaintID and extracted uniforms and textures. As such, they do not
43 // keep high-level Skia objects alive since these Params types should go out of scope by the end of
44 // the draw call.
45 class PaintParams {
46 public:
47 // Stores just the parameters of the implicit image shading model used by drawImageRect and
48 // other image-drawing APIs, e.g. to apply SkImageShader::MakeForDrawRect without the
49 // overhead of creating additional SkShader objects. Assumes clamp tiling; if no clamping is
50 // required, set to the image's bounds.
51 struct SimpleImage {
52 // fImage (required) and fLocalMatrix (optional) must outlive the PaintParams object
53 const SkImage* fImage;
54 const SkMatrix* fLocalMatrix = nullptr;
55 // Post local matrix strict clamping rectangle (e.g. relative to image's texels)
56 SkRect fSubset;
57 SkSamplingOptions fSamplingOptions;
58 };
59
60 // Converts an SkPaint to PaintParams, possibly adding a primitive blender (e.g. for
61 // drawVertices or text rendering).
62 explicit PaintParams(const SkPaint& paint,
63 const SkBlender* primitiveBlender = nullptr,
64 bool skipColorXform = false,
65 bool ignoreShader = false);
66
67 // Converts an SkPaint to PaintParams and accounts for the implicit SkImage shader override
68 // from drawImageRect and related functions. Multiplies `xtraAlpha` with the paint's alpha.
69 //
70 // NOTE: Does not copy `imageOverride`, this must live for the lifetime of PaintParams
71 PaintParams(const SkPaint&, const SimpleImage& imageOverride, float xtraAlpha=1.f);
72
73 // Creates a constant color PaintParams with the specific blend mode.
74 PaintParams(const SkColor4f& color, SkBlendMode finalBlendMode);
75
76 const SkColor4f& color() const { return fColor; }
77 const SkShader* shader() const { return fShader; }
78 const SimpleImage* imageShader() const { return fImageShader; }
79 const SkColorFilter* colorFilter() const { return fColorFilter; }
80 const SkBlender* primitiveBlender() const { return fPrimitiveBlender; }
81 bool skipPrimitiveColorXform() const { return fSkipColorXform; }
82
83 const SkBlender* finalBlender() const { return fFinalBlend.first; }
84 // Must also check finalBlender() to see if that overrides finalBlendMode() behavior.
85 SkBlendMode finalBlendMode() const { SkASSERT(!fFinalBlend.first); return fFinalBlend.second; }
86
87 bool dither() const { return fDither; }
88
89 /** Converts an SkColor4f to the destination color space. */
90 static SkColor4f Color4fPrepForDst(SkColor4f srgb, const SkColorInfo& dstColorInfo);
91
92 private:
93 PaintParams(const SkPaint&,
94 const SimpleImage* imageOverride,
95 const SkBlender* primitiveBlender,
96 bool skipColorXform,
97 bool ignoreShader);
98
99 SkColor4f fColor;
100
101 // Either a non-null SkBlender for runtime blending, or the SkBlendMode to use instead. If
102 // the blender is non-null, the blend mode is set to kSrc to match the HW blend config used for
103 // shader-based blending.
104 std::pair<const SkBlender*, SkBlendMode> fFinalBlend;
105
106 const SkShader* fShader;
107 const SimpleImage* fImageShader; // Overrides fShader for color images, mixes for alpha
108 const SkColorFilter* fColorFilter;
109
110 // A nullptr fPrimitiveBlender means there's no primitive color blending and it is skipped.
111 // In the case where there is primitive blending, the primitive color is the source color and
112 // the dest is the paint's color (or the paint's shader's computed color).
113 const SkBlender* fPrimitiveBlender;
114 bool fSkipColorXform;
115 bool fDither;
116 };
117
118 // ShadingParams wraps a PaintParams with the additional per-pixel state to handle clipping and
119 // anti-aliasing, as well as making the final determinations for how blending will be implemented
120 // given the current hardware.
121 class ShadingParams {
122 public:
123 // NOTE: Does not copy `paint`, `nonMSAAClip` or `clipShader`; these must outlive ShadingParams.
124 ShadingParams(const Caps* caps,
125 const PaintParams& paint,
126 const NonMSAAClip& nonMSAAClip,
127 const SkShader* clipShader,
128 Coverage coverage,
129 TextureFormat targetFormat);
130
131 bool dstReadRequired() const { return SkToBool(fDstUsage & DstUsage::kDstReadRequired); }
132
133 using Result = std::tuple<UniquePaintParamsID, SkEnumBitMask<DstUsage>>;
134 std::optional<Result> toKey(const KeyContext&) const;
135
136 private:
137 bool addPaintColorToKey(const KeyContext&) const;
138 bool handlePrimitiveColor(const KeyContext&) const;
139 bool handlePaintAlpha(const KeyContext&) const;
140 bool handleColorFilter(const KeyContext&) const;
141 bool handleDithering(const KeyContext&) const;
142 bool handleDstRead(const KeyContext&) const;
143 void handleClipping(const KeyContext&) const;
144
145 const PaintParams& fPaint;
146 const NonMSAAClip& fNonMSAAClip;
147 const SkShader* fClipShader;
148
149 // Base (incomplete) dst usage that will be augmented by opacity analysis calculated in toKey()
150 const SkEnumBitMask<DstUsage> fDstUsage;
151
152 // Used for asserts
153 SkDEBUGCODE(const SkEnumBitMask<DstUsage> fDstUsageNoCoverage;)
154 SkDEBUGCODE(const Coverage fRendererCoverage;)
155 };
156
157 } // namespace skgpu::graphite
158
159 #endif // skgpu_PaintParams_DEFINED