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>
30 #include <dali/integration-api/render-surface.h>
32 #include <dali/internal/event/actors/actor-impl.h>
33 #include <dali/internal/event/animation/animation-playlist.h>
34 #include <dali/internal/event/common/notification-manager.h>
35 #include <dali/internal/event/common/property-notification-manager.h>
36 #include <dali/internal/event/common/stage-impl.h>
37 #include <dali/internal/event/common/thread-local-storage.h>
38 #include <dali/internal/event/common/event-thread-services.h>
39 #include <dali/internal/event/common/type-registry-impl.h>
40 #include <dali/internal/event/effects/shader-factory.h>
41 #include <dali/internal/event/events/event-processor.h>
42 #include <dali/internal/event/events/gesture-event-processor.h>
43 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
44 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
46 #include <dali/internal/update/common/discard-queue.h>
47 #include <dali/internal/update/manager/update-manager.h>
48 #include <dali/internal/update/manager/render-task-processor.h>
50 #include <dali/internal/render/common/performance-monitor.h>
51 #include <dali/internal/render/common/render-manager.h>
52 #include <dali/internal/render/gl-resources/context.h>
54 using Dali::Internal::SceneGraph::UpdateManager;
55 using Dali::Internal::SceneGraph::RenderManager;
56 using Dali::Internal::SceneGraph::DiscardQueue;
57 using Dali::Internal::SceneGraph::RenderQueue;
61 // The Update for frame N+1 may be processed whilst frame N is being rendered.
62 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
75 using Integration::RenderController;
76 using Integration::PlatformAbstraction;
77 using Integration::GlSyncAbstraction;
78 using Integration::GlAbstraction;
79 using Integration::GlContextHelperAbstraction;
80 using Integration::Event;
81 using Integration::UpdateStatus;
82 using Integration::RenderStatus;
84 Core::Core( RenderController& renderController,
85 PlatformAbstraction& platform,
86 GlAbstraction& glAbstraction,
87 GlSyncAbstraction& glSyncAbstraction,
88 GlContextHelperAbstraction& glContextHelperAbstraction,
89 Integration::RenderToFrameBuffer renderToFboEnabled,
90 Integration::DepthBufferAvailable depthBufferAvailable,
91 Integration::StencilBufferAvailable stencilBufferAvailable )
92 : mRenderController( renderController ),
94 mProcessingEvent(false),
95 mForceNextUpdate( false )
97 // Create the thread local storage
98 CreateThreadLocalStorage();
100 // This does nothing until Core is built with --enable-performance-monitor
101 PERFORMANCE_MONITOR_INIT( platform );
103 mNotificationManager = new NotificationManager();
105 mAnimationPlaylist = AnimationPlaylist::New();
107 mPropertyNotificationManager = PropertyNotificationManager::New();
109 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
111 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable );
113 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
115 mDiscardQueue = new DiscardQueue( renderQueue );
117 mUpdateManager = new UpdateManager( *mNotificationManager,
119 *mPropertyNotificationManager,
124 *mRenderTaskProcessor );
126 mRenderManager->SetShaderSaver( *mUpdateManager );
128 mObjectRegistry = ObjectRegistry::New();
130 mStage = IntrusivePtr<Stage>( Stage::New( *mUpdateManager ) );
132 // This must be called after stage is created but before stage initialization
133 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
135 mGestureEventProcessor = new GestureEventProcessor( *mUpdateManager, mRenderController );
137 mShaderFactory = new ShaderFactory();
138 mUpdateManager->SetShaderSaver( *mShaderFactory );
140 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
146 * The order of destructing these singletons is important!!!
149 // clear the thread local storage first
150 // allows core to be created / deleted many times in the same thread (how TET cases work).
151 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
152 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
159 mObjectRegistry.Reset();
161 // Stop relayout requests being raised on stage destruction
162 mRelayoutController.Reset();
164 // remove (last?) reference to stage
169 void Core::Initialize()
171 mStage->Initialize( *mScenes[0] );
174 Integration::ContextNotifierInterface* Core::GetContextNotifier()
179 void Core::RecoverFromContextLoss()
181 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
183 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
186 void Core::ContextCreated()
188 mRenderManager->ContextCreated();
191 void Core::ContextDestroyed()
193 mRenderManager->ContextDestroyed();
196 void Core::SurfaceDeleted( Integration::RenderSurface* surface )
198 for( auto scene : mScenes )
200 if( scene->GetSurface() == surface )
202 scene->SurfaceDeleted();
208 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
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,
222 // Check the Notification Manager message queue to set needsNotification
223 status.needsNotification = mNotificationManager->MessagesToProcess();
225 // Check if the default surface is changed
226 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
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, bool forceClear, bool uploadOnly )
234 mRenderManager->Render( status, forceClear, uploadOnly );
237 void Core::SceneCreated()
239 mStage->EmitSceneCreatedSignal();
241 mRelayoutController->OnApplicationSceneCreated();
243 for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
245 Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
246 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
250 void Core::QueueEvent( const Integration::Event& event )
252 if (mScenes.size() != 0)
254 mScenes.front()->QueueEvent( event );
258 void Core::ProcessEvents()
260 // Guard against calls to ProcessEvents() during ProcessEvents()
261 if( mProcessingEvent )
263 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
264 mRenderController.RequestProcessEventsOnIdle( false );
268 mProcessingEvent = true;
269 mRelayoutController->SetProcessingCoreEvents( true );
271 // Signal that any messages received will be flushed soon
272 mUpdateManager->EventProcessingStarted();
274 // Scene could be added or removed while processing the events
275 // Copy the Scene container locally to avoid possibly invalid iterator
276 SceneContainer scenes = mScenes;
278 // process events in all scenes
279 for( auto scene : scenes )
281 scene->ProcessEvents();
284 mNotificationManager->ProcessMessages();
286 // Emit signal here to inform listeners that event processing has finished.
287 for( auto scene : scenes )
289 scene->EmitEventProcessingFinishedSignal();
292 // Run any registered processors
295 // Run the size negotiation after event processing finished signal
296 mRelayoutController->Relayout();
298 // Rebuild depth tree after event processing has finished
299 for( auto scene : scenes )
301 scene->RebuildDepthTree();
304 // Flush any queued messages for the update-thread
305 const bool messagesToProcess = mUpdateManager->FlushQueue();
307 // Check if the touch or gestures require updates.
308 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
309 // Check if the next update is forced.
310 const bool forceUpdate = IsNextUpdateForced();
312 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
314 // tell the render controller to keep update thread running
315 mRenderController.RequestUpdate( forceUpdate );
318 mRelayoutController->SetProcessingCoreEvents( false );
320 // ProcessEvents() may now be called again
321 mProcessingEvent = false;
324 uint32_t Core::GetMaximumUpdateCount() const
326 return MAXIMUM_UPDATE_COUNT;
329 void Core::RegisterProcessor( Integration::Processor& processor )
331 mProcessors.PushBack(&processor);
334 void Core::UnregisterProcessor( Integration::Processor& processor )
336 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
337 if( iter != mProcessors.End() )
339 mProcessors.Erase( iter );
343 void Core::RunProcessors()
345 // Copy processor pointers to prevent changes to vector affecting loop iterator.
346 Dali::Vector<Integration::Processor*> processors( mProcessors );
348 for( auto processor : processors )
352 processor->Process();
357 StagePtr Core::GetCurrentStage()
362 PlatformAbstraction& Core::GetPlatform()
367 UpdateManager& Core::GetUpdateManager()
369 return *(mUpdateManager);
372 RenderManager& Core::GetRenderManager()
374 return *(mRenderManager);
377 NotificationManager& Core::GetNotificationManager()
379 return *(mNotificationManager);
382 ShaderFactory& Core::GetShaderFactory()
384 return *(mShaderFactory);
387 GestureEventProcessor& Core::GetGestureEventProcessor()
389 return *(mGestureEventProcessor);
392 RelayoutController& Core::GetRelayoutController()
394 return *(mRelayoutController.Get());
397 ObjectRegistry& Core::GetObjectRegistry() const
399 return *(mObjectRegistry.Get());
402 EventThreadServices& Core::GetEventThreadServices()
407 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
409 return *(mPropertyNotificationManager);
412 AnimationPlaylist& Core::GetAnimationPlaylist() const
414 return *(mAnimationPlaylist);
417 void Core::AddScene( Scene* scene )
419 mScenes.push_back( scene );
422 void Core::RemoveScene( Scene* scene )
424 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
425 if( iter != mScenes.end() )
427 mScenes.erase( iter );
431 void Core::CreateThreadLocalStorage()
433 // a pointer to the ThreadLocalStorage object will be stored in TLS
434 // The ThreadLocalStorage object should be deleted by the Core destructor
435 new ThreadLocalStorage(this);
438 void Core::RegisterObject( Dali::BaseObject* object )
440 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
441 mObjectRegistry->RegisterObject( object );
444 void Core::UnregisterObject( Dali::BaseObject* object )
446 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
447 mObjectRegistry->UnregisterObject( object );
450 Integration::RenderController& Core::GetRenderController()
452 return mRenderController;
455 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
457 return mUpdateManager->ReserveMessageSlot( size, updateScene );
460 BufferIndex Core::GetEventBufferIndex() const
462 return mUpdateManager->GetEventBufferIndex();
465 void Core::ForceNextUpdate()
467 mForceNextUpdate = true;
470 bool Core::IsNextUpdateForced()
472 bool nextUpdateForced = mForceNextUpdate;
473 mForceNextUpdate = false;
474 return nextUpdateForced;
477 } // namespace Internal