xref: /aosp_15_r20/external/skia/include/gpu/graphite/precompile/PaintOptions.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 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_precompile_PaintOptions_DEFINED
9 #define skgpu_graphite_precompile_PaintOptions_DEFINED
10 
11 #include "include/core/SkBlendMode.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSpan.h"
14 #include "include/private/base/SkTArray.h"
15 #include "include/private/base/SkTDArray.h"
16 
17 #include <functional>
18 
19 namespace skgpu::graphite {
20 
21 class PrecompileBlender;
22 class PrecompileColorFilter;
23 class PrecompileImageFilter;
24 class PrecompileMaskFilter;
25 class PrecompileShader;
26 
27 enum class Coverage;
28 enum DrawTypeFlags : uint16_t;
29 enum class PrecompileImageFilterFlags : uint32_t;
30 
31 class KeyContext;
32 class PaintOptionsPriv;
33 class PaintParamsKeyBuilder;
34 class PipelineDataGatherer;
35 struct RenderPassDesc;
36 class UniquePaintParamsID;
37 
38 /** \class PaintOptions
39     This is the Precompilation analog to SkPaint. It encapsulates a set of options for each
40     field of the SkPaint (e.g., colorFilters, imageFilters, etc). Many of the specific details
41     of an SkPaint that are irrelevant to the final compiled Pipelines are abstracted away
42     (e.g., the SkPaint's color field).
43 
44     How Precompilation works in practice is a PaintOptions object is created and a set of options
45     for each slot (e.g., shader, blender) are added. When passed to the Precompile() function,
46     all the combinations specified by the PaintOptions will be created and precompiled.
47 
48     To be concrete, if a PaintOptions object had two shader options and two blender options,
49     four combinations would be precompiled.
50 */
51 class SK_API PaintOptions {
52 public:
53     /** Constructs a PaintOptions object with default values. It is equivalent to a default
54      *  initialized SkPaint.
55 
56         @return  default initialized PaintOptions
57     */
58     PaintOptions();
59     PaintOptions(const PaintOptions&);
60     ~PaintOptions();
61     PaintOptions& operator=(const PaintOptions&);
62 
63     /** Sets the shader options used when generating precompilation combinations.
64 
65         This corresponds to SkPaint's setShader() method
66 
67         @param shaders  The options used for shading when generating precompilation combinations.
68     */
69     void setShaders(SkSpan<const sk_sp<PrecompileShader>> shaders);
getShaders()70     SkSpan<const sk_sp<PrecompileShader>> getShaders() const {
71         return SkSpan<const sk_sp<PrecompileShader>>(fShaderOptions);
72     }
73 
74     /** Sets the image filter options used when generating precompilation combinations.
75 
76         This corresponds to SkPaint's setImageFilter() method
77 
78         @param imageFilters  The options used for image filtering when generating precompilation
79                              combinations.
80     */
81     void setImageFilters(SkSpan<const sk_sp<PrecompileImageFilter>> imageFilters);
getImageFilters()82     SkSpan<const sk_sp<PrecompileImageFilter>> getImageFilters() const {
83         return SkSpan<const sk_sp<PrecompileImageFilter>>(fImageFilterOptions);
84     }
85 
86     /** Sets the mask filter options used when generating precompilation combinations.
87 
88         This corresponds to SkPaint's setMaskFilter() method
89 
90         @param maskFilters  The options used for mask filtering when generating precompilation
91                             combinations.
92     */
93     void setMaskFilters(SkSpan<const sk_sp<PrecompileMaskFilter>> maskFilters);
getMaskFilters()94     SkSpan<const sk_sp<PrecompileMaskFilter>> getMaskFilters() const {
95         return SkSpan<const sk_sp<PrecompileMaskFilter>>(fMaskFilterOptions);
96     }
97 
98     /** Sets the color filter options used when generating precompilation combinations.
99 
100         This corresponds to SkPaint's setColorFilter() method
101 
102         @param colorFilters  The options used for color filtering when generating precompilation
103                              combinations.
104     */
105     void setColorFilters(SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters);
getColorFilters()106     SkSpan<const sk_sp<PrecompileColorFilter>> getColorFilters() const {
107         return SkSpan<const sk_sp<PrecompileColorFilter>>(fColorFilterOptions);
108     }
109 
110     /** Sets the blend mode options used when generating precompilation combinations.
111 
112         This corresponds to SkPaint's setBlendMode() method
113 
114         @param blendModes  The options used for blending when generating precompilation
115                            combinations.
116     */
117     void setBlendModes(SkSpan<const SkBlendMode> blendModes);
getBlendModes()118     SkSpan<const SkBlendMode> getBlendModes() const {
119         return SkSpan<const SkBlendMode>(fBlendModeOptions.data(), fBlendModeOptions.size());
120     }
121 
122     /** Sets the blender options used when generating precompilation combinations.
123 
124         This corresponds to SkPaint's setBlender() method
125 
126         @param blenders  The options used for blending when generating precompilation combinations.
127     */
128     void setBlenders(SkSpan<const sk_sp<PrecompileBlender>> blenders);
getBlenders()129     SkSpan<const sk_sp<PrecompileBlender>> getBlenders() const {
130         return SkSpan<const sk_sp<PrecompileBlender>>(fBlenderOptions);
131     }
132 
133     /** Sets the dither setting used when generating precompilation combinations
134 
135         This corresponds to SkPaint's setDither() method
136 
137         @param dither  the dither setting used when generating precompilation combinations.
138     */
setDither(bool dither)139     void setDither(bool dither) { fDither = dither; }
isDither()140     bool isDither() const { return fDither; }
141 
142     // Provides access to functions that aren't part of the public API.
143     PaintOptionsPriv priv();
144     const PaintOptionsPriv priv() const;  // NOLINT(readability-const-return-type)
145 
146 private:
147     friend class PaintOptionsPriv;
148     friend class PrecompileImageFilter; // for ProcessCombination access
149     friend class PrecompileMaskFilter;  // for ProcessCombination access
150 
151     void addColorFilter(sk_sp<PrecompileColorFilter> cf);
addBlendMode(SkBlendMode bm)152     void addBlendMode(SkBlendMode bm) {
153         fBlendModeOptions.push_back(bm);
154     }
155 
156     void setClipShaders(SkSpan<const sk_sp<PrecompileShader>> clipShaders);
157 
158     int numShaderCombinations() const;
159     int numColorFilterCombinations() const;
160     int numBlendCombinations() const;
161     int numClipShaderCombinations() const;
162 
163     int numCombinations() const;
164     // 'desiredCombination' must be less than the result of the numCombinations call
165     void createKey(const KeyContext&,
166                    PaintParamsKeyBuilder*,
167                    PipelineDataGatherer*,
168                    int desiredCombination,
169                    bool addPrimitiveBlender,
170                    Coverage coverage) const;
171 
172     typedef std::function<void(UniquePaintParamsID id,
173                                DrawTypeFlags,
174                                bool withPrimitiveBlender,
175                                Coverage,
176                                const RenderPassDesc&)> ProcessCombination;
177 
178     void buildCombinations(const KeyContext&,
179                            PipelineDataGatherer*,
180                            DrawTypeFlags,
181                            bool addPrimitiveBlender,
182                            Coverage,
183                            const RenderPassDesc&,
184                            const ProcessCombination&) const;
185 
186     skia_private::TArray<sk_sp<PrecompileShader>> fShaderOptions;
187     skia_private::TArray<sk_sp<PrecompileColorFilter>> fColorFilterOptions;
188     skia_private::TArray<SkBlendMode> fBlendModeOptions;
189     skia_private::TArray<sk_sp<PrecompileBlender>> fBlenderOptions;
190     skia_private::TArray<sk_sp<PrecompileShader>> fClipShaderOptions;
191 
192     skia_private::TArray<sk_sp<PrecompileImageFilter>> fImageFilterOptions;
193     skia_private::TArray<sk_sp<PrecompileMaskFilter>> fMaskFilterOptions;
194 
195     bool fDither = false;
196 };
197 
198 } // namespace skgpu::graphite
199 
200 #endif // skgpu_graphite_precompile_PaintOptions_DEFINED
201