2 * Copyright 2019 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/effects/SkRuntimeEffect.h"
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"
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"
52 #if defined(SK_BUILD_FOR_DEBUGGER)
53 #define SK_LENIENT_SKSL_DESERIALIZATION 1
55 #define SK_LENIENT_SKSL_DESERIALIZATION 0
60 using ChildType = SkRuntimeEffect::ChildType;
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:
74 SkRuntimeEffect::ChildPtr::ChildPtr(sk_sp<SkFlattenable> f) : fChild(std::move(f)) {
75 SkASSERT(flattenable_is_valid_as_child(fChild.get()));
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);
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; }
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; }
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;
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()) {
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) {
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).
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())) {
153 children->reserve_back(childCount);
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);
161 children->push_back(std::move(obj));
164 // If we are validating against an effect, make sure any (non-null) children are the right type
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);
176 return buffer.isValid();
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());
187 static std::vector<skvm::Val> make_skvm_uniforms(skvm::Builder* p,
188 skvm::Uniforms* uniforms,
190 const SkData& inputs) {
191 SkASSERTF(!(inputSize & 3), "inputSize was %zu, expected a multiple of 4", inputSize);
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) {
199 memcpy(&bits, data + index, sizeof(int32_t));
200 uniform.push_back(p->uniform32(uniforms->push(bits)).id);
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;
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__)}
219 SkRuntimeEffect::Result SkRuntimeEffect::MakeFromSource(SkString sksl,
220 const Options& options,
221 SkSL::ProgramKind kind) {
222 std::unique_ptr<SkSL::Program> program;
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);
232 RETURN_FAILURE("%s", compiler->errorText().c_str());
235 return MakeInternal(std::move(program), options, kind);
238 SkRuntimeEffect::Result SkRuntimeEffect::MakeInternal(std::unique_ptr<SkSL::Program> program,
239 const Options& options,
240 SkSL::ProgramKind kind) {
241 SkSL::SharedCompiler compiler;
243 // Find 'main', then locate the sample coords parameter. (It might not be present.)
244 const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main");
246 RETURN_FAILURE("missing 'main' function");
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;
252 const SkSL::ProgramUsage::VariableCounts sampleCoordsUsage =
253 iter != mainParams.end() ? program->usage()->get(**iter)
254 : SkSL::ProgramUsage::VariableCounts{};
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;
265 if (sampleCoordsUsage.fRead || sampleCoordsUsage.fWrite) {
266 flags |= kUsesSampleCoords_Flag;
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
272 if (flags & (kAllowColorFilter_Flag | kAllowBlender_Flag)) {
273 SkASSERT(!(flags & kUsesSampleCoords_Flag));
274 SkASSERT(!SkSL::Analysis::ReferencesFragCoords(*program));
277 if (SkSL::Analysis::CallsSampleOutsideMain(*program)) {
278 flags |= kSamplesOutsideMain_Flag;
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;
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;
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());
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>();
306 const SkSL::Variable& var = varDecl.var();
307 const SkSL::Type& varType = var.type();
309 // Child effects that can be sampled ('shader', 'colorFilter', 'blender')
310 if (varType.isEffectChild()) {
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());
325 // 'uniform' variables
326 else if (var.modifiers().fFlags & SkSL::Modifiers::kUniform_Flag) {
328 uni.name = SkString(var.name());
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();
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;
345 offset += uni.sizeInBytes();
346 SkASSERT(SkIsAlign4(offset));
348 uniforms.push_back(uni);
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;
361 #undef RETURN_FAILURE
363 sk_sp<SkRuntimeEffect> effect(new SkRuntimeEffect(std::move(program),
368 std::move(sampleUsages),
370 return Result{std::move(effect), SkString()};
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.
378 options.forceUnoptimized = true;
379 options.enforceES2Restrictions = false;
380 options.usePrivateRTShaderModule = true;
382 // We do know the original ProgramKind, so we don't need to re-derive it.
383 SkSL::ProgramKind kind = fBaseProgram->fConfig->fKind;
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;
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
393 SkSL::SharedCompiler compiler;
394 SkSL::Program::Settings settings = MakeSettings(options);
395 program = compiler->convertProgram(kind, *fBaseProgram->fSource, settings);
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);
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);
414 return result.effect;
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());
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());
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());
437 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (*make)(SkString sksl),
439 SK_BEGIN_REQUIRE_DENSE
444 bool operator==(const Key& that) const {
445 return this->skslHashA == that.skslHashA
446 && this->skslHashB == that.skslHashB;
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)) {}
455 static auto* mutex = new SkMutex;
456 static auto* cache = new SkLRUCache<Key, sk_sp<SkRuntimeEffect>>(11/*totally arbitrary*/);
460 SkAutoMutexExclusive _(*mutex);
461 if (sk_sp<SkRuntimeEffect>* found = cache->find(key)) {
466 auto [effect, err] = make(std::move(sksl));
470 SkASSERT(err.isEmpty());
473 SkAutoMutexExclusive _(*mutex);
474 cache->insert_or_update(key, effect);
479 static size_t uniform_element_size(SkRuntimeEffect::Uniform::Type 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;
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;
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;
498 size_t SkRuntimeEffect::Uniform::sizeInBytes() const {
499 static_assert(sizeof(int) == sizeof(float));
500 return uniform_element_size(this->type) * this->count;
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,
510 : fHash(SkOpts::hash_fn(baseProgram->fSource->c_str(), baseProgram->fSource->size(), 0))
511 , fBaseProgram(std::move(baseProgram))
513 , fUniforms(std::move(uniforms))
514 , fChildren(std::move(children))
515 , fSampleUsages(std::move(sampleUsages))
517 SkASSERT(fBaseProgram);
518 SkASSERT(fChildren.size() == fSampleUsages.size());
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;
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);
535 fFilterColorProgram = SkFilterColorProgram::Make(this);
538 SkRuntimeEffect::~SkRuntimeEffect() = default;
540 const std::string& SkRuntimeEffect::source() const {
541 return *fBaseProgram->fSource;
544 size_t SkRuntimeEffect::uniformSize() const {
545 return fUniforms.empty() ? 0
546 : SkAlign4(fUniforms.back().offset + fUniforms.back().sizeInBytes());
549 const SkRuntimeEffect::Uniform* SkRuntimeEffect::findUniform(const char* name) const {
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);
555 return iter == fUniforms.end() ? nullptr : &(*iter);
558 const SkRuntimeEffect::Child* SkRuntimeEffect::findChild(const char* name) const {
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);
564 return iter == fChildren.end() ? nullptr : &(*iter);
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()) {
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()) {
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;
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);
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;
616 class Callbacks : public SkSL::SkVMCallbacks {
618 Callbacks(skvm::Builder* builder,
619 const skvm::Uniforms* skslUniforms,
620 skvm::Uniforms* childColorUniforms,
621 skvm::Color inputColor,
622 std::vector<SkFilterColorProgram::SampleCall>* sampleCalls)
624 , fSkslUniforms(skslUniforms)
625 , fChildColorUniforms(childColorUniforms)
626 , fInputColor(inputColor)
627 , fSampleCalls(sampleCalls) {}
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)) {
634 skvm::Ptr uniPtr = fSkslUniforms->base;
635 if (ur.ptr != uniPtr || ug.ptr != uniPtr || ub.ptr != uniPtr || ua.ptr != uniPtr) {
638 *baseOffset = ur.offset;
639 return ug.offset == ur.offset + 4 &&
640 ub.offset == ur.offset + 8 &&
641 ua.offset == ur.offset + 12;
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;
648 skvm::Color sampleColorFilter(int ix, skvm::Color c) override {
650 fBuilder->uniformColor(/*placeholder*/ SkColors::kWhite, fChildColorUniforms);
651 SkFilterColorProgram::SampleCall call;
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(),
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;
669 fAllSampleCallsSupported = false;
671 fSampleCalls->push_back(call);
672 fChildColors.push_back(result);
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");
682 skvm::Color sampleBlender(int, skvm::Color, skvm::Color) override {
683 SkDEBUGFAIL("Unexpected child type");
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");
693 skvm::Color fromLinearSrgb(skvm::Color color) override {
694 SkDEBUGFAIL("Unexpected color transform intrinsic");
698 skvm::Builder* fBuilder;
699 const skvm::Uniforms* fSkslUniforms;
700 skvm::Uniforms* fChildColorUniforms;
701 skvm::Color fInputColor;
702 std::vector<SkFilterColorProgram::SampleCall>* fSampleCalls;
704 std::vector<skvm::Color> fChildColors;
705 bool fAllSampleCallsSupported = true;
707 Callbacks callbacks(&p, &skslUniforms, &childColorUniforms, inputColor, &sampleCalls);
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,
714 /*debugTrace=*/nullptr,
716 /*device=*/zeroCoord,
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);
726 if (!callbacks.fAllSampleCallsSupported) {
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);
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),
741 SkFilterColorProgram::SkFilterColorProgram(skvm::Program program,
742 std::vector<SampleCall> sampleCalls,
744 : fProgram(std::move(program))
745 , fSampleCalls(std::move(sampleCalls))
746 , fAlphaUnchanged(alphaUnchanged) {}
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;
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);
768 childColors.push_back(evalChild(s.fChild, passedColor));
772 fProgram.eval(1, uniformData, childColors.begin(), result.vec());
776 const SkFilterColorProgram* SkRuntimeEffect::getFilterColorProgram() {
777 return fFilterColorProgram.get();
780 ///////////////////////////////////////////////////////////////////////////////////////////////////
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);
790 sk_sp<SkData> uniforms = nullptr;
791 auto writableData = [&]() {
793 uniforms = SkData::MakeWithCopy(baseUniforms->data(), baseUniforms->size());
795 return uniforms->writable_data();
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) {
805 for (int i = 0; i < v.count; ++i) {
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.
814 for (int i = 0; i < v.count; ++i) {
815 memcpy(rgba, color, 3 * sizeof(float));
818 memcpy(color, rgba, 3 * sizeof(float));
825 return uniforms ? uniforms : baseUniforms;
829 static GrFPResult make_effect_fp(sk_sp<SkRuntimeEffect> effect,
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);
843 return GrFPFailure(std::move(inputFP));
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,
851 *childArgs.fDstColorInfo);
853 return GrFPFailure(std::move(inputFP));
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(
860 GrFragmentProcessor::UseDestColorAsInput(/*dstFP=*/nullptr),
863 return GrFPFailure(std::move(inputFP));
865 childFPs.push_back(std::move(childFP));
867 // We have a null child effect.
868 childFPs.push_back(nullptr);
871 auto fp = GrSkSLFP::MakeWithData(std::move(effect),
873 childArgs.fDstColorInfo->refColorSpace(),
875 std::move(destColorFP),
877 SkMakeSpan(childFPs));
879 return GrFPSuccess(std::move(fp));
883 class RuntimeEffectVMCallbacks : public SkSL::SkVMCallbacks {
885 RuntimeEffectVMCallbacks(skvm::Builder* builder,
886 skvm::Uniforms* uniforms,
888 const std::vector<SkRuntimeEffect::ChildPtr>& children,
890 const SkColorInfo& colorInfo)
892 , fUniforms(uniforms)
894 , fChildren(children)
896 , fColorInfo(colorInfo) {}
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);
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);
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);
918 return blend(SkBlendMode::kSrcOver, src, dst);
921 skvm::Color toLinearSrgb(skvm::Color color) override {
922 if (!fColorInfo.colorSpace()) {
923 // These intrinsics do nothing when color management is disabled
926 return SkColorSpaceXformSteps{fColorInfo.colorSpace(), kUnpremul_SkAlphaType,
927 sk_srgb_linear_singleton(), kUnpremul_SkAlphaType}
928 .program(fBuilder, fUniforms, color);
931 skvm::Color fromLinearSrgb(skvm::Color color) override {
932 if (!fColorInfo.colorSpace()) {
933 // These intrinsics do nothing when color management is disabled
936 return SkColorSpaceXformSteps{sk_srgb_linear_singleton(), kUnpremul_SkAlphaType,
937 fColorInfo.colorSpace(), kUnpremul_SkAlphaType}
938 .program(fBuilder, fUniforms, color);
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;
949 class SkRuntimeColorFilter : public SkColorFilterBase {
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()) {}
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());
966 SkOverrideDeviceMatrixProvider matrixProvider(SkMatrix::I());
967 GrFPArgs childArgs(context, matrixProvider, &colorInfo);
968 return make_effect_fp(fEffect,
969 "runtime_color_filter",
972 /*destColorFP=*/nullptr,
973 SkMakeSpan(fChildren),
978 bool onAppendStages(const SkStageRec& rec, bool shaderIsOpaque) const override {
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());
989 RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, c, colorInfo);
990 std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
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,
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();
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);
1010 // Get our specific uniform values
1011 sk_sp<SkData> inputs = get_xformed_uniforms(fEffect.get(), fUniforms, dstCS);
1014 auto evalChild = [&](int index, SkPMColor4f inColor) {
1015 const auto& child = fChildren[index];
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);
1026 return program->eval(color, inputs->data(), evalChild);
1029 bool onIsAlphaUnchanged() const override {
1030 return fEffect->getFilterColorProgram() &&
1031 fEffect->getFilterColorProgram()->isAlphaUnchanged();
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);
1040 SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
1042 SK_FLATTENABLE_HOOKS(SkRuntimeColorFilter)
1045 sk_sp<SkRuntimeEffect> fEffect;
1046 sk_sp<SkData> fUniforms;
1047 std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1050 sk_sp<SkFlattenable> SkRuntimeColorFilter::CreateProc(SkReadBuffer& buffer) {
1052 buffer.readString(&sksl);
1053 sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1055 auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForColorFilter, std::move(sksl));
1056 #if !SK_LENIENT_SKSL_DESERIALIZATION
1057 if (!buffer.validate(effect != nullptr)) {
1062 SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1063 if (!read_child_effects(buffer, effect.get(), &children)) {
1067 #if SK_LENIENT_SKSL_DESERIALIZATION
1069 SkDebugf("Serialized SkSL failed to compile. Ignoring/dropping SkSL color filter.\n");
1074 return effect->makeColorFilter(std::move(uniforms), SkMakeSpan(children));
1077 ///////////////////////////////////////////////////////////////////////////////////////////////////
1079 class SkRTShader : public SkShaderBase {
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()) {}
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));
1098 return SkRuntimeEffect::TracedShader{std::move(debugShader), std::move(debugTrace)};
1101 bool isOpaque() const override { return fEffect->alwaysOpaque(); }
1104 std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs& args) const override {
1106 if (!this->totalLocalMatrix(args.fPreLocalMatrix)->invert(&matrix)) {
1110 sk_sp<SkData> uniforms =
1111 get_xformed_uniforms(fEffect.get(), fUniforms, args.fDstColorInfo->colorSpace());
1114 auto [success, fp] = make_effect_fp(fEffect,
1116 std::move(uniforms),
1117 /*inputFP=*/nullptr,
1118 /*destColorFP=*/nullptr,
1119 SkMakeSpan(fChildren),
1125 return GrMatrixEffect::Make(matrix, std::move(fp));
1129 bool onAppendStages(const SkStageRec& rec) const override {
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());
1143 if (!this->computeTotalInverse(matrices.localToDevice(), localM, &inv)) {
1146 local = SkShaderBase::ApplyMatrix(p,inv,local,uniforms);
1148 RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, paint, colorInfo);
1149 std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
1152 return SkSL::ProgramToSkVM(*fEffect->fBaseProgram, fEffect->fMain, p, fDebugTrace.get(),
1153 SkMakeSpan(uniform), device, local, paint, paint, &callbacks);
1156 void flatten(SkWriteBuffer& buffer) const override {
1158 if (!this->getLocalMatrix().isIdentity()) {
1159 flags |= kHasLocalMatrix_Flag;
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());
1168 write_child_effects(buffer, fChildren);
1171 SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
1173 SK_FLATTENABLE_HOOKS(SkRTShader)
1177 kHasLocalMatrix_Flag = 1 << 1,
1180 sk_sp<SkRuntimeEffect> fEffect;
1181 sk_sp<SkSL::SkVMDebugTrace> fDebugTrace;
1183 sk_sp<SkData> fUniforms;
1184 std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1187 sk_sp<SkFlattenable> SkRTShader::CreateProc(SkReadBuffer& buffer) {
1189 buffer.readString(&sksl);
1190 sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1191 uint32_t flags = buffer.read32();
1193 SkMatrix localM, *localMPtr = nullptr;
1194 if (flags & kHasLocalMatrix_Flag) {
1195 buffer.readMatrix(&localM);
1196 localMPtr = &localM;
1199 auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForShader, std::move(sksl));
1200 #if !SK_LENIENT_SKSL_DESERIALIZATION
1201 if (!buffer.validate(effect != nullptr)) {
1206 SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1207 if (!read_child_effects(buffer, effect.get(), &children)) {
1211 #if SK_LENIENT_SKSL_DESERIALIZATION
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());
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");
1227 return effect->makeShader(std::move(uniforms), SkMakeSpan(children), localMPtr);
1230 ///////////////////////////////////////////////////////////////////////////////////////////////////
1232 class SkRuntimeBlender : public SkBlenderBase {
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()) {}
1241 SkRuntimeEffect* asRuntimeEffect() const override { return fEffect.get(); }
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());
1250 RuntimeEffectVMCallbacks callbacks(p, uniforms, alloc, fChildren, src, colorInfo);
1251 std::vector<skvm::Val> uniform = make_skvm_uniforms(p, uniforms, fEffect->uniformSize(),
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);
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());
1269 auto [success, fp] = make_effect_fp(fEffect,
1271 std::move(uniforms),
1274 SkMakeSpan(fChildren),
1277 return success ? std::move(fp) : nullptr;
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);
1287 SK_FLATTENABLE_HOOKS(SkRuntimeBlender)
1290 using INHERITED = SkBlenderBase;
1292 sk_sp<SkRuntimeEffect> fEffect;
1293 sk_sp<SkData> fUniforms;
1294 std::vector<SkRuntimeEffect::ChildPtr> fChildren;
1297 sk_sp<SkFlattenable> SkRuntimeBlender::CreateProc(SkReadBuffer& buffer) {
1299 buffer.readString(&sksl);
1300 sk_sp<SkData> uniforms = buffer.readByteArrayAsData();
1302 auto effect = SkMakeCachedRuntimeEffect(SkRuntimeEffect::MakeForBlender, std::move(sksl));
1303 #if !SK_LENIENT_SKSL_DESERIALIZATION
1304 if (!buffer.validate(effect != nullptr)) {
1309 SkSTArray<4, SkRuntimeEffect::ChildPtr> children;
1310 if (!read_child_effects(buffer, effect.get(), &children)) {
1314 #if SK_LENIENT_SKSL_DESERIALIZATION
1316 SkDebugf("Serialized SkSL failed to compile. Ignoring/dropping SkSL blender.\n");
1321 return effect->makeBlender(std::move(uniforms), SkMakeSpan(children));
1324 ///////////////////////////////////////////////////////////////////////////////////////////////////
1326 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<SkData> uniforms,
1327 sk_sp<SkShader> childShaders[],
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]);
1334 return this->makeShader(std::move(uniforms), SkMakeSpan(children), localMatrix);
1337 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<SkData> uniforms,
1338 SkSpan<ChildPtr> children,
1339 const SkMatrix* localMatrix) const {
1340 if (!this->allowShader()) {
1343 if (!verify_child_effects(fChildren, children)) {
1347 uniforms = SkData::MakeEmpty();
1349 if (uniforms->size() != this->uniformSize()) {
1352 return sk_make_sp<SkRTShader>(sk_ref_sp(this), /*debugTrace=*/nullptr, std::move(uniforms),
1353 localMatrix, children);
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 {
1364 if (!rContext->priv().caps()->mipmapSupport()) {
1367 auto fillContext = rContext->priv().makeSFC(resultInfo,
1368 SkBackingFit::kExact,
1370 GrMipmapped(mipmapped));
1374 uniforms = get_xformed_uniforms(this, std::move(uniforms), resultInfo.colorSpace());
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));
1389 auto fp = GrSkSLFP::MakeWithData(sk_ref_sp(this),
1391 colorInfo.refColorSpace(),
1392 /*inputFP=*/nullptr,
1393 /*destColorFP=*/nullptr,
1394 std::move(uniforms),
1395 SkMakeSpan(childFPs));
1399 if (!localMatrix->invert(&invLM)) {
1402 fillContext->fillWithFP(invLM, std::move(fp));
1404 fillContext->fillWithFP(std::move(fp));
1406 return sk_sp<SkImage>(new SkImage_Gpu(sk_ref_sp(rContext),
1407 kNeedNewImageUniqueID,
1408 fillContext->readSurfaceView(),
1409 resultInfo.colorInfo()));
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.
1421 auto surf = SkSurface::MakeRaster(resultInfo);
1425 SkCanvas* canvas = surf->getCanvas();
1426 SkTLazy<SkCanvas> tempCanvas;
1427 auto shader = this->makeShader(std::move(uniforms), children, localMatrix);
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();
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]);
1446 return this->makeColorFilter(std::move(uniforms), SkMakeSpan(children));
1449 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<SkData> uniforms,
1450 SkSpan<ChildPtr> children) const {
1451 if (!this->allowColorFilter()) {
1454 if (!verify_child_effects(fChildren, children)) {
1458 uniforms = SkData::MakeEmpty();
1460 if (uniforms->size() != this->uniformSize()) {
1463 return sk_make_sp<SkRuntimeColorFilter>(sk_ref_sp(this), std::move(uniforms), children);
1466 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<SkData> uniforms) const {
1467 return this->makeColorFilter(std::move(uniforms), /*children=*/{});
1470 sk_sp<SkBlender> SkRuntimeEffect::makeBlender(sk_sp<SkData> uniforms,
1471 SkSpan<ChildPtr> children) const {
1472 if (!this->allowBlender()) {
1475 if (!verify_child_effects(fChildren, children)) {
1479 uniforms = SkData::MakeEmpty();
1481 if (uniforms->size() != this->uniformSize()) {
1484 return sk_make_sp<SkRuntimeBlender>(sk_ref_sp(this), std::move(uniforms), children);
1487 ///////////////////////////////////////////////////////////////////////////////////////////////////
1489 SkRuntimeEffect::TracedShader SkRuntimeEffect::MakeTraced(sk_sp<SkShader> shader,
1490 const SkIPoint& traceCoord) {
1491 SkRuntimeEffect* effect = as_SB(shader)->asRuntimeEffect();
1493 return TracedShader{nullptr, nullptr};
1495 // An SkShader with an attached SkRuntimeEffect must be an SkRTShader.
1496 SkRTShader* rtShader = static_cast<SkRTShader*>(shader.get());
1497 return rtShader->makeTracedClone(traceCoord);
1500 ///////////////////////////////////////////////////////////////////////////////////////////////////
1502 std::optional<ChildType> SkRuntimeEffect::ChildPtr::type() const {
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;
1515 return std::nullopt;
1518 SkShader* SkRuntimeEffect::ChildPtr::shader() const {
1519 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkShader_Type)
1520 ? static_cast<SkShader*>(fChild.get())
1524 SkColorFilter* SkRuntimeEffect::ChildPtr::colorFilter() const {
1525 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkColorFilter_Type)
1526 ? static_cast<SkColorFilter*>(fChild.get())
1530 SkBlender* SkRuntimeEffect::ChildPtr::blender() const {
1531 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkBlender_Type)
1532 ? static_cast<SkBlender*>(fChild.get())
1536 ///////////////////////////////////////////////////////////////////////////////////////////////////
1538 void SkRuntimeEffect::RegisterFlattenables() {
1539 SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter);
1540 SK_REGISTER_FLATTENABLE(SkRTShader);
1541 SK_REGISTER_FLATTENABLE(SkRuntimeBlender);
1544 SkRuntimeShaderBuilder::SkRuntimeShaderBuilder(sk_sp<SkRuntimeEffect> effect)
1545 : INHERITED(std::move(effect)) {}
1547 SkRuntimeShaderBuilder::~SkRuntimeShaderBuilder() = default;
1549 sk_sp<SkImage> SkRuntimeShaderBuilder::makeImage(GrRecordingContext* recordingContext,
1550 const SkMatrix* localMatrix,
1551 SkImageInfo resultInfo,
1553 return this->effect()->makeImage(recordingContext,
1555 SkMakeSpan(this->children(), this->numChildren()),
1561 sk_sp<SkShader> SkRuntimeShaderBuilder::makeShader(const SkMatrix* localMatrix) {
1562 return this->effect()->makeShader(
1563 this->uniforms(), SkMakeSpan(this->children(), this->numChildren()), localMatrix);
1566 SkRuntimeBlendBuilder::SkRuntimeBlendBuilder(sk_sp<SkRuntimeEffect> effect)
1567 : INHERITED(std::move(effect)) {}
1569 SkRuntimeBlendBuilder::~SkRuntimeBlendBuilder() = default;
1571 sk_sp<SkBlender> SkRuntimeBlendBuilder::makeBlender() {
1572 return this->effect()->makeBlender(this->uniforms(),
1573 SkMakeSpan(this->children(), this->numChildren()));
1576 #endif // SK_ENABLE_SKSL