Merge "Direct Rendering" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / adaptor / common / application-impl.cpp
1 /*
2  * Copyright (c) 2022 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/devel-api/common/singleton-service.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/integration-api/trace.h>
25 #include <dali/public-api/object/object-registry.h>
26
27 // INTERNAL INCLUDES
28 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
29 #include <dali/devel-api/adaptor-framework/environment-variable.h>
30 #include <dali/devel-api/adaptor-framework/style-monitor.h>
31 #include <dali/devel-api/atspi-interfaces/accessible.h>
32 #include <dali/devel-api/text-abstraction/font-client.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/adaptor/common/framework.h>
35 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
36 #include <dali/internal/system/common/command-line-options.h>
37 #include <dali/internal/system/common/environment-variables.h>
38 #include <dali/internal/window-system/common/render-surface-factory.h>
39 #include <dali/internal/window-system/common/window-impl.h>
40 #include <dali/internal/window-system/common/window-render-surface.h>
41 #include <dali/internal/window-system/common/window-system.h>
42
43 // To disable a macro with the same name from one of OpenGL headers
44 #undef Status
45
46 namespace Dali
47 {
48 namespace TizenPlatform
49 {
50 class TizenPlatformAbstraction;
51 }
52
53 namespace Integration
54 {
55 class Core;
56 }
57
58 namespace Internal
59 {
60 namespace Adaptor
61 {
62 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_APPLICATION, true);
63
64 ApplicationPtr Application::gPreInitializedApplication(NULL);
65
66 ApplicationPtr Application::New(
67   int*                           argc,
68   char**                         argv[],
69   const std::string&             stylesheet,
70   Dali::Application::WINDOW_MODE windowMode,
71   const PositionSize&            positionSize,
72   Framework::Type                applicationType,
73   WindowType                     type,
74   bool                           useUiThread)
75 {
76   ApplicationPtr application(new Application(argc, argv, stylesheet, windowMode, positionSize, applicationType, type, useUiThread));
77   return application;
78 }
79
80 void Application::PreInitialize(int* argc, char** argv[])
81 {
82   if(!gPreInitializedApplication)
83   {
84     Dali::TextAbstraction::FontClientPreInitialize();
85
86     gPreInitializedApplication = new Application(argc, argv, "", Dali::Application::OPAQUE, PositionSize(), Framework::NORMAL, WindowType::NORMAL, false);
87     gPreInitializedApplication->CreateWindow(); // Only create window
88     gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
89   }
90 }
91
92 Application::Application(int* argc, char** argv[], const std::string& stylesheet, Dali::Application::WINDOW_MODE windowMode, const PositionSize& positionSize, Framework::Type applicationType, WindowType type, bool useUiThread)
93 : mInitSignal(),
94   mTerminateSignal(),
95   mPauseSignal(),
96   mResumeSignal(),
97   mResetSignal(),
98   mAppControlSignal(),
99   mLanguageChangedSignal(),
100   mRegionChangedSignal(),
101   mEventLoop(nullptr),
102   mFramework(nullptr),
103   mCommandLineOptions(nullptr),
104   mAdaptorBuilder(nullptr),
105   mAdaptor(nullptr),
106   mMainWindow(),
107   mMainWindowMode(windowMode),
108   mMainWindowName(),
109   mStylesheet(stylesheet),
110   mEnvironmentOptions(),
111   mWindowPositionSize(positionSize),
112   mLaunchpadState(Launchpad::NONE),
113   mDefaultWindowType(type),
114   mUseUiThread(useUiThread),
115   mSlotDelegate(this)
116 {
117   // Get mName from environment options
118   mMainWindowName = mEnvironmentOptions.GetWindowName();
119   if(mMainWindowName.empty() && argc && (*argc > 0))
120   {
121     // Set mName from command-line args if environment option not set
122     mMainWindowName = (*argv)[0];
123   }
124
125   const char* uiThreadEnabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_ENABLE_UI_THREAD);
126   if(uiThreadEnabled && std::atoi(uiThreadEnabled) != 0)
127   {
128     mUseUiThread = true;
129   }
130
131   mCommandLineOptions = new CommandLineOptions(argc, argv);
132   mFramework          = new Framework(*this, *this, argc, argv, applicationType, mUseUiThread);
133   mUseRemoteSurface   = (applicationType == Framework::WATCH);
134 }
135
136 Application::~Application()
137 {
138   SingletonService service = SingletonService::Get();
139   // Note this can be false i.e. if Application has never created a Core instance
140   if(service)
141   {
142     service.UnregisterAll();
143   }
144
145   mMainWindow.Reset();
146   delete mAdaptor;
147   delete mAdaptorBuilder;
148   delete mCommandLineOptions;
149   delete mFramework;
150 }
151
152 void Application::StoreWindowPositionSize(PositionSize positionSize)
153 {
154   mWindowPositionSize = positionSize;
155 }
156
157 void Application::ChangePreInitializedWindowSize()
158 {
159   int screenWidth, screenHeight;
160   Dali::Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
161
162   if(mWindowPositionSize != PositionSize(0, 0, 0, 0))
163   {
164     Dali::DevelWindow::SetPositionSize(mMainWindow, mWindowPositionSize);
165   }
166   else if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
167   {
168     // Command line options override environment options and full screen
169     mWindowPositionSize.width  = mCommandLineOptions->stageWidth;
170     mWindowPositionSize.height = mCommandLineOptions->stageHeight;
171     mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
172   }
173   else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
174   {
175     // Environment options override full screen functionality if command line arguments not provided
176     mWindowPositionSize.width  = mEnvironmentOptions.GetWindowWidth();
177     mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
178     mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
179   }
180   else if(screenWidth != mWindowPositionSize.width || screenHeight != mWindowPositionSize.height)
181   {
182     // Some apps can receive screen size differently after launching by specifying size in manifest.
183     mWindowPositionSize.width  = screenWidth;
184     mWindowPositionSize.height = screenHeight;
185     mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
186   }
187 }
188
189 void Application::CreateWindow()
190 {
191   if(mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0)
192   {
193     if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
194     {
195       // Command line options override environment options and full screen
196       mWindowPositionSize.width  = mCommandLineOptions->stageWidth;
197       mWindowPositionSize.height = mCommandLineOptions->stageHeight;
198     }
199     else if(mEnvironmentOptions.GetWindowWidth() && mEnvironmentOptions.GetWindowHeight())
200     {
201       // Environment options override full screen functionality if command line arguments not provided
202       mWindowPositionSize.width  = mEnvironmentOptions.GetWindowWidth();
203       mWindowPositionSize.height = mEnvironmentOptions.GetWindowHeight();
204     }
205   }
206
207   const std::string& windowClassName = mEnvironmentOptions.GetWindowClassName();
208
209   Internal::Adaptor::Window* window = Internal::Adaptor::Window::New(mWindowPositionSize, mMainWindowName, windowClassName, mDefaultWindowType, mMainWindowMode == Dali::Application::TRANSPARENT);
210   mMainWindow                       = Dali::Window(window);
211
212   // Quit the application when the window is closed
213   GetImplementation(mMainWindow).DeleteRequestSignal().Connect(mSlotDelegate, &Application::Quit);
214 }
215
216 void Application::CreateAdaptor()
217 {
218   DALI_ASSERT_ALWAYS(mMainWindow && "Window required to create adaptor");
219
220   auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
221
222   Integration::SceneHolder sceneHolder = Integration::SceneHolder(&Dali::GetImplementation(mMainWindow));
223
224   mAdaptor = Adaptor::New(graphicsFactory, sceneHolder, &mEnvironmentOptions);
225
226   Adaptor::GetImplementation(*mAdaptor).SetUseRemoteSurface(mUseRemoteSurface);
227 }
228
229 void Application::CreateAdaptorBuilder()
230 {
231   mAdaptorBuilder = new AdaptorBuilder(mEnvironmentOptions);
232 }
233
234 void Application::MainLoop()
235 {
236   // Run the application
237   mFramework->Run();
238 }
239
240 void Application::Lower()
241 {
242   // Lower the application without quitting it.
243   mMainWindow.Lower();
244 }
245
246 void Application::Quit()
247 {
248   // Actually quit the application.
249   // Force a call to Quit even if adaptor is not running.
250   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &Application::QuitFromMainLoop), false, true);
251 }
252
253 void Application::QuitFromMainLoop()
254 {
255   Accessibility::Bridge::GetCurrentBridge()->Terminate();
256
257   mAdaptor->Stop();
258
259   mFramework->Quit();
260   // This will trigger OnTerminate(), below, after the main loop has completed.
261 }
262
263 void Application::OnInit()
264 {
265   mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop));
266
267   CreateAdaptorBuilder();
268   // If an application was pre-initialized, a window was made in advance
269   if(mLaunchpadState == Launchpad::NONE)
270   {
271     CreateWindow();
272   }
273
274   CreateAdaptor();
275
276   if(mLaunchpadState == Launchpad::PRE_INITIALIZED)
277   {
278     ChangePreInitializedWindowSize();
279   }
280
281   // Run the adaptor
282   DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_ADAPTOR_START");
283   mAdaptor->Start();
284   DALI_TRACE_END(gTraceFilter, "DALI_APP_ADAPTOR_START");
285   Accessibility::Accessible::SetObjectRegistry(mAdaptor->GetObjectRegistry());
286
287   if(!mStylesheet.empty())
288   {
289     Dali::StyleMonitor::Get().SetTheme(mStylesheet);
290   }
291
292   // Wire up the LifecycleController
293   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
294
295   InitSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnInit);
296   TerminateSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnTerminate);
297   PauseSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnPause);
298   ResumeSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnResume);
299   ResetSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnReset);
300   LanguageChangedSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnLanguageChanged);
301
302   Dali::Application application(this);
303
304   DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
305   mInitSignal.Emit(application);
306   DALI_TRACE_END(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
307
308   mAdaptor->NotifySceneCreated();
309 }
310
311 void Application::OnTerminate()
312 {
313   // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
314   // delete the window as ecore_x has been destroyed by AppCore
315
316   Dali::Application application(this);
317   mTerminateSignal.Emit(application);
318
319   if(mAdaptor)
320   {
321     // Ensure that the render-thread is not using the surface(window) after we delete it
322     mAdaptor->Stop();
323   }
324
325   mMainWindow.Reset(); // This only resets (clears) the default Window
326 }
327
328 void Application::OnPause()
329 {
330   // A DALi app should handle Pause/Resume events.
331   // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
332   // Pausing DALi core only occurs on the Window Hidden framework event
333   Dali::Application application(this);
334   mPauseSignal.Emit(application);
335 }
336
337 void Application::OnResume()
338 {
339   // Emit the signal first so the application can queue any messages before we do an update/render
340   // This ensures we do not just redraw the last frame before pausing if that's not required
341   Dali::Application application(this);
342   mResumeSignal.Emit(application);
343
344   // DALi just delivers the framework Resume event to the application.
345   // Resuming DALi core only occurs on the Window Show framework event
346
347   // Trigger processing of events queued up while paused
348   CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(GetAdaptor());
349   coreEventInterface.ProcessCoreEvents();
350 }
351
352 void Application::OnReset()
353 {
354   /*
355    * usually, reset callback was called when a caller request to launch this application via aul.
356    * because Application class already handled initialization in OnInit(), OnReset do nothing.
357    */
358   Dali::Application application(this);
359   mResetSignal.Emit(application);
360 }
361
362 void Application::OnAppControl(void* data)
363 {
364   Dali::Application application(this);
365   mAppControlSignal.Emit(application, data);
366 }
367
368 void Application::OnLanguageChanged()
369 {
370   mAdaptor->NotifyLanguageChanged();
371   Dali::Application application(this);
372   mLanguageChangedSignal.Emit(application);
373 }
374
375 void Application::OnRegionChanged()
376 {
377   Dali::Application application(this);
378   mRegionChangedSignal.Emit(application);
379 }
380
381 void Application::OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
382 {
383   Dali::Application application(this);
384   mLowBatterySignal.Emit(status);
385 }
386
387 void Application::OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
388 {
389   Dali::Application application(this);
390   mLowMemorySignal.Emit(status);
391 }
392
393 void Application::OnSurfaceCreated(Any newSurface)
394 {
395   void* newWindow = AnyCast<void*>(newSurface);
396   void* oldWindow = AnyCast<void*>(mMainWindow.GetNativeHandle());
397   if(oldWindow != newWindow)
398   {
399     auto                                 renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
400     std::unique_ptr<WindowRenderSurface> newSurfacePtr        = renderSurfaceFactory->CreateWindowRenderSurface(PositionSize(), newSurface, true);
401
402     mAdaptor->ReplaceSurface(mMainWindow, *newSurfacePtr.release());
403   }
404 }
405
406 void Application::OnSurfaceDestroyed(Any surface)
407 {
408 }
409
410 void Application::OnTaskInit()
411 {
412   Dali::Application application(this);
413   mTaskInitSignal.Emit(application);
414 }
415
416 void Application::OnTaskTerminate()
417 {
418   Dali::Application application(this);
419   mTaskTerminateSignal.Emit(application);
420 }
421
422 void Application::OnTaskAppControl(void* data)
423 {
424   Dali::Application application(this);
425   mTaskAppControlSignal.Emit(application, data);
426 }
427
428 void Application::OnTaskLanguageChanged()
429 {
430   Dali::Application application(this);
431   mTaskLanguageChangedSignal.Emit(application);
432 }
433
434 void Application::OnTaskRegionChanged()
435 {
436   Dali::Application application(this);
437   mTaskRegionChangedSignal.Emit(application);
438 }
439
440 void Application::OnTaskBatteryLow(Dali::DeviceStatus::Battery::Status status)
441 {
442   Dali::Application application(this);
443   mTaskLowBatterySignal.Emit(status);
444 }
445
446 void Application::OnTaskMemoryLow(Dali::DeviceStatus::Memory::Status status)
447 {
448   Dali::Application application(this);
449   mTaskLowMemorySignal.Emit(status);
450 }
451
452 bool Application::AddIdle(CallbackBase* callback, bool hasReturnValue)
453 {
454   return mAdaptor->AddIdle(callback, hasReturnValue);
455 }
456
457 std::string Application::GetRegion() const
458 {
459   return mFramework->GetRegion();
460 }
461
462 std::string Application::GetLanguage() const
463 {
464   return mFramework->GetLanguage();
465 }
466
467 Dali::ObjectRegistry Application::GetObjectRegistry() const
468 {
469   Dali::ObjectRegistry objectRegistry;
470   if(mAdaptor)
471   {
472     objectRegistry = mAdaptor->GetObjectRegistry();
473   }
474   return objectRegistry;
475 }
476
477 Dali::Adaptor& Application::GetAdaptor()
478 {
479   return *mAdaptor;
480 }
481
482 Dali::Window Application::GetWindow()
483 {
484   return mMainWindow;
485 }
486
487 std::string Application::GetResourcePath()
488 {
489   return Internal::Adaptor::Framework::GetResourcePath();
490 }
491
492 std::string Application::GetDataPath()
493 {
494   return Internal::Adaptor::Framework::GetDataPath();
495 }
496
497 void Application::SetStyleSheet(const std::string& stylesheet)
498 {
499   mStylesheet = stylesheet;
500 }
501
502 void Application::SetCommandLineOptions(int* argc, char** argv[])
503 {
504   delete mCommandLineOptions;
505
506   mCommandLineOptions = new CommandLineOptions(argc, argv);
507
508   mFramework->SetCommandLineOptions(argc, argv);
509 }
510
511 void Application::SetDefaultWindowType(WindowType type)
512 {
513   mDefaultWindowType = type;
514   mMainWindow.SetType(type);
515 }
516
517 ApplicationPtr Application::GetPreInitializedApplication()
518 {
519   // Reset the handle to decrease the reference count
520   ApplicationPtr application = gPreInitializedApplication;
521   gPreInitializedApplication.Reset();
522
523   return application;
524 }
525
526 } // namespace Adaptor
527
528 } // namespace Internal
529
530 } // namespace Dali