DALi Version 1.4.8
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
1 /*
2  * Copyright (c) 2018 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/event/common/stage-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <cmath>
24 #include <cstring> // for strcmp
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/event/actors/layer-impl.h>
28 #include <dali/internal/event/actors/layer-list.h>
29 #include <dali/internal/event/actors/camera-actor-impl.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
31 #include <dali/internal/event/common/property-notification-manager.h>
32 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
33 #include <dali/internal/event/update/frame-callback-interface-impl.h>
34 #include <dali/internal/update/nodes/node.h>
35 #include <dali/internal/update/manager/scene-graph-frame-callback.h>
36 #include <dali/internal/event/common/object-registry-impl.h>
37 #include <dali/integration-api/platform-abstraction.h>
38 #include <dali/public-api/common/constants.h>
39 #include <dali/public-api/events/touch-data.h>
40 #include <dali/public-api/object/type-registry.h>
41 #include <dali/public-api/render-tasks/render-task-list.h>
42 #include <dali/public-api/rendering/frame-buffer.h>
43
44 using Dali::Internal::SceneGraph::Node;
45
46 namespace
47 {
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DEPTH_TIMER" );
50 #endif
51 }
52
53 namespace Dali
54 {
55
56 namespace Internal
57 {
58
59 namespace
60 {
61
62 // Signals
63
64 const char* const SIGNAL_KEY_EVENT =                 "keyEvent";
65 const char* const SIGNAL_KEY_EVENT_GENERATED =       "keyEventGenerated";
66 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
67 const char* const SIGNAL_TOUCHED =                   "touched";
68 const char* const SIGNAL_TOUCH =                     "touch";
69 const char* const SIGNAL_WHEEL_EVENT =               "wheelEvent";
70 const char* const SIGNAL_CONTEXT_LOST =              "contextLost";
71 const char* const SIGNAL_CONTEXT_REGAINED =          "contextRegained";
72 const char* const SIGNAL_SCENE_CREATED =             "sceneCreated";
73
74 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
75
76 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT,                 &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED,                   &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT,               &Stage::DoConnectSignal );
80 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST,              &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED,          &Stage::DoConnectSignal );
82 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED,             &Stage::DoConnectSignal );
83 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED,       &Stage::DoConnectSignal );
84 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH,                     &Stage::DoConnectSignal );
85
86 } // unnamed namespace
87
88 StagePtr Stage::New( AnimationPlaylist& playlist,
89                      PropertyNotificationManager& propertyNotificationManager,
90                      SceneGraph::UpdateManager& updateManager,
91                      NotificationManager& notificationManager,
92                      Integration::RenderController& renderController )
93 {
94   return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager, renderController ) );
95 }
96
97 void Stage::Initialize( bool renderToFbo )
98 {
99   mRenderToFbo = renderToFbo;
100   mObjectRegistry = ObjectRegistry::New();
101
102   // Create the ordered list of layers
103   mLayerList = LayerList::New( mUpdateManager );
104
105   // The stage owns the default layer
106   mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager );
107   mRootLayer->SetName("RootLayer");
108   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
109   // This stops actors parented to the stage having their relayout requests propagating
110   // up to the root layer, and down through other children unnecessarily.
111   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
112
113   // Create the default camera actor first; this is needed by the RenderTaskList
114   CreateDefaultCameraActor();
115
116   // Create the list of render-tasks
117   mRenderTaskList = RenderTaskList::New();
118
119   // Create the default render-task (don't need the returned handle)
120   mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
121 }
122
123 void Stage::Uninitialize()
124 {
125   if( mDefaultCamera )
126   {
127     // its enough to release the handle so the object is released
128     // don't need to remove it from root actor as root actor will delete the object
129     mDefaultCamera.Reset();
130   }
131
132   if( mRootLayer )
133   {
134     // we are closing down so just delete the root, no point emit disconnect
135     // signals or send messages to update
136     mRootLayer.Reset();
137   }
138
139   if( mRenderTaskList )
140   {
141     mRenderTaskList.Reset();
142   }
143 }
144
145 StagePtr Stage::GetCurrent()
146 {
147   StagePtr stage( NULL );
148   // no checking in this version
149   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
150   if( tls )
151   {
152     stage = tls->GetCurrentStage();
153   }
154   return stage;
155 }
156
157 bool Stage::IsInstalled()
158 {
159   return ThreadLocalStorage::Created();
160 }
161
162 ObjectRegistry& Stage::GetObjectRegistry()
163 {
164   return *mObjectRegistry;
165 }
166
167 void Stage::RegisterObject( Dali::BaseObject* object )
168 {
169   mObjectRegistry->RegisterObject( object );
170 }
171
172 void Stage::UnregisterObject( Dali::BaseObject* object )
173 {
174   mObjectRegistry->UnregisterObject( object );
175 }
176
177 Layer& Stage::GetRootActor()
178 {
179   return *mRootLayer;
180 }
181
182 AnimationPlaylist& Stage::GetAnimationPlaylist()
183 {
184   return mAnimationPlaylist;
185 }
186
187 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
188 {
189   return mPropertyNotificationManager;
190 }
191
192 void Stage::Add( Actor& actor )
193 {
194   mRootLayer->Add( actor );
195 }
196
197 void Stage::Remove( Actor& actor )
198 {
199   mRootLayer->Remove( actor );
200 }
201
202 void Stage::SurfaceResized( float width, float height )
203 {
204   if( ( fabsf( width - mSurfaceSize.width ) > Math::MACHINE_EPSILON_1000 ) || ( fabsf( height - mSurfaceSize.height ) > Math::MACHINE_EPSILON_1000 ) )
205   {
206     mSurfaceSize.width = width;
207     mSurfaceSize.height = height;
208
209     // Internally we want to report the actual size of the stage.
210     mSize.width = width;
211     mSize.height = height - static_cast<float>( mTopMargin );
212
213     // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
214     mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
215
216     // Adjust the camera height to allow for top-margin
217     SetDefaultCameraPosition();
218
219     mRootLayer->SetSize( mSize.width, mSize.height );
220
221     SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int32_t>( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
222
223     // if single render task to screen then set its viewport parameters
224     if( 1 == mRenderTaskList->GetTaskCount() )
225     {
226       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
227
228       if(!defaultRenderTask->GetTargetFrameBuffer())
229       {
230         defaultRenderTask->SetViewport( Viewport( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
231       }
232     }
233
234     if( mRenderToFbo )
235     {
236       Dali::FrameBuffer frameBuffer = Dali::FrameBuffer::New( static_cast<uint32_t>( width ), static_cast<uint32_t>( height ), Dali::FrameBuffer::Attachment::NONE );
237       Dali::Texture texture = Dali::Texture::New( Dali::TextureType::TEXTURE_2D, Dali::Pixel::RGB888, static_cast<uint32_t>( width ), static_cast<uint32_t>( height ) );
238       frameBuffer.AttachColorTexture( texture );
239
240       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
241       defaultRenderTask->SetFrameBuffer( &GetImplementation( frameBuffer ) );
242     }
243   }
244 }
245
246 Vector2 Stage::GetSize() const
247 {
248   return mSize;
249 }
250
251 void Stage::SetTopMargin( uint32_t margin )
252 {
253   if (mTopMargin == margin)
254   {
255     return;
256   }
257   mTopMargin = margin;
258
259   mSize.width = mSurfaceSize.width;
260   mSize.height = mSurfaceSize.height - static_cast<float>( mTopMargin );
261
262   // Adjust the camera height to allow for top-margin
263   SetDefaultCameraPosition();
264
265   mRootLayer->SetSize( mSize.width, mSize.height );
266 }
267
268 RenderTaskList& Stage::GetRenderTaskList() const
269 {
270   return *mRenderTaskList;
271 }
272
273 void Stage::CreateDefaultCameraActor()
274 {
275   // The default camera attributes and position is such that
276   // children of the default layer, can be positioned at (0,0) and
277   // be at the top-left of the viewport.
278   mDefaultCamera = CameraActor::New( Size::ZERO );
279   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
280   Add(*(mDefaultCamera.Get()));
281 }
282
283 void Stage::SetDefaultCameraPosition()
284 {
285   mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
286 }
287
288 Actor& Stage::GetDefaultRootActor()
289 {
290   return *mRootLayer;
291 }
292
293 CameraActor& Stage::GetDefaultCameraActor()
294 {
295   return *mDefaultCamera;
296 }
297
298 uint32_t Stage::GetLayerCount() const
299 {
300   return mLayerList->GetLayerCount();
301 }
302
303 Dali::Layer Stage::GetLayer( uint32_t depth ) const
304 {
305   return Dali::Layer(mLayerList->GetLayer( depth ));
306 }
307
308 Dali::Layer Stage::GetRootLayer() const
309 {
310   return Dali::Layer( mRootLayer.Get() );
311 }
312
313 LayerList& Stage::GetLayerList()
314 {
315   return *mLayerList;
316 }
317
318 void Stage::SetBackgroundColor(Vector4 color)
319 {
320   // Cache for public GetBackgroundColor()
321   mBackgroundColor = color;
322
323   // Send message to change color in next frame
324   SetBackgroundColorMessage( mUpdateManager, color );
325 }
326
327 Vector4 Stage::GetBackgroundColor() const
328 {
329   return mBackgroundColor;
330 }
331
332 Vector2 Stage::GetDpi() const
333 {
334   return mDpi;
335 }
336
337 void Stage::SetDpi(Vector2 dpi)
338 {
339   mDpi = dpi;
340 }
341
342 void Stage::KeepRendering( float durationSeconds )
343 {
344   // Send message to keep rendering
345   KeepRenderingMessage( mUpdateManager, durationSeconds );
346 }
347
348 void Stage::SetRenderingBehavior( DevelStage::Rendering renderingBehavior )
349 {
350   if( mRenderingBehavior != renderingBehavior )
351   {
352     // Send message to change the rendering behavior
353     SetRenderingBehaviorMessage( mUpdateManager, renderingBehavior );
354
355     mRenderingBehavior = renderingBehavior;
356   }
357 }
358
359 DevelStage::Rendering Stage::GetRenderingBehavior() const
360 {
361   return mRenderingBehavior;
362 }
363
364 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
365 {
366   bool connected( true );
367   Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
368
369   if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
370   {
371     stage->KeyEventSignal().Connect( tracker, functor );
372   }
373   else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
374   {
375     stage->KeyEventGeneratedSignal().Connect( tracker, functor );
376   }
377   else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
378   {
379     stage->EventProcessingFinishedSignal().Connect( tracker, functor );
380   }
381   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
382   {
383     stage->TouchedSignal().Connect( tracker, functor );
384   }
385   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
386   {
387     stage->TouchSignal().Connect( tracker, functor );
388   }
389   else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
390   {
391     stage->WheelEventSignal().Connect( tracker, functor );
392   }
393   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
394   {
395     stage->ContextLostSignal().Connect( tracker, functor );
396   }
397   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
398   {
399     stage->ContextRegainedSignal().Connect( tracker, functor );
400   }
401   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
402   {
403     stage->SceneCreatedSignal().Connect( tracker, functor );
404   }
405   else
406   {
407     // signalName does not match any signal
408     connected = false;
409   }
410
411   return connected;
412 }
413
414 void Stage::EmitKeyEventSignal(const KeyEvent& event)
415 {
416   // Emit the key event signal when no actor in the stage has gained the key input focus
417
418   mKeyEventSignal.Emit( event );
419 }
420
421 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
422 {
423   // Emit the KeyEventGenerated signal when KeyEvent is generated
424
425   return mKeyEventGeneratedSignal.Emit( event );
426 }
427
428 void Stage::EmitEventProcessingFinishedSignal()
429 {
430    mEventProcessingFinishedSignal.Emit();
431 }
432
433 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
434 {
435   mTouchedSignal.Emit( touchEvent );
436   mTouchSignal.Emit( touch );
437 }
438
439 void Stage::EmitWheelEventSignal(const WheelEvent& event)
440 {
441   // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
442
443   mWheelEventSignal.Emit( event );
444 }
445
446 void Stage::EmitSceneCreatedSignal()
447 {
448   mSceneCreatedSignal.Emit();
449 }
450
451 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
452 {
453   return mKeyEventSignal;
454 }
455
456 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
457 {
458   return mKeyEventGeneratedSignal;
459 }
460
461 void Stage::AddFrameCallback( FrameCallbackInterface& frameCallback, Actor& rootActor )
462 {
463   DALI_ASSERT_ALWAYS( ( ! FrameCallbackInterface::Impl::Get( frameCallback ).IsConnectedToSceneGraph() )
464                       && "FrameCallbackInterface implementation already added" );
465
466   // Create scene-graph object and transfer to UpdateManager
467   OwnerPointer< SceneGraph::FrameCallback > transferOwnership( SceneGraph::FrameCallback::New( frameCallback ) );
468   AddFrameCallbackMessage( mUpdateManager, transferOwnership, rootActor.GetNode() );
469 }
470
471 void Stage::RemoveFrameCallback( FrameCallbackInterface& frameCallback )
472 {
473   FrameCallbackInterface::Impl::Get( frameCallback ).Invalidate();
474   RemoveFrameCallbackMessage( mUpdateManager, frameCallback );
475 }
476
477 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
478 {
479   return mEventProcessingFinishedSignal;
480 }
481
482 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
483 {
484   DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
485   return mTouchedSignal;
486 }
487
488 Dali::Stage::TouchSignalType& Stage::TouchSignal()
489 {
490   return mTouchSignal;
491 }
492
493 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
494 {
495   return mWheelEventSignal;
496 }
497
498 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
499 {
500   return mContextLostSignal;
501 }
502
503 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
504 {
505   return mContextRegainedSignal;
506 }
507
508 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
509 {
510   return mSceneCreatedSignal;
511 }
512
513 void Stage::NotifyContextLost()
514 {
515   mContextLostSignal.Emit();
516 }
517
518 void Stage::NotifyContextRegained()
519 {
520   mContextRegainedSignal.Emit();
521 }
522
523
524 void Stage::RequestRebuildDepthTree()
525 {
526   DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
527   mDepthTreeDirty = true;
528 }
529
530 void Stage::RebuildDepthTree()
531 {
532   // If the depth tree needs rebuilding, do it in this frame only.
533   if( mDepthTreeDirty )
534   {
535     DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
536
537     ActorPtr actor( mRootLayer.Get() );
538     actor->RebuildDepthTree();
539     mDepthTreeDirty = false;
540   }
541 }
542
543
544 Stage::Stage( AnimationPlaylist& playlist,
545               PropertyNotificationManager& propertyNotificationManager,
546               SceneGraph::UpdateManager& updateManager,
547               NotificationManager& notificationManager,
548               Integration::RenderController& renderController )
549 : mAnimationPlaylist( playlist ),
550   mPropertyNotificationManager( propertyNotificationManager ),
551   mUpdateManager( updateManager ),
552   mNotificationManager( notificationManager ),
553   mRenderController( renderController ),
554   mSize( Vector2::ZERO ),
555   mSurfaceSize( Vector2::ZERO ),
556   mBackgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
557   mTopMargin( 0 ),
558   mDpi( Vector2::ZERO ),
559   mKeyEventSignal(),
560   mKeyEventGeneratedSignal(),
561   mEventProcessingFinishedSignal(),
562   mTouchedSignal(),
563   mTouchSignal(),
564   mWheelEventSignal(),
565   mContextLostSignal(),
566   mContextRegainedSignal(),
567   mSceneCreatedSignal(),
568   mRenderingBehavior( DevelStage::Rendering::IF_REQUIRED ),
569   mDepthTreeDirty( false ),
570   mForceNextUpdate( false ),
571   mRenderToFbo( false )
572 {
573 }
574
575 SceneGraph::UpdateManager& Stage::GetUpdateManager()
576 {
577   return mUpdateManager;
578 }
579
580 Integration::RenderController& Stage::GetRenderController()
581 {
582   return mRenderController;
583 }
584
585 uint32_t* Stage::ReserveMessageSlot( uint32_t size, bool updateScene )
586 {
587   return mUpdateManager.ReserveMessageSlot( size, updateScene );
588 }
589
590 BufferIndex Stage::GetEventBufferIndex() const
591 {
592   return mUpdateManager.GetEventBufferIndex();
593 }
594
595 void Stage::ForceNextUpdate()
596 {
597   mForceNextUpdate = true;
598 }
599
600 bool Stage::IsNextUpdateForced()
601 {
602   bool nextUpdateForced = mForceNextUpdate;
603   mForceNextUpdate = false;
604   return nextUpdateForced;
605 }
606
607 Stage::~Stage()
608 {
609 }
610
611 } // namespace Internal
612
613 } // namespace Dali