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 Integration::PartialUpdateAvailable partialUpdateAvailable )
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, partialUpdateAvailable );
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::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
198 // set the time delta so adaptor can easily print FPS with a release build with 0 as
199 // it is cached by frametime
200 status.secondsFromLastFrame = elapsedSeconds;
202 // Render returns true when there are updates on the stage or one or more animations are completed.
203 // Use the estimated time diff till we render as the elapsed time.
204 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
205 lastVSyncTimeMilliseconds,
206 nextVSyncTimeMilliseconds,
210 // Check the Notification Manager message queue to set needsNotification
211 status.needsNotification = mNotificationManager->MessagesToProcess();
213 // Check if the default surface is changed
214 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
216 // No need to keep update running if there are notifications to process.
217 // Any message to update will wake it up anyways
220 void Core::PreRender( RenderStatus& status, bool forceClear, bool uploadOnly )
222 mRenderManager->PreRender( status, forceClear, uploadOnly );
225 void Core::PreRender( Integration::Scene& scene, std::vector<Rect<int>>& damagedRects )
227 mRenderManager->PreRender( scene, damagedRects );
230 void Core::RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo )
232 mRenderManager->RenderScene( status, scene, renderToFbo );
235 void Core::RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect )
237 mRenderManager->RenderScene( status, scene, renderToFbo, clippingRect );
240 void Core::PostRender( bool uploadOnly )
242 mRenderManager->PostRender( uploadOnly );
245 void Core::SceneCreated()
247 mStage->EmitSceneCreatedSignal();
249 mRelayoutController->OnApplicationSceneCreated();
251 for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
253 Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
254 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
258 void Core::QueueEvent( const Integration::Event& event )
260 if (mScenes.size() != 0)
262 mScenes.front()->QueueEvent( event );
266 void Core::ProcessEvents()
268 // Guard against calls to ProcessEvents() during ProcessEvents()
269 if( mProcessingEvent )
271 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
272 mRenderController.RequestProcessEventsOnIdle( false );
276 mProcessingEvent = true;
277 mRelayoutController->SetProcessingCoreEvents( true );
279 // Signal that any messages received will be flushed soon
280 mUpdateManager->EventProcessingStarted();
282 // Scene could be added or removed while processing the events
283 // Copy the Scene container locally to avoid possibly invalid iterator
284 SceneContainer scenes = mScenes;
286 // process events in all scenes
287 for( auto scene : scenes )
289 scene->ProcessEvents();
292 mNotificationManager->ProcessMessages();
294 // Emit signal here to inform listeners that event processing has finished.
295 for( auto scene : scenes )
297 scene->EmitEventProcessingFinishedSignal();
300 // Run any registered processors
303 // Run the size negotiation after event processing finished signal
304 mRelayoutController->Relayout();
306 // Rebuild depth tree after event processing has finished
307 for( auto scene : scenes )
309 scene->RebuildDepthTree();
312 // Flush any queued messages for the update-thread
313 const bool messagesToProcess = mUpdateManager->FlushQueue();
315 // Check if the touch or gestures require updates.
316 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
317 // Check if the next update is forced.
318 const bool forceUpdate = IsNextUpdateForced();
320 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
322 // tell the render controller to keep update thread running
323 mRenderController.RequestUpdate( forceUpdate );
326 mRelayoutController->SetProcessingCoreEvents( false );
328 // ProcessEvents() may now be called again
329 mProcessingEvent = false;
332 uint32_t Core::GetMaximumUpdateCount() const
334 return MAXIMUM_UPDATE_COUNT;
337 void Core::RegisterProcessor( Integration::Processor& processor )
339 mProcessors.PushBack(&processor);
342 void Core::UnregisterProcessor( Integration::Processor& processor )
344 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
345 if( iter != mProcessors.End() )
347 mProcessors.Erase( iter );
351 void Core::RunProcessors()
353 // Copy processor pointers to prevent changes to vector affecting loop iterator.
354 Dali::Vector<Integration::Processor*> processors( mProcessors );
356 for( auto processor : processors )
360 processor->Process();
365 StagePtr Core::GetCurrentStage()
370 PlatformAbstraction& Core::GetPlatform()
375 UpdateManager& Core::GetUpdateManager()
377 return *(mUpdateManager);
380 RenderManager& Core::GetRenderManager()
382 return *(mRenderManager);
385 NotificationManager& Core::GetNotificationManager()
387 return *(mNotificationManager);
390 ShaderFactory& Core::GetShaderFactory()
392 return *(mShaderFactory);
395 GestureEventProcessor& Core::GetGestureEventProcessor()
397 return *(mGestureEventProcessor);
400 RelayoutController& Core::GetRelayoutController()
402 return *(mRelayoutController.Get());
405 ObjectRegistry& Core::GetObjectRegistry() const
407 return *(mObjectRegistry.Get());
410 EventThreadServices& Core::GetEventThreadServices()
415 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
417 return *(mPropertyNotificationManager);
420 AnimationPlaylist& Core::GetAnimationPlaylist() const
422 return *(mAnimationPlaylist);
425 void Core::AddScene( Scene* scene )
427 mScenes.push_back( scene );
430 void Core::RemoveScene( Scene* scene )
432 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
433 if( iter != mScenes.end() )
435 mScenes.erase( iter );
439 void Core::CreateThreadLocalStorage()
441 // a pointer to the ThreadLocalStorage object will be stored in TLS
442 // The ThreadLocalStorage object should be deleted by the Core destructor
443 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
447 void Core::RegisterObject( Dali::BaseObject* object )
449 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
450 mObjectRegistry->RegisterObject( object );
453 void Core::UnregisterObject( Dali::BaseObject* object )
455 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
456 mObjectRegistry->UnregisterObject( object );
459 Integration::RenderController& Core::GetRenderController()
461 return mRenderController;
464 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
466 return mUpdateManager->ReserveMessageSlot( size, updateScene );
469 BufferIndex Core::GetEventBufferIndex() const
471 return mUpdateManager->GetEventBufferIndex();
474 void Core::ForceNextUpdate()
476 mForceNextUpdate = true;
479 bool Core::IsNextUpdateForced()
481 bool nextUpdateForced = mForceNextUpdate;
482 mForceNextUpdate = false;
483 return nextUpdateForced;
486 } // namespace Internal