blob: 914626e0221785de253d0254d7d68ee1a1a81b87 [file] [log] [blame]
Ethan Nicholasc8452722021-10-07 10:47:32 -04001/*
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 Lubick08ece0c2022-03-18 16:40:58 -040011#include "include/core/SkTypes.h"
Kevin Lubick08ece0c2022-03-18 16:40:58 -040012#include "include/private/SkSLProgramKind.h"
13#include "include/sksl/SkSLErrorReporter.h"
Kevin Lubick08ece0c2022-03-18 16:40:58 -040014#include "src/sksl/SkSLContext.h"
Kevin Lubick08ece0c2022-03-18 16:40:58 -040015#include "src/sksl/SkSLProgramSettings.h"
Ethan Nicholasc8452722021-10-07 10:47:32 -040016#include "src/sksl/ir/SkSLProgram.h"
Kevin Lubick08ece0c2022-03-18 16:40:58 -040017
Kevin Lubick08ece0c2022-03-18 16:40:58 -040018#include <memory>
John Stiles074a0162022-02-01 15:31:57 -050019#include <string_view>
Kevin Lubick08ece0c2022-03-18 16:40:58 -040020#include <vector>
21
Ethan Nicholasc8452722021-10-07 10:47:32 -040022namespace SkSL {
23
24class Compiler;
Kevin Lubick08ece0c2022-03-18 16:40:58 -040025class ModifiersPool;
26class Pool;
Ethan Nicholas885c1d32022-04-08 11:37:08 -040027class Position;
Ethan Nicholasc8452722021-10-07 10:47:32 -040028class ProgramElement;
29class SymbolTable;
Ethan Nicholasc8452722021-10-07 10:47:32 -040030class Variable;
Kevin Lubick08ece0c2022-03-18 16:40:58 -040031struct Modifiers;
32struct ParsedModule;
Ethan Nicholasc8452722021-10-07 10:47:32 -040033
34namespace dsl {
35
36class DSLCore;
37class DSLWriter;
38
39} // namespace dsl
40
41/**
42 * Thread-safe class that tracks per-thread state associated with SkSL output.
43 */
44class ThreadContext {
45public:
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 Nicholasc8452722021-10-07 10:47:32 -040062 * 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 Stilesd42c59d2022-02-03 15:30:53 -050069 static const SkSL::ProgramSettings& Settings();
Ethan Nicholasc8452722021-10-07 10:47:32 -040070
71 /**
72 * Returns the Program::Inputs used by the current thread.
73 */
Ethan Nicholasc41ac912021-10-15 14:20:34 -040074 static SkSL::Program::Inputs& Inputs() { return Instance().fInputs; }
Ethan Nicholasc8452722021-10-07 10:47:32 -040075
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 Nicholas0e55a132021-10-15 11:30:57 -040088 * Returns the current SymbolTable.
Ethan Nicholasc8452722021-10-07 10:47:32 -040089 */
Ethan Nicholas0e55a132021-10-15 11:30:57 -040090 static std::shared_ptr<SkSL::SymbolTable>& SymbolTable();
Ethan Nicholasc8452722021-10-07 10:47:32 -040091
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 Stilesd42c59d2022-02-03 15:30:53 -0500105 static const std::unique_ptr<ProgramConfig>& GetProgramConfig() { return Instance().fConfig; }
Ethan Nicholasc8452722021-10-07 10:47:32 -0400106
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 Nicholasc9fe02f2021-10-13 13:55:12 -0400115 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 Nicholasc8452722021-10-07 10:47:32 -0400129 /**
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 Nicholas885c1d32022-04-08 11:37:08 -0400143 static void ReportError(std::string_view msg, Position pos = {});
Ethan Nicholasc8452722021-10-07 10:47:32 -0400144
Ethan Nicholasc8452722021-10-07 10:47:32 -0400145 static ThreadContext& Instance();
146
147 static void SetInstance(std::unique_ptr<ThreadContext> instance);
148
149private:
150 class DefaultErrorReporter : public ErrorReporter {
Ethan Nicholas9cd84562022-03-09 10:22:44 -0500151 void handleError(std::string_view msg, Position pos) override;
Ethan Nicholasc8452722021-10-07 10:47:32 -0400152 };
153
Ethan Nicholasab0a13d2021-10-18 10:39:20 -0400154 void setupSymbolTable();
155
Ethan Nicholasc8452722021-10-07 10:47:32 -0400156 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 Nicholasc9fe02f2021-10-13 13:55:12 -0400167 RTAdjustData fRTAdjust;
Ethan Nicholasc41ac912021-10-15 14:20:34 -0400168 Program::Inputs fInputs;
169
Ethan Nicholasc8452722021-10-07 10:47:32 -0400170 friend class dsl::DSLCore;
171 friend class dsl::DSLWriter;
172};
173
174} // namespace SkSL
175
176#endif