2 * Copyright 2020 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/sksl/SkSLThreadContext.h"
10 #include "include/private/SkSLProgramElement.h"
11 #include "include/sksl/SkSLPosition.h"
12 #include "src/sksl/SkSLBuiltinMap.h"
13 #include "src/sksl/SkSLCompiler.h"
14 #include "src/sksl/SkSLModifiersPool.h"
15 #include "src/sksl/SkSLParsedModule.h"
16 #include "src/sksl/SkSLPool.h"
17 #include "src/sksl/SkSLUtil.h"
18 #include "src/sksl/ir/SkSLExternalFunction.h"
19 #include "src/sksl/ir/SkSLSymbolTable.h"
21 #include <type_traits>
25 ThreadContext::ThreadContext(SkSL::Compiler* compiler, SkSL::ProgramKind kind,
26 const SkSL::ProgramSettings& settings, SkSL::ParsedModule module, bool isModule)
28 , fOldErrorReporter(*fCompiler->fContext->fErrors)
29 , fSettings(settings) {
30 fOldModifiersPool = fCompiler->fContext->fModifiersPool;
32 fOldConfig = fCompiler->fContext->fConfig;
35 if (compiler->context().fCaps.useNodePools() && settings.fDSLUseMemoryPool) {
36 fPool = Pool::Create();
37 fPool->attachToThread();
39 fModifiersPool = std::make_unique<SkSL::ModifiersPool>();
40 fCompiler->fContext->fModifiersPool = fModifiersPool.get();
43 fConfig = std::make_unique<SkSL::ProgramConfig>();
44 fConfig->fKind = kind;
45 fConfig->fSettings = settings;
46 fConfig->fIsBuiltinCode = isModule;
47 fCompiler->fContext->fConfig = fConfig.get();
48 fCompiler->fContext->fErrors = &fDefaultErrorReporter;
49 fCompiler->fContext->fBuiltins = module.fElements.get();
50 if (fCompiler->fContext->fBuiltins) {
51 fCompiler->fContext->fBuiltins->resetAlreadyIncluded();
54 fCompiler->fSymbolTable = module.fSymbols;
55 this->setupSymbolTable();
58 ThreadContext::~ThreadContext() {
60 fCompiler->fSymbolTable = nullptr;
61 fProgramElements.clear();
63 // We should only be here with a null symbol table if ReleaseProgram was called
64 SkASSERT(fProgramElements.empty());
66 fCompiler->fContext->fErrors = &fOldErrorReporter;
67 fCompiler->fContext->fConfig = fOldConfig;
68 fCompiler->fContext->fModifiersPool = fOldModifiersPool;
70 fPool->detachFromThread();
74 void ThreadContext::setupSymbolTable() {
75 SkSL::Context& context = *fCompiler->fContext;
76 SymbolTable::Push(&fCompiler->fSymbolTable, context.fConfig->fIsBuiltinCode);
78 if (fSettings.fExternalFunctions) {
79 // Add any external values to the new symbol table, so they're only visible to this Program.
80 SkSL::SymbolTable& symbols = *fCompiler->fSymbolTable;
81 for (const std::unique_ptr<ExternalFunction>& ef : *fSettings.fExternalFunctions) {
82 symbols.addWithoutOwnership(ef.get());
87 SkSL::Context& ThreadContext::Context() {
88 return Compiler().context();
91 const SkSL::ProgramSettings& ThreadContext::Settings() {
92 return Context().fConfig->fSettings;
95 std::shared_ptr<SkSL::SymbolTable>& ThreadContext::SymbolTable() {
96 return Compiler().fSymbolTable;
99 const SkSL::Modifiers* ThreadContext::Modifiers(const SkSL::Modifiers& modifiers) {
100 return Context().fModifiersPool->add(modifiers);
103 ThreadContext::RTAdjustData& ThreadContext::RTAdjustState() {
104 return Instance().fRTAdjust;
107 void ThreadContext::SetErrorReporter(ErrorReporter* errorReporter) {
108 SkASSERT(errorReporter);
109 Context().fErrors = errorReporter;
112 void ThreadContext::ReportError(std::string_view msg, Position pos) {
113 GetErrorReporter().error(msg, pos);
116 void ThreadContext::DefaultErrorReporter::handleError(std::string_view msg, Position pos) {
117 SK_ABORT("error: %.*s\nNo SkSL error reporter configured, treating this as a fatal error\n",
118 (int)msg.length(), msg.data());
121 void ThreadContext::ReportErrors(Position pos) {
122 GetErrorReporter().reportPendingErrors(pos);
125 thread_local ThreadContext* instance = nullptr;
127 bool ThreadContext::IsActive() {
128 return instance != nullptr;
131 ThreadContext& ThreadContext::Instance() {
132 SkASSERTF(instance, "dsl::Start() has not been called");
136 void ThreadContext::SetInstance(std::unique_ptr<ThreadContext> newInstance) {
137 SkASSERT((instance == nullptr) != (newInstance == nullptr));
139 instance = newInstance.release();