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/platform-abstraction.h>
25 #include <dali/integration-api/gl-sync-abstraction.h>
26 #include <dali/integration-api/render-controller.h>
27 #include <dali/internal/event/actors/actor-impl.h>
28 #include <dali/internal/event/common/stage-impl.h>
29 #include <dali/internal/event/animation/animation-playlist.h>
30 #include <dali/internal/event/common/property-notification-manager.h>
31 #include <dali/internal/event/common/notification-manager.h>
32 #include <dali/integration-api/events/event.h>
33 #include <dali/internal/event/events/event-processor.h>
34 #include <dali/internal/event/events/gesture-event-processor.h>
35 #include <dali/internal/update/manager/update-manager.h>
36 #include <dali/internal/render/common/performance-monitor.h>
37 #include <dali/internal/render/common/render-manager.h>
38 #include <dali/internal/update/common/discard-queue.h>
39 #include <dali/internal/common/event-to-update.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/modeling/model-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>
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;
59 // The Update for frame N+1 may be processed whilst frame N is being rendered.
60 static const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
68 using Integration::RenderController;
69 using Integration::PlatformAbstraction;
70 using Integration::GlSyncAbstraction;
71 using Integration::GestureManager;
72 using Integration::GlAbstraction;
73 using Integration::Event;
74 using Integration::UpdateStatus;
75 using Integration::RenderStatus;
77 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
78 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
79 GestureManager& gestureManager)
80 : mRenderController( renderController ),
82 mGestureEventProcessor(NULL),
83 mEventProcessor(NULL),
87 mResourcePostProcessQueue(),
88 mNotificationManager(NULL),
95 mProcessingEvent(false)
97 // Create the thread local storage
98 CreateThreadLocalStorage();
100 // This does nothing until Core is built with --enable-performance-monitor
101 PERFORMANCE_MONITOR_INIT( platform );
103 mNotificationManager = new NotificationManager();
105 mAnimationPlaylist = AnimationPlaylist::New();
107 mPropertyNotificationManager = PropertyNotificationManager::New();
109 std::vector< ResourcePostProcessRequest> init;
110 mResourcePostProcessQueue = new ResourcePostProcessList(init);
112 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
114 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
115 TextureCache& textureCache = mRenderManager->GetTextureCache();
116 mDiscardQueue = new DiscardQueue( renderQueue );
118 mResourceManager = new ResourceManager( mPlatform,
119 *mNotificationManager,
121 *mResourcePostProcessQueue,
126 mTouchResampler = TouchResampler::New();
128 mUpdateManager = new UpdateManager( *mNotificationManager,
131 *mPropertyNotificationManager,
140 mResourceClient = new ResourceClient( *mResourceManager, *mUpdateManager );
142 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
144 mStage->Initialize();
146 mUpdateManager->SetRenderTaskList( &mStage->GetRenderTaskList() );
148 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
149 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
151 mFontFactory = new FontFactory(*mResourceClient);
152 mImageFactory = new ImageFactory( *mResourceClient );
153 mModelFactory = new ModelFactory(*mResourceClient);
154 mShaderFactory = new ShaderFactory(*mResourceClient);
155 mShaderFactory->LoadDefaultShaders();
156 mEmojiFactory = new EmojiFactory();
158 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
164 * TODO this should be done by Adaptor, Core does not know about threading
165 * First stop the resource loading thread(s)
167 mPlatform.JoinLoaderThreads();
170 * The order of destructing these singletons is important!!!
173 // clear the thread local storage first
174 // allows core to be created / deleted many times in the same thread (how TET cases work).
175 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
176 ThreadLocalStorage::Get().Remove();
178 // Clean-up stage - remove default camera and root layer
179 mStage->Uninitialize();
181 // remove (last?) reference to stage
184 delete mEventProcessor;
185 delete mGestureEventProcessor;
186 delete mNotificationManager;
188 delete mImageFactory;
189 delete mModelFactory;
190 delete mShaderFactory;
191 delete mResourceClient;
192 delete mResourceManager;
193 delete mUpdateManager;
194 delete mTouchResampler;
195 delete mEmojiFactory;
196 delete mRenderManager;
197 delete mDiscardQueue;
198 delete mResourcePostProcessQueue;
201 void Core::ContextCreated()
203 mRenderManager->ContextCreated();
206 void Core::ContextToBeDestroyed()
208 mRenderManager->ContextDestroyed();
211 void Core::SurfaceResized(unsigned int width, unsigned int height)
213 mStage->SetSize(width, height);
216 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
218 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
219 mFontFactory->SetDpi( dpiHorizontal, dpiVertical);
220 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
223 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
225 // set the time delta so adaptor can easily print FPS with a release build with 0 as
226 // it is cached by frametime
227 status.secondsFromLastFrame = elapsedSeconds;
229 // Render returns true when there are updates on the stage or one or more animations are completed.
230 // Use the estimated time diff till we render as the elapsed time.
231 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
232 lastVSyncTimeMilliseconds,
233 nextVSyncTimeMilliseconds );
235 // Check the Notification Manager message queue to set needsNotification
236 status.needsNotification = mNotificationManager->MessagesToProcess();
238 // If there are notifications to process keep the update thread running as well.
239 // A notification event might add a new actor or animation to the stage which needs
240 // update thread to process it. This also prevents update thread from sleeping
241 // while actor thread is still processing events.
242 if ( status.needsNotification )
244 status.keepUpdating |= Integration::KeepUpdating::NOTIFICATIONS_PENDING;
247 if ( mResourceManager->ResourcesToProcess() )
249 // If we are still processing resources, then we have to continue the update
250 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
254 void Core::Render( RenderStatus& status )
256 bool updateRequired = mRenderManager->Render( status );
258 status.SetNeedsUpdate( updateRequired );
274 // trigger processing of events queued up while paused
278 void Core::QueueEvent( const Integration::Event& event )
280 mEventProcessor->QueueEvent( event );
283 void Core::ProcessEvents()
285 // Guard against calls to ProcessEvents() during ProcessEvents()
286 if( mProcessingEvent )
288 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
289 mRenderController.RequestProcessEventsOnIdle();
293 mProcessingEvent = true;
295 EventToUpdate& eventToUpdate = mUpdateManager->GetEventToUpdate();
297 // Signal that any messages received will be flushed soon
298 eventToUpdate.EventProcessingStarted();
300 mEventProcessor->ProcessEvents();
302 mNotificationManager->ProcessMessages();
304 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
307 // Emit signal here to start size negotiation and control relayout.
308 mStage->EmitEventProcessingFinishedSignal();
310 // Flush discard queue for image factory
311 mImageFactory->FlushReleaseQueue();
313 // send text requests if required
314 mFontFactory->SendTextRequests();
316 // Flush any queued messages for the update-thread
317 const bool messagesToProcess = eventToUpdate.FlushQueue();
319 // Check if the touch or gestures require updates.
320 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
321 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
323 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
325 // tell the render controller to keep update thread running
326 mRenderController.RequestUpdate();
330 // ProcessEvents() may now be called again
331 mProcessingEvent = false;
334 void Core::UpdateTouchData(const Integration::TouchData& touch)
336 mTouchResampler->SendTouchData( touch );
339 unsigned int Core::GetMaximumUpdateCount() const
341 return MAXIMUM_UPDATE_COUNT;
344 Integration::SystemOverlay& Core::GetSystemOverlay()
346 return mStage->GetSystemOverlay();
349 void Core::SetViewMode( ViewMode viewMode )
351 mStage->SetViewMode( viewMode );
354 ViewMode Core::GetViewMode() const
356 return mStage->GetViewMode();
359 void Core::SetStereoBase( float stereoBase )
361 mStage->SetStereoBase( stereoBase );
364 float Core::GetStereoBase() const
366 return mStage->GetStereoBase();
369 StagePtr Core::GetCurrentStage()
374 PlatformAbstraction& Core::GetPlatform()
379 UpdateManager& Core::GetUpdateManager()
381 return *(mUpdateManager);
384 RenderManager& Core::GetRenderManager()
386 return *(mRenderManager);
389 NotificationManager& Core::GetNotificationManager()
391 return *(mNotificationManager);
394 ResourceManager& Core::GetResourceManager()
396 return *(mResourceManager);
399 ResourceClient& Core::GetResourceClient()
401 return *(mResourceClient);
404 FontFactory& Core::GetFontFactory()
406 return *(mFontFactory);
409 ImageFactory& Core::GetImageFactory()
411 return *(mImageFactory);
414 ModelFactory& Core::GetModelFactory()
416 return *(mModelFactory);
419 ShaderFactory& Core::GetShaderFactory()
421 return *(mShaderFactory);
424 GestureEventProcessor& Core::GetGestureEventProcessor()
426 return *(mGestureEventProcessor);
429 EmojiFactory& Core::GetEmojiFactory()
431 return *mEmojiFactory;
434 void Core::CreateThreadLocalStorage()
436 // a pointer to the ThreadLocalStorage object will be stored in TLS
437 // and automatically deleted when the thread is killed
438 new ThreadLocalStorage(this);
441 } // namespace Internal