2 * Copyright (c) 2021 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)
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,
202 // Check the Notification Manager message queue to set needsNotification
203 status.needsNotification = mNotificationManager->MessagesToProcess();
205 // No need to keep update running if there are notifications to process.
206 // Any message to update will wake it up anyways
209 void Core::PreRender(RenderStatus& status, bool forceClear, bool uploadOnly)
211 mRenderManager->PreRender(status, forceClear, uploadOnly);
214 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
216 mRenderManager->PreRender(scene, damagedRects);
219 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
221 mRenderManager->RenderScene(status, scene, renderToFbo);
224 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
226 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
229 void Core::PostRender(bool uploadOnly)
231 mRenderManager->PostRender(uploadOnly);
234 void Core::SceneCreated()
236 mStage->EmitSceneCreatedSignal();
238 mRelayoutController->OnApplicationSceneCreated();
240 for(const auto& scene : mScenes)
242 Dali::Actor sceneRootLayer = scene->GetRootLayer();
243 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
247 void Core::QueueEvent(const Integration::Event& event)
249 if(mScenes.size() != 0)
251 mScenes.front()->QueueEvent(event);
255 void Core::ProcessEvents()
257 // Guard against calls to ProcessEvents() during ProcessEvents()
260 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
261 mRenderController.RequestProcessEventsOnIdle(false);
265 mProcessingEvent = true;
266 mRelayoutController->SetProcessingCoreEvents(true);
268 // Signal that any messages received will be flushed soon
269 mUpdateManager->EventProcessingStarted();
271 // Scene could be added or removed while processing the events
272 // Copy the Scene container locally to avoid possibly invalid iterator
273 SceneContainer scenes = mScenes;
275 // process events in all scenes
276 for(auto scene : scenes)
278 scene->ProcessEvents();
281 mNotificationManager->ProcessMessages();
283 // Emit signal here to inform listeners that event processing has finished.
284 for(auto scene : scenes)
286 scene->EmitEventProcessingFinishedSignal();
289 // Run any registered processors
292 // Run the size negotiation after event processing finished signal
293 mRelayoutController->Relayout();
295 // Run any registered post processors
298 // Rebuild depth tree after event processing has finished
299 for(auto scene : scenes)
301 scene->RebuildDepthTree();
304 // Flush any queued messages for the update-thread
305 const bool messagesToProcess = mUpdateManager->FlushQueue();
307 // Check if the touch or gestures require updates.
308 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
309 // Check if the next update is forced.
310 const bool forceUpdate = IsNextUpdateForced();
312 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
314 // tell the render controller to keep update thread running
315 mRenderController.RequestUpdate(forceUpdate);
318 mRelayoutController->SetProcessingCoreEvents(false);
320 // ProcessEvents() may now be called again
321 mProcessingEvent = false;
324 uint32_t Core::GetMaximumUpdateCount() const
326 return MAXIMUM_UPDATE_COUNT;
329 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
333 mPostProcessors.PushBack(&processor);
337 mProcessors.PushBack(&processor);
341 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
345 auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
346 if(iter != mPostProcessors.End())
348 mPostProcessors.Erase(iter);
353 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
354 if(iter != mProcessors.End())
356 mProcessors.Erase(iter);
361 void Core::RunProcessors()
363 // Copy processor pointers to prevent changes to vector affecting loop iterator.
364 Dali::Vector<Integration::Processor*> processors(mProcessors);
366 for(auto processor : processors)
370 processor->Process(false);
375 void Core::RunPostProcessors()
377 // Copy processor pointers to prevent changes to vector affecting loop iterator.
378 Dali::Vector<Integration::Processor*> processors(mPostProcessors);
380 for(auto processor : processors)
384 processor->Process(true);
389 StagePtr Core::GetCurrentStage()
394 PlatformAbstraction& Core::GetPlatform()
399 UpdateManager& Core::GetUpdateManager()
401 return *(mUpdateManager);
404 RenderManager& Core::GetRenderManager()
406 return *(mRenderManager);
409 NotificationManager& Core::GetNotificationManager()
411 return *(mNotificationManager);
414 ShaderFactory& Core::GetShaderFactory()
416 return *(mShaderFactory);
419 GestureEventProcessor& Core::GetGestureEventProcessor()
421 return *(mGestureEventProcessor);
424 RelayoutController& Core::GetRelayoutController()
426 return *(mRelayoutController.Get());
429 ObjectRegistry& Core::GetObjectRegistry() const
431 return *(mObjectRegistry.Get());
434 EventThreadServices& Core::GetEventThreadServices()
439 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
441 return *(mPropertyNotificationManager);
444 AnimationPlaylist& Core::GetAnimationPlaylist() const
446 return *(mAnimationPlaylist);
449 Integration::GlAbstraction& Core::GetGlAbstraction() const
451 return mGraphicsController.GetGlAbstraction();
454 void Core::AddScene(Scene* scene)
456 mScenes.push_back(scene);
459 void Core::RemoveScene(Scene* scene)
461 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
462 if(iter != mScenes.end())
468 void Core::CreateThreadLocalStorage()
470 // a pointer to the ThreadLocalStorage object will be stored in TLS
471 // The ThreadLocalStorage object should be deleted by the Core destructor
472 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
476 void Core::RegisterObject(Dali::BaseObject* object)
478 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
479 mObjectRegistry->RegisterObject(object);
482 void Core::UnregisterObject(Dali::BaseObject* object)
484 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
485 mObjectRegistry->UnregisterObject(object);
488 Integration::RenderController& Core::GetRenderController()
490 return mRenderController;
493 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
495 return mUpdateManager->ReserveMessageSlot(size, updateScene);
498 BufferIndex Core::GetEventBufferIndex() const
500 return mUpdateManager->GetEventBufferIndex();
503 void Core::ForceNextUpdate()
505 mForceNextUpdate = true;
508 bool Core::IsNextUpdateForced()
510 bool nextUpdateForced = mForceNextUpdate;
511 mForceNextUpdate = false;
512 return nextUpdateForced;
515 } // namespace Internal