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