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>
31 #include <dali/internal/event/actors/actor-impl.h>
32 #include <dali/internal/event/animation/animation-playlist.h>
33 #include <dali/internal/event/common/event-thread-services.h>
34 #include <dali/internal/event/common/notification-manager.h>
35 #include <dali/internal/event/common/property-notification-manager.h>
36 #include <dali/internal/event/common/stage-impl.h>
37 #include <dali/internal/event/common/thread-local-storage.h>
38 #include <dali/internal/event/common/type-registry-impl.h>
39 #include <dali/internal/event/effects/shader-factory.h>
40 #include <dali/internal/event/events/event-processor.h>
41 #include <dali/internal/event/events/gesture-event-processor.h>
42 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
43 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
45 #include <dali/internal/update/common/discard-queue.h>
46 #include <dali/internal/update/manager/render-task-processor.h>
47 #include <dali/internal/update/manager/update-manager.h>
49 #include <dali/internal/render/common/performance-monitor.h>
50 #include <dali/internal/render/common/render-manager.h>
52 using Dali::Internal::SceneGraph::DiscardQueue;
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 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
71 using Integration::Event;
72 using Integration::GlAbstraction;
73 using Integration::GlContextHelperAbstraction;
74 using Integration::PlatformAbstraction;
75 using Integration::RenderController;
76 using Integration::RenderStatus;
77 using Integration::UpdateStatus;
79 Core::Core(RenderController& renderController,
80 PlatformAbstraction& platform,
81 Graphics::Controller& graphicsController,
82 Integration::RenderToFrameBuffer renderToFboEnabled,
83 Integration::DepthBufferAvailable depthBufferAvailable,
84 Integration::StencilBufferAvailable stencilBufferAvailable,
85 Integration::PartialUpdateAvailable partialUpdateAvailable)
86 : mRenderController(renderController),
88 mGraphicsController(graphicsController),
89 mProcessingEvent(false),
90 mForceNextUpdate(false),
91 mProcessorUnregistered(false),
92 mPostProcessorUnregistered(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, bool uploadOnly)
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,
205 // Check the Notification Manager message queue to set needsNotification
206 status.needsNotification = mNotificationManager->MessagesToProcess();
208 // No need to keep update running if there are notifications to process.
209 // Any message to update will wake it up anyways
212 void Core::PreRender(RenderStatus& status, bool forceClear)
214 mRenderManager->PreRender(status, forceClear);
217 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
219 mRenderManager->PreRender(scene, damagedRects);
222 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
224 mRenderManager->RenderScene(status, scene, renderToFbo);
227 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
229 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
232 void Core::PostRender()
234 mRenderManager->PostRender();
237 void Core::SceneCreated()
239 mStage->EmitSceneCreatedSignal();
241 mRelayoutController->OnApplicationSceneCreated();
243 for(const auto& scene : mScenes)
245 Dali::Actor sceneRootLayer = scene->GetRootLayer();
246 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
250 void Core::QueueEvent(const Integration::Event& event)
252 if(mScenes.size() != 0)
254 mScenes.front()->QueueEvent(event);
258 void Core::ProcessEvents()
260 // Guard against calls to ProcessEvents() during ProcessEvents()
263 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
264 mRenderController.RequestProcessEventsOnIdle(false);
268 mProcessingEvent = true;
269 mRelayoutController->SetProcessingCoreEvents(true);
271 // Signal that any messages received will be flushed soon
272 mUpdateManager->EventProcessingStarted();
274 // Scene could be added or removed while processing the events
275 // Copy the Scene container locally to avoid possibly invalid iterator
276 SceneContainer scenes = mScenes;
278 // process events in all scenes
279 for(auto scene : scenes)
281 scene->ProcessEvents();
284 mNotificationManager->ProcessMessages();
286 // Emit signal here to inform listeners that event processing has finished.
287 for(auto scene : scenes)
289 scene->EmitEventProcessingFinishedSignal();
292 // Run any registered processors
295 // Run the size negotiation after event processing finished signal
296 mRelayoutController->Relayout();
298 // Run any registered post processors
301 // Rebuild depth tree after event processing has finished
302 for(auto scene : scenes)
304 scene->RebuildDepthTree();
307 // Flush any queued messages for the update-thread
308 const bool messagesToProcess = mUpdateManager->FlushQueue();
310 // Check if the touch or gestures require updates.
311 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
312 // Check if the next update is forced.
313 const bool forceUpdate = IsNextUpdateForced();
315 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
317 // tell the render controller to keep update thread running
318 mRenderController.RequestUpdate(forceUpdate);
321 mRelayoutController->SetProcessingCoreEvents(false);
323 // ProcessEvents() may now be called again
324 mProcessingEvent = false;
327 uint32_t Core::GetMaximumUpdateCount() const
329 return MAXIMUM_UPDATE_COUNT;
332 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
336 mPostProcessors.PushBack(&processor);
340 mProcessors.PushBack(&processor);
344 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
348 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
349 if(iter != mPostProcessors.End())
351 mPostProcessors.Erase(iter);
352 mPostProcessorUnregistered = true;
357 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
358 if(iter != mProcessors.End())
360 mProcessors.Erase(iter);
361 mProcessorUnregistered = true;
366 void Core::RunProcessors()
368 if(mProcessors.Count() != 0)
370 DALI_LOG_RELEASE_INFO("Start RunProcessors\n");
372 // Copy processor pointers to prevent changes to vector affecting loop iterator.
373 Dali::Vector<Integration::Processor*> processors(mProcessors);
375 // To prevent accessing processor unregistered during the loop
376 mProcessorUnregistered = false;
378 for(auto processor : processors)
382 if(!mProcessorUnregistered)
384 processor->Process(false);
388 // Run processor if the processor is still in the list.
389 // It may be removed during the loop.
390 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), processor);
391 if(iter != mProcessors.End())
393 processor->Process(false);
398 DALI_LOG_RELEASE_INFO("End RunProcessors\n");
402 void Core::RunPostProcessors()
404 if(mPostProcessors.Count() != 0)
406 DALI_LOG_RELEASE_INFO("Start RunPostProcessors\n");
408 // Copy processor pointers to prevent changes to vector affecting loop iterator.
409 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
411 // To prevent accessing processor unregistered during the loop
412 mPostProcessorUnregistered = false;
414 for(auto processor : processors)
418 if(!mPostProcessorUnregistered)
420 processor->Process(true);
424 // Run processor if the processor is still in the list.
425 // It may be removed during the loop.
426 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), processor);
427 if(iter != mPostProcessors.End())
429 processor->Process(true);
434 DALI_LOG_RELEASE_INFO("End RunPostProcessors\n");
438 StagePtr Core::GetCurrentStage()
443 PlatformAbstraction& Core::GetPlatform()
448 UpdateManager& Core::GetUpdateManager()
450 return *(mUpdateManager);
453 RenderManager& Core::GetRenderManager()
455 return *(mRenderManager);
458 NotificationManager& Core::GetNotificationManager()
460 return *(mNotificationManager);
463 ShaderFactory& Core::GetShaderFactory()
465 return *(mShaderFactory);
468 GestureEventProcessor& Core::GetGestureEventProcessor()
470 return *(mGestureEventProcessor);
473 RelayoutController& Core::GetRelayoutController()
475 return *(mRelayoutController.Get());
478 ObjectRegistry& Core::GetObjectRegistry() const
480 return *(mObjectRegistry.Get());
483 EventThreadServices& Core::GetEventThreadServices()
488 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
490 return *(mPropertyNotificationManager);
493 AnimationPlaylist& Core::GetAnimationPlaylist() const
495 return *(mAnimationPlaylist);
498 Integration::GlAbstraction& Core::GetGlAbstraction() const
500 return mGraphicsController.GetGlAbstraction();
503 void Core::AddScene(Scene* scene)
505 mScenes.push_back(scene);
508 void Core::RemoveScene(Scene* scene)
510 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
511 if(iter != mScenes.end())
517 void Core::CreateThreadLocalStorage()
519 // a pointer to the ThreadLocalStorage object will be stored in TLS
520 // The ThreadLocalStorage object should be deleted by the Core destructor
521 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
525 void Core::RegisterObject(Dali::BaseObject* object)
527 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
528 mObjectRegistry->RegisterObject(object);
531 void Core::UnregisterObject(Dali::BaseObject* object)
533 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
534 mObjectRegistry->UnregisterObject(object);
537 Integration::RenderController& Core::GetRenderController()
539 return mRenderController;
542 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
544 return mUpdateManager->ReserveMessageSlot(size, updateScene);
547 BufferIndex Core::GetEventBufferIndex() const
549 return mUpdateManager->GetEventBufferIndex();
552 void Core::ForceNextUpdate()
554 mForceNextUpdate = true;
557 bool Core::IsNextUpdateForced()
559 bool nextUpdateForced = mForceNextUpdate;
560 mForceNextUpdate = false;
561 return nextUpdateForced;
564 } // namespace Internal