Revert "[Tizen] Temporarily revert new APIs for Multi-window"
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / scene-holder-impl.cpp
1 /*
2  * Copyright (c) 2019 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/integration-api/scene-holder-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/actors/layer.h>
24
25 // INTERNAL HEADERS
26 #include <dali/internal/adaptor/common/lifecycle-observer.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace Adaptor
35 {
36
37 uint32_t SceneHolder::mSceneHolderCounter = 0;
38
39 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
40 {
41 public:
42
43   SceneHolderLifeCycleObserver(Adaptor*& adaptor)
44   : mAdaptor( adaptor )
45   {
46   };
47
48 private: // Adaptor::LifeCycleObserver interface
49
50   void OnStart() override {};
51   void OnPause() override {};
52   void OnResume() override {};
53   void OnStop() override {};
54   void OnDestroy() override
55   {
56     mAdaptor = nullptr;
57   };
58
59 private:
60   Adaptor*& mAdaptor;
61 };
62
63
64 SceneHolder::SceneHolder()
65 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
66   mId( mSceneHolderCounter++ ),
67   mSurface( nullptr ),
68   mAdaptor( nullptr ),
69   mAdaptorStarted( false ),
70   mVisible( true )
71 {
72 }
73
74 SceneHolder::~SceneHolder()
75 {
76   if ( mAdaptor )
77   {
78     mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
79     mAdaptor->RemoveWindow( this );
80     mAdaptor = nullptr;
81   }
82 }
83
84 void SceneHolder::Add( Dali::Actor actor )
85 {
86   if ( mScene )
87   {
88     mScene.Add( actor );
89   }
90 }
91
92 void SceneHolder::Remove( Dali::Actor actor )
93 {
94   if ( mScene )
95   {
96     mScene.Remove( actor );
97   }
98 }
99
100 Dali::Layer SceneHolder::GetRootLayer() const
101 {
102   return mScene ? mScene.GetRootLayer() : Dali::Layer();
103 }
104
105 uint32_t SceneHolder::GetId() const
106 {
107   return mId;
108 }
109
110 std::string SceneHolder::GetName() const
111 {
112   return mName;
113 }
114
115 bool SceneHolder::IsVisible() const
116 {
117   return mVisible;
118 }
119
120 Dali::Integration::Scene SceneHolder::GetScene()
121 {
122   return mScene;
123 }
124
125 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
126 {
127   mSurface.reset( surface );
128
129   mScene.SetSurface( *mSurface.get() );
130
131   unsigned int dpiHorizontal, dpiVertical;
132   dpiHorizontal = dpiVertical = 0;
133
134   mSurface->GetDpi( dpiHorizontal, dpiVertical );
135   mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
136
137   mSurface->SetAdaptor( *mAdaptor );
138
139   OnSurfaceSet( surface );
140 }
141
142 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
143 {
144   return mSurface.get();
145 }
146
147 void SceneHolder::SetBackgroundColor( const Vector4& color )
148 {
149   if ( mSurface )
150   {
151     mSurface->SetBackgroundColor( color );
152   }
153 }
154
155 Vector4 SceneHolder::GetBackgroundColor() const
156 {
157   return mSurface ? mSurface->GetBackgroundColor() : Vector4();
158 }
159
160 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
161 {
162   // Avoid doing this more than once
163   if( mAdaptorStarted )
164   {
165     return;
166   }
167
168   mAdaptorStarted = true;
169
170   // Create the scene
171   PositionSize positionSize = mSurface->GetPositionSize();
172   mScene = Dali::Integration::Scene::New( Vector2( positionSize.width, positionSize.height ) );
173   mScene.SetSurface( *mSurface.get() );
174
175   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
176   mAdaptor = &adaptorImpl;
177
178   // Create an observer for the adaptor lifecycle
179   mAdaptor->AddObserver( *mLifeCycleObserver );
180
181   if ( mSurface )
182   {
183     unsigned int dpiHorizontal, dpiVertical;
184     dpiHorizontal = dpiVertical = 0;
185
186     mSurface->GetDpi( dpiHorizontal, dpiVertical );
187     mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
188
189     mSurface->SetAdaptor( *mAdaptor );
190   }
191
192   OnAdaptorSet( adaptor );
193 }
194
195 void SceneHolder::Pause()
196 {
197   OnPause();
198 }
199
200 void SceneHolder::Resume()
201 {
202   OnResume();
203 }
204
205 }// Adaptor
206
207 }// Internal
208
209 } // Dali