2 * Copyright (c) 2017 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,
82 PlatformAbstraction& platform,
83 GlAbstraction& glAbstraction,
84 GlSyncAbstraction& glSyncAbstraction,
85 GestureManager& gestureManager,
86 ResourcePolicy::DataRetention dataRetentionPolicy,
87 bool renderToFboEnabled )
88 : mRenderController( renderController ),
90 mProcessingEvent(false)
92 // Create the thread local storage
93 CreateThreadLocalStorage();
95 // This does nothing until Core is built with --enable-performance-monitor
96 PERFORMANCE_MONITOR_INIT( platform );
98 mNotificationManager = new NotificationManager();
100 mAnimationPlaylist = AnimationPlaylist::New();
102 mPropertyNotificationManager = PropertyNotificationManager::New();
104 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
106 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction );
108 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
110 mDiscardQueue = new DiscardQueue( renderQueue );
112 mUpdateManager = new UpdateManager( *mNotificationManager,
114 *mPropertyNotificationManager,
119 *mRenderTaskProcessor );
121 mRenderManager->SetShaderSaver( *mUpdateManager );
123 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
125 // This must be called after stage is created but before stage initialization
126 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
128 mStage->Initialize( renderToFboEnabled );
130 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
131 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
133 mShaderFactory = new ShaderFactory();
134 mUpdateManager->SetShaderSaver( *mShaderFactory );
136 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
142 * The order of destructing these singletons is important!!!
145 // clear the thread local storage first
146 // allows core to be created / deleted many times in the same thread (how TET cases work).
147 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
148 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
155 // Stop relayout requests being raised on stage destruction
156 mRelayoutController.Reset();
158 // Clean-up stage - remove default camera and root layer
159 mStage->Uninitialize();
161 // remove (last?) reference to stage
166 Integration::ContextNotifierInterface* Core::GetContextNotifier()
171 void Core::RecoverFromContextLoss()
173 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
175 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
178 void Core::ContextCreated()
180 mRenderManager->ContextCreated();
183 void Core::ContextDestroyed()
185 mRenderManager->ContextDestroyed();
188 void Core::SurfaceResized( unsigned int width, unsigned int height )
190 mStage->SurfaceResized( width, height );
192 // The stage-size may be less than surface-size (reduced by top-margin)
193 Vector2 size = mStage->GetSize();
194 mRelayoutController->SetStageSize( size.width, size.height );
197 void Core::SetTopMargin( unsigned int margin )
199 mStage->SetTopMargin( margin );
201 // The stage-size may be less than surface-size (reduced by top-margin)
202 Vector2 size = mStage->GetSize();
203 mRelayoutController->SetStageSize( size.width, size.height );
206 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
208 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
211 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
213 // set the time delta so adaptor can easily print FPS with a release build with 0 as
214 // it is cached by frametime
215 status.secondsFromLastFrame = elapsedSeconds;
217 // Render returns true when there are updates on the stage or one or more animations are completed.
218 // Use the estimated time diff till we render as the elapsed time.
219 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
220 lastVSyncTimeMilliseconds,
221 nextVSyncTimeMilliseconds,
225 // Check the Notification Manager message queue to set needsNotification
226 status.needsNotification = mNotificationManager->MessagesToProcess();
228 // No need to keep update running if there are notifications to process.
229 // Any message to update will wake it up anyways
232 void Core::Render( RenderStatus& status )
234 mRenderManager->Render( status );
237 void Core::SceneCreated()
239 mStage->EmitSceneCreatedSignal();
241 mRelayoutController->OnApplicationSceneCreated();
244 void Core::QueueEvent( const Integration::Event& event )
246 mEventProcessor->QueueEvent( event );
249 void Core::ProcessEvents()
251 // Guard against calls to ProcessEvents() during ProcessEvents()
252 if( mProcessingEvent )
254 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
255 mRenderController.RequestProcessEventsOnIdle( false );
259 mProcessingEvent = true;
260 mRelayoutController->SetProcessingCoreEvents( true );
262 // Signal that any messages received will be flushed soon
263 mUpdateManager->EventProcessingStarted();
265 mEventProcessor->ProcessEvents();
267 mNotificationManager->ProcessMessages();
269 // Emit signal here to inform listeners that event processing has finished.
270 mStage->EmitEventProcessingFinishedSignal();
272 // Run the size negotiation after event processing finished signal
273 mRelayoutController->Relayout();
275 // Rebuild depth tree after event processing has finished
276 mStage->RebuildDepthTree();
278 // Flush any queued messages for the update-thread
279 const bool messagesToProcess = mUpdateManager->FlushQueue();
281 // Check if the touch or gestures require updates.
282 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
283 // Check if the next update is forced.
284 const bool forceUpdate = mStage->IsNextUpdateForced();
286 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
288 // tell the render controller to keep update thread running
289 mRenderController.RequestUpdate( forceUpdate );
292 mRelayoutController->SetProcessingCoreEvents( false );
294 // ProcessEvents() may now be called again
295 mProcessingEvent = false;
298 unsigned int Core::GetMaximumUpdateCount() const
300 return MAXIMUM_UPDATE_COUNT;
303 Integration::SystemOverlay& Core::GetSystemOverlay()
305 return mStage->GetSystemOverlay();
308 void Core::SetViewMode( ViewMode viewMode )
310 mStage->SetViewMode( viewMode );
313 ViewMode Core::GetViewMode() const
315 return mStage->GetViewMode();
318 void Core::SetStereoBase( float stereoBase )
320 mStage->SetStereoBase( stereoBase );
323 float Core::GetStereoBase() const
325 return mStage->GetStereoBase();
328 StagePtr Core::GetCurrentStage()
333 PlatformAbstraction& Core::GetPlatform()
338 UpdateManager& Core::GetUpdateManager()
340 return *(mUpdateManager);
343 RenderManager& Core::GetRenderManager()
345 return *(mRenderManager);
348 NotificationManager& Core::GetNotificationManager()
350 return *(mNotificationManager);
353 ShaderFactory& Core::GetShaderFactory()
355 return *(mShaderFactory);
358 GestureEventProcessor& Core::GetGestureEventProcessor()
360 return *(mGestureEventProcessor);
363 RelayoutController& Core::GetRelayoutController()
365 return *(mRelayoutController.Get());
368 void Core::CreateThreadLocalStorage()
370 // a pointer to the ThreadLocalStorage object will be stored in TLS
371 // The ThreadLocalStorage object should be deleted by the Core destructor
372 new ThreadLocalStorage(this);
375 } // namespace Internal