DALi Version 2.3.2
[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   // Force a call to Quit even if adaptor is not running.
93   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &OffscreenApplication::QuitFromMainLoop), false, true);
94 }
95
96 Dali::OffscreenWindow OffscreenApplication::GetWindow()
97 {
98   return mDefaultWindow;
99 }
100
101 void OffscreenApplication::RenderOnce()
102 {
103   mAdaptor->RenderOnce();
104 }
105
106 Any OffscreenApplication::GetFrameworkContext() const
107 {
108   return mFramework->GetMainLoopContext();
109 }
110
111 void OffscreenApplication::OnInit()
112 {
113   // Start the adaptor
114   mAdaptor->Start();
115
116   Dali::OffscreenApplication application(this);
117   mInitSignal.Emit();
118   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnInit);
119
120   mAdaptor->NotifySceneCreated();
121 }
122
123 void OffscreenApplication::OnTerminate()
124 {
125   Dali::OffscreenApplication application(this);
126   mTerminateSignal.Emit();
127   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnTerminate);
128
129   // Stop the adaptor
130   mAdaptor->Stop();
131
132   mDefaultWindow.Reset();
133 }
134
135 void OffscreenApplication::OnPause()
136 {
137   Dali::OffscreenApplication application(this);
138   mPauseSignal.Emit();
139   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnPause);
140 }
141
142 void OffscreenApplication::OnResume()
143 {
144   Dali::OffscreenApplication application(this);
145   mResumeSignal.Emit();
146   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnResume);
147
148   // DALi just delivers the framework Resume event to the application.
149   // Resuming DALi core only occurs on the Window Show framework event
150
151   // Trigger processing of events queued up while paused
152   Internal::Adaptor::CoreEventInterface& coreEventInterface = Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor);
153   coreEventInterface.ProcessCoreEvents();
154 }
155
156 void OffscreenApplication::OnReset()
157 {
158   /*
159    * usually, reset callback was called when a caller request to launch this application via aul.
160    * because Application class already handled initialization in OnInit(), OnReset do nothing.
161    */
162   Dali::OffscreenApplication application(this);
163   mResetSignal.Emit();
164   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnReset);
165 }
166
167 void OffscreenApplication::OnLanguageChanged()
168 {
169   mAdaptor->NotifyLanguageChanged();
170
171   Dali::OffscreenApplication application(this);
172   mLanguageChangedSignal.Emit();
173   EmitLifecycleControllerSignal(&Internal::Adaptor::LifecycleController::OnLanguageChanged);
174 }
175
176 void OffscreenApplication::QuitFromMainLoop()
177 {
178   mAdaptor->Stop();
179
180   mFramework->Quit();
181   // This will trigger OnTerminate(), below, after the main loop has completed.
182 }
183
184 } // namespace Internal
185
186 } // namespace Dali