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