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();
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::PostRender()
237 mUpdateManager->PostRender();
238 mRenderManager->PostRender();
241 void Core::SceneCreated()
243 mStage->EmitSceneCreatedSignal();
245 mRelayoutController->OnApplicationSceneCreated();
247 for(const auto& scene : mScenes)
249 Dali::Actor sceneRootLayer = scene->GetRootLayer();
250 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
254 void Core::QueueEvent(const Integration::Event& event)
256 if(mScenes.size() != 0)
258 mScenes.front()->QueueEvent(event);
262 void Core::ProcessEvents()
264 // Guard against calls to ProcessEvents() during ProcessEvents()
267 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
268 mRenderController.RequestProcessEventsOnIdle(false);
272 mProcessingEvent = true;
273 mRelayoutController->SetProcessingCoreEvents(true);
275 // Signal that any messages received will be flushed soon
276 mUpdateManager->EventProcessingStarted();
278 // Scene could be added or removed while processing the events
279 // Copy the Scene container locally to avoid possibly invalid iterator
280 SceneContainer scenes = mScenes;
282 // process events in all scenes
283 for(auto scene : scenes)
285 scene->ProcessEvents();
288 mNotificationManager->ProcessMessages();
290 // Emit signal here to inform listeners that event processing has finished.
291 for(auto scene : scenes)
293 scene->EmitEventProcessingFinishedSignal();
296 // Run any registered processors
299 // Run the size negotiation after event processing finished signal
300 mRelayoutController->Relayout();
302 // Run any registered post processors
305 // Rebuild depth tree after event processing has finished
306 for(auto scene : scenes)
308 scene->RebuildDepthTree();
311 // Flush any queued messages for the update-thread
312 const bool messagesToProcess = mUpdateManager->FlushQueue();
314 // Check if the touch or gestures require updates.
315 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
316 // Check if the next update is forced.
317 const bool forceUpdate = IsNextUpdateForced();
319 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
321 // tell the render controller to keep update thread running
322 mRenderController.RequestUpdate(forceUpdate);
325 mRelayoutController->SetProcessingCoreEvents(false);
327 // ProcessEvents() may now be called again
328 mProcessingEvent = false;
331 uint32_t Core::GetMaximumUpdateCount() const
333 return MAXIMUM_UPDATE_COUNT;
336 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
340 mPostProcessors.PushBack(&processor);
344 mProcessors.PushBack(&processor);
348 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
352 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
353 if(iter != mPostProcessors.End())
355 mPostProcessors.Erase(iter);
356 mPostProcessorUnregistered = true;
361 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
362 if(iter != mProcessors.End())
364 mProcessors.Erase(iter);
365 mProcessorUnregistered = true;
370 void Core::RunProcessors()
372 if(mProcessors.Count() != 0)
374 DALI_TRACE_BEGIN(gTraceFilter, "DALI_CORE_RUN_PROCESSORS");
376 // Copy processor pointers to prevent changes to vector affecting loop iterator.
377 Dali::Vector<Integration::Processor*> processors(mProcessors);
379 // To prevent accessing processor unregistered during the loop
380 mProcessorUnregistered = false;
382 for(auto processor : processors)
386 if(!mProcessorUnregistered)
388 processor->Process(false);
392 // Run processor if the processor is still in the list.
393 // It may be removed during the loop.
394 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), processor);
395 if(iter != mProcessors.End())
397 processor->Process(false);
402 DALI_TRACE_END(gTraceFilter, "DALI_CORE_RUN_PROCESSORS");
406 void Core::RunPostProcessors()
408 if(mPostProcessors.Count() != 0)
410 DALI_TRACE_BEGIN(gTraceFilter, "DALI_CORE_RUN_POST_PROCESSORS");
412 // Copy processor pointers to prevent changes to vector affecting loop iterator.
413 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
415 // To prevent accessing processor unregistered during the loop
416 mPostProcessorUnregistered = false;
418 for(auto processor : processors)
422 if(!mPostProcessorUnregistered)
424 processor->Process(true);
428 // Run processor if the processor is still in the list.
429 // It may be removed during the loop.
430 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), processor);
431 if(iter != mPostProcessors.End())
433 processor->Process(true);
438 DALI_TRACE_END(gTraceFilter, "DALI_CORE_RUN_POST_PROCESSORS");
442 StagePtr Core::GetCurrentStage()
447 PlatformAbstraction& Core::GetPlatform()
452 UpdateManager& Core::GetUpdateManager()
454 return *(mUpdateManager);
457 RenderManager& Core::GetRenderManager()
459 return *(mRenderManager);
462 NotificationManager& Core::GetNotificationManager()
464 return *(mNotificationManager);
467 ShaderFactory& Core::GetShaderFactory()
469 return *(mShaderFactory);
472 GestureEventProcessor& Core::GetGestureEventProcessor()
474 return *(mGestureEventProcessor);
477 RelayoutController& Core::GetRelayoutController()
479 return *(mRelayoutController.Get());
482 ObjectRegistry& Core::GetObjectRegistry() const
484 return *(mObjectRegistry.Get());
487 EventThreadServices& Core::GetEventThreadServices()
492 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
494 return *(mPropertyNotificationManager);
497 AnimationPlaylist& Core::GetAnimationPlaylist() const
499 return *(mAnimationPlaylist);
502 Integration::GlAbstraction& Core::GetGlAbstraction() const
504 return mGraphicsController.GetGlAbstraction();
507 void Core::AddScene(Scene* scene)
509 mScenes.push_back(scene);
512 void Core::RemoveScene(Scene* scene)
514 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
515 if(iter != mScenes.end())
521 void Core::CreateThreadLocalStorage()
523 // a pointer to the ThreadLocalStorage object will be stored in TLS
524 // The ThreadLocalStorage object should be deleted by the Core destructor
525 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
529 void Core::RegisterObject(Dali::BaseObject* object)
531 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
532 mObjectRegistry->RegisterObject(object);
535 void Core::UnregisterObject(Dali::BaseObject* object)
537 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
538 mObjectRegistry->UnregisterObject(object);
541 Integration::RenderController& Core::GetRenderController()
543 return mRenderController;
546 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
548 return mUpdateManager->ReserveMessageSlot(size, updateScene);
551 BufferIndex Core::GetEventBufferIndex() const
553 return mUpdateManager->GetEventBufferIndex();
556 void Core::ForceNextUpdate()
558 mForceNextUpdate = true;
561 bool Core::IsNextUpdateForced()
563 bool nextUpdateForced = mForceNextUpdate;
564 mForceNextUpdate = false;
565 return nextUpdateForced;
568 } // namespace Internal