blob: de082815b184efc4a94b3ef0f0460bb3cf529687 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 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#ifndef SKSL_DSLWRITER
9#define SKSL_DSLWRITER
10
11#include "src/sksl/dsl/DSLExpression.h"
12#include "src/sksl/ir/SkSLExpressionStatement.h"
13#include "src/sksl/ir/SkSLProgram.h"
14#include "src/sksl/ir/SkSLStatement.h"
15#if !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
16#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
17#endif // !defined(SKSL_STANDALONE) && SK_SUPPORT_GPU
18
19#include <stack>
20
21class AutoDSLContext;
22
23namespace SkSL {
24
25class Compiler;
26class Context;
27class IRGenerator;
28class SymbolTable;
29class Type;
30
31namespace dsl {
32
33class ErrorHandler;
34
35/**
36 * Thread-safe class that tracks per-thread state associated with DSL output. This class is for
37 * internal use only.
38 */
39class DSLWriter {
40public:
41 DSLWriter(SkSL::Compiler* compiler);
42
43 /**
44 * Returns the Compiler used by DSL operations in the current thread.
45 */
46 static SkSL::Compiler& Compiler() {
47 return *Instance().fCompiler;
48 }
49
50 /**
51 * Returns the IRGenerator used by DSL operations in the current thread.
52 */
53 static SkSL::IRGenerator& IRGenerator();
54
55 /**
56 * Returns the Context used by DSL operations in the current thread.
57 */
58 static const SkSL::Context& Context();
59
60 /**
61 * Returns the SymbolTable of the current thread's IRGenerator.
62 */
63 static const std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
64
65 /**
66 * Reports an error if the argument is null. Returns its argument unmodified.
67 */
68 static std::unique_ptr<SkSL::Expression> Check(std::unique_ptr<SkSL::Expression> expr);
69
70 /**
71 * Sets the ErrorHandler associated with the current thread. This object will be notified when
72 * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to
73 * stderr and a fatal exception will be generated.
74 */
75 static void SetErrorHandler(ErrorHandler* errorHandler) {
76 Instance().fErrorHandler = errorHandler;
77 }
78
79 /**
80 * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler
81 * (the default), any errors will be dumped to stderr and a fatal exception will be generated.
82 */
83 static void ReportError(const char* msg);
84
85 static DSLWriter& Instance();
86
87 static void SetInstance(std::unique_ptr<DSLWriter> instance);
88
89private:
90 SkSL::Program::Settings fSettings;
91 SkSL::Compiler* fCompiler;
92 ErrorHandler* fErrorHandler = nullptr;
93
94 friend class DSLCore;
95 friend class ::AutoDSLContext;
96};
97
98} // namespace dsl
99
100} // namespace SkSL
101
102#endif