[dali_1.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
1 /*
2  * Copyright (c) 2016 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/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/object/type-registry.h>
40 #include <dali/public-api/render-tasks/render-task-list.h>
41
42 using Dali::Internal::SceneGraph::Node;
43
44 namespace Dali
45 {
46
47 namespace Internal
48 {
49
50 namespace
51 {
52
53 const float DEFAULT_STEREO_BASE( 65.0f );
54
55 // Signals
56
57 const char* const SIGNAL_KEY_EVENT =                 "keyEvent";
58 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
59 const char* const SIGNAL_TOUCHED =                   "touched";
60 const char* const SIGNAL_WHEEL_EVENT =               "wheelEvent";
61 const char* const SIGNAL_CONTEXT_LOST =              "contextLost";
62 const char* const SIGNAL_CONTEXT_REGAINED =          "contextRegained";
63 const char* const SIGNAL_SCENE_CREATED =             "sceneCreated";
64
65 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
66
67 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT,                 &Stage::DoConnectSignal );
68 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
69 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED,                   &Stage::DoConnectSignal );
70 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT,               &Stage::DoConnectSignal );
71 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST,              &Stage::DoConnectSignal );
72 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED,          &Stage::DoConnectSignal );
73 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED,             &Stage::DoConnectSignal );
74
75 } // unnamed namespace
76
77 StagePtr Stage::New( AnimationPlaylist& playlist,
78                      PropertyNotificationManager& propertyNotificationManager,
79                      SceneGraph::UpdateManager& updateManager,
80                      NotificationManager& notificationManager )
81 {
82   return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
83 }
84
85 void Stage::Initialize()
86 {
87   mObjectRegistry = ObjectRegistry::New();
88
89   // Create the ordered list of layers
90   mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
91
92   // The stage owns the default layer
93   mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
94   mRootLayer->SetName("RootLayer");
95   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
96   // This stops actors parented to the stage having their relayout requests propagating
97   // up to the root layer, and down through other children unnecessarily.
98   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
99
100   // Create the default camera actor first; this is needed by the RenderTaskList
101   CreateDefaultCameraActor();
102
103   // Create the list of render-tasks
104   mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
105
106   // Create the default render-task
107   Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
108 }
109
110 void Stage::Uninitialize()
111 {
112   // Remove actors added to SystemOverlay
113   delete mSystemOverlay;
114   mSystemOverlay = NULL;
115
116   if( mDefaultCamera )
117   {
118     // its enough to release the handle so the object is released
119     // don't need to remove it from root actor as root actor will delete the object
120     mDefaultCamera.Reset();
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   // if single render task to screen then set its viewport parameters
209   if( 1 == mRenderTaskList->GetTaskCount() )
210   {
211     Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
212
213     if(!mDefaultRenderTask.GetTargetFrameBuffer())
214     {
215       mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
216     }
217   }
218
219 }
220
221 Vector2 Stage::GetSize() const
222 {
223   return mSize;
224 }
225
226 RenderTaskList& Stage::GetRenderTaskList() const
227 {
228   return *mRenderTaskList;
229 }
230
231 void Stage::CreateDefaultCameraActor()
232 {
233   // The default camera attributes and position is such that
234   // children of the default layer, can be positioned at (0,0) and
235   // be at the top-left of the viewport.
236   mDefaultCamera = CameraActor::New( Size::ZERO );
237   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
238   Add(*(mDefaultCamera.Get()));
239 }
240
241 Actor& Stage::GetDefaultRootActor()
242 {
243   return *mRootLayer;
244 }
245
246 CameraActor& Stage::GetDefaultCameraActor()
247 {
248   return *mDefaultCamera;
249 }
250
251 unsigned int Stage::GetLayerCount() const
252 {
253   return mLayerList->GetLayerCount();
254 }
255
256 Dali::Layer Stage::GetLayer( unsigned int depth ) const
257 {
258   return Dali::Layer(mLayerList->GetLayer( depth ));
259 }
260
261 Dali::Layer Stage::GetRootLayer() const
262 {
263   return Dali::Layer( mRootLayer.Get() );
264 }
265
266 LayerList& Stage::GetLayerList()
267 {
268   return *mLayerList;
269 }
270
271 Integration::SystemOverlay& Stage::GetSystemOverlay()
272 {
273   // Lazily create system-level if requested
274   if( !mSystemOverlay )
275   {
276     mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
277     DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
278
279     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
280   }
281
282   return *mSystemOverlay;
283 }
284
285 SystemOverlay* Stage::GetSystemOverlayInternal()
286 {
287   SystemOverlay* overlay( NULL );
288
289   if( mSystemOverlay )
290   {
291     overlay = mSystemOverlay->GetImpl();
292   }
293
294   return overlay;
295 }
296
297 void Stage::SetViewMode( ViewMode viewMode )
298 {
299   if( mViewMode != viewMode )
300   {
301     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
302
303     if( mViewMode == MONO )
304     {
305       mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
306       mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
307
308       //Create camera and RenderTask for left eye
309       mLeftCamera = CameraActor::New( Size::ZERO );
310       mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
311       mDefaultCamera->Add( *mLeftCamera.Get() );
312       mLeftRenderTask = mRenderTaskList->CreateTask();
313       mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
314       mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
315
316       //Create camera and RenderTask for right eye
317       mRightCamera = CameraActor::New( Size::ZERO );
318       mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
319       mDefaultCamera->Add( *mRightCamera.Get() );
320       mRightRenderTask = mRenderTaskList->CreateTask();
321       mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
322
323       mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
324       mRightCamera->SetType( Dali::Camera::FREE_LOOK );
325     }
326
327     // save new mode
328     mViewMode = viewMode;
329
330     switch( viewMode )
331     {
332       case MONO:
333       {
334         // delete extra stereoscopic render tasks and cameras
335         mRenderTaskList->RemoveTask( mLeftRenderTask );
336         mDefaultCamera->Remove( *mLeftCamera.Get() );
337         mLeftRenderTask.Reset();
338         mLeftCamera.Reset();
339         mRenderTaskList->RemoveTask( mRightRenderTask );
340         mDefaultCamera->Remove( *mRightCamera.Get() );
341         mRightRenderTask.Reset();
342         mRightCamera.Reset();
343         mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
344         mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
345         mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
346
347         break;
348       }
349       case STEREO_HORIZONTAL:
350       {
351         //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
352         //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
353
354         //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
355         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
356
357         //Calculate aspect ratio
358         float aspect = mSize.width / (mSize.height * 0.5f);
359
360         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
361         mLeftCamera->SetAspectRatio( aspect );
362
363         mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
364         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
365         mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
366
367         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0,  -stereoBase) );
368         mRightCamera->SetAspectRatio( aspect );
369         mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
370         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
371         mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
372
373         break;
374       }
375       case STEREO_VERTICAL:
376       {
377         //Calculate separation in pixels along horizontal axis
378         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
379
380         //Recalculate fov based on viewport size
381         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
382
383         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
384         mLeftCamera->SetFieldOfView( fov );
385         mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
386         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
387         mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
388
389         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
390         mRightCamera->SetFieldOfView( fov );
391         mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
392         mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
393         mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
394
395         break;
396       }
397       case STEREO_INTERLACED:
398       {
399         break;
400       }
401     }
402   }
403 }
404
405 ViewMode Stage::GetViewMode() const
406 {
407   return mViewMode;
408 }
409
410 void Stage::SetStereoBase( float stereoBase )
411 {
412   if( ! Equals( mStereoBase, stereoBase ) )
413   {
414     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
415     mStereoBase = stereoBase;
416
417     switch( mViewMode  )
418     {
419       case STEREO_HORIZONTAL:
420       {
421         stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
422         float aspect = mSize.width / (mSize.height * 0.5f);
423
424         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
425         mLeftCamera->SetAspectRatio( aspect );
426         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
427
428         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
429         mRightCamera->SetAspectRatio( aspect );
430         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
431
432         break;
433       }
434       case STEREO_VERTICAL:
435       {
436         stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
437         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
438
439         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
440         mLeftCamera->SetFieldOfView( fov );
441         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
442
443         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
444         mRightCamera->SetFieldOfView( fov );
445         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
446
447         break;
448       }
449       default:
450         break;
451     }
452   }
453 }
454
455 float Stage::GetStereoBase() const
456 {
457   return mStereoBase;
458 }
459
460 void Stage::SetBackgroundColor(Vector4 color)
461 {
462   // Cache for public GetBackgroundColor()
463   mBackgroundColor = color;
464
465   // Send message to change color in next frame
466   SetBackgroundColorMessage( mUpdateManager, color );
467 }
468
469 Vector4 Stage::GetBackgroundColor() const
470 {
471   return mBackgroundColor;
472 }
473
474 Vector2 Stage::GetDpi() const
475 {
476   return mDpi;
477 }
478
479 void Stage::SetDpi(Vector2 dpi)
480 {
481   mDpi = dpi;
482 }
483
484 void Stage::KeepRendering( float durationSeconds )
485 {
486   // Send message to keep rendering
487   KeepRenderingMessage( mUpdateManager, durationSeconds );
488 }
489
490 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
491 {
492   bool connected( true );
493   Stage* stage = dynamic_cast<Stage*>(object);
494
495   if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
496   {
497     stage->KeyEventSignal().Connect( tracker, functor );
498   }
499   else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
500   {
501     stage->EventProcessingFinishedSignal().Connect( tracker, functor );
502   }
503   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
504   {
505     stage->TouchedSignal().Connect( tracker, functor );
506   }
507   else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
508   {
509     stage->WheelEventSignal().Connect( tracker, functor );
510   }
511   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
512   {
513     stage->ContextLostSignal().Connect( tracker, functor );
514   }
515   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
516   {
517     stage->ContextRegainedSignal().Connect( tracker, functor );
518   }
519   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
520   {
521     stage->SceneCreatedSignal().Connect( tracker, functor );
522   }
523   else
524   {
525     // signalName does not match any signal
526     connected = false;
527   }
528
529   return connected;
530 }
531
532 void Stage::EmitKeyEventSignal(const KeyEvent& event)
533 {
534   // Emit the key event signal when no actor in the stage has gained the key input focus
535
536   mKeyEventSignal.Emit( event );
537 }
538
539 void Stage::EmitEventProcessingFinishedSignal()
540 {
541    mEventProcessingFinishedSignal.Emit();
542 }
543
544 void Stage::EmitTouchedSignal( const TouchEvent& touch )
545 {
546   mTouchedSignal.Emit( touch );
547 }
548
549 void Stage::EmitWheelEventSignal(const WheelEvent& event)
550 {
551   // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
552
553   mWheelEventSignal.Emit( event );
554 }
555
556 void Stage::EmitSceneCreatedSignal()
557 {
558   mSceneCreatedSignal.Emit();
559 }
560
561 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
562 {
563   return mKeyEventSignal;
564 }
565
566 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
567 {
568   return mEventProcessingFinishedSignal;
569 }
570
571 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
572 {
573   return mTouchedSignal;
574 }
575
576 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
577 {
578   return mWheelEventSignal;
579 }
580
581 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
582 {
583   return mContextLostSignal;
584 }
585
586 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
587 {
588   return mContextRegainedSignal;
589 }
590
591 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
592 {
593   return mSceneCreatedSignal;
594 }
595
596 void Stage::NotifyContextLost()
597 {
598   mContextLostSignal.Emit();
599 }
600
601 void Stage::NotifyContextRegained()
602 {
603   mContextRegainedSignal.Emit();
604 }
605
606 Stage::Stage( AnimationPlaylist& playlist,
607               PropertyNotificationManager& propertyNotificationManager,
608               SceneGraph::UpdateManager& updateManager,
609               NotificationManager& notificationManager )
610 : mAnimationPlaylist( playlist ),
611   mPropertyNotificationManager(propertyNotificationManager),
612   mUpdateManager(updateManager),
613   mNotificationManager(notificationManager),
614   mSize(Vector2::ZERO),
615   mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
616   mViewMode( MONO ),
617   mStereoBase( DEFAULT_STEREO_BASE ),
618   mSystemOverlay(NULL)
619 {
620 }
621
622 SceneGraph::UpdateManager& Stage::GetUpdateManager()
623 {
624   return mUpdateManager;
625 }
626
627 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
628 {
629   return mUpdateManager.ReserveMessageSlot( size, updateScene );
630 }
631
632 BufferIndex Stage::GetEventBufferIndex() const
633 {
634   return mUpdateManager.GetEventBufferIndex();
635 }
636
637 Stage::~Stage()
638 {
639   delete mSystemOverlay;
640
641   mObjectRegistry.Reset();
642 }
643
644 } // namespace Internal
645
646 } // namespace Dali