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