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 : mRenderController( renderController ),
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 );
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::SurfaceDeleted( Integration::RenderSurface* surface )
199 for( auto scene : mScenes )
201 if( scene->GetSurface() == surface )
203 scene->SurfaceDeleted();
209 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
211 // set the time delta so adaptor can easily print FPS with a release build with 0 as
212 // it is cached by frametime
213 status.secondsFromLastFrame = elapsedSeconds;
215 // Render returns true when there are updates on the stage or one or more animations are completed.
216 // Use the estimated time diff till we render as the elapsed time.
217 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
218 lastVSyncTimeMilliseconds,
219 nextVSyncTimeMilliseconds,
223 // Check the Notification Manager message queue to set needsNotification
224 status.needsNotification = mNotificationManager->MessagesToProcess();
226 // Check if the default surface is changed
227 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
229 // No need to keep update running if there are notifications to process.
230 // Any message to update will wake it up anyways
233 void Core::Render( RenderStatus& status, bool forceClear )
235 mRenderManager->Render( status, forceClear );
238 void Core::SceneCreated()
240 mStage->EmitSceneCreatedSignal();
242 mRelayoutController->OnApplicationSceneCreated();
244 for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
246 Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
247 mRelayoutController->RequestRelayoutTree( sceneRootLayer );
251 void Core::QueueEvent( const Integration::Event& event )
253 if (mScenes.size() != 0)
255 mScenes.front()->QueueEvent( event );
259 void Core::ProcessEvents()
261 // Guard against calls to ProcessEvents() during ProcessEvents()
262 if( mProcessingEvent )
264 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
265 mRenderController.RequestProcessEventsOnIdle( false );
269 mProcessingEvent = true;
270 mRelayoutController->SetProcessingCoreEvents( true );
272 // Signal that any messages received will be flushed soon
273 mUpdateManager->EventProcessingStarted();
275 // Scene could be added or removed while processing the events
276 // Copy the Scene container locally to avoid possibly invalid iterator
277 SceneContainer scenes = mScenes;
279 // process events in all scenes
280 for( auto scene : scenes )
282 scene->ProcessEvents();
285 mNotificationManager->ProcessMessages();
287 // Emit signal here to inform listeners that event processing has finished.
288 for( auto scene : scenes )
290 scene->EmitEventProcessingFinishedSignal();
293 // Run any registered processors
296 // Run the size negotiation after event processing finished signal
297 mRelayoutController->Relayout();
299 // Rebuild depth tree after event processing has finished
300 for( auto scene : scenes )
302 scene->RebuildDepthTree();
305 // Flush any queued messages for the update-thread
306 const bool messagesToProcess = mUpdateManager->FlushQueue();
308 // Check if the touch or gestures require updates.
309 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
310 // Check if the next update is forced.
311 const bool forceUpdate = IsNextUpdateForced();
313 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
315 // tell the render controller to keep update thread running
316 mRenderController.RequestUpdate( forceUpdate );
319 mRelayoutController->SetProcessingCoreEvents( false );
321 // ProcessEvents() may now be called again
322 mProcessingEvent = false;
325 uint32_t Core::GetMaximumUpdateCount() const
327 return MAXIMUM_UPDATE_COUNT;
330 void Core::RegisterProcessor( Integration::Processor& processor )
332 mProcessors.PushBack(&processor);
335 void Core::UnregisterProcessor( Integration::Processor& processor )
337 auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
338 if( iter != mProcessors.End() )
340 mProcessors.Erase( iter );
344 void Core::RunProcessors()
346 // Copy processor pointers to prevent changes to vector affecting loop iterator.
347 Dali::Vector<Integration::Processor*> processors( mProcessors );
349 for( auto processor : processors )
353 processor->Process();
358 StagePtr Core::GetCurrentStage()
363 PlatformAbstraction& Core::GetPlatform()
368 UpdateManager& Core::GetUpdateManager()
370 return *(mUpdateManager);
373 RenderManager& Core::GetRenderManager()
375 return *(mRenderManager);
378 NotificationManager& Core::GetNotificationManager()
380 return *(mNotificationManager);
383 ShaderFactory& Core::GetShaderFactory()
385 return *(mShaderFactory);
388 GestureEventProcessor& Core::GetGestureEventProcessor()
390 return *(mGestureEventProcessor);
393 RelayoutController& Core::GetRelayoutController()
395 return *(mRelayoutController.Get());
398 ObjectRegistry& Core::GetObjectRegistry() const
400 return *(mObjectRegistry.Get());
403 EventThreadServices& Core::GetEventThreadServices()
408 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
410 return *(mPropertyNotificationManager);
413 AnimationPlaylist& Core::GetAnimationPlaylist() const
415 return *(mAnimationPlaylist);
418 void Core::AddScene( Scene* scene )
420 mScenes.push_back( scene );
423 void Core::RemoveScene( Scene* scene )
425 auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
426 if( iter != mScenes.end() )
428 mScenes.erase( iter );
432 void Core::CreateThreadLocalStorage()
434 // a pointer to the ThreadLocalStorage object will be stored in TLS
435 // The ThreadLocalStorage object should be deleted by the Core destructor
436 new ThreadLocalStorage(this);
439 void Core::RegisterObject( Dali::BaseObject* object )
441 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
442 mObjectRegistry->RegisterObject( object );
445 void Core::UnregisterObject( Dali::BaseObject* object )
447 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
448 mObjectRegistry->UnregisterObject( object );
451 Integration::RenderController& Core::GetRenderController()
453 return mRenderController;
456 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
458 return mUpdateManager->ReserveMessageSlot( size, updateScene );
461 BufferIndex Core::GetEventBufferIndex() const
463 return mUpdateManager->GetEventBufferIndex();
466 void Core::ForceNextUpdate()
468 mForceNextUpdate = true;
471 bool Core::IsNextUpdateForced()
473 bool nextUpdateForced = mForceNextUpdate;
474 mForceNextUpdate = false;
475 return nextUpdateForced;
478 } // namespace Internal