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