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)
92 // Create the thread local storage
93 CreateThreadLocalStorage();
95 // This does nothing until Core is built with --enable-performance-monitor
96 PERFORMANCE_MONITOR_INIT(platform);
98 mNotificationManager = new NotificationManager();
100 mAnimationPlaylist = AnimationPlaylist::New();
102 mPropertyNotificationManager = PropertyNotificationManager::New();
104 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
106 mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
108 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
110 mDiscardQueue = new DiscardQueue(renderQueue);
112 mUpdateManager = new UpdateManager(*mNotificationManager,
114 *mPropertyNotificationManager,
119 *mRenderTaskProcessor);
121 mRenderManager->SetShaderSaver(*mUpdateManager);
123 mObjectRegistry = ObjectRegistry::New();
125 mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
127 // This must be called after stage is created but before stage initialization
128 mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
130 mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
132 mShaderFactory = new ShaderFactory();
133 mUpdateManager->SetShaderSaver(*mShaderFactory);
135 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
141 * The order of destructing these singletons is important!!!
144 // clear the thread local storage first
145 // allows core to be created / deleted many times in the same thread (how TET cases work).
146 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
147 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
154 mObjectRegistry.Reset();
156 // Stop relayout requests being raised on stage destruction
157 mRelayoutController.Reset();
159 // remove (last?) reference to stage
163 void Core::Initialize()
165 mStage->Initialize(*mScenes[0]);
168 Integration::ContextNotifierInterface* Core::GetContextNotifier()
173 void Core::RecoverFromContextLoss()
175 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
177 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
180 void Core::ContextCreated()
184 void Core::ContextDestroyed()
188 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo, bool uploadOnly)
190 // set the time delta so adaptor can easily print FPS with a release build with 0 as
191 // it is cached by frametime
192 status.secondsFromLastFrame = elapsedSeconds;
194 // Render returns true when there are updates on the stage or one or more animations are completed.
195 // Use the estimated time diff till we render as the elapsed time.
196 status.keepUpdating = mUpdateManager->Update(elapsedSeconds,
197 lastVSyncTimeMilliseconds,
198 nextVSyncTimeMilliseconds,
203 // Check the Notification Manager message queue to set needsNotification
204 status.needsNotification = mNotificationManager->MessagesToProcess();
206 // No need to keep update running if there are notifications to process.
207 // Any message to update will wake it up anyways
210 void Core::PreRender(RenderStatus& status, bool forceClear)
212 mRenderManager->PreRender(status, forceClear);
215 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
217 mRenderManager->PreRender(scene, damagedRects);
220 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
222 mRenderManager->RenderScene(status, scene, renderToFbo);
225 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
227 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
230 void Core::PostRender()
232 mRenderManager->PostRender();
235 void Core::SceneCreated()
237 mStage->EmitSceneCreatedSignal();
239 mRelayoutController->OnApplicationSceneCreated();
241 for(const auto& scene : mScenes)
243 Dali::Actor sceneRootLayer = scene->GetRootLayer();
244 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
248 void Core::QueueEvent(const Integration::Event& event)
250 if(mScenes.size() != 0)
252 mScenes.front()->QueueEvent(event);
256 void Core::ProcessEvents()
258 // Guard against calls to ProcessEvents() during ProcessEvents()
261 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
262 mRenderController.RequestProcessEventsOnIdle(false);
266 mProcessingEvent = true;
267 mRelayoutController->SetProcessingCoreEvents(true);
269 // Signal that any messages received will be flushed soon
270 mUpdateManager->EventProcessingStarted();
272 // Scene could be added or removed while processing the events
273 // Copy the Scene container locally to avoid possibly invalid iterator
274 SceneContainer scenes = mScenes;
276 // process events in all scenes
277 for(auto scene : scenes)
279 scene->ProcessEvents();
282 mNotificationManager->ProcessMessages();
284 // Emit signal here to inform listeners that event processing has finished.
285 for(auto scene : scenes)
287 scene->EmitEventProcessingFinishedSignal();
290 // Run any registered processors
293 // Run the size negotiation after event processing finished signal
294 mRelayoutController->Relayout();
296 // Run any registered post processors
299 // Rebuild depth tree after event processing has finished
300 for(auto scene : scenes)
302 scene->RebuildDepthTree();
305 // Flush any queued messages for the update-thread
306 const bool messagesToProcess = mUpdateManager->FlushQueue();
308 // Check if the touch or gestures require updates.
309 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
310 // Check if the next update is forced.
311 const bool forceUpdate = IsNextUpdateForced();
313 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
315 // tell the render controller to keep update thread running
316 mRenderController.RequestUpdate(forceUpdate);
319 mRelayoutController->SetProcessingCoreEvents(false);
321 // ProcessEvents() may now be called again
322 mProcessingEvent = false;
325 uint32_t Core::GetMaximumUpdateCount() const
327 return MAXIMUM_UPDATE_COUNT;
330 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
334 mPostProcessors.PushBack(&processor);
338 mProcessors.PushBack(&processor);
342 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
346 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
347 if(iter != mPostProcessors.End())
349 mPostProcessors.Erase(iter);
354 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
355 if(iter != mProcessors.End())
357 mProcessors.Erase(iter);
362 void Core::RunProcessors()
364 // Copy processor pointers to prevent changes to vector affecting loop iterator.
365 Dali::Vector<Integration::Processor*> processors(mProcessors);
367 for(auto processor : processors)
371 // Run processor if the processor is still in the list.
372 // It may be removed during the loop.
373 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), processor);
374 if(iter != mProcessors.End())
376 processor->Process(false);
382 void Core::RunPostProcessors()
384 // Copy processor pointers to prevent changes to vector affecting loop iterator.
385 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
387 for(auto processor : processors)
391 // Run processor if the processor is still in the list.
392 // It may be removed during the loop.
393 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), processor);
394 if(iter != mPostProcessors.End())
396 processor->Process(true);
402 StagePtr Core::GetCurrentStage()
407 PlatformAbstraction& Core::GetPlatform()
412 UpdateManager& Core::GetUpdateManager()
414 return *(mUpdateManager);
417 RenderManager& Core::GetRenderManager()
419 return *(mRenderManager);
422 NotificationManager& Core::GetNotificationManager()
424 return *(mNotificationManager);
427 ShaderFactory& Core::GetShaderFactory()
429 return *(mShaderFactory);
432 GestureEventProcessor& Core::GetGestureEventProcessor()
434 return *(mGestureEventProcessor);
437 RelayoutController& Core::GetRelayoutController()
439 return *(mRelayoutController.Get());
442 ObjectRegistry& Core::GetObjectRegistry() const
444 return *(mObjectRegistry.Get());
447 EventThreadServices& Core::GetEventThreadServices()
452 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
454 return *(mPropertyNotificationManager);
457 AnimationPlaylist& Core::GetAnimationPlaylist() const
459 return *(mAnimationPlaylist);
462 Integration::GlAbstraction& Core::GetGlAbstraction() const
464 return mGraphicsController.GetGlAbstraction();
467 void Core::AddScene(Scene* scene)
469 mScenes.push_back(scene);
472 void Core::RemoveScene(Scene* scene)
474 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
475 if(iter != mScenes.end())
481 void Core::CreateThreadLocalStorage()
483 // a pointer to the ThreadLocalStorage object will be stored in TLS
484 // The ThreadLocalStorage object should be deleted by the Core destructor
485 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
489 void Core::RegisterObject(Dali::BaseObject* object)
491 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
492 mObjectRegistry->RegisterObject(object);
495 void Core::UnregisterObject(Dali::BaseObject* object)
497 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
498 mObjectRegistry->UnregisterObject(object);
501 Integration::RenderController& Core::GetRenderController()
503 return mRenderController;
506 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
508 return mUpdateManager->ReserveMessageSlot(size, updateScene);
511 BufferIndex Core::GetEventBufferIndex() const
513 return mUpdateManager->GetEventBufferIndex();
516 void Core::ForceNextUpdate()
518 mForceNextUpdate = true;
521 bool Core::IsNextUpdateForced()
523 bool nextUpdateForced = mForceNextUpdate;
524 mForceNextUpdate = false;
525 return nextUpdateForced;
528 } // namespace Internal