[Tizen] Revert "Remove StereoMode"
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
1 /*
2  * Copyright (c) 2018 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/event/update/frame-callback-interface-impl.h>
36 #include <dali/internal/update/nodes/node.h>
37 #include <dali/internal/update/manager/scene-graph-frame-callback.h>
38 #include <dali/internal/event/common/object-registry-impl.h>
39 #include <dali/integration-api/platform-abstraction.h>
40 #include <dali/public-api/common/constants.h>
41 #include <dali/public-api/events/touch-data.h>
42 #include <dali/public-api/object/type-registry.h>
43 #include <dali/public-api/render-tasks/render-task-list.h>
44 #include <dali/public-api/rendering/frame-buffer.h>
45
46 using Dali::Internal::SceneGraph::Node;
47
48 namespace
49 {
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DEPTH_TIMER" );
52 #endif
53 }
54
55 namespace Dali
56 {
57
58 namespace Internal
59 {
60
61 namespace
62 {
63
64 const float DEFAULT_STEREO_BASE( 65.0f );
65
66 // Signals
67
68 const char* const SIGNAL_KEY_EVENT =                 "keyEvent";
69 const char* const SIGNAL_KEY_EVENT_GENERATED =       "keyEventGenerated";
70 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
71 const char* const SIGNAL_TOUCHED =                   "touched";
72 const char* const SIGNAL_TOUCH =                     "touch";
73 const char* const SIGNAL_WHEEL_EVENT =               "wheelEvent";
74 const char* const SIGNAL_CONTEXT_LOST =              "contextLost";
75 const char* const SIGNAL_CONTEXT_REGAINED =          "contextRegained";
76 const char* const SIGNAL_SCENE_CREATED =             "sceneCreated";
77
78 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
79
80 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT,                 &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
82 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED,                   &Stage::DoConnectSignal );
83 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT,               &Stage::DoConnectSignal );
84 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST,              &Stage::DoConnectSignal );
85 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED,          &Stage::DoConnectSignal );
86 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED,             &Stage::DoConnectSignal );
87 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED,       &Stage::DoConnectSignal );
88 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH,                     &Stage::DoConnectSignal );
89
90 } // unnamed namespace
91
92 StagePtr Stage::New( AnimationPlaylist& playlist,
93                      PropertyNotificationManager& propertyNotificationManager,
94                      SceneGraph::UpdateManager& updateManager,
95                      NotificationManager& notificationManager,
96                      Integration::RenderController& renderController )
97 {
98   return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager, renderController ) );
99 }
100
101 void Stage::Initialize( bool renderToFbo )
102 {
103   mRenderToFbo = renderToFbo;
104   mObjectRegistry = ObjectRegistry::New();
105
106   // Create the ordered list of layers
107   mLayerList = LayerList::New( mUpdateManager );
108
109   // The stage owns the default layer
110   mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager );
111   mRootLayer->SetName("RootLayer");
112   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
113   // This stops actors parented to the stage having their relayout requests propagating
114   // up to the root layer, and down through other children unnecessarily.
115   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
116
117   // Create the default camera actor first; this is needed by the RenderTaskList
118   CreateDefaultCameraActor();
119
120   // Create the list of render-tasks
121   mRenderTaskList = RenderTaskList::New();
122
123   // Create the default render-task
124   Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
125 }
126
127 void Stage::Uninitialize()
128 {
129   // Remove actors added to SystemOverlay
130   delete mSystemOverlay;
131   mSystemOverlay = NULL;
132
133   if( mDefaultCamera )
134   {
135     // its enough to release the handle so the object is released
136     // don't need to remove it from root actor as root actor will delete the object
137     mDefaultCamera.Reset();
138   }
139
140   if( mRootLayer )
141   {
142     // we are closing down so just delete the root, no point emit disconnect
143     // signals or send messages to update
144     mRootLayer.Reset();
145   }
146
147   if( mRenderTaskList )
148   {
149     mRenderTaskList.Reset();
150   }
151 }
152
153 StagePtr Stage::GetCurrent()
154 {
155   StagePtr stage( NULL );
156   // no checking in this version
157   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
158   if( tls )
159   {
160     stage = tls->GetCurrentStage();
161   }
162   return stage;
163 }
164
165 bool Stage::IsInstalled()
166 {
167   return ThreadLocalStorage::Created();
168 }
169
170 ObjectRegistry& Stage::GetObjectRegistry()
171 {
172   return *mObjectRegistry;
173 }
174
175 void Stage::RegisterObject( Dali::BaseObject* object )
176 {
177   mObjectRegistry->RegisterObject( object );
178 }
179
180 void Stage::UnregisterObject( Dali::BaseObject* object )
181 {
182   mObjectRegistry->UnregisterObject( object );
183 }
184
185 Layer& Stage::GetRootActor()
186 {
187   return *mRootLayer;
188 }
189
190 AnimationPlaylist& Stage::GetAnimationPlaylist()
191 {
192   return mAnimationPlaylist;
193 }
194
195 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
196 {
197   return mPropertyNotificationManager;
198 }
199
200 void Stage::Add( Actor& actor )
201 {
202   mRootLayer->Add( actor );
203 }
204
205 void Stage::Remove( Actor& actor )
206 {
207   mRootLayer->Remove( actor );
208 }
209
210 void Stage::SurfaceResized( float width, float height )
211 {
212   if( ( fabsf( width - mSurfaceSize.width ) > Math::MACHINE_EPSILON_1000 ) || ( fabsf( height - mSurfaceSize.height ) > Math::MACHINE_EPSILON_1000 ) )
213   {
214     mSurfaceSize.width = width;
215     mSurfaceSize.height = height;
216
217     // Internally we want to report the actual size of the stage.
218     mSize.width = width;
219     mSize.height = height - static_cast<float>( mTopMargin );
220
221     // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
222     mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
223
224     // Adjust the camera height to allow for top-margin
225     SetDefaultCameraPosition();
226
227     mRootLayer->SetSize( mSize.width, mSize.height );
228
229     // Repeat for SystemOverlay actors
230     if( mSystemOverlay )
231     {
232       // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
233       // This will remain unaffected by changes in SetDefaultCameraPosition()
234       mSystemOverlay->GetImpl()->SetSize( width, height );
235     }
236
237     SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int32_t>( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
238
239     // if single render task to screen then set its viewport parameters
240     if( 1 == mRenderTaskList->GetTaskCount() )
241     {
242       Dali::RenderTask defaultRenderTask = mRenderTaskList->GetTask( 0u );
243
244       if(!defaultRenderTask.GetTargetFrameBuffer())
245       {
246         defaultRenderTask.SetViewport( Viewport( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
247       }
248     }
249
250     if( mRenderToFbo )
251     {
252       Dali::FrameBuffer frameBuffer = Dali::FrameBuffer::New( static_cast<uint32_t>( width ), static_cast<uint32_t>( height ), Dali::FrameBuffer::Attachment::NONE );
253       Dali::Texture texture = Dali::Texture::New( Dali::TextureType::TEXTURE_2D, Dali::Pixel::RGB888, static_cast<uint32_t>( width ), static_cast<uint32_t>( height ) );
254       frameBuffer.AttachColorTexture( texture );
255
256       Dali::RenderTask defaultRenderTask = mRenderTaskList->GetTask( 0u );
257       defaultRenderTask.SetFrameBuffer( frameBuffer );
258     }
259   }
260 }
261
262 Vector2 Stage::GetSize() const
263 {
264   return mSize;
265 }
266
267 void Stage::SetTopMargin( uint32_t margin )
268 {
269   if (mTopMargin == margin)
270   {
271     return;
272   }
273   mTopMargin = margin;
274
275   mSize.width = mSurfaceSize.width;
276   mSize.height = mSurfaceSize.height - static_cast<float>( mTopMargin );
277
278   // Adjust the camera height to allow for top-margin
279   SetDefaultCameraPosition();
280
281   mRootLayer->SetSize( mSize.width, mSize.height );
282 }
283
284 RenderTaskList& Stage::GetRenderTaskList() const
285 {
286   return *mRenderTaskList;
287 }
288
289 void Stage::CreateDefaultCameraActor()
290 {
291   // The default camera attributes and position is such that
292   // children of the default layer, can be positioned at (0,0) and
293   // be at the top-left of the viewport.
294   mDefaultCamera = CameraActor::New( Size::ZERO );
295   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
296   Add(*(mDefaultCamera.Get()));
297 }
298
299 void Stage::SetDefaultCameraPosition()
300 {
301   mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
302 }
303
304 Actor& Stage::GetDefaultRootActor()
305 {
306   return *mRootLayer;
307 }
308
309 CameraActor& Stage::GetDefaultCameraActor()
310 {
311   return *mDefaultCamera;
312 }
313
314 uint32_t Stage::GetLayerCount() const
315 {
316   return mLayerList->GetLayerCount();
317 }
318
319 Dali::Layer Stage::GetLayer( uint32_t depth ) const
320 {
321   return Dali::Layer(mLayerList->GetLayer( depth ));
322 }
323
324 Dali::Layer Stage::GetRootLayer() const
325 {
326   return Dali::Layer( mRootLayer.Get() );
327 }
328
329 LayerList& Stage::GetLayerList()
330 {
331   return *mLayerList;
332 }
333
334 Integration::SystemOverlay& Stage::GetSystemOverlay()
335 {
336   // Lazily create system-level if requested
337   if( !mSystemOverlay )
338   {
339     mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
340     DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
341
342     mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
343   }
344
345   return *mSystemOverlay;
346 }
347
348 SystemOverlay* Stage::GetSystemOverlayInternal()
349 {
350   SystemOverlay* overlay( NULL );
351
352   if( mSystemOverlay )
353   {
354     overlay = mSystemOverlay->GetImpl();
355   }
356
357   return overlay;
358 }
359
360 void Stage::SetViewMode( ViewMode viewMode )
361 {
362   if( mViewMode != viewMode )
363   {
364     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
365
366     if( mViewMode == MONO )
367     {
368       mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
369       mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
370
371       //Create camera and RenderTask for left eye
372       mLeftCamera = CameraActor::New( Size::ZERO );
373       mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
374       mDefaultCamera->Add( *mLeftCamera.Get() );
375       mLeftRenderTask = mRenderTaskList->CreateTask();
376       mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
377       mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
378
379       //Create camera and RenderTask for right eye
380       mRightCamera = CameraActor::New( Size::ZERO );
381       mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
382       mDefaultCamera->Add( *mRightCamera.Get() );
383       mRightRenderTask = mRenderTaskList->CreateTask();
384       mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
385
386       mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
387       mRightCamera->SetType( Dali::Camera::FREE_LOOK );
388     }
389
390     // save new mode
391     mViewMode = viewMode;
392
393     switch( viewMode )
394     {
395       case MONO:
396       {
397         // delete extra stereoscopic render tasks and cameras
398         mRenderTaskList->RemoveTask( mLeftRenderTask );
399         mDefaultCamera->Remove( *mLeftCamera.Get() );
400         mLeftRenderTask.Reset();
401         mLeftCamera.Reset();
402         mRenderTaskList->RemoveTask( mRightRenderTask );
403         mDefaultCamera->Remove( *mRightCamera.Get() );
404         mRightRenderTask.Reset();
405         mRightCamera.Reset();
406         mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
407         mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
408         mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
409
410         break;
411       }
412       case STEREO_HORIZONTAL:
413       {
414         //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
415         //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
416
417         //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
418         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
419
420         //Calculate aspect ratio
421         float aspect = mSize.width / (mSize.height * 0.5f);
422
423         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
424         mLeftCamera->SetAspectRatio( aspect );
425
426         mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
427         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
428         mLeftRenderTask.SetViewport( Viewport(0, static_cast<int32_t>( mSize.height * 0.5f ), static_cast<int32_t>( mSize.width ), static_cast<int32_t>( mSize.height * 0.5f ) ) ); // truncated
429
430         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0,  -stereoBase ) );
431         mRightCamera->SetAspectRatio( aspect );
432         mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
433         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
434         mRightRenderTask.SetViewport( Viewport(0, 0, static_cast<int32_t>( mSize.width ), static_cast<int32_t>( mSize.height * 0.5f ) ) ); // truncated
435
436         break;
437       }
438       case STEREO_VERTICAL:
439       {
440         //Calculate separation in pixels along horizontal axis
441         const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
442
443         //Recalculate fov based on viewport size
444         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
445
446         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
447         mLeftCamera->SetFieldOfView( fov );
448         mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
449         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
450         mLeftRenderTask.SetViewport( Viewport(0, 0, static_cast<int32_t>( mSize.width * 0.5f ), static_cast<int32_t>( mSize.height ) ) ); // truncated
451
452         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
453         mRightCamera->SetFieldOfView( fov );
454         mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
455         mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
456         mRightRenderTask.SetViewport( Viewport( static_cast<int32_t>( mSize.width * 0.5f ), 0, static_cast<int32_t>( mSize.width * 0.5f ), static_cast<int32_t>( mSize.height ) ) ); // truncated
457
458         break;
459       }
460       case STEREO_INTERLACED:
461       {
462         break;
463       }
464     }
465   }
466 }
467
468 ViewMode Stage::GetViewMode() const
469 {
470   return mViewMode;
471 }
472
473 void Stage::SetStereoBase( float stereoBase )
474 {
475   if( ! Equals( mStereoBase, stereoBase ) )
476   {
477     DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
478     mStereoBase = stereoBase;
479
480     switch( mViewMode  )
481     {
482       case STEREO_HORIZONTAL:
483       {
484         stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
485         float aspect = mSize.width / (mSize.height * 0.5f);
486
487         mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
488         mLeftCamera->SetAspectRatio( aspect );
489         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
490
491         mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
492         mRightCamera->SetAspectRatio( aspect );
493         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
494
495         break;
496       }
497       case STEREO_VERTICAL:
498       {
499         stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
500         const float fov = 2.0f * std::atan(  mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
501
502         mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
503         mLeftCamera->SetFieldOfView( fov );
504         mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
505
506         mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
507         mRightCamera->SetFieldOfView( fov );
508         mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
509
510         break;
511       }
512       default:
513         break;
514     }
515   }
516 }
517
518 float Stage::GetStereoBase() const
519 {
520   return mStereoBase;
521 }
522
523 void Stage::SetBackgroundColor(Vector4 color)
524 {
525   // Cache for public GetBackgroundColor()
526   mBackgroundColor = color;
527
528   // Send message to change color in next frame
529   SetBackgroundColorMessage( mUpdateManager, color );
530 }
531
532 Vector4 Stage::GetBackgroundColor() const
533 {
534   return mBackgroundColor;
535 }
536
537 Vector2 Stage::GetDpi() const
538 {
539   return mDpi;
540 }
541
542 void Stage::SetDpi(Vector2 dpi)
543 {
544   mDpi = dpi;
545 }
546
547 void Stage::KeepRendering( float durationSeconds )
548 {
549   // Send message to keep rendering
550   KeepRenderingMessage( mUpdateManager, durationSeconds );
551 }
552
553 void Stage::SetRenderingBehavior( DevelStage::Rendering renderingBehavior )
554 {
555   if( mRenderingBehavior != renderingBehavior )
556   {
557     // Send message to change the rendering behavior
558     SetRenderingBehaviorMessage( mUpdateManager, renderingBehavior );
559
560     mRenderingBehavior = renderingBehavior;
561   }
562 }
563
564 DevelStage::Rendering Stage::GetRenderingBehavior() const
565 {
566   return mRenderingBehavior;
567 }
568
569 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
570 {
571   bool connected( true );
572   Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
573
574   if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
575   {
576     stage->KeyEventSignal().Connect( tracker, functor );
577   }
578   else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
579   {
580     stage->KeyEventGeneratedSignal().Connect( tracker, functor );
581   }
582   else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
583   {
584     stage->EventProcessingFinishedSignal().Connect( tracker, functor );
585   }
586   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
587   {
588     stage->TouchedSignal().Connect( tracker, functor );
589   }
590   else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
591   {
592     stage->TouchSignal().Connect( tracker, functor );
593   }
594   else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
595   {
596     stage->WheelEventSignal().Connect( tracker, functor );
597   }
598   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
599   {
600     stage->ContextLostSignal().Connect( tracker, functor );
601   }
602   else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
603   {
604     stage->ContextRegainedSignal().Connect( tracker, functor );
605   }
606   else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
607   {
608     stage->SceneCreatedSignal().Connect( tracker, functor );
609   }
610   else
611   {
612     // signalName does not match any signal
613     connected = false;
614   }
615
616   return connected;
617 }
618
619 void Stage::EmitKeyEventSignal(const KeyEvent& event)
620 {
621   // Emit the key event signal when no actor in the stage has gained the key input focus
622
623   mKeyEventSignal.Emit( event );
624 }
625
626 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
627 {
628   // Emit the KeyEventGenerated signal when KeyEvent is generated
629
630   return mKeyEventGeneratedSignal.Emit( event );
631 }
632
633 void Stage::EmitEventProcessingFinishedSignal()
634 {
635    mEventProcessingFinishedSignal.Emit();
636 }
637
638 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
639 {
640   mTouchedSignal.Emit( touchEvent );
641   mTouchSignal.Emit( touch );
642 }
643
644 void Stage::EmitWheelEventSignal(const WheelEvent& event)
645 {
646   // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
647
648   mWheelEventSignal.Emit( event );
649 }
650
651 void Stage::EmitSceneCreatedSignal()
652 {
653   mSceneCreatedSignal.Emit();
654 }
655
656 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
657 {
658   return mKeyEventSignal;
659 }
660
661 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
662 {
663   return mKeyEventGeneratedSignal;
664 }
665
666 void Stage::AddFrameCallback( FrameCallbackInterface& frameCallback, Actor& rootActor )
667 {
668   DALI_ASSERT_ALWAYS( ( ! FrameCallbackInterface::Impl::Get( frameCallback ).IsConnectedToSceneGraph() )
669                       && "FrameCallbackInterface implementation already added" );
670
671   // Create scene-graph object and transfer to UpdateManager
672   OwnerPointer< SceneGraph::FrameCallback > transferOwnership( SceneGraph::FrameCallback::New( frameCallback ) );
673   AddFrameCallbackMessage( mUpdateManager, transferOwnership, rootActor.GetNode() );
674 }
675
676 void Stage::RemoveFrameCallback( FrameCallbackInterface& frameCallback )
677 {
678   FrameCallbackInterface::Impl::Get( frameCallback ).Invalidate();
679   RemoveFrameCallbackMessage( mUpdateManager, frameCallback );
680 }
681
682 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
683 {
684   return mEventProcessingFinishedSignal;
685 }
686
687 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
688 {
689   DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
690   return mTouchedSignal;
691 }
692
693 Dali::Stage::TouchSignalType& Stage::TouchSignal()
694 {
695   return mTouchSignal;
696 }
697
698 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
699 {
700   return mWheelEventSignal;
701 }
702
703 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
704 {
705   return mContextLostSignal;
706 }
707
708 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
709 {
710   return mContextRegainedSignal;
711 }
712
713 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
714 {
715   return mSceneCreatedSignal;
716 }
717
718 void Stage::NotifyContextLost()
719 {
720   mContextLostSignal.Emit();
721 }
722
723 void Stage::NotifyContextRegained()
724 {
725   mContextRegainedSignal.Emit();
726 }
727
728
729 void Stage::RequestRebuildDepthTree()
730 {
731   DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
732   mDepthTreeDirty = true;
733 }
734
735 void Stage::RebuildDepthTree()
736 {
737   // If the depth tree needs rebuilding, do it in this frame only.
738   if( mDepthTreeDirty )
739   {
740     DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
741
742     ActorPtr actor( mRootLayer.Get() );
743     actor->RebuildDepthTree();
744     mDepthTreeDirty = false;
745   }
746 }
747
748
749 Stage::Stage( AnimationPlaylist& playlist,
750               PropertyNotificationManager& propertyNotificationManager,
751               SceneGraph::UpdateManager& updateManager,
752               NotificationManager& notificationManager,
753               Integration::RenderController& renderController )
754 : mAnimationPlaylist( playlist ),
755   mPropertyNotificationManager( propertyNotificationManager ),
756   mUpdateManager( updateManager ),
757   mNotificationManager( notificationManager ),
758   mRenderController( renderController ),
759   mSize( Vector2::ZERO ),
760   mSurfaceSize( Vector2::ZERO ),
761   mBackgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
762   mViewMode( MONO ),
763   mStereoBase( DEFAULT_STEREO_BASE ),
764   mTopMargin( 0 ),
765   mDpi( Vector2::ZERO ),
766   mRightRenderTask(),
767   mLeftRenderTask(),
768   mSystemOverlay( NULL ),
769   mKeyEventSignal(),
770   mKeyEventGeneratedSignal(),
771   mEventProcessingFinishedSignal(),
772   mTouchedSignal(),
773   mTouchSignal(),
774   mWheelEventSignal(),
775   mContextLostSignal(),
776   mContextRegainedSignal(),
777   mSceneCreatedSignal(),
778   mRenderingBehavior( DevelStage::Rendering::IF_REQUIRED ),
779   mDepthTreeDirty( false ),
780   mForceNextUpdate( false ),
781   mRenderToFbo( false )
782 {
783 }
784
785 SceneGraph::UpdateManager& Stage::GetUpdateManager()
786 {
787   return mUpdateManager;
788 }
789
790 Integration::RenderController& Stage::GetRenderController()
791 {
792   return mRenderController;
793 }
794
795 uint32_t* Stage::ReserveMessageSlot( uint32_t size, bool updateScene )
796 {
797   return mUpdateManager.ReserveMessageSlot( size, updateScene );
798 }
799
800 BufferIndex Stage::GetEventBufferIndex() const
801 {
802   return mUpdateManager.GetEventBufferIndex();
803 }
804
805 void Stage::ForceNextUpdate()
806 {
807   mForceNextUpdate = true;
808 }
809
810 bool Stage::IsNextUpdateForced()
811 {
812   bool nextUpdateForced = mForceNextUpdate;
813   mForceNextUpdate = false;
814   return nextUpdateForced;
815 }
816
817 Stage::~Stage()
818 {
819   delete mSystemOverlay;
820
821   mObjectRegistry.Reset();
822 }
823
824 } // namespace Internal
825
826 } // namespace Dali