[4.0] Supports screen rotation.
[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::SurfaceResized( unsigned int width, unsigned int height, int orientation )
200 {
201   mStage->SurfaceResized( width, height, orientation );
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::SetTopMargin( unsigned int margin )
209 {
210   mStage->SetTopMargin( margin );
211
212   // The stage-size may be less than surface-size (reduced by top-margin)
213   Vector2 size = mStage->GetSize();
214   mRelayoutController->SetStageSize( size.width, size.height );
215 }
216
217 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
218 {
219   mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
220 }
221
222 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
223 {
224   // set the time delta so adaptor can easily print FPS with a release build with 0 as
225   // it is cached by frametime
226   status.secondsFromLastFrame = elapsedSeconds;
227
228   // Render returns true when there are updates on the stage or one or more animations are completed.
229   // Use the estimated time diff till we render as the elapsed time.
230   status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
231                                                 lastVSyncTimeMilliseconds,
232                                                 nextVSyncTimeMilliseconds,
233                                                 renderToFboEnabled,
234                                                 isRenderingToFbo );
235
236   // Check the Notification Manager message queue to set needsNotification
237   status.needsNotification = mNotificationManager->MessagesToProcess();
238
239   // Check if the default surface is changed
240   status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
241
242   // No need to keep update running if there are notifications to process.
243   // Any message to update will wake it up anyways
244 }
245
246 void Core::Render( RenderStatus& status )
247 {
248   mRenderManager->Render( status );
249 }
250
251 void Core::SceneCreated()
252 {
253   mStage->EmitSceneCreatedSignal();
254
255   mRelayoutController->OnApplicationSceneCreated();
256 }
257
258 void Core::QueueEvent( const Integration::Event& event )
259 {
260   mEventProcessor->QueueEvent( event );
261 }
262
263 void Core::ProcessEvents()
264 {
265   // Guard against calls to ProcessEvents() during ProcessEvents()
266   if( mProcessingEvent )
267   {
268     DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
269     mRenderController.RequestProcessEventsOnIdle( false );
270     return;
271   }
272
273   mProcessingEvent = true;
274   mRelayoutController->SetProcessingCoreEvents( true );
275
276   // Signal that any messages received will be flushed soon
277   mUpdateManager->EventProcessingStarted();
278
279   mEventProcessor->ProcessEvents();
280
281   mNotificationManager->ProcessMessages();
282
283   // Emit signal here to inform listeners that event processing has finished.
284   mStage->EmitEventProcessingFinishedSignal();
285
286   // Run the size negotiation after event processing finished signal
287   mRelayoutController->Relayout();
288
289   // Rebuild depth tree after event processing has finished
290   mStage->RebuildDepthTree();
291
292   // Flush any queued messages for the update-thread
293   const bool messagesToProcess = mUpdateManager->FlushQueue();
294
295   // Check if the touch or gestures require updates.
296   const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
297   // Check if the next update is forced.
298   const bool forceUpdate = mStage->IsNextUpdateForced();
299
300   if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
301   {
302     // tell the render controller to keep update thread running
303     mRenderController.RequestUpdate( forceUpdate );
304   }
305
306   mRelayoutController->SetProcessingCoreEvents( false );
307
308   // ProcessEvents() may now be called again
309   mProcessingEvent = false;
310 }
311
312 unsigned int Core::GetMaximumUpdateCount() const
313 {
314   return MAXIMUM_UPDATE_COUNT;
315 }
316
317 Integration::SystemOverlay& Core::GetSystemOverlay()
318 {
319   return mStage->GetSystemOverlay();
320 }
321
322 void Core::SetViewMode( ViewMode viewMode )
323 {
324   mStage->SetViewMode( viewMode );
325 }
326
327 ViewMode Core::GetViewMode() const
328 {
329   return mStage->GetViewMode();
330 }
331
332 void Core::SetStereoBase( float stereoBase )
333 {
334   mStage->SetStereoBase( stereoBase );
335 }
336
337 float Core::GetStereoBase() const
338 {
339   return mStage->GetStereoBase();
340 }
341
342 StagePtr Core::GetCurrentStage()
343 {
344   return mStage.Get();
345 }
346
347 PlatformAbstraction& Core::GetPlatform()
348 {
349   return mPlatform;
350 }
351
352 UpdateManager& Core::GetUpdateManager()
353 {
354   return *(mUpdateManager);
355 }
356
357 RenderManager& Core::GetRenderManager()
358 {
359   return *(mRenderManager);
360 }
361
362 NotificationManager& Core::GetNotificationManager()
363 {
364   return *(mNotificationManager);
365 }
366
367 ShaderFactory& Core::GetShaderFactory()
368 {
369   return *(mShaderFactory);
370 }
371
372 GestureEventProcessor& Core::GetGestureEventProcessor()
373 {
374   return *(mGestureEventProcessor);
375 }
376
377 RelayoutController& Core::GetRelayoutController()
378 {
379   return *(mRelayoutController.Get());
380 }
381
382 void Core::CreateThreadLocalStorage()
383 {
384   // a pointer to the ThreadLocalStorage object will be stored in TLS
385   // The ThreadLocalStorage object should be deleted by the Core destructor
386   new ThreadLocalStorage(this);
387 }
388
389 } // namespace Internal
390
391 } // namespace Dali