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