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>
20 #include <dali/integration-api/graphics/graphics.h>
23 #include <dali/integration-api/system-overlay.h>
24 #include <dali/integration-api/core.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/events/event.h>
27 #include <dali/integration-api/gl-sync-abstraction.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/integration-api/render-controller.h>
30 #include <dali/integration-api/graphics/graphics.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/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 unsigned int MAXIMUM_UPDATE_COUNT = 2u;
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
73 using Integration::RenderController;
74 using Integration::PlatformAbstraction;
75 using Integration::GlSyncAbstraction;
76 using Integration::GestureManager;
77 using Integration::GlAbstraction;
78 using Integration::Event;
79 using Integration::UpdateStatus;
80 using Integration::RenderStatus;
81 using Integration::Graphics::Graphics;
84 Core::Core( RenderController& renderController,
85 PlatformAbstraction& platform,
87 GlAbstraction& glAbstraction,
88 GlSyncAbstraction& glSyncAbstraction,
89 GestureManager& gestureManager,
90 ResourcePolicy::DataRetention dataRetentionPolicy,
91 bool renderToFboEnabled )
92 : mRenderController( renderController ),
94 mProcessingEvent(false),
97 // fixme: for now to ensure libgraphics.a won't be removed during linking due to being
98 Integration::Graphics::IncludeThisLibrary();
100 // Create the thread local storage
101 CreateThreadLocalStorage();
103 // This does nothing until Core is built with --enable-performance-monitor
104 PERFORMANCE_MONITOR_INIT( platform );
106 mNotificationManager = new NotificationManager();
108 mAnimationPlaylist = AnimationPlaylist::New();
110 mPropertyNotificationManager = PropertyNotificationManager::New();
112 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
114 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction );
116 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
118 mDiscardQueue = new DiscardQueue( renderQueue );
120 mUpdateManager = new UpdateManager( *mNotificationManager,
122 *mPropertyNotificationManager,
127 *mRenderTaskProcessor );
129 mRenderManager->SetShaderSaver( *mUpdateManager );
131 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
133 // This must be called after stage is created but before stage initialization
134 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
136 mStage->Initialize( renderToFboEnabled );
138 mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
139 mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
141 mShaderFactory = new ShaderFactory();
142 mUpdateManager->SetShaderSaver( *mShaderFactory );
144 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
150 * The order of destructing these singletons is important!!!
153 // clear the thread local storage first
154 // allows core to be created / deleted many times in the same thread (how TET cases work).
155 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
156 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
163 // Stop relayout requests being raised on stage destruction
164 mRelayoutController.Reset();
166 // Clean-up stage - remove default camera and root layer
167 mStage->Uninitialize();
169 // remove (last?) reference to stage
174 Integration::ContextNotifierInterface* Core::GetContextNotifier()
179 void Core::RecoverFromContextLoss()
181 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
183 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
186 void Core::ContextCreated()
188 mRenderManager->ContextCreated();
191 void Core::ContextDestroyed()
193 mRenderManager->ContextDestroyed();
196 void Core::SurfaceResized( unsigned int width, unsigned int height )
198 mStage->SurfaceResized( width, height );
200 // The stage-size may be less than surface-size (reduced by top-margin)
201 Vector2 size = mStage->GetSize();
202 mRelayoutController->SetStageSize( size.width, size.height );
205 void Core::SetTopMargin( unsigned int margin )
207 mStage->SetTopMargin( margin );
209 // The stage-size may be less than surface-size (reduced by top-margin)
210 Vector2 size = mStage->GetSize();
211 mRelayoutController->SetStageSize( size.width, size.height );
214 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
216 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
219 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
221 // set the time delta so adaptor can easily print FPS with a release build with 0 as
222 // it is cached by frametime
223 status.secondsFromLastFrame = elapsedSeconds;
225 // Render returns true when there are updates on the stage or one or more animations are completed.
226 // Use the estimated time diff till we render as the elapsed time.
227 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
228 lastVSyncTimeMilliseconds,
229 nextVSyncTimeMilliseconds,
233 // Check the Notification Manager message queue to set needsNotification
234 status.needsNotification = mNotificationManager->MessagesToProcess();
236 // No need to keep update running if there are notifications to process.
237 // Any message to update will wake it up anyways
240 void Core::Render( RenderStatus& status )
242 mRenderManager->Render( status );
245 void Core::SceneCreated()
247 mStage->EmitSceneCreatedSignal();
249 mRelayoutController->OnApplicationSceneCreated();
252 void Core::QueueEvent( const Integration::Event& event )
254 mEventProcessor->QueueEvent( event );
257 void Core::ProcessEvents()
259 // Guard against calls to ProcessEvents() during ProcessEvents()
260 if( mProcessingEvent )
262 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
263 mRenderController.RequestProcessEventsOnIdle( false );
267 mProcessingEvent = true;
268 mRelayoutController->SetProcessingCoreEvents( true );
270 // Signal that any messages received will be flushed soon
271 mUpdateManager->EventProcessingStarted();
273 mEventProcessor->ProcessEvents();
275 mNotificationManager->ProcessMessages();
277 // Emit signal here to inform listeners that event processing has finished.
278 mStage->EmitEventProcessingFinishedSignal();
280 // Run the size negotiation after event processing finished signal
281 mRelayoutController->Relayout();
283 // Rebuild depth tree after event processing has finished
284 mStage->RebuildDepthTree();
286 // Flush any queued messages for the update-thread
287 const bool messagesToProcess = mUpdateManager->FlushQueue();
289 // Check if the touch or gestures require updates.
290 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
291 // Check if the next update is forced.
292 const bool forceUpdate = mStage->IsNextUpdateForced();
294 if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
296 // tell the render controller to keep update thread running
297 mRenderController.RequestUpdate( forceUpdate );
300 mRelayoutController->SetProcessingCoreEvents( false );
302 // ProcessEvents() may now be called again
303 mProcessingEvent = false;
306 unsigned int Core::GetMaximumUpdateCount() const
308 return MAXIMUM_UPDATE_COUNT;
311 Integration::SystemOverlay& Core::GetSystemOverlay()
313 return mStage->GetSystemOverlay();
316 void Core::SetViewMode( ViewMode viewMode )
318 mStage->SetViewMode( viewMode );
321 ViewMode Core::GetViewMode() const
323 return mStage->GetViewMode();
326 void Core::SetStereoBase( float stereoBase )
328 mStage->SetStereoBase( stereoBase );
331 float Core::GetStereoBase() const
333 return mStage->GetStereoBase();
336 StagePtr Core::GetCurrentStage()
341 PlatformAbstraction& Core::GetPlatform()
346 UpdateManager& Core::GetUpdateManager()
348 return *(mUpdateManager);
351 RenderManager& Core::GetRenderManager()
353 return *(mRenderManager);
356 NotificationManager& Core::GetNotificationManager()
358 return *(mNotificationManager);
361 ShaderFactory& Core::GetShaderFactory()
363 return *(mShaderFactory);
366 GestureEventProcessor& Core::GetGestureEventProcessor()
368 return *(mGestureEventProcessor);
371 RelayoutController& Core::GetRelayoutController()
373 return *(mRelayoutController.Get());
376 void Core::CreateThreadLocalStorage()
378 // a pointer to the ThreadLocalStorage object will be stored in TLS
379 // The ThreadLocalStorage object should be deleted by the Core destructor
380 new ThreadLocalStorage(this);
383 } // namespace Internal