Fixing up Vulkan branch after merge to dali_1.3.26
[platform/core/uifw/dali-core.git] / dali / internal / common / core-impl.cpp
1 /*
2  * Copyright (c) 2018 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
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/graphics/graphics.h>
27 #include <dali/integration-api/platform-abstraction.h>
28 #include <dali/integration-api/render-controller.h>
29 #include <dali/integration-api/system-overlay.h>
30
31 #include <dali/internal/common/performance-monitor.h>
32
33 #include <dali/internal/event/actors/actor-impl.h>
34 #include <dali/internal/event/animation/animation-playlist.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>
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 extern "C"
51 {
52 std::vector<uint32_t> GraphicsGetBuiltinShader( const std::string& tag );
53 }
54
55 using Dali::Internal::SceneGraph::UpdateManager;
56 using Dali::Internal::SceneGraph::DiscardQueue;
57
58 namespace
59 {
60 // The Update for frame N+1 may be processed whilst frame N is being rendered.
61 const unsigned int MAXIMUM_UPDATE_COUNT = 2u;
62
63 #if defined(DEBUG_ENABLED)
64 Debug::Filter* gCoreFilter = Debug::Filter::New(Debug::Concise, false, "LOG_CORE");
65 #endif
66 }
67
68 namespace Dali
69 {
70 namespace Internal
71 {
72
73 using Integration::RenderController;
74 using Integration::PlatformAbstraction;
75 using Integration::GestureManager;
76 using Integration::Event;
77 using Integration::UpdateStatus;
78 using Integration::RenderStatus;
79 using Integration::Graphics::Graphics;
80
81
82 Core::Core( RenderController& renderController,
83             PlatformAbstraction& platform,
84             Graphics& graphics,
85             GestureManager& gestureManager,
86             ResourcePolicy::DataRetention dataRetentionPolicy,
87             Integration::RenderToFrameBuffer renderToFboEnabled,
88             Integration::DepthBufferAvailable depthBufferAvailable,
89             Integration::StencilBufferAvailable stencilBufferAvailable )
90 : mRenderController( renderController ),
91   mPlatform(platform),
92   mProcessingEvent(false),
93   mGraphics(graphics)
94 {
95   // fixme: for now to ensure libgraphics.a won't be removed during linking due to being
96   Integration::Graphics::IncludeThisLibrary();
97
98   GraphicsGetBuiltinShader("");
99
100   // Create the thread local storage
101   CreateThreadLocalStorage();
102
103   // This does nothing until Core is built with --enable-performance-monitor
104   PERFORMANCE_MONITOR_INIT( platform );
105
106   mNotificationManager = new NotificationManager();
107
108   mAnimationPlaylist = AnimationPlaylist::New();
109
110   mPropertyNotificationManager = PropertyNotificationManager::New();
111
112   mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
113
114   mDiscardQueue = new DiscardQueue();
115
116   mUpdateManager = new UpdateManager( *mNotificationManager,
117                                       *mAnimationPlaylist,
118                                       *mPropertyNotificationManager,
119                                       *mDiscardQueue,
120                                        renderController,
121                                       *mRenderTaskProcessor,
122                                        graphics );
123
124
125   mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
126
127   // This must be called after stage is created but before stage initialization
128   mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
129
130   mStage->Initialize( renderToFboEnabled == Integration::RenderToFrameBuffer::TRUE );
131
132   mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
133   mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
134
135   mShaderFactory = new ShaderFactory();
136
137   GetImplementation(Dali::TypeRegistry::Get()).CallInitFunctions();
138 }
139
140 Core::~Core()
141 {
142   /*
143    * The order of destructing these singletons is important!!!
144    */
145
146   // clear the thread local storage first
147   // allows core to be created / deleted many times in the same thread (how TET cases work).
148   // Do this before mStage.Reset() so Stage::IsInstalled() returns false
149   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
150   if( tls )
151   {
152     tls->Remove();
153     delete tls;
154   }
155
156   // Stop relayout requests being raised on stage destruction
157   mRelayoutController.Reset();
158
159   // Clean-up stage - remove default camera and root layer
160   mStage->Uninitialize();
161
162   // remove (last?) reference to stage
163   mStage.Reset();
164 }
165
166 void Core::SurfaceResized( unsigned int width, unsigned int height )
167 {
168   mStage->SurfaceResized( width, height );
169
170   // The stage-size may be less than surface-size (reduced by top-margin)
171   Vector2 size = mStage->GetSize();
172   mRelayoutController->SetStageSize( size.width, size.height );
173 }
174
175 void Core::SetTopMargin( unsigned int margin )
176 {
177   mStage->SetTopMargin( margin );
178
179   // The stage-size may be less than surface-size (reduced by top-margin)
180   Vector2 size = mStage->GetSize();
181   mRelayoutController->SetStageSize( size.width, size.height );
182 }
183
184 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
185 {
186   mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
187 }
188
189 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
190 {
191   // set the time delta so adaptor can easily print FPS with a release build with 0 as
192   // it is cached by frametime
193   status.secondsFromLastFrame = elapsedSeconds;
194
195   // Render returns true when there are updates on the stage or one or more animations are completed.
196   // Use the estimated time diff till we render as the elapsed time.
197   status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
198                                                 lastVSyncTimeMilliseconds,
199                                                 nextVSyncTimeMilliseconds,
200                                                 renderToFboEnabled,
201                                                 isRenderingToFbo );
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::Render( RenderStatus& status, bool forceClear )
211 {
212   DALI_LOG_ERROR("Render()!\n");
213   (void)status;
214 }
215
216 void Core::SceneCreated()
217 {
218   mStage->EmitSceneCreatedSignal();
219
220   mRelayoutController->OnApplicationSceneCreated();
221 }
222
223 void Core::QueueEvent( const Integration::Event& event )
224 {
225   mEventProcessor->QueueEvent( event );
226 }
227
228 void Core::ProcessEvents()
229 {
230   // Guard against calls to ProcessEvents() during ProcessEvents()
231   if( mProcessingEvent )
232   {
233     DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
234     mRenderController.RequestProcessEventsOnIdle( false );
235     return;
236   }
237
238   mProcessingEvent = true;
239   mRelayoutController->SetProcessingCoreEvents( true );
240
241   // Signal that any messages received will be flushed soon
242   mUpdateManager->EventProcessingStarted();
243
244   mEventProcessor->ProcessEvents();
245
246   mNotificationManager->ProcessMessages();
247
248   // Emit signal here to inform listeners that event processing has finished.
249   mStage->EmitEventProcessingFinishedSignal();
250
251   // Run the size negotiation after event processing finished signal
252   mRelayoutController->Relayout();
253
254   // Run any registered processors
255   RunProcessors();
256
257   // Rebuild depth tree after event processing has finished
258   mStage->RebuildDepthTree();
259
260   // Flush any queued messages for the update-thread
261   const bool messagesToProcess = mUpdateManager->FlushQueue();
262
263   // Check if the touch or gestures require updates.
264   const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
265   // Check if the next update is forced.
266   const bool forceUpdate = mStage->IsNextUpdateForced();
267
268   if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
269   {
270     // tell the render controller to keep update thread running
271     mRenderController.RequestUpdate( forceUpdate );
272   }
273
274   mRelayoutController->SetProcessingCoreEvents( false );
275
276   // ProcessEvents() may now be called again
277   mProcessingEvent = false;
278 }
279
280 unsigned int Core::GetMaximumUpdateCount() const
281 {
282   return MAXIMUM_UPDATE_COUNT;
283 }
284
285 Integration::SystemOverlay& Core::GetSystemOverlay()
286 {
287   return mStage->GetSystemOverlay();
288 }
289
290 void Core::SetViewMode( ViewMode viewMode )
291 {
292   mStage->SetViewMode( viewMode );
293 }
294
295 ViewMode Core::GetViewMode() const
296 {
297   return mStage->GetViewMode();
298 }
299
300 void Core::SetStereoBase( float stereoBase )
301 {
302   mStage->SetStereoBase( stereoBase );
303 }
304
305 void Core::RegisterProcessor( Integration::Processor& processor )
306 {
307   mProcessors.PushBack(&processor);
308 }
309
310 void Core::UnregisterProcessor( Integration::Processor& processor )
311 {
312   auto iter = std::find( mProcessors.Begin(), mProcessors.End(), &processor );
313   if( iter != mProcessors.End() )
314   {
315     mProcessors.Erase( iter );
316   }
317 }
318
319 void Core::RunProcessors()
320 {
321   // Copy processor pointers to prevent changes to vector affecting loop iterator.
322   Dali::Vector<Integration::Processor*> processors( mProcessors );
323
324   for( auto processor : processors )
325   {
326     if( processor )
327     {
328       processor->Process();
329     }
330   }
331 }
332
333 float Core::GetStereoBase() const
334 {
335   return mStage->GetStereoBase();
336 }
337
338 StagePtr Core::GetCurrentStage()
339 {
340   return mStage.Get();
341 }
342
343 PlatformAbstraction& Core::GetPlatform()
344 {
345   return mPlatform;
346 }
347
348 UpdateManager& Core::GetUpdateManager()
349 {
350   return *(mUpdateManager);
351 }
352
353
354 NotificationManager& Core::GetNotificationManager()
355 {
356   return *(mNotificationManager);
357 }
358
359 ShaderFactory& Core::GetShaderFactory()
360 {
361   return *(mShaderFactory);
362 }
363
364 GestureEventProcessor& Core::GetGestureEventProcessor()
365 {
366   return *(mGestureEventProcessor);
367 }
368
369 RelayoutController& Core::GetRelayoutController()
370 {
371   return *(mRelayoutController.Get());
372 }
373
374 void Core::CreateThreadLocalStorage()
375 {
376   // a pointer to the ThreadLocalStorage object will be stored in TLS
377   // The ThreadLocalStorage object should be deleted by the Core destructor
378   new ThreadLocalStorage(this);
379 }
380
381 } // namespace Internal
382
383 } // namespace Dali