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