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