1 /*
2  * Copyright 2021 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/sksl/DSLRuntimeEffects.h"
9 
10 #include "include/effects/SkRuntimeEffect.h"
11 #include "include/sksl/DSLCore.h"
12 #include "src/sksl/SkSLCompiler.h"
13 #include "src/sksl/SkSLIRGenerator.h"
14 #include "src/sksl/dsl/priv/DSLWriter.h"
15 
16 namespace SkSL {
17 
18 namespace dsl {
19 
20 #ifndef SKSL_STANDALONE
21 
StartRuntimeShader(SkSL::Compiler * compiler)22 void StartRuntimeShader(SkSL::Compiler* compiler) {
23     Start(compiler, SkSL::ProgramKind::kRuntimeShader);
24     SkSL::ProgramSettings& settings = DSLWriter::IRGenerator().fContext.fConfig->fSettings;
25     SkASSERT(settings.fInlineThreshold == SkSL::kDefaultInlineThreshold);
26     settings.fInlineThreshold = 0;
27     SkASSERT(!settings.fAllowNarrowingConversions);
28     settings.fAllowNarrowingConversions = true;
29 }
30 
EndRuntimeShader()31 sk_sp<SkRuntimeEffect> EndRuntimeShader() {
32     std::unique_ptr<SkSL::Program> program = DSLWriter::ReleaseProgram();
33     auto result = SkRuntimeEffect::MakeForShader(std::move(program));
34     // TODO(skbug.com/11862): propagate errors properly
35     SkASSERTF(result.effect, "%s\n", result.errorText.c_str());
36     SkSL::ProgramSettings& settings = DSLWriter::IRGenerator().fContext.fConfig->fSettings;
37     settings.fInlineThreshold = SkSL::kDefaultInlineThreshold;
38     settings.fAllowNarrowingConversions = false;
39     End();
40     return result.effect;
41 }
42 
43 #endif // SKSL_STANDALONE
44 
45 } // namespace dsl
46 
47 } // namespace SkSL
48