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/images/image-factory.h>
43 #include <dali/internal/event/modeling/model-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),
100 mProcessingEvent(false)
102 // Create the thread local storage
103 CreateThreadLocalStorage();
105 // This does nothing until Core is built with --enable-performance-monitor
106 PERFORMANCE_MONITOR_INIT( platform );
108 mNotificationManager = new NotificationManager();
110 mAnimationPlaylist = AnimationPlaylist::New();
112 mPropertyNotificationManager = PropertyNotificationManager::New();
114 std::vector< ResourcePostProcessRequest> init;
115 mResourcePostProcessQueue = new ResourcePostProcessList(init);
117 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
119 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
120 TextureCache& textureCache = mRenderManager->GetTextureCache();
122 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::DISCARD;
123 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
125 discardPolicy = ResourcePolicy::RETAIN;
127 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
129 mDiscardQueue = new DiscardQueue( renderQueue );
131 mResourceManager = new ResourceManager( mPlatform,
132 *mNotificationManager,
134 *mResourcePostProcessQueue,
139 mTouchResampler = TouchResampler::New();
141 mUpdateManager = new UpdateManager( *mNotificationManager,
144 *mPropertyNotificationManager,
153 mResourceClient = new ResourceClient( *mResourceManager, *mUpdateManager, dataRetentionPolicy );
155 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
157 mStage->Initialize();
159 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
160 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
162 mImageFactory = new ImageFactory( *mResourceClient );
163 mModelFactory = new ModelFactory(*mResourceClient);
164 mShaderFactory = new ShaderFactory(*mResourceClient);
165 mShaderFactory->LoadDefaultShaders();
167 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
173 * TODO this should be done by Adaptor, Core does not know about threading
174 * First stop the resource loading thread(s)
176 mPlatform.JoinLoaderThreads();
179 * The order of destructing these singletons is important!!!
182 // clear the thread local storage first
183 // allows core to be created / deleted many times in the same thread (how TET cases work).
184 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
185 ThreadLocalStorage::Get().Remove();
187 // Clean-up stage - remove default camera and root layer
188 mStage->Uninitialize();
190 // remove (last?) reference to stage
193 delete mEventProcessor;
194 delete mGestureEventProcessor;
195 delete mNotificationManager;
196 delete mImageFactory;
197 delete mModelFactory;
198 delete mShaderFactory;
199 delete mResourceClient;
200 delete mResourceManager;
201 delete mUpdateManager;
202 delete mTouchResampler;
203 delete mRenderManager;
204 delete mDiscardQueue;
205 delete mResourcePostProcessQueue;
208 Integration::ContextNotifierInterface* Core::GetContextNotifier()
213 void Core::RecoverFromContextLoss()
215 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
217 mImageFactory->RecoverFromContextLoss(); // Reload images from files
218 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
221 void Core::ContextCreated()
223 mRenderManager->ContextCreated();
226 void Core::ContextDestroyed()
228 mRenderManager->ContextDestroyed();
231 void Core::SurfaceResized(unsigned int width, unsigned int height)
233 mStage->SetSize(width, height);
236 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
238 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
239 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
242 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
244 // set the time delta so adaptor can easily print FPS with a release build with 0 as
245 // it is cached by frametime
246 status.secondsFromLastFrame = elapsedSeconds;
248 // Render returns true when there are updates on the stage or one or more animations are completed.
249 // Use the estimated time diff till we render as the elapsed time.
250 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
251 lastVSyncTimeMilliseconds,
252 nextVSyncTimeMilliseconds );
254 // Check the Notification Manager message queue to set needsNotification
255 status.needsNotification = mNotificationManager->MessagesToProcess();
257 // No need to keep update running if there are notifications to process.
258 // Any message to update will wake it up anyways
260 if ( mResourceManager->ResourcesToProcess() )
262 // If we are still processing resources, then we have to continue the update
263 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
267 void Core::Render( RenderStatus& status )
269 bool updateRequired = mRenderManager->Render( status );
271 status.SetNeedsUpdate( updateRequired );
287 // trigger processing of events queued up while paused
291 void Core::SceneCreated()
293 mStage->EmitSceneCreatedSignal();
296 void Core::QueueEvent( const Integration::Event& event )
298 mEventProcessor->QueueEvent( event );
301 void Core::ProcessEvents()
303 // Guard against calls to ProcessEvents() during ProcessEvents()
304 if( mProcessingEvent )
306 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
307 mRenderController.RequestProcessEventsOnIdle();
311 mProcessingEvent = true;
313 EventToUpdate& eventToUpdate = mUpdateManager->GetEventToUpdate();
315 // Signal that any messages received will be flushed soon
316 eventToUpdate.EventProcessingStarted();
318 mEventProcessor->ProcessEvents();
320 mNotificationManager->ProcessMessages();
322 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
325 // Emit signal here to start size negotiation and control relayout.
326 mStage->EmitEventProcessingFinishedSignal();
328 // Flush discard queue for image factory
329 mImageFactory->FlushReleaseQueue();
331 // Flush any queued messages for the update-thread
332 const bool messagesToProcess = eventToUpdate.FlushQueue();
334 // Check if the touch or gestures require updates.
335 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
336 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
338 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
340 // tell the render controller to keep update thread running
341 mRenderController.RequestUpdate();
345 // ProcessEvents() may now be called again
346 mProcessingEvent = false;
349 void Core::UpdateTouchData(const Integration::TouchData& touch)
351 mTouchResampler->SendTouchData( touch );
354 unsigned int Core::GetMaximumUpdateCount() const
356 return MAXIMUM_UPDATE_COUNT;
359 Integration::SystemOverlay& Core::GetSystemOverlay()
361 return mStage->GetSystemOverlay();
364 void Core::SetViewMode( ViewMode viewMode )
366 mStage->SetViewMode( viewMode );
369 ViewMode Core::GetViewMode() const
371 return mStage->GetViewMode();
374 void Core::SetStereoBase( float stereoBase )
376 mStage->SetStereoBase( stereoBase );
379 float Core::GetStereoBase() const
381 return mStage->GetStereoBase();
384 StagePtr Core::GetCurrentStage()
389 PlatformAbstraction& Core::GetPlatform()
394 UpdateManager& Core::GetUpdateManager()
396 return *(mUpdateManager);
399 RenderManager& Core::GetRenderManager()
401 return *(mRenderManager);
404 NotificationManager& Core::GetNotificationManager()
406 return *(mNotificationManager);
409 ResourceManager& Core::GetResourceManager()
411 return *(mResourceManager);
414 ResourceClient& Core::GetResourceClient()
416 return *(mResourceClient);
419 ImageFactory& Core::GetImageFactory()
421 return *(mImageFactory);
424 ModelFactory& Core::GetModelFactory()
426 return *(mModelFactory);
429 ShaderFactory& Core::GetShaderFactory()
431 return *(mShaderFactory);
434 GestureEventProcessor& Core::GetGestureEventProcessor()
436 return *(mGestureEventProcessor);
439 void Core::CreateThreadLocalStorage()
441 // a pointer to the ThreadLocalStorage object will be stored in TLS
442 // and automatically deleted when the thread is killed
443 new ThreadLocalStorage(this);
446 } // namespace Internal