Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
21
22#include <cstddef>
23#include <cstdint>
24#include <functional>
25#include <memory>
26
28
29class SkArenaAlloc;
30class SkCapabilities;
31class SkColorSpace;
32class SkData;
33class SkMatrix;
34class SkReadBuffer;
35class SkShader;
36class SkWriteBuffer;
38
39namespace SkShaders {
40class MatrixRec;
41}
42
43namespace SkSL {
44class Context;
45class Variable;
46struct Program;
47}
48
50public:
54
55 // Private (experimental) API for creating runtime shaders with late-bound uniforms.
56 // The callback must produce a uniform data blob of the correct size for the effect.
57 // It is invoked at "draw" time (essentially, when a draw call is made against the canvas
58 // using the resulting shader). There are no strong guarantees about timing.
59 // Serializing the resulting shader will immediately invoke the callback (and record the
60 // resulting uniforms).
61 using UniformsCallback = std::function<sk_sp<const SkData>(const UniformsCallbackContext&)>;
63 UniformsCallback uniformsCallback,
65 const SkMatrix* localMatrix = nullptr);
66
67 // Helper function when creating an effect for a GrSkSLFP that verifies an effect will
68 // implement the GrFragmentProcessor "constant output for constant input" optimization flag.
70 // This optimization is only implemented for color filters without any children.
71 if (!effect->allowColorFilter() || !effect->children().empty()) {
72 return false;
73 }
74 return true;
75 }
76
77 static uint32_t Hash(const SkRuntimeEffect& effect) {
78 return effect.hash();
79 }
80
81 static uint32_t StableKey(const SkRuntimeEffect& effect) {
82 return effect.fStableKey;
83 }
84
85 static const SkSL::Program& Program(const SkRuntimeEffect& effect) {
86 return *effect.fBaseProgram;
87 }
88
94
96 options->allowPrivateAccess = true;
97 }
98
99 static void SetStableKey(SkRuntimeEffect::Options* options, uint32_t stableKey) {
100 options->fStableKey = stableKey;
101 }
102
104 const SkSL::Context&,
105 size_t* offset);
106
108 int index);
109
111
112 // If there are layout(color) uniforms then this performs color space transformation on the
113 // color values and returns a new SkData. Otherwise, the original data is returned.
115 sk_sp<const SkData> originalData,
118 sk_sp<const SkData> originalData,
119 const SkColorSpace* dstCS);
122 sk_sp<const SkData> originalData,
123 bool alwaysCopyIntoAlloc,
124 const SkColorSpace* destColorSpace,
125 SkArenaAlloc* alloc);
126
127 static bool CanDraw(const SkCapabilities*, const SkSL::Program*);
128 static bool CanDraw(const SkCapabilities*, const SkRuntimeEffect*);
129
131 const SkRuntimeEffect* effect,
135
136 static bool UsesColorTransform(const SkRuntimeEffect* effect) {
137 return effect->usesColorTransform();
138 }
139};
140
141// These internal APIs for creating runtime effects vary from the public API in two ways:
142//
143// 1) they're used in contexts where it's not useful to receive an error message;
144// 2) they're cached.
145//
146// Users of the public SkRuntimeEffect::Make*() can of course cache however they like themselves;
147// keeping these APIs private means users will not be forced into our cache or cache policy.
148
151 SkString sksl);
152
158
159// Internal API that assumes (and asserts) that the shader code is valid, but does no internal
160// caching. Used when the caller will cache the result in a static variable. Ownership is passed to
161// the caller; the effect will be leaked if it the pointer is not stored or explicitly deleted.
164 const char* sksl,
166#if defined(SK_DEBUG)
167 // Our SKSL snippets we embed in Skia should not have comments or excess indentation.
168 // Removing them helps trim down code size and speeds up parsing
169 if (SkStrContains(sksl, "//") || SkStrContains(sksl, " ")) {
170 SkDEBUGFAILF("Found SkSL snippet that can be minified: \n %s\n", sksl);
171 }
172#endif
174 auto result = make(SkString{sksl}, options);
175 if (!result.effect) {
176 SK_ABORT("%s", result.errorText.c_str());
177 }
178 return result.effect.release();
179}
180
182public:
183 // SkStageRec::fPaintColor is used (strictly) to tint alpha-only image shaders with the paint
184 // color. We want to suppress that behavior when they're sampled from runtime effects, so we
185 // just override the paint color here. See also: SkImageShader::appendStages.
187 const SkShaders::MatrixRec& m,
190 : fStage{s.fPipeline,
191 s.fAlloc,
192 s.fDstColorType,
193 s.fDstCS,
194 SkColors::kTransparent,
195 s.fSurfaceProps}
196 , fMatrix(m)
197 , fChildren(c)
198 , fSampleUsages(u) {}
199
200 bool appendShader(int index) override;
201 bool appendColorFilter(int index) override;
202 bool appendBlender(int index) override;
203
204 // TODO: If an effect calls these intrinsics more than once, we could cache and re-use the steps
205 // object(s), rather than re-creating them in the arena repeatedly.
206 void toLinearSrgb(const void* color) override;
207
208 void fromLinearSrgb(const void* color) override;
209
210private:
211 void applyColorSpaceXform(const SkColorSpaceXformSteps& tempXform, const void* color);
212
213 const SkStageRec fStage;
214 const SkShaders::MatrixRec& fMatrix;
217};
218
219#endif // SkRuntimeEffectPriv_DEFINED
const char * options
SkColor4f color
#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
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
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
struct MyStruct s
static const uint8_t buffer[]
GAsyncResult * result
static sk_sp< SkImage > make(sk_sp< SkColorSpace > cs)
Definition mipmap.cpp:65
Point offset