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