873dbf543c1abda60ec950482ecd112729e4bc88
[platform/core/uifw/dali-core.git] / dali / internal / common / core-impl.cpp
1 /*
2  * Copyright (c) 2017 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/common/core-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/system-overlay.h>
23 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/events/event.h>
26 #include <dali/integration-api/gl-sync-abstraction.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/integration-api/render-controller.h>
29
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/animation/animation-playlist.h>
32 #include <dali/internal/event/common/notification-manager.h>
33 #include <dali/internal/event/common/property-notification-manager.h>
34 #include <dali/internal/event/common/stage-impl.h>
35 #include <dali/internal/event/common/thread-local-storage.h>
36 #include <dali/internal/event/common/type-registry-impl.h>
37 #include <dali/internal/event/effects/shader-factory.h>
38 #include <dali/internal/event/events/event-processor.h>
39 #include <dali/internal/event/events/gesture-event-processor.h>
40 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
41 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
42
43 #include <dali/internal/update/common/discard-queue.h>
44 #include <dali/internal/update/manager/update-manager.h>
45 #include <dali/internal/update/manager/render-task-processor.h>
46
47 #include <dali/internal/render/common/performance-monitor.h>
48 #include <dali/internal/render/common/render-manager.h>
49 #include <dali/internal/render/gl-resources/context.h>
50
51 using Dali::Internal::SceneGraph::UpdateManager;
52 using Dali::Internal::SceneGraph::RenderManager;
53 using Dali::Internal::SceneGraph::DiscardQueue;
54 using Dali::Internal::SceneGraph::RenderQueue;
55
56 namespace
57 {
58 // The Update for frame N+1 may be processed whilst frame N is being rendered.
59 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
60
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
63 #endif
64 }
65
66 namespace Dali
67 {
68
69 namespace Internal
70 {
71
72 using Integration::RenderController;
73 using Integration::PlatformAbstraction;
74 using Integration::GlSyncAbstraction;
75 using Integration::GestureManager;
76 using Integration::GlAbstraction;
77 using Integration::Event;
78 using Integration::UpdateStatus;
79 using Integration::RenderStatus;
80
81 Core::Core( RenderController& renderController,
82             PlatformAbstraction& platform,
83             GlAbstraction& glAbstraction,
84             GlSyncAbstraction& glSyncAbstraction,
85             GestureManager& gestureManager,
86             ResourcePolicy::DataRetention dataRetentionPolicy,
87             bool renderToFboEnabled )
88 : mRenderController( renderController ),
89   mPlatform(platform),
90   mProcessingEvent(false)
91 {
92   // Create the thread local storage
93   CreateThreadLocalStorage();
94
95   // This does nothing until Core is built with --enable-performance-monitor
96   PERFORMANCE_MONITOR_INIT( platform );
97
98   mNotificationManager = new NotificationManager();
99
100   mAnimationPlaylist = AnimationPlaylist::New();
101
102   mPropertyNotificationManager = PropertyNotificationManager::New();
103
104   mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
105
106   mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction );
107
108   RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
109
110   mDiscardQueue = new DiscardQueue( renderQueue );
111
112   mUpdateManager = new UpdateManager( *mNotificationManager,
113                                       *mAnimationPlaylist,
114                                       *mPropertyNotificationManager,
115                                       *mDiscardQueue,
116                                        renderController,
117                                       *mRenderManager,
118                                        renderQueue,
119                                       *mRenderTaskProcessor );
120
121   mRenderManager->SetShaderSaver( *mUpdateManager );
122
123   mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
124
125   // This must be called after stage is created but before stage initialization
126   mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
127
128   mStage->Initialize( renderToFboEnabled );
129
130   mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
131   mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
132
133   mShaderFactory = new ShaderFactory();
134   mUpdateManager->SetShaderSaver( *mShaderFactory );
135
136   GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
137 }
138
139 Core::~Core()
140 {
141   /*
142    * The order of destructing these singletons is important!!!
143    */
144
145   // clear the thread local storage first
146   // allows core to be created / deleted many times in the same thread (how TET cases work).
147   // Do this before mStage.Reset() so Stage::IsInstalled() returns false
148   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
149   if( tls )
150   {
151     tls->Remove();
152     delete tls;
153   }
154
155   // Stop relayout requests being raised on stage destruction
156   mRelayoutController.Reset();
157
158   // Clean-up stage - remove default camera and root layer
159   mStage->Uninitialize();
160
161   // remove (last?) reference to stage
162   mStage.Reset();
163
164 }
165
166 Integration::ContextNotifierInterface* Core::GetContextNotifier()
167 {
168   return mStage.Get();
169 }
170
171 void Core::RecoverFromContextLoss()
172 {
173   DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
174
175   mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
176 }
177
178 void Core::ContextCreated()
179 {
180   mRenderManager->ContextCreated();
181 }
182
183 void Core::ContextDestroyed()
184 {
185   mRenderManager->ContextDestroyed();
186 }
187
188 void Core::SurfaceResized( unsigned int width, unsigned int height )
189 {
190   mStage->SurfaceResized( width, height );
191
192   // The stage-size may be less than surface-size (reduced by top-margin)
193   Vector2 size = mStage->GetSize();
194   mRelayoutController->SetStageSize( size.width, size.height );
195 }
196
197 void Core::SetTopMargin( unsigned int margin )
198 {
199   mStage->SetTopMargin( margin );
200
201   // The stage-size may be less than surface-size (reduced by top-margin)
202   Vector2 size = mStage->GetSize();
203   mRelayoutController->SetStageSize( size.width, size.height );
204 }
205
206 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
207 {
208   mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
209 }
210
211 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
212 {
213   // set the time delta so adaptor can easily print FPS with a release build with 0 as
214   // it is cached by frametime
215   status.secondsFromLastFrame = elapsedSeconds;
216
217   // Render returns true when there are updates on the stage or one or more animations are completed.
218   // Use the estimated time diff till we render as the elapsed time.
219   status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
220                                                 lastVSyncTimeMilliseconds,
221                                                 nextVSyncTimeMilliseconds,
222                                                 renderToFboEnabled,
223                                                 isRenderingToFbo );
224
225   // Check the Notification Manager message queue to set needsNotification
226   status.needsNotification = mNotificationManager->MessagesToProcess();
227
228   // No need to keep update running if there are notifications to process.
229   // Any message to update will wake it up anyways
230 }
231
232 void Core::Render( RenderStatus& status )
233 {
234   mRenderManager->Render( status );
235 }
236
237 void Core::SceneCreated()
238 {
239   mStage->EmitSceneCreatedSignal();
240
241   mRelayoutController->OnApplicationSceneCreated();
242 }
243
244 void Core::QueueEvent( const Integration::Event& event )
245 {
246   mEventProcessor->QueueEvent( event );
247 }
248
249 void Core::ProcessEvents()
250 {
251   // Guard against calls to ProcessEvents() during ProcessEvents()
252   if( mProcessingEvent )
253   {
254     DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
255     mRenderController.RequestProcessEventsOnIdle( false );
256     return;
257   }
258
259   mProcessingEvent = true;
260   mRelayoutController->SetProcessingCoreEvents( true );
261
262   // Signal that any messages received will be flushed soon
263   mUpdateManager->EventProcessingStarted();
264
265   mEventProcessor->ProcessEvents();
266
267   mNotificationManager->ProcessMessages();
268
269   // Emit signal here to inform listeners that event processing has finished.
270   mStage->EmitEventProcessingFinishedSignal();
271
272   // Run the size negotiation after event processing finished signal
273   mRelayoutController->Relayout();
274
275   // Rebuild depth tree after event processing has finished
276   mStage->RebuildDepthTree();
277
278   // Flush any queued messages for the update-thread
279   const bool messagesToProcess = mUpdateManager->FlushQueue();
280
281   // Check if the touch or gestures require updates.
282   const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
283   // Check if the next update is forced.
284   const bool forceUpdate = mStage->IsNextUpdateForced();
285
286   if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
287   {
288     // tell the render controller to keep update thread running
289     mRenderController.RequestUpdate( forceUpdate );
290   }
291
292   mRelayoutController->SetProcessingCoreEvents( false );
293
294   // ProcessEvents() may now be called again
295   mProcessingEvent = false;
296 }
297
298 unsigned int Core::GetMaximumUpdateCount() const
299 {
300   return MAXIMUM_UPDATE_COUNT;
301 }
302
303 Integration::SystemOverlay& Core::GetSystemOverlay()
304 {
305   return mStage->GetSystemOverlay();
306 }
307
308 void Core::SetViewMode( ViewMode viewMode )
309 {
310   mStage->SetViewMode( viewMode );
311 }
312
313 ViewMode Core::GetViewMode() const
314 {
315   return mStage->GetViewMode();
316 }
317
318 void Core::SetStereoBase( float stereoBase )
319 {
320   mStage->SetStereoBase( stereoBase );
321 }
322
323 float Core::GetStereoBase() const
324 {
325   return mStage->GetStereoBase();
326 }
327
328 StagePtr Core::GetCurrentStage()
329 {
330   return mStage.Get();
331 }
332
333 PlatformAbstraction& Core::GetPlatform()
334 {
335   return mPlatform;
336 }
337
338 UpdateManager& Core::GetUpdateManager()
339 {
340   return *(mUpdateManager);
341 }
342
343 RenderManager& Core::GetRenderManager()
344 {
345   return *(mRenderManager);
346 }
347
348 NotificationManager& Core::GetNotificationManager()
349 {
350   return *(mNotificationManager);
351 }
352
353 ShaderFactory& Core::GetShaderFactory()
354 {
355   return *(mShaderFactory);
356 }
357
358 GestureEventProcessor& Core::GetGestureEventProcessor()
359 {
360   return *(mGestureEventProcessor);
361 }
362
363 RelayoutController& Core::GetRelayoutController()
364 {
365   return *(mRelayoutController.Get());
366 }
367
368 void Core::CreateThreadLocalStorage()
369 {
370   // a pointer to the ThreadLocalStorage object will be stored in TLS
371   // The ThreadLocalStorage object should be deleted by the Core destructor
372   new ThreadLocalStorage(this);
373 }
374
375 } // namespace Internal
376
377 } // namespace Dali