Revert "[Tizen] Implement partial update"
[platform/core/uifw/dali-core.git] / dali / internal / common / core-impl.cpp
1 /*
2  * Copyright (c) 2020 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/integration-api/core.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/events/event.h>
25 #include <dali/integration-api/gl-sync-abstraction.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 #include <dali/integration-api/render-surface.h>
31
32 #include <dali/internal/event/actors/actor-impl.h>
33 #include <dali/internal/event/animation/animation-playlist.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/event-thread-services.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>
45
46 #include <dali/internal/update/common/discard-queue.h>
47 #include <dali/internal/update/manager/update-manager.h>
48 #include <dali/internal/update/manager/render-task-processor.h>
49
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>
53
54 using Dali::Internal::SceneGraph::UpdateManager;
55 using Dali::Internal::SceneGraph::RenderManager;
56 using Dali::Internal::SceneGraph::DiscardQueue;
57 using Dali::Internal::SceneGraph::RenderQueue;
58
59 namespace
60 {
61 // The Update for frame N+1 may be processed whilst frame N is being rendered.
62 const uint32_t MAXIMUM_UPDATE_COUNT = 2u;
63
64 #if defined(DEBUG_ENABLED)
65 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
66 #endif
67 }
68
69 namespace Dali
70 {
71
72 namespace Internal
73 {
74
75 using Integration::RenderController;
76 using Integration::PlatformAbstraction;
77 using Integration::GlSyncAbstraction;
78 using Integration::GlAbstraction;
79 using Integration::GlContextHelperAbstraction;
80 using Integration::Event;
81 using Integration::UpdateStatus;
82 using Integration::RenderStatus;
83
84 Core::Core( RenderController& renderController,
85             PlatformAbstraction& platform,
86             GlAbstraction& glAbstraction,
87             GlSyncAbstraction& glSyncAbstraction,
88             GlContextHelperAbstraction& glContextHelperAbstraction,
89             Integration::RenderToFrameBuffer renderToFboEnabled,
90             Integration::DepthBufferAvailable depthBufferAvailable,
91             Integration::StencilBufferAvailable stencilBufferAvailable )
92 : mRenderController( renderController ),
93   mPlatform(platform),
94   mProcessingEvent(false),
95   mForceNextUpdate( false )
96 {
97   // Create the thread local storage
98   CreateThreadLocalStorage();
99
100   // This does nothing until Core is built with --enable-performance-monitor
101   PERFORMANCE_MONITOR_INIT( platform );
102
103   mNotificationManager = new NotificationManager();
104
105   mAnimationPlaylist = AnimationPlaylist::New();
106
107   mPropertyNotificationManager = PropertyNotificationManager::New();
108
109   mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
110
111   mRenderManager = RenderManager::New( glAbstraction, glSyncAbstraction, glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable );
112
113   RenderQueue& renderQueue = mRenderManager->GetRenderQueue();
114
115   mDiscardQueue = new DiscardQueue( renderQueue );
116
117   mUpdateManager = new UpdateManager( *mNotificationManager,
118                                       *mAnimationPlaylist,
119                                       *mPropertyNotificationManager,
120                                       *mDiscardQueue,
121                                        renderController,
122                                       *mRenderManager,
123                                        renderQueue,
124                                       *mRenderTaskProcessor );
125
126   mRenderManager->SetShaderSaver( *mUpdateManager );
127
128   mObjectRegistry = ObjectRegistry::New();
129
130   mStage = IntrusivePtr<Stage>( Stage::New( *mUpdateManager ) );
131
132   // This must be called after stage is created but before stage initialization
133   mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
134
135   mGestureEventProcessor = new GestureEventProcessor( *mUpdateManager, mRenderController );
136
137   mShaderFactory = new ShaderFactory();
138   mUpdateManager->SetShaderSaver( *mShaderFactory );
139
140   GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
141 }
142
143 Core::~Core()
144 {
145   /*
146    * The order of destructing these singletons is important!!!
147    */
148
149   // clear the thread local storage first
150   // allows core to be created / deleted many times in the same thread (how TET cases work).
151   // Do this before mStage.Reset() so Stage::IsInstalled() returns false
152   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
153   if( tls )
154   {
155     tls->Remove();
156     tls->Unreference();
157   }
158
159   mObjectRegistry.Reset();
160
161   // Stop relayout requests being raised on stage destruction
162   mRelayoutController.Reset();
163
164   // remove (last?) reference to stage
165   mStage.Reset();
166
167 }
168
169 void Core::Initialize()
170 {
171   mStage->Initialize( *mScenes[0] );
172 }
173
174 Integration::ContextNotifierInterface* Core::GetContextNotifier()
175 {
176   return mStage.Get();
177 }
178
179 void Core::RecoverFromContextLoss()
180 {
181   DALI_LOG_INFO(gCoreFilter, Debug::Verbose, "Core::RecoverFromContextLoss()\n");
182
183   mStage->GetRenderTaskList().RecoverFromContextLoss(); // Re-trigger render-tasks
184 }
185
186 void Core::ContextCreated()
187 {
188   mRenderManager->ContextCreated();
189 }
190
191 void Core::ContextDestroyed()
192 {
193   mRenderManager->ContextDestroyed();
194 }
195
196 void Core::SurfaceDeleted( Integration::RenderSurface* surface )
197 {
198   for( auto scene : mScenes )
199   {
200     if( scene->GetSurface() == surface )
201     {
202       scene->SurfaceDeleted();
203       break;
204     }
205   }
206 }
207
208 void Core::Update( float elapsedSeconds, uint32_t lastVSyncTimeMilliseconds, uint32_t nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
209 {
210   // set the time delta so adaptor can easily print FPS with a release build with 0 as
211   // it is cached by frametime
212   status.secondsFromLastFrame = elapsedSeconds;
213
214   // Render returns true when there are updates on the stage or one or more animations are completed.
215   // Use the estimated time diff till we render as the elapsed time.
216   status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
217                                                 lastVSyncTimeMilliseconds,
218                                                 nextVSyncTimeMilliseconds,
219                                                 renderToFboEnabled,
220                                                 isRenderingToFbo );
221
222   // Check the Notification Manager message queue to set needsNotification
223   status.needsNotification = mNotificationManager->MessagesToProcess();
224
225   // Check if the default surface is changed
226   status.surfaceRectChanged = mUpdateManager->IsDefaultSurfaceRectChanged();
227
228   // No need to keep update running if there are notifications to process.
229   // Any message to update will wake it up anyways
230 }
231
232 void Core::Render( RenderStatus& status, bool forceClear, bool uploadOnly )
233 {
234   mRenderManager->Render( status, forceClear, uploadOnly );
235 }
236
237 void Core::SceneCreated()
238 {
239   mStage->EmitSceneCreatedSignal();
240
241   mRelayoutController->OnApplicationSceneCreated();
242
243   for( auto iter = mScenes.begin(); iter != mScenes.end(); ++iter )
244   {
245     Dali::Actor sceneRootLayer = (*iter)->GetRootLayer();
246     mRelayoutController->RequestRelayoutTree( sceneRootLayer );
247   }
248 }
249
250 void Core::QueueEvent( const Integration::Event& event )
251 {
252   if (mScenes.size() != 0)
253   {
254     mScenes.front()->QueueEvent( event );
255   }
256 }
257
258 void Core::ProcessEvents()
259 {
260   // Guard against calls to ProcessEvents() during ProcessEvents()
261   if( mProcessingEvent )
262   {
263     DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
264     mRenderController.RequestProcessEventsOnIdle( false );
265     return;
266   }
267
268   mProcessingEvent = true;
269   mRelayoutController->SetProcessingCoreEvents( true );
270
271   // Signal that any messages received will be flushed soon
272   mUpdateManager->EventProcessingStarted();
273
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;
277
278   // process events in all scenes
279   for( auto scene : scenes )
280   {
281     scene->ProcessEvents();
282   }
283
284   mNotificationManager->ProcessMessages();
285
286   // Emit signal here to inform listeners that event processing has finished.
287   for( auto scene : scenes )
288   {
289     scene->EmitEventProcessingFinishedSignal();
290   }
291
292   // Run any registered processors
293   RunProcessors();
294
295   // Run the size negotiation after event processing finished signal
296   mRelayoutController->Relayout();
297
298   // Rebuild depth tree after event processing has finished
299   for( auto scene : scenes )
300   {
301     scene->RebuildDepthTree();
302   }
303
304   // Flush any queued messages for the update-thread
305   const bool messagesToProcess = mUpdateManager->FlushQueue();
306
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();
311
312   if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
313   {
314     // tell the render controller to keep update thread running
315     mRenderController.RequestUpdate( forceUpdate );
316   }
317
318   mRelayoutController->SetProcessingCoreEvents( false );
319
320   // ProcessEvents() may now be called again
321   mProcessingEvent = false;
322 }
323
324 uint32_t Core::GetMaximumUpdateCount() const
325 {
326   return MAXIMUM_UPDATE_COUNT;
327 }
328
329 void Core::RegisterProcessor( Integration::Processor& processor )
330 {
331   mProcessors.PushBack(&processor);
332 }
333
334 void Core::UnregisterProcessor( Integration::Processor& processor )
335 {
336   auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
337   if( iter != mProcessors.End() )
338   {
339     mProcessors.Erase( iter );
340   }
341 }
342
343 void Core::RunProcessors()
344 {
345   // Copy processor pointers to prevent changes to vector affecting loop iterator.
346   Dali::Vector<Integration::Processor*> processors( mProcessors );
347
348   for( auto processor : processors )
349   {
350     if( processor )
351     {
352       processor->Process();
353     }
354   }
355 }
356
357 StagePtr Core::GetCurrentStage()
358 {
359   return mStage.Get();
360 }
361
362 PlatformAbstraction& Core::GetPlatform()
363 {
364   return mPlatform;
365 }
366
367 UpdateManager& Core::GetUpdateManager()
368 {
369   return *(mUpdateManager);
370 }
371
372 RenderManager& Core::GetRenderManager()
373 {
374   return *(mRenderManager);
375 }
376
377 NotificationManager& Core::GetNotificationManager()
378 {
379   return *(mNotificationManager);
380 }
381
382 ShaderFactory& Core::GetShaderFactory()
383 {
384   return *(mShaderFactory);
385 }
386
387 GestureEventProcessor& Core::GetGestureEventProcessor()
388 {
389   return *(mGestureEventProcessor);
390 }
391
392 RelayoutController& Core::GetRelayoutController()
393 {
394   return *(mRelayoutController.Get());
395 }
396
397 ObjectRegistry& Core::GetObjectRegistry() const
398 {
399   return *(mObjectRegistry.Get());
400 }
401
402 EventThreadServices& Core::GetEventThreadServices()
403 {
404   return *this;
405 }
406
407 PropertyNotificationManager& Core::GetPropertyNotificationManager() const
408 {
409   return *(mPropertyNotificationManager);
410 }
411
412 AnimationPlaylist& Core::GetAnimationPlaylist() const
413 {
414   return *(mAnimationPlaylist);
415 }
416
417 void Core::AddScene( Scene* scene )
418 {
419   mScenes.push_back( scene );
420 }
421
422 void Core::RemoveScene( Scene* scene )
423 {
424   auto iter = std::find( mScenes.begin(), mScenes.end(), scene );
425   if( iter != mScenes.end() )
426   {
427     mScenes.erase( iter );
428   }
429 }
430
431 void Core::CreateThreadLocalStorage()
432 {
433   // a pointer to the ThreadLocalStorage object will be stored in TLS
434   // The ThreadLocalStorage object should be deleted by the Core destructor
435   ThreadLocalStorage* tls = new ThreadLocalStorage(this);
436   tls->Reference();
437 }
438
439 void Core::RegisterObject( Dali::BaseObject* object )
440 {
441   mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
442   mObjectRegistry->RegisterObject( object );
443 }
444
445 void Core::UnregisterObject( Dali::BaseObject* object )
446 {
447   mObjectRegistry = &ThreadLocalStorage::Get().GetObjectRegistry();
448   mObjectRegistry->UnregisterObject( object );
449 }
450
451 Integration::RenderController& Core::GetRenderController()
452 {
453   return mRenderController;
454 }
455
456 uint32_t* Core::ReserveMessageSlot( uint32_t size, bool updateScene )
457 {
458   return mUpdateManager->ReserveMessageSlot( size, updateScene );
459 }
460
461 BufferIndex Core::GetEventBufferIndex() const
462 {
463   return mUpdateManager->GetEventBufferIndex();
464 }
465
466 void Core::ForceNextUpdate()
467 {
468   mForceNextUpdate = true;
469 }
470
471 bool Core::IsNextUpdateForced()
472 {
473   bool nextUpdateForced = mForceNextUpdate;
474   mForceNextUpdate = false;
475   return nextUpdateForced;
476 }
477
478 } // namespace Internal
479
480 } // namespace Dali