0b2682cbf799c35602597999d062c70331473980
[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/thread-controller-interface.h>
29 #include <dali/internal/offscreen/common/offscreen-window-impl.h>
30 #include <dali/internal/system/common/environment-variables.h>
31 #include <dali/internal/window-system/common/window-system.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 using RenderMode = Dali::OffscreenApplication::RenderMode;
38
39 IntrusivePtr<OffscreenApplication> OffscreenApplication::New(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent, RenderMode renderMode)
40 {
41   IntrusivePtr<OffscreenApplication> offscreenApplication = new OffscreenApplication(width, height, surface, isTranslucent, renderMode);
42   return offscreenApplication;
43 }
44
45 OffscreenApplication::OffscreenApplication(uint16_t width, uint16_t height, Dali::Any surface, bool isTranslucent, RenderMode renderMode)
46 {
47   // Disable partial update
48   EnvironmentVariable::SetEnvironmentVariable(DALI_ENV_DISABLE_PARTIAL_UPDATE, "1");
49
50   // Disable ATSPI
51   Dali::Accessibility::Bridge::DisableAutoInit();
52
53   // Now we assume separated main loop for the offscreen application
54   mFrameworkFactory = std::unique_ptr<Adaptor::FrameworkFactory>(Dali::Internal::Adaptor::CreateFrameworkFactory());
55   mFramework        = mFrameworkFactory->CreateFramework(Internal::Adaptor::FrameworkBackend::GLIB, *this, *this, nullptr, nullptr, Adaptor::Framework::NORMAL, false);
56
57   // Generate a default window
58   IntrusivePtr<Internal::OffscreenWindow> impl = Internal::OffscreenWindow::New(width, height, surface, isTranslucent);
59   mDefaultWindow                               = Dali::OffscreenWindow(impl.Get());
60
61   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));
62
63   // Initialize default window
64   impl->Initialize(true);
65 }
66
67 OffscreenApplication::~OffscreenApplication()
68 {
69 }
70
71 void OffscreenApplication::MainLoop()
72 {
73   mFramework->Run();
74 }
75
76 void OffscreenApplication::Quit()
77 {
78   // Actually quit the application.
79   // Force a call to Quit even if adaptor is not running.
80   Internal::Adaptor::Adaptor::GetImplementation(*mAdaptor).AddIdle(MakeCallback(this, &OffscreenApplication::QuitFromMainLoop), false, true);
81 }
82
83 Dali::OffscreenWindow OffscreenApplication::GetWindow()
84 {
85   return mDefaultWindow;
86 }
87
88 void OffscreenApplication::RenderOnce()
89 {
90   mAdaptor->RenderOnce();
91 }
92
93 Any OffscreenApplication::GetFrameworkContext() const
94 {
95   return mFramework->GetMainLoopContext();
96 }
97
98 void OffscreenApplication::OnInit()
99 {
100   // Start the adaptor
101   mAdaptor->Start();
102
103   mInitSignal.Emit();
104
105   mAdaptor->NotifySceneCreated();
106 }
107
108 void OffscreenApplication::OnTerminate()
109 {
110   mTerminateSignal.Emit();
111
112   // Stop the adaptor
113   mAdaptor->Stop();
114
115   mDefaultWindow.Reset();
116 }
117
118 void OffscreenApplication::QuitFromMainLoop()
119 {
120   mAdaptor->Stop();
121
122   mFramework->Quit();
123   // This will trigger OnTerminate(), below, after the main loop has completed.
124 }
125
126 } // namespace Internal
127
128 } // namespace Dali