Debian Codegraph @

src/core/SkRuntimeEffectPriv.h

raw
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
11 #include "include/core/SkColor.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkString.h"
14 #include "include/effects/SkRuntimeEffect.h"
15 #include "include/private/SkSLSampleUsage.h"
16 #include "include/private/base/SkAssert.h"
17 #include "include/private/base/SkDebug.h"
18 #include "include/private/base/SkSpan_impl.h"
19 #include "include/private/base/SkTArray.h"
20 #include "src/core/SkEffectPriv.h"
21 #include "src/core/SkKnownRuntimeEffects.h"
22 #include "src/sksl/codegen/SkSLRasterPipelineBuilder.h"
23
24 #include <cstddef>
25 #include <cstdint>
26 #include <functional>
27 #include <memory>
28 #include <vector>
29
30 #include "include/sksl/SkSLVersion.h"
31
32 class SkArenaAlloc;
33 class SkCapabilities;
34 class SkColorSpace;
35 class SkData;
36 class SkMatrix;
37 class SkReadBuffer;
38 class SkShader;
39 class SkWriteBuffer;
40 struct SkColorSpaceXformSteps;
41
42 namespace SkShaders {
43 class MatrixRec;
44 }
45
46 namespace SkSL {
47 class Context;
48 class Variable;
49 struct Program;
50 }
51
52 class SkRuntimeEffectPriv {
53 public:
54 struct UniformsCallbackContext {
55 const SkColorSpace* fDstColorSpace;
56 };
57
58 // Private (experimental) API for creating runtime shaders with late-bound uniforms.
59 // The callback must produce a uniform data blob of the correct size for the effect.
60 // It is invoked at "draw" time (essentially, when a draw call is made against the canvas
61 // using the resulting shader). There are no strong guarantees about timing.
62 // Serializing the resulting shader will immediately invoke the callback (and record the
63 // resulting uniforms).
64 using UniformsCallback = std::function<sk_sp<const SkData>(const UniformsCallbackContext&)>;
65 static sk_sp<SkShader> MakeDeferredShader(const SkRuntimeEffect* effect,
66 UniformsCallback uniformsCallback,
67 SkSpan<const SkRuntimeEffect::ChildPtr> children,
68 const SkMatrix* localMatrix = nullptr);
69
70 // Helper function when creating an effect for a GrSkSLFP that verifies an effect will
71 // implement the GrFragmentProcessor "constant output for constant input" optimization flag.
72 static bool SupportsConstantOutputForConstantInput(const SkRuntimeEffect* effect) {
73 // This optimization is only implemented for color filters without any children.
74 if (!effect->allowColorFilter() || !effect->children().empty()) {
75 return false;
76 }
77 return true;
78 }
79
80 static uint32_t Hash(const SkRuntimeEffect& effect) {
81 return effect.hash();
82 }
83
84 static bool HasName(const SkRuntimeEffect& effect) {
85 return !effect.fName.isEmpty();
86 }
87
88 static const char* GetName(const SkRuntimeEffect& effect) {
89 return effect.fName.c_str();
90 }
91
92 static uint32_t StableKey(const SkRuntimeEffect& effect) {
93 return effect.fStableKey;
94 }
95
96 // This method is only used on user-defined known runtime effects
97 static void SetStableKey(SkRuntimeEffect* effect, uint32_t stableKey) {
98 SkASSERT(!effect->fStableKey);
99 SkASSERT(SkKnownRuntimeEffects::IsViableUserDefinedKnownRuntimeEffect(stableKey));
100 effect->fStableKey = stableKey;
101 }
102
103 // This method is only used for Skia-internal known runtime effects
104 static void SetStableKeyOnOptions(SkRuntimeEffect::Options* options, uint32_t stableKey) {
105 SkASSERT(!options->fStableKey);
106 SkASSERT(SkKnownRuntimeEffects::IsSkiaKnownRuntimeEffect(stableKey));
107 options->fStableKey = stableKey;
108 }
109
110 static void ResetStableKey(SkRuntimeEffect* effect) {
111 effect->fStableKey = 0;
112 }
113
114 static const SkSL::Program& Program(const SkRuntimeEffect& effect) {
115 return *effect.fBaseProgram;
116 }
117
118 static SkRuntimeEffect::Options ES3Options() {
119 SkRuntimeEffect::Options options;
120 options.maxVersionAllowed = SkSL::Version::k300;
121 return options;
122 }
123
124 static void AllowPrivateAccess(SkRuntimeEffect::Options* options) {
125 options->allowPrivateAccess = true;
126 }
127
128 static SkRuntimeEffect::Uniform VarAsUniform(const SkSL::Variable&,
129 const SkSL::Context&,
130 size_t* offset);
131
132 static SkRuntimeEffect::Child VarAsChild(const SkSL::Variable& var,
133 int index);
134
135 static const char* ChildTypeToStr(SkRuntimeEffect::ChildType type);
136
137 // If there are layout(color) uniforms then this performs color space transformation on the
138 // color values and returns a new SkData. Otherwise, the original data is returned.
139 static sk_sp<const SkData> TransformUniforms(SkSpan<const SkRuntimeEffect::Uniform> uniforms,
140 sk_sp<const SkData> originalData,
141 const SkColorSpaceXformSteps&);
142 static sk_sp<const SkData> TransformUniforms(SkSpan<const SkRuntimeEffect::Uniform> uniforms,
143 sk_sp<const SkData> originalData,
144 const SkColorSpace* dstCS);
145 static SkSpan<const float> UniformsAsSpan(
146 SkSpan<const SkRuntimeEffect::Uniform> uniforms,
147 sk_sp<const SkData> originalData,
148 bool alwaysCopyIntoAlloc,
149 const SkColorSpace* destColorSpace,
150 SkArenaAlloc* alloc);
151
152 static bool CanDraw(const SkCapabilities*, const SkSL::Program*);
153 static bool CanDraw(const SkCapabilities*, const SkRuntimeEffect*);
154
155 static bool ReadChildEffects(SkReadBuffer& buffer,
156 const SkRuntimeEffect* effect,
157 skia_private::TArray<SkRuntimeEffect::ChildPtr>* children);
158 static void WriteChildEffects(SkWriteBuffer& buffer,
159 SkSpan<const SkRuntimeEffect::ChildPtr> children);
160
161 static bool UsesColorTransform(const SkRuntimeEffect* effect) {
162 return effect->usesColorTransform();
163 }
164 static SkSL::SampleUsage ChildSampleUsage(const SkRuntimeEffect* effect, int child) {
165 return effect->fSampleUsages[child];
166 }
167 };
168
169 // These internal APIs for creating runtime effects vary from the public API in two ways:
170 //
171 // 1) they're used in contexts where it's not useful to receive an error message;
172 // 2) they're cached.
173 //
174 // Users of the public SkRuntimeEffect::Make*() can of course cache however they like themselves;
175 // keeping these APIs private means users will not be forced into our cache or cache policy.
176
177 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(
178 SkRuntimeEffect::Result (*make)(SkString sksl, const SkRuntimeEffect::Options&),
179 SkString sksl);
180
181 inline sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(
182 SkRuntimeEffect::Result (*make)(SkString, const SkRuntimeEffect::Options&),
183 const char* sksl) {
184 return SkMakeCachedRuntimeEffect(make, SkString{sksl});
185 }
186
187 // Internal API that assumes (and asserts) that the shader code is valid, but does no internal
188 // caching. Used when the caller will cache the result in a static variable. Ownership is passed to
189 // the caller; the effect will be leaked if the pointer is not stored or explicitly deleted.
190 inline SkRuntimeEffect* SkMakeRuntimeEffect(
191 SkRuntimeEffect::Result (*make)(SkString, const SkRuntimeEffect::Options&),
192 const char* sksl,
193 SkRuntimeEffect::Options options = SkRuntimeEffect::Options{}) {
194 #if defined(SK_DEBUG)
195 // Our SKSL snippets we embed in Skia should not have comments or excess indentation.
196 // Removing them helps trim down code size and speeds up parsing
197 if (SkStrContains(sksl, "//") || SkStrContains(sksl, " ")) {
198 SkDEBUGFAILF("Found SkSL snippet that can be minified: \n %s\n", sksl);
199 }
200 #endif
201 SkRuntimeEffectPriv::AllowPrivateAccess(&options);
202 auto result = make(SkString{sksl}, options);
203 if (!result.effect) {
204 SK_ABORT("%s", result.errorText.c_str());
205 }
206 return result.effect.release();
207 }
208
209 class RuntimeEffectRPCallbacks : public SkSL::RP::Callbacks {
210 public:
211 // SkStageRec::fPaintColor is used (strictly) to tint alpha-only image shaders with the paint
212 // color. We want to suppress that behavior when they're sampled from runtime effects, so we
213 // just override the paint color here. See also: SkImageShader::appendStages.
214 RuntimeEffectRPCallbacks(const SkStageRec& s,
215 const SkShaders::MatrixRec& m,
216 SkSpan<const SkRuntimeEffect::ChildPtr> c,
217 SkSpan<const SkSL::SampleUsage> u)
218 : fStage{s.fPipeline,
219 s.fAlloc,
220 s.fDstColorType,
221 s.fDstCS,
222 SkColors::kTransparent,
223 s.fSurfaceProps,
224 s.fDstBounds}
225 , fMatrix(m)
226 , fChildren(c)
227 , fSampleUsages(u) {}
228
229 bool appendShader(int index) override;
230 bool appendColorFilter(int index) override;
231 bool appendBlender(int index) override;
232
233 // TODO: If an effect calls these intrinsics more than once, we could cache and re-use the steps
234 // object(s), rather than re-creating them in the arena repeatedly.
235 void toLinearSrgb(const void* color) override;
236
237 void fromLinearSrgb(const void* color) override;
238
239 private:
240 void applyColorSpaceXform(const SkColorSpaceXformSteps& tempXform, const void* color);
241
242 const SkStageRec fStage;
243 const SkShaders::MatrixRec& fMatrix;
244 SkSpan<const SkRuntimeEffect::ChildPtr> fChildren;
245 SkSpan<const SkSL::SampleUsage> fSampleUsages;
246 };
247
248 #endif // SkRuntimeEffectPriv_DEFINED