2 * Copyright (c) 2023 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::RenderManager;
54 using Dali::Internal::SceneGraph::RenderQueue;
55 using Dali::Internal::SceneGraph::UpdateManager;
59 // The Update for frame N+1 may be processed whilst frame N is being rendered.
60 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
62 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
73 using Integration::Event;
74 using Integration::GlAbstraction;
75 using Integration::GlContextHelperAbstraction;
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 mProcessorUnregistered(false),
93 mPostProcessorUnregistered(false),
96 // Create the thread local storage
97 CreateThreadLocalStorage();
99 // This does nothing until Core is built with --enable-performance-monitor
100 PERFORMANCE_MONITOR_INIT(platform);
102 mNotificationManager = new NotificationManager();
104 mAnimationPlaylist = AnimationPlaylist::New();
106 mPropertyNotificationManager = PropertyNotificationManager::New();
108 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
110 mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
112 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
114 mUpdateManager = new UpdateManager(*mNotificationManager,
116 *mPropertyNotificationManager,
120 *mRenderTaskProcessor);
122 mRenderManager->SetShaderSaver(*mUpdateManager);
124 mObjectRegistry = ObjectRegistry::New();
126 mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
128 // This must be called after stage is created but before stage initialization
129 mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
131 mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
133 mShaderFactory = new ShaderFactory();
134 mUpdateManager->SetShaderSaver(*mShaderFactory);
136 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
138 DALI_LOG_RELEASE_INFO("Node size: %lu\n", sizeof(Dali::Internal::SceneGraph::Node));
139 DALI_LOG_RELEASE_INFO("Renderer size: %lu\n", sizeof(Dali::Internal::SceneGraph::Renderer));
140 DALI_LOG_RELEASE_INFO("RenderItem size: %lu\n", sizeof(Dali::Internal::SceneGraph::RenderItem));
146 * The order of destructing these singletons is important!!!
149 // clear the thread local storage first
150 // allows core to be created / deleted many times in the same thread (how TET cases work).
151 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
152 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
159 mObjectRegistry.Reset();
161 // Stop relayout requests being raised on stage destruction
162 mRelayoutController.Reset();
164 // remove (last?) reference to stage
168 void Core::Initialize()
170 mStage->Initialize(*mScenes[0]);
173 Integration::ContextNotifierInterface* Core::GetContextNotifier()
178 void Core::RecoverFromContextLoss()
180 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
182 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
185 void Core::ContextCreated()
189 void Core::ContextDestroyed()
193 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo, bool uploadOnly)
195 // set the time delta so adaptor can easily print FPS with a release build with 0 as
196 // it is cached by frametime
197 status.secondsFromLastFrame = elapsedSeconds;
199 // Render returns true when there are updates on the stage or one or more animations are completed.
200 // Use the estimated time diff till we render as the elapsed time.
201 status.keepUpdating = mUpdateManager->Update(elapsedSeconds,
202 lastVSyncTimeMilliseconds,
203 nextVSyncTimeMilliseconds,
208 // Check the Notification Manager message queue to set needsNotification
209 status.needsNotification = mNotificationManager->MessagesToProcess();
211 // No need to keep update running if there are notifications to process.
212 // Any message to update will wake it up anyways
215 void Core::PreRender(RenderStatus& status, bool forceClear)
217 mRenderManager->PreRender(status, forceClear);
220 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
222 mRenderManager->PreRender(scene, damagedRects);
225 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
227 mRenderManager->RenderScene(status, scene, renderToFbo);
230 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
232 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
235 void Core::PreCompileShader(const std::string& vertexShader, const std::string& fragmentShader)
237 mRenderManager->PreCompileShader(vertexShader, fragmentShader);
240 void Core::PostRender()
242 mUpdateManager->PostRender();
243 mRenderManager->PostRender();
246 void Core::SceneCreated()
248 mStage->EmitSceneCreatedSignal();
250 mRelayoutController->OnApplicationSceneCreated();
252 for(const auto& scene : mScenes)
254 Dali::Actor sceneRootLayer = scene->GetRootLayer();
255 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
259 void Core::QueueEvent(const Integration::Event& event)
261 if(mScenes.size() != 0)
263 mScenes.front()->QueueEvent(event);
267 void Core::ForceRelayout()
271 DALI_LOG_ERROR("ForceRelayout should not be called from within RelayoutAndFlush!\n");
275 // Scene could be added or removed while processing the events
276 // Copy the Scene container locally to avoid possibly invalid iterator
277 SceneContainer scenes = mScenes;
279 RelayoutAndFlush(scenes);
282 void Core::ProcessEvents()
284 // Guard against calls to ProcessEvents() during ProcessEvents()
287 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
288 mRenderController.RequestProcessEventsOnIdle();
292 mProcessingEvent = true;
293 mRelayoutController->SetProcessingCoreEvents(true);
295 // Signal that any messages received will be flushed soon
296 mUpdateManager->EventProcessingStarted();
298 // Scene could be added or removed while processing the events
299 // Copy the Scene container locally to avoid possibly invalid iterator
300 SceneContainer scenes = mScenes;
302 // process events in all scenes
303 for(auto scene : scenes)
305 scene->ProcessEvents();
308 mNotificationManager->ProcessMessages();
310 // Emit signal here to inform listeners that event processing has finished.
311 for(auto scene : scenes)
313 scene->EmitEventProcessingFinishedSignal();
316 RelayoutAndFlush(scenes);
318 mUpdateManager->EventProcessingFinished();
320 // Check if the touch or gestures require updates.
321 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
323 if(gestureNeedsUpdate)
325 // tell the render controller to keep update thread running
326 mRenderController.RequestUpdate();
329 mRelayoutController->SetProcessingCoreEvents(false);
331 // ProcessEvents() may now be called again
332 mProcessingEvent = false;
335 void Core::RelayoutAndFlush(SceneContainer& scenes)
339 DALI_LOG_ERROR("RelayoutAndFlush should not be called from within RelayoutAndFlush!\n");
343 const bool isProcessEvents = mProcessingEvent;
347 // Fake that we are in ProcessEvents()
348 mProcessingEvent = true;
349 mRelayoutController->SetProcessingCoreEvents(true);
351 // Signal that any messages received will be flushed soon
352 mUpdateManager->EventProcessingStarted();
355 mRelayoutFlush = true;
357 // Run any registered processors
360 // Run the size negotiation after event processing finished signal
361 mRelayoutController->Relayout();
363 // Run any registered post processors
366 // Rebuild depth tree after event processing has finished
367 for(auto& scene : scenes)
369 scene->RebuildDepthTree();
372 // Flush any queued messages for the update-thread
373 const bool messagesToProcess = mUpdateManager->FlushQueue();
375 if(messagesToProcess)
377 // tell the render controller to keep update thread running
378 mRenderController.RequestUpdate();
381 mRelayoutFlush = false;
385 // Revert fake informations
386 mProcessingEvent = false;
387 mRelayoutController->SetProcessingCoreEvents(false);
389 mUpdateManager->EventProcessingFinished();
393 uint32_t Core::GetMaximumUpdateCount() const
395 return MAXIMUM_UPDATE_COUNT;
398 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
402 mPostProcessors.PushBack(&processor);
406 mProcessors.PushBack(&processor);
410 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
414 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
415 if(iter != mPostProcessors.End())
417 mPostProcessors.Erase(iter);
418 mPostProcessorUnregistered = true;
423 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
424 if(iter != mProcessors.End())
426 mProcessors.Erase(iter);
427 mProcessorUnregistered = true;
432 void Core::RunProcessors()
434 if(mProcessors.Count() != 0)
436 DALI_TRACE_SCOPE(gTraceFilter, "DALI_CORE_RUN_PROCESSORS");
438 // Copy processor pointers to prevent changes to vector affecting loop iterator.
439 Dali::Vector<Integration::Processor*> processors(mProcessors);
441 // To prevent accessing processor unregistered during the loop
442 mProcessorUnregistered = false;
444 for(auto processor : processors)
448 if(!mProcessorUnregistered)
450 processor->Process(false);
454 // Run processor if the processor is still in the list.
455 // It may be removed during the loop.
456 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), processor);
457 if(iter != mProcessors.End())
459 processor->Process(false);
467 void Core::RunPostProcessors()
469 if(mPostProcessors.Count() != 0)
471 DALI_TRACE_SCOPE(gTraceFilter, "DALI_CORE_RUN_POST_PROCESSORS");
473 // Copy processor pointers to prevent changes to vector affecting loop iterator.
474 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
476 // To prevent accessing processor unregistered during the loop
477 mPostProcessorUnregistered = false;
479 for(auto processor : processors)
483 if(!mPostProcessorUnregistered)
485 processor->Process(true);
489 // Run processor if the processor is still in the list.
490 // It may be removed during the loop.
491 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), processor);
492 if(iter != mPostProcessors.End())
494 processor->Process(true);
502 StagePtr Core::GetCurrentStage()
507 PlatformAbstraction& Core::GetPlatform()
512 UpdateManager& Core::GetUpdateManager()
514 return *(mUpdateManager);
517 RenderManager& Core::GetRenderManager()
519 return *(mRenderManager);
522 NotificationManager& Core::GetNotificationManager()
524 return *(mNotificationManager);
527 ShaderFactory& Core::GetShaderFactory()
529 return *(mShaderFactory);
532 GestureEventProcessor& Core::GetGestureEventProcessor()
534 return *(mGestureEventProcessor);
537 RelayoutController& Core::GetRelayoutController()
539 return *(mRelayoutController.Get());
542 ObjectRegistry& Core::GetObjectRegistry() const
544 return *(mObjectRegistry.Get());
547 void Core::LogMemoryPools() const
549 uint32_t animationPoolCapacity = SceneGraph::Animation::GetMemoryPoolCapacity();
550 uint32_t renderItemPoolCapacity = SceneGraph::RenderItem::GetMemoryPoolCapacity();
551 uint32_t relayoutItemPoolCapacity = mRelayoutController->GetMemoryPoolCapacity();
552 uint32_t rendererPoolCapacity = SceneGraph::Renderer::GetMemoryPoolCapacity();
553 uint32_t textureSetPoolCapacity = SceneGraph::TextureSet::GetMemoryPoolCapacity();
554 uint32_t renderTaskPoolCapacity = SceneGraph::RenderTaskList::GetMemoryPoolCapacity();
555 uint32_t nodePoolCapacity = SceneGraph::Node::GetMemoryPoolCapacity();
557 DALI_LOG_RELEASE_INFO(
558 "\nMemory Pool capacities:\n"
560 " RenderItems: %lu\n"
561 " RelayoutItems: %lu\n"
563 " TextureSets: %lu\n"
564 " RenderTasks: %lu\n"
566 animationPoolCapacity,
567 renderItemPoolCapacity,
568 relayoutItemPoolCapacity,
569 rendererPoolCapacity,
570 textureSetPoolCapacity,
571 renderTaskPoolCapacity,
574 uint32_t updateQCapacity = mUpdateManager->GetUpdateMessageQueueCapacity();
575 uint32_t renderQCapacity = mUpdateManager->GetRenderMessageQueueCapacity();
577 DALI_LOG_RELEASE_INFO(
578 "\nMessage Queue capacities:\n"
579 " UpdateQueue: %lu\n"
580 " RenderQueue: %lu\n",
584 size_t renderInstructionCapacity = mUpdateManager->GetRenderInstructionCapacity();
585 DALI_LOG_RELEASE_INFO("\nRenderInstruction capacity: %lu\n", renderInstructionCapacity);
588 EventThreadServices& Core::GetEventThreadServices()
593 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
595 return *(mPropertyNotificationManager);
598 AnimationPlaylist& Core::GetAnimationPlaylist() const
600 return *(mAnimationPlaylist);
603 Integration::GlAbstraction& Core::GetGlAbstraction() const
605 return mGraphicsController.GetGlAbstraction();
608 void Core::AddScene(Scene* scene)
610 mScenes.push_back(scene);
613 void Core::RemoveScene(Scene* scene)
615 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
616 if(iter != mScenes.end())
622 void Core::CreateThreadLocalStorage()
624 // a pointer to the ThreadLocalStorage object will be stored in TLS
625 // The ThreadLocalStorage object should be deleted by the Core destructor
626 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
630 void Core::RegisterObject(Dali::BaseObject* object)
632 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
633 mObjectRegistry->RegisterObject(object);
636 void Core::UnregisterObject(Dali::BaseObject* object)
638 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
639 mObjectRegistry->UnregisterObject(object);
642 Integration::RenderController& Core::GetRenderController()
644 return mRenderController;
647 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
649 return mUpdateManager->ReserveMessageSlot(size, updateScene);
652 BufferIndex Core::GetEventBufferIndex() const
654 return mUpdateManager->GetEventBufferIndex();
657 } // namespace Internal