Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 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 | #ifndef SKSL_THREADCONTEXT |
| 9 | #define SKSL_THREADCONTEXT |
| 10 | |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 12 | #include "include/private/SkSLProgramKind.h" |
| 13 | #include "include/sksl/SkSLErrorReporter.h" |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLContext.h" |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLProgramSettings.h" |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 16 | #include "src/sksl/ir/SkSLProgram.h" |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 17 | |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 18 | #include <memory> |
John Stiles | 074a016 | 2022-02-01 15:31:57 -0500 | [diff] [blame] | 19 | #include <string_view> |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
| 24 | class Compiler; |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 25 | class ModifiersPool; |
| 26 | class Pool; |
Ethan Nicholas | 885c1d3 | 2022-04-08 11:37:08 -0400 | [diff] [blame] | 27 | class Position; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 28 | class ProgramElement; |
| 29 | class SymbolTable; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 30 | class Variable; |
Kevin Lubick | 08ece0c | 2022-03-18 16:40:58 -0400 | [diff] [blame] | 31 | struct Modifiers; |
| 32 | struct ParsedModule; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 33 | |
| 34 | namespace dsl { |
| 35 | |
| 36 | class DSLCore; |
| 37 | class DSLWriter; |
| 38 | |
| 39 | } // namespace dsl |
| 40 | |
| 41 | /** |
| 42 | * Thread-safe class that tracks per-thread state associated with SkSL output. |
| 43 | */ |
| 44 | class ThreadContext { |
| 45 | public: |
| 46 | ThreadContext(SkSL::Compiler* compiler, SkSL::ProgramKind kind, |
| 47 | const SkSL::ProgramSettings& settings, SkSL::ParsedModule module, bool isModule); |
| 48 | |
| 49 | ~ThreadContext(); |
| 50 | |
| 51 | /** |
| 52 | * Returns true if the DSL has been started. |
| 53 | */ |
| 54 | static bool IsActive(); |
| 55 | |
| 56 | /** |
| 57 | * Returns the Compiler used by DSL operations in the current thread. |
| 58 | */ |
| 59 | static SkSL::Compiler& Compiler() { return *Instance().fCompiler; } |
| 60 | |
| 61 | /** |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 62 | * Returns the Context used by DSL operations in the current thread. |
| 63 | */ |
| 64 | static SkSL::Context& Context(); |
| 65 | |
| 66 | /** |
| 67 | * Returns the Settings used by DSL operations in the current thread. |
| 68 | */ |
John Stiles | d42c59d | 2022-02-03 15:30:53 -0500 | [diff] [blame] | 69 | static const SkSL::ProgramSettings& Settings(); |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Returns the Program::Inputs used by the current thread. |
| 73 | */ |
Ethan Nicholas | c41ac91 | 2021-10-15 14:20:34 -0400 | [diff] [blame] | 74 | static SkSL::Program::Inputs& Inputs() { return Instance().fInputs; } |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Returns the collection to which DSL program elements in this thread should be appended. |
| 78 | */ |
| 79 | static std::vector<std::unique_ptr<SkSL::ProgramElement>>& ProgramElements() { |
| 80 | return Instance().fProgramElements; |
| 81 | } |
| 82 | |
| 83 | static std::vector<const ProgramElement*>& SharedElements() { |
| 84 | return Instance().fSharedElements; |
| 85 | } |
| 86 | |
| 87 | /** |
Ethan Nicholas | 0e55a13 | 2021-10-15 11:30:57 -0400 | [diff] [blame] | 88 | * Returns the current SymbolTable. |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 89 | */ |
Ethan Nicholas | 0e55a13 | 2021-10-15 11:30:57 -0400 | [diff] [blame] | 90 | static std::shared_ptr<SkSL::SymbolTable>& SymbolTable(); |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Returns the current memory pool. |
| 94 | */ |
| 95 | static std::unique_ptr<Pool>& MemoryPool() { return Instance().fPool; } |
| 96 | |
| 97 | /** |
| 98 | * Returns the current modifiers pool. |
| 99 | */ |
| 100 | static std::unique_ptr<ModifiersPool>& GetModifiersPool() { return Instance().fModifiersPool; } |
| 101 | |
| 102 | /** |
| 103 | * Returns the current ProgramConfig. |
| 104 | */ |
John Stiles | d42c59d | 2022-02-03 15:30:53 -0500 | [diff] [blame] | 105 | static const std::unique_ptr<ProgramConfig>& GetProgramConfig() { return Instance().fConfig; } |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 106 | |
| 107 | static bool IsModule() { return GetProgramConfig()->fIsBuiltinCode; } |
| 108 | |
| 109 | /** |
| 110 | * Returns the final pointer to a pooled Modifiers object that should be used to represent the |
| 111 | * given modifiers. |
| 112 | */ |
| 113 | static const SkSL::Modifiers* Modifiers(const SkSL::Modifiers& modifiers); |
| 114 | |
Ethan Nicholas | c9fe02f | 2021-10-13 13:55:12 -0400 | [diff] [blame] | 115 | struct RTAdjustData { |
| 116 | // Points to a standalone sk_RTAdjust variable, if one exists. |
| 117 | const Variable* fVar = nullptr; |
| 118 | // Points to the interface block containing an sk_RTAdjust field, if one exists. |
| 119 | const Variable* fInterfaceBlock = nullptr; |
| 120 | // If fInterfaceBlock is non-null, contains the index of the sk_RTAdjust field within it. |
| 121 | int fFieldIndex = -1; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Returns a struct containing information about the RTAdjust variable. |
| 126 | */ |
| 127 | static RTAdjustData& RTAdjustState(); |
| 128 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 129 | /** |
| 130 | * Returns the ErrorReporter associated with the current thread. This object will be notified |
| 131 | * when any DSL errors occur. |
| 132 | */ |
| 133 | static ErrorReporter& GetErrorReporter() { |
| 134 | return *Context().fErrors; |
| 135 | } |
| 136 | |
| 137 | static void SetErrorReporter(ErrorReporter* errorReporter); |
| 138 | |
| 139 | /** |
| 140 | * Notifies the current ErrorReporter that an error has occurred. The default error handler |
| 141 | * prints the message to stderr and aborts. |
| 142 | */ |
Ethan Nicholas | 885c1d3 | 2022-04-08 11:37:08 -0400 | [diff] [blame] | 143 | static void ReportError(std::string_view msg, Position pos = {}); |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 144 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 145 | static ThreadContext& Instance(); |
| 146 | |
| 147 | static void SetInstance(std::unique_ptr<ThreadContext> instance); |
| 148 | |
| 149 | private: |
| 150 | class DefaultErrorReporter : public ErrorReporter { |
Ethan Nicholas | 9cd8456 | 2022-03-09 10:22:44 -0500 | [diff] [blame] | 151 | void handleError(std::string_view msg, Position pos) override; |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 152 | }; |
| 153 | |
Ethan Nicholas | ab0a13d | 2021-10-18 10:39:20 -0400 | [diff] [blame] | 154 | void setupSymbolTable(); |
| 155 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 156 | std::unique_ptr<SkSL::ProgramConfig> fConfig; |
| 157 | std::unique_ptr<SkSL::ModifiersPool> fModifiersPool; |
| 158 | SkSL::Compiler* fCompiler; |
| 159 | std::unique_ptr<Pool> fPool; |
| 160 | SkSL::ProgramConfig* fOldConfig; |
| 161 | SkSL::ModifiersPool* fOldModifiersPool; |
| 162 | std::vector<std::unique_ptr<SkSL::ProgramElement>> fProgramElements; |
| 163 | std::vector<const SkSL::ProgramElement*> fSharedElements; |
| 164 | DefaultErrorReporter fDefaultErrorReporter; |
| 165 | ErrorReporter& fOldErrorReporter; |
| 166 | ProgramSettings fSettings; |
Ethan Nicholas | c9fe02f | 2021-10-13 13:55:12 -0400 | [diff] [blame] | 167 | RTAdjustData fRTAdjust; |
Ethan Nicholas | c41ac91 | 2021-10-15 14:20:34 -0400 | [diff] [blame] | 168 | Program::Inputs fInputs; |
| 169 | |
Ethan Nicholas | c845272 | 2021-10-07 10:47:32 -0400 | [diff] [blame] | 170 | friend class dsl::DSLCore; |
| 171 | friend class dsl::DSLWriter; |
| 172 | }; |
| 173 | |
| 174 | } // namespace SkSL |
| 175 | |
| 176 | #endif |