[Vulkan] Builtin shaders and shaders offline compilation script
[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             bool renderToFboEnabled )
88 : mRenderController( renderController ),
89   mPlatform(platform),
90   mProcessingEvent(false),
91   mGraphics(graphics)
92 {
93   // fixme: for now to ensure libgraphics.a won't be removed during linking due to being
94   Integration::Graphics::IncludeThisLibrary();
95
96   GraphicsGetBuiltinShader("");
97
98   // Create the thread local storage
99   CreateThreadLocalStorage();
100
101   // This does nothing until Core is built with --enable-performance-monitor
102   PERFORMANCE_MONITOR_INIT( platform );
103
104   mNotificationManager = new NotificationManager();
105
106   mAnimationPlaylist = AnimationPlaylist::New();
107
108   mPropertyNotificationManager = PropertyNotificationManager::New();
109
110   mRenderTaskProcessor = new SceneGraph::RenderTaskProcessor();
111
112   mDiscardQueue = new DiscardQueue();
113
114   mUpdateManager = new UpdateManager( *mNotificationManager,
115                                       *mAnimationPlaylist,
116                                       *mPropertyNotificationManager,
117                                       *mDiscardQueue,
118                                        renderController,
119                                       *mRenderTaskProcessor,
120                                        graphics );
121
122
123   mStage = IntrusivePtr<Stage>( Stage::New( *mAnimationPlaylist, *mPropertyNotificationManager, *mUpdateManager, *mNotificationManager, mRenderController ) );
124
125   // This must be called after stage is created but before stage initialization
126   mRelayoutController = IntrusivePtr< RelayoutController >( new RelayoutController( mRenderController ) );
127
128   mStage->Initialize( renderToFboEnabled );
129
130   mGestureEventProcessor = new GestureEventProcessor( *mStage, *mUpdateManager, gestureManager, mRenderController );
131   mEventProcessor = new EventProcessor( *mStage, *mNotificationManager, *mGestureEventProcessor );
132
133   mShaderFactory = new ShaderFactory();
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     delete tls;
152   }
153
154   // Stop relayout requests being raised on stage destruction
155   mRelayoutController.Reset();
156
157   // Clean-up stage - remove default camera and root layer
158   mStage->Uninitialize();
159
160   // remove (last?) reference to stage
161   mStage.Reset();
162 }
163
164 void Core::SurfaceResized( unsigned int width, unsigned int height )
165 {
166   mStage->SurfaceResized( width, height );
167
168   // The stage-size may be less than surface-size (reduced by top-margin)
169   Vector2 size = mStage->GetSize();
170   mRelayoutController->SetStageSize( size.width, size.height );
171 }
172
173 void Core::SetTopMargin( unsigned int margin )
174 {
175   mStage->SetTopMargin( margin );
176
177   // The stage-size may be less than surface-size (reduced by top-margin)
178   Vector2 size = mStage->GetSize();
179   mRelayoutController->SetStageSize( size.width, size.height );
180 }
181
182 void Core::SetDpi( unsigned int dpiHorizontal, unsigned int dpiVertical )
183 {
184   mStage->SetDpi( Vector2( dpiHorizontal , dpiVertical) );
185 }
186
187 void Core::Update( float elapsedSeconds, unsigned int lastVSyncTimeMilliseconds, unsigned int nextVSyncTimeMilliseconds, Integration::UpdateStatus& status, bool renderToFboEnabled, bool isRenderingToFbo )
188 {
189   // set the time delta so adaptor can easily print FPS with a release build with 0 as
190   // it is cached by frametime
191   status.secondsFromLastFrame = elapsedSeconds;
192
193   // Render returns true when there are updates on the stage or one or more animations are completed.
194   // Use the estimated time diff till we render as the elapsed time.
195   status.keepUpdating = mUpdateManager->Update( elapsedSeconds,
196                                                 lastVSyncTimeMilliseconds,
197                                                 nextVSyncTimeMilliseconds,
198                                                 renderToFboEnabled,
199                                                 isRenderingToFbo );
200
201   // Check the Notification Manager message queue to set needsNotification
202   status.needsNotification = mNotificationManager->MessagesToProcess();
203
204   // No need to keep update running if there are notifications to process.
205   // Any message to update will wake it up anyways
206 }
207
208 void Core::Render( RenderStatus& status )
209 {
210   DALI_LOG_ERROR("Render()!");
211   (void)status;
212 }
213
214 void Core::SceneCreated()
215 {
216   mStage->EmitSceneCreatedSignal();
217
218   mRelayoutController->OnApplicationSceneCreated();
219 }
220
221 void Core::QueueEvent( const Integration::Event& event )
222 {
223   mEventProcessor->QueueEvent( event );
224 }
225
226 void Core::ProcessEvents()
227 {
228   // Guard against calls to ProcessEvents() during ProcessEvents()
229   if( mProcessingEvent )
230   {
231     DALI_LOG_ERROR( "ProcessEvents should not be called from within ProcessEvents!\n" );
232     mRenderController.RequestProcessEventsOnIdle( false );
233     return;
234   }
235
236   mProcessingEvent = true;
237   mRelayoutController->SetProcessingCoreEvents( true );
238
239   // Signal that any messages received will be flushed soon
240   mUpdateManager->EventProcessingStarted();
241
242   mEventProcessor->ProcessEvents();
243
244   mNotificationManager->ProcessMessages();
245
246   // Emit signal here to inform listeners that event processing has finished.
247   mStage->EmitEventProcessingFinishedSignal();
248
249   // Run the size negotiation after event processing finished signal
250   mRelayoutController->Relayout();
251
252   // Rebuild depth tree after event processing has finished
253   mStage->RebuildDepthTree();
254
255   // Flush any queued messages for the update-thread
256   const bool messagesToProcess = mUpdateManager->FlushQueue();
257
258   // Check if the touch or gestures require updates.
259   const bool gestureNeedsUpdate = mGestureEventProcessor->NeedsUpdate();
260   // Check if the next update is forced.
261   const bool forceUpdate = mStage->IsNextUpdateForced();
262
263   if( messagesToProcess || gestureNeedsUpdate || forceUpdate )
264   {
265     // tell the render controller to keep update thread running
266     mRenderController.RequestUpdate( forceUpdate );
267   }
268
269   mRelayoutController->SetProcessingCoreEvents( false );
270
271   // ProcessEvents() may now be called again
272   mProcessingEvent = false;
273 }
274
275 unsigned int Core::GetMaximumUpdateCount() const
276 {
277   return MAXIMUM_UPDATE_COUNT;
278 }
279
280 Integration::SystemOverlay& Core::GetSystemOverlay()
281 {
282   return mStage->GetSystemOverlay();
283 }
284
285 void Core::SetViewMode( ViewMode viewMode )
286 {
287   mStage->SetViewMode( viewMode );
288 }
289
290 ViewMode Core::GetViewMode() const
291 {
292   return mStage->GetViewMode();
293 }
294
295 void Core::SetStereoBase( float stereoBase )
296 {
297   mStage->SetStereoBase( stereoBase );
298 }
299
300 float Core::GetStereoBase() const
301 {
302   return mStage->GetStereoBase();
303 }
304
305 StagePtr Core::GetCurrentStage()
306 {
307   return mStage.Get();
308 }
309
310 PlatformAbstraction& Core::GetPlatform()
311 {
312   return mPlatform;
313 }
314
315 UpdateManager& Core::GetUpdateManager()
316 {
317   return *(mUpdateManager);
318 }
319
320
321 NotificationManager& Core::GetNotificationManager()
322 {
323   return *(mNotificationManager);
324 }
325
326 ShaderFactory& Core::GetShaderFactory()
327 {
328   return *(mShaderFactory);
329 }
330
331 GestureEventProcessor& Core::GetGestureEventProcessor()
332 {
333   return *(mGestureEventProcessor);
334 }
335
336 RelayoutController& Core::GetRelayoutController()
337 {
338   return *(mRelayoutController.Get());
339 }
340
341 void Core::CreateThreadLocalStorage()
342 {
343   // a pointer to the ThreadLocalStorage object will be stored in TLS
344   // The ThreadLocalStorage object should be deleted by the Core destructor
345   new ThreadLocalStorage(this);
346 }
347
348 } // namespace Internal
349
350 } // namespace Dali