2 * Copyright (c) 2014 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/platform-abstraction.h>
26 #include <dali/integration-api/gl-sync-abstraction.h>
27 #include <dali/integration-api/render-controller.h>
28 #include <dali/internal/event/actors/actor-impl.h>
29 #include <dali/internal/event/common/stage-impl.h>
30 #include <dali/internal/event/animation/animation-playlist.h>
31 #include <dali/internal/event/common/property-notification-manager.h>
32 #include <dali/internal/event/common/notification-manager.h>
33 #include <dali/integration-api/events/event.h>
34 #include <dali/internal/event/events/event-processor.h>
35 #include <dali/internal/event/events/gesture-event-processor.h>
36 #include <dali/internal/update/manager/update-manager.h>
37 #include <dali/internal/render/common/performance-monitor.h>
38 #include <dali/internal/render/common/render-manager.h>
39 #include <dali/internal/update/common/discard-queue.h>
40 #include <dali/internal/update/resources/resource-manager.h>
41 #include <dali/internal/event/images/image-factory.h>
42 #include <dali/internal/event/common/thread-local-storage.h>
43 #include <dali/internal/event/effects/shader-factory.h>
44 #include <dali/internal/update/touch/touch-resampler.h>
45 #include <dali/internal/event/common/type-registry-impl.h>
46 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
47 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
49 #include <dali/internal/render/gl-resources/texture-cache.h>
50 #include <dali/internal/render/gl-resources/context.h>
52 using Dali::Internal::SceneGraph::UpdateManager;
53 using Dali::Internal::SceneGraph::RenderManager;
54 using Dali::Internal::SceneGraph::DiscardQueue;
55 using Dali::Internal::SceneGraph::RenderQueue;
56 using Dali::Internal::SceneGraph::TextureCache;
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");
74 using Integration::RenderController;
75 using Integration::PlatformAbstraction;
76 using Integration::GlSyncAbstraction;
77 using Integration::GestureManager;
78 using Integration::GlAbstraction;
79 using Integration::Event;
80 using Integration::UpdateStatus;
81 using Integration::RenderStatus;
83 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
84 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
85 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
86 : mRenderController( renderController ),
88 mGestureEventProcessor(NULL),
89 mEventProcessor(NULL),
93 mResourcePostProcessQueue(),
94 mNotificationManager(NULL),
98 mProcessingEvent(false)
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 std::vector< ResourcePostProcessRequest> init;
113 mResourcePostProcessQueue = new ResourcePostProcessList(init);
115 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
117 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
118 TextureCache& textureCache = mRenderManager->GetTextureCache();
120 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::DISCARD;
121 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
123 discardPolicy = ResourcePolicy::RETAIN;
125 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
127 mDiscardQueue = new DiscardQueue( renderQueue );
129 mResourceManager = new ResourceManager( mPlatform,
130 *mNotificationManager,
132 *mResourcePostProcessQueue,
137 mTouchResampler = TouchResampler::New();
139 mUpdateManager = new UpdateManager( *mNotificationManager,
142 *mPropertyNotificationManager,
151 mRenderManager->SetShaderSaver( *mUpdateManager );
153 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
155 // This must be called after stage is created but before stage initialization
156 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
158 mStage->Initialize();
160 mResourceClient = new ResourceClient( *mResourceManager, *mStage, dataRetentionPolicy );
162 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
163 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
165 mImageFactory = new ImageFactory( *mResourceClient );
166 mShaderFactory = new ShaderFactory();
167 mUpdateManager->SetShaderSaver( *mShaderFactory );
168 mShaderFactory->LoadDefaultShaders();
170 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
176 * TODO this should be done by Adaptor, Core does not know about threading
177 * First stop the resource loading thread(s)
179 mPlatform.JoinLoaderThreads();
182 * The order of destructing these singletons is important!!!
185 // clear the thread local storage first
186 // allows core to be created / deleted many times in the same thread (how TET cases work).
187 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
188 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
194 // Stop relayout requests being raised on stage destruction
195 mRelayoutController.Reset();
197 // Clean-up stage - remove default camera and root layer
198 mStage->Uninitialize();
200 // remove (last?) reference to stage
203 delete mEventProcessor;
204 delete mGestureEventProcessor;
205 delete mNotificationManager;
206 delete mImageFactory;
207 delete mShaderFactory;
208 delete mResourceClient;
209 delete mResourceManager;
210 delete mUpdateManager;
211 delete mTouchResampler;
212 delete mRenderManager;
213 delete mDiscardQueue;
214 delete mResourcePostProcessQueue;
217 Integration::ContextNotifierInterface* Core::GetContextNotifier()
222 void Core::RecoverFromContextLoss()
224 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
226 mImageFactory->RecoverFromContextLoss(); // Reload images from files
227 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
230 void Core::ContextCreated()
232 mRenderManager->ContextCreated();
235 void Core::ContextDestroyed()
237 mRenderManager->ContextDestroyed();
240 void Core::SurfaceResized( unsigned int width, unsigned int height )
242 mStage->SetSize( width, height );
243 mRelayoutController->SetStageSize( width, height );
246 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
248 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
249 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
252 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
254 // set the time delta so adaptor can easily print FPS with a release build with 0 as
255 // it is cached by frametime
256 status.secondsFromLastFrame = elapsedSeconds;
258 // Render returns true when there are updates on the stage or one or more animations are completed.
259 // Use the estimated time diff till we render as the elapsed time.
260 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
261 lastVSyncTimeMilliseconds,
262 nextVSyncTimeMilliseconds );
264 // Check the Notification Manager message queue to set needsNotification
265 status.needsNotification = mNotificationManager->MessagesToProcess();
267 // No need to keep update running if there are notifications to process.
268 // Any message to update will wake it up anyways
270 if ( mResourceManager->ResourcesToProcess() )
272 // If we are still processing resources, then we have to continue the update
273 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
277 void Core::Render( RenderStatus& status )
279 bool updateRequired = mRenderManager->Render( status );
281 status.SetNeedsUpdate( updateRequired );
297 // trigger processing of events queued up while paused
301 void Core::SceneCreated()
303 mStage->EmitSceneCreatedSignal();
305 mRelayoutController->OnApplicationSceneCreated();
308 void Core::QueueEvent( const Integration::Event& event )
310 mEventProcessor->QueueEvent( event );
313 void Core::ProcessEvents()
315 // Guard against calls to ProcessEvents() during ProcessEvents()
316 if( mProcessingEvent )
318 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
319 mRenderController.RequestProcessEventsOnIdle();
323 mProcessingEvent = true;
324 mRelayoutController->SetProcessingCoreEvents( true );
326 // Signal that any messages received will be flushed soon
327 mUpdateManager->EventProcessingStarted();
329 mEventProcessor->ProcessEvents();
331 mNotificationManager->ProcessMessages();
333 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
336 // Emit signal here to start size negotiation and control relayout.
337 mStage->EmitEventProcessingFinishedSignal();
339 // Run the size negotiation after event processing finished signal
340 mRelayoutController->Relayout();
342 // Flush discard queue for image factory
343 mImageFactory->FlushReleaseQueue();
345 // Flush any queued messages for the update-thread
346 const bool messagesToProcess = mUpdateManager->FlushQueue();
348 // Check if the touch or gestures require updates.
349 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
350 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
352 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
354 // tell the render controller to keep update thread running
355 mRenderController.RequestUpdate();
359 mRelayoutController->SetProcessingCoreEvents( false );
361 // ProcessEvents() may now be called again
362 mProcessingEvent = false;
365 void Core::UpdateTouchData(const Integration::TouchData& touch)
367 mTouchResampler->SendTouchData( touch );
370 unsigned int Core::GetMaximumUpdateCount() const
372 return MAXIMUM_UPDATE_COUNT;
375 Integration::SystemOverlay& Core::GetSystemOverlay()
377 return mStage->GetSystemOverlay();
380 void Core::SetViewMode( ViewMode viewMode )
382 mStage->SetViewMode( viewMode );
385 ViewMode Core::GetViewMode() const
387 return mStage->GetViewMode();
390 void Core::SetStereoBase( float stereoBase )
392 mStage->SetStereoBase( stereoBase );
395 float Core::GetStereoBase() const
397 return mStage->GetStereoBase();
400 StagePtr Core::GetCurrentStage()
405 PlatformAbstraction& Core::GetPlatform()
410 UpdateManager& Core::GetUpdateManager()
412 return *(mUpdateManager);
415 RenderManager& Core::GetRenderManager()
417 return *(mRenderManager);
420 NotificationManager& Core::GetNotificationManager()
422 return *(mNotificationManager);
425 ResourceManager& Core::GetResourceManager()
427 return *(mResourceManager);
430 ResourceClient& Core::GetResourceClient()
432 return *(mResourceClient);
435 ImageFactory& Core::GetImageFactory()
437 return *(mImageFactory);
440 ShaderFactory& Core::GetShaderFactory()
442 return *(mShaderFactory);
445 GestureEventProcessor& Core::GetGestureEventProcessor()
447 return *(mGestureEventProcessor);
450 RelayoutController& Core::GetRelayoutController()
452 return *(mRelayoutController.Get());
455 void Core::CreateThreadLocalStorage()
457 // a pointer to the ThreadLocalStorage object will be stored in TLS
458 // and automatically deleted when the thread is killed
459 new ThreadLocalStorage(this);
462 } // namespace Internal