2 * Copyright (c) 2019 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/processor-interface.h>
29 #include <dali/integration-api/render-controller.h>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/animation/animation-playlist.h>
33 #include <dali/internal/event/common/notification-manager.h>
34 #include <dali/internal/event/common/property-notification-manager.h>
35 #include <dali/internal/event/common/stage-impl.h>
36 #include <dali/internal/event/common/thread-local-storage.h>
37 #include <dali/internal/event/common/type-registry-impl.h>
38 #include <dali/internal/event/effects/shader-factory.h>
39 #include <dali/internal/event/events/event-processor.h>
40 #include <dali/internal/event/events/gesture-event-processor.h>
41 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
42 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
44 #include <dali/internal/update/common/discard-queue.h>
45 #include <dali/internal/update/manager/update-manager.h>
46 #include <dali/internal/update/manager/render-task-processor.h>
48 #include <dali/internal/render/common/performance-monitor.h>
49 #include <dali/internal/render/common/render-manager.h>
50 #include <dali/internal/render/gl-resources/context.h>
52 using Dali::Internal::SceneGraph::UpdateManager;
53 using Dali::Internal::SceneGraph::RenderManager;
54 using Dali::Internal::SceneGraph::DiscardQueue;
55 using Dali::Internal::SceneGraph::RenderQueue;
59 // The Update for frame N+1 may be processed whilst frame N is being rendered.
60 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
62 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
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;
82 Core::Core( RenderController& renderController,
83 PlatformAbstraction& platform,
84 GlAbstraction& glAbstraction,
85 GlSyncAbstraction& glSyncAbstraction,
86 GestureManager& gestureManager,
87 ResourcePolicy::DataRetention dataRetentionPolicy,
88 Integration::RenderToFrameBuffer renderToFboEnabled,
89 Integration::DepthBufferAvailable depthBufferAvailable,
90 Integration::StencilBufferAvailable stencilBufferAvailable )
91 : mRenderController( renderController ),
93 mProcessingEvent(false)
95 // Create the thread local storage
96 CreateThreadLocalStorage();
98 // This does nothing until Core is built with --enable-performance-monitor
99 PERFORMANCE_MONITOR_INIT( platform );
101 mNotificationManager = new NotificationManager();
103 mAnimationPlaylist = AnimationPlaylist::New();
105 mPropertyNotificationManager = PropertyNotificationManager::New();
107 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
109 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, depthBufferAvailable, stencilBufferAvailable );
111 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
113 mDiscardQueue = new DiscardQueue( renderQueue );
115 mUpdateManager = new UpdateManager( *mNotificationManager,
117 *mPropertyNotificationManager,
122 *mRenderTaskProcessor );
124 mRenderManager->SetShaderSaver( *mUpdateManager );
126 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
128 // This must be called after stage is created but before stage initialization
129 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
131 mStage->Initialize( renderToFboEnabled == Integration::RenderToFrameBuffer::TRUE );
133 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
134 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
136 mShaderFactory = new ShaderFactory();
137 mUpdateManager->SetShaderSaver( *mShaderFactory );
139 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
145 * The order of destructing these singletons is important!!!
148 // clear the thread local storage first
149 // allows core to be created / deleted many times in the same thread (how TET cases work).
150 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
151 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
158 // Stop relayout requests being raised on stage destruction
159 mRelayoutController.Reset();
161 // Clean-up stage - remove default camera and root layer
162 mStage->Uninitialize();
164 // remove (last?) reference to stage
169 Integration::ContextNotifierInterface* Core::GetContextNotifier()
174 void Core::RecoverFromContextLoss()
176 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
178 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
181 void Core::ContextCreated()
183 mRenderManager->ContextCreated();
186 void Core::ContextDestroyed()
188 mRenderManager->ContextDestroyed();
191 void Core::SurfaceResized( uint32_t width, uint32_t height )
193 mStage->SurfaceResized( static_cast<float>( width ), static_cast<float>( height ) );
195 // The stage-size may be less than surface-size (reduced by top-margin)
196 Vector2 size = mStage->GetSize();
197 mRelayoutController->SetStageSize( static_cast<uint32_t>( size.width ), static_cast<uint32_t>( size.height ) ); // values get truncated
200 void Core::SetTopMargin( uint32_t margin )
202 mStage->SetTopMargin( margin );
204 // The stage-size may be less than surface-size (reduced by top-margin)
205 Vector2 size = mStage->GetSize();
206 mRelayoutController->SetStageSize( static_cast<uint32_t>( size.width ), static_cast<uint32_t>( size.height ) ); // values get truncated
209 void Core::SetDpi( uint32_t dpiHorizontal, uint32_t dpiVertical )
211 mStage->SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
214 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
216 // set the time delta so adaptor can easily print FPS with a release build with 0 as
217 // it is cached by frametime
218 status.secondsFromLastFrame = elapsedSeconds;
220 // Render returns true when there are updates on the stage or one or more animations are completed.
221 // Use the estimated time diff till we render as the elapsed time.
222 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
223 lastVSyncTimeMilliseconds,
224 nextVSyncTimeMilliseconds,
228 // Check the Notification Manager message queue to set needsNotification
229 status.needsNotification = mNotificationManager->MessagesToProcess();
231 // Check if the default surface is changed
232 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
234 // No need to keep update running if there are notifications to process.
235 // Any message to update will wake it up anyways
238 void Core::Render( RenderStatus& status, bool forceClear )
240 mRenderManager->Render( status, forceClear );
243 void Core::SceneCreated()
245 mStage->EmitSceneCreatedSignal();
247 mRelayoutController->OnApplicationSceneCreated();
250 void Core::QueueEvent( const Integration::Event& event )
252 mEventProcessor->QueueEvent( event );
255 void Core::ProcessEvents()
257 // Guard against calls to ProcessEvents() during ProcessEvents()
258 if( mProcessingEvent )
260 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
261 mRenderController.RequestProcessEventsOnIdle( false );
265 mProcessingEvent = true;
266 mRelayoutController->SetProcessingCoreEvents( true );
268 // Signal that any messages received will be flushed soon
269 mUpdateManager->EventProcessingStarted();
271 mEventProcessor->ProcessEvents();
273 mNotificationManager->ProcessMessages();
275 // Emit signal here to inform listeners that event processing has finished.
276 mStage->EmitEventProcessingFinishedSignal();
278 // Run any registered processors
281 // Run the size negotiation after event processing finished signal
282 mRelayoutController->Relayout();
285 // Rebuild depth tree after event processing has finished
286 mStage->RebuildDepthTree();
288 // Flush any queued messages for the update-thread
289 const bool messagesToProcess = mUpdateManager->FlushQueue();
291 // Check if the touch or gestures require updates.
292 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
293 // Check if the next update is forced.
294 const bool forceUpdate = mStage->IsNextUpdateForced();
296 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
298 // tell the render controller to keep update thread running
299 mRenderController.RequestUpdate( forceUpdate );
302 mRelayoutController->SetProcessingCoreEvents( false );
304 // ProcessEvents() may now be called again
305 mProcessingEvent = false;
308 uint32_t Core::GetMaximumUpdateCount() const
310 return MAXIMUM_UPDATE_COUNT;
313 Integration::SystemOverlay& Core::GetSystemOverlay()
315 return mStage->GetSystemOverlay();
318 void Core::RegisterProcessor( Integration::Processor& processor )
320 mProcessors.PushBack(&processor);
323 void Core::UnregisterProcessor( Integration::Processor& processor )
325 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
326 if( iter != mProcessors.End() )
328 mProcessors.Erase( iter );
332 void Core::RunProcessors()
334 // Copy processor pointers to prevent changes to vector affecting loop iterator.
335 Dali::Vector<Integration::Processor*> processors( mProcessors );
337 for( auto processor : processors )
341 processor->Process();
346 StagePtr Core::GetCurrentStage()
351 PlatformAbstraction& Core::GetPlatform()
356 UpdateManager& Core::GetUpdateManager()
358 return *(mUpdateManager);
361 RenderManager& Core::GetRenderManager()
363 return *(mRenderManager);
366 NotificationManager& Core::GetNotificationManager()
368 return *(mNotificationManager);
371 ShaderFactory& Core::GetShaderFactory()
373 return *(mShaderFactory);
376 GestureEventProcessor& Core::GetGestureEventProcessor()
378 return *(mGestureEventProcessor);
381 RelayoutController& Core::GetRelayoutController()
383 return *(mRelayoutController.Get());
386 void Core::CreateThreadLocalStorage()
388 // a pointer to the ThreadLocalStorage object will be stored in TLS
389 // The ThreadLocalStorage object should be deleted by the Core destructor
390 new ThreadLocalStorage(this);
393 } // namespace Internal