Support multiple surfaces for partial update
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / scene-holder-impl.cpp
1 /*
2  * Copyright (c) 2020 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/adaptor-framework/scene-holder-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/time.h>
23 #include <dali/public-api/common/dali-common.h>
24 #include <dali/public-api/actors/actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/events/key-event-integ.h>
28 #include <dali/integration-api/events/touch-event-integ.h>
29 #include <dali/integration-api/events/hover-event-integ.h>
30 #include <dali/integration-api/events/wheel-event-integ.h>
31
32 // INTERNAL INCLUDES
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/adaptor/common/lifecycle-observer.h>
35 #include <dali/internal/graphics/gles/egl-graphics.h>
36 #include <dali/internal/input/common/key-impl.h>
37 #include <dali/internal/input/common/physical-keyboard-impl.h>
38
39 namespace Dali
40 {
41
42 namespace Internal
43 {
44
45 namespace Adaptor
46 {
47
48 namespace
49 {
50
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SCENE_HOLDER" );
53 #endif
54
55 // Copied from x server
56 static uint32_t GetCurrentMilliSeconds(void)
57 {
58   struct timeval tv;
59
60   struct timespec tp;
61   static clockid_t clockid;
62
63   if (!clockid)
64   {
65 #ifdef CLOCK_MONOTONIC_COARSE
66     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
67       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
68     {
69       clockid = CLOCK_MONOTONIC_COARSE;
70     }
71     else
72 #endif
73     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
74     {
75       clockid = CLOCK_MONOTONIC;
76     }
77     else
78     {
79       clockid = ~0L;
80     }
81   }
82   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
83   {
84     return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
85   }
86
87   gettimeofday(&tv, NULL);
88   return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
89 }
90
91 } // unnamed namespace
92
93 uint32_t SceneHolder::mSceneHolderCounter = 0;
94
95 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
96 {
97 public:
98
99   SceneHolderLifeCycleObserver(Adaptor*& adaptor)
100   : mAdaptor( adaptor )
101   {
102   };
103
104 private: // Adaptor::LifeCycleObserver interface
105
106   void OnStart() override {};
107   void OnPause() override {};
108   void OnResume() override {};
109   void OnStop() override {};
110   void OnDestroy() override
111   {
112     mAdaptor = nullptr;
113   };
114
115 private:
116   Adaptor*& mAdaptor;
117 };
118
119
120 SceneHolder::SceneHolder()
121 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
122   mId( mSceneHolderCounter++ ),
123   mSurface( nullptr ),
124   mAdaptor( nullptr ),
125   mDpi(),
126   mIsBeingDeleted( false ),
127   mAdaptorStarted( false ),
128   mVisible( true )
129 {
130 }
131
132 SceneHolder::~SceneHolder()
133 {
134   if ( mAdaptor )
135   {
136     mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
137     mAdaptor->RemoveWindow( this );
138
139     mAdaptor->DeleteSurface( *mSurface.get() );
140
141     mAdaptor = nullptr;
142   }
143
144   if ( mScene )
145   {
146     mScene.Discard();
147   }
148 }
149
150 void SceneHolder::Add( Dali::Actor actor )
151 {
152   if ( mScene )
153   {
154     mScene.Add( actor );
155   }
156 }
157
158 void SceneHolder::Remove( Dali::Actor actor )
159 {
160   if ( mScene )
161   {
162     mScene.Remove( actor );
163   }
164 }
165
166 Dali::Layer SceneHolder::GetRootLayer() const
167 {
168   return mScene ? mScene.GetRootLayer() : Dali::Layer();
169 }
170
171 uint32_t SceneHolder::GetId() const
172 {
173   return mId;
174 }
175
176 std::string SceneHolder::GetName() const
177 {
178   return mName;
179 }
180
181 bool SceneHolder::IsVisible() const
182 {
183   return mVisible;
184 }
185
186 Dali::Integration::Scene SceneHolder::GetScene()
187 {
188   return mScene;
189 }
190
191 Uint16Pair SceneHolder::GetDpi() const
192 {
193   return mDpi;
194 }
195
196 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
197 {
198   mSurface.reset( surface );
199
200   mScene.SurfaceReplaced();
201
202   SurfaceResized();
203
204   InitializeDpi();
205
206   mSurface->SetAdaptor( *mAdaptor );
207   mSurface->SetScene( mScene );
208
209   OnSurfaceSet( surface );
210 }
211
212 void SceneHolder::SurfaceResized()
213 {
214   PositionSize surfacePositionSize = mSurface->GetPositionSize();
215   mScene.SurfaceResized( static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height ) );
216
217   mSurface->SetFullSwapNextFrame();
218 }
219
220 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
221 {
222   return mSurface.get();
223 }
224
225 void SceneHolder::SetBackgroundColor( const Vector4& color )
226 {
227   if( mScene )
228   {
229     mScene.SetBackgroundColor( color );
230
231     mSurface->SetFullSwapNextFrame();
232   }
233 }
234
235 Vector4 SceneHolder::GetBackgroundColor() const
236 {
237   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
238 }
239
240 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
241 {
242   // Avoid doing this more than once
243   if( mAdaptorStarted )
244   {
245     return;
246   }
247
248   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
249
250   mAdaptorStarted = true;
251
252   // Create the scene
253   PositionSize surfacePositionSize = mSurface->GetPositionSize();
254   mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )) );
255
256   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
257   mAdaptor = &adaptorImpl;
258
259   // Create an observer for the adaptor lifecycle
260   mAdaptor->AddObserver( *mLifeCycleObserver );
261
262   InitializeDpi();
263
264   mSurface->SetAdaptor( *mAdaptor );
265   mSurface->SetScene( mScene );
266
267   OnAdaptorSet( adaptor );
268 }
269
270 void SceneHolder::Pause()
271 {
272   Reset();
273
274   OnPause();
275 }
276
277 void SceneHolder::Resume()
278 {
279   Reset();
280
281   OnResume();
282 }
283
284 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
285 {
286   if( timeStamp < 1 )
287   {
288     timeStamp = GetCurrentMilliSeconds();
289   }
290
291   RecalculateTouchPosition( point );
292
293   Integration::TouchEvent touchEvent;
294   Integration::HoverEvent hoverEvent;
295   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
296   if( type != Integration::TouchEventCombiner::DispatchNone )
297   {
298     DALI_LOG_INFO( gSceneHolderLogFilter, Debug::Verbose, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y );
299
300     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
301     // Keep the handle alive until the core events are processed.
302     Dali::BaseHandle sceneHolder( this );
303
304     // First the touch and/or hover event & related gesture events are queued
305     if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
306     {
307       mScene.QueueEvent( touchEvent );
308     }
309
310     if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
311     {
312       mScene.QueueEvent( hoverEvent );
313     }
314
315     // Next the events are processed with a single call into Core
316     mAdaptor->ProcessCoreEvents();
317   }
318 }
319
320 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
321 {
322   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
323   // Keep the handle alive until the core events are processed.
324   Dali::BaseHandle sceneHolder( this );
325
326   mScene.QueueEvent( wheelEvent );
327   mAdaptor->ProcessCoreEvents();
328 }
329
330 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
331 {
332   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
333   if( physicalKeyboard )
334   {
335     if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
336     {
337       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
338     }
339   }
340
341   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
342   // Keep the handle alive until the core events are processed.
343   Dali::BaseHandle sceneHolder( this );
344
345   // Create send KeyEvent to Core.
346   mScene.QueueEvent( keyEvent );
347   mAdaptor->ProcessCoreEvents();
348 }
349
350 void SceneHolder::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
351 {
352   mScene.AddFrameRenderedCallback( std::move( callback ), frameId );
353
354   DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId );
355 }
356
357 void SceneHolder::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
358 {
359   mScene.AddFramePresentedCallback( std::move( callback ), frameId );
360
361   DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId );
362 }
363
364 Dali::Integration::SceneHolder SceneHolder::Get( Dali::Actor actor )
365 {
366   SceneHolder* sceneHolderImpl = nullptr;
367
368   if ( Internal::Adaptor::Adaptor::IsAvailable() )
369   {
370     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
371     sceneHolderImpl = adaptor.GetWindow( actor );
372   }
373
374   return Dali::Integration::SceneHolder( sceneHolderImpl );
375 }
376
377 void SceneHolder::Reset()
378 {
379   mCombiner.Reset();
380
381   // Any touch listeners should be told of the interruption.
382   Integration::TouchEvent event;
383   Integration::Point point;
384   point.SetState( PointState::INTERRUPTED );
385   event.AddPoint( point );
386
387   // First the touch event & related gesture events are queued
388   mScene.QueueEvent( event );
389
390   // Next the events are processed with a single call into Core
391   mAdaptor->ProcessCoreEvents();
392 }
393
394 void SceneHolder::InitializeDpi()
395 {
396   unsigned int dpiHorizontal, dpiVertical;
397   dpiHorizontal = dpiVertical = 0;
398
399   mSurface->GetDpi( dpiHorizontal, dpiVertical );
400   mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
401
402   mDpi.SetX( dpiHorizontal );
403   mDpi.SetY( dpiVertical );
404 }
405
406 }// Adaptor
407
408 }// Internal
409
410 } // Dali