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