2 * Copyright (c) 2021 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/graphics-api/graphics-controller.h>
23 #include <dali/integration-api/core.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/events/event.h>
26 #include <dali/integration-api/gl-context-helper-abstraction.h>
27 #include <dali/integration-api/gl-sync-abstraction.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/integration-api/processor-interface.h>
30 #include <dali/integration-api/render-controller.h>
32 #include <dali/internal/event/actors/actor-impl.h>
33 #include <dali/internal/event/animation/animation-playlist.h>
34 #include <dali/internal/event/common/event-thread-services.h>
35 #include <dali/internal/event/common/notification-manager.h>
36 #include <dali/internal/event/common/property-notification-manager.h>
37 #include <dali/internal/event/common/stage-impl.h>
38 #include <dali/internal/event/common/thread-local-storage.h>
39 #include <dali/internal/event/common/type-registry-impl.h>
40 #include <dali/internal/event/effects/shader-factory.h>
41 #include <dali/internal/event/events/event-processor.h>
42 #include <dali/internal/event/events/gesture-event-processor.h>
43 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
44 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
46 #include <dali/internal/update/common/discard-queue.h>
47 #include <dali/internal/update/manager/render-task-processor.h>
48 #include <dali/internal/update/manager/update-manager.h>
50 #include <dali/internal/render/common/performance-monitor.h>
51 #include <dali/internal/render/common/render-manager.h>
53 using Dali::Internal::SceneGraph::DiscardQueue;
54 using Dali::Internal::SceneGraph::RenderManager;
55 using Dali::Internal::SceneGraph::RenderQueue;
56 using Dali::Internal::SceneGraph::UpdateManager;
60 // The Update for frame N+1 may be processed whilst frame N is being rendered.
61 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
72 using Integration::Event;
73 using Integration::GlAbstraction;
74 using Integration::GlContextHelperAbstraction;
75 using Integration::GlSyncAbstraction;
76 using Integration::PlatformAbstraction;
77 using Integration::RenderController;
78 using Integration::RenderStatus;
79 using Integration::UpdateStatus;
81 Core::Core(RenderController& renderController,
82 PlatformAbstraction& platform,
83 Graphics::Controller& graphicsController,
84 Integration::RenderToFrameBuffer renderToFboEnabled,
85 Integration::DepthBufferAvailable depthBufferAvailable,
86 Integration::StencilBufferAvailable stencilBufferAvailable,
87 Integration::PartialUpdateAvailable partialUpdateAvailable)
88 : mRenderController(renderController),
90 mGraphicsController(graphicsController),
91 mProcessingEvent(false),
92 mForceNextUpdate(false)
94 // Create the thread local storage
95 CreateThreadLocalStorage();
97 // This does nothing until Core is built with --enable-performance-monitor
98 PERFORMANCE_MONITOR_INIT(platform);
100 mNotificationManager = new NotificationManager();
102 mAnimationPlaylist = AnimationPlaylist::New();
104 mPropertyNotificationManager = PropertyNotificationManager::New();
106 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
108 mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
110 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
112 mDiscardQueue = new DiscardQueue(renderQueue);
114 mUpdateManager = new UpdateManager(*mNotificationManager,
116 *mPropertyNotificationManager,
121 *mRenderTaskProcessor);
123 mRenderManager->SetShaderSaver(*mUpdateManager);
125 mObjectRegistry = ObjectRegistry::New();
127 mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
129 // This must be called after stage is created but before stage initialization
130 mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
132 mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
134 mShaderFactory = new ShaderFactory();
135 mUpdateManager->SetShaderSaver(*mShaderFactory);
137 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
143 * The order of destructing these singletons is important!!!
146 // clear the thread local storage first
147 // allows core to be created / deleted many times in the same thread (how TET cases work).
148 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
149 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
156 mObjectRegistry.Reset();
158 // Stop relayout requests being raised on stage destruction
159 mRelayoutController.Reset();
161 // remove (last?) reference to stage
165 void Core::Initialize()
167 mStage->Initialize(*mScenes[0]);
170 Integration::ContextNotifierInterface* Core::GetContextNotifier()
175 void Core::RecoverFromContextLoss()
177 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
179 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
182 void Core::ContextCreated()
186 void Core::ContextDestroyed()
190 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo)
192 // set the time delta so adaptor can easily print FPS with a release build with 0 as
193 // it is cached by frametime
194 status.secondsFromLastFrame = elapsedSeconds;
196 // Render returns true when there are updates on the stage or one or more animations are completed.
197 // Use the estimated time diff till we render as the elapsed time.
198 status.keepUpdating = mUpdateManager->Update(elapsedSeconds,
199 lastVSyncTimeMilliseconds,
200 nextVSyncTimeMilliseconds,
204 // Check the Notification Manager message queue to set needsNotification
205 status.needsNotification = mNotificationManager->MessagesToProcess();
207 // No need to keep update running if there are notifications to process.
208 // Any message to update will wake it up anyways
211 void Core::PreRender(RenderStatus& status, bool forceClear, bool uploadOnly)
213 mRenderManager->PreRender(status, forceClear, uploadOnly);
216 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
218 mRenderManager->PreRender(scene, damagedRects);
221 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
223 mRenderManager->RenderScene(status, scene, renderToFbo);
226 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
228 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
231 void Core::PostRender(bool uploadOnly)
233 mRenderManager->PostRender(uploadOnly);
236 void Core::SceneCreated()
238 mStage->EmitSceneCreatedSignal();
240 mRelayoutController->OnApplicationSceneCreated();
242 for(const auto& scene : mScenes)
244 Dali::Actor sceneRootLayer = scene->GetRootLayer();
245 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
249 void Core::QueueEvent(const Integration::Event& event)
251 if(mScenes.size() != 0)
253 mScenes.front()->QueueEvent(event);
257 void Core::ProcessEvents()
259 // Guard against calls to ProcessEvents() during ProcessEvents()
262 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
263 mRenderController.RequestProcessEventsOnIdle(false);
267 mProcessingEvent = true;
268 mRelayoutController->SetProcessingCoreEvents(true);
270 // Signal that any messages received will be flushed soon
271 mUpdateManager->EventProcessingStarted();
273 // Scene could be added or removed while processing the events
274 // Copy the Scene container locally to avoid possibly invalid iterator
275 SceneContainer scenes = mScenes;
277 // process events in all scenes
278 for(auto scene : scenes)
280 scene->ProcessEvents();
283 mNotificationManager->ProcessMessages();
285 // Emit signal here to inform listeners that event processing has finished.
286 for(auto scene : scenes)
288 scene->EmitEventProcessingFinishedSignal();
291 // Run any registered processors
294 // Run the size negotiation after event processing finished signal
295 mRelayoutController->Relayout();
297 // Run any registered post processors
300 // Rebuild depth tree after event processing has finished
301 for(auto scene : scenes)
303 scene->RebuildDepthTree();
306 // Flush any queued messages for the update-thread
307 const bool messagesToProcess = mUpdateManager->FlushQueue();
309 // Check if the touch or gestures require updates.
310 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
311 // Check if the next update is forced.
312 const bool forceUpdate = IsNextUpdateForced();
314 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
316 // tell the render controller to keep update thread running
317 mRenderController.RequestUpdate(forceUpdate);
320 mRelayoutController->SetProcessingCoreEvents(false);
322 // ProcessEvents() may now be called again
323 mProcessingEvent = false;
326 uint32_t Core::GetMaximumUpdateCount() const
328 return MAXIMUM_UPDATE_COUNT;
331 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
335 mPostProcessors.PushBack(&processor);
339 mProcessors.PushBack(&processor);
343 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
347 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
348 if(iter != mPostProcessors.End())
350 mPostProcessors.Erase(iter);
355 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
356 if(iter != mProcessors.End())
358 mProcessors.Erase(iter);
363 void Core::RunProcessors()
365 // Copy processor pointers to prevent changes to vector affecting loop iterator.
366 Dali::Vector<Integration::Processor*> processors(mProcessors);
368 for(auto processor : processors)
372 processor->Process(false);
377 void Core::RunPostProcessors()
379 // Copy processor pointers to prevent changes to vector affecting loop iterator.
380 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
382 for(auto processor : processors)
386 processor->Process(true);
391 StagePtr Core::GetCurrentStage()
396 PlatformAbstraction& Core::GetPlatform()
401 UpdateManager& Core::GetUpdateManager()
403 return *(mUpdateManager);
406 RenderManager& Core::GetRenderManager()
408 return *(mRenderManager);
411 NotificationManager& Core::GetNotificationManager()
413 return *(mNotificationManager);
416 ShaderFactory& Core::GetShaderFactory()
418 return *(mShaderFactory);
421 GestureEventProcessor& Core::GetGestureEventProcessor()
423 return *(mGestureEventProcessor);
426 RelayoutController& Core::GetRelayoutController()
428 return *(mRelayoutController.Get());
431 ObjectRegistry& Core::GetObjectRegistry() const
433 return *(mObjectRegistry.Get());
436 EventThreadServices& Core::GetEventThreadServices()
441 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
443 return *(mPropertyNotificationManager);
446 AnimationPlaylist& Core::GetAnimationPlaylist() const
448 return *(mAnimationPlaylist);
451 Integration::GlAbstraction& Core::GetGlAbstraction() const
453 return mGraphicsController.GetGlAbstraction();
456 void Core::AddScene(Scene* scene)
458 mScenes.push_back(scene);
461 void Core::RemoveScene(Scene* scene)
463 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
464 if(iter != mScenes.end())
470 void Core::CreateThreadLocalStorage()
472 // a pointer to the ThreadLocalStorage object will be stored in TLS
473 // The ThreadLocalStorage object should be deleted by the Core destructor
474 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
478 void Core::RegisterObject(Dali::BaseObject* object)
480 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
481 mObjectRegistry->RegisterObject(object);
484 void Core::UnregisterObject(Dali::BaseObject* object)
486 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
487 mObjectRegistry->UnregisterObject(object);
490 Integration::RenderController& Core::GetRenderController()
492 return mRenderController;
495 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
497 return mUpdateManager->ReserveMessageSlot(size, updateScene);
500 BufferIndex Core::GetEventBufferIndex() const
502 return mUpdateManager->GetEventBufferIndex();
505 void Core::ForceNextUpdate()
507 mForceNextUpdate = true;
510 bool Core::IsNextUpdateForced()
512 bool nextUpdateForced = mForceNextUpdate;
513 mForceNextUpdate = false;
514 return nextUpdateForced;
517 } // namespace Internal