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/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 ResourcePolicy::DataRetention dataRetentionPolicy,
90 Integration::RenderToFrameBuffer renderToFboEnabled,
91 Integration::DepthBufferAvailable depthBufferAvailable,
92 Integration::StencilBufferAvailable stencilBufferAvailable,
93 Integration::PartialUpdateAvailable partialUpdateAvailable )
94 : mRenderController( renderController ),
96 mProcessingEvent(false),
97 mForceNextUpdate( false )
99 // Create the thread local storage
100 CreateThreadLocalStorage();
102 // This does nothing until Core is built with --enable-performance-monitor
103 PERFORMANCE_MONITOR_INIT( platform );
105 mNotificationManager = new NotificationManager();
107 mAnimationPlaylist = AnimationPlaylist::New();
109 mPropertyNotificationManager = PropertyNotificationManager::New();
111 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
113 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable );
115 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
117 mDiscardQueue = new DiscardQueue( renderQueue );
119 mUpdateManager = new UpdateManager( *mNotificationManager,
121 *mPropertyNotificationManager,
126 *mRenderTaskProcessor,
127 partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE );
129 mRenderManager->SetShaderSaver( *mUpdateManager );
131 mObjectRegistry = ObjectRegistry::New();
133 mStage = IntrusivePtr<Stage>( Stage::New( *mUpdateManager ) );
135 // This must be called after stage is created but before stage initialization
136 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
138 mGestureEventProcessor = new GestureEventProcessor( *mUpdateManager, mRenderController );
140 mShaderFactory = new ShaderFactory();
141 mUpdateManager->SetShaderSaver( *mShaderFactory );
143 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
149 * The order of destructing these singletons is important!!!
152 // clear the thread local storage first
153 // allows core to be created / deleted many times in the same thread (how TET cases work).
154 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
155 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
162 mObjectRegistry.Reset();
164 // Stop relayout requests being raised on stage destruction
165 mRelayoutController.Reset();
167 // remove (last?) reference to stage
172 void Core::Initialize()
174 mStage->Initialize( *mScenes[0] );
177 Integration::ContextNotifierInterface* Core::GetContextNotifier()
182 void Core::RecoverFromContextLoss()
184 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
186 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
189 void Core::ContextCreated()
191 mRenderManager->ContextCreated();
194 void Core::ContextDestroyed()
196 mRenderManager->ContextDestroyed();
199 void Core::SurfaceDeleted( Integration::RenderSurface* surface )
201 for( auto scene : mScenes )
203 if( scene->GetSurface() == surface )
205 scene->SurfaceDeleted();
211 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
213 // set the time delta so adaptor can easily print FPS with a release build with 0 as
214 // it is cached by frametime
215 status.secondsFromLastFrame = elapsedSeconds;
217 // Render returns true when there are updates on the stage or one or more animations are completed.
218 // Use the estimated time diff till we render as the elapsed time.
219 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
220 lastVSyncTimeMilliseconds,
221 nextVSyncTimeMilliseconds,
225 // Check the Notification Manager message queue to set needsNotification
226 status.needsNotification = mNotificationManager->MessagesToProcess();
228 // Check if the default surface is changed
229 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
231 // No need to keep update running if there are notifications to process.
232 // Any message to update will wake it up anyways
235 void Core::Render( RenderStatus& status, bool forceClear, bool uploadOnly )
237 mRenderManager->Render( status, forceClear, uploadOnly );
240 void Core::SceneCreated()
242 mStage->EmitSceneCreatedSignal();
244 mRelayoutController->OnApplicationSceneCreated();
246 for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
248 Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
249 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
253 void Core::QueueEvent( const Integration::Event& event )
255 if (mScenes.size() != 0)
257 mScenes.front()->QueueEvent( event );
261 void Core::ProcessEvents()
263 // Guard against calls to ProcessEvents() during ProcessEvents()
264 if( mProcessingEvent )
266 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
267 mRenderController.RequestProcessEventsOnIdle( false );
271 mProcessingEvent = true;
272 mRelayoutController->SetProcessingCoreEvents( true );
274 // Signal that any messages received will be flushed soon
275 mUpdateManager->EventProcessingStarted();
277 // Scene could be added or removed while processing the events
278 // Copy the Scene container locally to avoid possibly invalid iterator
279 SceneContainer scenes = mScenes;
281 // process events in all scenes
282 for( auto scene : scenes )
284 scene->ProcessEvents();
287 mNotificationManager->ProcessMessages();
289 // Emit signal here to inform listeners that event processing has finished.
290 for( auto scene : scenes )
292 scene->EmitEventProcessingFinishedSignal();
295 // Run any registered processors
298 // Run the size negotiation after event processing finished signal
299 mRelayoutController->Relayout();
301 // Rebuild depth tree after event processing has finished
302 for( auto scene : scenes )
304 scene->RebuildDepthTree();
307 // Flush any queued messages for the update-thread
308 const bool messagesToProcess = mUpdateManager->FlushQueue();
310 // Check if the touch or gestures require updates.
311 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
312 // Check if the next update is forced.
313 const bool forceUpdate = IsNextUpdateForced();
315 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
317 // tell the render controller to keep update thread running
318 mRenderController.RequestUpdate( forceUpdate );
321 mRelayoutController->SetProcessingCoreEvents( false );
323 // ProcessEvents() may now be called again
324 mProcessingEvent = false;
327 uint32_t Core::GetMaximumUpdateCount() const
329 return MAXIMUM_UPDATE_COUNT;
332 void Core::RegisterProcessor( Integration::Processor& processor )
334 mProcessors.PushBack(&processor);
337 void Core::UnregisterProcessor( Integration::Processor& processor )
339 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
340 if( iter != mProcessors.End() )
342 mProcessors.Erase( iter );
346 void Core::RunProcessors()
348 // Copy processor pointers to prevent changes to vector affecting loop iterator.
349 Dali::Vector<Integration::Processor*> processors( mProcessors );
351 for( auto processor : processors )
355 processor->Process();
360 StagePtr Core::GetCurrentStage()
365 PlatformAbstraction& Core::GetPlatform()
370 UpdateManager& Core::GetUpdateManager()
372 return *(mUpdateManager);
375 RenderManager& Core::GetRenderManager()
377 return *(mRenderManager);
380 NotificationManager& Core::GetNotificationManager()
382 return *(mNotificationManager);
385 ShaderFactory& Core::GetShaderFactory()
387 return *(mShaderFactory);
390 GestureEventProcessor& Core::GetGestureEventProcessor()
392 return *(mGestureEventProcessor);
395 RelayoutController& Core::GetRelayoutController()
397 return *(mRelayoutController.Get());
400 ObjectRegistry& Core::GetObjectRegistry() const
402 return *(mObjectRegistry.Get());
405 EventThreadServices& Core::GetEventThreadServices()
410 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
412 return *(mPropertyNotificationManager);
415 AnimationPlaylist& Core::GetAnimationPlaylist() const
417 return *(mAnimationPlaylist);
420 void Core::AddScene( Scene* scene )
422 mScenes.push_back( scene );
425 void Core::RemoveScene( Scene* scene )
427 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
428 if( iter != mScenes.end() )
430 mScenes.erase( iter );
434 void Core::CreateThreadLocalStorage()
436 // a pointer to the ThreadLocalStorage object will be stored in TLS
437 // The ThreadLocalStorage object should be deleted by the Core destructor
438 new ThreadLocalStorage(this);
441 void Core::RegisterObject( Dali::BaseObject* object )
443 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
444 mObjectRegistry->RegisterObject( object );
447 void Core::UnregisterObject( Dali::BaseObject* object )
449 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
450 mObjectRegistry->UnregisterObject( object );
453 Integration::RenderController& Core::GetRenderController()
455 return mRenderController;
458 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
460 return mUpdateManager->ReserveMessageSlot( size, updateScene );
463 BufferIndex Core::GetEventBufferIndex() const
465 return mUpdateManager->GetEventBufferIndex();
468 void Core::ForceNextUpdate()
470 mForceNextUpdate = true;
473 bool Core::IsNextUpdateForced()
475 bool nextUpdateForced = mForceNextUpdate;
476 mForceNextUpdate = false;
477 return nextUpdateForced;
480 } // namespace Internal