2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/adaptor/common/application-impl.h>
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>
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-factory.h>
35 #include <dali/internal/adaptor/common/framework.h>
36 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
37 #include <dali/internal/system/common/command-line-options.h>
38 #include <dali/internal/system/common/environment-variables.h>
39 #include <dali/internal/system/common/system-settings.h>
40 #include <dali/internal/window-system/common/render-surface-factory.h>
41 #include <dali/internal/window-system/common/window-impl.h>
42 #include <dali/internal/window-system/common/window-render-surface.h>
43 #include <dali/internal/window-system/common/window-system.h>
45 // To disable a macro with the same name from one of OpenGL headers
50 namespace TizenPlatform
52 class TizenPlatformAbstraction;
64 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_APPLICATION, true);
66 ApplicationPtr Application::gPreInitializedApplication(NULL);
68 ApplicationPtr Application::New(
71 const std::string& stylesheet,
72 Framework::Type applicationType,
74 const WindowData& windowData)
76 ApplicationPtr application(new Application(argc, argv, stylesheet, applicationType, useUiThread, windowData));
80 void Application::PreInitialize(int* argc, char** argv[])
82 if(!gPreInitializedApplication)
84 Dali::TextAbstraction::FontClientPreInitialize();
85 WindowData windowData;
86 gPreInitializedApplication = new Application(argc, argv, "", Framework::NORMAL, false, windowData);
87 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
88 gPreInitializedApplication->CreateWindow(); // Only create window
92 Application::Application(int* argc, char** argv[], const std::string& stylesheet, Framework::Type applicationType, bool useUiThread, const WindowData& windowData)
99 mLanguageChangedSignal(),
100 mRegionChangedSignal(),
102 mCommandLineOptions(nullptr),
103 mAdaptorBuilder(nullptr),
105 mEnvironmentOptions(nullptr),
107 mMainWindowMode(windowData.GetTransparency() ? WINDOW_MODE::TRANSPARENT : WINDOW_MODE::OPAQUE),
109 mStylesheet(stylesheet),
110 mWindowPositionSize(windowData.GetPositionSize()),
111 mLaunchpadState(Launchpad::NONE),
112 mDefaultWindowType(windowData.GetWindowType()),
113 mUseUiThread(useUiThread),
116 // Set mName from command-line args
117 if(argc && (*argc > 0))
119 mMainWindowName = (*argv)[0];
122 const char* uiThreadEnabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_ENABLE_UI_THREAD);
123 if(uiThreadEnabled && std::atoi(uiThreadEnabled) != 0)
128 mCommandLineOptions = new CommandLineOptions(argc, argv);
129 mFramework = Dali::Internal::Adaptor::GetFrameworkFactory()->CreateFramework(FrameworkBackend::DEFAULT, *this, *this, argc, argv, applicationType, mUseUiThread);
131 mUseRemoteSurface = (applicationType == Framework::WATCH);
134 Application::~Application()
136 SingletonService service = SingletonService::Get();
137 // Note this can be false i.e. if Application has never created a Core instance
140 service.UnregisterAll();
145 delete mCommandLineOptions;
147 // Application is created in Main thread whether UI Threading is enabled or not.
148 // But some resources are created in Main thread or UI thread.
149 // The below code is for the resource are created in Main thread.
153 delete mAdaptorBuilder;
154 WindowSystem::Shutdown();
158 void Application::StoreWindowPositionSize(PositionSize positionSize)
160 mWindowPositionSize = positionSize;
163 void Application::ChangePreInitializedWindowInfo()
166 auto windowClassName = mEnvironmentOptions->GetWindowClassName();
167 auto windowName = mEnvironmentOptions->GetWindowName();
168 if(!windowName.empty())
170 mMainWindowName = windowName;
172 mMainWindow.SetClass(mMainWindowName, windowClassName);
174 // The real screen size may be different from the value of the preinitialized state. Update it.
175 Dali::Internal::Adaptor::WindowSystem::UpdateScreenSize();
177 int screenWidth, screenHeight;
178 Dali::Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
180 // Set window position / size
181 if(mWindowPositionSize != PositionSize(0, 0, 0, 0))
183 Dali::DevelWindow::SetPositionSize(mMainWindow, mWindowPositionSize);
185 else if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
187 // Command line options override environment options and full screen
188 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
189 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
190 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
192 else if(mEnvironmentOptions->GetWindowWidth() && mEnvironmentOptions->GetWindowHeight())
194 // Environment options override full screen functionality if command line arguments not provided
195 mWindowPositionSize.width = mEnvironmentOptions->GetWindowWidth();
196 mWindowPositionSize.height = mEnvironmentOptions->GetWindowHeight();
197 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
199 else if(screenWidth != mWindowPositionSize.width || screenHeight != mWindowPositionSize.height)
201 // Some apps can receive screen size differently after launching by specifying size in manifest.
202 mWindowPositionSize.width = screenWidth;
203 mWindowPositionSize.height = screenHeight;
204 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
208 void Application::CreateWindow()
210 Internal::Adaptor::Window* window;
211 WindowData windowData;
212 windowData.SetTransparency(mMainWindowMode);
213 windowData.SetWindowType(mDefaultWindowType);
215 WindowSystem::Initialize();
217 if(mLaunchpadState != Launchpad::PRE_INITIALIZED)
219 if(mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0)
221 if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
223 // Command line options override environment options and full screen
224 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
225 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
227 else if(mEnvironmentOptions->GetWindowWidth() && mEnvironmentOptions->GetWindowHeight())
229 // Environment options override full screen functionality if command line arguments not provided
230 mWindowPositionSize.width = mEnvironmentOptions->GetWindowWidth();
231 mWindowPositionSize.height = mEnvironmentOptions->GetWindowHeight();
235 auto windowClassName = mEnvironmentOptions->GetWindowClassName();
236 auto windowName = mEnvironmentOptions->GetWindowName();
237 if(!windowName.empty())
239 mMainWindowName = windowName;
242 windowData.SetPositionSize(mWindowPositionSize);
243 window = Internal::Adaptor::Window::New(mMainWindowName, windowClassName, windowData);
247 // The position, size and the window name of the pre-initialized application will be updated in ChangePreInitializedWindowInfo()
248 // when the real application is launched.
249 windowData.SetPositionSize(mWindowPositionSize);
250 window = Internal::Adaptor::Window::New("", "", windowData);
253 mMainWindow = Dali::Window(window);
255 // Quit the application when the window is closed
256 GetImplementation(mMainWindow).DeleteRequestSignal().Connect(mSlotDelegate, &Application::Quit);
259 void Application::CreateAdaptor()
261 DALI_ASSERT_ALWAYS(mMainWindow && "Window required to create adaptor");
263 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
265 Integration::SceneHolder sceneHolder = Integration::SceneHolder(&Dali::GetImplementation(mMainWindow));
267 mAdaptor = Adaptor::New(graphicsFactory, sceneHolder, mEnvironmentOptions.get());
269 Adaptor::GetImplementation(*mAdaptor).SetUseRemoteSurface(mUseRemoteSurface);
272 void Application::CreateAdaptorBuilder()
274 mAdaptorBuilder = new AdaptorBuilder(*mEnvironmentOptions);
277 void Application::MainLoop()
279 // Run the application
283 void Application::Lower()
285 // Lower the application without quitting it.
289 void Application::Quit()
291 // Actually quit the application.
292 // Force a call to Quit even if adaptor is not running.
293 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &Application::QuitFromMainLoop), false, true);
296 void Application::QuitFromMainLoop()
298 Accessibility::Bridge::GetCurrentBridge()->Terminate();
303 // This will trigger OnTerminate(), below, after the main loop has completed.
306 void Application::OnInit()
308 mEnvironmentOptions = std::unique_ptr<EnvironmentOptions>(new EnvironmentOptions());
310 mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop));
312 CreateAdaptorBuilder();
313 // If an application was pre-initialized, a window was made in advance
314 if(mLaunchpadState == Launchpad::NONE)
321 if(mLaunchpadState == Launchpad::PRE_INITIALIZED)
323 ChangePreInitializedWindowInfo();
327 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_ADAPTOR_START");
329 DALI_TRACE_END(gTraceFilter, "DALI_APP_ADAPTOR_START");
330 Accessibility::Accessible::SetObjectRegistry(mAdaptor->GetObjectRegistry());
332 if(!mStylesheet.empty())
334 Dali::StyleMonitor::Get().SetTheme(mStylesheet);
337 // Wire up the LifecycleController
338 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
340 InitSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnInit);
341 TerminateSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnTerminate);
342 PauseSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnPause);
343 ResumeSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnResume);
344 ResetSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnReset);
345 LanguageChangedSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnLanguageChanged);
347 Dali::Application application(this);
349 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
350 mInitSignal.Emit(application);
351 DALI_TRACE_END(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
353 mAdaptor->NotifySceneCreated();
355 // Ensure the join of Font thread at this point
356 Dali::TextAbstraction::FontClientJoinFontThreads();
359 void Application::OnTerminate()
361 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
362 // delete the window as ecore_x has been destroyed by AppCore
364 Dali::Application application(this);
365 mTerminateSignal.Emit(application);
369 // Ensure that the render-thread is not using the surface(window) after we delete it
373 mMainWindow.Reset(); // This only resets (clears) the default Window
375 // If DALi's UI Thread works, some resources are created in UI Thread, not Main thread.
376 // For that case, these resource should be deleted in UI Thread.
380 delete mAdaptorBuilder;
381 WindowSystem::Shutdown();
385 void Application::OnPause()
387 // A DALi app should handle Pause/Resume events.
388 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
389 // Pausing DALi core only occurs on the Window Hidden framework event
390 Dali::Application application(this);
391 mPauseSignal.Emit(application);
394 void Application::OnResume()
396 // Emit the signal first so the application can queue any messages before we do an update/render
397 // This ensures we do not just redraw the last frame before pausing if that's not required
398 Dali::Application application(this);
399 mResumeSignal.Emit(application);
401 // DALi just delivers the framework Resume event to the application.
402 // Resuming DALi core only occurs on the Window Show framework event
404 // Trigger processing of events queued up while paused
405 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(GetAdaptor());
406 coreEventInterface.ProcessCoreEvents();
409 void Application::OnReset()
412 * usually, reset callback was called when a caller request to launch this application via aul.
413 * because Application class already handled initialization in OnInit(), OnReset do nothing.
415 Dali::Application application(this);
416 mResetSignal.Emit(application);
419 void Application::OnAppControl(void* data)
421 Dali::Application application(this);
422 mAppControlSignal.Emit(application, data);
425 void Application::OnLanguageChanged()
427 mAdaptor->NotifyLanguageChanged();
428 Dali::Application application(this);
429 mLanguageChangedSignal.Emit(application);
432 void Application::OnRegionChanged()
434 Dali::Application application(this);
435 mRegionChangedSignal.Emit(application);
438 void Application::OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
440 Dali::Application application(this);
441 mLowBatterySignal.Emit(status);
444 void Application::OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
446 Dali::Application application(this);
447 mLowMemorySignal.Emit(status);
450 void Application::OnDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
452 Dali::Application application(this);
453 mDeviceOrientationChangedSignal.Emit(status);
456 void Application::OnSurfaceCreated(Any newSurface)
458 void* newWindow = AnyCast<void*>(newSurface);
459 void* oldWindow = AnyCast<void*>(mMainWindow.GetNativeHandle());
460 if(oldWindow != newWindow)
462 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
463 std::unique_ptr<WindowRenderSurface> newSurfacePtr = renderSurfaceFactory->CreateWindowRenderSurface(PositionSize(), newSurface, true);
465 mAdaptor->ReplaceSurface(mMainWindow, *newSurfacePtr.release());
469 void Application::OnSurfaceDestroyed(Any surface)
473 void Application::OnTaskInit()
475 Dali::Application application(this);
476 mTaskInitSignal.Emit(application);
479 void Application::OnTaskTerminate()
481 Dali::Application application(this);
482 mTaskTerminateSignal.Emit(application);
485 void Application::OnTaskAppControl(void* data)
487 Dali::Application application(this);
488 mTaskAppControlSignal.Emit(application, data);
491 void Application::OnTaskLanguageChanged()
493 Dali::Application application(this);
494 mTaskLanguageChangedSignal.Emit(application);
497 void Application::OnTaskRegionChanged()
499 Dali::Application application(this);
500 mTaskRegionChangedSignal.Emit(application);
503 void Application::OnTaskBatteryLow(Dali::DeviceStatus::Battery::Status status)
505 Dali::Application application(this);
506 mTaskLowBatterySignal.Emit(status);
509 void Application::OnTaskMemoryLow(Dali::DeviceStatus::Memory::Status status)
511 Dali::Application application(this);
512 mTaskLowMemorySignal.Emit(status);
515 void Application::OnTaskDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
517 Dali::Application application(this);
518 mTaskDeviceOrientationChangedSignal.Emit(status);
521 bool Application::AddIdle(CallbackBase* callback, bool hasReturnValue)
523 return mAdaptor->AddIdle(callback, hasReturnValue);
526 std::string Application::GetRegion() const
528 return mFramework->GetRegion();
531 std::string Application::GetLanguage() const
533 return mFramework->GetLanguage();
536 Dali::ObjectRegistry Application::GetObjectRegistry() const
538 Dali::ObjectRegistry objectRegistry;
541 objectRegistry = mAdaptor->GetObjectRegistry();
543 return objectRegistry;
546 Dali::Adaptor& Application::GetAdaptor()
551 Dali::Window Application::GetWindow()
556 std::string Application::GetResourcePath()
558 return SystemSettings::GetResourcePath();
561 std::string Application::GetDataPath()
563 return SystemSettings::GetDataPath();
566 void Application::SetStyleSheet(const std::string& stylesheet)
568 mStylesheet = stylesheet;
571 void Application::SetCommandLineOptions(int* argc, char** argv[])
573 delete mCommandLineOptions;
575 mCommandLineOptions = new CommandLineOptions(argc, argv);
577 mFramework->SetCommandLineOptions(argc, argv);
579 if(argc && (*argc > 0))
581 // Set mName from command-line args
582 mMainWindowName = (*argv)[0];
586 void Application::SetDefaultWindowType(WindowType type)
588 mDefaultWindowType = type;
589 mMainWindow.SetType(type);
592 int32_t Application::GetRenderThreadId() const
596 return Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).GetRenderThreadId();
601 ApplicationPtr Application::GetPreInitializedApplication()
603 // Reset the handle to decrease the reference count
604 ApplicationPtr application = gPreInitializedApplication;
605 gPreInitializedApplication.Reset();
610 } // namespace Adaptor
612 } // namespace Internal