1 /*
2 * Copyright 2019 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/effects/SkRuntimeEffect.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBlender.h"
12 #include "include/core/SkCapabilities.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkColorFilter.h"
15 #include "include/core/SkData.h"
16 #include "include/private/base/SkAlign.h"
17 #include "include/private/base/SkDebug.h"
18 #include "include/private/base/SkMutex.h"
19 #include "include/private/base/SkOnce.h"
20 #include "include/private/base/SkTArray.h"
21 #include "src/base/SkArenaAlloc.h"
22 #include "src/base/SkEnumBitMask.h"
23 #include "src/base/SkNoDestructor.h"
24 #include "src/core/SkBlenderBase.h"
25 #include "src/core/SkChecksum.h"
26 #include "src/core/SkColorSpacePriv.h"
27 #include "src/core/SkColorSpaceXformSteps.h"
28 #include "src/core/SkEffectPriv.h"
29 #include "src/core/SkLRUCache.h"
30 #include "src/core/SkRasterPipeline.h"
31 #include "src/core/SkRasterPipelineOpList.h"
32 #include "src/core/SkReadBuffer.h"
33 #include "src/core/SkRuntimeBlender.h"
34 #include "src/core/SkRuntimeEffectPriv.h"
35 #include "src/core/SkStreamPriv.h"
36 #include "src/core/SkWriteBuffer.h"
37 #include "src/effects/colorfilters/SkColorFilterBase.h"
38 #include "src/effects/colorfilters/SkRuntimeColorFilter.h"
39 #include "src/shaders/SkLocalMatrixShader.h"
40 #include "src/shaders/SkRuntimeShader.h"
41 #include "src/shaders/SkShaderBase.h"
42 #include "src/sksl/SkSLAnalysis.h"
43 #include "src/sksl/SkSLBuiltinTypes.h"
44 #include "src/sksl/SkSLCompiler.h"
45 #include "src/sksl/SkSLContext.h"
46 #include "src/sksl/SkSLDefines.h"
47 #include "src/sksl/SkSLProgramKind.h"
48 #include "src/sksl/SkSLProgramSettings.h"
49 #include "src/sksl/analysis/SkSLProgramUsage.h"
50 #include "src/sksl/codegen/SkSLRasterPipelineBuilder.h"
51 #include "src/sksl/codegen/SkSLRasterPipelineCodeGenerator.h"
52 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
53 #include "src/sksl/ir/SkSLLayout.h"
54 #include "src/sksl/ir/SkSLModifierFlags.h"
55 #include "src/sksl/ir/SkSLProgram.h"
56 #include "src/sksl/ir/SkSLProgramElement.h"
57 #include "src/sksl/ir/SkSLStatement.h"
58 #include "src/sksl/ir/SkSLType.h"
59 #include "src/sksl/ir/SkSLVarDeclarations.h"
60 #include "src/sksl/ir/SkSLVariable.h"
61 #include "src/sksl/tracing/SkSLDebugTracePriv.h"
62 #include "src/sksl/transform/SkSLTransform.h"
63
64 #include <algorithm>
65
66 using namespace skia_private;
67
68 class SkColorSpace;
69 struct SkIPoint;
70
71 constexpr bool kRPEnableLiveTrace = false;
72
73 using ChildType = SkRuntimeEffect::ChildType;
74
init_uniform_type(const SkSL::Context & ctx,const SkSL::Type * type,SkRuntimeEffect::Uniform * v)75 static bool init_uniform_type(const SkSL::Context& ctx,
76 const SkSL::Type* type,
77 SkRuntimeEffect::Uniform* v) {
78 using Type = SkRuntimeEffect::Uniform::Type;
79 if (type->matches(*ctx.fTypes.fFloat)) { v->type = Type::kFloat; return true; }
80 if (type->matches(*ctx.fTypes.fHalf)) { v->type = Type::kFloat; return true; }
81 if (type->matches(*ctx.fTypes.fFloat2)) { v->type = Type::kFloat2; return true; }
82 if (type->matches(*ctx.fTypes.fHalf2)) { v->type = Type::kFloat2; return true; }
83 if (type->matches(*ctx.fTypes.fFloat3)) { v->type = Type::kFloat3; return true; }
84 if (type->matches(*ctx.fTypes.fHalf3)) { v->type = Type::kFloat3; return true; }
85 if (type->matches(*ctx.fTypes.fFloat4)) { v->type = Type::kFloat4; return true; }
86 if (type->matches(*ctx.fTypes.fHalf4)) { v->type = Type::kFloat4; return true; }
87 if (type->matches(*ctx.fTypes.fFloat2x2)) { v->type = Type::kFloat2x2; return true; }
88 if (type->matches(*ctx.fTypes.fHalf2x2)) { v->type = Type::kFloat2x2; return true; }
89 if (type->matches(*ctx.fTypes.fFloat3x3)) { v->type = Type::kFloat3x3; return true; }
90 if (type->matches(*ctx.fTypes.fHalf3x3)) { v->type = Type::kFloat3x3; return true; }
91 if (type->matches(*ctx.fTypes.fFloat4x4)) { v->type = Type::kFloat4x4; return true; }
92 if (type->matches(*ctx.fTypes.fHalf4x4)) { v->type = Type::kFloat4x4; return true; }
93
94 if (type->matches(*ctx.fTypes.fInt)) { v->type = Type::kInt; return true; }
95 if (type->matches(*ctx.fTypes.fInt2)) { v->type = Type::kInt2; return true; }
96 if (type->matches(*ctx.fTypes.fInt3)) { v->type = Type::kInt3; return true; }
97 if (type->matches(*ctx.fTypes.fInt4)) { v->type = Type::kInt4; return true; }
98
99 return false;
100 }
101
VarAsUniform(const SkSL::Variable & var,const SkSL::Context & context,size_t * offset)102 SkRuntimeEffect::Uniform SkRuntimeEffectPriv::VarAsUniform(const SkSL::Variable& var,
103 const SkSL::Context& context,
104 size_t* offset) {
105 using Uniform = SkRuntimeEffect::Uniform;
106 SkASSERT(var.modifierFlags().isUniform());
107 Uniform uni;
108 uni.name = var.name();
109 uni.flags = 0;
110 uni.count = 1;
111
112 const SkSL::Type* type = &var.type();
113 if (type->isArray()) {
114 uni.flags |= Uniform::kArray_Flag;
115 uni.count = type->columns();
116 type = &type->componentType();
117 }
118
119 if (type->hasPrecision() && !type->highPrecision()) {
120 uni.flags |= Uniform::kHalfPrecision_Flag;
121 }
122
123 SkAssertResult(init_uniform_type(context, type, &uni));
124 if (var.layout().fFlags & SkSL::LayoutFlag::kColor) {
125 uni.flags |= Uniform::kColor_Flag;
126 }
127
128 uni.offset = *offset;
129 *offset += uni.sizeInBytes();
130 SkASSERT(SkIsAlign4(*offset));
131 return uni;
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
ChildTypeToStr(ChildType type)143 const char* SkRuntimeEffectPriv::ChildTypeToStr(ChildType type) {
144 switch (type) {
145 case ChildType::kBlender: return "blender";
146 case ChildType::kColorFilter: return "color filter";
147 case ChildType::kShader: return "shader";
148 default: SkUNREACHABLE;
149 }
150 }
151
VarAsChild(const SkSL::Variable & var,int index)152 SkRuntimeEffect::Child SkRuntimeEffectPriv::VarAsChild(const SkSL::Variable& var, int index) {
153 SkRuntimeEffect::Child c;
154 c.name = var.name();
155 c.type = child_type(var.type());
156 c.index = index;
157 return c;
158 }
159
TransformUniforms(SkSpan<const SkRuntimeEffect::Uniform> uniforms,sk_sp<const SkData> originalData,const SkColorSpace * dstCS)160 sk_sp<const SkData> SkRuntimeEffectPriv::TransformUniforms(
161 SkSpan<const SkRuntimeEffect::Uniform> uniforms,
162 sk_sp<const SkData> originalData,
163 const SkColorSpace* dstCS) {
164 if (!dstCS) {
165 // There's no destination color-space; we can early-out immediately.
166 return originalData;
167 }
168 SkColorSpaceXformSteps steps(sk_srgb_singleton(), kUnpremul_SkAlphaType,
169 dstCS, kUnpremul_SkAlphaType);
170 return TransformUniforms(uniforms, std::move(originalData), steps);
171 }
172
TransformUniforms(SkSpan<const SkRuntimeEffect::Uniform> uniforms,sk_sp<const SkData> originalData,const SkColorSpaceXformSteps & steps)173 sk_sp<const SkData> SkRuntimeEffectPriv::TransformUniforms(
174 SkSpan<const SkRuntimeEffect::Uniform> uniforms,
175 sk_sp<const SkData> originalData,
176 const SkColorSpaceXformSteps& steps) {
177 using Flags = SkRuntimeEffect::Uniform::Flags;
178 using Type = SkRuntimeEffect::Uniform::Type;
179
180 sk_sp<SkData> data = nullptr;
181 auto writableData = [&]() {
182 if (!data) {
183 data = SkData::MakeWithCopy(originalData->data(), originalData->size());
184 }
185 return data->writable_data();
186 };
187
188 for (const auto& u : uniforms) {
189 if (u.flags & Flags::kColor_Flag) {
190 SkASSERT(u.type == Type::kFloat3 || u.type == Type::kFloat4);
191 if (steps.flags.mask()) {
192 float* color = SkTAddOffset<float>(writableData(), u.offset);
193 if (u.type == Type::kFloat4) {
194 // RGBA, easy case
195 for (int i = 0; i < u.count; ++i) {
196 steps.apply(color);
197 color += 4;
198 }
199 } else {
200 // RGB, need to pad out to include alpha. Technically, this isn't necessary,
201 // because steps shouldn't include unpremul or premul, and thus shouldn't
202 // read or write the fourth element. But let's be safe.
203 float rgba[4];
204 for (int i = 0; i < u.count; ++i) {
205 memcpy(rgba, color, 3 * sizeof(float));
206 rgba[3] = 1.0f;
207 steps.apply(rgba);
208 memcpy(color, rgba, 3 * sizeof(float));
209 color += 3;
210 }
211 }
212 }
213 }
214 }
215 return data ? data : originalData;
216 }
217
getRPProgram(SkSL::DebugTracePriv * debugTrace) const218 const SkSL::RP::Program* SkRuntimeEffect::getRPProgram(SkSL::DebugTracePriv* debugTrace) const {
219 // Lazily compile the program the first time `getRPProgram` is called.
220 // By using an SkOnce, we avoid thread hazards and behave in a conceptually const way, but we
221 // can avoid the cost of invoking the RP code generator until it's actually needed.
222 fCompileRPProgramOnce([&] {
223 // We generally do not run the inliner when an SkRuntimeEffect program is initially created,
224 // because the final compile to native shader code will do this. However, in SkRP, there's
225 // no additional compilation occurring, so we need to manually inline here if we want the
226 // performance boost of inlining.
227 if (!(fFlags & kDisableOptimization_Flag)) {
228 SkSL::Compiler compiler;
229 fBaseProgram->fConfig->fSettings.fInlineThreshold = SkSL::kDefaultInlineThreshold;
230 compiler.runInliner(*fBaseProgram);
231
232 // After inlining, the program is likely to have dead functions left behind.
233 while (SkSL::Transform::EliminateDeadFunctions(*fBaseProgram)) {
234 // Removing dead functions may cause more functions to become unreferenced.
235 }
236 }
237
238 SkSL::DebugTracePriv tempDebugTrace;
239 if (debugTrace) {
240 const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram(
241 *fBaseProgram, fMain, debugTrace, /*writeTraceOps=*/true);
242 } else if (kRPEnableLiveTrace) {
243 debugTrace = &tempDebugTrace;
244 const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram(
245 *fBaseProgram, fMain, debugTrace, /*writeTraceOps=*/false);
246 } else {
247 const_cast<SkRuntimeEffect*>(this)->fRPProgram = MakeRasterPipelineProgram(
248 *fBaseProgram, fMain, /*debugTrace=*/nullptr, /*writeTraceOps=*/false);
249 }
250
251 if (kRPEnableLiveTrace) {
252 if (fRPProgram) {
253 SkDebugf("-----\n\n");
254 SkDebugfStream stream;
255 fRPProgram->dump(&stream, /*writeInstructionCount=*/true);
256 SkDebugf("\n-----\n\n");
257 } else {
258 SkDebugf("----- RP unsupported -----\n\n");
259 }
260 }
261 });
262
263 return fRPProgram.get();
264 }
265
UniformsAsSpan(SkSpan<const SkRuntimeEffect::Uniform> uniforms,sk_sp<const SkData> originalData,bool alwaysCopyIntoAlloc,const SkColorSpace * destColorSpace,SkArenaAlloc * alloc)266 SkSpan<const float> SkRuntimeEffectPriv::UniformsAsSpan(
267 SkSpan<const SkRuntimeEffect::Uniform> uniforms,
268 sk_sp<const SkData> originalData,
269 bool alwaysCopyIntoAlloc,
270 const SkColorSpace* destColorSpace,
271 SkArenaAlloc* alloc) {
272 // Transform the uniforms into the destination colorspace.
273 sk_sp<const SkData> transformedData = SkRuntimeEffectPriv::TransformUniforms(uniforms,
274 originalData,
275 destColorSpace);
276 if (alwaysCopyIntoAlloc || originalData != transformedData) {
277 // The transformed uniform data's lifetime is not long enough to reuse; instead, we copy the
278 // uniform data directly into the alloc.
279 int numBytes = transformedData->size();
280 int numFloats = numBytes / sizeof(float);
281 float* uniformsInAlloc = alloc->makeArrayDefault<float>(numFloats);
282 memcpy(uniformsInAlloc, transformedData->data(), numBytes);
283 return SkSpan{uniformsInAlloc, numFloats};
284 }
285 // It's safe to return a pointer into existing data.
286 return SkSpan{static_cast<const float*>(originalData->data()),
287 originalData->size() / sizeof(float)};
288 }
289
appendShader(int index)290 bool RuntimeEffectRPCallbacks::appendShader(int index) {
291 if (SkShader* shader = fChildren[index].shader()) {
292 if (fSampleUsages[index].isPassThrough()) {
293 // Given a passthrough sample, the total-matrix is still as valid as before.
294 return as_SB(shader)->appendStages(fStage, fMatrix);
295 }
296 // For a non-passthrough sample, we need to explicitly mark the total-matrix as invalid.
297 SkShaders::MatrixRec nonPassthroughMatrix = fMatrix;
298 nonPassthroughMatrix.markTotalMatrixInvalid();
299 return as_SB(shader)->appendStages(fStage, nonPassthroughMatrix);
300 }
301 // Return transparent black when a null shader is evaluated.
302 fStage.fPipeline->appendConstantColor(fStage.fAlloc, SkColors::kTransparent);
303 return true;
304 }
appendColorFilter(int index)305 bool RuntimeEffectRPCallbacks::appendColorFilter(int index) {
306 if (SkColorFilter* colorFilter = fChildren[index].colorFilter()) {
307 return as_CFB(colorFilter)->appendStages(fStage, /*shaderIsOpaque=*/false);
308 }
309 // Return the original color as-is when a null child color filter is evaluated.
310 return true;
311 }
appendBlender(int index)312 bool RuntimeEffectRPCallbacks::appendBlender(int index) {
313 if (SkBlender* blender = fChildren[index].blender()) {
314 return as_BB(blender)->appendStages(fStage);
315 }
316 // Return a source-over blend when a null blender is evaluated.
317 fStage.fPipeline->append(SkRasterPipelineOp::srcover);
318 return true;
319 }
320
321 // TODO: If an effect calls these intrinsics more than once, we could cache and re-use the steps
322 // object(s), rather than re-creating them in the arena repeatedly.
toLinearSrgb(const void * color)323 void RuntimeEffectRPCallbacks::toLinearSrgb(const void* color) {
324 if (fStage.fDstCS) {
325 SkColorSpaceXformSteps xform{fStage.fDstCS, kUnpremul_SkAlphaType,
326 sk_srgb_linear_singleton(), kUnpremul_SkAlphaType};
327 if (xform.flags.mask()) {
328 // We have a non-identity colorspace transform; apply it.
329 this->applyColorSpaceXform(xform, color);
330 }
331 }
332 }
333
fromLinearSrgb(const void * color)334 void RuntimeEffectRPCallbacks::fromLinearSrgb(const void* color) {
335 if (fStage.fDstCS) {
336 SkColorSpaceXformSteps xform{sk_srgb_linear_singleton(), kUnpremul_SkAlphaType,
337 fStage.fDstCS, kUnpremul_SkAlphaType};
338 if (xform.flags.mask()) {
339 // We have a non-identity colorspace transform; apply it.
340 this->applyColorSpaceXform(xform, color);
341 }
342 }
343 }
344
applyColorSpaceXform(const SkColorSpaceXformSteps & tempXform,const void * color)345 void RuntimeEffectRPCallbacks::applyColorSpaceXform(const SkColorSpaceXformSteps& tempXform,
346 const void* color) {
347 // Copy the transform steps into our alloc.
348 SkColorSpaceXformSteps* xform = fStage.fAlloc->make<SkColorSpaceXformSteps>(tempXform);
349
350 // Put the color into src.rgba (and temporarily stash the execution mask there instead).
351 fStage.fPipeline->append(SkRasterPipelineOp::exchange_src, color);
352 // Add the color space transform to our raster pipeline.
353 xform->apply(fStage.fPipeline);
354 // Restore the execution mask, and move the color back into program data.
355 fStage.fPipeline->append(SkRasterPipelineOp::exchange_src, color);
356 }
357
CanDraw(const SkCapabilities * caps,const SkSL::Program * program)358 bool SkRuntimeEffectPriv::CanDraw(const SkCapabilities* caps, const SkSL::Program* program) {
359 SkASSERT(caps && program);
360 SkASSERT(program->fConfig->enforcesSkSLVersion());
361 return program->fConfig->fRequiredSkSLVersion <= caps->skslVersion();
362 }
363
CanDraw(const SkCapabilities * caps,const SkRuntimeEffect * effect)364 bool SkRuntimeEffectPriv::CanDraw(const SkCapabilities* caps, const SkRuntimeEffect* effect) {
365 SkASSERT(effect);
366 return CanDraw(caps, effect->fBaseProgram.get());
367 }
368
369 //////////////////////////////////////////////////////////////////////////////
370
flattenable_is_valid_as_child(const SkFlattenable * f)371 static bool flattenable_is_valid_as_child(const SkFlattenable* f) {
372 if (!f) { return true; }
373 switch (f->getFlattenableType()) {
374 case SkFlattenable::kSkShader_Type:
375 case SkFlattenable::kSkColorFilter_Type:
376 case SkFlattenable::kSkBlender_Type:
377 return true;
378 default:
379 return false;
380 }
381 }
382
ChildPtr(sk_sp<SkFlattenable> f)383 SkRuntimeEffect::ChildPtr::ChildPtr(sk_sp<SkFlattenable> f) : fChild(std::move(f)) {
384 SkASSERT(flattenable_is_valid_as_child(fChild.get()));
385 }
386
verify_child_effects(const std::vector<SkRuntimeEffect::Child> & reflected,SkSpan<const SkRuntimeEffect::ChildPtr> effectPtrs)387 static bool verify_child_effects(const std::vector<SkRuntimeEffect::Child>& reflected,
388 SkSpan<const SkRuntimeEffect::ChildPtr> effectPtrs) {
389 // Verify that the number of passed-in child-effect pointers matches the SkSL code.
390 if (reflected.size() != effectPtrs.size()) {
391 return false;
392 }
393
394 // Verify that each child object's type matches its declared type in the SkSL.
395 for (size_t i = 0; i < effectPtrs.size(); ++i) {
396 std::optional<ChildType> effectType = effectPtrs[i].type();
397 if (effectType && effectType != reflected[i].type) {
398 return false;
399 }
400 }
401 return true;
402 }
403
404 /**
405 * If `effect` is specified, then the number and type of child objects are validated against the
406 * children() of `effect`. If it's nullptr, this is skipped, allowing deserialization of children,
407 * even when the effect could not be constructed (ie, due to malformed SkSL).
408 */
ReadChildEffects(SkReadBuffer & buffer,const SkRuntimeEffect * effect,TArray<SkRuntimeEffect::ChildPtr> * children)409 bool SkRuntimeEffectPriv::ReadChildEffects(SkReadBuffer& buffer,
410 const SkRuntimeEffect* effect,
411 TArray<SkRuntimeEffect::ChildPtr>* children) {
412 size_t childCount = buffer.read32();
413 if (effect && !buffer.validate(childCount == effect->children().size())) {
414 return false;
415 }
416
417 children->clear();
418 children->reserve_exact(childCount);
419
420 for (size_t i = 0; i < childCount; i++) {
421 sk_sp<SkFlattenable> obj(buffer.readRawFlattenable());
422 if (!flattenable_is_valid_as_child(obj.get())) {
423 buffer.validate(false);
424 return false;
425 }
426 children->push_back(std::move(obj));
427 }
428
429 // If we are validating against an effect, make sure any (non-null) children are the right type
430 if (effect) {
431 auto childInfo = effect->children();
432 SkASSERT(childInfo.size() == SkToSizeT(children->size()));
433 for (size_t i = 0; i < childCount; i++) {
434 std::optional<ChildType> ct = (*children)[i].type();
435 if (ct.has_value() && (*ct) != childInfo[i].type) {
436 buffer.validate(false);
437 }
438 }
439 }
440
441 return buffer.isValid();
442 }
443
WriteChildEffects(SkWriteBuffer & buffer,SkSpan<const SkRuntimeEffect::ChildPtr> children)444 void SkRuntimeEffectPriv::WriteChildEffects(
445 SkWriteBuffer& buffer, SkSpan<const SkRuntimeEffect::ChildPtr> children) {
446 buffer.write32(children.size());
447 for (const auto& child : children) {
448 buffer.writeFlattenable(child.flattenable());
449 }
450 }
451
MakeSettings(const Options & options)452 SkSL::ProgramSettings SkRuntimeEffect::MakeSettings(const Options& options) {
453 SkSL::ProgramSettings settings;
454 settings.fInlineThreshold = 0;
455 settings.fForceNoInline = options.forceUnoptimized;
456 settings.fOptimize = !options.forceUnoptimized;
457 settings.fMaxVersionAllowed = options.maxVersionAllowed;
458
459 // SkSL created by the GPU backend is typically parsed, converted to a backend format,
460 // and the IR is immediately discarded. In that situation, it makes sense to use node
461 // pools to accelerate the IR allocations. Here, SkRuntimeEffect instances are often
462 // long-lived (especially those created internally for runtime FPs). In this situation,
463 // we're willing to pay for a slightly longer compile so that we don't waste huge
464 // amounts of memory.
465 settings.fUseMemoryPool = false;
466 settings.fUseAF = options.useAF;
467 return settings;
468 }
469
470 // TODO: Many errors aren't caught until we process the generated Program here. Catching those
471 // in the IR generator would provide better errors messages (with locations).
472 #define RETURN_FAILURE(...) return Result{nullptr, SkStringPrintf(__VA_ARGS__)}
473
MakeFromSource(SkString sksl,const Options & options,SkSL::ProgramKind kind)474 SkRuntimeEffect::Result SkRuntimeEffect::MakeFromSource(SkString sksl,
475 const Options& options,
476 SkSL::ProgramKind kind) {
477 SkSL::Compiler compiler;
478 SkSL::ProgramSettings settings = MakeSettings(options);
479 std::unique_ptr<SkSL::Program> program =
480 compiler.convertProgram(kind, std::string(sksl.c_str(), sksl.size()), settings);
481
482 if (!program) {
483 RETURN_FAILURE("%s", compiler.errorText().c_str());
484 }
485
486 return MakeInternal(std::move(program), options, kind);
487 }
488
489 // Advanced Filter
getAF() const490 bool SkRuntimeEffect::getAF() const
491 {
492 if (fBaseProgram == nullptr || fBaseProgram->fConfig == nullptr) {
493 return false;
494 }
495 return fBaseProgram->fConfig->fSettings.fUseAF;
496 }
497
MakeInternal(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ProgramKind kind)498 SkRuntimeEffect::Result SkRuntimeEffect::MakeInternal(std::unique_ptr<SkSL::Program> program,
499 const Options& options,
500 SkSL::ProgramKind kind) {
501 SkSL::Compiler compiler;
502
503 uint32_t flags = 0;
504 switch (kind) {
505 case SkSL::ProgramKind::kPrivateRuntimeColorFilter:
506 case SkSL::ProgramKind::kRuntimeColorFilter:
507 // TODO(skia:11209): Figure out a way to run ES3+ color filters on the CPU. This doesn't
508 // need to be fast - it could just be direct IR evaluation. But without it, there's no
509 // way for us to fully implement the SkColorFilter API (eg, `filterColor4f`)
510 if (!SkRuntimeEffectPriv::CanDraw(SkCapabilities::RasterBackend().get(),
511 program.get())) {
512 RETURN_FAILURE("SkSL color filters must target #version 100");
513 }
514 flags |= kAllowColorFilter_Flag;
515 break;
516 case SkSL::ProgramKind::kPrivateRuntimeShader:
517 case SkSL::ProgramKind::kRuntimeShader:
518 flags |= kAllowShader_Flag;
519 break;
520 case SkSL::ProgramKind::kPrivateRuntimeBlender:
521 case SkSL::ProgramKind::kRuntimeBlender:
522 flags |= kAllowBlender_Flag;
523 break;
524 default:
525 SkUNREACHABLE;
526 }
527
528 if (options.forceUnoptimized) {
529 flags |= kDisableOptimization_Flag;
530 }
531
532 // Find 'main', then locate the sample coords parameter. (It might not be present.)
533 const SkSL::FunctionDeclaration* main = program->getFunction("main");
534 if (!main) {
535 RETURN_FAILURE("missing 'main' function");
536 }
537 const SkSL::Variable* coordsParam = main->getMainCoordsParameter();
538
539 const SkSL::ProgramUsage::VariableCounts sampleCoordsUsage =
540 coordsParam ? program->usage()->get(*coordsParam)
541 : SkSL::ProgramUsage::VariableCounts{};
542
543 if (sampleCoordsUsage.fRead || sampleCoordsUsage.fWrite) {
544 flags |= kUsesSampleCoords_Flag;
545 }
546
547 // Color filters and blends are not allowed to depend on position (local or device) in any way.
548 // The signature of main, and the declarations in sksl_rt_colorfilter/sksl_rt_blend should
549 // guarantee this.
550 if (flags & (kAllowColorFilter_Flag | kAllowBlender_Flag)) {
551 SkASSERT(!(flags & kUsesSampleCoords_Flag));
552 SkASSERT(!SkSL::Analysis::ReferencesFragCoords(*program));
553 }
554
555 if (SkSL::Analysis::CallsSampleOutsideMain(*program)) {
556 flags |= kSamplesOutsideMain_Flag;
557 }
558
559 // Look for color filters that preserve the input alpha. This analysis is very conservative, and
560 // only returns true when the input alpha is returned as-is from main() with no intervening
561 // copies or arithmetic.
562 if (flags & kAllowColorFilter_Flag) {
563 if (SkSL::Analysis::ReturnsInputAlpha(*main->definition(), *program->usage())) {
564 flags |= kAlphaUnchanged_Flag;
565 }
566 }
567
568 // Determine if this effect uses of the color transform intrinsics. Effects need to know this
569 // so they can allocate color transform objects, etc.
570 if (SkSL::Analysis::CallsColorTransformIntrinsics(*program)) {
571 flags |= kUsesColorTransform_Flag;
572 }
573
574 // Shaders are the only thing that cares about this, but it's inexpensive (and safe) to call.
575 if (SkSL::Analysis::ReturnsOpaqueColor(*main->definition())) {
576 flags |= kAlwaysOpaque_Flag;
577 }
578
579 // Go through program elements, pulling out information that we need
580 size_t offset = 0;
581 std::vector<Uniform> uniforms;
582 std::vector<Child> children;
583 std::vector<SkSL::SampleUsage> sampleUsages;
584 int elidedSampleCoords = 0;
585 const SkSL::Context& ctx(compiler.context());
586
587 for (const SkSL::ProgramElement* elem : program->elements()) {
588 // Variables (uniform, etc.)
589 if (elem->is<SkSL::GlobalVarDeclaration>()) {
590 const SkSL::GlobalVarDeclaration& global = elem->as<SkSL::GlobalVarDeclaration>();
591 const SkSL::VarDeclaration& varDecl = global.declaration()->as<SkSL::VarDeclaration>();
592 const SkSL::Variable& var = *varDecl.var();
593
594 // Child effects that can be sampled ('shader', 'colorFilter', 'blender')
595 if (var.type().isEffectChild()) {
596 children.push_back(SkRuntimeEffectPriv::VarAsChild(var, children.size()));
597 auto usage = SkSL::Analysis::GetSampleUsage(
598 *program, var, sampleCoordsUsage.fWrite != 0, &elidedSampleCoords);
599 // If the child is never sampled, we pretend that it's actually in PassThrough mode.
600 // Otherwise, the GP code for collecting transforms and emitting transform code gets
601 // very confused, leading to asserts and bad (backend) shaders. There's an implicit
602 // assumption that every FP is used by its parent. (skbug.com/12429)
603 sampleUsages.push_back(usage.isSampled() ? usage
604 : SkSL::SampleUsage::PassThrough());
605 }
606 // 'uniform' variables
607 else if (var.modifierFlags().isUniform()) {
608 uniforms.push_back(SkRuntimeEffectPriv::VarAsUniform(var, ctx, &offset));
609 }
610 }
611 }
612
613 // If the sample coords are never written to, then we will have converted sample calls that use
614 // them unmodified into "passthrough" sampling. If all references to the sample coords were of
615 // that form, then we don't actually "use" sample coords. We unset the flag to prevent creating
616 // an extra (unused) varying holding the coords.
617 if (elidedSampleCoords == sampleCoordsUsage.fRead && sampleCoordsUsage.fWrite == 0) {
618 flags &= ~kUsesSampleCoords_Flag;
619 }
620
621 #undef RETURN_FAILURE
622
623 sk_sp<SkRuntimeEffect> effect(new SkRuntimeEffect(std::move(program),
624 options,
625 *main->definition(),
626 std::move(uniforms),
627 std::move(children),
628 std::move(sampleUsages),
629 flags));
630 return Result{std::move(effect), SkString()};
631 }
632
makeUnoptimizedClone()633 sk_sp<SkRuntimeEffect> SkRuntimeEffect::makeUnoptimizedClone() {
634 // Compile with maximally-permissive options; any restrictions we need to enforce were already
635 // handled when the original SkRuntimeEffect was made. We don't keep around the Options struct
636 // from when it was initially made so we don't know what was originally requested.
637 Options options;
638 options.forceUnoptimized = true;
639 options.maxVersionAllowed = SkSL::Version::k300;
640 options.allowPrivateAccess = true;
641
642 // We do know the original ProgramKind, so we don't need to re-derive it.
643 SkSL::ProgramKind kind = fBaseProgram->fConfig->fKind;
644
645 // Attempt to recompile the program's source with optimizations off. This ensures that the
646 // Debugger shows results on every line, even for things that could be optimized away (static
647 // branches, unused variables, etc). If recompilation fails, we fall back to the original code.
648 SkSL::Compiler compiler;
649 SkSL::ProgramSettings settings = MakeSettings(options);
650 std::unique_ptr<SkSL::Program> program =
651 compiler.convertProgram(kind, *fBaseProgram->fSource, settings);
652
653 if (!program) {
654 // Turning off compiler optimizations can theoretically expose a program error that
655 // had been optimized away (e.g. "all control paths return a value" might be found on a path
656 // that is completely eliminated in the optimized program).
657 // If this happens, the debugger will just have to show the optimized code.
658 return sk_ref_sp(this);
659 }
660
661 SkRuntimeEffect::Result result = MakeInternal(std::move(program), options, kind);
662 if (!result.effect) {
663 // Nothing in MakeInternal should change as a result of optimizations being toggled.
664 SkDEBUGFAILF("makeUnoptimizedClone: MakeInternal failed\n%s",
665 result.errorText.c_str());
666 return sk_ref_sp(this);
667 }
668
669 return result.effect;
670 }
671
MakeForColorFilter(SkString sksl,const Options & options)672 SkRuntimeEffect::Result SkRuntimeEffect::MakeForColorFilter(SkString sksl, const Options& options) {
673 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeColorFilter
674 : SkSL::ProgramKind::kRuntimeColorFilter;
675 auto result = MakeFromSource(std::move(sksl), options, programKind);
676 SkASSERT(!result.effect || result.effect->allowColorFilter());
677 return result;
678 }
679
MakeForShader(SkString sksl,const Options & options)680 SkRuntimeEffect::Result SkRuntimeEffect::MakeForShader(SkString sksl, const Options& options) {
681 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeShader
682 : SkSL::ProgramKind::kRuntimeShader;
683 auto result = MakeFromSource(std::move(sksl), options, programKind);
684 SkASSERT(!result.effect || result.effect->allowShader());
685 return result;
686 }
687
MakeForBlender(SkString sksl,const Options & options)688 SkRuntimeEffect::Result SkRuntimeEffect::MakeForBlender(SkString sksl, const Options& options) {
689 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeBlender
690 : SkSL::ProgramKind::kRuntimeBlender;
691 auto result = MakeFromSource(std::move(sksl), options, programKind);
692 SkASSERT(!result.effect || result.effect->allowBlender());
693 return result;
694 }
695
SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (* make)(SkString sksl,const SkRuntimeEffect::Options &),SkString sksl)696 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(
697 SkRuntimeEffect::Result (*make)(SkString sksl, const SkRuntimeEffect::Options&),
698 SkString sksl) {
699 static SkNoDestructor<SkMutex> mutex;
700 static SkNoDestructor<SkLRUCache<uint64_t, sk_sp<SkRuntimeEffect>>> cache(11 /*arbitrary*/);
701
702 uint64_t key = SkChecksum::Hash64(sksl.c_str(), sksl.size());
703 {
704 SkAutoMutexExclusive _(*mutex);
705 if (sk_sp<SkRuntimeEffect>* found = cache->find(key)) {
706 return *found;
707 }
708 }
709
710 SkRuntimeEffect::Options options;
711 SkRuntimeEffectPriv::AllowPrivateAccess(&options);
712
713 auto [effect, err] = make(std::move(sksl), options);
714 if (!effect) {
715 SkDEBUGFAILF("%s", err.c_str());
716 return nullptr;
717 }
718 SkASSERT(err.isEmpty());
719
720 {
721 SkAutoMutexExclusive _(*mutex);
722 cache->insert_or_update(key, effect);
723 }
724 return effect;
725 }
726
uniform_element_size(SkRuntimeEffect::Uniform::Type type)727 static size_t uniform_element_size(SkRuntimeEffect::Uniform::Type type) {
728 switch (type) {
729 case SkRuntimeEffect::Uniform::Type::kFloat: return sizeof(float);
730 case SkRuntimeEffect::Uniform::Type::kFloat2: return sizeof(float) * 2;
731 case SkRuntimeEffect::Uniform::Type::kFloat3: return sizeof(float) * 3;
732 case SkRuntimeEffect::Uniform::Type::kFloat4: return sizeof(float) * 4;
733
734 case SkRuntimeEffect::Uniform::Type::kFloat2x2: return sizeof(float) * 4;
735 case SkRuntimeEffect::Uniform::Type::kFloat3x3: return sizeof(float) * 9;
736 case SkRuntimeEffect::Uniform::Type::kFloat4x4: return sizeof(float) * 16;
737
738 case SkRuntimeEffect::Uniform::Type::kInt: return sizeof(int);
739 case SkRuntimeEffect::Uniform::Type::kInt2: return sizeof(int) * 2;
740 case SkRuntimeEffect::Uniform::Type::kInt3: return sizeof(int) * 3;
741 case SkRuntimeEffect::Uniform::Type::kInt4: return sizeof(int) * 4;
742 default: SkUNREACHABLE;
743 }
744 }
745
sizeInBytes() const746 size_t SkRuntimeEffect::Uniform::sizeInBytes() const {
747 static_assert(sizeof(int) == sizeof(float));
748 return uniform_element_size(this->type) * this->count;
749 }
750
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)751 SkRuntimeEffect::SkRuntimeEffect(std::unique_ptr<SkSL::Program> baseProgram,
752 const Options& options,
753 const SkSL::FunctionDefinition& main,
754 std::vector<Uniform>&& uniforms,
755 std::vector<Child>&& children,
756 std::vector<SkSL::SampleUsage>&& sampleUsages,
757 uint32_t flags)
758 : fHash(SkChecksum::Hash32(baseProgram->fSource->c_str(), baseProgram->fSource->size()))
759 , fStableKey(options.fStableKey)
760 , fBaseProgram(std::move(baseProgram))
761 , fMain(main)
762 , fUniforms(std::move(uniforms))
763 , fChildren(std::move(children))
764 , fSampleUsages(std::move(sampleUsages))
765 , fFlags(flags) {
766 SkASSERT(fBaseProgram);
767 SkASSERT(fChildren.size() == fSampleUsages.size());
768
769 // Everything from SkRuntimeEffect::Options which could influence the compiled result needs to
770 // be accounted for in `fHash`. If you've added a new field to Options and caused the static-
771 // assert below to trigger, please incorporate your field into `fHash` and update KnownOptions
772 // to match the layout of Options.
773 struct KnownOptions {
774 bool forceUnoptimized, allowPrivateAccess, useAF;
775 uint32_t fStableKey;
776 SkSL::Version maxVersionAllowed;
777 };
778 static_assert(sizeof(Options) == sizeof(KnownOptions));
779 fHash = SkChecksum::Hash32(&options.forceUnoptimized,
780 sizeof(options.forceUnoptimized), fHash);
781 fHash = SkChecksum::Hash32(&options.allowPrivateAccess,
782 sizeof(options.allowPrivateAccess), fHash);
783 fHash = SkChecksum::Hash32(&options.fStableKey,
784 sizeof(options.fStableKey), fHash);
785 fHash = SkChecksum::Hash32(&options.maxVersionAllowed,
786 sizeof(options.maxVersionAllowed), fHash);
787 // Advanced Filter
788 fHash = SkChecksum::Hash32(&options.useAF, sizeof(options.useAF), fHash);
789 }
790
791 SkRuntimeEffect::~SkRuntimeEffect() = default;
792
source() const793 const std::string& SkRuntimeEffect::source() const {
794 return *fBaseProgram->fSource;
795 }
796
uniformSize() const797 size_t SkRuntimeEffect::uniformSize() const {
798 return fUniforms.empty() ? 0
799 : SkAlign4(fUniforms.back().offset + fUniforms.back().sizeInBytes());
800 }
801
findUniform(std::string_view name) const802 const SkRuntimeEffect::Uniform* SkRuntimeEffect::findUniform(std::string_view name) const {
803 auto iter = std::find_if(fUniforms.begin(), fUniforms.end(), [name](const Uniform& u) {
804 return u.name == name;
805 });
806 return iter == fUniforms.end() ? nullptr : &(*iter);
807 }
808
findChild(std::string_view name) const809 const SkRuntimeEffect::Child* SkRuntimeEffect::findChild(std::string_view name) const {
810 auto iter = std::find_if(fChildren.begin(), fChildren.end(), [name](const Child& c) {
811 return c.name == name;
812 });
813 return iter == fChildren.end() ? nullptr : &(*iter);
814 }
815
816 ///////////////////////////////////////////////////////////////////////////////////////////////////
817
MakeDeferredShader(const SkRuntimeEffect * effect,UniformsCallback uniformsCallback,SkSpan<const SkRuntimeEffect::ChildPtr> children,const SkMatrix * localMatrix)818 sk_sp<SkShader> SkRuntimeEffectPriv::MakeDeferredShader(
819 const SkRuntimeEffect* effect,
820 UniformsCallback uniformsCallback,
821 SkSpan<const SkRuntimeEffect::ChildPtr> children,
822 const SkMatrix* localMatrix) {
823 if (!effect->allowShader()) {
824 return nullptr;
825 }
826 if (!verify_child_effects(effect->fChildren, children)) {
827 return nullptr;
828 }
829 if (!uniformsCallback) {
830 return nullptr;
831 }
832 return SkLocalMatrixShader::MakeWrapped<SkRuntimeShader>(localMatrix,
833 sk_ref_sp(effect),
834 /*debugTrace=*/nullptr,
835 std::move(uniformsCallback),
836 children);
837 }
838
makeShader(sk_sp<const SkData> uniforms,sk_sp<SkShader> childShaders[],size_t childCount,const SkMatrix * localMatrix) const839 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<const SkData> uniforms,
840 sk_sp<SkShader> childShaders[],
841 size_t childCount,
842 const SkMatrix* localMatrix) const {
843 STArray<4, ChildPtr> children(childCount);
844 for (size_t i = 0; i < childCount; ++i) {
845 children.emplace_back(childShaders[i]);
846 }
847 return this->makeShader(std::move(uniforms), SkSpan(children), localMatrix);
848 }
849
makeShader(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children,const SkMatrix * localMatrix) const850 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<const SkData> uniforms,
851 SkSpan<const ChildPtr> children,
852 const SkMatrix* localMatrix) const {
853 if (!this->allowShader()) {
854 return nullptr;
855 }
856 if (!verify_child_effects(fChildren, children)) {
857 return nullptr;
858 }
859 if (!uniforms) {
860 uniforms = SkData::MakeEmpty();
861 }
862 if (uniforms->size() != this->uniformSize()) {
863 return nullptr;
864 }
865 return SkLocalMatrixShader::MakeWrapped<SkRuntimeShader>(localMatrix,
866 sk_ref_sp(this),
867 /*debugTrace=*/nullptr,
868 std::move(uniforms),
869 children);
870 }
871
makeColorFilter(sk_sp<const SkData> uniforms,sk_sp<SkColorFilter> childColorFilters[],size_t childCount) const872 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms,
873 sk_sp<SkColorFilter> childColorFilters[],
874 size_t childCount) const {
875 STArray<4, ChildPtr> children(childCount);
876 for (size_t i = 0; i < childCount; ++i) {
877 children.emplace_back(childColorFilters[i]);
878 }
879 return this->makeColorFilter(std::move(uniforms), SkSpan(children));
880 }
881
makeColorFilter(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children) const882 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms,
883 SkSpan<const ChildPtr> children) const {
884 if (!this->allowColorFilter()) {
885 return nullptr;
886 }
887 if (!verify_child_effects(fChildren, children)) {
888 return nullptr;
889 }
890 if (!uniforms) {
891 uniforms = SkData::MakeEmpty();
892 }
893 if (uniforms->size() != this->uniformSize()) {
894 return nullptr;
895 }
896 return sk_make_sp<SkRuntimeColorFilter>(sk_ref_sp(this), std::move(uniforms), children);
897 }
898
makeColorFilter(sk_sp<const SkData> uniforms) const899 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms) const {
900 return this->makeColorFilter(std::move(uniforms), /*children=*/{});
901 }
902
makeBlender(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children) const903 sk_sp<SkBlender> SkRuntimeEffect::makeBlender(sk_sp<const SkData> uniforms,
904 SkSpan<const ChildPtr> children) const {
905 if (!this->allowBlender()) {
906 return nullptr;
907 }
908 if (!verify_child_effects(fChildren, children)) {
909 return nullptr;
910 }
911 if (!uniforms) {
912 uniforms = SkData::MakeEmpty();
913 }
914 if (uniforms->size() != this->uniformSize()) {
915 return nullptr;
916 }
917 return sk_make_sp<SkRuntimeBlender>(sk_ref_sp(this), std::move(uniforms), children);
918 }
919
920 ///////////////////////////////////////////////////////////////////////////////////////////////////
921
MakeTraced(sk_sp<SkShader> shader,const SkIPoint & traceCoord)922 SkRuntimeEffect::TracedShader SkRuntimeEffect::MakeTraced(sk_sp<SkShader> shader,
923 const SkIPoint& traceCoord) {
924 SkRuntimeEffect* effect = as_SB(shader)->asRuntimeEffect();
925 if (!effect) {
926 return TracedShader{nullptr, nullptr};
927 }
928 // An SkShader with an attached SkRuntimeEffect must be an SkRuntimeShader.
929 SkRuntimeShader* rtShader = static_cast<SkRuntimeShader*>(shader.get());
930 return rtShader->makeTracedClone(traceCoord);
931 }
932
933 ///////////////////////////////////////////////////////////////////////////////////////////////////
934
type() const935 std::optional<ChildType> SkRuntimeEffect::ChildPtr::type() const {
936 if (fChild) {
937 switch (fChild->getFlattenableType()) {
938 case SkFlattenable::kSkShader_Type:
939 return ChildType::kShader;
940 case SkFlattenable::kSkColorFilter_Type:
941 return ChildType::kColorFilter;
942 case SkFlattenable::kSkBlender_Type:
943 return ChildType::kBlender;
944 default:
945 break;
946 }
947 }
948 return std::nullopt;
949 }
950
shader() const951 SkShader* SkRuntimeEffect::ChildPtr::shader() const {
952 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkShader_Type)
953 ? static_cast<SkShader*>(fChild.get())
954 : nullptr;
955 }
956
colorFilter() const957 SkColorFilter* SkRuntimeEffect::ChildPtr::colorFilter() const {
958 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkColorFilter_Type)
959 ? static_cast<SkColorFilter*>(fChild.get())
960 : nullptr;
961 }
962
blender() const963 SkBlender* SkRuntimeEffect::ChildPtr::blender() const {
964 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkBlender_Type)
965 ? static_cast<SkBlender*>(fChild.get())
966 : nullptr;
967 }
968
969 ///////////////////////////////////////////////////////////////////////////////////////////////////
970
RegisterFlattenables()971 void SkRuntimeEffect::RegisterFlattenables() {
972 SK_REGISTER_FLATTENABLE(SkRuntimeBlender);
973 SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter);
974 SK_REGISTER_FLATTENABLE(SkRuntimeShader);
975
976 // Previous name
977 SkFlattenable::Register("SkRTShader", SkRuntimeShader::CreateProc);
978 }
979
makeShader(const SkMatrix * localMatrix) const980 sk_sp<SkShader> SkRuntimeEffectBuilder::makeShader(const SkMatrix* localMatrix) const {
981 return this->effect()->makeShader(this->uniforms(), this->children(), localMatrix);
982 }
983
makeBlender() const984 sk_sp<SkBlender> SkRuntimeEffectBuilder::makeBlender() const {
985 return this->effect()->makeBlender(this->uniforms(), this->children());
986 }
987
makeColorFilter() const988 sk_sp<SkColorFilter> SkRuntimeEffectBuilder::makeColorFilter() const {
989 return this->effect()->makeColorFilter(this->uniforms(), this->children());
990 }
991