25b3d0602ca98a5607d467f42b01630161110d56
[platform/core/uifw/dali-core.git] / dali / internal / event / common / thread-local-storage.cpp
1 /*
2  * Copyright (c) 2022 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 }
65
66 ThreadLocalStorage::~ThreadLocalStorage() = default;
67
68 void ThreadLocalStorage::Remove()
69 {
70   threadLocal    = nullptr;
71   isShuttingDown = true;
72 }
73
74 ThreadLocalStorage& ThreadLocalStorage::Get()
75 {
76   DALI_ASSERT_ALWAYS(threadLocal);
77
78   return *threadLocal;
79 }
80
81 Dali::SingletonService ThreadLocalStorage::GetSingletonService()
82 {
83   Dali::SingletonService singletonService;
84   if(threadLocal)
85   {
86     singletonService = Dali::SingletonService(threadLocal);
87   }
88   return singletonService;
89 }
90
91 bool ThreadLocalStorage::Created()
92 {
93   // see if the TLS has been set yet
94   return (threadLocal != nullptr);
95 }
96
97 bool ThreadLocalStorage::IsSuttingDown()
98 {
99   return isShuttingDown;
100 }
101
102 ThreadLocalStorage* ThreadLocalStorage::GetInternal()
103 {
104   return threadLocal;
105 }
106
107 Dali::Integration::PlatformAbstraction& ThreadLocalStorage::GetPlatformAbstraction()
108 {
109   return mCore->GetPlatform();
110 }
111
112 SceneGraph::UpdateManager& ThreadLocalStorage::GetUpdateManager()
113 {
114   return mCore->GetUpdateManager();
115 }
116
117 NotificationManager& ThreadLocalStorage::GetNotificationManager()
118 {
119   return mCore->GetNotificationManager();
120 }
121
122 ShaderFactory& ThreadLocalStorage::GetShaderFactory()
123 {
124   return mCore->GetShaderFactory();
125 }
126
127 StagePtr ThreadLocalStorage::GetCurrentStage()
128 {
129   return mCore->GetCurrentStage();
130 }
131
132 GestureEventProcessor& ThreadLocalStorage::GetGestureEventProcessor()
133 {
134   return mCore->GetGestureEventProcessor();
135 }
136
137 RelayoutController& ThreadLocalStorage::GetRelayoutController()
138 {
139   return mCore->GetRelayoutController();
140 }
141
142 ObjectRegistry& ThreadLocalStorage::GetObjectRegistry()
143 {
144   return mCore->GetObjectRegistry();
145 }
146
147 EventThreadServices& ThreadLocalStorage::GetEventThreadServices()
148 {
149   return mCore->GetEventThreadServices();
150 }
151
152 PropertyNotificationManager& ThreadLocalStorage::GetPropertyNotificationManager()
153 {
154   return mCore->GetPropertyNotificationManager();
155 }
156
157 AnimationPlaylist& ThreadLocalStorage::GetAnimationPlaylist()
158 {
159   return mCore->GetAnimationPlaylist();
160 }
161
162 bool ThreadLocalStorage::IsBlendEquationSupported(DevelBlendEquation::Type blendEquation)
163 {
164   return mCore->GetGlAbstraction().IsBlendEquationSupported(blendEquation);
165 }
166
167 std::string ThreadLocalStorage::GetShaderVersionPrefix()
168 {
169   return mCore->GetGlAbstraction().GetShaderVersionPrefix();
170 }
171
172 std::string ThreadLocalStorage::GetVertexShaderPrefix()
173 {
174   return mCore->GetGlAbstraction().GetVertexShaderPrefix();
175 }
176
177 std::string ThreadLocalStorage::GetFragmentShaderPrefix()
178 {
179   return mCore->GetGlAbstraction().GetFragmentShaderPrefix();
180 }
181
182 void ThreadLocalStorage::AddScene(Scene* scene)
183 {
184   mCore->AddScene(scene);
185 }
186
187 void ThreadLocalStorage::RemoveScene(Scene* scene)
188 {
189   mCore->RemoveScene(scene);
190 }
191
192 void ThreadLocalStorage::Register(const std::type_info& info, BaseHandle singleton)
193 {
194   if(singleton)
195   {
196     DALI_LOG_SINGLETON_SERVICE(Debug::General, "Singleton Added: %s\n", info.name());
197     mSingletonContainer.push_back(SingletonPair(info.name(), singleton));
198
199     Integration::Processor* processor = dynamic_cast<Integration::Processor*>(&singleton.GetBaseObject());
200     if(processor)
201     {
202       mCore->RegisterProcessor(*processor);
203     }
204   }
205 }
206
207 void ThreadLocalStorage::UnregisterAll()
208 {
209   mSingletonContainer.clear();
210 }
211
212 BaseHandle ThreadLocalStorage::GetSingleton(const std::type_info& info) const
213 {
214   BaseHandle object;
215
216   const SingletonContainer::const_iterator end = mSingletonContainer.end();
217   for(SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter)
218   {
219     // comparing the addresses as these are allocated statically per library
220     if((*iter).first == info.name())
221     {
222       object = (*iter).second;
223     }
224   }
225
226   return object;
227 }
228
229 } // namespace Internal
230
231 } // namespace Dali