xref: /aosp_15_r20/external/skia/src/gpu/graphite/PaintParams.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/gpu/graphite/Caps.h"
14 #include "src/gpu/graphite/geom/AnalyticClip.h"
15 #include <functional>  // std::function
16 
17 class SkColorInfo;
18 class SkShader;
19 
20 namespace skgpu::graphite {
21 
22 class DrawContext;
23 class KeyContext;
24 class PaintParamsKeyBuilder;
25 class PipelineDataGatherer;
26 class Recorder;
27 class TextureProxy;
28 
29 // TBD: If occlusion culling is eliminated as a phase, we can easily move the paint conversion
30 // back to Device when the command is recorded (similar to SkPaint -> GrPaint), and then
31 // PaintParams is not required as an intermediate representation.
32 // NOTE: Only represents the shading state of an SkPaint. Style and complex effects (mask filters,
33 // image filters, path effects) must be handled higher up. AA is not tracked since everything is
34 // assumed to be anti-aliased.
35 class PaintParams {
36 public:
37     explicit PaintParams(const SkPaint&,
38                          sk_sp<SkBlender> primitiveBlender,
39                          const CircularRRectClip& analyticClip,
40                          sk_sp<SkShader> clipShader,
41                          DstReadRequirement dstReadReq,
42                          bool skipColorXform);
43 
44     PaintParams(const PaintParams&);
45     ~PaintParams();
46 
47     PaintParams& operator=(const PaintParams&);
48 
color()49     SkColor4f color() const { return fColor; }
50 
51     std::optional<SkBlendMode> asFinalBlendMode() const;
finalBlender()52     SkBlender* finalBlender() const { return fFinalBlender.get(); }
53     sk_sp<SkBlender> refFinalBlender() const;
54 
shader()55     SkShader* shader() const { return fShader.get(); }
56     sk_sp<SkShader> refShader() const;
57 
colorFilter()58     SkColorFilter* colorFilter() const { return fColorFilter.get(); }
59     sk_sp<SkColorFilter> refColorFilter() const;
60 
primitiveBlender()61     SkBlender* primitiveBlender() const { return fPrimitiveBlender.get(); }
62     sk_sp<SkBlender> refPrimitiveBlender() const;
63 
dstReadRequirement()64     DstReadRequirement dstReadRequirement() const { return fDstReadReq; }
skipColorXform()65     bool skipColorXform() const { return fSkipColorXform; }
dither()66     bool dither() const { return fDither; }
67 
68     /** Converts an SkColor4f to the destination color space. */
69     static SkColor4f Color4fPrepForDst(SkColor4f srgb, const SkColorInfo& dstColorInfo);
70 
71     void toKey(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
72 
73     void notifyImagesInUse(Recorder*, DrawContext*) const;
74 
75 private:
76     void addPaintColorToKey(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
77     void handlePrimitiveColor(const KeyContext&,
78                               PaintParamsKeyBuilder*,
79                               PipelineDataGatherer*) const;
80     void handlePaintAlpha(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
81     void handleColorFilter(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
82     void handleDithering(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
83     void handleDstRead(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
84     void handleClipping(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*) const;
85 
86     SkColor4f            fColor;
87     sk_sp<SkBlender>     fFinalBlender; // A nullptr here means SrcOver blending
88     sk_sp<SkShader>      fShader;
89     sk_sp<SkColorFilter> fColorFilter;
90     // A nullptr fPrimitiveBlender means there's no primitive color blending and it is skipped.
91     // In the case where there is primitive blending, the primitive color is the source color and
92     // the dest is the paint's color (or the paint's shader's computed color).
93     sk_sp<SkBlender>     fPrimitiveBlender;
94     CircularRRectClip    fAnalyticClip;
95     sk_sp<SkShader>      fClipShader;
96     DstReadRequirement   fDstReadReq;
97     bool                 fSkipColorXform;
98     bool                 fDither;
99 };
100 
101 using AddToKeyFn = std::function<void()>;
102 
103 void Blend(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*,
104            AddToKeyFn addBlendToKey, AddToKeyFn addSrcToKey, AddToKeyFn addDstToKey);
105 void Compose(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*,
106              AddToKeyFn addInnerToKey, AddToKeyFn addOuterToKey);
107 // Add a fixed blend mode node for a specific SkBlendMode.
108 void AddFixedBlendMode(const KeyContext&,
109                        PaintParamsKeyBuilder*,
110                        PipelineDataGatherer*,
111                        SkBlendMode);
112 // Add a blend mode node for an SkBlendMode that can vary
113 void AddBlendMode(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, SkBlendMode);
114 void AddDitherBlock(const KeyContext&, PaintParamsKeyBuilder*, PipelineDataGatherer*, SkColorType);
115 
116 } // namespace skgpu::graphite
117 
118 #endif // skgpu_PaintParams_DEFINED
119