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