[4.0] Supports screen rotation.
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 905cfba..0ae2eaf
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
 #include <dali/public-api/events/touch-data.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/render-tasks/render-task-list.h>
+#include <dali/public-api/rendering/frame-buffer.h>
 
 using Dali::Internal::SceneGraph::Node;
 
@@ -89,13 +90,15 @@ SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH,                     &
 StagePtr Stage::New( AnimationPlaylist& playlist,
                      PropertyNotificationManager& propertyNotificationManager,
                      SceneGraph::UpdateManager& updateManager,
-                     NotificationManager& notificationManager )
+                     NotificationManager& notificationManager,
+                     Integration::RenderController& renderController )
 {
-  return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
+  return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager, renderController ) );
 }
 
-void Stage::Initialize()
+void Stage::Initialize( bool renderToFbo )
 {
+  mRenderToFbo = renderToFbo;
   mObjectRegistry = ObjectRegistry::New();
 
   // Create the ordered list of layers
@@ -117,6 +120,10 @@ void Stage::Initialize()
 
   // Create the default render-task
   Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
+
+  // init current default camera orientation
+  mNeedToRotation = false;
+  mCurrentOrientation = 0;
 }
 
 void Stage::Uninitialize()
@@ -197,44 +204,133 @@ void Stage::Remove( Actor& actor )
   mRootLayer->Remove( actor );
 }
 
-void Stage::SurfaceResized(float width, float height)
+void Stage::SurfaceResized( float width, float height )
 {
-  mSurfaceSize.width = width;
-  mSurfaceSize.height = height;
+  if( ( fabs( width - mSurfaceSize.width ) > Math::MACHINE_EPSILON_1000 ) || ( fabs( height - mSurfaceSize.height ) > Math::MACHINE_EPSILON_1000 )
+      || mNeedToRotation )
+  {
+    mNeedToRotation = false;
 
-  // Internally we want to report the actual size of the stage.
-  mSize.width = width;
-  mSize.height = height - mTopMargin;
+    mSurfaceSize.width = width;
+    mSurfaceSize.height = height;
 
-  // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
-  mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
+    // Internally we want to report the actual size of the stage.
+    mSize.width = width;
+    mSize.height = height - mTopMargin;
 
-  // Adjust the camera height to allow for top-margin
-  SetDefaultCameraPosition();
+    // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
+    mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
 
-  mRootLayer->SetSize( mSize.width, mSize.height );
+    // Adjust the camera height to allow for top-margin
+    SetDefaultCameraPosition();
 
-  // Repeat for SystemOverlay actors
-  if( mSystemOverlay )
+    mRootLayer->SetSize( mSize.width, mSize.height );
+
+    // Repeat for SystemOverlay actors
+    if( mSystemOverlay )
+    {
+      // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
+      // This will remain unaffected by changes in SetDefaultCameraPosition()
+      mSystemOverlay->GetImpl()->SetSize( width, height );
+    }
+
+    if( mCurrentOrientation == 90 || mCurrentOrientation == 270)
+    {
+      SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, height, width ) );
+    }
+    else
+    {
+      SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
+    }
+
+    // if single render task to screen then set its viewport parameters
+    if( 1 == mRenderTaskList->GetTaskCount() )
+    {
+      Dali::RenderTask defaultRenderTask = mRenderTaskList->GetTask( 0u );
+
+      if(!defaultRenderTask.GetTargetFrameBuffer())
+      {
+        if( mCurrentOrientation == 90 || mCurrentOrientation == 270)
+        {
+          defaultRenderTask.SetViewport( Viewport(0, 0, height, width) );
+        }
+        else
+        {
+          defaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
+        }
+      }
+    }
+
+    if( mRenderToFbo )
+    {
+      Dali::FrameBuffer frameBuffer = Dali::FrameBuffer::New( width, height, Dali::FrameBuffer::Attachment::NONE );
+      Dali::Texture texture = Dali::Texture::New( Dali::TextureType::TEXTURE_2D, Dali::Pixel::RGB888, width, height );
+      frameBuffer.AttachColorTexture( texture );
+
+      Dali::RenderTask defaultRenderTask = mRenderTaskList->GetTask( 0u );
+      defaultRenderTask.SetFrameBuffer( frameBuffer );
+    }
+  }
+}
+
+void Stage::SurfaceResized( float width, float height, int orientation )
+{
+  // Calculates the angle of rotation.
+  int rotDelta = ( 360 + orientation - mCurrentOrientation ) % 360;
+  Quaternion rotateAngle;
+
+  switch( rotDelta )
   {
-    // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
-    // This will remain unaffected by changes in SetDefaultCameraPosition()
-    mSystemOverlay->GetImpl()->SetSize( width, height );
+    case 90:
+    {
+      rotateAngle = Quaternion( Dali::ANGLE_270, Vector3::ZAXIS );
+      break;
+    }
+    case 270:
+    {
+      rotateAngle = Quaternion( Dali::ANGLE_90, Vector3::ZAXIS );
+      break;
+    }
+    case 180:
+    {
+      rotateAngle = Quaternion( Dali::ANGLE_180, Vector3::ZAXIS );
+      break;
+    }
+    default:
+      rotateAngle = Quaternion( Dali::ANGLE_0, Vector3::ZAXIS );
+      break;
+  }
+
+  // set current orientation
+  mCurrentOrientation = orientation;
+  if( rotDelta )
+  {
+    mNeedToRotation = true;
   }
 
-  SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
+  // do surface resized
+  SurfaceResized( width, height );
 
-  // if single render task to screen then set its viewport parameters
-  if( 1 == mRenderTaskList->GetTaskCount() )
+  // If we need to rotate, rotate the camera.
+  if( rotDelta )
   {
-    Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
+    // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position depending on the orientation
+    if( mCurrentOrientation == 90 || mCurrentOrientation == 270 )
+    {
+      mDefaultCamera->SetPerspectiveProjection( Vector2( height, width ) );
+      if( mSystemOverlay )
+      {
+        mSystemOverlay->GetImpl()->GetDefaultCameraActor().SetPerspectiveProjection( Vector2( height, width ) );
+      }
+    }
 
-    if(!mDefaultRenderTask.GetTargetFrameBuffer())
+    mDefaultCamera->RotateBy( rotateAngle );
+
+    if( mSystemOverlay )
     {
-      mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
+      mSystemOverlay->GetImpl()->GetDefaultCameraActor().RotateBy( rotateAngle );
     }
   }
-
 }
 
 Vector2 Stage::GetSize() const
