Remove last reference to boost in dali-core
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
1 /*
2  * Copyright (c) 2014 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/integration-api/system-overlay.h>
28 #include <dali/internal/event/actors/layer-impl.h>
29 #include <dali/internal/event/actors/layer-list.h>
30 #include <dali/internal/event/actors/camera-actor-impl.h>
31 #include <dali/internal/event/actor-attachments/camera-attachment-impl.h>
32 #include <dali/internal/event/common/system-overlay-impl.h>
33 #include <dali/internal/event/common/thread-local-storage.h>
34 #include <dali/internal/event/common/property-notification-manager.h>
35 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
36 #include <dali/internal/update/nodes/node.h>
37 #include <dali/internal/event/common/object-registry-impl.h>
38 #include <dali/integration-api/platform-abstraction.h>
39 #include <dali/public-api/common/constants.h>
40 #include <dali/public-api/object/type-registry.h>
41 #include <dali/public-api/render-tasks/render-task-list.h>
42
43 #ifdef DYNAMICS_SUPPORT
44 #include <dali/internal/event/dynamics/dynamics-world-config-impl.h>
45 #include <dali/internal/event/dynamics/dynamics-world-impl.h>
46 #include <dali/integration-api/dynamics/dynamics-factory-intf.h>
47 #include <dali/integration-api/dynamics/dynamics-world-settings.h>
48 #endif
49
50 using Dali::Internal::SceneGraph::Node;
51
52 namespace Dali
53 {
54
55 namespace Internal
56 {
57
58 namespace
59 {
60
61 const float DEFAULT_STEREO_BASE( 65.0f );
62
63 // Signals
64
65 const char* const SIGNAL_KEY_EVENT =                 "key-event";
66 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "event-processing-finished";
67 const char* const SIGNAL_TOUCHED =                   "touched";
68 const char* const SIGNAL_CONTEXT_LOST =              "context-lost";
69 const char* const SIGNAL_CONTEXT_REGAINED =          "context-regained";
70 const char* const SIGNAL_SCENE_CREATED =             "scene-created";
71
72 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
73
74 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT,                 &Stage::DoConnectSignal );
75 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
76 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED,                   &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector4( mType, SIGNAL_CONTEXT_LOST,              &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_REGAINED,          &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector6( mType, SIGNAL_SCENE_CREATED,             &Stage::DoConnectSignal );
80
81 } // unnamed namespace
82
83 StagePtr Stage::New( AnimationPlaylist& playlist,
84                      PropertyNotificationManager& propertyNotificationManager,
85                      SceneGraph::UpdateManager& updateManager,
86                      NotificationManager& notificationManager )
87 {
88   return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
89 }
90
91 void Stage::Initialize()
92 {
93   mObjectRegistry = ObjectRegistry::New();
94
95   // Create the ordered list of layers
96   mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
97
98   // The stage owns the default layer
99   mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
100   mRootLayer->SetName("RootLayer");
101   mRootLayer->SetRelayoutEnabled( false );   // Exclude from size negotiation
102
103   // Create the default camera actor first; this is needed by the RenderTaskList
104   CreateDefaultCameraActor();
105
106   // Create the list of render-tasks
107   mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
108
109   // Create the default render-task
110   Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
111 }
112
113 void Stage::Uninitialize()
114 {
115   // Remove actors added to SystemOverlay
116   delete mSystemOverlay;
117   mSystemOverlay = NULL;
118
119   if( mDefaultCamera )
120   {
121     Remove(*(mDefaultCamera.Get()));
122   }
123
124   if( mRootLayer )
125   {
126     // we are closing down so just delete the root, no point emit disconnect
127     // signals or send messages to update
128     mRootLayer.Reset();
129   }
130 }
131
132 StagePtr Stage::GetCurrent()
133 {
134   StagePtr stage( NULL );
135   // no checking in this version
136   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
137   if( tls )
138   {
139     stage = tls->GetCurrentStage();
140   }
141   return stage;
142 }
143
144 bool Stage::IsInstalled()
145 {
146   return ThreadLocalStorage::Created();
147 }
148
149 ObjectRegistry& Stage::GetObjectRegistry()
150 {
151   return *mObjectRegistry;
152 }
153
154 void Stage::RegisterObject( Dali::BaseObject* object )
155 {
156   mObjectRegistry->RegisterObject( object );
157 }
158
159 void Stage::UnregisterObject( Dali::BaseObject* object )
160 {
161   mObjectRegistry->UnregisterObject( object );
162 }
163
164 Layer& Stage::GetRootActor()
165 {
166   return *mRootLayer;
167 }
168
169 AnimationPlaylist& Stage::GetAnimationPlaylist()
170 {
171   return mAnimationPlaylist;
172 }
173
174 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
175 {
176   return mPropertyNotificationManager;
177 }
178
179 void Stage::Add( Actor& actor )
180 {
181   mRootLayer->Add( actor );
182 }
183
184 void Stage::Remove( Actor& actor )
185 {
186   mRootLayer->Remove( actor );
187 }
188
189 void Stage::SetSize(float width, float height)
190 {
191   // Internally we want to report the actual size of the stage.
192   mSize.width  = width;
193   mSize.height = height;
194
195   // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
196   mDefaultCamera->SetPerspectiveProjection( mSize );
197
198   // The depth of the stage gets set to the maximun of these values
199   mRootLayer->SetSize( mSize );
200
201   // Repeat for SystemOverlay actors
202   if( mSystemOverlay )
203   {
204     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
205   }
206
207   SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
208 }
209
210 Vector2 Stage::GetSize() const
211 {
212   return mSize;
213 }
214
215 RenderTaskList& Stage::GetRenderTaskList() const
216 {
217   return *mRenderTaskList;
218 }
219
220 void Stage::CreateDefaultCameraActor()
221 {
222   // The default camera attributes and position is such that
223   // children of the default layer, can be positioned at (0,0) and
224   // be at the top-left of the viewport.
225   mDefaultCamera = CameraActor::New( Size::ZERO );
226   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
227   Add(*(mDefaultCamera.Get()));
228 }
229
230 Actor& Stage::GetDefaultRootActor()
231 {
232   return *mRootLayer;
233 }
234
235 CameraActor& Stage::GetDefaultCameraActor()
236 {
237   return *mDefaultCamera;
238 }
239
240 unsigned int Stage::GetLayerCount() const
241 {
242   return mLayerList->GetLayerCount();
243 }
244
245 Dali::Layer Stage::GetLayer( unsigned int depth ) const
246 {
247   return Dali::Layer(mLayerList->GetLayer( depth ));
248 }
249
250 Dali::Layer Stage::GetRootLayer() const
251 {
252   return Dali::Layer( mRootLayer.Get() );
253 }
254
255 LayerList& Stage::GetLayerList()
256 {
257   return *mLayerList;
258 }
259
260 Integration::SystemOverlay& Stage::GetSystemOverlay()
261 {
262   // Lazily create system-level if requested
263   if( !mSystemOverlay )
264   {
265     mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
266     DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
267
268     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
269   }
270
271   return *mSystemOverlay;
272 }
273
274 SystemOverlay* Stage::GetSystemOverlayInternal()
275 {
276   SystemOverlay* overlay( NULL );
277
278   if( mSystemOverlay )
279   {
280     overlay = mSystemOverlay->GetImpl();
281   }
282
283   return overlay;
284 }
285
286 void Stage::SetViewMode( ViewMode viewMode )
287 {
288   if( mViewMode != viewMode )
289   {
290     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
291
292     if( mViewMode == MONO )
293     {
294       mDefaultCamera->SetOrientation( Degree( 180.0f ), Vector3::YAXIS );
295       mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
296
297       //Create camera and RenderTask for left eye
298       mLeftCamera = CameraActor::New( Size::ZERO );
299       mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
300       mDefaultCamera->Add( *mLeftCamera.Get() );
301       mLeftRenderTask = mRenderTaskList->CreateTask();
302       mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
303       mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
304
305       //Create camera and RenderTask for right eye
306       mRightCamera = CameraActor::New( Size::ZERO );
307       mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
308       mDefaultCamera->Add( *mRightCamera.Get() );
309       mRightRenderTask = mRenderTaskList->CreateTask();
310       mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
311
312       mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
313       mRightCamera->SetType( Dali::Camera::FREE_LOOK );
314     }
315
316     // save new mode
317     mViewMode = viewMode;
318
319     switch( viewMode )
320     {
321       case MONO:
322       {
323         // delete extra stereoscopic render tasks and cameras
324         mRenderTaskList->RemoveTask( mLeftRenderTask );
325         mDefaultCamera->Remove( *mLeftCamera.Get() );
326         mLeftRenderTask.Reset();
327         mLeftCamera.Reset();
328         mRenderTaskList->RemoveTask( mRightRenderTask );
329         mDefaultCamera->Remove( *mRightCamera.Get() );
330         mRightRenderTask.Reset();
331         mRightCamera.Reset();
332
333         mDefaultCamera->SetOrientation( Degree( 0.0f ), Vector3::YAXIS );
334         mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
335         mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
336
337         break;
338       }
339       case STEREO_HORIZONTAL:
340       {
341         //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
342         //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
343
344         //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
345         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
346
347         //Calculate aspect ratio
348         float aspect = mSize.width / (mSize.height * 0.5f);
349
350         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
351         mLeftCamera->SetAspectRatio( aspect );
352         mLeftCamera->SetOrientation( Degree(-90.0f), Vector3::ZAXIS );
353         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
354         mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
355
356         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0,  -stereoBase) );
357         mRightCamera->SetAspectRatio( aspect );
358         mRightCamera->SetOrientation( Degree(-90.0f), Vector3::ZAXIS );
359         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
360         mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
361
362         break;
363       }
364       case STEREO_VERTICAL:
365       {
366         //Calculate separation in pixels along horizontal axis
367         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
368
369         //Recalculate fov based on viewport size
370         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
371
372         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
373         mLeftCamera->SetFieldOfView( fov );
374         mLeftCamera->SetOrientation( Degree(0.0f), Vector3::ZAXIS );
375         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
376         mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
377
378         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
379         mRightCamera->SetFieldOfView( fov );
380         mRightCamera->SetOrientation( Degree(0.0f), Vector3::ZAXIS );
381         mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
382         mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
383
384         break;
385       }
386       case STEREO_INTERLACED:
387       {
388         break;
389       }
390     }
391   }
392 }
393
394 ViewMode Stage::GetViewMode() const
395 {
396   return mViewMode;
397 }
398
399 void Stage::SetStereoBase( float stereoBase )
400 {
401   if( ! Equals( mStereoBase, stereoBase ) )
402   {
403     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
404     mStereoBase = stereoBase;
405
406     switch( mViewMode  )
407     {
408       case STEREO_HORIZONTAL:
409       {
410         stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
411         float aspect = mSize.width / (mSize.height * 0.5f);
412
413         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
414         mLeftCamera->SetAspectRatio( aspect );
415         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
416
417         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
418         mRightCamera->SetAspectRatio( aspect );
419         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
420
421         break;
422       }
423       case STEREO_VERTICAL:
424       {
425         stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
426         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
427
428         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
429         mLeftCamera->SetFieldOfView( fov );
430         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
431
432         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
433         mRightCamera->SetFieldOfView( fov );
434         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
435
436         break;
437       }
438       default:
439         break;
440     }
441   }
442 }
443
444 float Stage::GetStereoBase() const
445 {
446   return mStereoBase;
447 }
448
449 void Stage::SetBackgroundColor(Vector4 color)
450 {
451   // Cache for public GetBackgroundColor()
452   mBackgroundColor = color;
453
454   // Send message to change color in next frame
455   SetBackgroundColorMessage( mUpdateManager, color );
456 }
457
458 Vector4 Stage::GetBackgroundColor() const
459 {
460   return mBackgroundColor;
461 }
462
463 Vector2 Stage::GetDpi() const
464 {
465   return mDpi;
466 }
467
468 void Stage::SetDpi(Vector2 dpi)
469 {
470   mDpi = dpi;
471 }
472
473 #ifdef DYNAMICS_SUPPORT
474
475 DynamicsNotifier& Stage::GetDynamicsNotifier()
476 {
477   return mDynamicsNotifier;
478 }
479
480 DynamicsWorldPtr Stage::InitializeDynamics(DynamicsWorldConfigPtr config)
481 {
482   if( !mDynamicsFactory )
483   {
484     mDynamicsFactory = ThreadLocalStorage::Get().GetPlatformAbstraction().GetDynamicsFactory();
485   }
486
487   if( mDynamicsFactory && !mDynamicsWorld )
488   {
489     if( mDynamicsFactory->InitializeDynamics( *(config->GetSettings()) ) )
490     {
491       mDynamicsWorld = DynamicsWorld::New();
492       mDynamicsWorld->Initialize( *this, *mDynamicsFactory, config );
493     }
494   }
495   return mDynamicsWorld;
496 }
497
498 DynamicsWorldPtr Stage::GetDynamicsWorld()
499 {
500   return mDynamicsWorld;
501 }
502
503 void Stage::TerminateDynamics()
504 {
505   if( mDynamicsWorld )
506   {
507     mDynamicsWorld->Terminate(*this);
508     mDynamicsWorld = NULL;
509   }
510 }
511
512 #endif // DYNAMICS_SUPPORT
513
514 void Stage::KeepRendering( float durationSeconds )
515 {
516   // Send message to keep rendering
517   KeepRenderingMessage( mUpdateManager, durationSeconds );
518 }
519
520 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
521 {
522   bool connected( true );
523   Stage* stage = dynamic_cast<Stage*>(object);
524
525   if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
526   {
527     stage->KeyEventSignal().Connect( tracker, functor );
528   }
529   else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
530   {
531     stage->EventProcessingFinishedSignal().Connect( tracker, functor );
532   }
533   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
534   {
535     stage->TouchedSignal().Connect( tracker, functor );
536   }
537   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
538   {
539     stage->ContextLostSignal().Connect( tracker, functor );
540   }
541   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
542   {
543     stage->ContextRegainedSignal().Connect( tracker, functor );
544   }
545   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
546   {
547     stage->SceneCreatedSignal().Connect( tracker, functor );
548   }
549   else
550   {
551     // signalName does not match any signal
552     connected = false;
553   }
554
555   return connected;
556 }
557
558 void Stage::EmitKeyEventSignal(const KeyEvent& event)
559 {
560   // Emit the key event signal when no actor in the stage has gained the key input focus
561
562   mKeyEventSignal.Emit( event );
563 }
564
565 void Stage::EmitEventProcessingFinishedSignal()
566 {
567    mEventProcessingFinishedSignal.Emit();
568 }
569
570 void Stage::EmitTouchedSignal( const TouchEvent& touch )
571 {
572   mTouchedSignal.Emit( touch );
573 }
574
575
576 void Stage::EmitSceneCreatedSignal()
577 {
578   mSceneCreatedSignal.Emit();
579 }
580
581 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
582 {
583   return mKeyEventSignal;
584 }
585
586 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
587 {
588   return mEventProcessingFinishedSignal;
589 }
590
591 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
592 {
593   return mTouchedSignal;
594 }
595
596 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
597 {
598   return mContextLostSignal;
599 }
600
601 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
602 {
603   return mContextRegainedSignal;
604 }
605
606 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
607 {
608   return mSceneCreatedSignal;
609 }
610
611 void Stage::NotifyContextLost()
612 {
613   mContextLostSignal.Emit();
614 }
615
616 void Stage::NotifyContextRegained()
617 {
618   mContextRegainedSignal.Emit();
619 }
620
621 Stage::Stage( AnimationPlaylist& playlist,
622               PropertyNotificationManager& propertyNotificationManager,
623               SceneGraph::UpdateManager& updateManager,
624               NotificationManager& notificationManager )
625 : mAnimationPlaylist( playlist ),
626   mPropertyNotificationManager(propertyNotificationManager),
627   mUpdateManager(updateManager),
628   mNotificationManager(notificationManager),
629   mSize(Vector2::ZERO),
630   mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
631   mViewMode( MONO ),
632   mStereoBase( DEFAULT_STEREO_BASE ),
633 #ifdef DYNAMICS_SUPPORT
634   mDynamicsFactory(NULL),
635 #endif
636   mSystemOverlay(NULL)
637 {
638 }
639
640 SceneGraph::UpdateManager& Stage::GetUpdateManager()
641 {
642   return mUpdateManager;
643 }
644
645 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
646 {
647   return mUpdateManager.ReserveMessageSlot( size, updateScene );
648 }
649
650 BufferIndex Stage::GetEventBufferIndex() const
651 {
652   return mUpdateManager.GetEventBufferIndex();
653 }
654
655 Stage::~Stage()
656 {
657   delete mSystemOverlay;
658
659   mObjectRegistry.Reset();
660 }
661
662 } // namespace Internal
663
664 } // namespace Dali