Dali 3D User Interface Engine
core-impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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
20 
21 // INTERNAL INCLUDES
48 
51 
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 
71 namespace Internal
72 {
73 
74 using Integration::RenderController;
75 using Integration::PlatformAbstraction;
76 using Integration::GlSyncAbstraction;
77 using Integration::GestureManager;
78 using Integration::GlAbstraction;
79 using Integration::Event;
80 using Integration::UpdateStatus;
81 using Integration::RenderStatus;
82 
83 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
84  GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
85  GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
86 : mRenderController( renderController ),
87  mPlatform(platform),
88  mGestureEventProcessor(NULL),
89  mEventProcessor(NULL),
90  mUpdateManager(NULL),
91  mRenderManager(NULL),
92  mDiscardQueue(NULL),
93  mResourcePostProcessQueue(),
94  mNotificationManager(NULL),
95  mImageFactory(NULL),
96  mShaderFactory(NULL),
97  mIsActive(true),
98  mProcessingEvent(false)
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  std::vector< ResourcePostProcessRequest> init;
113  mResourcePostProcessQueue = new ResourcePostProcessList(init);
114 
115  mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, *mResourcePostProcessQueue );
116 
117  RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
118  TextureCache& textureCache = mRenderManager->GetTextureCache();
119 
121  if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
122  {
123  discardPolicy = ResourcePolicy::OWNED_RETAIN;
124  }
125  textureCache.SetDiscardBitmapsPolicy(discardPolicy);
126 
127  mDiscardQueue = new DiscardQueue( renderQueue );
128 
129  mResourceManager = new ResourceManager( mPlatform,
130  *mNotificationManager,
131  textureCache,
132  *mResourcePostProcessQueue,
133  *mRenderManager,
134  *mDiscardQueue,
135  renderQueue );
136 
137  mTouchResampler = TouchResampler::New();
138 
139  mUpdateManager = new UpdateManager( *mNotificationManager,
140  *mAnimationPlaylist,
141  *mPropertyNotificationManager,
142  *mResourceManager,
143  *mDiscardQueue,
144  renderController,
145  *mRenderManager,
146  renderQueue,
147  textureCache,
148  *mTouchResampler );
149 
150  mRenderManager->SetShaderSaver( *mUpdateManager );
151 
152  mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
153 
154  // This must be called after stage is created but before stage initialization
155  mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
156 
157  mStage->Initialize();
158 
159  mResourceClient = new ResourceClient( *mResourceManager, *mStage );
160 
161  mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
162  mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
163 
164  mImageFactory = new ImageFactory( *mResourceClient );
165  mShaderFactory = new ShaderFactory();
166  mUpdateManager->SetShaderSaver( *mShaderFactory );
167  mShaderFactory->LoadDefaultShaders();
168 
169  GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
170 }
171 
172 Core::~Core()
173 {
178  mPlatform.JoinLoaderThreads();
179 
180  /*
181  * The order of destructing these singletons is important!!!
182  */
183 
184  // clear the thread local storage first
185  // allows core to be created / deleted many times in the same thread (how TET cases work).
186  // Do this before mStage.Reset() so Stage::IsInstalled() returns false
187  ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
188  if( tls )
189  {
190  tls->Remove();
191  }
192 
193  // Stop relayout requests being raised on stage destruction
194  mRelayoutController.Reset();
195 
196  // Clean-up stage - remove default camera and root layer
197  mStage->Uninitialize();
198 
199  // remove (last?) reference to stage
200  mStage.Reset();
201 
202  delete mEventProcessor;
203  delete mGestureEventProcessor;
204  delete mNotificationManager;
205  delete mImageFactory;
206  delete mShaderFactory;
207  delete mResourceClient;
208  delete mResourceManager;
209  delete mUpdateManager;
210  delete mTouchResampler;
211  delete mRenderManager;
212  delete mDiscardQueue;
213  delete mResourcePostProcessQueue;
214 }
215 
216 Integration::ContextNotifierInterface* Core::GetContextNotifier()
217 {
218  return mStage.Get();
219 }
220 
221 void Core::RecoverFromContextLoss()
222 {
223  DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
224 
225  mImageFactory->RecoverFromContextLoss(); // Reload images from files
226  mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
227 }
228 
229 void Core::ContextCreated()
230 {
231  mRenderManager->ContextCreated();
232 }
233 
234 void Core::ContextDestroyed()
235 {
236  mRenderManager->ContextDestroyed();
237 }
238 
239 void Core::SurfaceResized( unsigned int width, unsigned int height )
240 {
241  mStage->SetSize( width, height );
242  mRelayoutController->SetStageSize( width, height );
243 }
244 
245 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
246 {
247  mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
248 }
249 
250 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
251 {
252  // set the time delta so adaptor can easily print FPS with a release build with 0 as
253  // it is cached by frametime
254  status.secondsFromLastFrame = elapsedSeconds;
255 
256  // Render returns true when there are updates on the stage or one or more animations are completed.
257  // Use the estimated time diff till we render as the elapsed time.
258  status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
259  lastVSyncTimeMilliseconds,
260  nextVSyncTimeMilliseconds );
261 
262  // Check the Notification Manager message queue to set needsNotification
263  status.needsNotification = mNotificationManager->MessagesToProcess();
264 
265  // No need to keep update running if there are notifications to process.
266  // Any message to update will wake it up anyways
267 
268  if ( mResourceManager->ResourcesToProcess() )
269  {
270  // If we are still processing resources, then we have to continue the update
271  status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
272  }
273 }
274 
275 void Core::Render( RenderStatus& status )
276 {
277  bool updateRequired = mRenderManager->Render( status );
278 
279  status.SetNeedsUpdate( updateRequired );
280 }
281 
282 void Core::Suspend()
283 {
284  mPlatform.Suspend();
285 
286  mIsActive = false;
287 }
288 
289 void Core::Resume()
290 {
291  mPlatform.Resume();
292 
293  mIsActive = true;
294 
295  // trigger processing of events queued up while paused
296  ProcessEvents();
297 }
298 
299 void Core::SceneCreated()
300 {
301  mStage->EmitSceneCreatedSignal();
302 
303  mRelayoutController->OnApplicationSceneCreated();
304 }
305 
306 void Core::QueueEvent( const Integration::Event& event )
307 {
308  mEventProcessor->QueueEvent( event );
309 }
310 
311 void Core::ProcessEvents()
312 {
313  // Guard against calls to ProcessEvents() during ProcessEvents()
314  if( mProcessingEvent )
315  {
316  DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
317  mRenderController.RequestProcessEventsOnIdle();
318  return;
319  }
320 
321  mProcessingEvent = true;
322  mRelayoutController->SetProcessingCoreEvents( true );
323 
324  // Signal that any messages received will be flushed soon
325  mUpdateManager->EventProcessingStarted();
326 
327  mEventProcessor->ProcessEvents();
328 
329  mNotificationManager->ProcessMessages();
330 
331  // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
332  if( mIsActive )
333  {
334  // Emit signal here to start size negotiation and control relayout.
335  mStage->EmitEventProcessingFinishedSignal();
336 
337  // Run the size negotiation after event processing finished signal
338  mRelayoutController->Relayout();
339 
340  // Flush discard queue for image factory
341  mImageFactory->FlushReleaseQueue();
342 
343  // Flush any queued messages for the update-thread
344  const bool messagesToProcess = mUpdateManager->FlushQueue();
345 
346  // Check if the touch or gestures require updates.
347  const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
348  const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
349 
350  if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
351  {
352  // tell the render controller to keep update thread running
353  mRenderController.RequestUpdate();
354  }
355  }
356 
357  mRelayoutController->SetProcessingCoreEvents( false );
358 
359  // ProcessEvents() may now be called again
360  mProcessingEvent = false;
361 }
362 
363 void Core::UpdateTouchData(const Integration::TouchData& touch)
364 {
365  mTouchResampler->SendTouchData( touch );
366 }
367 
368 unsigned int Core::GetMaximumUpdateCount() const
369 {
370  return MAXIMUM_UPDATE_COUNT;
371 }
372 
373 Integration::SystemOverlay& Core::GetSystemOverlay()
374 {
375  return mStage->GetSystemOverlay();
376 }
377 
378 void Core::SetViewMode( ViewMode viewMode )
379 {
380  mStage->SetViewMode( viewMode );
381 }
382 
383 ViewMode Core::GetViewMode() const
384 {
385  return mStage->GetViewMode();
386 }
387 
388 void Core::SetStereoBase( float stereoBase )
389 {
390  mStage->SetStereoBase( stereoBase );
391 }
392 
393 float Core::GetStereoBase() const
394 {
395  return mStage->GetStereoBase();
396 }
397 
398 StagePtr Core::GetCurrentStage()
399 {
400  return mStage.Get();
401 }
402 
403 PlatformAbstraction& Core::GetPlatform()
404 {
405  return mPlatform;
406 }
407 
408 UpdateManager& Core::GetUpdateManager()
409 {
410  return *(mUpdateManager);
411 }
412 
413 RenderManager& Core::GetRenderManager()
414 {
415  return *(mRenderManager);
416 }
417 
418 NotificationManager& Core::GetNotificationManager()
419 {
420  return *(mNotificationManager);
421 }
422 
423 ResourceManager& Core::GetResourceManager()
424 {
425  return *(mResourceManager);
426 }
427 
428 ResourceClient& Core::GetResourceClient()
429 {
430  return *(mResourceClient);
431 }
432 
433 ImageFactory& Core::GetImageFactory()
434 {
435  return *(mImageFactory);
436 }
437 
438 ShaderFactory& Core::GetShaderFactory()
439 {
440  return *(mShaderFactory);
441 }
442 
443 GestureEventProcessor& Core::GetGestureEventProcessor()
444 {
445  return *(mGestureEventProcessor);
446 }
447 
448 RelayoutController& Core::GetRelayoutController()
449 {
450  return *(mRelayoutController.Get());
451 }
452 
453 void Core::CreateThreadLocalStorage()
454 {
455  // a pointer to the ThreadLocalStorage object will be stored in TLS
456  // and automatically deleted when the thread is killed
457  new ThreadLocalStorage(this);
458 }
459 
460 } // namespace Internal
461 
462 } // namespace Dali
Dali Docs Home
Read more about Dali