86e848d99dec4cd1631af6782d16ad47486399b8
[platform/core/uifw/dali-core.git] / dali / internal / event / common / thread-local-storage.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/thread-local-storage.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/gl-abstraction.h>
23 #include <dali/integration-api/processor-interface.h>
24 #include <dali/internal/common/core-impl.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/public-api/common/dali-common.h>
27
28 #if defined(DEBUG_ENABLED)
29 #include <dali/integration-api/debug.h>
30 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SINGLETON_SERVICE");
31
32 // Need to define own macro as the log function is not installed when this object is created so no logging is shown with DALI_LOG_INFO at construction and destruction
33 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)                           \
34   if(gSingletonServiceLogFilter && gSingletonServiceLogFilter->IsEnabledFor(level)) \
35   {                                                                                 \
36     std::string string(message);                                                    \
37     Dali::TizenPlatform::LogMessage(Debug::INFO, string);                           \
38   }
39
40 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ##__VA_ARGS__)
41 #else
42
43 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
44 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...)
45
46 #endif
47
48 namespace Dali
49 {
50 namespace Internal
51 {
52 namespace
53 {
54 thread_local ThreadLocalStorage* threadLocal    = nullptr;
55 thread_local bool                isShuttingDown = false;
56 } // namespace
57
58 ThreadLocalStorage::ThreadLocalStorage(Core* core)
59 : mCore(core)
60 {
61   DALI_ASSERT_ALWAYS(threadLocal == nullptr && "Cannot create more than one ThreadLocalStorage object");
62
63   threadLocal    = this;
64   isShuttingDown = false;
65 }
66
67 ThreadLocalStorage::~ThreadLocalStorage() = default;
68
69 void ThreadLocalStorage::Remove()
70 {
71   threadLocal    = nullptr;
72   isShuttingDown = true;
73 }
74
75 ThreadLocalStorage& ThreadLocalStorage::Get()
76 {
77   DALI_ASSERT_ALWAYS(threadLocal);
78
79   return *threadLocal;
80 }
81
82 Dali::SingletonService ThreadLocalStorage::GetSingletonService()
83 {
84   Dali::SingletonService singletonService;
85   if(threadLocal)
86   {
87     singletonService = Dali::SingletonService(threadLocal);
88   }
89   return singletonService;
90 }
91
92 bool ThreadLocalStorage::Created()
93 {
94   // see if the TLS has been set yet
95   return (threadLocal != nullptr);
96 }
97
98 bool ThreadLocalStorage::IsShuttingDown()
99 {
100   return isShuttingDown;
101 }
102
103 ThreadLocalStorage* ThreadLocalStorage::GetInternal()
104 {
105   return threadLocal;
106 }
107
108 Dali::Integration::PlatformAbstraction& ThreadLocalStorage::GetPlatformAbstraction()
109 {
110   return mCore->GetPlatform();
111 }
112
113 SceneGraph::UpdateManager& ThreadLocalStorage::GetUpdateManager()
114 {
115   return mCore->GetUpdateManager();
116 }
117
118 NotificationManager& ThreadLocalStorage::GetNotificationManager()
119 {
120   return mCore->GetNotificationManager();
121 }
122
123 ShaderFactory& ThreadLocalStorage::GetShaderFactory()
124 {
125   return mCore->GetShaderFactory();
126 }
127
128 StagePtr ThreadLocalStorage::GetCurrentStage()
129 {
130   return mCore->GetCurrentStage();
131 }
132
133 GestureEventProcessor& ThreadLocalStorage::GetGestureEventProcessor()
134 {
135   return mCore->GetGestureEventProcessor();
136 }
137
138 RelayoutController& ThreadLocalStorage::GetRelayoutController()
139 {
140   return mCore->GetRelayoutController();
141 }
142
143 ObjectRegistry& ThreadLocalStorage::GetObjectRegistry()
144 {
145   return mCore->GetObjectRegistry();
146 }
147
148 EventThreadServices& ThreadLocalStorage::GetEventThreadServices()
149 {
150   return mCore->GetEventThreadServices();
151 }
152
153 PropertyNotificationManager& ThreadLocalStorage::GetPropertyNotificationManager()
154 {
155   return mCore->GetPropertyNotificationManager();
156 }
157
158 AnimationPlaylist& ThreadLocalStorage::GetAnimationPlaylist()
159 {
160   return mCore->GetAnimationPlaylist();
161 }
162
163 bool ThreadLocalStorage::IsBlendEquationSupported(DevelBlendEquation::Type blendEquation)
164 {
165   return mCore->GetGlAbstraction().IsBlendEquationSupported(blendEquation);
166 }
167
168 std::string ThreadLocalStorage::GetShaderVersionPrefix()
169 {
170   return mCore->GetGlAbstraction().GetShaderVersionPrefix();
171 }
172
173 std::string ThreadLocalStorage::GetVertexShaderPrefix()
174 {
175   return mCore->GetGlAbstraction().GetVertexShaderPrefix();
176 }
177
178 std::string ThreadLocalStorage::GetFragmentShaderPrefix()
179 {
180   return mCore->GetGlAbstraction().GetFragmentShaderPrefix();
181 }
182
183 void ThreadLocalStorage::AddScene(Scene* scene)
184 {
185   mCore->AddScene(scene);
186 }
187
188 void ThreadLocalStorage::RemoveScene(Scene* scene)
189 {
190   mCore->RemoveScene(scene);
191 }
192
193 void ThreadLocalStorage::Register(const std::type_info& info, BaseHandle singleton)
194 {
195   if(singleton)
196   {
197     DALI_LOG_SINGLETON_SERVICE(Debug::General, "Singleton Added: %s\n", info.name());
198     mSingletonContainer.push_back(SingletonPair(info.name(), singleton));
199
200     Integration::Processor* processor = dynamic_cast<Integration::Processor*>(&singleton.GetBaseObject());
201     if(processor)
202     {
203       mCore->RegisterProcessor(*processor);
204     }
205   }
206 }
207
208 void ThreadLocalStorage::UnregisterAll()
209 {
210   mSingletonContainer.clear();
211 }
212
213 BaseHandle ThreadLocalStorage::GetSingleton(const std::type_info& info) const
214 {
215   BaseHandle object;
216
217   const SingletonContainer::const_iterator end = mSingletonContainer.end();
218   for(SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter)
219   {
220     // comparing the addresses as these are allocated statically per library
221     if((*iter).first == info.name())
222     {
223       object = (*iter).second;
224     }
225   }
226
227   return object;
228 }
229
230 } // namespace Internal
231
232 } // namespace Dali