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>
50 #include <dali/internal/render/gl-resources/texture-cache.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;
57 using Dali::Internal::SceneGraph::TextureCache;
61 // The Update for frame N+1 may be processed whilst frame N is being rendered.
62 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
75 using Integration::RenderController;
76 using Integration::PlatformAbstraction;
77 using Integration::GlSyncAbstraction;
78 using Integration::GestureManager;
79 using Integration::GlAbstraction;
80 using Integration::Event;
81 using Integration::UpdateStatus;
82 using Integration::RenderStatus;
84 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
85 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
86 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
87 : mRenderController( renderController ),
89 mGestureEventProcessor(NULL),
90 mEventProcessor(NULL),
94 mResourcePostProcessQueue(),
95 mNotificationManager(NULL),
101 mProcessingEvent(false)
103 // Create the thread local storage
104 CreateThreadLocalStorage();
106 // This does nothing until Core is built with --enable-performance-monitor
107 PERFORMANCE_MONITOR_INIT( platform );
109 mNotificationManager = new NotificationManager();
111 mAnimationPlaylist = AnimationPlaylist::New();
113 mPropertyNotificationManager = PropertyNotificationManager::New();
115 std::vector< ResourcePostProcessRequest> init;
116 mResourcePostProcessQueue = new ResourcePostProcessList(init);
118 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
120 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
121 TextureCache& textureCache = mRenderManager->GetTextureCache();
123 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::DISCARD;
124 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
126 discardPolicy = ResourcePolicy::RETAIN;
128 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
130 mDiscardQueue = new DiscardQueue( renderQueue );
132 mResourceManager = new ResourceManager( mPlatform,
133 *mNotificationManager,
135 *mResourcePostProcessQueue,
140 mTouchResampler = TouchResampler::New();
142 mUpdateManager = new UpdateManager( *mNotificationManager,
145 *mPropertyNotificationManager,
154 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
155 mStage->Initialize();
157 mResourceClient = new ResourceClient( *mResourceManager, *mStage, dataRetentionPolicy );
159 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
160 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
162 mFontFactory = new FontFactory(*mResourceClient);
163 mImageFactory = new ImageFactory( *mResourceClient );
164 mShaderFactory = new ShaderFactory(*mResourceClient);
165 mShaderFactory->LoadDefaultShaders();
166 mEmojiFactory = new EmojiFactory();
168 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
174 * TODO this should be done by Adaptor, Core does not know about threading
175 * First stop the resource loading thread(s)
177 mPlatform.JoinLoaderThreads();
180 * The order of destructing these singletons is important!!!
183 // clear the thread local storage first
184 // allows core to be created / deleted many times in the same thread (how TET cases work).
185 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
186 ThreadLocalStorage::Get().Remove();
188 // Clean-up stage - remove default camera and root layer
189 mStage->Uninitialize();
191 // remove (last?) reference to stage
194 delete mEventProcessor;
195 delete mGestureEventProcessor;
196 delete mNotificationManager;
198 delete mImageFactory;
199 delete mShaderFactory;
200 delete mResourceClient;
201 delete mResourceManager;
202 delete mUpdateManager;
203 delete mTouchResampler;
204 delete mEmojiFactory;
205 delete mRenderManager;
206 delete mDiscardQueue;
207 delete mResourcePostProcessQueue;
210 Integration::ContextNotifierInterface* Core::GetContextNotifier()
215 void Core::RecoverFromContextLoss()
217 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
219 mImageFactory->RecoverFromContextLoss(); // Reload images from files
220 mFontFactory->RecoverFromContextLoss(); // Reload glyphs from cache into new atlas
221 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
224 void Core::ContextCreated()
226 mRenderManager->ContextCreated();
229 void Core::ContextDestroyed()
231 mRenderManager->ContextDestroyed();
234 void Core::SurfaceResized(unsigned int width, unsigned int height)
236 mStage->SetSize(width, height);
239 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
241 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
242 mFontFactory->SetDpi( dpiHorizontal, dpiVertical);
243 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
246 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
248 // set the time delta so adaptor can easily print FPS with a release build with 0 as
249 // it is cached by frametime
250 status.secondsFromLastFrame = elapsedSeconds;
252 // Render returns true when there are updates on the stage or one or more animations are completed.
253 // Use the estimated time diff till we render as the elapsed time.
254 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
255 lastVSyncTimeMilliseconds,
256 nextVSyncTimeMilliseconds );
258 // Check the Notification Manager message queue to set needsNotification
259 status.needsNotification = mNotificationManager->MessagesToProcess();
261 // No need to keep update running if there are notifications to process.
262 // Any message to update will wake it up anyways
264 if ( mResourceManager->ResourcesToProcess() )
266 // If we are still processing resources, then we have to continue the update
267 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
271 void Core::Render( RenderStatus& status )
273 bool updateRequired = mRenderManager->Render( status );
275 status.SetNeedsUpdate( updateRequired );
291 // trigger processing of events queued up while paused
295 void Core::SceneCreated()
297 mStage->EmitSceneCreatedSignal();
300 void Core::QueueEvent( const Integration::Event& event )
302 mEventProcessor->QueueEvent( event );
305 void Core::ProcessEvents()
307 // Guard against calls to ProcessEvents() during ProcessEvents()
308 if( mProcessingEvent )
310 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
311 mRenderController.RequestProcessEventsOnIdle();
315 mProcessingEvent = true;
317 // Signal that any messages received will be flushed soon
318 mUpdateManager->EventProcessingStarted();
320 mEventProcessor->ProcessEvents();
322 mNotificationManager->ProcessMessages();
324 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
327 // Emit signal here to start size negotiation and control relayout.
328 mStage->EmitEventProcessingFinishedSignal();
330 // Flush discard queue for image factory
331 mImageFactory->FlushReleaseQueue();
333 // send text requests if required
334 mFontFactory->SendTextRequests();
336 // Flush any queued messages for the update-thread
337 const bool messagesToProcess = mUpdateManager->FlushQueue();
339 // Check if the touch or gestures require updates.
340 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
341 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
343 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
345 // tell the render controller to keep update thread running
346 mRenderController.RequestUpdate();
350 // ProcessEvents() may now be called again
351 mProcessingEvent = false;
354 void Core::UpdateTouchData(const Integration::TouchData& touch)
356 mTouchResampler->SendTouchData( touch );
359 unsigned int Core::GetMaximumUpdateCount() const
361 return MAXIMUM_UPDATE_COUNT;
364 Integration::SystemOverlay& Core::GetSystemOverlay()
366 return mStage->GetSystemOverlay();
369 void Core::SetViewMode( ViewMode viewMode )
371 mStage->SetViewMode( viewMode );
374 ViewMode Core::GetViewMode() const
376 return mStage->GetViewMode();
379 void Core::SetStereoBase( float stereoBase )
381 mStage->SetStereoBase( stereoBase );
384 float Core::GetStereoBase() const
386 return mStage->GetStereoBase();
389 StagePtr Core::GetCurrentStage()
394 PlatformAbstraction& Core::GetPlatform()
399 UpdateManager& Core::GetUpdateManager()
401 return *(mUpdateManager);
404 RenderManager& Core::GetRenderManager()
406 return *(mRenderManager);
409 NotificationManager& Core::GetNotificationManager()
411 return *(mNotificationManager);
414 ResourceManager& Core::GetResourceManager()
416 return *(mResourceManager);
419 ResourceClient& Core::GetResourceClient()
421 return *(mResourceClient);
424 FontFactory& Core::GetFontFactory()
426 return *(mFontFactory);
429 ImageFactory& Core::GetImageFactory()
431 return *(mImageFactory);
434 ShaderFactory& Core::GetShaderFactory()
436 return *(mShaderFactory);
439 GestureEventProcessor& Core::GetGestureEventProcessor()
441 return *(mGestureEventProcessor);
444 EmojiFactory& Core::GetEmojiFactory()
446 return *mEmojiFactory;
449 void Core::CreateThreadLocalStorage()
451 // a pointer to the ThreadLocalStorage object will be stored in TLS
452 // and automatically deleted when the thread is killed
453 new ThreadLocalStorage(this);
456 } // namespace Internal