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 mGlAbstraction(glAbstraction),
95 mProcessingEvent(false),
96 mForceNextUpdate( false )
98 // Create the thread local storage
99 CreateThreadLocalStorage();
101 // This does nothing until Core is built with --enable-performance-monitor
102 PERFORMANCE_MONITOR_INIT( platform );
104 mNotificationManager = new NotificationManager();
106 mAnimationPlaylist = AnimationPlaylist::New();
108 mPropertyNotificationManager = PropertyNotificationManager::New();
110 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
112 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable );
114 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
116 mDiscardQueue = new DiscardQueue( renderQueue );
118 mUpdateManager = new UpdateManager( *mNotificationManager,
120 *mPropertyNotificationManager,
125 *mRenderTaskProcessor );
127 mRenderManager->SetShaderSaver( *mUpdateManager );
129 mObjectRegistry = ObjectRegistry::New();
131 mStage = IntrusivePtr<Stage>( Stage::New( *mUpdateManager ) );
133 // This must be called after stage is created but before stage initialization
134 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
136 mGestureEventProcessor = new GestureEventProcessor( *mUpdateManager, mRenderController );
138 mShaderFactory = new ShaderFactory();
139 mUpdateManager->SetShaderSaver( *mShaderFactory );
141 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
147 * The order of destructing these singletons is important!!!
150 // clear the thread local storage first
151 // allows core to be created / deleted many times in the same thread (how TET cases work).
152 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
153 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
160 mObjectRegistry.Reset();
162 // Stop relayout requests being raised on stage destruction
163 mRelayoutController.Reset();
165 // remove (last?) reference to stage
170 void Core::Initialize()
172 mStage->Initialize( *mScenes[0] );
175 Integration::ContextNotifierInterface* Core::GetContextNotifier()
180 void Core::RecoverFromContextLoss()
182 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
184 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
187 void Core::ContextCreated()
189 mRenderManager->ContextCreated();
192 void Core::ContextDestroyed()
194 mRenderManager->ContextDestroyed();
197 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
199 // set the time delta so adaptor can easily print FPS with a release build with 0 as
200 // it is cached by frametime
201 status.secondsFromLastFrame = elapsedSeconds;
203 // Render returns true when there are updates on the stage or one or more animations are completed.
204 // Use the estimated time diff till we render as the elapsed time.
205 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
206 lastVSyncTimeMilliseconds,
207 nextVSyncTimeMilliseconds,
211 // Check the Notification Manager message queue to set needsNotification
212 status.needsNotification = mNotificationManager->MessagesToProcess();
214 // Check if the default surface is changed
215 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
217 // No need to keep update running if there are notifications to process.
218 // Any message to update will wake it up anyways
221 void Core::PreRender( RenderStatus& status, bool forceClear, bool uploadOnly )
223 mRenderManager->PreRender( status, forceClear, uploadOnly );
226 void Core::PreRender( Integration::Scene& scene, std::vector<Rect<int>>& damagedRects )
228 mRenderManager->PreRender( scene, damagedRects );
231 void Core::RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo )
233 mRenderManager->RenderScene( status, scene, renderToFbo );
236 void Core::RenderScene( RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect )
238 mRenderManager->RenderScene( status, scene, renderToFbo, clippingRect );
241 void Core::PostRender( bool uploadOnly )
243 mRenderManager->PostRender( uploadOnly );
246 void Core::SceneCreated()
248 mStage->EmitSceneCreatedSignal();
250 mRelayoutController->OnApplicationSceneCreated();
252 for( const auto& scene : mScenes )
254 Dali::Actor sceneRootLayer = scene->GetRootLayer();
255 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
259 void Core::QueueEvent( const Integration::Event& event )
261 if (mScenes.size() != 0)
263 mScenes.front()->QueueEvent( event );
267 void Core::ProcessEvents()
269 // Guard against calls to ProcessEvents() during ProcessEvents()
270 if( mProcessingEvent )
272 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
273 mRenderController.RequestProcessEventsOnIdle( false );
277 mProcessingEvent = true;
278 mRelayoutController->SetProcessingCoreEvents( true );
280 // Signal that any messages received will be flushed soon
281 mUpdateManager->EventProcessingStarted();
283 // Scene could be added or removed while processing the events
284 // Copy the Scene container locally to avoid possibly invalid iterator
285 SceneContainer scenes = mScenes;
287 // process events in all scenes
288 for( auto scene : scenes )
290 scene->ProcessEvents();
293 mNotificationManager->ProcessMessages();
295 // Emit signal here to inform listeners that event processing has finished.
296 for( auto scene : scenes )
298 scene->EmitEventProcessingFinishedSignal();
301 // Run any registered processors
304 // Run the size negotiation after event processing finished signal
305 mRelayoutController->Relayout();
307 // Rebuild depth tree after event processing has finished
308 for( auto scene : scenes )
310 scene->RebuildDepthTree();
313 // Flush any queued messages for the update-thread
314 const bool messagesToProcess = mUpdateManager->FlushQueue();
316 // Check if the touch or gestures require updates.
317 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
318 // Check if the next update is forced.
319 const bool forceUpdate = IsNextUpdateForced();
321 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
323 // tell the render controller to keep update thread running
324 mRenderController.RequestUpdate( forceUpdate );
327 mRelayoutController->SetProcessingCoreEvents( false );
329 // ProcessEvents() may now be called again
330 mProcessingEvent = false;
333 uint32_t Core::GetMaximumUpdateCount() const
335 return MAXIMUM_UPDATE_COUNT;
338 void Core::RegisterProcessor( Integration::Processor& processor )
340 mProcessors.PushBack(&processor);
343 void Core::UnregisterProcessor( Integration::Processor& processor )
345 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
346 if( iter != mProcessors.End() )
348 mProcessors.Erase( iter );
352 void Core::RunProcessors()
354 // Copy processor pointers to prevent changes to vector affecting loop iterator.
355 Dali::Vector<Integration::Processor*> processors( mProcessors );
357 for( auto processor : processors )
361 processor->Process();
366 StagePtr Core::GetCurrentStage()
371 PlatformAbstraction& Core::GetPlatform()
376 UpdateManager& Core::GetUpdateManager()
378 return *(mUpdateManager);
381 RenderManager& Core::GetRenderManager()
383 return *(mRenderManager);
386 NotificationManager& Core::GetNotificationManager()
388 return *(mNotificationManager);
391 ShaderFactory& Core::GetShaderFactory()
393 return *(mShaderFactory);
396 GestureEventProcessor& Core::GetGestureEventProcessor()
398 return *(mGestureEventProcessor);
401 RelayoutController& Core::GetRelayoutController()
403 return *(mRelayoutController.Get());
406 ObjectRegistry& Core::GetObjectRegistry() const
408 return *(mObjectRegistry.Get());
411 EventThreadServices& Core::GetEventThreadServices()
416 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
418 return *(mPropertyNotificationManager);
421 AnimationPlaylist& Core::GetAnimationPlaylist() const
423 return *(mAnimationPlaylist);
426 Integration::GlAbstraction& Core::GetGlAbstraction() const
428 return mGlAbstraction;
431 void Core::AddScene( Scene* scene )
433 mScenes.push_back( scene );
436 void Core::RemoveScene( Scene* scene )
438 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
439 if( iter != mScenes.end() )
441 mScenes.erase( iter );
445 void Core::CreateThreadLocalStorage()
447 // a pointer to the ThreadLocalStorage object will be stored in TLS
448 // The ThreadLocalStorage object should be deleted by the Core destructor
449 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
453 void Core::RegisterObject( Dali::BaseObject* object )
455 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
456 mObjectRegistry->RegisterObject( object );
459 void Core::UnregisterObject( Dali::BaseObject* object )
461 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
462 mObjectRegistry->UnregisterObject( object );
465 Integration::RenderController& Core::GetRenderController()
467 return mRenderController;
470 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
472 return mUpdateManager->ReserveMessageSlot( size, updateScene );
475 BufferIndex Core::GetEventBufferIndex() const
477 return mUpdateManager->GetEventBufferIndex();
480 void Core::ForceNextUpdate()
482 mForceNextUpdate = true;
485 bool Core::IsNextUpdateForced()
487 bool nextUpdateForced = mForceNextUpdate;
488 mForceNextUpdate = false;
489 return nextUpdateForced;
492 } // namespace Internal