@@ -695,17 +791,22 @@ void Stage::RebuildDepthTree()
 Stage::Stage( AnimationPlaylist& playlist,
               PropertyNotificationManager& propertyNotificationManager,
               SceneGraph::UpdateManager& updateManager,
-              NotificationManager& notificationManager )
+              NotificationManager& notificationManager,
+              Integration::RenderController& renderController )
 : mAnimationPlaylist( playlist ),
-  mPropertyNotificationManager(propertyNotificationManager),
-  mUpdateManager(updateManager),
-  mNotificationManager(notificationManager),
-  mSize(Vector2::ZERO),
-  mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
+  mPropertyNotificationManager( propertyNotificationManager ),
+  mUpdateManager( updateManager ),
+  mNotificationManager( notificationManager ),
+  mRenderController( renderController ),
+  mSize( Vector2::ZERO ),
+  mBackgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
   mViewMode( MONO ),
   mStereoBase( DEFAULT_STEREO_BASE ),
   mTopMargin( 0 ),
-  mSystemOverlay(NULL)
+  mSystemOverlay( NULL ),
+  mDepthTreeDirty( false ),
+  mForceNextUpdate( false ),
+  mRenderToFbo( false )
 {
 }
 
@@ -714,6 +815,11 @@ SceneGraph::UpdateManager& Stage::GetUpdateManager()
   return mUpdateManager;
 }
 
+Integration::RenderController& Stage::GetRenderController()
+{
+  return mRenderController;
+}
+
 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
 {
   return mUpdateManager.ReserveMessageSlot( size, updateScene );
@@ -724,6 +830,18 @@ BufferIndex Stage::GetEventBufferIndex() const
   return mUpdateManager.GetEventBufferIndex();
 }
 
+void Stage::ForceNextUpdate()
+{
+  mForceNextUpdate = true;
+}
+
+bool Stage::IsNextUpdateForced()
+{
+  bool nextUpdateForced = mForceNextUpdate;
+  mForceNextUpdate = false;
+  return nextUpdateForced;
+}
+
 Stage::~Stage()
 {
   delete mSystemOverlay;