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 bool isUseUIThread = false;
86 #ifdef UI_THREAD_AVAILABLE
87 char* retEnv = std::getenv("TIZEN_UI_THREAD");
90 std::string uiThreadEnv = retEnv;
91 std::string enabledString = "true";
92 if(uiThreadEnv == enabledString)
99 Dali::TextAbstraction::FontClientPreInitialize();
100 WindowData windowData;
101 gPreInitializedApplication = new Application(argc, argv, "", Framework::NORMAL, isUseUIThread, windowData);
102 gPreInitializedApplication->mLaunchpadState = Launchpad::PRE_INITIALIZED;
104 #ifdef UI_THREAD_AVAILABLE
107 DALI_LOG_RELEASE_INFO("PRE_INITIALIZED with UI Threading");
108 gPreInitializedApplication->mUIThreadLoader = new UIThreadLoader(argc, argv);
109 gPreInitializedApplication->mUIThreadLoader->Run([&]() { gPreInitializedApplication->CreateWindow(); });
114 DALI_LOG_RELEASE_INFO("Only PRE_INITIALIZED");
115 gPreInitializedApplication->CreateWindow(); // Only create window
120 Application::Application(int* argc, char** argv[], const std::string& stylesheet, Framework::Type applicationType, bool useUiThread, const WindowData& windowData)
127 mLanguageChangedSignal(),
128 mRegionChangedSignal(),
130 mFrameworkFactory(nullptr),
131 mCommandLineOptions(nullptr),
132 mAdaptorBuilder(nullptr),
134 mEnvironmentOptions(nullptr),
136 mMainWindowMode(windowData.GetTransparency() ? WINDOW_MODE::TRANSPARENT : WINDOW_MODE::OPAQUE),
138 mStylesheet(stylesheet),
139 mWindowPositionSize(windowData.GetPositionSize()),
140 mLaunchpadState(Launchpad::NONE),
141 mDefaultWindowType(windowData.GetWindowType()),
142 mUseUiThread(useUiThread),
143 mIsSystemInitialized(false),
145 mUIThreadLoader(nullptr)
147 // Set mName from command-line args
148 if(argc && (*argc > 0))
150 mMainWindowName = (*argv)[0];
153 const char* uiThreadEnabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_ENABLE_UI_THREAD);
154 if(uiThreadEnabled && std::atoi(uiThreadEnabled) != 0)
159 mCommandLineOptions = new CommandLineOptions(argc, argv);
161 mFrameworkFactory = std::unique_ptr<FrameworkFactory>(Dali::Internal::Adaptor::CreateFrameworkFactory());
162 mFramework = mFrameworkFactory->CreateFramework(FrameworkBackend::DEFAULT, *this, *this, argc, argv, applicationType, mUseUiThread);
164 mUseRemoteSurface = (applicationType == Framework::WATCH);
167 Application::~Application()
169 SingletonService service = SingletonService::Get();
170 // Note this can be false i.e. if Application has never created a Core instance
173 service.UnregisterAll();
178 delete mCommandLineOptions;
180 // Application is created in Main thread whether UI Threading is enabled or not.
181 // But some resources are created in Main thread or UI thread.
182 // The below code is for the resource are created in Main thread.
186 delete mAdaptorBuilder;
187 if(mIsSystemInitialized)
189 WindowSystem::Shutdown();
196 delete mUIThreadLoader;
201 void Application::StoreWindowPositionSize(PositionSize positionSize)
203 mWindowPositionSize = positionSize;
206 void Application::ChangePreInitializedWindowInfo()
209 auto windowClassName = mEnvironmentOptions->GetWindowClassName();
210 auto windowName = mEnvironmentOptions->GetWindowName();
211 if(!windowName.empty())
213 mMainWindowName = windowName;
215 mMainWindow.SetClass(mMainWindowName, windowClassName);
217 // The real screen size may be different from the value of the preinitialized state. Update it.
218 Dali::Internal::Adaptor::WindowSystem::UpdateScreenSize();
220 int screenWidth, screenHeight;
221 Dali::Internal::Adaptor::WindowSystem::GetScreenSize(screenWidth, screenHeight);
223 // Set window position / size
224 if(mWindowPositionSize != PositionSize(0, 0, 0, 0))
226 Dali::DevelWindow::SetPositionSize(mMainWindow, mWindowPositionSize);
228 else if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
230 // Command line options override environment options and full screen
231 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
232 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
233 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
235 else if(mEnvironmentOptions->GetWindowWidth() && mEnvironmentOptions->GetWindowHeight())
237 // Environment options override full screen functionality if command line arguments not provided
238 mWindowPositionSize.width = mEnvironmentOptions->GetWindowWidth();
239 mWindowPositionSize.height = mEnvironmentOptions->GetWindowHeight();
240 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
242 else if(screenWidth != mWindowPositionSize.width || screenHeight != mWindowPositionSize.height)
244 // Some apps can receive screen size differently after launching by specifying size in manifest.
245 mWindowPositionSize.width = screenWidth;
246 mWindowPositionSize.height = screenHeight;
247 mMainWindow.SetSize(Dali::Window::WindowSize(mWindowPositionSize.width, mWindowPositionSize.height));
251 void Application::CreateWindow()
253 Internal::Adaptor::Window* window;
254 WindowData windowData;
255 windowData.SetTransparency(mMainWindowMode);
256 windowData.SetWindowType(mDefaultWindowType);
258 DALI_LOG_RELEASE_INFO("Create Default Window");
260 WindowSystem::Initialize();
261 mIsSystemInitialized = true;
263 if(mLaunchpadState != Launchpad::PRE_INITIALIZED)
265 if(mWindowPositionSize.width == 0 && mWindowPositionSize.height == 0)
267 if(mCommandLineOptions->stageWidth > 0 && mCommandLineOptions->stageHeight > 0)
269 // Command line options override environment options and full screen
270 mWindowPositionSize.width = mCommandLineOptions->stageWidth;
271 mWindowPositionSize.height = mCommandLineOptions->stageHeight;
273 else if(mEnvironmentOptions->GetWindowWidth() && mEnvironmentOptions->GetWindowHeight())
275 // Environment options override full screen functionality if command line arguments not provided
276 mWindowPositionSize.width = mEnvironmentOptions->GetWindowWidth();
277 mWindowPositionSize.height = mEnvironmentOptions->GetWindowHeight();
281 auto windowClassName = mEnvironmentOptions->GetWindowClassName();
282 auto windowName = mEnvironmentOptions->GetWindowName();
283 if(!windowName.empty())
285 mMainWindowName = windowName;
288 windowData.SetPositionSize(mWindowPositionSize);
289 window = Internal::Adaptor::Window::New(mMainWindowName, windowClassName, windowData);
293 // The position, size and the window name of the pre-initialized application will be updated in ChangePreInitializedWindowInfo()
294 // when the real application is launched.
295 windowData.SetPositionSize(mWindowPositionSize);
296 window = Internal::Adaptor::Window::New("", "", windowData);
299 mMainWindow = Dali::Window(window);
301 // Quit the application when the window is closed
302 GetImplementation(mMainWindow).DeleteRequestSignal().Connect(mSlotDelegate, &Application::Quit);
305 void Application::CreateAdaptor()
307 DALI_ASSERT_ALWAYS(mMainWindow && "Window required to create adaptor");
309 auto graphicsFactory = mAdaptorBuilder->GetGraphicsFactory();
311 Integration::SceneHolder sceneHolder = Integration::SceneHolder(&Dali::GetImplementation(mMainWindow));
313 mAdaptor = Adaptor::New(graphicsFactory, sceneHolder, mEnvironmentOptions.get());
315 Adaptor::GetImplementation(*mAdaptor).SetUseRemoteSurface(mUseRemoteSurface);
318 void Application::CreateAdaptorBuilder()
320 mAdaptorBuilder = new AdaptorBuilder(*mEnvironmentOptions);
323 void Application::MainLoop()
325 // Run the application
329 void Application::Lower()
331 // Lower the application without quitting it.
335 void Application::Quit()
337 // Actually quit the application.
338 // Force a call to Quit even if adaptor is not running.
339 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &Application::QuitFromMainLoop), false, true);
342 void Application::QuitFromMainLoop()
344 Accessibility::Bridge::GetCurrentBridge()->Terminate();
349 // This will trigger OnTerminate(), below, after the main loop has completed.
352 void Application::OnInit()
354 mEnvironmentOptions = std::unique_ptr<EnvironmentOptions>(new EnvironmentOptions());
356 mFramework->AddAbortCallback(MakeCallback(this, &Application::QuitFromMainLoop));
358 CreateAdaptorBuilder();
359 // If an application was pre-initialized, a window was made in advance
360 if(mLaunchpadState == Launchpad::NONE)
362 DALI_LOG_RELEASE_INFO("default Window is created in standalone");
368 if(mLaunchpadState == Launchpad::PRE_INITIALIZED)
370 ChangePreInitializedWindowInfo();
374 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_ADAPTOR_START");
376 DALI_TRACE_END(gTraceFilter, "DALI_APP_ADAPTOR_START");
377 Accessibility::Accessible::SetObjectRegistry(mAdaptor->GetObjectRegistry());
379 if(!mStylesheet.empty())
381 Dali::StyleMonitor::Get().SetTheme(mStylesheet);
384 // Wire up the LifecycleController
385 Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
387 InitSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnInit);
388 TerminateSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnTerminate);
389 PauseSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnPause);
390 ResumeSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnResume);
391 ResetSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnReset);
392 LanguageChangedSignal().Connect(&GetImplementation(lifecycleController), &LifecycleController::OnLanguageChanged);
394 Dali::Application application(this);
396 DALI_TRACE_BEGIN(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
397 mInitSignal.Emit(application);
398 DALI_TRACE_END(gTraceFilter, "DALI_APP_EMIT_INIT_SIGNAL");
400 mAdaptor->NotifySceneCreated();
402 // Ensure the join of Font thread at this point
403 Dali::TextAbstraction::FontClientJoinFontThreads();
406 void Application::OnTerminate()
408 // We've been told to quit by AppCore, ecore_x_destroy has been called, need to quit synchronously
409 // delete the window as ecore_x has been destroyed by AppCore
411 Dali::Application application(this);
412 mTerminateSignal.Emit(application);
416 // Ensure that the render-thread is not using the surface(window) after we delete it
420 mMainWindow.Reset(); // This only resets (clears) the default Window
422 // If DALi's UI Thread works, some resources are created in UI Thread, not Main thread.
423 // For that case, these resource should be deleted in UI Thread.
427 delete mAdaptorBuilder;
428 WindowSystem::Shutdown();
432 void Application::OnPause()
434 // A DALi app should handle Pause/Resume events.
435 // DALi just delivers the framework Pause event to the application, but not actually pause DALi core.
436 // Pausing DALi core only occurs on the Window Hidden framework event
437 Dali::Application application(this);
438 mPauseSignal.Emit(application);
441 void Application::OnResume()
443 // Emit the signal first so the application can queue any messages before we do an update/render
444 // This ensures we do not just redraw the last frame before pausing if that's not required
445 Dali::Application application(this);
446 mResumeSignal.Emit(application);
448 // DALi just delivers the framework Resume event to the application.
449 // Resuming DALi core only occurs on the Window Show framework event
451 // Trigger processing of events queued up while paused
452 CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(GetAdaptor());
453 coreEventInterface.ProcessCoreEvents();
456 void Application::OnReset()
459 * usually, reset callback was called when a caller request to launch this application via aul.
460 * because Application class already handled initialization in OnInit(), OnReset do nothing.
462 Dali::Application application(this);
463 mResetSignal.Emit(application);
466 void Application::OnAppControl(void* data)
468 Dali::Application application(this);
469 mAppControlSignal.Emit(application, data);
472 void Application::OnLanguageChanged()
474 mAdaptor->NotifyLanguageChanged();
475 Dali::Application application(this);
476 mLanguageChangedSignal.Emit(application);
479 void Application::OnRegionChanged()
481 Dali::Application application(this);
482 mRegionChangedSignal.Emit(application);
485 void Application::OnBatteryLow(Dali::DeviceStatus::Battery::Status status)
487 Dali::Application application(this);
488 mLowBatterySignal.Emit(status);
491 void Application::OnMemoryLow(Dali::DeviceStatus::Memory::Status status)
493 Dali::Application application(this);
494 mLowMemorySignal.Emit(status);
497 void Application::OnDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
499 Dali::Application application(this);
500 mDeviceOrientationChangedSignal.Emit(status);
503 void Application::OnSurfaceCreated(Any newSurface)
505 void* newWindow = AnyCast<void*>(newSurface);
506 void* oldWindow = AnyCast<void*>(mMainWindow.GetNativeHandle());
507 if(oldWindow != newWindow)
509 auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
510 std::unique_ptr<WindowRenderSurface> newSurfacePtr = renderSurfaceFactory->CreateWindowRenderSurface(PositionSize(), newSurface, true);
512 mAdaptor->ReplaceSurface(mMainWindow, *newSurfacePtr.release());
516 void Application::OnSurfaceDestroyed(Any surface)
520 void Application::OnTaskInit()
522 Dali::Application application(this);
523 mTaskInitSignal.Emit(application);
526 void Application::OnTaskTerminate()
528 Dali::Application application(this);
529 mTaskTerminateSignal.Emit(application);
532 void Application::OnTaskAppControl(void* data)
534 Dali::Application application(this);
535 mTaskAppControlSignal.Emit(application, data);
538 void Application::OnTaskLanguageChanged()
540 Dali::Application application(this);
541 mTaskLanguageChangedSignal.Emit(application);
544 void Application::OnTaskRegionChanged()
546 Dali::Application application(this);
547 mTaskRegionChangedSignal.Emit(application);
550 void Application::OnTaskBatteryLow(Dali::DeviceStatus::Battery::Status status)
552 Dali::Application application(this);
553 mTaskLowBatterySignal.Emit(status);
556 void Application::OnTaskMemoryLow(Dali::DeviceStatus::Memory::Status status)
558 Dali::Application application(this);
559 mTaskLowMemorySignal.Emit(status);
562 void Application::OnTaskDeviceOrientationChanged(Dali::DeviceStatus::Orientation::Status status)
564 Dali::Application application(this);
565 mTaskDeviceOrientationChangedSignal.Emit(status);
568 bool Application::AddIdle(CallbackBase* callback, bool hasReturnValue)
570 return mAdaptor->AddIdle(callback, hasReturnValue);
573 std::string Application::GetRegion() const
575 return mFramework->GetRegion();
578 std::string Application::GetLanguage() const
580 return mFramework->GetLanguage();
583 Dali::ObjectRegistry Application::GetObjectRegistry() const
585 Dali::ObjectRegistry objectRegistry;
588 objectRegistry = mAdaptor->GetObjectRegistry();
590 return objectRegistry;
593 Dali::Adaptor& Application::GetAdaptor()
598 Dali::Window Application::GetWindow()
603 std::string Application::GetResourcePath()
605 return SystemSettings::GetResourcePath();
608 std::string Application::GetDataPath()
610 return SystemSettings::GetDataPath();
613 void Application::FlushUpdateMessages()
615 Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).FlushUpdateMessages();
618 void Application::SetStyleSheet(const std::string& stylesheet)
620 mStylesheet = stylesheet;
623 void Application::SetCommandLineOptions(int* argc, char** argv[])
625 delete mCommandLineOptions;
627 mCommandLineOptions = new CommandLineOptions(argc, argv);
629 mFramework->SetCommandLineOptions(argc, argv);
631 if(argc && (*argc > 0))
633 // Set mName from command-line args
634 mMainWindowName = (*argv)[0];
638 void Application::SetDefaultWindowType(WindowType type)
640 mDefaultWindowType = type;
641 mMainWindow.SetType(type);
644 int32_t Application::GetRenderThreadId() const
648 return Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).GetRenderThreadId();
653 ApplicationPtr Application::GetPreInitializedApplication()
655 // Reset the handle to decrease the reference count
656 ApplicationPtr application = gPreInitializedApplication;
657 gPreInitializedApplication.Reset();
662 } // namespace Adaptor
664 } // namespace Internal