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