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 f68060e..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,34 +43,35 @@ namespace Dali
 namespace Internal
 {
 
-namespace
+ScenePtr Scene::New(Size size, int orientation)
 {
-
-const Vector4 DEFAULT_BACKGROUND_COLOR(0.0f, 0.0f, 0.0f, 1.0f); // Default background color
-
-} //Unnamed namespace
-
-ScenePtr Scene::New( Size size )
-{
-  ScenePtr scene = new Scene( size );
+  ScenePtr scene = new Scene;
 
   // Second-phase construction
-  scene->Initialize();
+  scene->Initialize(size, orientation);
 
   return scene;
 }
 
-Scene::Scene( Size size )
-: mSurface( nullptr ),
-  mSize( size ),
-  mSurfaceSize( Vector2::ZERO ),
-  mDpi( Vector2::ZERO ),
-  mDepthTreeDirty( false )
+Scene::Scene()
+: 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()),
+  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
@@ -88,14 +91,11 @@ Scene::~Scene()
     mRenderTaskList.Reset();
   }
 
-  if( ThreadLocalStorage::Created() )
-  {
-    ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
-    tls->RemoveScene( this );
-  }
+  // 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()
+void Scene::Initialize(Size size, int orientation)
 {
   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
 
@@ -108,8 +108,8 @@ void Scene::Initialize()
   // Create the ordered list of layers
   mLayerList = LayerList::New( updateManager );
 
-  // The stage owns the default layer
-  mRootLayer = Layer::NewRoot( *mLayerList, updateManager );
+  // The scene owns the default layer
+  mRootLayer = Layer::NewRoot( *mLayerList );
   mRootLayer->SetName("RootLayer");
   mRootLayer->SetScene( *this );
 
@@ -121,15 +121,24 @@ void Scene::Initialize()
   // 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.
-  mDefaultCamera = CameraActor::New( mSize );
+  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 );
+
+  // 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)
@@ -192,45 +201,30 @@ Actor& Scene::GetDefaultRootActor()
   return *mRootLayer;
 }
 
-void Scene::SetSurface( Integration::RenderSurface& surface )
+void Scene::SurfaceResized(float width, float height)
 {
-  mSurface = &surface;
-  if ( mSurface )
+  if((fabsf(mSize.width - width) > Math::MACHINE_EPSILON_1) || (fabsf(mSize.height - height) > Math::MACHINE_EPSILON_1))
   {
-    mSurfaceSize.width = static_cast<float>( mSurface->GetPositionSize().width );
-    mSurfaceSize.height = static_cast<float>( mSurface->GetPositionSize().height );
-
-    mSize.width = mSurfaceSize.width;
-    mSize.height = mSurfaceSize.height;
-
-    // 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 );
+    ChangedSurface(width, height, mSurfaceOrientation);
+  }
+}
 
+void Scene::SurfaceReplaced()
+{
+  if ( mSceneObject )
+  {
     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
-    SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
-    SetDefaultSurfaceRectMessage( updateManager, Rect<int32_t>( 0, 0, static_cast<int32_t>( mSurfaceSize.width ), static_cast<int32_t>( mSurfaceSize.height ) ) ); // truncated
-
-    RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
-
-    // if single render task to screen then set its viewport parameters
-    if( 1 == mRenderTaskList->GetTaskCount() )
-    {
-      if( !defaultRenderTask->GetTargetFrameBuffer() )
-      {
-        defaultRenderTask->SetViewport( Viewport( 0, 0, static_cast<int32_t>( mSurfaceSize.width ), static_cast<int32_t>( mSurfaceSize.height ) ) ); // truncated
-      }
-    }
-
-    mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
-    defaultRenderTask->SetFrameBuffer( mFrameBuffer );
+    SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
   }
 }
 
-Integration::RenderSurface* Scene::GetSurface() const
+void Scene::Discard()
 {
-  return mSurface;
+  if( ThreadLocalStorage::Created() )
+  {
+    ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
+    tls->RemoveScene( this );
+  }
 }
 
 void Scene::RequestRebuildDepthTree()
@@ -238,6 +232,16 @@ void Scene::RequestRebuildDepthTree()
   mDepthTreeDirty = true;
 }
 
+void Scene::QueueEvent( const Integration::Event& event )
+{
+  mEventProcessor.QueueEvent( event );
+}
+
+void Scene::ProcessEvents()
+{
+  mEventProcessor.ProcessEvents();
+}
+
 void Scene::RebuildDepthTree()
 {
   // If the depth tree needs rebuilding, do it in this frame only.
@@ -249,19 +253,155 @@ void Scene::RebuildDepthTree()
   }
 }
 
-void Scene::SetBackgroundColor(Vector4 color)
+void Scene::SetBackgroundColor( const Vector4& color )
+{
+  mBackgroundColor = color;
+
+  mRenderTaskList->GetTask( 0u )->SetClearColor( color );
+  mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
+}
+
+Vector4 Scene::GetBackgroundColor() const
+{
+  return mBackgroundColor;
+}
+
+SceneGraph::Scene* Scene::GetSceneObject() const
 {
-  if( mSurface )
+  return mSceneObject;
+}
+
+void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
+{
+  if ( !mKeyEventSignal.Empty() )
   {
-    mSurface->SetBackgroundColor( color );
+    Dali::Integration::Scene handle( this );
+    mKeyEventSignal.Emit( event );
   }
 }
 
-Vector4 Scene::GetBackgroundColor() const
+void Scene::SurfaceRotated(float width, float height, int orientation)
+{
+  mSurfaceOrientation = orientation;
+  ChangedSurface(width, height, orientation);
+}
+
+int Scene::GetSurfaceOrientation()
 {
-  return mSurface ? mSurface->GetBackgroundColor() : DEFAULT_BACKGROUND_COLOR;
+  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 );
+}
+
+void Scene::EmitEventProcessingFinishedSignal()
+{
+  if ( !mEventProcessingFinishedSignal.Empty() )
+  {
+    Dali::Integration::Scene handle( this );
+    mEventProcessingFinishedSignal.Emit();
+  }
+}
+
+void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
+{
+  Dali::Integration::Scene handle( this );
+  if ( !mTouchedSignal.Empty() )
+  {
+    mTouchedSignal.Emit( touch );
+  }
+}
+
+void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
+{
+  if ( !mWheelEventSignal.Empty() )
+  {
+    Dali::Integration::Scene handle( this );
+    mWheelEventSignal.Emit( 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;
+}
+
+Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
+{
+  return mKeyEventGeneratedSignal;
+}
+
+Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
+{
+  return mEventProcessingFinishedSignal;
+}
+
+Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
+{
+  return mTouchedSignal;
+}
+
+Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
+{
+  return mWheelEventSignal;
+}
+
+std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
+{
+  return mItemsDirtyRects;
+}
 
 } // Internal