2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/common/core-impl.h>
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>
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>
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>
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>
51 using Dali::Internal::SceneGraph::UpdateManager;
52 using Dali::Internal::SceneGraph::RenderManager;
53 using Dali::Internal::SceneGraph::DiscardQueue;
54 using Dali::Internal::SceneGraph::RenderQueue;
58 // The Update for frame N+1 may be processed whilst frame N is being rendered.
59 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
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;
81 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
82 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
83 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
84 : mRenderController( renderController ),
87 mProcessingEvent(false)
89 // Create the thread local storage
90 CreateThreadLocalStorage();
92 // This does nothing until Core is built with --enable-performance-monitor
93 PERFORMANCE_MONITOR_INIT( platform );
95 mNotificationManager = new NotificationManager();
97 mAnimationPlaylist = AnimationPlaylist::New();
99 mPropertyNotificationManager = PropertyNotificationManager::New();
101 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
103 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction );
105 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
107 mDiscardQueue = new DiscardQueue( renderQueue );
109 mUpdateManager = new UpdateManager( *mNotificationManager,
111 *mPropertyNotificationManager,
116 *mRenderTaskProcessor );
118 mRenderManager->SetShaderSaver( *mUpdateManager );
120 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
122 // This must be called after stage is created but before stage initialization
123 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
125 mStage->Initialize();
127 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
128 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
130 mShaderFactory = new ShaderFactory();
131 mUpdateManager->SetShaderSaver( *mShaderFactory );
133 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
139 * The order of destructing these singletons is important!!!
142 // clear the thread local storage first
143 // allows core to be created / deleted many times in the same thread (how TET cases work).
144 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
145 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
152 // Stop relayout requests being raised on stage destruction
153 mRelayoutController.Reset();
155 // Clean-up stage - remove default camera and root layer
156 mStage->Uninitialize();
158 // remove (last?) reference to stage
163 Integration::ContextNotifierInterface* Core::GetContextNotifier()
168 void Core::RecoverFromContextLoss()
170 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
172 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
175 void Core::ContextCreated()
177 mRenderManager->ContextCreated();
180 void Core::ContextDestroyed()
182 mRenderManager->ContextDestroyed();
185 void Core::SurfaceResized( unsigned int width, unsigned int height )
187 mStage->SurfaceResized( width, height );
189 // The stage-size may be less than surface-size (reduced by top-margin)
190 Vector2 size = mStage->GetSize();
191 mRelayoutController->SetStageSize( size.width, size.height );
194 void Core::SetTopMargin( unsigned int margin )
196 mStage->SetTopMargin( margin );
198 // The stage-size may be less than surface-size (reduced by top-margin)
199 Vector2 size = mStage->GetSize();
200 mRelayoutController->SetStageSize( size.width, size.height );
203 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
205 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
208 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
210 // set the time delta so adaptor can easily print FPS with a release build with 0 as
211 // it is cached by frametime
212 status.secondsFromLastFrame = elapsedSeconds;
214 // Render returns true when there are updates on the stage or one or more animations are completed.
215 // Use the estimated time diff till we render as the elapsed time.
216 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
217 lastVSyncTimeMilliseconds,
218 nextVSyncTimeMilliseconds );
220 // Check the Notification Manager message queue to set needsNotification
221 status.needsNotification = mNotificationManager->MessagesToProcess();
223 // No need to keep update running if there are notifications to process.
224 // Any message to update will wake it up anyways
227 void Core::Render( RenderStatus& status )
229 mRenderManager->Render( status );
241 // trigger processing of events queued up while paused
245 void Core::SceneCreated()
247 mStage->EmitSceneCreatedSignal();
249 mRelayoutController->OnApplicationSceneCreated();
252 void Core::QueueEvent( const Integration::Event& event )
254 mEventProcessor->QueueEvent( event );
257 void Core::ProcessEvents()
259 // Guard against calls to ProcessEvents() during ProcessEvents()
260 if( mProcessingEvent )
262 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
263 mRenderController.RequestProcessEventsOnIdle();
267 mProcessingEvent = true;
268 mRelayoutController->SetProcessingCoreEvents( true );
270 // Signal that any messages received will be flushed soon
271 mUpdateManager->EventProcessingStarted();
273 mEventProcessor->ProcessEvents();
275 mNotificationManager->ProcessMessages();
277 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
280 // Emit signal here to inform listeners that event processing has finished.
281 mStage->EmitEventProcessingFinishedSignal();
283 // Run the size negotiation after event processing finished signal
284 mRelayoutController->Relayout();
286 // Rebuild depth tree after event processing has finished
287 mStage->RebuildDepthTree();
289 // Flush any queued messages for the update-thread
290 const bool messagesToProcess = mUpdateManager->FlushQueue();
292 // Check if the touch or gestures require updates.
293 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
295 if( messagesToProcess || gestureNeedsUpdate )
297 // tell the render controller to keep update thread running
298 mRenderController.RequestUpdate();
302 mRelayoutController->SetProcessingCoreEvents( false );
304 // ProcessEvents() may now be called again
305 mProcessingEvent = false;
308 unsigned int Core::GetMaximumUpdateCount() const
310 return MAXIMUM_UPDATE_COUNT;
313 Integration::SystemOverlay& Core::GetSystemOverlay()
315 return mStage->GetSystemOverlay();
318 void Core::SetViewMode( ViewMode viewMode )
320 mStage->SetViewMode( viewMode );
323 ViewMode Core::GetViewMode() const
325 return mStage->GetViewMode();
328 void Core::SetStereoBase( float stereoBase )
330 mStage->SetStereoBase( stereoBase );
333 float Core::GetStereoBase() const
335 return mStage->GetStereoBase();
338 StagePtr Core::GetCurrentStage()
343 PlatformAbstraction& Core::GetPlatform()
348 UpdateManager& Core::GetUpdateManager()
350 return *(mUpdateManager);
353 RenderManager& Core::GetRenderManager()
355 return *(mRenderManager);
358 NotificationManager& Core::GetNotificationManager()
360 return *(mNotificationManager);
363 ShaderFactory& Core::GetShaderFactory()
365 return *(mShaderFactory);
368 GestureEventProcessor& Core::GetGestureEventProcessor()
370 return *(mGestureEventProcessor);
373 RelayoutController& Core::GetRelayoutController()
375 return *(mRelayoutController.Get());
378 void Core::CreateThreadLocalStorage()
380 // a pointer to the ThreadLocalStorage object will be stored in TLS
381 // The ThreadLocalStorage object should be deleted by the Core destructor
382 new ThreadLocalStorage(this);
385 } // namespace Internal