Flutter Engine
The Flutter Engine
SkRuntimeEffectPriv.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 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 SkRuntimeEffectPriv_DEFINED
9#define SkRuntimeEffectPriv_DEFINED
10
22
23#include <cstddef>
24#include <cstdint>
25#include <functional>
26#include <memory>
27
29
30class SkArenaAlloc;
31class SkCapabilities;
32class SkColorSpace;
33class SkData;
34class SkMatrix;
35class SkReadBuffer;
36class SkShader;
37class SkWriteBuffer;
39
40namespace SkShaders {
41class MatrixRec;
42}
43
44namespace SkSL {
45class Context;
46class Variable;
47struct Program;
48}
49
51public:
54 };
55
56 // Private (experimental) API for creating runtime shaders with late-bound uniforms.
57 // The callback must produce a uniform data blob of the correct size for the effect.
58 // It is invoked at "draw" time (essentially, when a draw call is made against the canvas
59 // using the resulting shader). There are no strong guarantees about timing.
60 // Serializing the resulting shader will immediately invoke the callback (and record the
61 // resulting uniforms).
62 using UniformsCallback = std::function<sk_sp<const SkData>(const UniformsCallbackContext&)>;
64 UniformsCallback uniformsCallback,
66 const SkMatrix* localMatrix = nullptr);
67
68 // Helper function when creating an effect for a GrSkSLFP that verifies an effect will
69 // implement the GrFragmentProcessor "constant output for constant input" optimization flag.
71 // This optimization is only implemented for color filters without any children.
72 if (!effect->allowColorFilter() || !effect->children().empty()) {
73 return false;
74 }
75 return true;
76 }
77
78 static uint32_t Hash(const SkRuntimeEffect& effect) {
79 return effect.hash();
80 }
81
82 static uint32_t StableKey(const SkRuntimeEffect& effect) {
83 return effect.fStableKey;
84 }
85
86 static const SkSL::Program& Program(const SkRuntimeEffect& effect) {
87 return *effect.fBaseProgram;
88 }
89
92 options.maxVersionAllowed = SkSL::Version::k300;
93 return options;
94 }
95
97 options->allowPrivateAccess = true;
98 }
99
100 static void SetStableKey(SkRuntimeEffect::Options* options, uint32_t stableKey) {
101 options->fStableKey = stableKey;
102 }
103
105 const SkSL::Context&,
106 size_t* offset);
107
109 int index);
110
112
113 // If there are layout(color) uniforms then this performs color space transformation on the
114 // color values and returns a new SkData. Otherwise, the original data is returned.
116 sk_sp<const SkData> originalData,
119 sk_sp<const SkData> originalData,
120 const SkColorSpace* dstCS);
123 sk_sp<const SkData> originalData,
124 bool alwaysCopyIntoAlloc,
125 const SkColorSpace* destColorSpace,
126 SkArenaAlloc* alloc);
127
128 static bool CanDraw(const SkCapabilities*, const SkSL::Program*);
129 static bool CanDraw(const SkCapabilities*, const SkRuntimeEffect*);
130
132 const SkRuntimeEffect* effect,
136
137 static bool UsesColorTransform(const SkRuntimeEffect* effect) {
138 return effect->usesColorTransform();
139 }
140};
141
142// These internal APIs for creating runtime effects vary from the public API in two ways:
143//
144// 1) they're used in contexts where it's not useful to receive an error message;
145// 2) they're cached.
146//
147// Users of the public SkRuntimeEffect::Make*() can of course cache however they like themselves;
148// keeping these APIs private means users will not be forced into our cache or cache policy.
149
152 SkString sksl);
153
156 const char* sksl) {
158}
159
160// Internal API that assumes (and asserts) that the shader code is valid, but does no internal
161// caching. Used when the caller will cache the result in a static variable. Ownership is passed to
162// the caller; the effect will be leaked if it the pointer is not stored or explicitly deleted.
165 const char* sksl,
167#if defined(SK_DEBUG)
168 // Our SKSL snippets we embed in Skia should not have comments or excess indentation.
169 // Removing them helps trim down code size and speeds up parsing
170 if (SkStrContains(sksl, "//") || SkStrContains(sksl, " ")) {
171 SkDEBUGFAILF("Found SkSL snippet that can be minified: \n %s\n", sksl);
172 }
173#endif
175 auto result = make(SkString{sksl}, options);
176 if (!result.effect) {
177 SK_ABORT("%s", result.errorText.c_str());
178 }
179 return result.effect.release();
180}
181
183public:
184 // SkStageRec::fPaintColor is used (strictly) to tint alpha-only image shaders with the paint
185 // color. We want to suppress that behavior when they're sampled from runtime effects, so we
186 // just override the paint color here. See also: SkImageShader::appendStages.
188 const SkShaders::MatrixRec& m,
191 : fStage{s.fPipeline,
192 s.fAlloc,
193 s.fDstColorType,
194 s.fDstCS,
196 s.fSurfaceProps}
197 , fMatrix(m)
198 , fChildren(c)
199 , fSampleUsages(u) {}
200
201 bool appendShader(int index) override;
202 bool appendColorFilter(int index) override;
203 bool appendBlender(int index) override;
204
205 // TODO: If an effect calls these intrinsics more than once, we could cache and re-use the steps
206 // object(s), rather than re-creating them in the arena repeatedly.
207 void toLinearSrgb(const void* color) override;
208
209 void fromLinearSrgb(const void* color) override;
210
211private:
212 void applyColorSpaceXform(const SkColorSpaceXformSteps& tempXform, const void* color);
213
214 const SkStageRec fStage;
215 const SkShaders::MatrixRec& fMatrix;
218};
219
220#endif // SkRuntimeEffectPriv_DEFINED
const char * options
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
#define SkDEBUGFAILF(fmt,...)
Definition: SkAssert.h:119
SkRuntimeEffect * SkMakeRuntimeEffect(SkRuntimeEffect::Result(*make)(SkString, const SkRuntimeEffect::Options &), const char *sksl, SkRuntimeEffect::Options options=SkRuntimeEffect::Options{})
sk_sp< SkRuntimeEffect > SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result(*make)(SkString sksl, const SkRuntimeEffect::Options &), SkString sksl)
static bool SkStrContains(const char string[], const char substring[])
Definition: SkString.h:53
GLenum type
void toLinearSrgb(const void *color) override
bool appendColorFilter(int index) override
bool appendBlender(int index) override
void fromLinearSrgb(const void *color) override
RuntimeEffectRPCallbacks(const SkStageRec &s, const SkShaders::MatrixRec &m, SkSpan< const SkRuntimeEffect::ChildPtr > c, SkSpan< const SkSL::SampleUsage > u)
bool appendShader(int index) override
Definition: SkData.h:25
static bool ReadChildEffects(SkReadBuffer &buffer, const SkRuntimeEffect *effect, skia_private::TArray< SkRuntimeEffect::ChildPtr > *children)
static sk_sp< SkShader > MakeDeferredShader(const SkRuntimeEffect *effect, UniformsCallback uniformsCallback, SkSpan< const SkRuntimeEffect::ChildPtr > children, const SkMatrix *localMatrix=nullptr)
static const char * ChildTypeToStr(SkRuntimeEffect::ChildType type)
static uint32_t StableKey(const SkRuntimeEffect &effect)
std::function< sk_sp< const SkData >(const UniformsCallbackContext &)> UniformsCallback
static SkSpan< const float > UniformsAsSpan(SkSpan< const SkRuntimeEffect::Uniform > uniforms, sk_sp< const SkData > originalData, bool alwaysCopyIntoAlloc, const SkColorSpace *destColorSpace, SkArenaAlloc *alloc)
static uint32_t Hash(const SkRuntimeEffect &effect)
static void AllowPrivateAccess(SkRuntimeEffect::Options *options)
static SkRuntimeEffect::Uniform VarAsUniform(const SkSL::Variable &, const SkSL::Context &, size_t *offset)
static sk_sp< const SkData > TransformUniforms(SkSpan< const SkRuntimeEffect::Uniform > uniforms, sk_sp< const SkData > originalData, const SkColorSpaceXformSteps &)
static bool SupportsConstantOutputForConstantInput(const SkRuntimeEffect *effect)
static void SetStableKey(SkRuntimeEffect::Options *options, uint32_t stableKey)
static const SkSL::Program & Program(const SkRuntimeEffect &effect)
static bool CanDraw(const SkCapabilities *, const SkSL::Program *)
static void WriteChildEffects(SkWriteBuffer &buffer, SkSpan< const SkRuntimeEffect::ChildPtr > children)
static bool UsesColorTransform(const SkRuntimeEffect *effect)
static SkRuntimeEffect::Child VarAsChild(const SkSL::Variable &var, int index)
static SkRuntimeEffect::Options ES3Options()
SkSpan< const Child > children() const
bool allowColorFilter() const
DlColor color
struct MyStruct s
GAsyncResult * result
constexpr SkColor4f kTransparent
Definition: SkColor.h:434
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
static void make(SkBitmap *bitmap, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace)
Definition: encode_srgb.cpp:35
SeparatedVector2 offset