blob: 77becd9aa82fab7cf1fa8bd1bb17276d89b5f646 [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
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050070 static DSLExpression Construct(const SkSL::Type& type, std::vector<DSLExpression> rawArgs);
71
Ethan Nicholas95046142021-01-07 10:57:27 -050072 /**
73 * Sets the ErrorHandler associated with the current thread. This object will be notified when
74 * any DSL errors occur. With a null ErrorHandler (the default), any errors will be dumped to
75 * stderr and a fatal exception will be generated.
76 */
77 static void SetErrorHandler(ErrorHandler* errorHandler) {
78 Instance().fErrorHandler = errorHandler;
79 }
80
81 /**
82 * Notifies the current ErrorHandler that a DSL error has occurred. With a null ErrorHandler
83 * (the default), any errors will be dumped to stderr and a fatal exception will be generated.
84 */
85 static void ReportError(const char* msg);
86
87 static DSLWriter& Instance();
88
89 static void SetInstance(std::unique_ptr<DSLWriter> instance);
90
91private:
92 SkSL::Program::Settings fSettings;
93 SkSL::Compiler* fCompiler;
94 ErrorHandler* fErrorHandler = nullptr;
95
96 friend class DSLCore;
97 friend class ::AutoDSLContext;
98};
99
100} // namespace dsl
101
102} // namespace SkSL
103
104#endif