2 * Copyright (c) 2018 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/system-overlay.h>
23 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/events/event.h>
26 #include <dali/integration-api/gl-sync-abstraction.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/integration-api/render-controller.h>
30 #include <dali/internal/event/actors/actor-impl.h>
31 #include <dali/internal/event/animation/animation-playlist.h>
32 #include <dali/internal/event/common/notification-manager.h>
33 #include <dali/internal/event/common/property-notification-manager.h>
34 #include <dali/internal/event/common/stage-impl.h>
35 #include <dali/internal/event/common/thread-local-storage.h>
36 #include <dali/internal/event/common/type-registry-impl.h>
37 #include <dali/internal/event/effects/shader-factory.h>
38 #include <dali/internal/event/events/event-processor.h>
39 #include <dali/internal/event/events/gesture-event-processor.h>
40 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
41 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
43 #include <dali/internal/update/common/discard-queue.h>
44 #include <dali/internal/update/manager/update-manager.h>
45 #include <dali/internal/update/manager/render-task-processor.h>
47 #include <dali/internal/render/common/performance-monitor.h>
48 #include <dali/internal/render/common/render-manager.h>
49 #include <dali/internal/render/gl-resources/context.h>
51 using Dali::Internal::SceneGraph::UpdateManager;
52 using Dali::Internal::SceneGraph::RenderManager;
53 using Dali::Internal::SceneGraph::DiscardQueue;
54 using Dali::Internal::SceneGraph::RenderQueue;
58 // The Update for frame N+1 may be processed whilst frame N is being rendered.
59 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
61 #if defined(DEBUG_ENABLED)
62 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
72 using Integration::RenderController;
73 using Integration::PlatformAbstraction;
74 using Integration::GlSyncAbstraction;
75 using Integration::GestureManager;
76 using Integration::GlAbstraction;
77 using Integration::Event;
78 using Integration::UpdateStatus;
79 using Integration::RenderStatus;
81 Core::Core( RenderController& renderController,
82 PlatformAbstraction& platform,
83 GlAbstraction& glAbstraction,
84 GlSyncAbstraction& glSyncAbstraction,
85 GestureManager& gestureManager,
86 ResourcePolicy::DataRetention dataRetentionPolicy,
87 Integration::RenderToFrameBuffer renderToFboEnabled,
88 Integration::DepthBufferAvailable depthBufferAvailable,
89 Integration::StencilBufferAvailable stencilBufferAvailable )
90 : mRenderController( renderController ),
92 mProcessingEvent(false)
94 // Create the thread local storage
95 CreateThreadLocalStorage();
97 // This does nothing until Core is built with --enable-performance-monitor
98 PERFORMANCE_MONITOR_INIT( platform );
100 mNotificationManager = new NotificationManager();
102 mAnimationPlaylist = AnimationPlaylist::New();
104 mPropertyNotificationManager = PropertyNotificationManager::New();
106 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
108 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, depthBufferAvailable, stencilBufferAvailable );
110 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
112 mDiscardQueue = new DiscardQueue( renderQueue );
114 mUpdateManager = new UpdateManager( *mNotificationManager,
116 *mPropertyNotificationManager,
121 *mRenderTaskProcessor );
123 mRenderManager->SetShaderSaver( *mUpdateManager );
125 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
127 // This must be called after stage is created but before stage initialization
128 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
130 mStage->Initialize( renderToFboEnabled == Integration::RenderToFrameBuffer::TRUE );
132 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
133 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
135 mShaderFactory = new ShaderFactory();
136 mUpdateManager->SetShaderSaver( *mShaderFactory );
138 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
144 * The order of destructing these singletons is important!!!
147 // clear the thread local storage first
148 // allows core to be created / deleted many times in the same thread (how TET cases work).
149 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
150 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
157 // Stop relayout requests being raised on stage destruction
158 mRelayoutController.Reset();
160 // Clean-up stage - remove default camera and root layer
161 mStage->Uninitialize();
163 // remove (last?) reference to stage
168 Integration::ContextNotifierInterface* Core::GetContextNotifier()
173 void Core::RecoverFromContextLoss()
175 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
177 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
180 void Core::ContextCreated()
182 mRenderManager->ContextCreated();
185 void Core::ContextDestroyed()
187 mRenderManager->ContextDestroyed();
190 void Core::SurfaceResized( unsigned int width, unsigned int height )
192 mStage->SurfaceResized( width, height );
194 // The stage-size may be less than surface-size (reduced by top-margin)
195 Vector2 size = mStage->GetSize();
196 mRelayoutController->SetStageSize( size.width, size.height );
199 void Core::SetTopMargin( unsigned int margin )
201 mStage->SetTopMargin( margin );
203 // The stage-size may be less than surface-size (reduced by top-margin)
204 Vector2 size = mStage->GetSize();
205 mRelayoutController->SetStageSize( size.width, size.height );
208 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
210 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
213 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
215 // set the time delta so adaptor can easily print FPS with a release build with 0 as
216 // it is cached by frametime
217 status.secondsFromLastFrame = elapsedSeconds;
219 // Render returns true when there are updates on the stage or one or more animations are completed.
220 // Use the estimated time diff till we render as the elapsed time.
221 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
222 lastVSyncTimeMilliseconds,
223 nextVSyncTimeMilliseconds,
227 // Check the Notification Manager message queue to set needsNotification
228 status.needsNotification = mNotificationManager->MessagesToProcess();
230 // Check if the default surface is changed
231 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
233 // No need to keep update running if there are notifications to process.
234 // Any message to update will wake it up anyways
237 void Core::Render( RenderStatus& status, bool forceClear )
239 mRenderManager->Render( status, forceClear );
242 void Core::SceneCreated()
244 mStage->EmitSceneCreatedSignal();
246 mRelayoutController->OnApplicationSceneCreated();
249 void Core::QueueEvent( const Integration::Event& event )
251 mEventProcessor->QueueEvent( event );
254 void Core::ProcessEvents()
256 // Guard against calls to ProcessEvents() during ProcessEvents()
257 if( mProcessingEvent )
259 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
260 mRenderController.RequestProcessEventsOnIdle( false );
264 mProcessingEvent = true;
265 mRelayoutController->SetProcessingCoreEvents( true );
267 // Signal that any messages received will be flushed soon
268 mUpdateManager->EventProcessingStarted();
270 mEventProcessor->ProcessEvents();
272 mNotificationManager->ProcessMessages();
274 // Emit signal here to inform listeners that event processing has finished.
275 mStage->EmitEventProcessingFinishedSignal();
277 // Run any registered processors
280 // Run the size negotiation after event processing finished signal
281 mRelayoutController->Relayout();
284 // Rebuild depth tree after event processing has finished
285 mStage->RebuildDepthTree();
287 // Flush any queued messages for the update-thread
288 const bool messagesToProcess = mUpdateManager->FlushQueue();
290 // Check if the touch or gestures require updates.
291 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
292 // Check if the next update is forced.
293 const bool forceUpdate = mStage->IsNextUpdateForced();
295 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
297 // tell the render controller to keep update thread running
298 mRenderController.RequestUpdate( forceUpdate );
301 mRelayoutController->SetProcessingCoreEvents( false );
303 // ProcessEvents() may now be called again
304 mProcessingEvent = false;
307 unsigned int Core::GetMaximumUpdateCount() const
309 return MAXIMUM_UPDATE_COUNT;
312 Integration::SystemOverlay& Core::GetSystemOverlay()
314 return mStage->GetSystemOverlay();
317 void Core::SetViewMode( ViewMode viewMode )
319 mStage->SetViewMode( viewMode );
322 ViewMode Core::GetViewMode() const
324 return mStage->GetViewMode();
327 void Core::SetStereoBase( float stereoBase )
329 mStage->SetStereoBase( stereoBase );
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 float Core::GetStereoBase() const
362 return mStage->GetStereoBase();
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 void Core::CreateThreadLocalStorage()
407 // a pointer to the ThreadLocalStorage object will be stored in TLS
408 // The ThreadLocalStorage object should be deleted by the Core destructor
409 new ThreadLocalStorage(this);
412 } // namespace Internal