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