[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / offscreen / common / offscreen-application-impl.cpp
1 /*
2  * Copyright (c) 2023 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/offscreen/common/offscreen-application-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
23 #include <dali/devel-api/adaptor-framework/environment-variable.h>
24 #include <dali/integration-api/adaptor-framework/adaptor.h>
25 #include <dali/integration-api/adaptor-framework/native-render-surface.h>
26 #include <dali/internal/adaptor/common/adaptor-impl.h>
27 #include <dali/internal/adaptor/common/framework-factory.h>
28 #include <dali/internal/adaptor/common/lifecycle-controller-impl.h>
29 #include <dali/internal/adaptor/common/thread-controller-interface.h>
30 #include <dali/internal/offscreen/common/offscreen-window-impl.h>
31 #include <dali/internal/system/common/environment-variables.h>
32 #include <dali/internal/window-system/common/window-system.h>
33
34 namespace Dali
35 {
36 namespace Internal
37 {
38 namespace
39 {
40 void EmitLifecycleControllerSignal(void (Internal::Adaptor::LifecycleController::*member)(Dali::Application&))
41 {
42   Dali::LifecycleController lifecycleController = Dali::LifecycleController::Get();
43   if(DALI_LIKELY(lifecycleController))
44   {
45     Dali::Application dummyApplication;
46     (GetImplementation(lifecycleController).*member)(dummyApplication);
47   }
48 }
49 } // namespace
50 using RenderMode = Dali::OffscreenApplication::RenderMode;
51
52 IntrusivePtr<OffscreenApplication> OffscreenApplication::New(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent, RenderMode renderMode)
53 {
54   IntrusivePtr<OffscreenApplication> offscreenApplication = new OffscreenApplication(width, height, surface, isTranslucent, renderMode);
55   return offscreenApplication;
56 }
57
58 OffscreenApplication::OffscreenApplication(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent, RenderMode renderMode)
59 {
60   // Disable partial update
61   EnvironmentVariable::SetEnvironmentVariable(DALI_ENV_DISABLE_PARTIAL_UPDATE, "1");
62
63   // Disable ATSPI
64   Dali::Accessibility::Bridge::DisableAutoInit();
65
66   // Now we assume separated main loop for the offscreen application
67   mFrameworkFactory = std::unique_ptr<Adaptor::FrameworkFactory>(Dali::Internal::Adaptor::CreateFrameworkFactory());
68   mFramework        = mFrameworkFactory->CreateFramework(Internal::Adaptor::FrameworkBackend::GLIB, *this, *this, nullptr, nullptr, Adaptor::Framework::NORMAL, false);
69
70   // Generate a default window
71   IntrusivePtr<Internal::OffscreenWindow> impl = Internal::OffscreenWindow::New(width, height, surface, isTranslucent);
72   mDefaultWindow                               = Dali::OffscreenWindow(impl.Get());
73
74   mAdaptor.reset(Dali::Internal::Adaptor::Adaptor::New(Dali::Integration::SceneHolder(impl.Get()), impl->GetSurface(), NULL, renderMode == RenderMode::AUTO ? Dali::Internal::Adaptor::ThreadMode::NORMAL : Dali::Internal::Adaptor::ThreadMode::RUN_IF_REQUESTED));
75
76   // Initialize default window
77   impl->Initialize(true);
78 }
79
80 OffscreenApplication::~OffscreenApplication()
81 {
82 }
83
84 void OffscreenApplication::MainLoop()
85 {
86   mFramework->Run();
87 }
88
89 void OffscreenApplication::Quit()
90 {
91   // Actually quit the application.
92   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &OffscreenApplication::QuitFromMainLoop), false);
93 }
94
95 Dali::OffscreenWindow OffscreenApplication::GetWindow()
96 {
97   return mDefaultWindow;
98 }
99
100 void OffscreenApplication::RenderOnce()
101 {
102   mAdaptor->RenderOnce();
103 }
104
105 Any OffscreenApplication::GetFrameworkContext() const
106 {
107   return mFramework->GetMainLoopContext();
108 }
109
110 void OffscreenApplication::OnInit()
111 {
112   // Start the adaptor
113   mAdaptor->Start();
114
115   Dali::OffscreenApplication application(this);
116   mInitSignal.Emit();
117   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnInit);
118
119   mAdaptor->NotifySceneCreated();
120 }
121
122 void OffscreenApplication::OnTerminate()
123 {
124   Dali::OffscreenApplication application(this);
125   mTerminateSignal.Emit();
126   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnTerminate);
127
128   // Stop the adaptor
129   mAdaptor->Stop();
130
131   mDefaultWindow.Reset();
132 }
133
134 void OffscreenApplication::OnPause()
135 {
136   Dali::OffscreenApplication application(this);
137   mPauseSignal.Emit();
138   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnPause);
139 }
140
141 void OffscreenApplication::OnResume()
142 {
143   Dali::OffscreenApplication application(this);
144   mResumeSignal.Emit();
145   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnResume);
146
147   // DALi just delivers the framework Resume event to the application.
148   // Resuming DALi core only occurs on the Window Show framework event
149
150   // Trigger processing of events queued up while paused
151   Internal::Adaptor::CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor);
152   coreEventInterface.ProcessCoreEvents();
153 }
154
155 void OffscreenApplication::OnReset()
156 {
157   /*
158    * usually, reset callback was called when a caller request to launch this application via aul.
159    * because Application class already handled initialization in OnInit(), OnReset do nothing.
160    */
161   Dali::OffscreenApplication application(this);
162   mResetSignal.Emit();
163   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnReset);
164 }
165
166 void OffscreenApplication::OnLanguageChanged()
167 {
168   mAdaptor->NotifyLanguageChanged();
169
170   Dali::OffscreenApplication application(this);
171   mLanguageChangedSignal.Emit();
172   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnLanguageChanged);
173 }
174
175 void OffscreenApplication::QuitFromMainLoop()
176 {
177   mAdaptor->Stop();
178
179   mFramework->Quit();
180   // This will trigger OnTerminate(), below, after the main loop has completed.
181 }
182
183 } // namespace Internal
184
185 } // namespace Dali