Support window resizing
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "application-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <style-monitor.h>
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28 #include <common/framework.h>
29 #include <singleton-service-impl.h>
30 #include <lifecycle-controller-impl.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::New(
52   int* argc,
53   char **argv[],
54   const std::string& stylesheet,
55   Dali::Application::WINDOW_MODE windowMode,
56   const PositionSize& positionSize,
57   Framework::Type applicationType)
58 {
59   ApplicationPtr application ( new Application (argc, argv, stylesheet, windowMode, positionSize, applicationType ) );
60   return application;
61 }
62
63 Application::Application( int* argc, char** argv[], const std::string& stylesheet,
64   Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType )
65 : mInitSignal(),
66   mTerminateSignal(),
67   mPauseSignal(),
68   mResumeSignal(),
69   mResetSignal(),
70   mResizeSignal(),
71   mAppControlSignal(),
72   mLanguageChangedSignal(),
73   mRegionChangedSignal(),
74   mBatteryLowSignal(),
75   mMemoryLowSignal(),
76   mEventLoop( NULL ),
77   mFramework( NULL ),
78   mContextLossConfiguration( Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS ),
79   mCommandLineOptions( NULL ),
80   mSingletonService( SingletonService::New() ),
81   mAdaptor( NULL ),
82   mWindow(),
83   mWindowMode( windowMode ),
84   mName(),
85   mStylesheet( stylesheet ),
86   mEnvironmentOptions(),
87   mWindowPositionSize( positionSize ),
88   mSlotDelegate( this )
89 {
90   // Get mName from environment options
91   mName = mEnvironmentOptions.GetWindowName();
92   if( mName.empty() && argc && ( *argc > 0 ) )
93   {
94     // Set mName from command-line args if environment option not set
95     mName = (*argv)[0];
96   }
97
98   mCommandLineOptions = new CommandLineOptions(argc, argv);
99   mFramework = new Framework( *this, argc, argv, applicationType );
100   mUseRemoteSurface = (applicationType == Framework::WATCH);
101 }
102
103 Application::~Application()
104 {
105   mSingletonService.UnregisterAll();
106
107   mWindow.Reset();
108   delete mAdaptor;
109   delete mCommandLineOptions;
110   delete mFramework;
111 }
112
113 void Application::CreateWindow()
114 {
115   if( mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0 )
116   {
117     if( mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0 )
118     {
119       // Command line options override environment options and full screen
120       mWindowPositionSize.width = mCommandLineOptions->stageWidth;
121       mWindowPositionSize.height = mCommandLineOptions->stageHeight;
122     }
123     else if( mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight() )
124     {
125       // Environment options override full screen functionality if command line arguments not provided
126       mWindowPositionSize.width = mEnvironmentOptions.GetWindowWidth();
127       mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
128     }
129   }
130
131   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
132   mWindow = Dali::Window::New( mWindowPositionSize, mName, windowClassName, mWindowMode == Dali::Application::TRANSPARENT );
133
134   // Quit the application when the window is closed
135   GetImplementation( mWindow ).DeleteRequestSignal().Connect( mSlotDelegate, &Application::Quit );
136 }
137
138 void Application::CreateAdaptor()
139 {
140   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
141
142   mAdaptor = Dali::Internal::Adaptor::Adaptor::New( mWindow, mContextLossConfiguration, &mEnvironmentOptions );
143
144   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
145
146   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetUseRemoteSurface( mUseRemoteSurface );
147 }
148
149 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
150 {
151   mContextLossConfiguration = configuration;
152
153   // Run the application
154   mFramework->Run();
155 }
156
157 void Application::Lower()
158 {
159   // Lower the application without quitting it.
160   mWindow.Lower();
161 }
162
163 void Application::Quit()
164 {
165   // Actually quit the application.
166   AddIdle( MakeCallback( this, &Application::QuitFromMainLoop ) );
167 }
168
169 void Application::QuitFromMainLoop()
170 {
171   mAdaptor->Stop();
172
173   mFramework->Quit();
174   // This will trigger OnTerminate(), below, after the main loop has completed.
175 }
176
177 void Application::DoInit()
178 {
179   CreateWindow();
180   CreateAdaptor();
181
182   // Run the adaptor
183   mAdaptor->Start();
184
185   // Check if user requires no vsyncing and set Adaptor
186   if (mCommandLineOptions->noVSyncOnRender)
187   {
188     mAdaptor->SetUseHardwareVSync(false);
189   }
190
191   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
192   if( mCommandLineOptions->viewMode != 0 )
193   {
194     ViewMode viewMode = MONO;
195     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
196     {
197       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
198     }
199     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
200   }
201
202   if( ! mStylesheet.empty() )
203   {
204     Dali::StyleMonitor::Get().SetTheme( mStylesheet );
205   }
206 }
207
208 void Application::DoStart()
209 {
210   mAdaptor->NotifySceneCreated();
211 }
212
213 void Application::DoTerminate()
214 {
215   if( mAdaptor )
216   {
217     // Ensure that the render-thread is not using the surface(window) after we delete it
218     mAdaptor->Stop();
219   }
220
221   mWindow.Reset();
222 }
223
224 void Application::DoPause()
225 {
226   mAdaptor->Pause();
227 }
228
229 void Application::DoResume()
230 {
231   mAdaptor->Resume();
232 }
233
234 void Application::DoLanguageChange()
235 {
236   mAdaptor->NotifyLanguageChanged();
237 }
238
239 void Application::OnInit()
240 {
241   mFramework->AddAbortCallback( MakeCallback( this, &Application::QuitFromMainLoop ) );
242
243   DoInit();
244
245   // Wire up the LifecycleController
246   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
247
248   InitSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnInit );
249   TerminateSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnTerminate );
250   PauseSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnPause );
251   ResumeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResume );
252   ResetSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnReset );
253   ResizeSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnResize );
254   LanguageChangedSignal().Connect( &GetImplementation( lifecycleController ), &LifecycleController::OnLanguageChanged );
255
256   Dali::Application application(this);
257   mInitSignal.Emit( application );
258
259   DoStart();
260 }
261
262 void Application::OnTerminate()
263 {
264   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
265   // delete the window as ecore_x has been destroyed by AppCore
266
267   Dali::Application application(this);
268   mTerminateSignal.Emit( application );
269
270   DoTerminate();
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
293 void Application::OnReset()
294 {
295   /*
296    * usually, reset callback was called when a caller request to launch this application via aul.
297    * because Application class already handled initialization in OnInit(), OnReset do nothing.
298    */
299   Dali::Application application(this);
300   mResetSignal.Emit( application );
301 }
302
303 void Application::OnAppControl(void *data)
304 {
305   Dali::Application application(this);
306   mAppControlSignal.Emit( application , data );
307 }
308
309 void Application::OnLanguageChanged()
310 {
311   DoLanguageChange();
312   Dali::Application application(this);
313   mLanguageChangedSignal.Emit( application );
314 }
315
316 void Application::OnRegionChanged()
317 {
318   Dali::Application application(this);
319   mRegionChangedSignal.Emit( application );
320 }
321
322 void Application::OnBatteryLow()
323 {
324   Dali::Application application(this);
325   mBatteryLowSignal.Emit( application );
326 }
327
328 void Application::OnMemoryLow()
329 {
330   Dali::Application application(this);
331   mMemoryLowSignal.Emit( application );
332 }
333
334 void Application::OnResize(Dali::Adaptor& adaptor)
335 {
336   Dali::Application application(this);
337   mResizeSignal.Emit( application );
338 }
339
340 bool Application::AddIdle( CallbackBase* callback )
341 {
342   return mAdaptor->AddIdle( callback );
343 }
344
345 Dali::Adaptor& Application::GetAdaptor()
346 {
347   return *mAdaptor;
348 }
349
350 Dali::Window Application::GetWindow()
351 {
352   return mWindow;
353 }
354
355 // Stereoscopy
356
357 void Application::SetViewMode( ViewMode viewMode )
358 {
359   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
360 }
361
362 ViewMode Application::GetViewMode() const
363 {
364   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
365 }
366
367 void Application::SetStereoBase( float stereoBase )
368 {
369   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
370 }
371
372 float Application::GetStereoBase() const
373 {
374   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
375 }
376
377
378 void Application::ReplaceWindow( const PositionSize& positionSize, const std::string& name )
379 {
380   Dali::Window newWindow = Dali::Window::New( positionSize, name, mWindowMode == Dali::Application::TRANSPARENT );
381   Window& windowImpl = GetImplementation(newWindow);
382   windowImpl.SetAdaptor(*mAdaptor);
383   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
384   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
385
386   Any nativeWindow = newWindow.GetNativeHandle();
387   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SurfaceSizeChanged( Dali::Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
388   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(nativeWindow, *renderSurface);
389   mWindow = newWindow;
390   mWindowPositionSize = positionSize;
391 }
392
393 std::string Application::GetResourcePath()
394 {
395   return Internal::Adaptor::Framework::GetResourcePath();
396 }
397
398 } // namespace Adaptor
399
400 } // namespace Internal
401
402 } // namespace Dali