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/text/font-factory.h>
42 #include <dali/internal/event/images/image-factory.h>
43 #include <dali/internal/event/images/emoji-factory.h>
44 #include <dali/internal/event/common/thread-local-storage.h>
45 #include <dali/internal/event/effects/shader-factory.h>
46 #include <dali/internal/update/touch/touch-resampler.h>
47 #include <dali/internal/event/common/type-registry-impl.h>
48 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
49 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
51 #include <dali/internal/render/gl-resources/texture-cache.h>
52 #include <dali/internal/render/gl-resources/context.h>
54 using Dali::Internal::SceneGraph::UpdateManager;
55 using Dali::Internal::SceneGraph::RenderManager;
56 using Dali::Internal::SceneGraph::DiscardQueue;
57 using Dali::Internal::SceneGraph::RenderQueue;
58 using Dali::Internal::SceneGraph::TextureCache;
62 // The Update for frame N+1 may be processed whilst frame N is being rendered.
63 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
65 #if defined(DEBUG_ENABLED)
66 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
76 using Integration::RenderController;
77 using Integration::PlatformAbstraction;
78 using Integration::GlSyncAbstraction;
79 using Integration::GestureManager;
80 using Integration::GlAbstraction;
81 using Integration::Event;
82 using Integration::UpdateStatus;
83 using Integration::RenderStatus;
85 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
86 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
87 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
88 : mRenderController( renderController ),
90 mGestureEventProcessor(NULL),
91 mEventProcessor(NULL),
95 mResourcePostProcessQueue(),
96 mNotificationManager(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 std::vector< ResourcePostProcessRequest> init;
117 mResourcePostProcessQueue = new ResourcePostProcessList(init);
119 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
121 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
122 TextureCache& textureCache = mRenderManager->GetTextureCache();
124 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::DISCARD;
125 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
127 discardPolicy = ResourcePolicy::RETAIN;
129 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
131 mDiscardQueue = new DiscardQueue( renderQueue );
133 mResourceManager = new ResourceManager( mPlatform,
134 *mNotificationManager,
136 *mResourcePostProcessQueue,
141 mTouchResampler = TouchResampler::New();
143 mUpdateManager = new UpdateManager( *mNotificationManager,
146 *mPropertyNotificationManager,
155 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
157 // This must be called after stage is created but before stage initialization
158 mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController() );
160 mStage->Initialize();
162 mResourceClient = new ResourceClient( *mResourceManager, *mStage, dataRetentionPolicy );
164 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
165 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
167 mFontFactory = new FontFactory(*mResourceClient);
168 mImageFactory = new ImageFactory( *mResourceClient );
169 mShaderFactory = new ShaderFactory(*mResourceClient);
170 mShaderFactory->LoadDefaultShaders();
171 mEmojiFactory = new EmojiFactory();
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::Get().Remove();
193 // Stop relayout requests being raised on stage destruction
194 mRelayoutController.Reset();
196 // Clean-up stage - remove default camera and root layer
197 mStage->Uninitialize();
199 // remove (last?) reference to stage
202 delete mEventProcessor;
203 delete mGestureEventProcessor;
204 delete mNotificationManager;
206 delete mImageFactory;
207 delete mShaderFactory;
208 delete mResourceClient;
209 delete mResourceManager;
210 delete mUpdateManager;
211 delete mTouchResampler;
212 delete mEmojiFactory;
213 delete mRenderManager;
214 delete mDiscardQueue;
215 delete mResourcePostProcessQueue;
218 Integration::ContextNotifierInterface* Core::GetContextNotifier()
223 void Core::RecoverFromContextLoss()
225 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
227 mImageFactory->RecoverFromContextLoss(); // Reload images from files
228 mFontFactory->RecoverFromContextLoss(); // Reload glyphs from cache into new atlas
229 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
232 void Core::ContextCreated()
234 mRenderManager->ContextCreated();
237 void Core::ContextDestroyed()
239 mRenderManager->ContextDestroyed();
242 void Core::SurfaceResized(unsigned int width, unsigned int height)
244 mStage->SetSize(width, height);
247 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
249 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
250 mFontFactory->SetDpi( dpiHorizontal, dpiVertical);
251 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
254 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
256 // set the time delta so adaptor can easily print FPS with a release build with 0 as
257 // it is cached by frametime
258 status.secondsFromLastFrame = elapsedSeconds;
260 // Render returns true when there are updates on the stage or one or more animations are completed.
261 // Use the estimated time diff till we render as the elapsed time.
262 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
263 lastVSyncTimeMilliseconds,
264 nextVSyncTimeMilliseconds );
266 // Check the Notification Manager message queue to set needsNotification
267 status.needsNotification = mNotificationManager->MessagesToProcess();
269 // No need to keep update running if there are notifications to process.
270 // Any message to update will wake it up anyways
272 if ( mResourceManager->ResourcesToProcess() )
274 // If we are still processing resources, then we have to continue the update
275 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
279 void Core::Render( RenderStatus& status )
281 bool updateRequired = mRenderManager->Render( status );
283 status.SetNeedsUpdate( updateRequired );
299 // trigger processing of events queued up while paused
303 void Core::SceneCreated()
305 mStage->EmitSceneCreatedSignal();
307 mRelayoutController->OnApplicationSceneCreated();
310 void Core::QueueEvent( const Integration::Event& event )
312 mEventProcessor->QueueEvent( event );
315 void Core::ProcessEvents()
317 // Guard against calls to ProcessEvents() during ProcessEvents()
318 if( mProcessingEvent )
320 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
321 mRenderController.RequestProcessEventsOnIdle();
325 mProcessingEvent = true;
327 // Signal that any messages received will be flushed soon
328 mUpdateManager->EventProcessingStarted();
330 mEventProcessor->ProcessEvents();
332 mNotificationManager->ProcessMessages();
334 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
337 // Emit signal here to start size negotiation and control relayout.
338 mStage->EmitEventProcessingFinishedSignal();
340 // Run the size negotiation after event processing finished signal
341 mRelayoutController->Relayout();
343 // Flush discard queue for image factory
344 mImageFactory->FlushReleaseQueue();
346 // send text requests if required
347 mFontFactory->SendTextRequests();
349 // Flush any queued messages for the update-thread
350 const bool messagesToProcess = mUpdateManager->FlushQueue();
352 // Check if the touch or gestures require updates.
353 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
354 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
356 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
358 // tell the render controller to keep update thread running
359 mRenderController.RequestUpdate();
363 // ProcessEvents() may now be called again
364 mProcessingEvent = false;
367 void Core::UpdateTouchData(const Integration::TouchData& touch)
369 mTouchResampler->SendTouchData( touch );
372 unsigned int Core::GetMaximumUpdateCount() const
374 return MAXIMUM_UPDATE_COUNT;
377 Integration::SystemOverlay& Core::GetSystemOverlay()
379 return mStage->GetSystemOverlay();
382 void Core::SetViewMode( ViewMode viewMode )
384 mStage->SetViewMode( viewMode );
387 ViewMode Core::GetViewMode() const
389 return mStage->GetViewMode();
392 void Core::SetStereoBase( float stereoBase )
394 mStage->SetStereoBase( stereoBase );
397 float Core::GetStereoBase() const
399 return mStage->GetStereoBase();
402 StagePtr Core::GetCurrentStage()
407 PlatformAbstraction& Core::GetPlatform()
412 UpdateManager& Core::GetUpdateManager()
414 return *(mUpdateManager);
417 RenderManager& Core::GetRenderManager()
419 return *(mRenderManager);
422 NotificationManager& Core::GetNotificationManager()
424 return *(mNotificationManager);
427 ResourceManager& Core::GetResourceManager()
429 return *(mResourceManager);
432 ResourceClient& Core::GetResourceClient()
434 return *(mResourceClient);
437 FontFactory& Core::GetFontFactory()
439 return *(mFontFactory);
442 ImageFactory& Core::GetImageFactory()
444 return *(mImageFactory);
447 ShaderFactory& Core::GetShaderFactory()
449 return *(mShaderFactory);
452 GestureEventProcessor& Core::GetGestureEventProcessor()
454 return *(mGestureEventProcessor);
457 EmojiFactory& Core::GetEmojiFactory()
459 return *mEmojiFactory;
462 RelayoutController& Core::GetRelayoutController()
464 return *(mRelayoutController.Get());
467 void Core::CreateThreadLocalStorage()
469 // a pointer to the ThreadLocalStorage object will be stored in TLS
470 // and automatically deleted when the thread is killed
471 new ThreadLocalStorage(this);
474 } // namespace Internal