2 * Copyright (c) 2016 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/images/image-factory.h>
41 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
42 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
44 #include <dali/internal/update/common/discard-queue.h>
45 #include <dali/internal/update/common/texture-cache-dispatcher.h>
46 #include <dali/internal/update/manager/update-manager.h>
47 #include <dali/internal/update/manager/render-task-processor.h>
48 #include <dali/internal/update/resources/resource-manager.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/texture-cache.h>
53 #include <dali/internal/render/gl-resources/context.h>
55 using Dali::Internal::SceneGraph::UpdateManager;
56 using Dali::Internal::SceneGraph::RenderManager;
57 using Dali::Internal::SceneGraph::DiscardQueue;
58 using Dali::Internal::SceneGraph::RenderQueue;
59 using Dali::Internal::SceneGraph::TextureCache;
60 using Dali::Internal::SceneGraph::TextureCacheDispatcher;
64 // The Update for frame N+1 may be processed whilst frame N is being rendered.
65 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
67 #if defined(DEBUG_ENABLED)
68 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
78 using Integration::RenderController;
79 using Integration::PlatformAbstraction;
80 using Integration::GlSyncAbstraction;
81 using Integration::GestureManager;
82 using Integration::GlAbstraction;
83 using Integration::Event;
84 using Integration::UpdateStatus;
85 using Integration::RenderStatus;
87 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
88 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
89 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
90 : mRenderController( renderController ),
92 mGestureEventProcessor(NULL),
93 mEventProcessor(NULL),
97 mTextureUploadedQueue(),
98 mNotificationManager(NULL),
100 mShaderFactory(NULL),
102 mProcessingEvent(false)
104 // Create the thread local storage
105 CreateThreadLocalStorage();
107 // This does nothing until Core is built with --enable-performance-monitor
108 PERFORMANCE_MONITOR_INIT( platform );
110 mNotificationManager = new NotificationManager();
112 mAnimationPlaylist = AnimationPlaylist::New();
114 mPropertyNotificationManager = PropertyNotificationManager::New();
116 mTextureUploadedQueue = new LockedResourceQueue;
118 mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, *mTextureUploadedQueue );
120 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
122 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
123 TextureCache& textureCache = mRenderManager->GetTextureCache();
125 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::OWNED_DISCARD;
126 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
128 discardPolicy = ResourcePolicy::OWNED_RETAIN;
130 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
132 mTextureCacheDispatcher = new SceneGraph::TextureCacheDispatcher( renderQueue, textureCache );
134 mDiscardQueue = new DiscardQueue( renderQueue );
136 mResourceManager = new ResourceManager( mPlatform,
137 *mNotificationManager,
138 *mTextureCacheDispatcher,
139 *mTextureUploadedQueue,
143 mUpdateManager = new UpdateManager( *mNotificationManager,
145 *mPropertyNotificationManager,
151 *mTextureCacheDispatcher,
152 *mRenderTaskProcessor );
154 mRenderManager->SetShaderSaver( *mUpdateManager );
156 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
158 // This must be called after stage is created but before stage initialization
159 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
161 mStage->Initialize();
163 mResourceClient = new ResourceClient( *mResourceManager, *mStage );
165 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
166 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
168 mImageFactory = new ImageFactory( *mResourceClient );
169 mShaderFactory = new ShaderFactory();
170 mUpdateManager->SetShaderSaver( *mShaderFactory );
171 mShaderFactory->LoadDefaultShaders();
173 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
179 * TODO this should be done by Adaptor, Core does not know about threading
180 * First stop the resource loading thread(s)
182 mPlatform.JoinLoaderThreads();
185 * The order of destructing these singletons is important!!!
188 // clear the thread local storage first
189 // allows core to be created / deleted many times in the same thread (how TET cases work).
190 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
191 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
197 // Stop relayout requests being raised on stage destruction
198 mRelayoutController.Reset();
200 // Clean-up stage - remove default camera and root layer
201 mStage->Uninitialize();
203 // remove (last?) reference to stage
206 delete mEventProcessor;
207 delete mGestureEventProcessor;
208 delete mNotificationManager;
209 delete mImageFactory;
210 delete mShaderFactory;
211 delete mResourceClient;
212 delete mResourceManager;
213 delete mDiscardQueue;
214 delete mTextureCacheDispatcher;
215 delete mUpdateManager;
216 delete mRenderManager;
217 delete mRenderTaskProcessor;
218 delete mTextureUploadedQueue;
221 Integration::ContextNotifierInterface* Core::GetContextNotifier()
226 void Core::RecoverFromContextLoss()
228 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
230 mImageFactory->RecoverFromContextLoss(); // Reload images from files
231 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
234 void Core::ContextCreated()
236 mRenderManager->ContextCreated();
239 void Core::ContextDestroyed()
241 mRenderManager->ContextDestroyed();
244 void Core::SurfaceResized( unsigned int width, unsigned int height )
246 mStage->SurfaceResized( width, height );
248 // The stage-size may be less than surface-size (reduced by top-margin)
249 Vector2 size = mStage->GetSize();
250 mRelayoutController->SetStageSize( size.width, size.height );
253 void Core::SetTopMargin( unsigned int margin )
255 mStage->SetTopMargin( margin );
257 // The stage-size may be less than surface-size (reduced by top-margin)
258 Vector2 size = mStage->GetSize();
259 mRelayoutController->SetStageSize( size.width, size.height );
262 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
264 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
267 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
269 // set the time delta so adaptor can easily print FPS with a release build with 0 as
270 // it is cached by frametime
271 status.secondsFromLastFrame = elapsedSeconds;
273 // Render returns true when there are updates on the stage or one or more animations are completed.
274 // Use the estimated time diff till we render as the elapsed time.
275 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
276 lastVSyncTimeMilliseconds,
277 nextVSyncTimeMilliseconds );
279 // Check the Notification Manager message queue to set needsNotification
280 status.needsNotification = mNotificationManager->MessagesToProcess();
282 // No need to keep update running if there are notifications to process.
283 // Any message to update will wake it up anyways
285 if ( mResourceManager->ResourcesToProcess() )
287 // If we are still processing resources, then we have to continue the update
288 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
292 void Core::Render( RenderStatus& status )
294 bool updateRequired = mRenderManager->Render( status );
296 status.SetNeedsUpdate( updateRequired );
312 // trigger processing of events queued up while paused
316 void Core::SceneCreated()
318 mStage->EmitSceneCreatedSignal();
320 mRelayoutController->OnApplicationSceneCreated();
323 void Core::QueueEvent( const Integration::Event& event )
325 mEventProcessor->QueueEvent( event );
328 void Core::ProcessEvents()
330 // Guard against calls to ProcessEvents() during ProcessEvents()
331 if( mProcessingEvent )
333 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
334 mRenderController.RequestProcessEventsOnIdle();
338 mProcessingEvent = true;
339 mRelayoutController->SetProcessingCoreEvents( true );
341 // Signal that any messages received will be flushed soon
342 mUpdateManager->EventProcessingStarted();
344 mEventProcessor->ProcessEvents();
346 mNotificationManager->ProcessMessages();
348 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
351 // Emit signal here to start size negotiation and control relayout.
352 mStage->EmitEventProcessingFinishedSignal();
354 // Run the size negotiation after event processing finished signal
355 mRelayoutController->Relayout();
357 // Flush discard queue for image factory
358 mImageFactory->FlushReleaseQueue();
360 // Flush any queued messages for the update-thread
361 const bool messagesToProcess = mUpdateManager->FlushQueue();
363 // Check if the touch or gestures require updates.
364 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
366 if( messagesToProcess || gestureNeedsUpdate )
368 // tell the render controller to keep update thread running
369 mRenderController.RequestUpdate();
373 mRelayoutController->SetProcessingCoreEvents( false );
375 // ProcessEvents() may now be called again
376 mProcessingEvent = false;
379 unsigned int Core::GetMaximumUpdateCount() const
381 return MAXIMUM_UPDATE_COUNT;
384 Integration::SystemOverlay& Core::GetSystemOverlay()
386 return mStage->GetSystemOverlay();
389 void Core::SetViewMode( ViewMode viewMode )
391 mStage->SetViewMode( viewMode );
394 ViewMode Core::GetViewMode() const
396 return mStage->GetViewMode();
399 void Core::SetStereoBase( float stereoBase )
401 mStage->SetStereoBase( stereoBase );
404 float Core::GetStereoBase() const
406 return mStage->GetStereoBase();
409 StagePtr Core::GetCurrentStage()
414 PlatformAbstraction& Core::GetPlatform()
419 UpdateManager& Core::GetUpdateManager()
421 return *(mUpdateManager);
424 RenderManager& Core::GetRenderManager()
426 return *(mRenderManager);
429 NotificationManager& Core::GetNotificationManager()
431 return *(mNotificationManager);
434 ResourceManager& Core::GetResourceManager()
436 return *(mResourceManager);
439 ResourceClient& Core::GetResourceClient()
441 return *(mResourceClient);
444 ImageFactory& Core::GetImageFactory()
446 return *(mImageFactory);
449 ShaderFactory& Core::GetShaderFactory()
451 return *(mShaderFactory);
454 GestureEventProcessor& Core::GetGestureEventProcessor()
456 return *(mGestureEventProcessor);
459 RelayoutController& Core::GetRelayoutController()
461 return *(mRelayoutController.Get());
464 void Core::CreateThreadLocalStorage()
466 // a pointer to the ThreadLocalStorage object will be stored in TLS
467 // and automatically deleted when the thread is killed
468 new ThreadLocalStorage(this);
471 } // namespace Internal