2 * Copyright (c) 2017 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::SurfaceResized( unsigned int width, unsigned int height, int orientation )
201 mStage->SurfaceResized( width, height, orientation );
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::SetTopMargin( unsigned int margin )
210 mStage->SetTopMargin( margin );
212 // The stage-size may be less than surface-size (reduced by top-margin)
213 Vector2 size = mStage->GetSize();
214 mRelayoutController->SetStageSize( size.width, size.height );
217 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
219 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
222 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
224 // set the time delta so adaptor can easily print FPS with a release build with 0 as
225 // it is cached by frametime
226 status.secondsFromLastFrame = elapsedSeconds;
228 // Render returns true when there are updates on the stage or one or more animations are completed.
229 // Use the estimated time diff till we render as the elapsed time.
230 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
231 lastVSyncTimeMilliseconds,
232 nextVSyncTimeMilliseconds,
236 // Check the Notification Manager message queue to set needsNotification
237 status.needsNotification = mNotificationManager->MessagesToProcess();
239 // Check if the default surface is changed
240 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
242 // No need to keep update running if there are notifications to process.
243 // Any message to update will wake it up anyways
246 void Core::Render( RenderStatus& status )
248 mRenderManager->Render( status );
251 void Core::SceneCreated()
253 mStage->EmitSceneCreatedSignal();
255 mRelayoutController->OnApplicationSceneCreated();
258 void Core::QueueEvent( const Integration::Event& event )
260 mEventProcessor->QueueEvent( event );
263 void Core::ProcessEvents()
265 // Guard against calls to ProcessEvents() during ProcessEvents()
266 if( mProcessingEvent )
268 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
269 mRenderController.RequestProcessEventsOnIdle( false );
273 mProcessingEvent = true;
274 mRelayoutController->SetProcessingCoreEvents( true );
276 // Signal that any messages received will be flushed soon
277 mUpdateManager->EventProcessingStarted();
279 mEventProcessor->ProcessEvents();
281 mNotificationManager->ProcessMessages();
283 // Emit signal here to inform listeners that event processing has finished.
284 mStage->EmitEventProcessingFinishedSignal();
286 // Run the size negotiation after event processing finished signal
287 mRelayoutController->Relayout();
289 // Rebuild depth tree after event processing has finished
290 mStage->RebuildDepthTree();
292 // Flush any queued messages for the update-thread
293 const bool messagesToProcess = mUpdateManager->FlushQueue();
295 // Check if the touch or gestures require updates.
296 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
297 // Check if the next update is forced.
298 const bool forceUpdate = mStage->IsNextUpdateForced();
300 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
302 // tell the render controller to keep update thread running
303 mRenderController.RequestUpdate( forceUpdate );
306 mRelayoutController->SetProcessingCoreEvents( false );
308 // ProcessEvents() may now be called again
309 mProcessingEvent = false;
312 unsigned int Core::GetMaximumUpdateCount() const
314 return MAXIMUM_UPDATE_COUNT;
317 Integration::SystemOverlay& Core::GetSystemOverlay()
319 return mStage->GetSystemOverlay();
322 void Core::SetViewMode( ViewMode viewMode )
324 mStage->SetViewMode( viewMode );
327 ViewMode Core::GetViewMode() const
329 return mStage->GetViewMode();
332 void Core::SetStereoBase( float stereoBase )
334 mStage->SetStereoBase( stereoBase );
337 float Core::GetStereoBase() const
339 return mStage->GetStereoBase();
342 StagePtr Core::GetCurrentStage()
347 PlatformAbstraction& Core::GetPlatform()
352 UpdateManager& Core::GetUpdateManager()
354 return *(mUpdateManager);
357 RenderManager& Core::GetRenderManager()
359 return *(mRenderManager);
362 NotificationManager& Core::GetNotificationManager()
364 return *(mNotificationManager);
367 ShaderFactory& Core::GetShaderFactory()
369 return *(mShaderFactory);
372 GestureEventProcessor& Core::GetGestureEventProcessor()
374 return *(mGestureEventProcessor);
377 RelayoutController& Core::GetRelayoutController()
379 return *(mRelayoutController.Get());
382 void Core::CreateThreadLocalStorage()
384 // a pointer to the ThreadLocalStorage object will be stored in TLS
385 // The ThreadLocalStorage object should be deleted by the Core destructor
386 new ThreadLocalStorage(this);
389 } // namespace Internal