2 * Copyright (c) 2018 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 Integration::RenderToFrameBuffer renderToFboEnabled,
88 Integration::DepthBufferAvailable depthBufferAvailable,
89 Integration::StencilBufferAvailable stencilBufferAvailable )
90 : mRenderController( renderController ),
92 mProcessingEvent(false)
94 // Create the thread local storage
95 CreateThreadLocalStorage();
97 // This does nothing until Core is built with --enable-performance-monitor
98 PERFORMANCE_MONITOR_INIT( platform );
100 mNotificationManager = new NotificationManager();
102 mAnimationPlaylist = AnimationPlaylist::New();
104 mPropertyNotificationManager = PropertyNotificationManager::New();
106 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
108 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, depthBufferAvailable, stencilBufferAvailable );
110 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
112 mDiscardQueue = new DiscardQueue( renderQueue );
114 mUpdateManager = new UpdateManager( *mNotificationManager,
116 *mPropertyNotificationManager,
121 *mRenderTaskProcessor );
123 mRenderManager->SetShaderSaver( *mUpdateManager );
125 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
127 // This must be called after stage is created but before stage initialization
128 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
130 mStage->Initialize( renderToFboEnabled == Integration::RenderToFrameBuffer::TRUE );
132 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
133 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
135 mShaderFactory = new ShaderFactory();
136 mUpdateManager->SetShaderSaver( *mShaderFactory );
138 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
144 * The order of destructing these singletons is important!!!
147 // clear the thread local storage first
148 // allows core to be created / deleted many times in the same thread (how TET cases work).
149 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
150 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
157 // Stop relayout requests being raised on stage destruction
158 mRelayoutController.Reset();
160 // Clean-up stage - remove default camera and root layer
161 mStage->Uninitialize();
163 // remove (last?) reference to stage
168 Integration::ContextNotifierInterface* Core::GetContextNotifier()
173 void Core::RecoverFromContextLoss()
175 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
177 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
180 void Core::ContextCreated()
182 mRenderManager->ContextCreated();
185 void Core::ContextDestroyed()
187 mRenderManager->ContextDestroyed();
190 void Core::SurfaceResized( unsigned int width, unsigned int height )
192 mStage->SurfaceResized( width, height );
194 // The stage-size may be less than surface-size (reduced by top-margin)
195 Vector2 size = mStage->GetSize();
196 mRelayoutController->SetStageSize( size.width, size.height );
199 void Core::SetTopMargin( unsigned int margin )
201 mStage->SetTopMargin( margin );
203 // The stage-size may be less than surface-size (reduced by top-margin)
204 Vector2 size = mStage->GetSize();
205 mRelayoutController->SetStageSize( size.width, size.height );
208 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
210 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
213 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
215 // set the time delta so adaptor can easily print FPS with a release build with 0 as
216 // it is cached by frametime
217 status.secondsFromLastFrame = elapsedSeconds;
219 // Render returns true when there are updates on the stage or one or more animations are completed.
220 // Use the estimated time diff till we render as the elapsed time.
221 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
222 lastVSyncTimeMilliseconds,
223 nextVSyncTimeMilliseconds,
227 // Check the Notification Manager message queue to set needsNotification
228 status.needsNotification = mNotificationManager->MessagesToProcess();
230 // No need to keep update running if there are notifications to process.
231 // Any message to update will wake it up anyways
234 void Core::Render( RenderStatus& status, bool forceClear )
236 mRenderManager->Render( status, forceClear );
239 void Core::SceneCreated()
241 mStage->EmitSceneCreatedSignal();
243 mRelayoutController->OnApplicationSceneCreated();
246 void Core::QueueEvent( const Integration::Event& event )
248 mEventProcessor->QueueEvent( event );
251 void Core::ProcessEvents()
253 // Guard against calls to ProcessEvents() during ProcessEvents()
254 if( mProcessingEvent )
256 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
257 mRenderController.RequestProcessEventsOnIdle( false );
261 mProcessingEvent = true;
262 mRelayoutController->SetProcessingCoreEvents( true );
264 // Signal that any messages received will be flushed soon
265 mUpdateManager->EventProcessingStarted();
267 mEventProcessor->ProcessEvents();
269 mNotificationManager->ProcessMessages();
271 // Emit signal here to inform listeners that event processing has finished.
272 mStage->EmitEventProcessingFinishedSignal();
274 // Run the size negotiation after event processing finished signal
275 mRelayoutController->Relayout();
277 // Run any registered processors
280 // Rebuild depth tree after event processing has finished
281 mStage->RebuildDepthTree();
283 // Flush any queued messages for the update-thread
284 const bool messagesToProcess = mUpdateManager->FlushQueue();
286 // Check if the touch or gestures require updates.
287 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
288 // Check if the next update is forced.
289 const bool forceUpdate = mStage->IsNextUpdateForced();
291 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
293 // tell the render controller to keep update thread running
294 mRenderController.RequestUpdate( forceUpdate );
297 mRelayoutController->SetProcessingCoreEvents( false );
299 // ProcessEvents() may now be called again
300 mProcessingEvent = false;
303 unsigned int Core::GetMaximumUpdateCount() const
305 return MAXIMUM_UPDATE_COUNT;
308 Integration::SystemOverlay& Core::GetSystemOverlay()
310 return mStage->GetSystemOverlay();
313 void Core::SetViewMode( ViewMode viewMode )
315 mStage->SetViewMode( viewMode );
318 ViewMode Core::GetViewMode() const
320 return mStage->GetViewMode();
323 void Core::SetStereoBase( float stereoBase )
325 mStage->SetStereoBase( stereoBase );
328 void Core::RegisterProcessor( Integration::Processor& processor )
330 mProcessors.PushBack(&processor);
333 void Core::UnregisterProcessor( Integration::Processor& processor )
335 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
336 if( iter != mProcessors.End() )
338 mProcessors.Erase( iter );
342 void Core::RunProcessors()
344 // Copy processor pointers to prevent changes to vector affecting loop iterator.
345 Dali::Vector<Integration::Processor*> processors( mProcessors );
347 for( auto processor : processors )
351 processor->Process();
356 float Core::GetStereoBase() const
358 return mStage->GetStereoBase();
361 StagePtr Core::GetCurrentStage()
366 PlatformAbstraction& Core::GetPlatform()
371 UpdateManager& Core::GetUpdateManager()
373 return *(mUpdateManager);
376 RenderManager& Core::GetRenderManager()
378 return *(mRenderManager);
381 NotificationManager& Core::GetNotificationManager()
383 return *(mNotificationManager);
386 ShaderFactory& Core::GetShaderFactory()
388 return *(mShaderFactory);
391 GestureEventProcessor& Core::GetGestureEventProcessor()
393 return *(mGestureEventProcessor);
396 RelayoutController& Core::GetRelayoutController()
398 return *(mRelayoutController.Get());
401 void Core::CreateThreadLocalStorage()
403 // a pointer to the ThreadLocalStorage object will be stored in TLS
404 // The ThreadLocalStorage object should be deleted by the Core destructor
405 new ThreadLocalStorage(this);
408 } // namespace Internal