Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame^] | 1 | /* |
| 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 | |
| 21 | class AutoDSLContext; |
| 22 | |
| 23 | namespace SkSL { |
| 24 | |
| 25 | class Compiler; |
| 26 | class Context; |
| 27 | class IRGenerator; |
| 28 | class SymbolTable; |
| 29 | class Type; |
| 30 | |
| 31 | namespace dsl { |
| 32 | |
| 33 | class 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 | */ |
| 39 | class DSLWriter { |
| 40 | public: |
| 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 | |
| 89 | private: |
| 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 |