[Tizen] Fix to do not update state of render task in case of uploadOnly
[platform/core/uifw/dali-core.git] / dali / internal / common / core-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/common/core-impl.h>
20
21 // INTERNAL INCLUDES
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
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>
44
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>
48
49 #include <dali/internal/render/common/performance-monitor.h>
50 #include <dali/internal/render/common/render-manager.h>
51
52 using Dali::Internal::SceneGraph::DiscardQueue;
53 using Dali::Internal::SceneGraph::RenderManager;
54 using Dali::Internal::SceneGraph::RenderQueue;
55 using Dali::Internal::SceneGraph::UpdateManager;
56
57 namespace
58 {
59 // The Update for frame N+1 may be processed whilst frame N is being rendered.
60 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
61
62 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
64 #endif
65 } // namespace
66
67 namespace Dali
68 {
69 namespace Internal
70 {
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;
78
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),
87   mPlatform(platform),
88   mGraphicsController(graphicsController),
89   mProcessingEvent(false),
90   mForceNextUpdate(false)
91 {
92   // Create the thread local storage
93   CreateThreadLocalStorage();
94
95   // This does nothing until Core is built with --enable-performance-monitor
96   PERFORMANCE_MONITOR_INIT(platform);
97
98   mNotificationManager = new NotificationManager();
99
100   mAnimationPlaylist = AnimationPlaylist::New();
101
102   mPropertyNotificationManager = PropertyNotificationManager::New();
103
104   mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
105
106   mRenderManager = RenderManager::New(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable);
107
108   RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
109
110   mDiscardQueue = new DiscardQueue(renderQueue);
111
112   mUpdateManager = new UpdateManager(*mNotificationManager,
113                                      *mAnimationPlaylist,
114                                      *mPropertyNotificationManager,
115                                      *mDiscardQueue,
116                                      renderController,
117                                      *mRenderManager,
118                                      renderQueue,
119                                      *mRenderTaskProcessor);
120
121   mRenderManager->SetShaderSaver(*mUpdateManager);
122
123   mObjectRegistry = ObjectRegistry::New();
124
125   mStage = IntrusivePtr<Stage>(Stage::New(*mUpdateManager));
126
127   // This must be called after stage is created but before stage initialization
128   mRelayoutController = IntrusivePtr<RelayoutController>(new RelayoutController(mRenderController));
129
130   mGestureEventProcessor = new GestureEventProcessor(*mUpdateManager, mRenderController);
131
132   mShaderFactory = new ShaderFactory();
133   mUpdateManager->SetShaderSaver(*mShaderFactory);
134
135   GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
136 }
137
138 Core::~Core()
139 {
140   /*
141    * The order of destructing these singletons is important!!!
142    */
143
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();
148   if(tls)
149   {
150     tls->Remove();
151     tls->Unreference();
152   }
153
154   mObjectRegistry.Reset();
155
156   // Stop relayout requests being raised on stage destruction
157   mRelayoutController.Reset();
158
159   // remove (last?) reference to stage
160   mStage.Reset();
161 }
162
163 void Core::Initialize()
164 {
165   mStage->Initialize(*mScenes[0]);
166 }
167
168 Integration::ContextNotifierInterface* Core::GetContextNotifier()
169 {
170   return mStage.Get();
171 }
172
173 void Core::RecoverFromContextLoss()
174 {
175   DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
176
177   mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
178 }
179
180 void Core::ContextCreated()
181 {
182 }
183
184 void Core::ContextDestroyed()
185 {
186 }
187
188 void Core::Update(float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo, bool uploadOnly)
189 {
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;
193
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,
199                                                renderToFboEnabled,
200                                                isRenderingToFbo,
201                                                uploadOnly);
202
203   // Check the Notification Manager message queue to set needsNotification
204   status.needsNotification = mNotificationManager->MessagesToProcess();
205
206   // No need to keep update running if there are notifications to process.
207   // Any message to update will wake it up anyways
208 }
209
210 void Core::PreRender(RenderStatus& status, bool forceClear)
211 {
212   mRenderManager->PreRender(status, forceClear);
213 }
214
215 void Core::PreRender(RenderStatus& status, Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
216 {
217   mRenderManager->PreRender(status, scene, damagedRects);
218 }
219
220 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
221 {
222   mRenderManager->RenderScene(status, scene, renderToFbo);
223 }
224
225 void Core::RenderScene(RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
226 {
227   mRenderManager->RenderScene(status, scene, renderToFbo, clippingRect);
228 }
229
230 void Core::PostRender()
231 {
232   mRenderManager->PostRender();
233 }
234
235 void Core::SceneCreated()
236 {
237   mStage->EmitSceneCreatedSignal();
238
239   mRelayoutController->OnApplicationSceneCreated();
240
241   for(const auto& scene : mScenes)
242   {
243     Dali::Actor sceneRootLayer = scene->GetRootLayer();
244     mRelayoutController->RequestRelayoutTree(sceneRootLayer);
245   }
246 }
247
248 void Core::QueueEvent(const Integration::Event& event)
249 {
250   if(mScenes.size() != 0)
251   {
252     mScenes.front()->QueueEvent(event);
253   }
254 }
255
256 void Core::ProcessEvents()
257 {
258   // Guard against calls to ProcessEvents() during ProcessEvents()
259   if(mProcessingEvent)
260   {
261     DALI_LOG_ERROR("ProcessEvents should not be called from within ProcessEvents!\n");
262     mRenderController.RequestProcessEventsOnIdle(false);
263     return;
264   }
265
266   mProcessingEvent = true;
267   mRelayoutController->SetProcessingCoreEvents(true);
268
269   // Signal that any messages received will be flushed soon
270   mUpdateManager->EventProcessingStarted();
271
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;
275
276   // process events in all scenes
277   for(auto scene : scenes)
278   {
279     scene->ProcessEvents();
280   }
281
282   mNotificationManager->ProcessMessages();
283
284   // Emit signal here to inform listeners that event processing has finished.
285   for(auto scene : scenes)
286   {
287     scene->EmitEventProcessingFinishedSignal();
288   }
289
290   // Run any registered processors
291   RunProcessors();
292
293   // Run the size negotiation after event processing finished signal
294   mRelayoutController->Relayout();
295
296   // Run any registered post processors
297   RunPostProcessors();
298
299   // Rebuild depth tree after event processing has finished
300   for(auto scene : scenes)
301   {
302     scene->RebuildDepthTree();
303   }
304
305   // Flush any queued messages for the update-thread
306   const bool messagesToProcess = mUpdateManager->FlushQueue();
307
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();
312
313   if(messagesToProcess || gestureNeedsUpdate || forceUpdate)
314   {
315     // tell the render controller to keep update thread running
316     mRenderController.RequestUpdate(forceUpdate);
317   }
318
319   mRelayoutController->SetProcessingCoreEvents(false);
320
321   // ProcessEvents() may now be called again
322   mProcessingEvent = false;
323 }
324
325 uint32_t Core::GetMaximumUpdateCount() const
326 {
327   return MAXIMUM_UPDATE_COUNT;
328 }
329
330 void Core::RegisterProcessor(Integration::Processor& processor, bool postProcessor)
331 {
332   if(postProcessor)
333   {
334     mPostProcessors.PushBack(&processor);
335   }
336   else
337   {
338     mProcessors.PushBack(&processor);
339   }
340 }
341
342 void Core::UnregisterProcessor(Integration::Processor& processor, bool postProcessor)
343 {
344   if(postProcessor)
345   {
346     auto iter = std::find(mPostProcessors.Begin(), mPostProcessors.End(), &processor);
347     if(iter != mPostProcessors.End())
348     {
349       mPostProcessors.Erase(iter);
350     }
351   }
352   else
353   {
354     auto iter = std::find(mProcessors.Begin(), mProcessors.End(), &processor);
355     if(iter != mProcessors.End())
356     {
357       mProcessors.Erase(iter);
358     }
359   }
360 }
361
362 void Core::RunProcessors()
363 {
364   // Copy processor pointers to prevent changes to vector affecting loop iterator.
365   Dali::Vector<Integration::Processor*> processors(mProcessors);
366
367   for(auto processor : processors)
368   {
369     if(processor)
370     {
371       processor->Process(false);
372     }
373   }
374 }
375
376 void Core::RunPostProcessors()
377 {
378   // Copy processor pointers to prevent changes to vector affecting loop iterator.
379   Dali::Vector<Integration::Processor*> processors(mPostProcessors);
380
381   for(auto processor : processors)
382   {
383     if(processor)
384     {
385       processor->Process(true);
386     }
387   }
388 }
389
390 StagePtr Core::GetCurrentStage()
391 {
392   return mStage.Get();
393 }
394
395 PlatformAbstraction& Core::GetPlatform()
396 {
397   return mPlatform;
398 }
399
400 UpdateManager& Core::GetUpdateManager()
401 {
402   return *(mUpdateManager);
403 }
404
405 RenderManager& Core::GetRenderManager()
406 {
407   return *(mRenderManager);
408 }
409
410 NotificationManager& Core::GetNotificationManager()
411 {
412   return *(mNotificationManager);
413 }
414
415 ShaderFactory& Core::GetShaderFactory()
416 {
417   return *(mShaderFactory);
418 }
419
420 GestureEventProcessor& Core::GetGestureEventProcessor()
421 {
422   return *(mGestureEventProcessor);
423 }
424
425 RelayoutController& Core::GetRelayoutController()
426 {
427   return *(mRelayoutController.Get());
428 }
429
430 ObjectRegistry& Core::GetObjectRegistry() const
431 {
432   return *(mObjectRegistry.Get());
433 }
434
435 EventThreadServices& Core::GetEventThreadServices()
436 {
437   return *this;
438 }
439
440 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
441 {
442   return *(mPropertyNotificationManager);
443 }
444
445 AnimationPlaylist& Core::GetAnimationPlaylist() const
446 {
447   return *(mAnimationPlaylist);
448 }
449
450 Integration::GlAbstraction& Core::GetGlAbstraction() const
451 {
452   return mGraphicsController.GetGlAbstraction();
453 }
454
455 void Core::AddScene(Scene* scene)
456 {
457   mScenes.push_back(scene);
458 }
459
460 void Core::RemoveScene(Scene* scene)
461 {
462   auto iter = std::find(mScenes.begin(), mScenes.end(), scene);
463   if(iter != mScenes.end())
464   {
465     mScenes.erase(iter);
466   }
467 }
468
469 void Core::CreateThreadLocalStorage()
470 {
471   // a pointer to the ThreadLocalStorage object will be stored in TLS
472   // The ThreadLocalStorage object should be deleted by the Core destructor
473   ThreadLocalStorage* tls = new ThreadLocalStorage(this);
474   tls->Reference();
475 }
476
477 void Core::RegisterObject(Dali::BaseObject* object)
478 {
479   mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
480   mObjectRegistry->RegisterObject(object);
481 }
482
483 void Core::UnregisterObject(Dali::BaseObject* object)
484 {
485   mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
486   mObjectRegistry->UnregisterObject(object);
487 }
488
489 Integration::RenderController& Core::GetRenderController()
490 {
491   return mRenderController;
492 }
493
494 uint32_t* Core::ReserveMessageSlot(uint32_t size, bool updateScene)
495 {
496   return mUpdateManager->ReserveMessageSlot(size, updateScene);
497 }
498
499 BufferIndex Core::GetEventBufferIndex() const
500 {
501   return mUpdateManager->GetEventBufferIndex();
502 }
503
504 void Core::ForceNextUpdate()
505 {
506   mForceNextUpdate = true;
507 }
508
509 bool Core::IsNextUpdateForced()
510 {
511   bool nextUpdateForced = mForceNextUpdate;
512   mForceNextUpdate      = false;
513   return nextUpdateForced;
514 }
515
516 } // namespace Internal
517
518 } // namespace Dali