Remove StereoMode
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / application-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/adaptor/common/application-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/adaptor-framework/style-monitor.h>
26 #include <dali/internal/system/common/command-line-options.h>
27 #include <dali/internal/adaptor/common/framework.h>
28 #include <dali/internal/system/common/singleton-service-impl.h>
29 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
30 #include <dali/internal/window-system/common/window-render-surface.h>
31
32 namespace Dali
33 {
34
35 namespace TizenPlatform
36 {
37 class TizenPlatformAbstraction;
38 }
39
40 namespace Integration
41 {
42 class Core;
43 }
44
45 namespace Internal
46 {
47
48 namespace Adaptor
49 {
50
51 ApplicationPtr Application::gPreInitializedApplication( NULL );
52
53 ApplicationPtr Application::New(
54   int* argc,
55   char **argv[],
56   const std::string& stylesheet,
57   Dali::Application::WINDOW_MODE windowMode,
58   const PositionSize& positionSize,
59   Framework::Type applicationType)
60 {
61   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
62   return application;
63 }
64
65 void Application::PreInitialize( int* argc, char** argv[] )
66 {
67   if( !gPreInitializedApplication )
68   {
69     gPreInitializedApplication = new Application ( argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL );
70
71     gPreInitializedApplication->CreateWindow();    // Only create window
72
73     gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
74   }
75 }
76
77 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
78   Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
79 : mInitSignal(),
80   mTerminateSignal(),
81   mPauseSignal(),
82   mResumeSignal(),
83   mResetSignal(),
84   mResizeSignal(),
85   mAppControlSignal(),
86   mLanguageChangedSignal(),
87   mRegionChangedSignal(),
88   mBatteryLowSignal(),
89   mMemoryLowSignal(),
90   mEventLoop( nullptr ),
91   mFramework( nullptr ),
92   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
93   mCommandLineOptions( nullptr ),
94   mSingletonService( SingletonService::New() ),
95   mAdaptorBuilder( nullptr ),
96   mAdaptor( nullptr ),
97   mMainWindow(),
98   mMainWindowMode( windowMode ),
99   mMainWindowName(),
100   mStylesheet( stylesheet ),
101   mEnvironmentOptions(),
102   mWindowPositionSize( positionSize ),
103   mLaunchpadState( Launchpad::NONE ),
104   mSlotDelegate( this )
105 {
106   // Get mName from environment options
107   mMainWindowName = mEnvironmentOptions.GetWindowName();
108   if( mMainWindowName.empty() && argc && ( *argc > 0 ) )
109   {
110     // Set mName from command-line args if environment option not set
111     mMainWindowName = (*argv)[0];
112   }
113
114   mCommandLineOptions = new CommandLineOptions(argc, argv);
115   mFramework = new Framework( *this, argc, argv, applicationType );
116   mUseRemoteSurface = (applicationType == Framework::WATCH);
117 }
118
119 Application::~Application()
120 {
121   mSingletonService.UnregisterAll();
122
123   mMainWindow.Reset();
124   delete mAdaptor;
125   delete mAdaptorBuilder;
126   delete mCommandLineOptions;
127   delete mFramework;
128 }
129
130 void Application::CreateWindow()
131 {
132   if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
133   {
134     if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
135     {
136       // Command line options override environment options and full screen
137       mWindowPositionSize.width = mCommandLineOptions->stageWidth;
138       mWindowPositionSize.height = mCommandLineOptions->stageHeight;
139     }
140     else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
141     {
142       // Environment options override full screen functionality if command line arguments not provided
143       mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
144       mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
145     }
146   }
147
148   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
149   mMainWindow = Dali::Window::New( mWindowPositionSize, mMainWindowName, windowClassName, mMainWindowMode == Dali::Application::TRANSPARENT );
150
151   int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
152   if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
153   {
154     GetImplementation( mMainWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
155   }
156
157   // Quit the application when the window is closed
158   GetImplementation( mMainWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
159 }
160
161 void Application::CreateAdaptor()
162 {
163   DALI_ASSERT_ALWAYS( mMainWindow && "Window required to create adaptor" );
164
165   auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
166
167   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( graphicsFactory, mMainWindow, mContextLossConfiguration, &mEnvironmentOptions );
168
169   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
170
171   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
172 }
173
174 void Application::CreateAdaptorBuilder()
175 {
176   mAdaptorBuilder = new AdaptorBuilder();
177 }
178
179 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
180 {
181   mContextLossConfiguration = configuration;
182
183   // Run the application
184   mFramework->Run();
185 }
186
187 void Application::Lower()
188 {
189   // Lower the application without quitting it.
190   mMainWindow.Lower();
191 }
192
193 void Application::Quit()
194 {
195   // Actually quit the application.
196   // Force a call to Quit even if adaptor is not running.
197   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ), false, true );
198 }
199
200 void Application::QuitFromMainLoop()
201 {
202   mAdaptor->Stop();
203
204   mFramework->Quit();
205   // This will trigger OnTerminate(), below, after the main loop has completed.
206 }
207
208 void Application::DoInit()
209 {
210   CreateAdaptorBuilder();
211
212   // If an application was pre-initialized, a window was made in advance
213   if( mLaunchpadState == Launchpad::NONE )
214   {
215     CreateWindow();
216   }
217
218   CreateAdaptor();
219
220   // Run the adaptor
221   mAdaptor->Start();
222
223   // Check if user requires no vsyncing and set Adaptor
224   if (mCommandLineOptions->noVSyncOnRender)
225   {
226     mAdaptor->SetUseHardwareVSync(false);
227   }
228
229   if( ! mStylesheet.empty() )
230   {
231     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
232   }
233 }
234
235 void Application::DoStart()
236 {
237   mAdaptor->NotifySceneCreated();
238 }
239
240 void Application::DoTerminate()
241 {
242   if( mAdaptor )
243   {
244     // Ensure that the render-thread is not using the surface(window) after we delete it
245     mAdaptor->Stop();
246   }
247
248   mMainWindow.Reset(); // This only resets (clears) the default Window
249 }
250
251 void Application::DoPause()
252 {
253   mAdaptor->Pause();
254 }
255
256 void Application::DoResume()
257 {
258   mAdaptor->Resume();
259 }
260
261 void Application::DoLanguageChange()
262 {
263   mAdaptor->NotifyLanguageChanged();
264 }
265
266 void Application::OnInit()
267 {
268   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
269
270   DoInit();
271
272   // Wire up the LifecycleController
273   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
274
275   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
276   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
277   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
278   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
279   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
280   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
281   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
282
283   Dali::Application application(this);
284   mInitSignal.Emit( application );
285
286   DoStart();
287 }
288
289 void Application::OnTerminate()
290 {
291   // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
292   // delete the window as ecore_x has been destroyed by AppCore
293
294   Dali::Application application(this);
295   mTerminateSignal.Emit( application );
296
297   DoTerminate();
298 }
299
300 void Application::OnPause()
301 {
302   // A DALi app should handle Pause/Resume events.
303   // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
304   // Pausing DALi core only occurs on the Window Hidden framework event
305   Dali::Application application(this);
306   mPauseSignal.Emit( application );
307 }
308
309 void Application::OnResume()
310 {
311   // Emit the signal first so the application can queue any messages before we do an update/render
312   // This ensures we do not just redraw the last frame before pausing if that's not required
313   Dali::Application application(this);
314   mResumeSignal.Emit( application );
315
316   // DALi just delivers the framework Resume event to the application.
317   // Resuming DALi core only occurs on the Window Show framework event
318
319   // Trigger processing of events queued up while paused
320   CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation( GetAdaptor() );
321   coreEventInterface.ProcessCoreEvents();
322 }
323
324 void Application::OnReset()
325 {
326   /*
327    * usually, reset callback was called when a caller request to launch this application via aul.
328    * because Application class already handled initialization in OnInit(), OnReset do nothing.
329    */
330   Dali::Application application(this);
331   mResetSignal.Emit( application );
332 }
333
334 void Application::OnAppControl(void *data)
335 {
336   Dali::Application application(this);
337   mAppControlSignal.Emit( application , data );
338 }
339
340 void Application::OnLanguageChanged()
341 {
342   DoLanguageChange();
343   Dali::Application application(this);
344   mLanguageChangedSignal.Emit( application );
345 }
346
347 void Application::OnRegionChanged()
348 {
349   Dali::Application application(this);
350   mRegionChangedSignal.Emit( application );
351 }
352
353 void Application::OnBatteryLow( Dali::DeviceStatus::Battery::Status status )
354 {
355   Dali::Application application(this);
356   mBatteryLowSignal.Emit( application );
357
358   mLowBatterySignal.Emit( status );
359 }
360
361 void Application::OnMemoryLow( Dali::DeviceStatus::Memory::Status status )
362 {
363   Dali::Application application(this);
364   mMemoryLowSignal.Emit( application );
365
366   mLowMemorySignal.Emit( status );
367 }
368 void Application::OnResize(Dali::Adaptor& adaptor)
369 {
370   Dali::Application application(this);
371   mResizeSignal.Emit( application );
372 }
373
374 bool Application::AddIdle( CallbackBase* callback, bool hasReturnValue )
375 {
376   return mAdaptor->AddIdle( callback, hasReturnValue );
377 }
378
379 std::string Application::GetRegion() const
380 {
381   return mFramework->GetRegion();
382 }
383
384 std::string Application::GetLanguage() const
385 {
386   return mFramework->GetLanguage();
387 }
388
389 Dali::Adaptor& Application::GetAdaptor()
390 {
391   return *mAdaptor;
392 }
393
394 Dali::Window Application::GetWindow()
395 {
396   return mMainWindow;
397 }
398
399 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
400 {
401   Dali::Window newWindow = Dali::Window::New( positionSize, name, mMainWindowMode == Dali::Application::TRANSPARENT );
402   Window& windowImpl = GetImplementation(newWindow);
403   windowImpl.SetAdaptor(*mAdaptor);
404
405   int indicatorVisibleMode = mEnvironmentOptions.GetIndicatorVisibleMode();
406   if( indicatorVisibleMode >= Dali::Window::INVISIBLE && indicatorVisibleMode <= Dali::Window::AUTO )
407   {
408     GetImplementation( newWindow ).SetIndicatorVisibleMode( static_cast< Dali::Window::IndicatorVisibleMode >( indicatorVisibleMode ) );
409   }
410
411   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
412
413   Any nativeWindow = newWindow.GetNativeHandle();
414   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
415   mMainWindow = newWindow;
416   mWindowPositionSize = positionSize;
417 }
418
419 std::string Application::GetResourcePath()
420 {
421   return Internal::Adaptor::Framework::GetResourcePath();
422 }
423
424 void Application::SetStyleSheet( const std::string& stylesheet )
425 {
426   mStylesheet = stylesheet;
427 }
428
429 ApplicationPtr Application::GetPreInitializedApplication()
430 {
431   return gPreInitializedApplication;
432 }
433
434 } // namespace Adaptor
435
436 } // namespace Internal
437
438 } // namespace Dali