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