Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / skia / src / sksl / SkSLThreadContext.cpp
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 #include "src/sksl/SkSLThreadContext.h"
9
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"
20
21 #include <type_traits>
22
23 namespace SkSL {
24
25 ThreadContext::ThreadContext(SkSL::Compiler* compiler, SkSL::ProgramKind kind,
26         const SkSL::ProgramSettings& settings, SkSL::ParsedModule module, bool isModule)
27     : fCompiler(compiler)
28     , fOldErrorReporter(*fCompiler->fContext->fErrors)
29     , fSettings(settings) {
30     fOldModifiersPool = fCompiler->fContext->fModifiersPool;
31
32     fOldConfig = fCompiler->fContext->fConfig;
33
34     if (!isModule) {
35         if (compiler->context().fCaps.useNodePools() && settings.fDSLUseMemoryPool) {
36             fPool = Pool::Create();
37             fPool->attachToThread();
38         }
39         fModifiersPool = std::make_unique<SkSL::ModifiersPool>();
40         fCompiler->fContext->fModifiersPool = fModifiersPool.get();
41     }
42
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();
52     }
53
54     fCompiler->fSymbolTable = module.fSymbols;
55     this->setupSymbolTable();
56 }
57
58 ThreadContext::~ThreadContext() {
59     if (SymbolTable()) {
60         fCompiler->fSymbolTable = nullptr;
61         fProgramElements.clear();
62     } else {
63         // We should only be here with a null symbol table if ReleaseProgram was called
64         SkASSERT(fProgramElements.empty());
65     }
66     fCompiler->fContext->fErrors = &fOldErrorReporter;
67     fCompiler->fContext->fConfig = fOldConfig;
68     fCompiler->fContext->fModifiersPool = fOldModifiersPool;
69     if (fPool) {
70         fPool->detachFromThread();
71     }
72 }
73
74 void ThreadContext::setupSymbolTable() {
75     SkSL::Context& context = *fCompiler->fContext;
76     SymbolTable::Push(&fCompiler->fSymbolTable, context.fConfig->fIsBuiltinCode);
77
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());
83         }
84     }
85 }
86
87 SkSL::Context& ThreadContext::Context() {
88     return Compiler().context();
89 }
90
91 const SkSL::ProgramSettings& ThreadContext::Settings() {
92     return Context().fConfig->fSettings;
93 }
94
95 std::shared_ptr<SkSL::SymbolTable>& ThreadContext::SymbolTable() {
96     return Compiler().fSymbolTable;
97 }
98
99 const SkSL::Modifiers* ThreadContext::Modifiers(const SkSL::Modifiers& modifiers) {
100     return Context().fModifiersPool->add(modifiers);
101 }
102
103 ThreadContext::RTAdjustData& ThreadContext::RTAdjustState() {
104     return Instance().fRTAdjust;
105 }
106
107 void ThreadContext::SetErrorReporter(ErrorReporter* errorReporter) {
108     SkASSERT(errorReporter);
109     Context().fErrors = errorReporter;
110 }
111
112 void ThreadContext::ReportError(std::string_view msg, Position pos) {
113     GetErrorReporter().error(msg, pos);
114 }
115
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());
119 }
120
121 void ThreadContext::ReportErrors(Position pos) {
122     GetErrorReporter().reportPendingErrors(pos);
123 }
124
125 thread_local ThreadContext* instance = nullptr;
126
127 bool ThreadContext::IsActive() {
128     return instance != nullptr;
129 }
130
131 ThreadContext& ThreadContext::Instance() {
132     SkASSERTF(instance, "dsl::Start() has not been called");
133     return *instance;
134 }
135
136 void ThreadContext::SetInstance(std::unique_ptr<ThreadContext> newInstance) {
137     SkASSERT((instance == nullptr) != (newInstance == nullptr));
138     delete instance;
139     instance = newInstance.release();
140 }
141
142 } // namespace SkSL