Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Precompile.h
Go to the documentation of this file.
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_Precompile_DEFINED
9#define skgpu_graphite_Precompile_DEFINED
10
13#include "include/core/SkSpan.h"
16
17#include <functional>
18#include <optional>
19#include <vector>
20
21class SkRuntimeEffect;
22
23namespace skgpu::graphite {
24
25enum class Coverage;
26class KeyContext;
27class PipelineDataGatherer;
28class PrecompileBasePriv;
29class UniquePaintParamsID;
30
31// Create the Pipelines specified by 'options' by combining the shading portion w/ the specified
32// 'drawTypes' and a stock set of RenderPass descriptors (e.g., kDepth+msaa, kDepthStencil+msaa)
34 const PaintOptions& options,
35 const KeyContext& keyContext,
36 DrawTypeFlags drawTypes,
37 bool withPrimitiveBlender,
38 Coverage coverage);
39
40class PrecompileBase : public SkRefCnt {
41public:
42 enum class Type {
47 kShader,
48 // TODO: add others: kDrawable, kPathEffect (?!)
49 };
50
52
53 Type type() const { return fType; }
54
55 // TODO: Maybe convert these two to be parameters passed into PrecompileBase from all the
56 // derived classes and then make them non-virtual.
57 virtual int numIntrinsicCombinations() const { return 1; }
58 virtual int numChildCombinations() const { return 1; }
59
60 int numCombinations() const {
61 return this->numIntrinsicCombinations() * this->numChildCombinations();
62 }
63
64 // Provides access to functions that aren't part of the public API.
66 const PrecompileBasePriv priv() const; // NOLINT(readability-const-return-type)
67
68protected:
69 // This returns the desired option along with the child options.
70 template<typename T>
71 static std::pair<sk_sp<T>, int> SelectOption(SkSpan<const sk_sp<T>> options, int desiredOption);
72
73 // In general, derived classes should use AddToKey to select the desired child option from
74 // a vector and then have it added to the key with its reduced/nested child option.
75 template<typename T>
76 static void AddToKey(const KeyContext&,
79 SkSpan<const sk_sp<T>> options,
80 int desiredOption);
81
82private:
83 friend class PaintOptions;
84 friend class PrecompileBasePriv;
85
86 virtual bool isALocalMatrixShader() const { return false; }
87
88 virtual void addToKey(const KeyContext&,
91 int desiredCombination) const = 0;
92
93 Type fType;
94};
95
96//--------------------------------------------------------------------------------------------------
97
98template<typename T>
99std::pair<sk_sp<T>, int> PrecompileBase::SelectOption(SkSpan<const sk_sp<T>> options,
100 int desiredOption) {
101 for (const sk_sp<T>& option : options) {
102 if (desiredOption < (option ? option->numCombinations() : 1)) {
103 return { option, desiredOption };
104 }
105 desiredOption -= option ? option->numCombinations() : 1;
106 }
107 return { nullptr, 0 };
108}
109
110template<typename T>
112 PaintParamsKeyBuilder* builder,
113 PipelineDataGatherer* gatherer,
114 SkSpan<const sk_sp<T>> options,
115 int desiredOption) {
116 auto [option, childOptions] = SelectOption(options, desiredOption);
117 if (option) {
118 option->priv().addToKey(keyContext, builder, gatherer, childOptions);
119 }
120}
121
122//--------------------------------------------------------------------------------------------------
124
139
144
151
153public:
155
156 virtual std::optional<SkBlendMode> asBlendMode() const { return {}; }
157
159};
160
161enum class PrecompileImageFilters : uint32_t {
162 kNone = 0x0,
163 kBlur = 0x1,
164};
166
167//--------------------------------------------------------------------------------------------------
168class PaintOptionsPriv;
169
171public:
173 fShaderOptions.assign(shaders.begin(), shaders.end());
174 }
175
177 fMaskFilterOptions.assign(maskFilters.begin(), maskFilters.end());
178 }
179
181 fColorFilterOptions.assign(colorFilters.begin(), colorFilters.end());
182 }
183
187
189 fBlendModeOptions.append(blendModes.size(), blendModes.data());
190 }
192 for (const sk_sp<PrecompileBlender>& b: blenders) {
193 if (b->asBlendMode().has_value()) {
194 fBlendModeOptions.push_back(b->asBlendMode().value());
195 } else {
196 fBlenderOptions.push_back(b);
197 }
198 }
199 }
200
201 void setClipShaders(SkSpan<const sk_sp<PrecompileShader>> clipShaders);
202
203 void setDither(bool dither) { fDither = dither; }
204
205 typedef std::function<void(UniquePaintParamsID id,
207 bool withPrimitiveBlender,
209
210 // Provides access to functions that aren't part of the public API.
212 const PaintOptionsPriv priv() const; // NOLINT(readability-const-return-type)
213
214private:
215 friend class PaintOptionsPriv;
216
217 int numShaderCombinations() const;
218 int numMaskFilterCombinations() const;
219 int numColorFilterCombinations() const;
220 // TODO: need to decompose imagefilters into component draws
221 int numBlendModeCombinations() const;
222 int numClipShaderCombinations() const;
223
224 int numCombinations() const;
225 // 'desiredCombination' must be less than the result of the numCombinations call
226 void createKey(const KeyContext&,
229 int desiredCombination,
230 bool addPrimitiveBlender,
231 Coverage coverage) const;
232
233 void buildCombinations(
234 const KeyContext&,
236 DrawTypeFlags drawTypes,
237 bool addPrimitiveBlender,
238 Coverage coverage,
239 const ProcessCombination& processCombination) const;
240
241 std::vector<sk_sp<PrecompileShader>> fShaderOptions;
242 std::vector<sk_sp<PrecompileMaskFilter>> fMaskFilterOptions;
243 std::vector<sk_sp<PrecompileColorFilter>> fColorFilterOptions;
245 SkTDArray<SkBlendMode> fBlendModeOptions;
247 std::vector<sk_sp<PrecompileShader>> fClipShaderOptions;
248 bool fDither = false;
249};
250
251} // namespace skgpu::graphite
252
253#endif // skgpu_graphite_Precompile_DEFINED
const char * options
SkBlendMode
Definition SkBlendMode.h:38
#define SK_MAKE_BITMASK_OPS(E)
constexpr T * data() const
Definition SkSpan_impl.h:94
constexpr size_t size() const
Definition SkSpan_impl.h:95
void push_back(const T &v)
Definition SkTDArray.h:219
T * append()
Definition SkTDArray.h:191
void setColorFilters(SkSpan< const sk_sp< PrecompileColorFilter > > colorFilters)
Definition Precompile.h:180
void setImageFilters(SkEnumBitMask< PrecompileImageFilters > options)
Definition Precompile.h:184
void setDither(bool dither)
Definition Precompile.h:203
void setShaders(SkSpan< const sk_sp< PrecompileShader > > shaders)
Definition Precompile.h:172
void setBlendModes(SkSpan< SkBlendMode > blendModes)
Definition Precompile.h:188
std::function< void(UniquePaintParamsID id, DrawTypeFlags, bool withPrimitiveBlender, Coverage)> ProcessCombination
Definition Precompile.h:208
void setMaskFilters(SkSpan< const sk_sp< PrecompileMaskFilter > > maskFilters)
Definition Precompile.h:176
void setClipShaders(SkSpan< const sk_sp< PrecompileShader > > clipShaders)
void setBlenders(SkSpan< const sk_sp< PrecompileBlender > > blenders)
Definition Precompile.h:191
static void AddToKey(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, SkSpan< const sk_sp< T > > options, int desiredOption)
Definition Precompile.h:111
virtual bool isALocalMatrixShader() const
Definition Precompile.h:86
virtual int numChildCombinations() const
Definition Precompile.h:58
virtual int numIntrinsicCombinations() const
Definition Precompile.h:57
virtual void addToKey(const KeyContext &, PaintParamsKeyBuilder *, PipelineDataGatherer *, int desiredCombination) const =0
static std::pair< sk_sp< T >, int > SelectOption(SkSpan< const sk_sp< T > > options, int desiredOption)
Definition Precompile.h:99
virtual std::optional< SkBlendMode > asBlendMode() const
Definition Precompile.h:156
sk_sp< PrecompileColorFilter > makeComposed(sk_sp< PrecompileColorFilter > inner) const
sk_sp< PrecompileShader > makeWithColorFilter(sk_sp< PrecompileColorFilter >)
sk_sp< PrecompileShader > makeWithWorkingColorSpace(sk_sp< SkColorSpace >)
sk_sp< PrecompileShader > makeWithCTM()
virtual bool isConstant(int desiredCombination) const
Definition Precompile.h:129
sk_sp< PrecompileShader > makeWithLocalMatrix()
static bool b
void PrecompileCombinations(Context *context, const PaintOptions &options, const KeyContext &keyContext, DrawTypeFlags drawTypes, bool withPrimitiveBlender, Coverage coverage)