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/gl-sync-abstraction.h>
28 #include <dali/integration-api/platform-abstraction.h>
29 #include <dali/integration-api/processor-interface.h>
30 #include <dali/integration-api/render-controller.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>
52 #include <dali/internal/render/gl-resources/context.h>
54 using Dali::Internal::SceneGraph::DiscardQueue;
55 using Dali::Internal::SceneGraph::RenderManager;
56 using Dali::Internal::SceneGraph::RenderQueue;
57 using Dali::Internal::SceneGraph::UpdateManager;
61 // The Update for frame N+1 may be processed whilst frame N is being rendered.
62 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
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::GlSyncAbstraction;
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)
95 // Create the thread local storage
96 CreateThreadLocalStorage();
98 // This does nothing until Core is built with --enable-performance-monitor
99 PERFORMANCE_MONITOR_INIT(platform);
101 mNotificationManager = new NotificationManager();
103 mAnimationPlaylist = AnimationPlaylist::New();
105 mPropertyNotificationManager = PropertyNotificationManager::New();
107 mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
109 mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
111 RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
113 mDiscardQueue = new DiscardQueue(renderQueue);
115 mUpdateManager = new UpdateManager(*mNotificationManager,
117 *mPropertyNotificationManager,
122 *mRenderTaskProcessor);
124 mRenderManager->SetShaderSaver(*mUpdateManager);
126 mObjectRegistry = ObjectRegistry::New();
128 mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
130 // This must be called after stage is created but before stage initialization
131 mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
133 mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
135 mShaderFactory = new ShaderFactory();
136 mUpdateManager->SetShaderSaver(*mShaderFactory);
138 GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
144 * The order of destructing these singletons is important!!!
147 // clear the thread local storage first
148 // allows core to be created / deleted many times in the same thread (how TET cases work).
149 // Do this before mStage.Reset() so Stage::IsInstalled() returns false
150 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
157 mObjectRegistry.Reset();
159 // Stop relayout requests being raised on stage destruction
160 mRelayoutController.Reset();
162 // remove (last?) reference to stage
166 void Core::Initialize()
168 mStage->Initialize(*mScenes[0]);
171 Integration::ContextNotifierInterface* Core::GetContextNotifier()
176 void Core::RecoverFromContextLoss()
178 DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
180 mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
183 void Core::ContextCreated()
185 mRenderManager->ContextCreated();
188 void Core::ContextDestroyed()
190 mRenderManager->ContextDestroyed();
193 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo)
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,
207 // Check the Notification Manager message queue to set needsNotification
208 status.needsNotification = mNotificationManager->MessagesToProcess();
210 // Check if the default surface is changed
211 status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
213 // No need to keep update running if there are notifications to process.
214 // Any message to update will wake it up anyways
217 void Core::PreRender(RenderStatus& status, bool forceClear, bool uploadOnly)
219 mRenderManager->PreRender(status, forceClear, uploadOnly);
222 void Core::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
224 mRenderManager->PreRender(scene, damagedRects);
227 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
229 mRenderManager->RenderScene(status, scene, renderToFbo);
232 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
234 mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
237 void Core::PostRender(bool uploadOnly)
239 mRenderManager->PostRender(uploadOnly);
242 void Core::SceneCreated()
244 mStage->EmitSceneCreatedSignal();
246 mRelayoutController->OnApplicationSceneCreated();
248 for(const auto& scene : mScenes)
250 Dali::Actor sceneRootLayer = scene->GetRootLayer();
251 mRelayoutController->RequestRelayoutTree(sceneRootLayer);
255 void Core::QueueEvent(const Integration::Event& event)
257 if(mScenes.size() != 0)
259 mScenes.front()->QueueEvent(event);
263 void Core::ProcessEvents()
265 // Guard against calls to ProcessEvents() during ProcessEvents()
268 DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
269 mRenderController.RequestProcessEventsOnIdle(false);
273 mProcessingEvent = true;
274 mRelayoutController->SetProcessingCoreEvents(true);
276 // Signal that any messages received will be flushed soon
277 mUpdateManager->EventProcessingStarted();
279 // Scene could be added or removed while processing the events
280 // Copy the Scene container locally to avoid possibly invalid iterator
281 SceneContainer scenes = mScenes;
283 // process events in all scenes
284 for(auto scene : scenes)
286 scene->ProcessEvents();
289 mNotificationManager->ProcessMessages();
291 // Emit signal here to inform listeners that event processing has finished.
292 for(auto scene : scenes)
294 scene->EmitEventProcessingFinishedSignal();
297 // Run any registered processors
300 // Run the size negotiation after event processing finished signal
301 mRelayoutController->Relayout();
303 // Rebuild depth tree after event processing has finished
304 for(auto scene : scenes)
306 scene->RebuildDepthTree();
309 // Flush any queued messages for the update-thread
310 const bool messagesToProcess = mUpdateManager->FlushQueue();
312 // Check if the touch or gestures require updates.
313 const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
314 // Check if the next update is forced.
315 const bool forceUpdate = IsNextUpdateForced();
317 if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
319 // tell the render controller to keep update thread running
320 mRenderController.RequestUpdate(forceUpdate);
323 mRelayoutController->SetProcessingCoreEvents(false);
325 // ProcessEvents() may now be called again
326 mProcessingEvent = false;
329 uint32_t Core::GetMaximumUpdateCount() const
331 return MAXIMUM_UPDATE_COUNT;
334 void Core::RegisterProcessor(Integration::Processor& processor)
336 mProcessors.PushBack(&processor);
339 void Core::UnregisterProcessor(Integration::Processor& processor)
341 auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
342 if(iter != mProcessors.End())
344 mProcessors.Erase(iter);
348 void Core::RunProcessors()
350 // Copy processor pointers to prevent changes to vector affecting loop iterator.
351 Dali::Vector<Integration::Processor*> processors(mProcessors);
353 for(auto processor : processors)
357 processor->Process();
362 StagePtr Core::GetCurrentStage()
367 PlatformAbstraction& Core::GetPlatform()
372 UpdateManager& Core::GetUpdateManager()
374 return *(mUpdateManager);
377 RenderManager& Core::GetRenderManager()
379 return *(mRenderManager);
382 NotificationManager& Core::GetNotificationManager()
384 return *(mNotificationManager);
387 ShaderFactory& Core::GetShaderFactory()
389 return *(mShaderFactory);
392 GestureEventProcessor& Core::GetGestureEventProcessor()
394 return *(mGestureEventProcessor);
397 RelayoutController& Core::GetRelayoutController()
399 return *(mRelayoutController.Get());
402 ObjectRegistry& Core::GetObjectRegistry() const
404 return *(mObjectRegistry.Get());
407 EventThreadServices& Core::GetEventThreadServices()
412 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
414 return *(mPropertyNotificationManager);
417 AnimationPlaylist& Core::GetAnimationPlaylist() const
419 return *(mAnimationPlaylist);
422 Integration::GlAbstraction& Core::GetGlAbstraction() const
424 return mGraphicsController.GetGlAbstraction();
427 void Core::AddScene(Scene* scene)
429 mScenes.push_back(scene);
432 void Core::RemoveScene(Scene* scene)
434 auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
435 if(iter != mScenes.end())
441 void Core::CreateThreadLocalStorage()
443 // a pointer to the ThreadLocalStorage object will be stored in TLS
444 // The ThreadLocalStorage object should be deleted by the Core destructor
445 ThreadLocalStorage* tls = new ThreadLocalStorage(this);
449 void Core::RegisterObject(Dali::BaseObject* object)
451 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
452 mObjectRegistry->RegisterObject(object);
455 void Core::UnregisterObject(Dali::BaseObject* object)
457 mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
458 mObjectRegistry->UnregisterObject(object);
461 Integration::RenderController& Core::GetRenderController()
463 return mRenderController;
466 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
468 return mUpdateManager->ReserveMessageSlot(size, updateScene);
471 BufferIndex Core::GetEventBufferIndex() const
473 return mUpdateManager->GetEventBufferIndex();
476 void Core::ForceNextUpdate()
478 mForceNextUpdate = true;
481 bool Core::IsNextUpdateForced()
483 bool nextUpdateForced = mForceNextUpdate;
484 mForceNextUpdate = false;
485 return nextUpdateForced;
488 } // namespace Internal