Support screen and client rotation
[platform/core/uifw/dali-core.git] / dali / internal / event / common / scene-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 69b0724..4ab5142
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 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.
@@ -28,6 +28,8 @@
 #include <dali/internal/event/common/object-registry-impl.h>
 #include <dali/internal/update/nodes/node.h>
 #include <dali/internal/update/manager/update-manager.h>
+#include <dali/internal/update/common/scene-graph-scene.h>
+#include <dali/public-api/common/constants.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/render-tasks/render-task-list.h>
 #include <dali/internal/event/rendering/frame-buffer-impl.h>
@@ -41,35 +43,35 @@ namespace Dali
 namespace Internal
 {
 
-namespace
-{
-
-const Vector4 DEFAULT_BACKGROUND_COLOR(0.0f, 0.0f, 0.0f, 1.0f); // Default background color
-
-} //Unnamed namespace
-
-ScenePtr Scene::New( Integration::RenderSurface& surface )
+ScenePtr Scene::New(Size size, int orientation)
 {
   ScenePtr scene = new Scene;
 
   // Second-phase construction
-  scene->Initialize( surface );
+  scene->Initialize(size, orientation);
 
   return scene;
 }
 
 Scene::Scene()
-: mSurface( nullptr ),
+: mSceneObject(nullptr),
   mSize(), // Don't set the proper value here, this will be set when the surface is set later
   mDpi(),
-  mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
-  mDepthTreeDirty( false ),
-  mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
+  mBackgroundColor(DEFAULT_BACKGROUND_COLOR),
+  mDepthTreeDirty(false),
+  mEventProcessor(*this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor()),
+  mSurfaceOrientation(0)
 {
 }
 
 Scene::~Scene()
 {
+  if( EventThreadServices::IsCoreRunning() && mSceneObject )
+  {
+    ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
+    RemoveSceneMessage( tls->GetUpdateManager(), *mSceneObject );
+  }
+
   if( mDefaultCamera )
   {
     // its enough to release the handle so the object is released
@@ -89,16 +91,11 @@ Scene::~Scene()
     mRenderTaskList.Reset();
   }
 
-  if ( mFrameBuffer )
-  {
-    mFrameBuffer.Reset();
-  }
-
   // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
   // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
 }
 
-void Scene::Initialize( Integration::RenderSurface& surface )
+void Scene::Initialize(Size size, int orientation)
 {
   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
 
@@ -124,20 +121,24 @@ void Scene::Initialize( Integration::RenderSurface& surface )
   // Create the default camera actor first; this is needed by the RenderTaskList
   // The default camera attributes and position is such that children of the default layer,
   // can be positioned at (0,0) and be at the top-left of the viewport.
-  const PositionSize positionSize = surface.GetPositionSize();
-  const Vector2 surfaceSize( static_cast< float >( positionSize.width ), static_cast< float >( positionSize.height ) );
-  mDefaultCamera = CameraActor::New( surfaceSize );
+  mDefaultCamera = CameraActor::New( size );
   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
   Add(*(mDefaultCamera.Get()));
 
   // Create the list of render-tasks
   mRenderTaskList = RenderTaskList::New();
 
-  // Create the default render-task
-  mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
+  // Create the default render-task and ensure clear is enabled on it to show the background color
+  RenderTaskPtr renderTask = mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
+  renderTask->SetClearEnabled(true);
+  mSurfaceOrientation = orientation;
+
+  SurfaceRotated( size.width, size.height, mSurfaceOrientation );
 
-  // Set the surface
-  SetSurface( surface );
+  // Create scene graph object
+  mSceneObject = new SceneGraph::Scene();
+  OwnerPointer< SceneGraph::Scene > transferOwnership( const_cast< SceneGraph::Scene* >( mSceneObject ) );
+  AddSceneMessage( updateManager, transferOwnership );
 }
 
 void Scene::Add(Actor& actor)
@@ -200,68 +201,23 @@ Actor& Scene::GetDefaultRootActor()
   return *mRootLayer;
 }
 
