Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / common / thread-local-storage.cpp
1 /*
2  * Copyright (c) 2019 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/internal/common/core-impl.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/integration-api/processor-interface.h>
26
27 #if defined(DEBUG_ENABLED)
28 #include <dali/integration-api/debug.h>
29 Debug::Filter* gSingletonServiceLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SINGLETON_SERVICE" );
30
31 // 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
32 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)                        \
33     if(gSingletonServiceLogFilter && gSingletonServiceLogFilter->IsEnabledFor(level)) { std::string string(message); Dali::TizenPlatform::LogMessage( Debug::DebugInfo, string );  }
34
35 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...) DALI_LOG_INFO(gSingletonServiceLogFilter, level, format, ## __VA_ARGS__ )
36 #else
37
38 #define DALI_LOG_SINGLETON_SERVICE_DIRECT(level, message)
39 #define DALI_LOG_SINGLETON_SERVICE(level, format, ...)
40
41 #endif
42
43 namespace Dali
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51 thread_local ThreadLocalStorage* threadLocal = nullptr;
52 }
53
54 ThreadLocalStorage::ThreadLocalStorage(Core* core)
55 : mCore( core )
56 {
57   DALI_ASSERT_ALWAYS( threadLocal == nullptr && "Cannot create more than one ThreadLocalStorage object" );
58
59   threadLocal = this;
60 }
61
62 ThreadLocalStorage::~ThreadLocalStorage() = default;
63
64 void ThreadLocalStorage::Remove()
65 {
66   threadLocal = nullptr;
67 }
68
69 ThreadLocalStorage& ThreadLocalStorage::Get()
70 {
71   DALI_ASSERT_ALWAYS(threadLocal);
72
73   return *threadLocal;
74 }
75
76 Dali::SingletonService ThreadLocalStorage::GetSingletonService()
77 {
78   Dali::SingletonService singletonService;
79   if ( threadLocal )
80   {
81     singletonService = Dali::SingletonService( threadLocal );
82   }
83   return singletonService;
84 }
85
86 bool ThreadLocalStorage::Created()
87 {
88   // see if the TLS has been set yet
89   return (threadLocal != nullptr);
90 }
91
92 ThreadLocalStorage* ThreadLocalStorage::GetInternal()
93 {
94   return threadLocal;
95 }
96
97 Dali::Integration::PlatformAbstraction& ThreadLocalStorage::GetPlatformAbstraction()
98 {
99   return mCore->GetPlatform();
100 }
101
102 SceneGraph::UpdateManager& ThreadLocalStorage::GetUpdateManager()
103 {
104   return mCore->GetUpdateManager();
105 }
106
107 NotificationManager& ThreadLocalStorage::GetNotificationManager()
108 {
109   return mCore->GetNotificationManager();
110 }
111
112 ShaderFactory& ThreadLocalStorage::GetShaderFactory()
113 {
114   return mCore->GetShaderFactory();
115 }
116
117 StagePtr ThreadLocalStorage::GetCurrentStage()
118 {
119   return mCore->GetCurrentStage();
120 }
121
122 GestureEventProcessor& ThreadLocalStorage::GetGestureEventProcessor()
123 {
124   return mCore->GetGestureEventProcessor();
125 }
126
127 RelayoutController& ThreadLocalStorage::GetRelayoutController()
128 {
129   return mCore->GetRelayoutController();
130 }
131
132 ObjectRegistry& ThreadLocalStorage::GetObjectRegistry()
133 {
134   return mCore->GetObjectRegistry();
135 }
136
137 EventThreadServices& ThreadLocalStorage::GetEventThreadServices()
138 {
139   return mCore->GetEventThreadServices();
140 }
141
142 PropertyNotificationManager& ThreadLocalStorage::GetPropertyNotificationManager()
143 {
144   return mCore->GetPropertyNotificationManager();
145 }
146
147 AnimationPlaylist& ThreadLocalStorage::GetAnimationPlaylist()
148 {
149   return mCore->GetAnimationPlaylist();
150 }
151
152 bool ThreadLocalStorage::IsBlendEquationSupported( DevelBlendEquation::Type blendEquation )
153 {
154   return mCore->GetGlAbstraction().IsBlendEquationSupported( blendEquation );
155 }
156
157 std::string ThreadLocalStorage::GetShaderVersionPrefix()
158 {
159   return mCore->GetGlAbstraction().GetShaderVersionPrefix();
160 }
161
162 std::string ThreadLocalStorage::GetVertexShaderPrefix()
163 {
164   return mCore->GetGlAbstraction().GetVertexShaderPrefix();
165 }
166
167 std::string ThreadLocalStorage::GetFragmentShaderPrefix()
168 {
169   return mCore->GetGlAbstraction().GetFragmentShaderPrefix();
170 }
171
172 void ThreadLocalStorage::AddScene( Scene* scene )
173 {
174   mCore->AddScene( scene );
175 }
176
177 void ThreadLocalStorage::RemoveScene( Scene* scene )
178 {
179   mCore->RemoveScene( scene );
180 }
181
182 void ThreadLocalStorage::Register( const std::type_info& info, BaseHandle singleton )
183 {
184   if( singleton )
185   {
186     DALI_LOG_SINGLETON_SERVICE( Debug::General, "Singleton Added: %s\n", info.name() );
187     mSingletonContainer.push_back( SingletonPair( info.name(), singleton ) );
188
189     Integration::Processor* processor = dynamic_cast<Integration::Processor*>( &singleton.GetBaseObject() );
190     if( processor )
191     {
192       mCore->RegisterProcessor( *processor );
193     }
194   }
195 }
196
197 void ThreadLocalStorage::UnregisterAll( )
198 {
199   mSingletonContainer.clear();
200 }
201
202 BaseHandle ThreadLocalStorage::GetSingleton( const std::type_info& info ) const
203 {
204   BaseHandle object;
205
206   const SingletonContainer::const_iterator end = mSingletonContainer.end();
207   for( SingletonContainer::const_iterator iter = mSingletonContainer.begin(); iter != end; ++iter )
208   {
209     // comparing the addresses as these are allocated statically per library
210     if( ( *iter ).first == info.name() )
211     {
212       object = ( *iter ).second;
213     }
214   }
215
216   return object;
217 }
218
219 } // namespace Internal
220
221 } // namespace Dali