2 * Copyright (c) 2022 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/platform-abstraction.h>
28 #include <dali/integration-api/processor-interface.h>
29 #include <dali/integration-api/render-controller.h>
30 #include <dali/integration-api/trace.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 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
65 #if defined(DEBUG_ENABLED)
66 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
74 using Integration::Event;
75 using Integration::GlAbstraction;
76 using Integration::GlContextHelperAbstraction;
77 using Integration::PlatformAbstraction;
78 using Integration::RenderController;
79 using Integration::RenderStatus;
80 using Integration::UpdateStatus;
82 Core::Core(RenderController& renderController,
83 PlatformAbstraction& platform,
84 Graphics::Controller& graphicsController,
85 Integration::RenderToFrameBuffer renderToFboEnabled,
86 Integration::DepthBufferAvailable depthBufferAvailable,
87 Integration::StencilBufferAvailable stencilBufferAvailable,
88 Integration::PartialUpdateAvailable partialUpdateAvailable)
89 : mRenderController(renderController),
91 mGraphicsController(graphicsController),
92 mProcessingEvent(false),
93 mForceNextUpdate(false),
94 mProcessorUnregistered(false),
95 mPostProcessorUnregistered(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 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
111 mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
113 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
115 mDiscardQueue = new DiscardQueue(renderQueue);
117 mUpdateManager = new UpdateManager(*mNotificationManager,
119 *mPropertyNotificationManager,
124 *mRenderTaskProcessor);
126 mRenderManager->SetShaderSaver(*mUpdateManager);
128 mObjectRegistry = ObjectRegistry::New();
130 mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
132 // This must be called after stage is created but before stage initialization
133 mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
135 mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
137 mShaderFactory = new ShaderFactory();
138 mUpdateManager->SetShaderSaver(*mShaderFactory);
140 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
142 DALI_LOG_RELEASE_INFO("Node size: %lu\n", sizeof(Dali::Internal::SceneGraph::Node));
143 DALI_LOG_RELEASE_INFO("Renderer size: %lu\n", sizeof(Dali::Internal::SceneGraph::Renderer));
144 DALI_LOG_RELEASE_INFO("RenderItem size: %lu\n", sizeof(Dali::Internal::SceneGraph::RenderItem));
150 * The order of destructing these singletons is important!!!
153 // clear the thread local storage first
154 // allows core to be created / deleted many times in the same thread (how TET cases work).
155 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
156 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
163 mObjectRegistry.Reset();
165 // Stop relayout requests being raised on stage destruction
166 mRelayoutController.Reset();
168 // remove (last?) reference to stage
172 void Core::Initialize()
174 mStage->Initialize(*mScenes[0]);
177 Integration::ContextNotifierInterface* Core::GetContextNotifier()
182 void Core::RecoverFromContextLoss()
184 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
186 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
189 void Core::ContextCreated()
193 void Core::ContextDestroyed()
197 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo, bool uploadOnly)
199 // set the time delta so adaptor can easily print FPS with a release build with 0 as
200 // it is cached by frametime
201 status.secondsFromLastFrame = elapsedSeconds;
203 // Render returns true when there are updates on the stage or one or more animations are completed.
204 // Use the estimated time diff till we render as the elapsed time.
205 status.keepUpdating = mUpdateManager->Update(elapsedSeconds,
206 lastVSyncTimeMilliseconds,
207 nextVSyncTimeMilliseconds,
212 // Check the Notification Manager message queue to set needsNotification
213 status.needsNotification = mNotificationManager->MessagesToProcess();
215 // No need to keep update running if there are notifications to process.
216 // Any message to update will wake it up anyways
219 void Core::PreRender(RenderStatus& status, bool forceClear)
221 mRenderManager->PreRender(status, forceClear);
224 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
226 mRenderManager->PreRender(scene, damagedRects);
229 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
231 mRenderManager->RenderScene(status, scene, renderToFbo);
234 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
236 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
239 void Core::PostRender()
241 mUpdateManager->PostRender();
242 mRenderManager->PostRender();
245 void Core::SceneCreated()
247 mStage->EmitSceneCreatedSignal();
249 mRelayoutController->OnApplicationSceneCreated();
251 for(const auto& scene : mScenes)
253 Dali::Actor sceneRootLayer = scene->GetRootLayer();
254 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
258 void Core::QueueEvent(const Integration::Event& event)
260 if(mScenes.size() != 0)
262 mScenes.front()->QueueEvent(event);
266 void Core::ProcessEvents()
268 // Guard against calls to ProcessEvents() during ProcessEvents()
271 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
272 mRenderController.RequestProcessEventsOnIdle(false);
276 mProcessingEvent = true;
277 mRelayoutController->SetProcessingCoreEvents(true);
279 // Signal that any messages received will be flushed soon
280 mUpdateManager->EventProcessingStarted();
282 // Scene could be added or removed while processing the events
283 // Copy the Scene container locally to avoid possibly invalid iterator
284 SceneContainer scenes = mScenes;
286 // process events in all scenes
287 for(auto scene : scenes)
289 scene->ProcessEvents();
292 mNotificationManager->ProcessMessages();
294 // Emit signal here to inform listeners that event processing has finished.
295 for(auto scene : scenes)
297 scene->EmitEventProcessingFinishedSignal();
300 // Run any registered processors
303 // Run the size negotiation after event processing finished signal
304 mRelayoutController->Relayout();
306 // Run any registered post processors
309 // Rebuild depth tree after event processing has finished
310 for(auto scene : scenes)
312 scene->RebuildDepthTree();
315 // Flush any queued messages for the update-thread
316 const bool messagesToProcess = mUpdateManager->FlushQueue();
318 // Check if the touch or gestures require updates.
319 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
320 // Check if the next update is forced.
321 const bool forceUpdate = IsNextUpdateForced();
323 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
325 // tell the render controller to keep update thread running
326 mRenderController.RequestUpdate(forceUpdate);
329 mRelayoutController->SetProcessingCoreEvents(false);
331 // ProcessEvents() may now be called again
332 mProcessingEvent = false;
335 uint32_t Core::GetMaximumUpdateCount() const
337 return MAXIMUM_UPDATE_COUNT;
340 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
344 mPostProcessors.PushBack(&processor);
348 mProcessors.PushBack(&processor);
352 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
356 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
357 if(iter != mPostProcessors.End())
359 mPostProcessors.Erase(iter);
360 mPostProcessorUnregistered = true;
365 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
366 if(iter != mProcessors.End())
368 mProcessors.Erase(iter);
369 mProcessorUnregistered = true;
374 void Core::RunProcessors()
376 if(mProcessors.Count() != 0)
378 DALI_TRACE_SCOPE(gTraceFilter, "DALI_CORE_RUN_PROCESSORS");
380 // Copy processor pointers to prevent changes to vector affecting loop iterator.
381 Dali::Vector<Integration::Processor*> processors(mProcessors);
383 // To prevent accessing processor unregistered during the loop
384 mProcessorUnregistered = false;
386 for(auto processor : processors)
390 if(!mProcessorUnregistered)
392 processor->Process(false);
396 // Run processor if the processor is still in the list.
397 // It may be removed during the loop.
398 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), processor);
399 if(iter != mProcessors.End())
401 processor->Process(false);
409 void Core::RunPostProcessors()
411 if(mPostProcessors.Count() != 0)
413 DALI_TRACE_SCOPE(gTraceFilter, "DALI_CORE_RUN_POST_PROCESSORS");
415 // Copy processor pointers to prevent changes to vector affecting loop iterator.
416 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
418 // To prevent accessing processor unregistered during the loop
419 mPostProcessorUnregistered = false;
421 for(auto processor : processors)
425 if(!mPostProcessorUnregistered)
427 processor->Process(true);
431 // Run processor if the processor is still in the list.
432 // It may be removed during the loop.
433 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), processor);
434 if(iter != mPostProcessors.End())
436 processor->Process(true);
444 StagePtr Core::GetCurrentStage()
449 PlatformAbstraction& Core::GetPlatform()
454 UpdateManager& Core::GetUpdateManager()
456 return *(mUpdateManager);
459 RenderManager& Core::GetRenderManager()
461 return *(mRenderManager);
464 NotificationManager& Core::GetNotificationManager()
466 return *(mNotificationManager);
469 ShaderFactory& Core::GetShaderFactory()
471 return *(mShaderFactory);
474 GestureEventProcessor& Core::GetGestureEventProcessor()
476 return *(mGestureEventProcessor);
479 RelayoutController& Core::GetRelayoutController()
481 return *(mRelayoutController.Get());
484 ObjectRegistry& Core::GetObjectRegistry() const
486 return *(mObjectRegistry.Get());
489 void Core::LogMemoryPools() const
491 uint32_t animationPoolCapacity = SceneGraph::Animation::GetMemoryPoolCapacity();
492 uint32_t renderItemPoolCapacity = SceneGraph::RenderItem::GetMemoryPoolCapacity();
493 uint32_t relayoutItemPoolCapacity = mRelayoutController->GetMemoryPoolCapacity();
494 uint32_t rendererPoolCapacity = SceneGraph::Renderer::GetMemoryPoolCapacity();
495 uint32_t textureSetPoolCapacity = SceneGraph::TextureSet::GetMemoryPoolCapacity();
496 uint32_t renderTaskPoolCapacity = SceneGraph::RenderTaskList::GetMemoryPoolCapacity();
497 uint32_t nodePoolCapacity = SceneGraph::Node::GetMemoryPoolCapacity();
499 DALI_LOG_RELEASE_INFO(
500 "\nMemory Pool capacities:\n"
502 " RenderItems: %lu\n"
503 " RelayoutItems: %lu\n"
505 " TextureSets: %lu\n"
506 " RenderTasks: %lu\n"
508 animationPoolCapacity,
509 renderItemPoolCapacity,
510 relayoutItemPoolCapacity,
511 rendererPoolCapacity,
512 textureSetPoolCapacity,
513 renderTaskPoolCapacity,
516 uint32_t updateQCapacity = mUpdateManager->GetUpdateMessageQueueCapacity();
517 uint32_t renderQCapacity = mUpdateManager->GetRenderMessageQueueCapacity();
519 DALI_LOG_RELEASE_INFO(
520 "\nMessage Queue capacities:\n"
521 " UpdateQueue: %lu\n"
522 " RenderQueue: %lu\n",
526 size_t renderInstructionCapacity = mUpdateManager->GetRenderInstructionCapacity();
527 DALI_LOG_RELEASE_INFO("\nRenderInstruction capacity: %lu\n", renderInstructionCapacity);
530 EventThreadServices& Core::GetEventThreadServices()
535 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
537 return *(mPropertyNotificationManager);
540 AnimationPlaylist& Core::GetAnimationPlaylist() const
542 return *(mAnimationPlaylist);
545 Integration::GlAbstraction& Core::GetGlAbstraction() const
547 return mGraphicsController.GetGlAbstraction();
550 void Core::AddScene(Scene* scene)
552 mScenes.push_back(scene);
555 void Core::RemoveScene(Scene* scene)
557 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
558 if(iter != mScenes.end())
564 void Core::CreateThreadLocalStorage()
566 // a pointer to the ThreadLocalStorage object will be stored in TLS
567 // The ThreadLocalStorage object should be deleted by the Core destructor
568 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
572 void Core::RegisterObject(Dali::BaseObject* object)
574 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
575 mObjectRegistry->RegisterObject(object);
578 void Core::UnregisterObject(Dali::BaseObject* object)
580 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
581 mObjectRegistry->UnregisterObject(object);
584 Integration::RenderController& Core::GetRenderController()
586 return mRenderController;
589 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
591 return mUpdateManager->ReserveMessageSlot(size, updateScene);
594 BufferIndex Core::GetEventBufferIndex() const
596 return mUpdateManager->GetEventBufferIndex();
599 void Core::ForceNextUpdate()
601 mForceNextUpdate = true;
604 bool Core::IsNextUpdateForced()
606 bool nextUpdateForced = mForceNextUpdate;
607 mForceNextUpdate = false;
608 return nextUpdateForced;
611 } // namespace Internal