-void Scene::SetSurface( Integration::RenderSurface& surface )
-{
-  if( mSurface != &surface )
-  {
-    mSurface = &surface;
-
-    RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
-
-    mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
-    defaultRenderTask->SetFrameBuffer( mFrameBuffer );
-
-    SurfaceResized();
-  }
-}
-
-void Scene::SurfaceResized()
+void Scene::SurfaceResized(float width, float height)
 {
-  if( mSurface )
+  if((fabsf(mSize.width - width) > Math::MACHINE_EPSILON_1) || (fabsf(mSize.height - height) > Math::MACHINE_EPSILON_1))
   {
-    const PositionSize surfacePositionSize = mSurface->GetPositionSize();
-    const float fWidth = static_cast< float >( surfacePositionSize.width );
-    const float fHeight = static_cast< float >( surfacePositionSize.height );
-
-    if( ( fabsf( mSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
-    {
-      Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( surfacePositionSize.width ), static_cast< int32_t >( surfacePositionSize.height ) ); // truncated
-
-      mSize.width = fWidth;
-      mSize.height = fHeight;
-
-      // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
-      mDefaultCamera->SetPerspectiveProjection( mSize );
-
-      mRootLayer->SetSize( mSize.width, mSize.height );
-
-      ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
-      SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
-      SetDefaultSurfaceRectMessage( updateManager, newSize );
-
-      // set default render-task viewport parameters
-      RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
-      defaultRenderTask->SetViewport( newSize );
-      defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
-    }
+    ChangedSurface(width, height, mSurfaceOrientation);
   }
 }
 
-void Scene::SurfaceDeleted()
+void Scene::SurfaceReplaced()
 {
-  mSurface = nullptr;
-  if ( mFrameBuffer )
+  if ( mSceneObject )
   {
-    // The frame buffer doesn't have a valid render surface any more.
-    mFrameBuffer->MarkSurfaceAsInvalid();
+    ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
+    SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
   }
 }
 
-Integration::RenderSurface* Scene::GetSurface() const
-{
-  return mSurface;
-}
-
 void Scene::Discard()
 {
   if( ThreadLocalStorage::Created() )
@@ -301,11 +257,8 @@ void Scene::SetBackgroundColor( const Vector4& color )
 {
   mBackgroundColor = color;
 
-  if( mSurface )
-  {
-    mRenderTaskList->GetTask( 0u )->SetClearColor( color );
-    mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
-  }
+  mRenderTaskList->GetTask( 0u )->SetClearColor( color );
+  mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
 }
 
 Vector4 Scene::GetBackgroundColor() const
@@ -313,7 +266,12 @@ Vector4 Scene::GetBackgroundColor() const
   return mBackgroundColor;
 }
 
-void Scene::EmitKeyEventSignal(const KeyEvent& event)
+SceneGraph::Scene* Scene::GetSceneObject() const
+{
+  return mSceneObject;
+}
+
+void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
 {
   if ( !mKeyEventSignal.Empty() )
   {
@@ -322,9 +280,47 @@ void Scene::EmitKeyEventSignal(const KeyEvent& event)
   }
 }
 
-bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
+void Scene::SurfaceRotated(float width, float height, int orientation)
+{
+  mSurfaceOrientation = orientation;
+  ChangedSurface(width, height, orientation);
+}
+
+int Scene::GetSurfaceOrientation()
+{
+  return mSurfaceOrientation;
+}
+
+void Scene::ChangedSurface(float width, float height, int orientation)
+{
+  Rect<int32_t> newSize(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height)); // truncated
+
+  mSize.width  = width;
+  mSize.height = height;
+
+  // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
+  mDefaultCamera->SetPerspectiveProjection(mSize);
+  // Set the surface orientation to Default camera for window/screen rotation
+  mDefaultCamera->RotateProjection(orientation);
+
+  mRootLayer->SetSize(width, height);
+
+  ThreadLocalStorage*        tls           = ThreadLocalStorage::GetInternal();
+  SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
+  SetDefaultSurfaceRectMessage(updateManager, newSize);
+
+  // Send the surface orientation to render manager for calculating glViewport/Scissor
+  SetDefaultSurfaceOrientationMessage(updateManager, orientation);
+
+  // set default render-task viewport parameters
+  RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask(0u);
+  defaultRenderTask->SetViewport(newSize);
+}
+
+bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
 {
   // Emit the KeyEventGenerated signal when KeyEvent is generated
+  Dali::Integration::Scene handle( this );
   return mKeyEventGeneratedSignal.Emit( event );
 }
 
@@ -337,20 +333,16 @@ void Scene::EmitEventProcessingFinishedSignal()
   }
 }
 
-void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
+void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
 {
   Dali::Integration::Scene handle( this );
   if ( !mTouchedSignal.Empty() )
   {
-    mTouchedSignal.Emit( touchEvent );
-  }
-  if ( !mTouchSignal.Empty() )
-  {
-    mTouchSignal.Emit( touch );
+    mTouchedSignal.Emit( touch );
   }
 }
 
-void Scene::EmitWheelEventSignal(const WheelEvent& event)
+void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
 {
   if ( !mWheelEventSignal.Empty() )
   {
@@ -359,6 +351,28 @@ void Scene::EmitWheelEventSignal(const WheelEvent& event)
   }
 }
 
+void Scene::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
+{
+  ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
+  AddFrameRenderedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
+}
+
+void Scene::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
+{
+  ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
+  AddFramePresentedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
+}
+
+void Scene::GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
+{
+  mSceneObject->GetFrameRenderedCallback( callbacks );
+}
+
+void Scene::GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
+{
+  mSceneObject->GetFramePresentedCallback( callbacks );
+}
+
 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
 {
   return mKeyEventSignal;
@@ -374,19 +388,19 @@ Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFin
   return mEventProcessingFinishedSignal;
 }
 
-Scene::TouchedSignalType& Scene::TouchedSignal()
+Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
 {
   return mTouchedSignal;
 }
 
-Integration::Scene::TouchSignalType& Scene::TouchSignal()
+Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
 {
-  return mTouchSignal;
+  return mWheelEventSignal;
 }
 
-Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
+std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
 {
-  return mWheelEventSignal;
+  return mItemsDirtyRects;
 }
 
 } // Internal