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