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