• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 SkRuntimeEffect_DEFINED
9 #define SkRuntimeEffect_DEFINED
10 
11 #include "include/core/SkBlender.h"  // IWYU pragma: keep
12 #include "include/core/SkColorFilter.h"  // IWYU pragma: keep
13 #include "include/core/SkData.h"
14 #include "include/core/SkFlattenable.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSpan.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTypes.h"
21 #include "include/private/SkSLSampleUsage.h"
22 #include "include/private/base/SkOnce.h"
23 #include "include/private/base/SkTemplates.h"
24 #include "include/private/base/SkTo.h"
25 #include "include/private/base/SkTypeTraits.h"
26 #include "include/sksl/SkSLDebugTrace.h"
27 #include "include/sksl/SkSLVersion.h"
28 
29 #include <cstddef>
30 #include <cstdint>
31 #include <cstring>
32 #include <memory>
33 #include <optional>
34 #include <string>
35 #include <string_view>
36 #include <utility>
37 #include <vector>
38 
39 struct SkIPoint;
40 
41 namespace SkSL {
42 class DebugTracePriv;
43 class FunctionDefinition;
44 struct Program;
45 enum class ProgramKind : int8_t;
46 struct ProgramSettings;
47 }  // namespace SkSL
48 
49 namespace SkSL::RP {
50 class Program;
51 }
52 
53 /*
54  * SkRuntimeEffect supports creating custom SkShader and SkColorFilter objects using Skia's SkSL
55  * shading language.
56  *
57  * NOTE: This API is experimental and subject to change.
58  */
59 class SK_API SkRuntimeEffect : public SkRefCnt {
60 public:
61     // Reflected description of a uniform variable in the effect's SkSL
62     struct SK_API Uniform {
63         enum class Type {
64             kFloat,
65             kFloat2,
66             kFloat3,
67             kFloat4,
68             kFloat2x2,
69             kFloat3x3,
70             kFloat4x4,
71             kInt,
72             kInt2,
73             kInt3,
74             kInt4,
75         };
76 
77         enum Flags {
78             // Uniform is declared as an array. 'count' contains array length.
79             kArray_Flag = 0x1,
80 
81             // Uniform is declared with layout(color). Colors should be supplied as unpremultiplied,
82             // extended-range (unclamped) sRGB (ie SkColor4f). The uniform will be automatically
83             // transformed to unpremultiplied extended-range working-space colors.
84             kColor_Flag = 0x2,
85 
86             // When used with SkMeshSpecification, indicates that the uniform is present in the
87             // vertex shader. Not used with SkRuntimeEffect.
88             kVertex_Flag = 0x4,
89 
90             // When used with SkMeshSpecification, indicates that the uniform is present in the
91             // fragment shader. Not used with SkRuntimeEffect.
92             kFragment_Flag = 0x8,
93 
94             // This flag indicates that the SkSL uniform uses a medium-precision type
95             // (i.e., `half` instead of `float`).
96             kHalfPrecision_Flag = 0x10,
97         };
98 
99         std::string_view name;
100         size_t           offset;
101         Type             type;
102         int              count;
103         uint32_t         flags;
104 
isArrayUniform105         bool isArray() const { return SkToBool(this->flags & kArray_Flag); }
isColorUniform106         bool isColor() const { return SkToBool(this->flags & kColor_Flag); }
107         size_t sizeInBytes() const;
108     };
109 
110     // Reflected description of a uniform child (shader or colorFilter) in the effect's SkSL
111     enum class ChildType {
112         kShader,
113         kColorFilter,
114         kBlender,
115     };
116 
117     struct Child {
118         std::string_view name;
119         ChildType        type;
120         int              index;
121     };
122 
123     class Options {
124     public:
125         // For testing purposes, disables optimization and inlining. (Normally, Runtime Effects
126         // don't run the inliner directly, but they still get an inlining pass once they are
127         // painted.)
128         bool forceUnoptimized = false;
129 
130         // Advanced Filter: It indicates whether AF is enabled
131         // The value will pass to ProgramSettings::fUseAF.
132         bool useAF = false;
133 
134     private:
135         friend class SkRuntimeEffect;
136         friend class SkRuntimeEffectPriv;
137 
138         // This flag allows Runtime Effects to access Skia implementation details like sk_FragCoord
139         // and functions with private identifiers (e.g. $rgb_to_hsl).
140         bool allowPrivateAccess = false;
141         // When not 0, this field allows Skia to assign a stable key to a known runtime effect
142         uint32_t fStableKey = 0;
143 
144         // TODO(skia:11209) - Replace this with a promised SkCapabilities?
145         // This flag lifts the ES2 restrictions on Runtime Effects that are gated by the
146         // `strictES2Mode` check. Be aware that the software renderer and pipeline-stage effect are
147         // still largely ES3-unaware and can still fail or crash if post-ES2 features are used.
148         // This is only intended for use by tests and certain internally created effects.
149         SkSL::Version maxVersionAllowed = SkSL::Version::k100;
150     };
151 
152     // If the effect is compiled successfully, `effect` will be non-null.
153     // Otherwise, `errorText` will contain the reason for failure.
154     struct Result {
155         sk_sp<SkRuntimeEffect> effect;
156         SkString errorText;
157     };
158 
159     // MakeForColorFilter and MakeForShader verify that the SkSL code is valid for those stages of
160     // the Skia pipeline. In all of the signatures described below, color parameters and return
161     // values are flexible. They are listed as being 'vec4', but they can also be 'half4' or
162     // 'float4'. ('vec4' is an alias for 'float4').
163 
164     // We can't use a default argument for `options` due to a bug in Clang.
165     // https://bugs.llvm.org/show_bug.cgi?id=36684
166 
167     // Color filter SkSL requires an entry point that looks like:
168     //     vec4 main(vec4 inColor) { ... }
169     //     https://fiddle.skia.org/c/@runtimeeffect_colorfilter_grid
170     static Result MakeForColorFilter(SkString sksl, const Options&);
MakeForColorFilter(SkString sksl)171     static Result MakeForColorFilter(SkString sksl) {
172         return MakeForColorFilter(std::move(sksl), Options{});
173     }
174 
175     // Shader SkSL requires an entry point that looks like:
176     //     vec4 main(vec2 inCoords) { ... }
177     // The color that is returned should be premultiplied.
178     static Result MakeForShader(SkString sksl, const Options&);
MakeForShader(SkString sksl)179     static Result MakeForShader(SkString sksl) {
180         return MakeForShader(std::move(sksl), Options{});
181     }
182 
183     // Blend SkSL requires an entry point that looks like:
184     //     vec4 main(vec4 srcColor, vec4 dstColor) { ... }
185     static Result MakeForBlender(SkString sksl, const Options&);
MakeForBlender(SkString sksl)186     static Result MakeForBlender(SkString sksl) {
187         return MakeForBlender(std::move(sksl), Options{});
188     }
189 
190     // Object that allows passing a SkShader, SkColorFilter or SkBlender as a child
191     class SK_API ChildPtr {
192     public:
193         ChildPtr() = default;
ChildPtr(sk_sp<SkShader> s)194         ChildPtr(sk_sp<SkShader> s) : fChild(std::move(s)) {}
ChildPtr(sk_sp<SkColorFilter> cf)195         ChildPtr(sk_sp<SkColorFilter> cf) : fChild(std::move(cf)) {}
ChildPtr(sk_sp<SkBlender> b)196         ChildPtr(sk_sp<SkBlender> b) : fChild(std::move(b)) {}
197 
198         // Asserts that the flattenable is either null, or one of the legal derived types
199         ChildPtr(sk_sp<SkFlattenable> f);
200 
201         std::optional<ChildType> type() const;
202 
203         SkShader* shader() const;
204         SkColorFilter* colorFilter() const;
205         SkBlender* blender() const;
flattenable()206         SkFlattenable* flattenable() const { return fChild.get(); }
207 
208         using sk_is_trivially_relocatable = std::true_type;
209 
210     private:
211         sk_sp<SkFlattenable> fChild;
212 
213         static_assert(::sk_is_trivially_relocatable<decltype(fChild)>::value);
214     };
215 
216     sk_sp<SkShader> makeShader(sk_sp<const SkData> uniforms,
217                                sk_sp<SkShader> children[],
218                                size_t childCount,
219                                const SkMatrix* localMatrix = nullptr) const;
220     sk_sp<SkShader> makeShader(sk_sp<const SkData> uniforms,
221                                SkSpan<const ChildPtr> children,
222                                const SkMatrix* localMatrix = nullptr) const;
223 
224     sk_sp<SkColorFilter> makeColorFilter(sk_sp<const SkData> uniforms) const;
225     sk_sp<SkColorFilter> makeColorFilter(sk_sp<const SkData> uniforms,
226                                          sk_sp<SkColorFilter> children[],
227                                          size_t childCount) const;
228     sk_sp<SkColorFilter> makeColorFilter(sk_sp<const SkData> uniforms,
229                                          SkSpan<const ChildPtr> children) const;
230 
231     sk_sp<SkBlender> makeBlender(sk_sp<const SkData> uniforms,
232                                  SkSpan<const ChildPtr> children = {}) const;
233 
234     /**
235      * Creates a new Runtime Effect patterned after an already-existing one. The new shader behaves
236      * like the original, but also creates a debug trace of its execution at the requested
237      * coordinate. After painting with this shader, the associated DebugTrace object will contain a
238      * shader execution trace. Call `writeTrace` on the debug trace object to generate a full trace
239      * suitable for a debugger, or call `dump` to emit a human-readable trace.
240      *
241      * Debug traces are only supported on a raster (non-GPU) canvas.
242 
243      * Debug traces are currently only supported on shaders. Color filter and blender tracing is a
244      * work-in-progress.
245      */
246     struct TracedShader {
247         sk_sp<SkShader> shader;
248         sk_sp<SkSL::DebugTrace> debugTrace;
249     };
250     static TracedShader MakeTraced(sk_sp<SkShader> shader, const SkIPoint& traceCoord);
251 
252     // Returns the SkSL source of the runtime effect shader.
253     const std::string& source() const;
254 
255     // Combined size of all 'uniform' variables. When calling makeColorFilter or makeShader,
256     // provide an SkData of this size, containing values for all of those variables.
257     size_t uniformSize() const;
258 
uniforms()259     SkSpan<const Uniform> uniforms() const { return SkSpan(fUniforms); }
children()260     SkSpan<const Child> children() const { return SkSpan(fChildren); }
261 
262     // Returns pointer to the named uniform variable's description, or nullptr if not found
263     const Uniform* findUniform(std::string_view name) const;
264 
265     // Returns pointer to the named child's description, or nullptr if not found
266     const Child* findChild(std::string_view name) const;
267 
268     // Allows the runtime effect type to be identified.
allowShader()269     bool allowShader()        const { return (fFlags & kAllowShader_Flag);        }
allowColorFilter()270     bool allowColorFilter()   const { return (fFlags & kAllowColorFilter_Flag);   }
allowBlender()271     bool allowBlender()       const { return (fFlags & kAllowBlender_Flag);       }
272 
273     // Advanced Filter: get AF state
274     bool getAF() const;
275 
276     static void RegisterFlattenables();
277     ~SkRuntimeEffect() override;
278 
279 private:
280     enum Flags {
281         kUsesSampleCoords_Flag    = 0x001,
282         kAllowColorFilter_Flag    = 0x002,
283         kAllowShader_Flag         = 0x004,
284         kAllowBlender_Flag        = 0x008,
285         kSamplesOutsideMain_Flag  = 0x010,
286         kUsesColorTransform_Flag  = 0x020,
287         kAlwaysOpaque_Flag        = 0x040,
288         kAlphaUnchanged_Flag      = 0x080,
289         kDisableOptimization_Flag = 0x100,
290     };
291 
292     SkRuntimeEffect(std::unique_ptr<SkSL::Program> baseProgram,
293                     const Options& options,
294                     const SkSL::FunctionDefinition& main,
295                     std::vector<Uniform>&& uniforms,
296                     std::vector<Child>&& children,
297                     std::vector<SkSL::SampleUsage>&& sampleUsages,
298                     uint32_t flags);
299 
300     sk_sp<SkRuntimeEffect> makeUnoptimizedClone();
301 
302     static Result MakeFromSource(SkString sksl, const Options& options, SkSL::ProgramKind kind);
303 
304     static Result MakeInternal(std::unique_ptr<SkSL::Program> program,
305                                const Options& options,
306                                SkSL::ProgramKind kind);
307 
308     static SkSL::ProgramSettings MakeSettings(const Options& options);
309 
hash()310     uint32_t hash() const { return fHash; }
usesSampleCoords()311     bool usesSampleCoords()   const { return (fFlags & kUsesSampleCoords_Flag);   }
samplesOutsideMain()312     bool samplesOutsideMain() const { return (fFlags & kSamplesOutsideMain_Flag); }
usesColorTransform()313     bool usesColorTransform() const { return (fFlags & kUsesColorTransform_Flag); }
alwaysOpaque()314     bool alwaysOpaque()       const { return (fFlags & kAlwaysOpaque_Flag);       }
isAlphaUnchanged()315     bool isAlphaUnchanged()   const { return (fFlags & kAlphaUnchanged_Flag);     }
316 
317     const SkSL::RP::Program* getRPProgram(SkSL::DebugTracePriv* debugTrace) const;
318 
319     friend class GrSkSLFP;              // usesColorTransform
320     friend class SkRuntimeShader;       // fBaseProgram, fMain, fSampleUsages, getRPProgram()
321     friend class SkRuntimeBlender;      //
322     friend class SkRuntimeColorFilter;  //
323 
324     friend class SkRuntimeEffectPriv;
325 
326     uint32_t fHash;
327     uint32_t fStableKey;
328 
329     std::unique_ptr<SkSL::Program> fBaseProgram;
330     std::unique_ptr<SkSL::RP::Program> fRPProgram;
331     mutable SkOnce fCompileRPProgramOnce;
332     const SkSL::FunctionDefinition& fMain;
333     std::vector<Uniform> fUniforms;
334     std::vector<Child> fChildren;
335     std::vector<SkSL::SampleUsage> fSampleUsages;
336 
337     uint32_t fFlags;  // Flags
338 };
339 
340 /**
341  * SkRuntimeEffectBuilder is a utility to simplify creating SkShader, SkColorFilter, and SkBlender
342  * objects from SkRuntimeEffects.
343  *
344  * NOTE: Like SkRuntimeEffect, this API is experimental and subject to change!
345  *
346  * Given an SkRuntimeEffect, the SkRuntimeEffectBuilder manages creating an input data block and
347  * provides named access to the 'uniform' variables in that block, as well as named access
348  * to a list of child shader slots. Usage:
349  *
350  *   sk_sp<SkRuntimeEffect> effect = ...;
351  *   SkRuntimeEffectBuilder builder(effect);
352  *   builder.uniform("some_uniform_float")  = 3.14f;
353  *   builder.uniform("some_uniform_matrix") = SkM44::Rotate(...);
354  *   builder.child("some_child_effect")     = mySkImage->makeShader(...);
355  *   ...
356  *   sk_sp<SkShader> shader = builder.makeShader(nullptr, false);
357  *
358  * Upon calling makeShader, makeColorFilter, or makeBlender, the builder will check the validity
359  * of the SkSL to see if the entry point is correct.
360  *
361  * Note that SkRuntimeEffectBuilder is built entirely on the public API of SkRuntimeEffect,
362  * so can be used as-is or serve as inspiration for other interfaces or binding techniques.
363  */
364 class SK_API SkRuntimeEffectBuilder {
365 public:
SkRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect)366     explicit SkRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect)
367             : fEffect(std::move(effect))
368             , fUniforms(SkData::MakeZeroInitialized(fEffect->uniformSize()))
369             , fChildren(fEffect->children().size()) {}
SkRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect,sk_sp<SkData> uniforms)370     explicit SkRuntimeEffectBuilder(sk_sp<SkRuntimeEffect> effect, sk_sp<SkData> uniforms)
371             : fEffect(std::move(effect))
372             , fUniforms(std::move(uniforms))
373             , fChildren(fEffect->children().size()) {}
374 
375     // This is currently required by Android Framework but may go away if that dependency
376     // can be removed.
377     SkRuntimeEffectBuilder(const SkRuntimeEffectBuilder&) = default;
378 
379     struct BuilderUniform {
380         // Copy 'val' to this variable. No type conversion is performed - 'val' must be same
381         // size as expected by the effect. Information about the variable can be queried by
382         // looking at fVar. If the size is incorrect, no copy will be performed, and debug
383         // builds will abort. If this is the result of querying a missing variable, fVar will
384         // be nullptr, and assigning will also do nothing (and abort in debug builds).
385         template <typename T>
386         std::enable_if_t<std::is_trivially_copyable<T>::value, BuilderUniform&> operator=(
387                 const T& val) {
388             if (!fVar) {
389                 SkDEBUGFAIL("Assigning to missing variable");
390             } else if (sizeof(val) != fVar->sizeInBytes()) {
391                 SkDEBUGFAIL("Incorrect value size");
392             } else {
393                 memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset),
394                        &val, sizeof(val));
395             }
396             return *this;
397         }
398 
399         BuilderUniform& operator=(const SkMatrix& val) {
400             if (!fVar) {
401                 SkDEBUGFAIL("Assigning to missing variable");
402             } else if (fVar->sizeInBytes() != 9 * sizeof(float)) {
403                 SkDEBUGFAIL("Incorrect value size");
404             } else {
405                 float* data = SkTAddOffset<float>(fOwner->writableUniformData(),
406                                                   (ptrdiff_t)fVar->offset);
407                 data[0] = val.get(0); data[1] = val.get(3); data[2] = val.get(6);
408                 data[3] = val.get(1); data[4] = val.get(4); data[5] = val.get(7);
409                 data[6] = val.get(2); data[7] = val.get(5); data[8] = val.get(8);
410             }
411             return *this;
412         }
413 
414         template <typename T>
setBuilderUniform415         bool set(const T val[], const int count) {
416             static_assert(std::is_trivially_copyable<T>::value, "Value must be trivial copyable");
417             if (!fVar) {
418                 SkDEBUGFAIL("Assigning to missing variable");
419                 return false;
420             } else if (sizeof(T) * count != fVar->sizeInBytes()) {
421                 SkDEBUGFAIL("Incorrect value size");
422                 return false;
423             } else {
424                 memcpy(SkTAddOffset<void>(fOwner->writableUniformData(), fVar->offset),
425                        val, sizeof(T) * count);
426             }
427             return true;
428         }
429 
430         SkRuntimeEffectBuilder*         fOwner;
431         const SkRuntimeEffect::Uniform* fVar;    // nullptr if the variable was not found
432     };
433 
434     struct BuilderChild {
435         template <typename T> BuilderChild& operator=(sk_sp<T> val) {
436             if (!fChild) {
437                 SkDEBUGFAIL("Assigning to missing child");
438             } else {
439                 fOwner->fChildren[(size_t)fChild->index] = std::move(val);
440             }
441             return *this;
442         }
443 
444         BuilderChild& operator=(std::nullptr_t) {
445             if (!fChild) {
446                 SkDEBUGFAIL("Assigning to missing child");
447             } else {
448                 fOwner->fChildren[(size_t)fChild->index] = SkRuntimeEffect::ChildPtr{};
449             }
450             return *this;
451         }
452 
453         SkRuntimeEffectBuilder*       fOwner;
454         const SkRuntimeEffect::Child* fChild;  // nullptr if the child was not found
455     };
456 
effect()457     const SkRuntimeEffect* effect() const { return fEffect.get(); }
458 
uniform(std::string_view name)459     BuilderUniform uniform(std::string_view name) { return { this, fEffect->findUniform(name) }; }
child(std::string_view name)460     BuilderChild child(std::string_view name) { return { this, fEffect->findChild(name) }; }
461 
462     // Get access to the collated uniforms and children (in the order expected by APIs like
463     // makeShader on the effect):
uniforms()464     sk_sp<const SkData> uniforms() const { return fUniforms; }
children()465     SkSpan<const SkRuntimeEffect::ChildPtr> children() const { return fChildren; }
466 
467     // Build methods, at this point checks are made to ensure the SkSL entry point `main` is correct
468     sk_sp<SkShader> makeShader(const SkMatrix* localMatrix = nullptr) const;
469     sk_sp<SkColorFilter> makeColorFilter() const;
470     sk_sp<SkBlender> makeBlender() const;
471 
472     ~SkRuntimeEffectBuilder() = default;
473 
474 protected:
475     SkRuntimeEffectBuilder() = delete;
476 
477     SkRuntimeEffectBuilder(SkRuntimeEffectBuilder&&) = default;
478 
479     SkRuntimeEffectBuilder& operator=(SkRuntimeEffectBuilder&&) = delete;
480     SkRuntimeEffectBuilder& operator=(const SkRuntimeEffectBuilder&) = delete;
481 
482 private:
writableUniformData()483     void* writableUniformData() {
484         if (!fUniforms->unique()) {
485             fUniforms = SkData::MakeWithCopy(fUniforms->data(), fUniforms->size());
486         }
487         return fUniforms->writable_data();
488     }
489 
490     sk_sp<SkRuntimeEffect>                 fEffect;
491     sk_sp<SkData>                          fUniforms;
492     std::vector<SkRuntimeEffect::ChildPtr> fChildren;
493 
494     friend class SkRuntimeImageFilter;
495 };
496 
497 /**
498  * DEPRECATED: Subclass logic has been moved to base class SkRuntimeEffectBuilder.
499  */
500 using SkRuntimeShaderBuilder = SkRuntimeEffectBuilder;
501 using SkRuntimeColorFilterBuilder = SkRuntimeEffectBuilder;
502 using SkRuntimeBlendBuilder = SkRuntimeEffectBuilder;
503 
504 #endif  // SkRuntimeEffect_DEFINED
505