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/common/event-to-update.h>
41 #include <dali/internal/update/resources/resource-manager.h>
42 #include <dali/internal/event/text/font-factory.h>
43 #include <dali/internal/event/images/image-factory.h>
44 #include <dali/internal/event/images/emoji-factory.h>
45 #include <dali/internal/event/common/thread-local-storage.h>
46 #include <dali/internal/event/effects/shader-factory.h>
47 #include <dali/internal/update/touch/touch-resampler.h>
48 #include <dali/internal/event/common/type-registry-impl.h>
49 #include <dali/internal/event/render-tasks/render-task-list-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 mResourceClient = new ResourceClient( *mResourceManager, *mUpdateManager, dataRetentionPolicy );
157 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
159 mStage->Initialize();
161 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
162 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
164 mFontFactory = new FontFactory(*mResourceClient);
165 mImageFactory = new ImageFactory( *mResourceClient );
166 mShaderFactory = new ShaderFactory(*mResourceClient);
167 mShaderFactory->LoadDefaultShaders();
168 mEmojiFactory = new EmojiFactory();
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::Get().Remove();
190 // Clean-up stage - remove default camera and root layer
191 mStage->Uninitialize();
193 // remove (last?) reference to stage
196 delete mEventProcessor;
197 delete mGestureEventProcessor;
198 delete mNotificationManager;
200 delete mImageFactory;
201 delete mShaderFactory;
202 delete mResourceClient;
203 delete mResourceManager;
204 delete mUpdateManager;
205 delete mTouchResampler;
206 delete mEmojiFactory;
207 delete mRenderManager;
208 delete mDiscardQueue;
209 delete mResourcePostProcessQueue;
212 Integration::ContextNotifierInterface* Core::GetContextNotifier()
217 void Core::RecoverFromContextLoss()
219 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
221 mImageFactory->RecoverFromContextLoss(); // Reload images from files
222 mFontFactory->RecoverFromContextLoss(); // Reload glyphs from cache into new atlas
223 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
226 void Core::ContextCreated()
228 mRenderManager->ContextCreated();
231 void Core::ContextDestroyed()
233 mRenderManager->ContextDestroyed();
236 void Core::SurfaceResized(unsigned int width, unsigned int height)
238 mStage->SetSize(width, height);
241 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
243 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
244 mFontFactory->SetDpi( dpiHorizontal, dpiVertical);
245 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
248 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
250 // set the time delta so adaptor can easily print FPS with a release build with 0 as
251 // it is cached by frametime
252 status.secondsFromLastFrame = elapsedSeconds;
254 // Render returns true when there are updates on the stage or one or more animations are completed.
255 // Use the estimated time diff till we render as the elapsed time.
256 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
257 lastVSyncTimeMilliseconds,
258 nextVSyncTimeMilliseconds );
260 // Check the Notification Manager message queue to set needsNotification
261 status.needsNotification = mNotificationManager->MessagesToProcess();
263 // No need to keep update running if there are notifications to process.
264 // Any message to update will wake it up anyways
266 if ( mResourceManager->ResourcesToProcess() )
268 // If we are still processing resources, then we have to continue the update
269 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
273 void Core::Render( RenderStatus& status )
275 bool updateRequired = mRenderManager->Render( status );
277 status.SetNeedsUpdate( updateRequired );
293 // trigger processing of events queued up while paused
297 void Core::SceneCreated()
299 mStage->EmitSceneCreatedSignal();
302 void Core::QueueEvent( const Integration::Event& event )
304 mEventProcessor->QueueEvent( event );
307 void Core::ProcessEvents()
309 // Guard against calls to ProcessEvents() during ProcessEvents()
310 if( mProcessingEvent )
312 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
313 mRenderController.RequestProcessEventsOnIdle();
317 mProcessingEvent = true;
319 EventToUpdate& eventToUpdate = mUpdateManager->GetEventToUpdate();
321 // Signal that any messages received will be flushed soon
322 eventToUpdate.EventProcessingStarted();
324 mEventProcessor->ProcessEvents();
326 mNotificationManager->ProcessMessages();
328 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
331 // Emit signal here to start size negotiation and control relayout.
332 mStage->EmitEventProcessingFinishedSignal();
334 // Flush discard queue for image factory
335 mImageFactory->FlushReleaseQueue();
337 // send text requests if required
338 mFontFactory->SendTextRequests();
340 // Flush any queued messages for the update-thread
341 const bool messagesToProcess = eventToUpdate.FlushQueue();
343 // Check if the touch or gestures require updates.
344 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
345 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
347 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
349 // tell the render controller to keep update thread running
350 mRenderController.RequestUpdate();
354 // ProcessEvents() may now be called again
355 mProcessingEvent = false;
358 void Core::UpdateTouchData(const Integration::TouchData& touch)
360 mTouchResampler->SendTouchData( touch );
363 unsigned int Core::GetMaximumUpdateCount() const
365 return MAXIMUM_UPDATE_COUNT;
368 Integration::SystemOverlay& Core::GetSystemOverlay()
370 return mStage->GetSystemOverlay();
373 void Core::SetViewMode( ViewMode viewMode )
375 mStage->SetViewMode( viewMode );
378 ViewMode Core::GetViewMode() const
380 return mStage->GetViewMode();
383 void Core::SetStereoBase( float stereoBase )
385 mStage->SetStereoBase( stereoBase );
388 float Core::GetStereoBase() const
390 return mStage->GetStereoBase();
393 StagePtr Core::GetCurrentStage()
398 PlatformAbstraction& Core::GetPlatform()
403 UpdateManager& Core::GetUpdateManager()
405 return *(mUpdateManager);
408 RenderManager& Core::GetRenderManager()
410 return *(mRenderManager);
413 NotificationManager& Core::GetNotificationManager()
415 return *(mNotificationManager);
418 ResourceManager& Core::GetResourceManager()
420 return *(mResourceManager);
423 ResourceClient& Core::GetResourceClient()
425 return *(mResourceClient);
428 FontFactory& Core::GetFontFactory()
430 return *(mFontFactory);
433 ImageFactory& Core::GetImageFactory()
435 return *(mImageFactory);
438 ShaderFactory& Core::GetShaderFactory()
440 return *(mShaderFactory);
443 GestureEventProcessor& Core::GetGestureEventProcessor()
445 return *(mGestureEventProcessor);
448 EmojiFactory& Core::GetEmojiFactory()
450 return *mEmojiFactory;
453 void Core::CreateThreadLocalStorage()
455 // a pointer to the ThreadLocalStorage object will be stored in TLS
456 // and automatically deleted when the thread is killed
457 new ThreadLocalStorage(this);
460 } // namespace Internal