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/common/thread-local-storage.h>
44 #include <dali/internal/event/effects/shader-factory.h>
45 #include <dali/internal/update/touch/touch-resampler.h>
46 #include <dali/internal/event/common/type-registry-impl.h>
47 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
49 #include <dali/internal/render/gl-resources/texture-cache.h>
50 #include <dali/internal/render/gl-resources/context.h>
52 using Dali::Internal::SceneGraph::UpdateManager;
53 using Dali::Internal::SceneGraph::RenderManager;
54 using Dali::Internal::SceneGraph::DiscardQueue;
55 using Dali::Internal::SceneGraph::RenderQueue;
56 using Dali::Internal::SceneGraph::TextureCache;
60 // The Update for frame N+1 may be processed whilst frame N is being rendered.
61 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
74 using Integration::RenderController;
75 using Integration::PlatformAbstraction;
76 using Integration::GlSyncAbstraction;
77 using Integration::GestureManager;
78 using Integration::GlAbstraction;
79 using Integration::Event;
80 using Integration::UpdateStatus;
81 using Integration::RenderStatus;
83 Core::Core( RenderController& renderController, PlatformAbstraction& platform,
84 GlAbstraction& glAbstraction, GlSyncAbstraction& glSyncAbstraction,
85 GestureManager& gestureManager, ResourcePolicy::DataRetention dataRetentionPolicy)
86 : mRenderController( renderController ),
88 mGestureEventProcessor(NULL),
89 mEventProcessor(NULL),
93 mResourcePostProcessQueue(),
94 mNotificationManager(NULL),
98 mProcessingEvent(false)
100 // Create the thread local storage
101 CreateThreadLocalStorage();
103 // This does nothing until Core is built with --enable-performance-monitor
104 PERFORMANCE_MONITOR_INIT( platform );
106 mNotificationManager = new NotificationManager();
108 mAnimationPlaylist = AnimationPlaylist::New();
110 mPropertyNotificationManager = PropertyNotificationManager::New();
112 std::vector< ResourcePostProcessRequest> init;
113 mResourcePostProcessQueue = new ResourcePostProcessList(init);
115 mRenderManager = RenderManager::New( glAbstraction, *mResourcePostProcessQueue );
117 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
118 TextureCache& textureCache = mRenderManager->GetTextureCache();
120 ResourcePolicy::Discardable discardPolicy = ResourcePolicy::DISCARD;
121 if( dataRetentionPolicy == ResourcePolicy::DALI_RETAINS_ALL_DATA )
123 discardPolicy = ResourcePolicy::RETAIN;
125 textureCache.SetDiscardBitmapsPolicy(discardPolicy);
127 mDiscardQueue = new DiscardQueue( renderQueue );
129 mResourceManager = new ResourceManager( mPlatform,
130 *mNotificationManager,
132 *mResourcePostProcessQueue,
137 mTouchResampler = TouchResampler::New();
139 mUpdateManager = new UpdateManager( *mNotificationManager,
142 *mPropertyNotificationManager,
151 mResourceClient = new ResourceClient( *mResourceManager, *mUpdateManager, dataRetentionPolicy );
153 mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager ) );
155 mStage->Initialize();
157 mGestureEventProcessor = new GestureEventProcessor(*mStage, gestureManager, mRenderController);
158 mEventProcessor = new EventProcessor(*mStage, *mNotificationManager, *mGestureEventProcessor);
160 mImageFactory = new ImageFactory( *mResourceClient );
161 mShaderFactory = new ShaderFactory(*mResourceClient);
162 mShaderFactory->LoadDefaultShaders();
164 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
170 * TODO this should be done by Adaptor, Core does not know about threading
171 * First stop the resource loading thread(s)
173 mPlatform.JoinLoaderThreads();
176 * The order of destructing these singletons is important!!!
179 // clear the thread local storage first
180 // allows core to be created / deleted many times in the same thread (how TET cases work).
181 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
182 ThreadLocalStorage::Get().Remove();
184 // Clean-up stage - remove default camera and root layer
185 mStage->Uninitialize();
187 // remove (last?) reference to stage
190 delete mEventProcessor;
191 delete mGestureEventProcessor;
192 delete mNotificationManager;
193 delete mImageFactory;
194 delete mShaderFactory;
195 delete mResourceClient;
196 delete mResourceManager;
197 delete mUpdateManager;
198 delete mTouchResampler;
199 delete mRenderManager;
200 delete mDiscardQueue;
201 delete mResourcePostProcessQueue;
204 Integration::ContextNotifierInterface* Core::GetContextNotifier()
209 void Core::RecoverFromContextLoss()
211 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
213 mImageFactory->RecoverFromContextLoss(); // Reload images from files
214 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
217 void Core::ContextCreated()
219 mRenderManager->ContextCreated();
222 void Core::ContextDestroyed()
224 mRenderManager->ContextDestroyed();
227 void Core::SurfaceResized(unsigned int width, unsigned int height)
229 mStage->SetSize(width, height);
232 void Core::SetDpi(unsigned int dpiHorizontal, unsigned int dpiVertical)
234 mPlatform.SetDpi( dpiHorizontal, dpiVertical );
235 mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
238 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status )
240 // set the time delta so adaptor can easily print FPS with a release build with 0 as
241 // it is cached by frametime
242 status.secondsFromLastFrame = elapsedSeconds;
244 // Render returns true when there are updates on the stage or one or more animations are completed.
245 // Use the estimated time diff till we render as the elapsed time.
246 status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
247 lastVSyncTimeMilliseconds,
248 nextVSyncTimeMilliseconds );
250 // Check the Notification Manager message queue to set needsNotification
251 status.needsNotification = mNotificationManager->MessagesToProcess();
253 // No need to keep update running if there are notifications to process.
254 // Any message to update will wake it up anyways
256 if ( mResourceManager->ResourcesToProcess() )
258 // If we are still processing resources, then we have to continue the update
259 status.keepUpdating |= Integration::KeepUpdating::LOADING_RESOURCES;
263 void Core::Render( RenderStatus& status )
265 bool updateRequired = mRenderManager->Render( status );
267 status.SetNeedsUpdate( updateRequired );
283 // trigger processing of events queued up while paused
287 void Core::SceneCreated()
289 mStage->EmitSceneCreatedSignal();
292 void Core::QueueEvent( const Integration::Event& event )
294 mEventProcessor->QueueEvent( event );
297 void Core::ProcessEvents()
299 // Guard against calls to ProcessEvents() during ProcessEvents()
300 if( mProcessingEvent )
302 DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!" );
303 mRenderController.RequestProcessEventsOnIdle();
307 mProcessingEvent = true;
309 EventToUpdate& eventToUpdate = mUpdateManager->GetEventToUpdate();
311 // Signal that any messages received will be flushed soon
312 eventToUpdate.EventProcessingStarted();
314 mEventProcessor->ProcessEvents();
316 mNotificationManager->ProcessMessages();
318 // Avoid allocating MessageBuffers, triggering size-negotiation or sending any other spam whilst paused
321 // Emit signal here to start size negotiation and control relayout.
322 mStage->EmitEventProcessingFinishedSignal();
324 // Flush discard queue for image factory
325 mImageFactory->FlushReleaseQueue();
327 // Flush any queued messages for the update-thread
328 const bool messagesToProcess = eventToUpdate.FlushQueue();
330 // Check if the touch or gestures require updates.
331 const bool touchNeedsUpdate = mTouchResampler->NeedsUpdate();
332 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
334 if( messagesToProcess || touchNeedsUpdate || gestureNeedsUpdate )
336 // tell the render controller to keep update thread running
337 mRenderController.RequestUpdate();
341 // ProcessEvents() may now be called again
342 mProcessingEvent = false;
345 void Core::UpdateTouchData(const Integration::TouchData& touch)
347 mTouchResampler->SendTouchData( touch );
350 unsigned int Core::GetMaximumUpdateCount() const
352 return MAXIMUM_UPDATE_COUNT;
355 Integration::SystemOverlay& Core::GetSystemOverlay()
357 return mStage->GetSystemOverlay();
360 void Core::SetViewMode( ViewMode viewMode )
362 mStage->SetViewMode( viewMode );
365 ViewMode Core::GetViewMode() const
367 return mStage->GetViewMode();
370 void Core::SetStereoBase( float stereoBase )
372 mStage->SetStereoBase( stereoBase );
375 float Core::GetStereoBase() const
377 return mStage->GetStereoBase();
380 StagePtr Core::GetCurrentStage()
385 PlatformAbstraction& Core::GetPlatform()
390 UpdateManager& Core::GetUpdateManager()
392 return *(mUpdateManager);
395 RenderManager& Core::GetRenderManager()
397 return *(mRenderManager);
400 NotificationManager& Core::GetNotificationManager()
402 return *(mNotificationManager);
405 ResourceManager& Core::GetResourceManager()
407 return *(mResourceManager);
410 ResourceClient& Core::GetResourceClient()
412 return *(mResourceClient);
415 ImageFactory& Core::GetImageFactory()
417 return *(mImageFactory);
420 ShaderFactory& Core::GetShaderFactory()
422 return *(mShaderFactory);
425 GestureEventProcessor& Core::GetGestureEventProcessor()
427 return *(mGestureEventProcessor);
430 void Core::CreateThreadLocalStorage()
432 // a pointer to the ThreadLocalStorage object will be stored in TLS
433 // and automatically deleted when the thread is killed
434 new ThreadLocalStorage(this);
437 } // namespace Internal