Add handling for context loss and regain behaviour
[platform/core/uifw/dali-adaptor.git] / adaptors / common / application-impl.cpp
1 /*
2  * Copyright (c) 2014 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 #include <style-monitor.h>
24
25 // INTERNAL INCLUDES
26 #include <command-line-options.h>
27 #include <common/adaptor-impl.h>
28 #include <singleton-service-impl.h>
29
30 namespace Dali
31 {
32
33 namespace SlpPlatform
34 {
35 class SlpPlatformAbstraction;
36 }
37
38 namespace Integration
39 {
40 class Core;
41 }
42
43 namespace Internal
44 {
45
46 namespace Adaptor
47 {
48
49 namespace
50 {
51 // Defaults taken from H2 device
52 const unsigned int DEFAULT_WINDOW_WIDTH   = 480;
53 const unsigned int DEFAULT_WINDOW_HEIGHT  = 800;
54 const float        DEFAULT_HORIZONTAL_DPI = 220;
55 const float        DEFAULT_VERTICAL_DPI   = 217;
56 }
57
58 ApplicationPtr Application::New(
59   int* argc,
60   char **argv[],
61   const std::string& name,
62   const DeviceLayout& baseLayout,
63   Dali::Application::WINDOW_MODE windowMode)
64 {
65   ApplicationPtr application ( new Application (argc, argv, name, baseLayout, windowMode ) );
66   return application;
67 }
68
69 Application::Application( int* argc, char** argv[], const std::string& name, const DeviceLayout& baseLayout, Dali::Application::WINDOW_MODE windowMode)
70 : mInitSignalV2(),
71   mTerminateSignalV2(),
72   mPauseSignalV2(),
73   mResumeSignalV2(),
74   mResetSignalV2(),
75   mResizeSignalV2(),
76   mLanguageChangedSignalV2(),
77   mEventLoop( NULL ),
78   mFramework( NULL ),
79   mCommandLineOptions( NULL ),
80   mSingletonService( SingletonService::New() ),
81   mAdaptor( NULL ),
82   mWindow(),
83   mWindowMode( windowMode ),
84   mName( name ),
85   mInitialized( false ),
86   mBaseLayout( baseLayout ),
87   mSlotDelegate( this )
88 {
89   mCommandLineOptions = new CommandLineOptions(argc, argv);
90
91   mFramework = new Framework(*this, argc, argv, name);
92 }
93
94 Application::~Application()
95 {
96   delete mFramework;
97   delete mCommandLineOptions;
98   delete mAdaptor;
99   mWindow.Reset();
100 }
101
102 void Application::CreateWindow()
103 {
104 #ifndef __arm__
105    PositionSize windowPosition(0, 0, DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
106 #else
107    PositionSize windowPosition(0, 0, 0, 0);  // this will use full screen
108 #endif
109   if (mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
110   {
111     // let the command line options over ride
112     windowPosition = PositionSize(0,0,mCommandLineOptions->stageWidth,mCommandLineOptions->stageHeight);
113   }
114
115   mWindow = Dali::Window::New( windowPosition, mName, mWindowMode == Dali::Application::TRANSPARENT );
116 }
117
118 void Application::CreateAdaptor()
119 {
120   DALI_ASSERT_ALWAYS( mWindow && "Window required to create adaptor" );
121
122   mAdaptor = &Dali::Adaptor::New( mWindow, mBaseLayout, mContextLossConfiguration );
123
124   // Allow DPI to be overridden from command line.
125   unsigned int hDPI=DEFAULT_HORIZONTAL_DPI;
126   unsigned int vDPI=DEFAULT_VERTICAL_DPI;
127
128   std::string dpiStr = mCommandLineOptions->stageDPI;
129   if(!dpiStr.empty())
130   {
131     sscanf(dpiStr.c_str(), "%ux%u", &hDPI, &vDPI);
132   }
133   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetDpi(hDPI, vDPI);
134
135   mAdaptor->ResizedSignal().Connect( mSlotDelegate, &Application::OnResize );
136 }
137
138 void Application::MainLoop(Dali::Configuration::ContextLoss configuration)
139 {
140   mContextLossConfiguration = configuration;
141
142   // Run the application
143   mFramework->Run();
144 }
145
146 void Application::Lower()
147 {
148   // Lower the application without quitting it.
149   mWindow.Lower();
150 }
151
152 void Application::Quit()
153 {
154   // Actually quit the application.
155   AddIdle(boost::bind(&Application::QuitFromMainLoop, this));
156 }
157
158 void Application::QuitFromMainLoop()
159 {
160   mAdaptor->Stop();
161
162   Dali::Application application(this);
163   mTerminateSignalV2.Emit( application );
164
165   mFramework->Quit();
166   // This will trigger OnTerminate(), below, after the main loop has completed.
167   mInitialized = false;
168 }
169
170 void Application::OnInit()
171 {
172   mFramework->AddAbortCallback(boost::bind(&Application::QuitFromMainLoop, this));
173
174   CreateWindow();
175   CreateAdaptor();
176
177   // Run the adaptor
178   mAdaptor->Start();
179
180   // Check if user requires no vsyncing and set on X11 Adaptor
181   if (mCommandLineOptions->noVSyncOnRender)
182   {
183     mAdaptor->SetUseHardwareVSync(false);
184   }
185
186   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( mCommandLineOptions->stereoBase );
187   if( mCommandLineOptions->viewMode != 0 )
188   {
189     ViewMode viewMode = MONO;
190     if( mCommandLineOptions->viewMode <= STEREO_INTERLACED )
191     {
192       viewMode = static_cast<ViewMode>( mCommandLineOptions->viewMode );
193     }
194     Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
195   }
196
197   mInitialized = true;
198
199   // in default, auto hide indicator mode
200   mWindow.ShowIndicator(Dali::Window::AUTO);
201
202   Dali::Application application(this);
203   mInitSignalV2.Emit( application );
204 }
205
206 void Application::OnTerminate()
207 {
208   // we've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
209   // delete the window as ecore_x has been destroyed by AppCore
210
211   mWindow.Reset();
212   mInitialized = false;
213 }
214
215 void Application::OnPause()
216 {
217   mAdaptor->Pause();
218   Dali::Application application(this);
219   mPauseSignalV2.Emit( application );
220 }
221
222 void Application::OnResume()
223 {
224   mAdaptor->Resume();
225   Dali::Application application(this);
226   mResumeSignalV2.Emit( application );
227 }
228
229 void Application::OnReset()
230 {
231   /*
232    * usually, reset callback was called when a caller request to launch this application via aul.
233    * because Application class already handled initialization in OnInit(), OnReset do nothing.
234    */
235   Dali::Application application(this);
236   mResetSignalV2.Emit( application );
237
238   mWindow.Raise();
239 }
240
241 void Application::OnLanguageChanged()
242 {
243   mAdaptor->NotifyLanguageChanged();
244 }
245
246 void Application::OnResize(Dali::Adaptor& adaptor)
247 {
248   Dali::Application application(this);
249   mResizeSignalV2.Emit( application );
250 }
251
252 bool Application::AddIdle(boost::function<void(void)> callBack)
253 {
254   return mAdaptor->AddIdle(callBack);
255 }
256
257 Dali::Adaptor& Application::GetAdaptor()
258 {
259   return *mAdaptor;
260 }
261
262 Dali::Window Application::GetWindow()
263 {
264   return mWindow;
265 }
266
267 const std::string& Application::GetTheme()
268 {
269   return Dali::StyleMonitor::Get().GetTheme();
270 }
271
272 void Application::SetTheme(const std::string& themeFilePath)
273 {
274   return Dali::StyleMonitor::Get().SetTheme(themeFilePath);
275 }
276
277 // Stereoscopy
278
279 void Application::SetViewMode( ViewMode viewMode )
280 {
281   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetViewMode( viewMode );
282 }
283
284 ViewMode Application::GetViewMode() const
285 {
286   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetViewMode();
287 }
288
289 void Application::SetStereoBase( float stereoBase )
290 {
291   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).SetStereoBase( stereoBase );
292 }
293
294 float Application::GetStereoBase() const
295 {
296   return Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).GetStereoBase();
297 }
298
299
300 void Application::ReplaceWindow(PositionSize windowPosition, const std::string& name)
301 {
302   Dali::Window newWindow = Dali::Window::New( windowPosition, name, mWindowMode == Dali::Application::TRANSPARENT );
303   Window& windowImpl = GetImplementation(newWindow);
304   windowImpl.SetAdaptor(*mAdaptor);
305   newWindow.ShowIndicator(Dali::Window::INVISIBLE);
306   Dali::RenderSurface* renderSurface = windowImpl.GetSurface();
307   Internal::Adaptor::Adaptor::GetImplementation( *mAdaptor ).ReplaceSurface(*renderSurface);
308   mWindow = newWindow;
309 }
310
311 } // namespace Adaptor
312
313 } // namespace Internal
314
315 } // namespace Dali