• 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 #include "include/effects/SkRuntimeEffect.h"
9 
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkSurface.h"
13 #include "include/private/SkMutex.h"
14 #include "include/sksl/DSLCore.h"
15 #include "src/core/SkBlenderBase.h"
16 #include "src/core/SkCanvasPriv.h"
17 #include "src/core/SkColorFilterBase.h"
18 #include "src/core/SkColorSpacePriv.h"
19 #include "src/core/SkColorSpaceXformSteps.h"
20 #include "src/core/SkLRUCache.h"
21 #include "src/core/SkMatrixProvider.h"
22 #include "src/core/SkOpts.h"
23 #include "src/core/SkRasterPipeline.h"
24 #include "src/core/SkReadBuffer.h"
25 #include "src/core/SkRuntimeEffectPriv.h"
26 #include "src/core/SkUtils.h"
27 #include "src/core/SkVM.h"
28 #include "src/core/SkWriteBuffer.h"
29 #include "src/sksl/SkSLAnalysis.h"
30 #include "src/sksl/SkSLCompiler.h"
31 #include "src/sksl/SkSLSharedCompiler.h"
32 #include "src/sksl/SkSLUtil.h"
33 #include "src/sksl/codegen/SkSLVMCodeGenerator.h"
34 #include "src/sksl/ir/SkSLFunctionDefinition.h"
35 #include "src/sksl/ir/SkSLVarDeclarations.h"
36 #include "src/sksl/tracing/SkVMDebugTrace.h"
37 
38 #if SK_SUPPORT_GPU
39 #include "include/gpu/GrRecordingContext.h"
40 #include "src/gpu/GrColorInfo.h"
41 #include "src/gpu/GrFPArgs.h"
42 #include "src/gpu/GrImageInfo.h"
43 #include "src/gpu/GrRecordingContextPriv.h"
44 #include "src/gpu/SurfaceFillContext.h"
45 #include "src/gpu/effects/GrMatrixEffect.h"
46 #include "src/gpu/effects/GrSkSLFP.h"
47 #include "src/image/SkImage_Gpu.h"
48 #endif
49 
50 #include <algorithm>
51 
52 using ChildType = SkRuntimeEffect::ChildType;
53 
54 #ifdef SK_ENABLE_SKSL
55 
make_skvm_debug_trace(SkRuntimeEffect * effect,const SkIPoint & coord)56 static sk_sp<SkSL::SkVMDebugTrace> make_skvm_debug_trace(SkRuntimeEffect* effect,
57                                                          const SkIPoint& coord) {
58     auto debugTrace = sk_make_sp<SkSL::SkVMDebugTrace>();
59     debugTrace->setSource(effect->source());
60     debugTrace->setTraceCoord(coord);
61     return debugTrace;
62 }
63 
init_uniform_type(const SkSL::Context & ctx,const SkSL::Type * type,SkRuntimeEffect::Uniform * v)64 static bool init_uniform_type(const SkSL::Context& ctx,
65                               const SkSL::Type* type,
66                               SkRuntimeEffect::Uniform* v) {
67     using Type = SkRuntimeEffect::Uniform::Type;
68     if (type->matches(*ctx.fTypes.fFloat))    { v->type = Type::kFloat;    return true; }
69     if (type->matches(*ctx.fTypes.fHalf))     { v->type = Type::kFloat;    return true; }
70     if (type->matches(*ctx.fTypes.fFloat2))   { v->type = Type::kFloat2;   return true; }
71     if (type->matches(*ctx.fTypes.fHalf2))    { v->type = Type::kFloat2;   return true; }
72     if (type->matches(*ctx.fTypes.fFloat3))   { v->type = Type::kFloat3;   return true; }
73     if (type->matches(*ctx.fTypes.fHalf3))    { v->type = Type::kFloat3;   return true; }
74     if (type->matches(*ctx.fTypes.fFloat4))   { v->type = Type::kFloat4;   return true; }
75     if (type->matches(*ctx.fTypes.fHalf4))    { v->type = Type::kFloat4;   return true; }
76     if (type->matches(*ctx.fTypes.fFloat2x2)) { v->type = Type::kFloat2x2; return true; }
77     if (type->matches(*ctx.fTypes.fHalf2x2))  { v->type = Type::kFloat2x2; return true; }
78     if (type->matches(*ctx.fTypes.fFloat3x3)) { v->type = Type::kFloat3x3; return true; }
79     if (type->matches(*ctx.fTypes.fHalf3x3))  { v->type = Type::kFloat3x3; return true; }
80     if (type->matches(*ctx.fTypes.fFloat4x4)) { v->type = Type::kFloat4x4; return true; }
81     if (type->matches(*ctx.fTypes.fHalf4x4))  { v->type = Type::kFloat4x4; return true; }
82 
83     if (type->matches(*ctx.fTypes.fInt))  { v->type = Type::kInt;  return true; }
84     if (type->matches(*ctx.fTypes.fInt2)) { v->type = Type::kInt2; return true; }
85     if (type->matches(*ctx.fTypes.fInt3)) { v->type = Type::kInt3; return true; }
86     if (type->matches(*ctx.fTypes.fInt4)) { v->type = Type::kInt4; return true; }
87 
88     return false;
89 }
90 
child_type(const SkSL::Type & type)91 static ChildType child_type(const SkSL::Type& type) {
92     switch (type.typeKind()) {
93         case SkSL::Type::TypeKind::kBlender:     return ChildType::kBlender;
94         case SkSL::Type::TypeKind::kColorFilter: return ChildType::kColorFilter;
95         case SkSL::Type::TypeKind::kShader:      return ChildType::kShader;
96         default: SkUNREACHABLE;
97     }
98 }
99 
verify_child_effects(const std::vector<SkRuntimeEffect::Child> & reflected,SkSpan<SkRuntimeEffect::ChildPtr> effectPtrs)100 static bool verify_child_effects(const std::vector<SkRuntimeEffect::Child>& reflected,
101                                  SkSpan<SkRuntimeEffect::ChildPtr> effectPtrs) {
102     // Verify that the number of passed-in child-effect pointers matches the SkSL code.
103     if (reflected.size() != effectPtrs.size()) {
104         return false;
105     }
106 
107     // Verify that each child object's type matches its declared type in the SkSL.
108     for (size_t i = 0; i < effectPtrs.size(); ++i) {
109         std::optional<ChildType> effectType = effectPtrs[i].type();
110         if (effectType && effectType != reflected[i].type) {
111             return false;
112         }
113     }
114     return true;
115 }
116 
read_child_effects(SkReadBuffer & buffer,const SkRuntimeEffect * effect,SkTArray<SkRuntimeEffect::ChildPtr> * children)117 static bool read_child_effects(SkReadBuffer& buffer,
118                                const SkRuntimeEffect* effect,
119                                SkTArray<SkRuntimeEffect::ChildPtr>* children) {
120     size_t childCount = buffer.read32();
121     if (!buffer.validate(childCount == effect->children().size())) {
122         return false;
123     }
124 
125     children->reset();
126     children->reserve_back(childCount);
127 
128     for (const auto& child : effect->children()) {
129         if (child.type == ChildType::kShader) {
130             children->emplace_back(buffer.readShader());
131         } else if (child.type == ChildType::kColorFilter) {
132             children->emplace_back(buffer.readColorFilter());
133         } else if (child.type == ChildType::kBlender) {
134             children->emplace_back(buffer.readBlender());
135         } else {
136             return false;
137         }
138     }
139 
140     return buffer.isValid();
141 }
142 
write_child_effects(SkWriteBuffer & buffer,const std::vector<SkRuntimeEffect::ChildPtr> & children)143 static void write_child_effects(SkWriteBuffer& buffer,
144                                 const std::vector<SkRuntimeEffect::ChildPtr>& children) {
145     buffer.write32(children.size());
146     for (const auto& child : children) {
147         buffer.writeFlattenable(child.flattenable());
148     }
149 }
150 
make_skvm_uniforms(skvm::Builder * p,skvm::Uniforms * uniforms,size_t inputSize,const SkData & inputs)151 static std::vector<skvm::Val> make_skvm_uniforms(skvm::Builder* p,
152                                                  skvm::Uniforms* uniforms,
153                                                  size_t inputSize,
154                                                  const SkData& inputs) {
155     SkASSERTF(!(inputSize & 3), "inputSize was %zu, expected a multiple of 4", inputSize);
156 
157     const int32_t* data = reinterpret_cast<const int32_t*>(inputs.data());
158     const size_t uniformCount = inputSize / sizeof(int32_t);
159     std::vector<skvm::Val> uniform;
160     uniform.reserve(uniformCount);
161     for (size_t index = 0; index < uniformCount; ++index) {
162         int32_t bits;
163         memcpy(&bits, data + index, sizeof(int32_t));
164         uniform.push_back(p->uniform32(uniforms->push(bits)).id);
165     }
166 
167     return uniform;
168 }
169 
MakeSettings(const Options & options,bool optimize)170 SkSL::ProgramSettings SkRuntimeEffect::MakeSettings(const Options& options, bool optimize) {
171     SkSL::ProgramSettings settings;
172     settings.fInlineThreshold = 0;
173     settings.fForceNoInline = options.forceNoInline;
174     settings.fEnforceES2Restrictions = options.enforceES2Restrictions;
175     settings.fOptimize = optimize;
176     return settings;
177 }
178 
179 // TODO: Many errors aren't caught until we process the generated Program here. Catching those
180 // in the IR generator would provide better errors messages (with locations).
181 #define RETURN_FAILURE(...) return Result{nullptr, SkStringPrintf(__VA_ARGS__)}
182 
MakeFromSource(SkString sksl,const Options & options,SkSL::ProgramKind kind)183 SkRuntimeEffect::Result SkRuntimeEffect::MakeFromSource(SkString sksl,
184                                                         const Options& options,
185                                                         SkSL::ProgramKind kind) {
186     std::unique_ptr<SkSL::Program> program;
187     {
188         // We keep this SharedCompiler in a separate scope to make sure it's destroyed before
189         // calling the Make overload at the end, which creates its own (non-reentrant)
190         // SharedCompiler instance
191         SkSL::SharedCompiler compiler;
192         SkSL::Program::Settings settings = MakeSettings(options, /*optimize=*/true);
193         program = compiler->convertProgram(kind, std::string(sksl.c_str(), sksl.size()), settings);
194 
195         if (!program) {
196             RETURN_FAILURE("%s", compiler->errorText().c_str());
197         }
198     }
199     return MakeInternal(std::move(program), options, kind);
200 }
201 
MakeFromDSL(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ProgramKind kind)202 SkRuntimeEffect::Result SkRuntimeEffect::MakeFromDSL(std::unique_ptr<SkSL::Program> program,
203                                                      const Options& options,
204                                                      SkSL::ProgramKind kind) {
205     // This factory is used for all DSL runtime effects, which don't have anything stored in the
206     // program's source. Populate it so that we can compute fHash, and serialize these effects.
207     program->fSource = std::make_unique<std::string>(program->description());
208     return MakeInternal(std::move(program), options, kind);
209 }
210 
MakeFromDSL(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ProgramKind kind,SkSL::ErrorReporter * errors)211 sk_sp<SkRuntimeEffect> SkRuntimeEffect::MakeFromDSL(std::unique_ptr<SkSL::Program> program,
212                                                     const Options& options,
213                                                     SkSL::ProgramKind kind,
214                                                     SkSL::ErrorReporter* errors) {
215     Result result = MakeFromDSL(std::move(program), options, kind);
216     if (!result.effect) {
217         errors->error(result.errorText.c_str(), SkSL::PositionInfo(nullptr, -1));
218     }
219     return std::move(result.effect);
220 }
221 
MakeInternal(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ProgramKind kind)222 SkRuntimeEffect::Result SkRuntimeEffect::MakeInternal(std::unique_ptr<SkSL::Program> program,
223                                                       const Options& options,
224                                                       SkSL::ProgramKind kind) {
225     SkSL::SharedCompiler compiler;
226 
227     // Find 'main', then locate the sample coords parameter. (It might not be present.)
228     const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
229     if (!main) {
230         RETURN_FAILURE("missing 'main' function");
231     }
232     const auto& mainParams = main->declaration().parameters();
233     auto iter = std::find_if(mainParams.begin(), mainParams.end(), [](const SkSL::Variable* p) {
234         return p->modifiers().fLayout.fBuiltin == SK_MAIN_COORDS_BUILTIN;
235     });
236     const SkSL::ProgramUsage::VariableCounts sampleCoordsUsage =
237             iter != mainParams.end() ? program->usage()->get(**iter)
238                                      : SkSL::ProgramUsage::VariableCounts{};
239 
240     uint32_t flags = 0;
241     switch (kind) {
242         case SkSL::ProgramKind::kRuntimeColorFilter: flags |= kAllowColorFilter_Flag; break;
243         case SkSL::ProgramKind::kRuntimeShader:      flags |= kAllowShader_Flag;      break;
244         case SkSL::ProgramKind::kRuntimeBlender:     flags |= kAllowBlender_Flag;     break;
245         default: SkUNREACHABLE;
246     }
247 
248     if (sampleCoordsUsage.fRead || sampleCoordsUsage.fWrite) {
249         flags |= kUsesSampleCoords_Flag;
250     }
251 
252     // TODO(skia:12202): When we can layer modules, implement this restriction by moving the
253     // declaration of sk_FragCoord to a private module.
254     if (!options.allowFragCoord && SkSL::Analysis::ReferencesFragCoords(*program)) {
255         RETURN_FAILURE("unknown identifier 'sk_FragCoord'");
256     }
257 
258     // Color filters and blends are not allowed to depend on position (local or device) in any way.
259     // The signature of main, and the declarations in sksl_rt_colorfilter/sksl_rt_blend should
260     // guarantee this.
261     if (flags & (kAllowColorFilter_Flag | kAllowBlender_Flag)) {
262         SkASSERT(!(flags & kUsesSampleCoords_Flag));
263         SkASSERT(!SkSL::Analysis::ReferencesFragCoords(*program));
264     }
265 
266     if (SkSL::Analysis::CallsSampleOutsideMain(*program)) {
267         flags |= kSamplesOutsideMain_Flag;
268     }
269 
270     // Determine if this effect uses of the color transform intrinsics. Effects need to know this
271     // so they can allocate color transform objects, etc.
272     if (SkSL::Analysis::CallsColorTransformIntrinsics(*program)) {
273         flags |= kUsesColorTransform_Flag;
274     }
275 
276     // Shaders are the only thing that cares about this, but it's inexpensive (and safe) to call.
277     if (SkSL::Analysis::ReturnsOpaqueColor(*main)) {
278         flags |= kAlwaysOpaque_Flag;
279     }
280 
281     size_t offset = 0;
282     std::vector<Uniform> uniforms;
283     std::vector<Child> children;
284     std::vector<SkSL::SampleUsage> sampleUsages;
285     int elidedSampleCoords = 0;
286     const SkSL::Context& ctx(compiler->context());
287 
288     // Go through program elements, pulling out information that we need
289     for (const SkSL::ProgramElement* elem : program->elements()) {
290         // Variables (uniform, etc.)
291         if (elem->is<SkSL::GlobalVarDeclaration>()) {
292             const SkSL::GlobalVarDeclaration& global = elem->as<SkSL::GlobalVarDeclaration>();
293             const SkSL::VarDeclaration& varDecl = global.declaration()->as<SkSL::VarDeclaration>();
294 
295             const SkSL::Variable& var = varDecl.var();
296             const SkSL::Type& varType = var.type();
297 
298             // Child effects that can be sampled ('shader', 'colorFilter', 'blender')
299             if (varType.isEffectChild()) {
300                 Child c;
301                 c.name  = SkString(var.name());
302                 c.type  = child_type(varType);
303                 c.index = children.size();
304                 children.push_back(c);
305                 auto usage = SkSL::Analysis::GetSampleUsage(
306                         *program, var, sampleCoordsUsage.fWrite != 0, &elidedSampleCoords);
307                 // If the child is never sampled, we pretend that it's actually in PassThrough mode.
308                 // Otherwise, the GP code for collecting transforms and emitting transform code gets
309                 // very confused, leading to asserts and bad (backend) shaders. There's an implicit
310                 // assumption that every FP is used by its parent. (skbug.com/12429)
311                 sampleUsages.push_back(usage.isSampled() ? usage
312                                                          : SkSL::SampleUsage::PassThrough());
313             }
314             // 'uniform' variables
315             else if (var.modifiers().fFlags & SkSL::Modifiers::kUniform_Flag) {
316                 Uniform uni;
317                 uni.name = SkString(var.name());
318                 uni.flags = 0;
319                 uni.count = 1;
320 
321                 const SkSL::Type* type = &var.type();
322                 if (type->isArray()) {
323                     uni.flags |= Uniform::kArray_Flag;
324                     uni.count = type->columns();
325                     type = &type->componentType();
326                 }
327 
328                 if (!init_uniform_type(ctx, type, &uni)) {
329                     RETURN_FAILURE("Invalid uniform type: '%s'", type->displayName().c_str());
330                 }
331 
332                 if (var.modifiers().fLayout.fFlags & SkSL::Layout::Flag::kColor_Flag) {
333                     uni.flags |= Uniform::kColor_Flag;
334                 }
335 
336                 uni.offset = offset;
337                 offset += uni.sizeInBytes();
338                 SkASSERT(SkIsAlign4(offset));
339 
340                 uniforms.push_back(uni);
341             }
342         }
343     }
344 
345     // If the sample coords are never written to, then we will have converted sample calls that use
346     // them unmodified into "passthrough" sampling. If all references to the sample coords were of
347     // that form, then we don't actually "use" sample coords. We unset the flag to prevent creating
348     // an extra (unused) varying holding the coords.
349     if (elidedSampleCoords == sampleCoordsUsage.fRead && sampleCoordsUsage.fWrite == 0) {
350         flags &= ~kUsesSampleCoords_Flag;
351     }
352 
353 #undef RETURN_FAILURE
354 
355     sk_sp<SkRuntimeEffect> effect(new SkRuntimeEffect(std::move(program),
356                                                       options,
357                                                       *main,
358                                                       std::move(uniforms),
359                                                       std::move(children),
360                                                       std::move(sampleUsages),
361                                                       flags));
362     return Result{std::move(effect), SkString()};
363 }
364 
makeUnoptimizedClone()365 sk_sp<SkRuntimeEffect> SkRuntimeEffect::makeUnoptimizedClone() {
366     // Compile with maximally-permissive options; any restrictions we need to enforce were already
367     // handled when the original SkRuntimeEffect was made. We don't keep around the Options struct
368     // from when it was initially made so we don't know what was originally requested.
369     Options options;
370     options.forceNoInline = true;
371     options.enforceES2Restrictions = false;
372     options.allowFragCoord = true;
373 
374     // We do know the original ProgramKind, so we don't need to re-derive it.
375     SkSL::ProgramKind kind = fBaseProgram->fConfig->fKind;
376 
377     // Attempt to recompile the program's source with optimizations off. This ensures that the
378     // Debugger shows results on every line, even for things that could be optimized away (static
379     // branches, unused variables, etc). If recompilation fails, we fall back to the original code.
380     std::unique_ptr<SkSL::Program> program;
381     {
382         // We keep this SharedCompiler in a separate scope to make sure it's destroyed before
383         // calling MakeInternal at the end, which creates its own (non-reentrant) SharedCompiler
384         // instance.
385         SkSL::SharedCompiler compiler;
386         SkSL::Program::Settings settings = MakeSettings(options, /*optimize=*/false);
387         program = compiler->convertProgram(kind, *fBaseProgram->fSource, settings);
388 
389         if (!program) {
390             // Turning off compiler optimizations can theoretically expose a program error that
391             // had been optimized away (e.g. "all control paths return a value" might appear if
392             // optimizing a program simplifies its control flow).
393             // If this happens, the debugger will just have to show the optimized code.
394             return sk_ref_sp(this);
395         }
396     }
397 
398     SkRuntimeEffect::Result result = MakeInternal(std::move(program), options, kind);
399     if (!result.effect) {
400         // Nothing in MakeInternal should change as a result of optimizations being toggled.
401         SkDEBUGFAILF("makeUnoptimizedClone: MakeInternal failed\n%s",
402                      result.errorText.c_str());
403         return sk_ref_sp(this);
404     }
405 
406     return result.effect;
407 }
408 
MakeForColorFilter(SkString sksl,const Options & options)409 SkRuntimeEffect::Result SkRuntimeEffect::MakeForColorFilter(SkString sksl, const Options& options) {
410     auto result = MakeFromSource(std::move(sksl), options, SkSL::ProgramKind::kRuntimeColorFilter);
411     SkASSERT(!result.effect || result.effect->allowColorFilter());
412     return result;
413 }
414 
MakeForShader(SkString sksl,const Options & options)415 SkRuntimeEffect::Result SkRuntimeEffect::MakeForShader(SkString sksl, const Options& options) {
416     auto result = MakeFromSource(std::move(sksl), options, SkSL::ProgramKind::kRuntimeShader);
417     SkASSERT(!result.effect || result.effect->allowShader());
418     return result;
419 }
420 
MakeForBlender(SkString sksl,const Options & options)421 SkRuntimeEffect::Result SkRuntimeEffect::MakeForBlender(SkString sksl, const Options& options) {
422     auto result = MakeFromSource(std::move(sksl), options, SkSL::ProgramKind::kRuntimeBlender);
423     SkASSERT(!result.effect || result.effect->allowBlender());
424     return result;
425 }
426 
MakeForColorFilter(std::unique_ptr<SkSL::Program> program,const Options & options)427 SkRuntimeEffect::Result SkRuntimeEffect::MakeForColorFilter(std::unique_ptr<SkSL::Program> program,
428                                                             const Options& options) {
429     auto result = MakeFromDSL(std::move(program), options, SkSL::ProgramKind::kRuntimeColorFilter);
430     SkASSERT(!result.effect || result.effect->allowColorFilter());
431     return result;
432 }
433 
MakeForShader(std::unique_ptr<SkSL::Program> program,const Options & options)434 SkRuntimeEffect::Result SkRuntimeEffect::MakeForShader(std::unique_ptr<SkSL::Program> program,
435                                                        const Options& options) {
436     auto result = MakeFromDSL(std::move(program), options, SkSL::ProgramKind::kRuntimeShader);
437     SkASSERT(!result.effect || result.effect->allowShader());
438     return result;
439 }
440 
MakeForShader(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ErrorReporter * errors)441 sk_sp<SkRuntimeEffect> SkRuntimeEffect::MakeForShader(std::unique_ptr<SkSL::Program> program,
442                                                       const Options& options,
443                                                       SkSL::ErrorReporter* errors) {
444     auto result = MakeFromDSL(std::move(program), options, SkSL::ProgramKind::kRuntimeShader,
445             errors);
446     SkASSERT(!result || result->allowShader());
447     return result;
448 }
449 
MakeForBlender(std::unique_ptr<SkSL::Program> program,const Options & options)450 SkRuntimeEffect::Result SkRuntimeEffect::MakeForBlender(std::unique_ptr<SkSL::Program> program,
451                                                         const Options& options) {
452     auto result = MakeFromDSL(std::move(program), options, SkSL::ProgramKind::kRuntimeBlender);
453     SkASSERT(!result.effect || result.effect->allowBlender());
454     return result;
455 }
456 
MakeForColorFilter(std::unique_ptr<SkSL::Program> program)457 SkRuntimeEffect::Result SkRuntimeEffect::MakeForColorFilter(
458         std::unique_ptr<SkSL::Program> program) {
459     return MakeForColorFilter(std::move(program), Options{});
460 }
461 
MakeForShader(std::unique_ptr<SkSL::Program> program)462 SkRuntimeEffect::Result SkRuntimeEffect::MakeForShader(std::unique_ptr<SkSL::Program> program) {
463     return MakeForShader(std::move(program), Options{});
464 }
465 
MakeForBlender(std::unique_ptr<SkSL::Program> program)466 SkRuntimeEffect::Result SkRuntimeEffect::MakeForBlender(std::unique_ptr<SkSL::Program> program) {
467     return MakeForBlender(std::move(program), Options{});
468 }
469 
SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (* make)(SkString sksl),SkString sksl)470 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (*make)(SkString sksl),
471                                                  SkString sksl) {
472     SK_BEGIN_REQUIRE_DENSE
473     struct Key {
474         uint32_t skslHashA;
475         uint32_t skslHashB;
476 
477         bool operator==(const Key& that) const {
478             return this->skslHashA == that.skslHashA
479                 && this->skslHashB == that.skslHashB;
480         }
481 
482         explicit Key(const SkString& sksl)
483             : skslHashA(SkOpts::hash(sksl.c_str(), sksl.size(), 0))
484             , skslHashB(SkOpts::hash(sksl.c_str(), sksl.size(), 1)) {}
485     };
486     SK_END_REQUIRE_DENSE
487 
488     static auto* mutex = new SkMutex;
489     static auto* cache = new SkLRUCache<Key, sk_sp<SkRuntimeEffect>>(11/*totally arbitrary*/);
490 
491     Key key(sksl);
492     {
493         SkAutoMutexExclusive _(*mutex);
494         if (sk_sp<SkRuntimeEffect>* found = cache->find(key)) {
495             return *found;
496         }
497     }
498 
499     auto [effect, err] = make(std::move(sksl));
500     if (!effect) {
501         return nullptr;
502     }
503     SkASSERT(err.isEmpty());
504 
505     {
506         SkAutoMutexExclusive _(*mutex);
507         cache->insert_or_update(key, effect);
508     }
509     return effect;
510 }
511 
uniform_element_size(SkRuntimeEffect::Uniform::Type type)512 static size_t uniform_element_size(SkRuntimeEffect::Uniform::Type type) {
513     switch (type) {
514         case SkRuntimeEffect::Uniform::Type::kFloat:  return sizeof(float);
515         case SkRuntimeEffect::Uniform::Type::kFloat2: return sizeof(float) * 2;
516         case SkRuntimeEffect::Uniform::Type::kFloat3: return sizeof(float) * 3;
517         case SkRuntimeEffect::Uniform::Type::kFloat4: return sizeof(float) * 4;
518 
519         case SkRuntimeEffect::Uniform::Type::kFloat2x2: return sizeof(float) * 4;
520         case SkRuntimeEffect::Uniform::Type::kFloat3x3: return sizeof(float) * 9;
521         case SkRuntimeEffect::Uniform::Type::kFloat4x4: return sizeof(float) * 16;
522 
523         case SkRuntimeEffect::Uniform::Type::kInt:  return sizeof(int);
524         case SkRuntimeEffect::Uniform::Type::kInt2: return sizeof(int) * 2;
525         case SkRuntimeEffect::Uniform::Type::kInt3: return sizeof(int) * 3;
526         case SkRuntimeEffect::Uniform::Type::kInt4: return sizeof(int) * 4;
527         default: SkUNREACHABLE;
528     }
529 }
530 
sizeInBytes() const531 size_t SkRuntimeEffect::Uniform::sizeInBytes() const {
532     static_assert(sizeof(int) == sizeof(float));
533     return uniform_element_size(this->type) * this->count;
534 }
535 
SkRuntimeEffect(std::unique_ptr<SkSL::Program> baseProgram,const Options & options,const SkSL::FunctionDefinition & main,std::vector<Uniform> && uniforms,std::vector<Child> && children,std::vector<SkSL::SampleUsage> && sampleUsages,uint32_t flags)536 SkRuntimeEffect::SkRuntimeEffect(std::unique_ptr<SkSL::Program> baseProgram,
537                                  const Options& options,
538                                  const SkSL::FunctionDefinition& main,
539                                  std::vector<Uniform>&& uniforms,
540                                  std::vector<Child>&& children,
541                                  std::vector<SkSL::SampleUsage>&& sampleUsages,
542                                  uint32_t flags)
543         : fHash(SkOpts::hash_fn(baseProgram->fSource->c_str(), baseProgram->fSource->size(), 0))
544         , fBaseProgram(std::move(baseProgram))
545         , fMain(main)
546         , fUniforms(std::move(uniforms))
547         , fChildren(std::move(children))
548         , fSampleUsages(std::move(sampleUsages))
549         , fFlags(flags) {
550     SkASSERT(fBaseProgram);
551     SkASSERT(fChildren.size() == fSampleUsages.size());
552 
553     // Everything from SkRuntimeEffect::Options which could influence the compiled result needs to
554     // be accounted for in `fHash`. If you've added a new field to Options and caused the static-
555     // assert below to trigger, please incorporate your field into `fHash` and update KnownOptions
556     // to match the layout of Options.
557     struct KnownOptions { bool forceNoInline, enforceES2Restrictions, allowFragCoord; };
558     static_assert(sizeof(Options) == sizeof(KnownOptions));
559     fHash = SkOpts::hash_fn(&options.forceNoInline,
560                       sizeof(options.forceNoInline), fHash);
561     fHash = SkOpts::hash_fn(&options.enforceES2Restrictions,
562                       sizeof(options.enforceES2Restrictions), fHash);
563     fHash = SkOpts::hash_fn(&options.allowFragCoord,
564                       sizeof(options.allowFragCoord), fHash);
565 
566     fFilterColorProgram = SkFilterColorProgram::Make(this);
567 }
568 
569 SkRuntimeEffect::~SkRuntimeEffect() = default;
570 
source() const571 const std::string& SkRuntimeEffect::source() const {
572     return *fBaseProgram->fSource;
573 }
574 
uniformSize() const575 size_t SkRuntimeEffect::uniformSize() const {
576     return fUniforms.empty() ? 0
577                              : SkAlign4(fUniforms.back().offset + fUniforms.back().sizeInBytes());
578 }
579 
findUniform(const char * name) const580 const SkRuntimeEffect::Uniform* SkRuntimeEffect::findUniform(const char* name) const {
581     SkASSERT(name);
582     size_t len = strlen(name);
583     auto iter = std::find_if(fUniforms.begin(), fUniforms.end(), [name, len](const Uniform& u) {
584         return u.name.equals(name, len);
585     });
586     return iter == fUniforms.end() ? nullptr : &(*iter);
587 }
588 
findChild(const char * name) const589 const SkRuntimeEffect::Child* SkRuntimeEffect::findChild(const char* name) const {
590     SkASSERT(name);
591     size_t len = strlen(name);
592     auto iter = std::find_if(fChildren.begin(), fChildren.end(), [name, len](const Child& c) {
593         return c.name.equals(name, len);
594     });
595     return iter == fChildren.end() ? nullptr : &(*iter);
596 }
597 
Make(const SkRuntimeEffect * effect)598 std::unique_ptr<SkFilterColorProgram> SkFilterColorProgram::Make(const SkRuntimeEffect* effect) {
599     // Our per-effect program technique is only possible (and necessary) for color filters
600     if (!effect->allowColorFilter()) {
601         return nullptr;
602     }
603 
604     // TODO(skia:10479): Can we support this? When the color filter is invoked like this, there
605     // may not be a real working space? If there is, we'd need to add it as a parameter to eval,
606     // and then coordinate where the relevant uniforms go. For now, just fall back to the slow
607     // path if we see these intrinsics being called.
608     if (effect->usesColorTransform()) {
609         return nullptr;
610     }
611 
612     // We require that any children are color filters (not shaders or blenders). In theory, we could
613     // detect the coords being passed to shader children, and replicate those calls, but that's very
614     // complicated, and has diminishing returns. (eg, for table lookup color filters).
615     if (!std::all_of(effect->fChildren.begin(),
616                      effect->fChildren.end(),
617                      [](const SkRuntimeEffect::Child& c) {
618                          return c.type == ChildType::kColorFilter;
619                      })) {
620         return nullptr;
621     }
622 
623     skvm::Builder p;
624 
625     // For SkSL uniforms, we reserve space and allocate skvm Uniform ids for each one. When we run
626     // the program, these ids will be loads from the *first* arg ptr, the uniform data of the
627     // specific color filter instance.
628     skvm::Uniforms skslUniforms{p.uniform(), 0};
629     const size_t uniformCount = effect->uniformSize() / 4;
630     std::vector<skvm::Val> uniform;
631     uniform.reserve(uniformCount);
632     for (size_t i = 0; i < uniformCount; i++) {
633         uniform.push_back(p.uniform32(skslUniforms.push(/*placeholder*/ 0)).id);
634     }
635 
636     // We reserve a uniform color for each child invocation. While processing the SkSL, we record
637     // the index of the child, and the color being filtered (in a SampleCall struct).
638     // When we run this program later, we use the SampleCall to evaluate the correct child, and
639     // populate these uniform values. These Uniform ids are loads from the *second* arg ptr.
640     // If the color being passed is too complex for us to describe and re-create using SampleCall,
641     // we are unable to use this per-effect program, and callers will need to fall back to another
642     // (slower) implementation.
643     skvm::Uniforms childColorUniforms{p.uniform(), 0};
644     skvm::Color inputColor = p.uniformColor(/*placeholder*/ SkColors::kWhite, &childColorUniforms);
645     std::vector<SkFilterColorProgram::SampleCall> sampleCalls;
646 
647     class Callbacks : public SkSL::SkVMCallbacks {
648     public:
649         Callbacks(skvm::Builder* builder,
650                   const skvm::Uniforms* skslUniforms,
651                   skvm::Uniforms* childColorUniforms,
652                   skvm::Color inputColor,
653                   std::vector<SkFilterColorProgram::SampleCall>* sampleCalls)
654                 : fBuilder(builder)
655                 , fSkslUniforms(skslUniforms)
656                 , fChildColorUniforms(childColorUniforms)
657                 , fInputColor(inputColor)
658                 , fSampleCalls(sampleCalls) {}
659 
660         bool isSimpleUniform(skvm::Color c, int* baseOffset) {
661             skvm::Uniform ur, ug, ub, ua;
662             if (!fBuilder->allUniform(c.r.id, &ur, c.g.id, &ug, c.b.id, &ub, c.a.id, &ua)) {
663                 return false;
664             }
665             skvm::Ptr uniPtr = fSkslUniforms->base;
666             if (ur.ptr != uniPtr || ug.ptr != uniPtr || ub.ptr != uniPtr || ua.ptr != uniPtr) {
667                 return false;
668             }
669             *baseOffset = ur.offset;
670             return ug.offset == ur.offset + 4 &&
671                    ub.offset == ur.offset + 8 &&
672                    ua.offset == ur.offset + 12;
673         }
674 
675         static bool IDsEqual(skvm::Color x, skvm::Color y) {
676             return x.r.id == y.r.id && x.g.id == y.g.id && x.b.id == y.b.id && x.a.id == y.a.id;
677         }
678 
679         skvm::Color sampleColorFilter(int ix, skvm::Color c) override {
680             skvm::Color result =
681                     fBuilder->uniformColor(/*placeholder*/ SkColors::kWhite, fChildColorUniforms);
682             SkFilterColorProgram::SampleCall call;
683             call.fChild = ix;
684             if (IDsEqual(c, fInputColor)) {
685                 call.fKind = SkFilterColorProgram::SampleCall::Kind::kInputColor;
686             } else if (fBuilder->allImm(c.r.id, &call.fImm.fR,
687                                         c.g.id, &call.fImm.fG,
688                                         c.b.id, &call.fImm.fB,
689                                         c.a.id, &call.fImm.fA)) {
690                 call.fKind = SkFilterColorProgram::SampleCall::Kind::kImmediate;
691             } else if (auto it = std::find_if(fChildColors.begin(),
692                                               fChildColors.end(),
693                                               [&](skvm::Color x) { return IDsEqual(x, c); });
694                        it != fChildColors.end()) {
695                 call.fKind = SkFilterColorProgram::SampleCall::Kind::kPrevious;
696                 call.fPrevious = SkTo<int>(it - fChildColors.begin());
697             } else if (isSimpleUniform(c, &call.fOffset)) {
698                 call.fKind = SkFilterColorProgram::SampleCall::Kind::kUniform;
699             } else {
700                 fAllSampleCallsSupported = false;
701             }
702             fSampleCalls->push_back(call);
703             fChildColors.push_back(result);
704             return result;
705         }
706 
707         // We did an early return from this function if we saw any child that wasn't a shader, so
708         // it should be impossible for either of these callbacks to occur:
709         skvm::Color sampleShader(int, skvm::Coord) override {
710             SkDEBUGFAIL("Unexpected child type");
711             return {};
712         }
713         skvm::Color sampleBlender(int, skvm::Color, skvm::Color) override {
714             SkDEBUGFAIL("Unexpected child type");
715             return {};
716         }
717 
718         // We did an early return from this function if we saw any call to these intrinsics, so it
719         // should be impossible for either of these callbacks to occur:
720         skvm::Color toLinearSrgb(skvm::Color color) override {
721             SkDEBUGFAIL("Unexpected color transform intrinsic");
722             return {};
723         }
724         skvm::Color fromLinearSrgb(skvm::Color color) override {
725             SkDEBUGFAIL("Unexpected color transform intrinsic");
726             return {};
727         }
728 
729         skvm::Builder* fBuilder;
730         const skvm::Uniforms* fSkslUniforms;
731         skvm::Uniforms* fChildColorUniforms;
732         skvm::Color fInputColor;
733         std::vector<SkFilterColorProgram::SampleCall>* fSampleCalls;
734 
735         std::vector<skvm::Color> fChildColors;
736         bool fAllSampleCallsSupported = true;
737     };
738     Callbacks callbacks(&p, &skslUniforms, &childColorUniforms, inputColor, &sampleCalls);
739 
740     // Emit the skvm instructions for the SkSL
741     skvm::Coord zeroCoord = {p.splat(0.0f), p.splat(0.0f)};
742     skvm::Color result = SkSL::ProgramToSkVM(*effect->fBaseProgram,
743                                              effect->fMain,
744                                              &p,
745                                              /*debugTrace=*/nullptr,
746                                              SkMakeSpan(uniform),
747                                              /*device=*/zeroCoord,
748                                              /*local=*/zeroCoord,
749                                              inputColor,
750                                              inputColor,
751                                              &callbacks);
752 
753     // Then store the result to the *third* arg ptr
754     p.store({skvm::PixelFormat::FLOAT, 32, 32, 32, 32, 0, 32, 64, 96},
755             p.varying<skvm::F32>(), result);
756 
757     if (!callbacks.fAllSampleCallsSupported) {
758         return nullptr;
759     }
760 
761     // This is conservative. If a filter gets the input color by sampling a null child, we'll
762     // return an (acceptable) false negative. All internal runtime color filters should work.
763     bool alphaUnchanged = (inputColor.a.id == result.a.id);
764 
765     // We'll use this program to filter one color at a time, don't bother with jit
766     return std::unique_ptr<SkFilterColorProgram>(
767             new SkFilterColorProgram(p.done(/*debug_name=*/nullptr, /*allow_jit=*/false),
768                                      std::move(sampleCalls),
769                                      alphaUnchanged));
770 }
771 
SkFilterColorProgram(skvm::Program program,std::vector<SampleCall> sampleCalls,bool alphaUnchanged)772 SkFilterColorProgram::SkFilterColorProgram(skvm::Program program,
773                                            std::vector<SampleCall> sampleCalls,
774                                            bool alphaUnchanged)
775         : fProgram(std::move(program))
776         , fSampleCalls(std::move(sampleCalls))
777         , fAlphaUnchanged(alphaUnchanged) {}
778 
eval(const SkPMColor4f & inColor,const void * uniformData,std::function<SkPMColor4f (int,SkPMColor4f)> evalChild) const779 SkPMColor4f SkFilterColorProgram::eval(
780         const SkPMColor4f& inColor,
781         const void* uniformData,
782         std::function<SkPMColor4f(int, SkPMColor4f)> evalChild) const {
783     // Our program defines sampling any child as returning a uniform color. Assemble a buffer
784     // containing those colors. The first entry is always the input color. Subsequent entries
785     // are for each sample call, based on the information in fSampleCalls. For any null children,
786     // the sample result is just the passed-in color.
787     SkSTArray<4, SkPMColor4f, true> childColors;
788     childColors.push_back(inColor);
789     for (const auto& s : fSampleCalls) {
790         SkPMColor4f passedColor = inColor;
791         switch (s.fKind) {
792             case SampleCall::Kind::kInputColor:                                             break;
793             case SampleCall::Kind::kImmediate:  passedColor = s.fImm;                       break;
794             case SampleCall::Kind::kPrevious:   passedColor = childColors[s.fPrevious + 1]; break;
795             case SampleCall::Kind::kUniform:
796                 passedColor = *SkTAddOffset<const SkPMColor4f>(uniformData, s.fOffset);
797                 break;
798         }
799         childColors.push_back(evalChild(s.fChild, passedColor));
800     }
801 
802     SkPMColor4f result;
803     fProgram.eval(1, uniformData, childColors.begin(), result.vec());
804     return result;
805 }
806 
getFilterColorProgram()807 const SkFilterColorProgram* SkRuntimeEffect::getFilterColorProgram() {
808     return fFilterColorProgram.get();
809 }
810 
811 ///////////////////////////////////////////////////////////////////////////////////////////////////
812 
get_xformed_uniforms(const SkRuntimeEffect * effect,sk_sp<SkData> baseUniforms,const SkColorSpace * dstCS)813 static sk_sp<SkData> get_xformed_uniforms(const SkRuntimeEffect* effect,
814                                           sk_sp<SkData> baseUniforms,
815                                           const SkColorSpace* dstCS) {
816     using Flags = SkRuntimeEffect::Uniform::Flags;
817     using Type = SkRuntimeEffect::Uniform::Type;
818     SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
819                                  dstCS,               kUnpremul_SkAlphaType);
820 
821     sk_sp<SkData> uniforms = nullptr;
822     auto writableData = [&]() {
823         if (!uniforms) {
824             uniforms = SkData::MakeWithCopy(baseUniforms->data(), baseUniforms->size());
825         }
826         return uniforms->writable_data();
827     };
828 
829     for (const auto& v : effect->uniforms()) {
830         if (v.flags & Flags::kColor_Flag) {
831             SkASSERT(v.type == Type::kFloat3 || v.type == Type::kFloat4);
832             if (steps.flags.mask()) {
833                 float* color = SkTAddOffset<float>(writableData(), v.offset);
834                 if (v.type == Type::kFloat4) {
835                     // RGBA, easy case
836                     for (int i = 0; i < v.count; ++i) {
837                         steps.apply(color);
838                         color += 4;
839                     }
840                 } else {
841                     // RGB, need to pad out to include alpha. Technically, this isn't necessary,
842                     // because steps shouldn't include unpremul or premul, and thus shouldn't
843                     // read or write the fourth element. But let's be safe.
844                     float rgba[4];
845                     for (int i = 0; i < v.count; ++i) {
846                         memcpy(rgba, color, 3 * sizeof(float));
847                         rgba[3] = 1.0f;
848                         steps.apply(rgba);
849                         memcpy(color, rgba, 3 * sizeof(float));
850                         color += 3;
851                     }
852                 }
853             }
854         }
855     }
856     return uniforms ? uniforms : baseUniforms;
857 }
858 
859 #if SK_SUPPORT_GPU
make_effect_fp(sk_sp<SkRuntimeEffect> effect,const char * name,sk_sp<SkData> uniforms,std::unique_ptr<GrFragmentProcessor> inputFP,std::unique_ptr<GrFragmentProcessor> destColorFP,SkSpan<const SkRuntimeEffect::ChildPtr> children,const GrFPArgs & childArgs)860 static GrFPResult make_effect_fp(sk_sp<SkRuntimeEffect> effect,
861                                  const char* name,
862                                  sk_sp<SkData> uniforms,
863                                  std::unique_ptr<GrFragmentProcessor> inputFP,
864                                  std::unique_ptr<GrFragmentProcessor> destColorFP,
865                                  SkSpan<const SkRuntimeEffect::ChildPtr> children,
866                                  const GrFPArgs& childArgs) {
867     SkSTArray<8, std::unique_ptr<GrFragmentProcessor>> childFPs;
868     for (const auto& child : children) {
869         std::optional<ChildType> type = child.type();
870         if (type == ChildType::kShader) {
871             // Convert a SkShader into a child FP.
872             auto childFP = as_SB(child.shader())->asFragmentProcessor(childArgs);
873             if (!childFP) {
874                 return GrFPFailure(std::move(inputFP));
875             }
876             childFPs.push_back(std::move(childFP));
877         } else if (type == ChildType::kColorFilter) {
878             // Convert a SkColorFilter into a child FP.
879             auto [success, childFP] = as_CFB(child.colorFilter())
880                                               ->asFragmentProcessor(/*inputFP=*/nullptr,
881                                                                     childArgs.fContext,
882                                                                     *childArgs.fDstColorInfo);
883             if (!success) {
884                 return GrFPFailure(std::move(inputFP));
885             }
886             childFPs.push_back(std::move(childFP));
887         } else if (type == ChildType::kBlender) {
888             // Convert a SkBlender into a child FP.
889             auto childFP = as_BB(child.blender())->asFragmentProcessor(
890                     /*srcFP=*/nullptr,
891                     GrFragmentProcessor::UseDestColorAsInput(/*dstFP=*/nullptr),
892                     childArgs);
893             if (!childFP) {
894                 return GrFPFailure(std::move(inputFP));
895             }
896             childFPs.push_back(std::move(childFP));
897         } else {
898             // We have a null child effect.
899             childFPs.push_back(nullptr);
900         }
901     }
902     auto fp = GrSkSLFP::MakeWithData(std::move(effect),
903                                      name,
904                                      childArgs.fDstColorInfo->refColorSpace(),
905                                      std::move(inputFP),
906                                      std::move(destColorFP),
907                                      std::move(uniforms),
908                                      SkMakeSpan(childFPs));
909     SkASSERT(fp);
910     return GrFPSuccess(std::move(fp));
911 }
912 #endif
913 
914 class RuntimeEffectVMCallbacks : public SkSL::SkVMCallbacks {
915 public:
RuntimeEffectVMCallbacks(skvm::Builder * builder,skvm::Uniforms * uniforms,SkArenaAlloc * alloc,const std::vector<SkRuntimeEffect::ChildPtr> & children,skvm::Color inColor,const SkColorInfo & colorInfo)916     RuntimeEffectVMCallbacks(skvm::Builder* builder,
917                              skvm::Uniforms* uniforms,
918                              SkArenaAlloc* alloc,
919                              const std::vector<SkRuntimeEffect::ChildPtr>& children,
920                              skvm::Color inColor,
921                              const SkColorInfo& colorInfo)
922             : fBuilder(builder)
923             , fUniforms(uniforms)
924             , fAlloc(alloc)
925             , fChildren(children)
926             , fInColor(inColor)
927             , fColorInfo(colorInfo) {}
928 
sampleShader(int ix,skvm::Coord coord)929     skvm::Color sampleShader(int ix, skvm::Coord coord) override {
930         if (SkShader* shader = fChildren[ix].shader()) {
931             SkOverrideDeviceMatrixProvider matrixProvider(SkMatrix::I());
932             return as_SB(shader)->program(fBuilder, coord, coord, fInColor, matrixProvider,
933                                           /*localM=*/nullptr, fColorInfo, fUniforms, fAlloc);
934         }
935         return fInColor;
936     }
937 
sampleColorFilter(int ix,skvm::Color color)938     skvm::Color sampleColorFilter(int ix, skvm::Color color) override {
939         if (SkColorFilter* colorFilter = fChildren[ix].colorFilter()) {
940             return as_CFB(colorFilter)->program(fBuilder, color, fColorInfo, fUniforms, fAlloc);
941         }
942         return color;
943     }
944 
sampleBlender(int ix,skvm::Color src,skvm::Color dst)945     skvm::Color sampleBlender(int ix, skvm::Color src, skvm::Color dst) override {
946         if (SkBlender* blender = fChildren[ix].blender()) {
947             return as_BB(blender)->program(fBuilder, src, dst, fColorInfo, fUniforms, fAlloc);
948         }
949         return blend(SkBlendMode::kSrcOver, src, dst);
950     }
951 
toLinearSrgb(skvm::Color color)952     skvm::Color toLinearSrgb(skvm::Color color) override {
953         if (!fColorInfo.colorSpace()) {
954             // These intrinsics do nothing when color management is disabled
955             return color;
956         }
957         return SkColorSpaceXformSteps{fColorInfo.colorSpace(),    kUnpremul_SkAlphaType,
958                                       sk_srgb_linear_singleton(), kUnpremul_SkAlphaType}
959                 .program(fBuilder, fUniforms, color);
960     }
961 
fromLinearSrgb(skvm::Color color)962     skvm::Color fromLinearSrgb(skvm::Color color) override {
963         if (!fColorInfo.colorSpace()) {
964             // These intrinsics do nothing when color management is disabled
965             return color;
966         }
967         return SkColorSpaceXformSteps{sk_srgb_linear_singleton(), kUnpremul_SkAlphaType,
968                                       fColorInfo.colorSpace(),    kUnpremul_SkAlphaType}
969                 .program(fBuilder, fUniforms, color);
970     }
971 
972     skvm::Builder* fBuilder;
973     skvm::Uniforms* fUniforms;
974     SkArenaAlloc* fAlloc;
975     const std::vector<SkRuntimeEffect::ChildPtr>& fChildren;
976     const skvm::Color fInColor;
977     const SkColorInfo& fColorInfo;
978 };
979 
980 class SkRuntimeColorFilter : public SkColorFilterBase {
981 public:
SkRuntimeColorFilter(sk_sp<SkRuntimeEffect> effect,sk_sp<SkData> uniforms,SkSpan<SkRuntimeEffect::ChildPtr> children)982     SkRuntimeColorFilter(sk_sp<SkRuntimeEffect> effect,
983                          sk_sp<SkData> uniforms,
984                          SkSpan<SkRuntimeEffect::ChildPtr> children)
985             : fEffect(std::move(effect))
986             , fUniforms(std::move(uniforms))
987             , fChildren(children.begin(), children.end()) {}
988 
989 #if SK_SUPPORT_GPU
asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,GrRecordingContext * context,const GrColorInfo & colorInfo) const990     GrFPResult asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> inputFP,
991                                    GrRecordingContext* context,
992                                    const GrColorInfo& colorInfo) const override {
993         sk_sp<SkData> uniforms =
994                 get_xformed_uniforms(fEffect.get(), fUniforms, colorInfo.colorSpace());
995         SkASSERT(uniforms);
996 
997         SkOverrideDeviceMatrixProvider matrixProvider(SkMatrix::I());
998         GrFPArgs childArgs(context, matrixProvider, &colorInfo);
999         return make_effect_fp(fEffect,
1000                               "runtime_color_filter",
1001                               std::move(uniforms),
1002                               std::move(inputFP),
1003                               /*destColorFP=*/nullptr,
1004                               SkMakeSpan(fChildren),
1005                               childArgs);
1006     }
1007 #endif
1008 
onAppendStages(const SkStageRec & rec,bool shaderIsOpaque) const1009     bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
1010         return false;
1011     }
1012 
onProgram(skvm::Builder * p,skvm::Color c,const SkColorInfo & colorInfo,skvm::Uniforms * uniforms,SkArenaAlloc * alloc) const1013     skvm::Color onProgram(skvm::Builder* p, skvm::Color c,
1014                           const SkColorInfo& colorInfo,
1015                           skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const override {
1016         sk_sp<SkData> inputs =
1017                 get_xformed_uniforms(fEffect.get(), fUniforms, colorInfo.colorSpace());
1018         SkASSERT(inputs);
1019 
1020         RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, c, colorInfo);
1021         std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
1022                                                             *inputs);
1023 
1024         // There should be no way for the color filter to use device coords, but we need to supply
1025         // something. (Uninitialized values can trigger asserts in skvm::Builder).
1026         skvm::Coord zeroCoord = { p->splat(0.0f), p->splat(0.0f) };
1027         return SkSL::ProgramToSkVM(*fEffect->fBaseProgram, fEffect->fMain, p,/*debugTrace=*/nullptr,
1028                                    SkMakeSpan(uniform), /*device=*/zeroCoord, /*local=*/zeroCoord,
1029                                    c, c, &callbacks);
1030     }
1031 
onFilterColor4f(const SkPMColor4f & color,SkColorSpace * dstCS) const1032     SkPMColor4f onFilterColor4f(const SkPMColor4f& color, SkColorSpace* dstCS) const override {
1033         // Get the generic program for filtering a single color
1034         const SkFilterColorProgram* program = fEffect->getFilterColorProgram();
1035         if (!program) {
1036             // We were unable to build a cached (per-effect) program. Use the base-class fallback,
1037             // which builds a program for the specific filter instance.
1038             return SkColorFilterBase::onFilterColor4f(color, dstCS);
1039         }
1040 
1041         // Get our specific uniform values
1042         sk_sp<SkData> inputs = get_xformed_uniforms(fEffect.get(), fUniforms, dstCS);
1043         SkASSERT(inputs);
1044 
1045         auto evalChild = [&](int index, SkPMColor4f inColor) {
1046             const auto& child = fChildren[index];
1047 
1048             // SkFilterColorProgram::Make has guaranteed that any children will be color filters.
1049             SkASSERT(!child.shader());
1050             SkASSERT(!child.blender());
1051             if (SkColorFilter* colorFilter = child.colorFilter()) {
1052                 return as_CFB(colorFilter)->onFilterColor4f(inColor, dstCS);
1053             }
1054             return inColor;
1055         };
1056 
1057         return program->eval(color, inputs->data(), evalChild);
1058     }
1059 
onIsAlphaUnchanged() const1060     bool onIsAlphaUnchanged() const override {
1061         return fEffect->getFilterColorProgram() &&
1062                fEffect->getFilterColorProgram()->isAlphaUnchanged();
1063     }
1064 
flatten(SkWriteBuffer & buffer) const1065     void flatten(SkWriteBuffer& buffer) const override {
1066         buffer.writeString(fEffect->source().c_str());
1067         buffer.writeDataAsByteArray(fUniforms.get());
1068         write_child_effects(buffer, fChildren);
1069     }
1070 
asRuntimeEffect() const1071     SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
1072 
1073     SK_FLATTENABLE_HOOKS(SkRuntimeColorFilter)
1074 
1075 private:
1076     sk_sp<SkRuntimeEffect> fEffect;
1077     sk_sp<SkData> fUniforms;
1078     std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1079 };
1080 
CreateProc(SkReadBuffer & buffer)1081 sk_sp<SkFlattenable> SkRuntimeColorFilter::CreateProc(SkReadBuffer& buffer) {
1082     SkString sksl;
1083     buffer.readString(&sksl);
1084     sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1085 
1086     auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, std::move(sksl));
1087     if (!buffer.validate(effect != nullptr)) {
1088         return nullptr;
1089     }
1090 
1091     SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1092     if (!read_child_effects(buffer, effect.get(), &children)) {
1093         return nullptr;
1094     }
1095 
1096     return effect->makeColorFilter(std::move(uniforms), SkMakeSpan(children));
1097 }
1098 
1099 ///////////////////////////////////////////////////////////////////////////////////////////////////
1100 
1101 class SkRTShader : public SkShaderBase {
1102 public:
SkRTShader(sk_sp<SkRuntimeEffect> effect,sk_sp<SkSL::SkVMDebugTrace> debugTrace,sk_sp<SkData> uniforms,const SkMatrix * localMatrix,SkSpan<SkRuntimeEffect::ChildPtr> children)1103     SkRTShader(sk_sp<SkRuntimeEffect> effect,
1104                sk_sp<SkSL::SkVMDebugTrace> debugTrace,
1105                sk_sp<SkData> uniforms,
1106                const SkMatrix* localMatrix,
1107                SkSpan<SkRuntimeEffect::ChildPtr> children)
1108             : SkShaderBase(localMatrix)
1109             , fEffect(std::move(effect))
1110             , fDebugTrace(std::move(debugTrace))
1111             , fUniforms(std::move(uniforms))
1112             , fChildren(children.begin(), children.end()) {}
1113 
makeTracedClone(const SkIPoint & coord)1114     SkRuntimeEffect::TracedShader makeTracedClone(const SkIPoint& coord) {
1115         sk_sp<SkRuntimeEffect> unoptimized = fEffect->makeUnoptimizedClone();
1116         sk_sp<SkSL::SkVMDebugTrace> debugTrace = make_skvm_debug_trace(unoptimized.get(), coord);
1117         auto debugShader = sk_make_sp<SkRTShader>(unoptimized, debugTrace, fUniforms,
1118                                                   &this->getLocalMatrix(), SkMakeSpan(fChildren));
1119 
1120         return SkRuntimeEffect::TracedShader{std::move(debugShader), std::move(debugTrace)};
1121     }
1122 
isOpaque() const1123     bool isOpaque() const override { return fEffect->alwaysOpaque(); }
1124 
1125 #if SK_SUPPORT_GPU
asFragmentProcessor(const GrFPArgs & args) const1126     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs& args) const override {
1127         SkMatrix matrix;
1128         if (!this->totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
1129             return nullptr;
1130         }
1131 
1132         sk_sp<SkData> uniforms =
1133                 get_xformed_uniforms(fEffect.get(), fUniforms, args.fDstColorInfo->colorSpace());
1134         SkASSERT(uniforms);
1135 
1136         // We handle the pre-local matrix at this level so strip it out.
1137         GrFPArgs fpArgs = args;
1138         fpArgs.fPreLocalMatrix = nullptr;
1139         auto [success, fp] = make_effect_fp(fEffect,
1140                                             "runtime_shader",
1141                                             std::move(uniforms),
1142                                             /*inputFP=*/nullptr,
1143                                             /*destColorFP=*/nullptr,
1144                                             SkMakeSpan(fChildren),
1145                                             fpArgs);
1146         if (!success) {
1147             return nullptr;
1148         }
1149 
1150         return GrMatrixEffect::Make(matrix, std::move(fp));
1151     }
1152 #endif
1153 
onAppendStages(const SkStageRec & rec) const1154     bool onAppendStages(const SkStageRec& rec) const override {
1155         return false;
1156     }
1157 
onProgram(skvm::Builder * p,skvm::Coord device,skvm::Coord local,skvm::Color paint,const SkMatrixProvider & matrices,const SkMatrix * localM,const SkColorInfo & colorInfo,skvm::Uniforms * uniforms,SkArenaAlloc * alloc) const1158     skvm::Color onProgram(skvm::Builder* p,
1159                           skvm::Coord device, skvm::Coord local, skvm::Color paint,
1160                           const SkMatrixProvider& matrices, const SkMatrix* localM,
1161                           const SkColorInfo& colorInfo,
1162                           skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const override {
1163         sk_sp<SkData> inputs =
1164                 get_xformed_uniforms(fEffect.get(), fUniforms, colorInfo.colorSpace());
1165         SkASSERT(inputs);
1166 
1167         SkMatrix inv;
1168         if (!this->computeTotalInverse(matrices.localToDevice(), localM, &inv)) {
1169             return {};
1170         }
1171         local = SkShaderBase::ApplyMatrix(p,inv,local,uniforms);
1172 
1173         RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, paint, colorInfo);
1174         std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
1175                                                             *inputs);
1176 
1177         return SkSL::ProgramToSkVM(*fEffect->fBaseProgram, fEffect->fMain, p, fDebugTrace.get(),
1178                                    SkMakeSpan(uniform), device, local, paint, paint, &callbacks);
1179     }
1180 
flatten(SkWriteBuffer & buffer) const1181     void flatten(SkWriteBuffer& buffer) const override {
1182         uint32_t flags = 0;
1183         if (!this->getLocalMatrix().isIdentity()) {
1184             flags |= kHasLocalMatrix_Flag;
1185         }
1186 
1187         buffer.writeString(fEffect->source().c_str());
1188         buffer.writeDataAsByteArray(fUniforms.get());
1189         buffer.write32(flags);
1190         if (flags & kHasLocalMatrix_Flag) {
1191             buffer.writeMatrix(this->getLocalMatrix());
1192         }
1193         write_child_effects(buffer, fChildren);
1194     }
1195 
asRuntimeEffect() const1196     SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
1197 
1198     SK_FLATTENABLE_HOOKS(SkRTShader)
1199 
1200 private:
1201     enum Flags {
1202         kHasLocalMatrix_Flag    = 1 << 1,
1203     };
1204 
1205     sk_sp<SkRuntimeEffect> fEffect;
1206     sk_sp<SkSL::SkVMDebugTrace> fDebugTrace;
1207 
1208     sk_sp<SkData> fUniforms;
1209     std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1210 };
1211 
CreateProc(SkReadBuffer & buffer)1212 sk_sp<SkFlattenable> SkRTShader::CreateProc(SkReadBuffer& buffer) {
1213     SkString sksl;
1214     buffer.readString(&sksl);
1215     sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1216     uint32_t flags = buffer.read32();
1217 
1218     SkMatrix localM, *localMPtr = nullptr;
1219     if (flags & kHasLocalMatrix_Flag) {
1220         buffer.readMatrix(&localM);
1221         localMPtr = &localM;
1222     }
1223 
1224     auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForShader, std::move(sksl));
1225     if (!buffer.validate(effect != nullptr)) {
1226         return nullptr;
1227     }
1228 
1229     SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1230     if (!read_child_effects(buffer, effect.get(), &children)) {
1231         return nullptr;
1232     }
1233 
1234     return effect->makeShader(std::move(uniforms), SkMakeSpan(children), localMPtr);
1235 }
1236 
1237 ///////////////////////////////////////////////////////////////////////////////////////////////////
1238 
1239 class SkRuntimeBlender : public SkBlenderBase {
1240 public:
SkRuntimeBlender(sk_sp<SkRuntimeEffect> effect,sk_sp<SkData> uniforms,SkSpan<SkRuntimeEffect::ChildPtr> children)1241     SkRuntimeBlender(sk_sp<SkRuntimeEffect> effect,
1242                      sk_sp<SkData> uniforms,
1243                      SkSpan<SkRuntimeEffect::ChildPtr> children)
1244             : fEffect(std::move(effect))
1245             , fUniforms(std::move(uniforms))
1246             , fChildren(children.begin(), children.end()) {}
1247 
asRuntimeEffect() const1248     SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
1249 
onProgram(skvm::Builder * p,skvm::Color src,skvm::Color dst,const SkColorInfo & colorInfo,skvm::Uniforms * uniforms,SkArenaAlloc * alloc) const1250     skvm::Color onProgram(skvm::Builder* p, skvm::Color src, skvm::Color dst,
1251                           const SkColorInfo& colorInfo, skvm::Uniforms* uniforms,
1252                           SkArenaAlloc* alloc) const override {
1253         sk_sp<SkData> inputs = get_xformed_uniforms(fEffect.get(), fUniforms,
1254                                                     colorInfo.colorSpace());
1255         SkASSERT(inputs);
1256 
1257         RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, src, colorInfo);
1258         std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
1259                                                             *inputs);
1260 
1261         // Emit the blend function as an SkVM program.
1262         skvm::Coord zeroCoord = {p->splat(0.0f), p->splat(0.0f)};
1263         return SkSL::ProgramToSkVM(*fEffect->fBaseProgram, fEffect->fMain, p,/*debugTrace=*/nullptr,
1264                                    SkMakeSpan(uniform), /*device=*/zeroCoord, /*local=*/zeroCoord,
1265                                    src, dst, &callbacks);
1266     }
1267 
1268 #if SK_SUPPORT_GPU
asFragmentProcessor(std::unique_ptr<GrFragmentProcessor> srcFP,std::unique_ptr<GrFragmentProcessor> dstFP,const GrFPArgs & args) const1269     std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(
1270             std::unique_ptr<GrFragmentProcessor> srcFP,
1271             std::unique_ptr<GrFragmentProcessor> dstFP,
1272             const GrFPArgs& args) const override {
1273         sk_sp<SkData> uniforms = get_xformed_uniforms(fEffect.get(), fUniforms,
1274                                                       args.fDstColorInfo->colorSpace());
1275         SkASSERT(uniforms);
1276         auto [success, fp] = make_effect_fp(fEffect,
1277                                             "runtime_blender",
1278                                             std::move(uniforms),
1279                                             std::move(srcFP),
1280                                             std::move(dstFP),
1281                                             SkMakeSpan(fChildren),
1282                                             args);
1283 
1284         return success ? std::move(fp) : nullptr;
1285     }
1286 #endif
1287 
flatten(SkWriteBuffer & buffer) const1288     void flatten(SkWriteBuffer& buffer) const override {
1289         buffer.writeString(fEffect->source().c_str());
1290         buffer.writeDataAsByteArray(fUniforms.get());
1291         write_child_effects(buffer, fChildren);
1292     }
1293 
1294     SK_FLATTENABLE_HOOKS(SkRuntimeBlender)
1295 
1296 private:
1297     using INHERITED = SkBlenderBase;
1298 
1299     sk_sp<SkRuntimeEffect> fEffect;
1300     sk_sp<SkData> fUniforms;
1301     std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1302 };
1303 
CreateProc(SkReadBuffer & buffer)1304 sk_sp<SkFlattenable> SkRuntimeBlender::CreateProc(SkReadBuffer& buffer) {
1305     SkString sksl;
1306     buffer.readString(&sksl);
1307     sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1308 
1309     auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForBlender, std::move(sksl));
1310     if (!buffer.validate(effect != nullptr)) {
1311         return nullptr;
1312     }
1313 
1314     SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1315     if (!read_child_effects(buffer, effect.get(), &children)) {
1316         return nullptr;
1317     }
1318 
1319     return effect->makeBlender(std::move(uniforms), SkMakeSpan(children));
1320 }
1321 
1322 ///////////////////////////////////////////////////////////////////////////////////////////////////
1323 
makeShader(sk_sp<SkData> uniforms,sk_sp<SkShader> childShaders[],size_t childCount,const SkMatrix * localMatrix) const1324 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<SkData> uniforms,
1325                                             sk_sp<SkShader> childShaders[],
1326                                             size_t childCount,
1327                                             const SkMatrix* localMatrix) const {
1328     SkSTArray<4, ChildPtr> children(childCount);
1329     for (size_t i = 0; i < childCount; ++i) {
1330         children.emplace_back(childShaders[i]);
1331     }
1332     return this->makeShader(std::move(uniforms), SkMakeSpan(children), localMatrix);
1333 }
1334 
makeShader(sk_sp<SkData> uniforms,SkSpan<ChildPtr> children,const SkMatrix * localMatrix) const1335 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<SkData> uniforms,
1336                                             SkSpan<ChildPtr> children,
1337                                             const SkMatrix* localMatrix) const {
1338     if (!this->allowShader()) {
1339         return nullptr;
1340     }
1341     if (!verify_child_effects(fChildren, children)) {
1342         return nullptr;
1343     }
1344     if (!uniforms) {
1345         uniforms = SkData::MakeEmpty();
1346     }
1347     if (uniforms->size() != this->uniformSize()) {
1348         return nullptr;
1349     }
1350     return sk_make_sp<SkRTShader>(sk_ref_sp(this), /*debugTrace=*/nullptr, std::move(uniforms),
1351                                   localMatrix, children);
1352 }
1353 
makeImage(GrRecordingContext * rContext,sk_sp<SkData> uniforms,SkSpan<ChildPtr> children,const SkMatrix * localMatrix,SkImageInfo resultInfo,bool mipmapped) const1354 sk_sp<SkImage> SkRuntimeEffect::makeImage(GrRecordingContext* rContext,
1355                                           sk_sp<SkData> uniforms,
1356                                           SkSpan<ChildPtr> children,
1357                                           const SkMatrix* localMatrix,
1358                                           SkImageInfo resultInfo,
1359                                           bool mipmapped) const {
1360     if (rContext) {
1361 #if SK_SUPPORT_GPU
1362         if (!rContext->priv().caps()->mipmapSupport()) {
1363             mipmapped = false;
1364         }
1365         auto fillContext = rContext->priv().makeSFC(resultInfo,
1366                                                     SkBackingFit::kExact,
1367                                                     /*sample count*/ 1,
1368                                                     GrMipmapped(mipmapped));
1369         if (!fillContext) {
1370             return nullptr;
1371         }
1372         uniforms = get_xformed_uniforms(this, std::move(uniforms), resultInfo.colorSpace());
1373         SkASSERT(uniforms);
1374 
1375         SkOverrideDeviceMatrixProvider matrixProvider(SkMatrix::I());
1376         GrColorInfo colorInfo(resultInfo.colorInfo());
1377         GrFPArgs args(rContext, matrixProvider, &colorInfo);
1378         SkSTArray<8, std::unique_ptr<GrFragmentProcessor>> childFPs;
1379         for (size_t i = 0; i < children.size(); ++i) {
1380             // TODO: add support for other types of child effects
1381             if (SkShader* shader = children[i].shader()) {
1382                 childFPs.push_back(as_SB(shader)->asFragmentProcessor(args));
1383             } else {
1384                 return nullptr;
1385             }
1386         }
1387         auto fp = GrSkSLFP::MakeWithData(sk_ref_sp(this),
1388                                          "runtime_image",
1389                                          colorInfo.refColorSpace(),
1390                                          /*inputFP=*/nullptr,
1391                                          /*destColorFP=*/nullptr,
1392                                          std::move(uniforms),
1393                                          SkMakeSpan(childFPs));
1394 
1395         if (localMatrix) {
1396             SkMatrix invLM;
1397             if (!localMatrix->invert(&invLM)) {
1398                 return nullptr;
1399             }
1400             fillContext->fillWithFP(invLM, std::move(fp));
1401         } else {
1402             fillContext->fillWithFP(std::move(fp));
1403         }
1404         return sk_sp<SkImage>(new SkImage_Gpu(sk_ref_sp(rContext),
1405                                               kNeedNewImageUniqueID,
1406                                               fillContext->readSurfaceView(),
1407                                               resultInfo.colorInfo()));
1408 #else
1409         return nullptr;
1410 #endif
1411     }
1412     if (resultInfo.alphaType() == kUnpremul_SkAlphaType) {
1413         // We don't have a good way of supporting this right now. In this case the runtime effect
1414         // will produce a unpremul value. The shader generated from it is assumed to produce
1415         // premul and RGB get pinned to A. Moreover, after the blend in premul the new dst is
1416         // unpremul'ed, producing a double unpremul result.
1417         return nullptr;
1418     }
1419     auto surf = SkSurface::MakeRaster(resultInfo);
1420     if (!surf) {
1421         return nullptr;
1422     }
1423     SkCanvas* canvas = surf->getCanvas();
1424     SkTLazy<SkCanvas> tempCanvas;
1425     auto shader = this->makeShader(std::move(uniforms), children, localMatrix);
1426     if (!shader) {
1427         return nullptr;
1428     }
1429     SkPaint paint;
1430     paint.setShader(std::move(shader));
1431     paint.setBlendMode(SkBlendMode::kSrc);
1432     canvas->drawPaint(paint);
1433     // TODO: Specify snapshot should have mip levels if mipmapped is true.
1434     return surf->makeImageSnapshot();
1435 }
1436 
makeColorFilter(sk_sp<SkData> uniforms,sk_sp<SkColorFilter> childColorFilters[],size_t childCount) const1437 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<SkData> uniforms,
1438                                                       sk_sp<SkColorFilter> childColorFilters[],
1439                                                       size_t childCount) const {
1440     SkSTArray<4, ChildPtr> children(childCount);
1441     for (size_t i = 0; i < childCount; ++i) {
1442         children.emplace_back(childColorFilters[i]);
1443     }
1444     return this->makeColorFilter(std::move(uniforms), SkMakeSpan(children));
1445 }
1446 
makeColorFilter(sk_sp<SkData> uniforms,SkSpan<ChildPtr> children) const1447 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<SkData> uniforms,
1448                                                       SkSpan<ChildPtr> children) const {
1449     if (!this->allowColorFilter()) {
1450         return nullptr;
1451     }
1452     if (!verify_child_effects(fChildren, children)) {
1453         return nullptr;
1454     }
1455     if (!uniforms) {
1456         uniforms = SkData::MakeEmpty();
1457     }
1458     if (uniforms->size() != this->uniformSize()) {
1459         return nullptr;
1460     }
1461     return sk_make_sp<SkRuntimeColorFilter>(sk_ref_sp(this), std::move(uniforms), children);
1462 }
1463 
makeColorFilter(sk_sp<SkData> uniforms) const1464 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<SkData> uniforms) const {
1465     return this->makeColorFilter(std::move(uniforms), /*children=*/{});
1466 }
1467 
makeBlender(sk_sp<SkData> uniforms,SkSpan<ChildPtr> children) const1468 sk_sp<SkBlender> SkRuntimeEffect::makeBlender(sk_sp<SkData> uniforms,
1469                                               SkSpan<ChildPtr> children) const {
1470     if (!this->allowBlender()) {
1471         return nullptr;
1472     }
1473     if (!verify_child_effects(fChildren, children)) {
1474         return nullptr;
1475     }
1476     if (!uniforms) {
1477         uniforms = SkData::MakeEmpty();
1478     }
1479     if (uniforms->size() != this->uniformSize()) {
1480         return nullptr;
1481     }
1482     return sk_make_sp<SkRuntimeBlender>(sk_ref_sp(this), std::move(uniforms), children);
1483 }
1484 
1485 ///////////////////////////////////////////////////////////////////////////////////////////////////
1486 
MakeTraced(sk_sp<SkShader> shader,const SkIPoint & traceCoord)1487 SkRuntimeEffect::TracedShader SkRuntimeEffect::MakeTraced(sk_sp<SkShader> shader,
1488                                                           const SkIPoint& traceCoord) {
1489     SkRuntimeEffect* effect = as_SB(shader)->asRuntimeEffect();
1490     if (!effect) {
1491         return TracedShader{nullptr, nullptr};
1492     }
1493     // An SkShader with an attached SkRuntimeEffect must be an SkRTShader.
1494     SkRTShader* rtShader = static_cast<SkRTShader*>(shader.get());
1495     return rtShader->makeTracedClone(traceCoord);
1496 }
1497 
1498 ///////////////////////////////////////////////////////////////////////////////////////////////////
1499 
type() const1500 std::optional<ChildType> SkRuntimeEffect::ChildPtr::type() const {
1501     if (fChild) {
1502         switch (fChild->getFlattenableType()) {
1503             case SkFlattenable::kSkShader_Type:
1504                 return ChildType::kShader;
1505             case SkFlattenable::kSkColorFilter_Type:
1506                 return ChildType::kColorFilter;
1507             case SkFlattenable::kSkBlender_Type:
1508                 return ChildType::kBlender;
1509             default:
1510                 break;
1511         }
1512     }
1513     return std::nullopt;
1514 }
1515 
shader() const1516 SkShader* SkRuntimeEffect::ChildPtr::shader() const {
1517     return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkShader_Type)
1518                    ? static_cast<SkShader*>(fChild.get())
1519                    : nullptr;
1520 }
1521 
colorFilter() const1522 SkColorFilter* SkRuntimeEffect::ChildPtr::colorFilter() const {
1523     return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkColorFilter_Type)
1524                    ? static_cast<SkColorFilter*>(fChild.get())
1525                    : nullptr;
1526 }
1527 
blender() const1528 SkBlender* SkRuntimeEffect::ChildPtr::blender() const {
1529     return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkBlender_Type)
1530                    ? static_cast<SkBlender*>(fChild.get())
1531                    : nullptr;
1532 }
1533 
1534 ///////////////////////////////////////////////////////////////////////////////////////////////////
1535 
RegisterFlattenables()1536 void SkRuntimeEffect::RegisterFlattenables() {
1537     SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter);
1538     SK_REGISTER_FLATTENABLE(SkRTShader);
1539     SK_REGISTER_FLATTENABLE(SkRuntimeBlender);
1540 }
1541 
SkRuntimeShaderBuilder(sk_sp<SkRuntimeEffect> effect)1542 SkRuntimeShaderBuilder::SkRuntimeShaderBuilder(sk_sp<SkRuntimeEffect> effect)
1543         : INHERITED(std::move(effect)) {}
1544 
1545 SkRuntimeShaderBuilder::~SkRuntimeShaderBuilder() = default;
1546 
makeImage(GrRecordingContext * recordingContext,const SkMatrix * localMatrix,SkImageInfo resultInfo,bool mipmapped)1547 sk_sp<SkImage> SkRuntimeShaderBuilder::makeImage(GrRecordingContext* recordingContext,
1548                                                  const SkMatrix* localMatrix,
1549                                                  SkImageInfo resultInfo,
1550                                                  bool mipmapped) {
1551     return this->effect()->makeImage(recordingContext,
1552                                      this->uniforms(),
1553                                      SkMakeSpan(this->children(), this->numChildren()),
1554                                      localMatrix,
1555                                      resultInfo,
1556                                      mipmapped);
1557 }
1558 
makeShader(const SkMatrix * localMatrix)1559 sk_sp<SkShader> SkRuntimeShaderBuilder::makeShader(const SkMatrix* localMatrix) {
1560     return this->effect()->makeShader(
1561             this->uniforms(), SkMakeSpan(this->children(), this->numChildren()), localMatrix);
1562 }
1563 
SkRuntimeBlendBuilder(sk_sp<SkRuntimeEffect> effect)1564 SkRuntimeBlendBuilder::SkRuntimeBlendBuilder(sk_sp<SkRuntimeEffect> effect)
1565         : INHERITED(std::move(effect)) {}
1566 
1567 SkRuntimeBlendBuilder::~SkRuntimeBlendBuilder() = default;
1568 
makeBlender()1569 sk_sp<SkBlender> SkRuntimeBlendBuilder::makeBlender() {
1570     return this->effect()->makeBlender(this->uniforms(),
1571                                        SkMakeSpan(this->children(), this->numChildren()));
1572 }
1573 
1574 #endif  // SK_ENABLE_SKSL
1575