2 * Copyright (c) 2020 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/core.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/events/event.h>
25 #include <dali/integration-api/gl-sync-abstraction.h>
26 #include <dali/integration-api/gl-context-helper-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/event-thread-services.h>
38 #include <dali/internal/event/common/type-registry-impl.h>
39 #include <dali/internal/event/effects/shader-factory.h>
40 #include <dali/internal/event/events/event-processor.h>
41 #include <dali/internal/event/events/gesture-event-processor.h>
42 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
43 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
45 #include <dali/internal/update/common/discard-queue.h>
46 #include <dali/internal/update/manager/update-manager.h>
47 #include <dali/internal/update/manager/render-task-processor.h>
49 #include <dali/internal/render/common/performance-monitor.h>
50 #include <dali/internal/render/common/render-manager.h>
51 #include <dali/internal/render/gl-resources/context.h>
53 using Dali::Internal::SceneGraph::UpdateManager;
54 using Dali::Internal::SceneGraph::RenderManager;
55 using Dali::Internal::SceneGraph::DiscardQueue;
56 using Dali::Internal::SceneGraph::RenderQueue;
60 // The Update for frame N+1 may be processed whilst frame N is being rendered.
61 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
74 using Integration::RenderController;
75 using Integration::PlatformAbstraction;
76 using Integration::GlSyncAbstraction;
77 using Integration::GlAbstraction;
78 using Integration::GlContextHelperAbstraction;
79 using Integration::Event;
80 using Integration::UpdateStatus;
81 using Integration::RenderStatus;
83 Core::Core( RenderController& renderController,
84 PlatformAbstraction& platform,
85 GlAbstraction& glAbstraction,
86 GlSyncAbstraction& glSyncAbstraction,
87 GlContextHelperAbstraction& glContextHelperAbstraction,
88 Integration::RenderToFrameBuffer renderToFboEnabled,
89 Integration::DepthBufferAvailable depthBufferAvailable,
90 Integration::StencilBufferAvailable stencilBufferAvailable )
91 : mRenderController( renderController ),
93 mProcessingEvent(false),
94 mForceNextUpdate( false )
96 // Create the thread local storage
97 CreateThreadLocalStorage();
99 // This does nothing until Core is built with --enable-performance-monitor
100 PERFORMANCE_MONITOR_INIT( platform );
102 mNotificationManager = new NotificationManager();
104 mAnimationPlaylist = AnimationPlaylist::New();
106 mPropertyNotificationManager = PropertyNotificationManager::New();
108 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
110 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable );
112 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
114 mDiscardQueue = new DiscardQueue( renderQueue );
116 mUpdateManager = new UpdateManager( *mNotificationManager,
118 *mPropertyNotificationManager,
123 *mRenderTaskProcessor );
125 mRenderManager->SetShaderSaver( *mUpdateManager );
127 mObjectRegistry = ObjectRegistry::New();
129 mStage = IntrusivePtr<Stage>( Stage::New( *mUpdateManager ) );
131 // This must be called after stage is created but before stage initialization
132 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
134 mGestureEventProcessor = new GestureEventProcessor( *mUpdateManager, mRenderController );
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 mObjectRegistry.Reset();
160 // Stop relayout requests being raised on stage destruction
161 mRelayoutController.Reset();
163 // remove (last?) reference to stage
168 void Core::Initialize()
170 mStage->Initialize( *mScenes[0] );
173 Integration::ContextNotifierInterface* Core::GetContextNotifier()
178 void Core::RecoverFromContextLoss()
180 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
182 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
185 void Core::ContextCreated()
187 mRenderManager->ContextCreated();
190 void Core::ContextDestroyed()
192 mRenderManager->ContextDestroyed();
195 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
197 // set the time delta so adaptor can easily print FPS with a release build with 0 as
198 // it is cached by frametime
199 status.secondsFromLastFrame = elapsedSeconds;
201 // Render returns true when there are updates on the stage or one or more animations are completed.
202 // Use the estimated time diff till we render as the elapsed time.
203 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
204 lastVSyncTimeMilliseconds,
205 nextVSyncTimeMilliseconds,
209 // Check the Notification Manager message queue to set needsNotification
210 status.needsNotification = mNotificationManager->MessagesToProcess();
212 // Check if the default surface is changed
213 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
215 // No need to keep update running if there are notifications to process.
216 // Any message to update will wake it up anyways
219 void Core::PreRender( RenderStatus& status, bool forceClear, bool uploadOnly )
221 mRenderManager->PreRender( status, forceClear, uploadOnly );
224 void Core::RenderScene( Integration::Scene& scene, bool renderToFbo )
226 mRenderManager->RenderScene( scene, renderToFbo );
229 void Core::PostRender( bool uploadOnly )
231 mRenderManager->PostRender( uploadOnly );
234 void Core::SceneCreated()
236 mStage->EmitSceneCreatedSignal();
238 mRelayoutController->OnApplicationSceneCreated();
240 for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
242 Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
243 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
247 void Core::QueueEvent( const Integration::Event& event )
249 if (mScenes.size() != 0)
251 mScenes.front()->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 // Scene could be added or removed while processing the events
272 // Copy the Scene container locally to avoid possibly invalid iterator
273 SceneContainer scenes = mScenes;
275 // process events in all scenes
276 for( auto scene : scenes )
278 scene->ProcessEvents();
281 mNotificationManager->ProcessMessages();
283 // Emit signal here to inform listeners that event processing has finished.
284 for( auto scene : scenes )
286 scene->EmitEventProcessingFinishedSignal();
289 // Run any registered processors
292 // Run the size negotiation after event processing finished signal
293 mRelayoutController->Relayout();
295 // Rebuild depth tree after event processing has finished
296 for( auto scene : scenes )
298 scene->RebuildDepthTree();
301 // Flush any queued messages for the update-thread
302 const bool messagesToProcess = mUpdateManager->FlushQueue();
304 // Check if the touch or gestures require updates.
305 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
306 // Check if the next update is forced.
307 const bool forceUpdate = IsNextUpdateForced();
309 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
311 // tell the render controller to keep update thread running
312 mRenderController.RequestUpdate( forceUpdate );
315 mRelayoutController->SetProcessingCoreEvents( false );
317 // ProcessEvents() may now be called again
318 mProcessingEvent = false;
321 uint32_t Core::GetMaximumUpdateCount() const
323 return MAXIMUM_UPDATE_COUNT;
326 void Core::RegisterProcessor( Integration::Processor& processor )
328 mProcessors.PushBack(&processor);
331 void Core::UnregisterProcessor( Integration::Processor& processor )
333 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
334 if( iter != mProcessors.End() )
336 mProcessors.Erase( iter );
340 void Core::RunProcessors()
342 // Copy processor pointers to prevent changes to vector affecting loop iterator.
343 Dali::Vector<Integration::Processor*> processors( mProcessors );
345 for( auto processor : processors )
349 processor->Process();
354 StagePtr Core::GetCurrentStage()
359 PlatformAbstraction& Core::GetPlatform()
364 UpdateManager& Core::GetUpdateManager()
366 return *(mUpdateManager);
369 RenderManager& Core::GetRenderManager()
371 return *(mRenderManager);
374 NotificationManager& Core::GetNotificationManager()
376 return *(mNotificationManager);
379 ShaderFactory& Core::GetShaderFactory()
381 return *(mShaderFactory);
384 GestureEventProcessor& Core::GetGestureEventProcessor()
386 return *(mGestureEventProcessor);
389 RelayoutController& Core::GetRelayoutController()
391 return *(mRelayoutController.Get());
394 ObjectRegistry& Core::GetObjectRegistry() const
396 return *(mObjectRegistry.Get());
399 EventThreadServices& Core::GetEventThreadServices()
404 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
406 return *(mPropertyNotificationManager);
409 AnimationPlaylist& Core::GetAnimationPlaylist() const
411 return *(mAnimationPlaylist);
414 void Core::AddScene( Scene* scene )
416 mScenes.push_back( scene );
419 void Core::RemoveScene( Scene* scene )
421 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
422 if( iter != mScenes.end() )
424 mScenes.erase( iter );
428 void Core::CreateThreadLocalStorage()
430 // a pointer to the ThreadLocalStorage object will be stored in TLS
431 // The ThreadLocalStorage object should be deleted by the Core destructor
432 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
436 void Core::RegisterObject( Dali::BaseObject* object )
438 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
439 mObjectRegistry->RegisterObject( object );
442 void Core::UnregisterObject( Dali::BaseObject* object )
444 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
445 mObjectRegistry->UnregisterObject( object );
448 Integration::RenderController& Core::GetRenderController()
450 return mRenderController;
453 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
455 return mUpdateManager->ReserveMessageSlot( size, updateScene );
458 BufferIndex Core::GetEventBufferIndex() const
460 return mUpdateManager->GetEventBufferIndex();
463 void Core::ForceNextUpdate()
465 mForceNextUpdate = true;
468 bool Core::IsNextUpdateForced()
470 bool nextUpdateForced = mForceNextUpdate;
471 mForceNextUpdate = false;
472 return nextUpdateForced;
475 } // namespace Internal