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 return settings;
467 }
468
469 // TODO: Many errors aren't caught until we process the generated Program here. Catching those
470 // in the IR generator would provide better errors messages (with locations).
471 #define RETURN_FAILURE(...) return Result{nullptr, SkStringPrintf(__VA_ARGS__)}
472
MakeFromSource(SkString sksl,const Options & options,SkSL::ProgramKind kind)473 SkRuntimeEffect::Result SkRuntimeEffect::MakeFromSource(SkString sksl,
474 const Options& options,
475 SkSL::ProgramKind kind) {
476 SkSL::Compiler compiler;
477 SkSL::ProgramSettings settings = MakeSettings(options);
478 std::unique_ptr<SkSL::Program> program =
479 compiler.convertProgram(kind, std::string(sksl.c_str(), sksl.size()), settings);
480
481 if (!program) {
482 RETURN_FAILURE("%s", compiler.errorText().c_str());
483 }
484
485 return MakeInternal(std::move(program), options, kind);
486 }
487
MakeInternal(std::unique_ptr<SkSL::Program> program,const Options & options,SkSL::ProgramKind kind)488 SkRuntimeEffect::Result SkRuntimeEffect::MakeInternal(std::unique_ptr<SkSL::Program> program,
489 const Options& options,
490 SkSL::ProgramKind kind) {
491 SkSL::Compiler compiler;
492
493 uint32_t flags = 0;
494 switch (kind) {
495 case SkSL::ProgramKind::kPrivateRuntimeColorFilter:
496 case SkSL::ProgramKind::kRuntimeColorFilter:
497 // TODO(skia:11209): Figure out a way to run ES3+ color filters on the CPU. This doesn't
498 // need to be fast - it could just be direct IR evaluation. But without it, there's no
499 // way for us to fully implement the SkColorFilter API (eg, `filterColor4f`)
500 if (!SkRuntimeEffectPriv::CanDraw(SkCapabilities::RasterBackend().get(),
501 program.get())) {
502 RETURN_FAILURE("SkSL color filters must target #version 100");
503 }
504 flags |= kAllowColorFilter_Flag;
505 break;
506 case SkSL::ProgramKind::kPrivateRuntimeShader:
507 case SkSL::ProgramKind::kRuntimeShader:
508 flags |= kAllowShader_Flag;
509 break;
510 case SkSL::ProgramKind::kPrivateRuntimeBlender:
511 case SkSL::ProgramKind::kRuntimeBlender:
512 flags |= kAllowBlender_Flag;
513 break;
514 default:
515 SkUNREACHABLE;
516 }
517
518 if (options.forceUnoptimized) {
519 flags |= kDisableOptimization_Flag;
520 }
521
522 // Find 'main', then locate the sample coords parameter. (It might not be present.)
523 const SkSL::FunctionDeclaration* main = program->getFunction("main");
524 if (!main) {
525 RETURN_FAILURE("missing 'main' function");
526 }
527 const SkSL::Variable* coordsParam = main->getMainCoordsParameter();
528
529 const SkSL::ProgramUsage::VariableCounts sampleCoordsUsage =
530 coordsParam ? program->usage()->get(*coordsParam)
531 : SkSL::ProgramUsage::VariableCounts{};
532
533 if (sampleCoordsUsage.fRead || sampleCoordsUsage.fWrite) {
534 flags |= kUsesSampleCoords_Flag;
535 }
536
537 // Color filters and blends are not allowed to depend on position (local or device) in any way.
538 // The signature of main, and the declarations in sksl_rt_colorfilter/sksl_rt_blend should
539 // guarantee this.
540 if (flags & (kAllowColorFilter_Flag | kAllowBlender_Flag)) {
541 SkASSERT(!(flags & kUsesSampleCoords_Flag));
542 SkASSERT(!SkSL::Analysis::ReferencesFragCoords(*program));
543 }
544
545 if (SkSL::Analysis::CallsSampleOutsideMain(*program)) {
546 flags |= kSamplesOutsideMain_Flag;
547 }
548
549 // Look for color filters that preserve the input alpha. This analysis is very conservative, and
550 // only returns true when the input alpha is returned as-is from main() with no intervening
551 // copies or arithmetic.
552 if (flags & kAllowColorFilter_Flag) {
553 if (SkSL::Analysis::ReturnsInputAlpha(*main->definition(), *program->usage())) {
554 flags |= kAlphaUnchanged_Flag;
555 }
556 }
557
558 // Determine if this effect uses of the color transform intrinsics. Effects need to know this
559 // so they can allocate color transform objects, etc.
560 if (SkSL::Analysis::CallsColorTransformIntrinsics(*program)) {
561 flags |= kUsesColorTransform_Flag;
562 }
563
564 // Shaders are the only thing that cares about this, but it's inexpensive (and safe) to call.
565 if (SkSL::Analysis::ReturnsOpaqueColor(*main->definition())) {
566 flags |= kAlwaysOpaque_Flag;
567 }
568
569 // Go through program elements, pulling out information that we need
570 size_t offset = 0;
571 std::vector<Uniform> uniforms;
572 std::vector<Child> children;
573 std::vector<SkSL::SampleUsage> sampleUsages;
574 int elidedSampleCoords = 0;
575 const SkSL::Context& ctx(compiler.context());
576
577 for (const SkSL::ProgramElement* elem : program->elements()) {
578 // Variables (uniform, etc.)
579 if (elem->is<SkSL::GlobalVarDeclaration>()) {
580 const SkSL::GlobalVarDeclaration& global = elem->as<SkSL::GlobalVarDeclaration>();
581 const SkSL::VarDeclaration& varDecl = global.declaration()->as<SkSL::VarDeclaration>();
582 const SkSL::Variable& var = *varDecl.var();
583
584 // Child effects that can be sampled ('shader', 'colorFilter', 'blender')
585 if (var.type().isEffectChild()) {
586 children.push_back(SkRuntimeEffectPriv::VarAsChild(var, children.size()));
587 auto usage = SkSL::Analysis::GetSampleUsage(
588 *program, var, sampleCoordsUsage.fWrite != 0, &elidedSampleCoords);
589 // If the child is never sampled, we pretend that it's actually in PassThrough mode.
590 // Otherwise, the GP code for collecting transforms and emitting transform code gets
591 // very confused, leading to asserts and bad (backend) shaders. There's an implicit
592 // assumption that every FP is used by its parent. (skbug.com/12429)
593 sampleUsages.push_back(usage.isSampled() ? usage
594 : SkSL::SampleUsage::PassThrough());
595 }
596 // 'uniform' variables
597 else if (var.modifierFlags().isUniform()) {
598 uniforms.push_back(SkRuntimeEffectPriv::VarAsUniform(var, ctx, &offset));
599 }
600 }
601 }
602
603 // If the sample coords are never written to, then we will have converted sample calls that use
604 // them unmodified into "passthrough" sampling. If all references to the sample coords were of
605 // that form, then we don't actually "use" sample coords. We unset the flag to prevent creating
606 // an extra (unused) varying holding the coords.
607 if (elidedSampleCoords == sampleCoordsUsage.fRead && sampleCoordsUsage.fWrite == 0) {
608 flags &= ~kUsesSampleCoords_Flag;
609 }
610
611 #undef RETURN_FAILURE
612
613 sk_sp<SkRuntimeEffect> effect(new SkRuntimeEffect(std::move(program),
614 options,
615 *main->definition(),
616 std::move(uniforms),
617 std::move(children),
618 std::move(sampleUsages),
619 flags));
620 return Result{std::move(effect), SkString()};
621 }
622
makeUnoptimizedClone()623 sk_sp<SkRuntimeEffect> SkRuntimeEffect::makeUnoptimizedClone() {
624 // Compile with maximally-permissive options; any restrictions we need to enforce were already
625 // handled when the original SkRuntimeEffect was made. We don't keep around the Options struct
626 // from when it was initially made so we don't know what was originally requested.
627 Options options;
628 options.forceUnoptimized = true;
629 options.maxVersionAllowed = SkSL::Version::k300;
630 options.allowPrivateAccess = true;
631
632 // We do know the original ProgramKind, so we don't need to re-derive it.
633 SkSL::ProgramKind kind = fBaseProgram->fConfig->fKind;
634
635 // Attempt to recompile the program's source with optimizations off. This ensures that the
636 // Debugger shows results on every line, even for things that could be optimized away (static
637 // branches, unused variables, etc). If recompilation fails, we fall back to the original code.
638 SkSL::Compiler compiler;
639 SkSL::ProgramSettings settings = MakeSettings(options);
640 std::unique_ptr<SkSL::Program> program =
641 compiler.convertProgram(kind, *fBaseProgram->fSource, settings);
642
643 if (!program) {
644 // Turning off compiler optimizations can theoretically expose a program error that
645 // had been optimized away (e.g. "all control paths return a value" might be found on a path
646 // that is completely eliminated in the optimized program).
647 // If this happens, the debugger will just have to show the optimized code.
648 return sk_ref_sp(this);
649 }
650
651 SkRuntimeEffect::Result result = MakeInternal(std::move(program), options, kind);
652 if (!result.effect) {
653 // Nothing in MakeInternal should change as a result of optimizations being toggled.
654 SkDEBUGFAILF("makeUnoptimizedClone: MakeInternal failed\n%s",
655 result.errorText.c_str());
656 return sk_ref_sp(this);
657 }
658
659 return result.effect;
660 }
661
MakeForColorFilter(SkString sksl,const Options & options)662 SkRuntimeEffect::Result SkRuntimeEffect::MakeForColorFilter(SkString sksl, const Options& options) {
663 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeColorFilter
664 : SkSL::ProgramKind::kRuntimeColorFilter;
665 auto result = MakeFromSource(std::move(sksl), options, programKind);
666 SkASSERT(!result.effect || result.effect->allowColorFilter());
667 return result;
668 }
669
MakeForShader(SkString sksl,const Options & options)670 SkRuntimeEffect::Result SkRuntimeEffect::MakeForShader(SkString sksl, const Options& options) {
671 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeShader
672 : SkSL::ProgramKind::kRuntimeShader;
673 auto result = MakeFromSource(std::move(sksl), options, programKind);
674 SkASSERT(!result.effect || result.effect->allowShader());
675 return result;
676 }
677
MakeForBlender(SkString sksl,const Options & options)678 SkRuntimeEffect::Result SkRuntimeEffect::MakeForBlender(SkString sksl, const Options& options) {
679 auto programKind = options.allowPrivateAccess ? SkSL::ProgramKind::kPrivateRuntimeBlender
680 : SkSL::ProgramKind::kRuntimeBlender;
681 auto result = MakeFromSource(std::move(sksl), options, programKind);
682 SkASSERT(!result.effect || result.effect->allowBlender());
683 return result;
684 }
685
SkMakeCachedRuntimeEffect(SkRuntimeEffect::Result (* make)(SkString sksl,const SkRuntimeEffect::Options &),SkString sksl)686 sk_sp<SkRuntimeEffect> SkMakeCachedRuntimeEffect(
687 SkRuntimeEffect::Result (*make)(SkString sksl, const SkRuntimeEffect::Options&),
688 SkString sksl) {
689 static SkNoDestructor<SkMutex> mutex;
690 static SkNoDestructor<SkLRUCache<uint64_t, sk_sp<SkRuntimeEffect>>> cache(11 /*arbitrary*/);
691
692 uint64_t key = SkChecksum::Hash64(sksl.c_str(), sksl.size());
693 {
694 SkAutoMutexExclusive _(*mutex);
695 if (sk_sp<SkRuntimeEffect>* found = cache->find(key)) {
696 return *found;
697 }
698 }
699
700 SkRuntimeEffect::Options options;
701 SkRuntimeEffectPriv::AllowPrivateAccess(&options);
702
703 auto [effect, err] = make(std::move(sksl), options);
704 if (!effect) {
705 SkDEBUGFAILF("%s", err.c_str());
706 return nullptr;
707 }
708 SkASSERT(err.isEmpty());
709
710 {
711 SkAutoMutexExclusive _(*mutex);
712 cache->insert_or_update(key, effect);
713 }
714 return effect;
715 }
716
uniform_element_size(SkRuntimeEffect::Uniform::Type type)717 static size_t uniform_element_size(SkRuntimeEffect::Uniform::Type type) {
718 switch (type) {
719 case SkRuntimeEffect::Uniform::Type::kFloat: return sizeof(float);
720 case SkRuntimeEffect::Uniform::Type::kFloat2: return sizeof(float) * 2;
721 case SkRuntimeEffect::Uniform::Type::kFloat3: return sizeof(float) * 3;
722 case SkRuntimeEffect::Uniform::Type::kFloat4: return sizeof(float) * 4;
723
724 case SkRuntimeEffect::Uniform::Type::kFloat2x2: return sizeof(float) * 4;
725 case SkRuntimeEffect::Uniform::Type::kFloat3x3: return sizeof(float) * 9;
726 case SkRuntimeEffect::Uniform::Type::kFloat4x4: return sizeof(float) * 16;
727
728 case SkRuntimeEffect::Uniform::Type::kInt: return sizeof(int);
729 case SkRuntimeEffect::Uniform::Type::kInt2: return sizeof(int) * 2;
730 case SkRuntimeEffect::Uniform::Type::kInt3: return sizeof(int) * 3;
731 case SkRuntimeEffect::Uniform::Type::kInt4: return sizeof(int) * 4;
732 default: SkUNREACHABLE;
733 }
734 }
735
sizeInBytes() const736 size_t SkRuntimeEffect::Uniform::sizeInBytes() const {
737 static_assert(sizeof(int) == sizeof(float));
738 return uniform_element_size(this->type) * this->count;
739 }
740
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)741 SkRuntimeEffect::SkRuntimeEffect(std::unique_ptr<SkSL::Program> baseProgram,
742 const Options& options,
743 const SkSL::FunctionDefinition& main,
744 std::vector<Uniform>&& uniforms,
745 std::vector<Child>&& children,
746 std::vector<SkSL::SampleUsage>&& sampleUsages,
747 uint32_t flags)
748 : fHash(SkChecksum::Hash32(baseProgram->fSource->c_str(), baseProgram->fSource->size()))
749 , fStableKey(options.fStableKey)
750 , fBaseProgram(std::move(baseProgram))
751 , fMain(main)
752 , fUniforms(std::move(uniforms))
753 , fChildren(std::move(children))
754 , fSampleUsages(std::move(sampleUsages))
755 , fFlags(flags) {
756 SkASSERT(fBaseProgram);
757 SkASSERT(fChildren.size() == fSampleUsages.size());
758
759 // Everything from SkRuntimeEffect::Options which could influence the compiled result needs to
760 // be accounted for in `fHash`. If you've added a new field to Options and caused the static-
761 // assert below to trigger, please incorporate your field into `fHash` and update KnownOptions
762 // to match the layout of Options.
763 struct KnownOptions {
764 bool forceUnoptimized, allowPrivateAccess;
765 uint32_t fStableKey;
766 SkSL::Version maxVersionAllowed;
767 };
768 static_assert(sizeof(Options) == sizeof(KnownOptions));
769 fHash = SkChecksum::Hash32(&options.forceUnoptimized,
770 sizeof(options.forceUnoptimized), fHash);
771 fHash = SkChecksum::Hash32(&options.allowPrivateAccess,
772 sizeof(options.allowPrivateAccess), fHash);
773 fHash = SkChecksum::Hash32(&options.fStableKey,
774 sizeof(options.fStableKey), fHash);
775 fHash = SkChecksum::Hash32(&options.maxVersionAllowed,
776 sizeof(options.maxVersionAllowed), fHash);
777 }
778
779 SkRuntimeEffect::~SkRuntimeEffect() = default;
780
source() const781 const std::string& SkRuntimeEffect::source() const {
782 return *fBaseProgram->fSource;
783 }
784
uniformSize() const785 size_t SkRuntimeEffect::uniformSize() const {
786 return fUniforms.empty() ? 0
787 : SkAlign4(fUniforms.back().offset + fUniforms.back().sizeInBytes());
788 }
789
findUniform(std::string_view name) const790 const SkRuntimeEffect::Uniform* SkRuntimeEffect::findUniform(std::string_view name) const {
791 auto iter = std::find_if(fUniforms.begin(), fUniforms.end(), [name](const Uniform& u) {
792 return u.name == name;
793 });
794 return iter == fUniforms.end() ? nullptr : &(*iter);
795 }
796
findChild(std::string_view name) const797 const SkRuntimeEffect::Child* SkRuntimeEffect::findChild(std::string_view name) const {
798 auto iter = std::find_if(fChildren.begin(), fChildren.end(), [name](const Child& c) {
799 return c.name == name;
800 });
801 return iter == fChildren.end() ? nullptr : &(*iter);
802 }
803
804 ///////////////////////////////////////////////////////////////////////////////////////////////////
805
MakeDeferredShader(const SkRuntimeEffect * effect,UniformsCallback uniformsCallback,SkSpan<const SkRuntimeEffect::ChildPtr> children,const SkMatrix * localMatrix)806 sk_sp<SkShader> SkRuntimeEffectPriv::MakeDeferredShader(
807 const SkRuntimeEffect* effect,
808 UniformsCallback uniformsCallback,
809 SkSpan<const SkRuntimeEffect::ChildPtr> children,
810 const SkMatrix* localMatrix) {
811 if (!effect->allowShader()) {
812 return nullptr;
813 }
814 if (!verify_child_effects(effect->fChildren, children)) {
815 return nullptr;
816 }
817 if (!uniformsCallback) {
818 return nullptr;
819 }
820 return SkLocalMatrixShader::MakeWrapped<SkRuntimeShader>(localMatrix,
821 sk_ref_sp(effect),
822 /*debugTrace=*/nullptr,
823 std::move(uniformsCallback),
824 children);
825 }
826
makeShader(sk_sp<const SkData> uniforms,sk_sp<SkShader> childShaders[],size_t childCount,const SkMatrix * localMatrix) const827 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<const SkData> uniforms,
828 sk_sp<SkShader> childShaders[],
829 size_t childCount,
830 const SkMatrix* localMatrix) const {
831 STArray<4, ChildPtr> children(childCount);
832 for (size_t i = 0; i < childCount; ++i) {
833 children.emplace_back(childShaders[i]);
834 }
835 return this->makeShader(std::move(uniforms), SkSpan(children), localMatrix);
836 }
837
makeShader(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children,const SkMatrix * localMatrix) const838 sk_sp<SkShader> SkRuntimeEffect::makeShader(sk_sp<const SkData> uniforms,
839 SkSpan<const ChildPtr> children,
840 const SkMatrix* localMatrix) const {
841 if (!this->allowShader()) {
842 return nullptr;
843 }
844 if (!verify_child_effects(fChildren, children)) {
845 return nullptr;
846 }
847 if (!uniforms) {
848 uniforms = SkData::MakeEmpty();
849 }
850 if (uniforms->size() != this->uniformSize()) {
851 return nullptr;
852 }
853 return SkLocalMatrixShader::MakeWrapped<SkRuntimeShader>(localMatrix,
854 sk_ref_sp(this),
855 /*debugTrace=*/nullptr,
856 std::move(uniforms),
857 children);
858 }
859
makeColorFilter(sk_sp<const SkData> uniforms,sk_sp<SkColorFilter> childColorFilters[],size_t childCount) const860 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms,
861 sk_sp<SkColorFilter> childColorFilters[],
862 size_t childCount) const {
863 STArray<4, ChildPtr> children(childCount);
864 for (size_t i = 0; i < childCount; ++i) {
865 children.emplace_back(childColorFilters[i]);
866 }
867 return this->makeColorFilter(std::move(uniforms), SkSpan(children));
868 }
869
makeColorFilter(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children) const870 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms,
871 SkSpan<const ChildPtr> children) const {
872 if (!this->allowColorFilter()) {
873 return nullptr;
874 }
875 if (!verify_child_effects(fChildren, children)) {
876 return nullptr;
877 }
878 if (!uniforms) {
879 uniforms = SkData::MakeEmpty();
880 }
881 if (uniforms->size() != this->uniformSize()) {
882 return nullptr;
883 }
884 return sk_make_sp<SkRuntimeColorFilter>(sk_ref_sp(this), std::move(uniforms), children);
885 }
886
makeColorFilter(sk_sp<const SkData> uniforms) const887 sk_sp<SkColorFilter> SkRuntimeEffect::makeColorFilter(sk_sp<const SkData> uniforms) const {
888 return this->makeColorFilter(std::move(uniforms), /*children=*/{});
889 }
890
makeBlender(sk_sp<const SkData> uniforms,SkSpan<const ChildPtr> children) const891 sk_sp<SkBlender> SkRuntimeEffect::makeBlender(sk_sp<const SkData> uniforms,
892 SkSpan<const ChildPtr> children) const {
893 if (!this->allowBlender()) {
894 return nullptr;
895 }
896 if (!verify_child_effects(fChildren, children)) {
897 return nullptr;
898 }
899 if (!uniforms) {
900 uniforms = SkData::MakeEmpty();
901 }
902 if (uniforms->size() != this->uniformSize()) {
903 return nullptr;
904 }
905 return sk_make_sp<SkRuntimeBlender>(sk_ref_sp(this), std::move(uniforms), children);
906 }
907
908 ///////////////////////////////////////////////////////////////////////////////////////////////////
909
MakeTraced(sk_sp<SkShader> shader,const SkIPoint & traceCoord)910 SkRuntimeEffect::TracedShader SkRuntimeEffect::MakeTraced(sk_sp<SkShader> shader,
911 const SkIPoint& traceCoord) {
912 SkRuntimeEffect* effect = as_SB(shader)->asRuntimeEffect();
913 if (!effect) {
914 return TracedShader{nullptr, nullptr};
915 }
916 // An SkShader with an attached SkRuntimeEffect must be an SkRuntimeShader.
917 SkRuntimeShader* rtShader = static_cast<SkRuntimeShader*>(shader.get());
918 return rtShader->makeTracedClone(traceCoord);
919 }
920
921 ///////////////////////////////////////////////////////////////////////////////////////////////////
922
type() const923 std::optional<ChildType> SkRuntimeEffect::ChildPtr::type() const {
924 if (fChild) {
925 switch (fChild->getFlattenableType()) {
926 case SkFlattenable::kSkShader_Type:
927 return ChildType::kShader;
928 case SkFlattenable::kSkColorFilter_Type:
929 return ChildType::kColorFilter;
930 case SkFlattenable::kSkBlender_Type:
931 return ChildType::kBlender;
932 default:
933 break;
934 }
935 }
936 return std::nullopt;
937 }
938
shader() const939 SkShader* SkRuntimeEffect::ChildPtr::shader() const {
940 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkShader_Type)
941 ? static_cast<SkShader*>(fChild.get())
942 : nullptr;
943 }
944
colorFilter() const945 SkColorFilter* SkRuntimeEffect::ChildPtr::colorFilter() const {
946 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkColorFilter_Type)
947 ? static_cast<SkColorFilter*>(fChild.get())
948 : nullptr;
949 }
950
blender() const951 SkBlender* SkRuntimeEffect::ChildPtr::blender() const {
952 return (fChild && fChild->getFlattenableType() == SkFlattenable::kSkBlender_Type)
953 ? static_cast<SkBlender*>(fChild.get())
954 : nullptr;
955 }
956
957 ///////////////////////////////////////////////////////////////////////////////////////////////////
958
RegisterFlattenables()959 void SkRuntimeEffect::RegisterFlattenables() {
960 SK_REGISTER_FLATTENABLE(SkRuntimeBlender);
961 SK_REGISTER_FLATTENABLE(SkRuntimeColorFilter);
962 SK_REGISTER_FLATTENABLE(SkRuntimeShader);
963
964 // Previous name
965 SkFlattenable::Register("SkRTShader", SkRuntimeShader::CreateProc);
966 }
967
makeShader(const SkMatrix * localMatrix) const968 sk_sp<SkShader> SkRuntimeEffectBuilder::makeShader(const SkMatrix* localMatrix) const {
969 return this->effect()->makeShader(this->uniforms(), this->children(), localMatrix);
970 }
971
makeBlender() const972 sk_sp<SkBlender> SkRuntimeEffectBuilder::makeBlender() const {
973 return this->effect()->makeBlender(this->uniforms(), this->children());
974 }
975
makeColorFilter() const976 sk_sp<SkColorFilter> SkRuntimeEffectBuilder::makeColorFilter() const {
977 return this->effect()->makeColorFilter(this->uniforms(), this->children());
978 }
979