[dali_1.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / video-view / video-view-impl.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 671ebb7..1e6da13
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
 #include <dali/devel-api/scripting/scripting.h>
 #include <dali/public-api/adaptor-framework/native-image-source.h>
 #include <dali/integration-api/adaptors/adaptor.h>
+#include <dali/integration-api/debug.h>
+#include <dali/public-api/animation/constraint.h>
+#include <dali/devel-api/actors/actor-devel.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/video-view/video-view.h>
+#include <dali-toolkit/public-api/visuals/visual-properties.h>
+#include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
+#include <dali-toolkit/internal/visuals/visual-string-constants.h>
+#include <dali-toolkit/internal/visuals/visual-base-impl.h>
+#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/internal/visuals/visual-factory-cache.h>
 
 namespace Dali
 {
@@ -53,6 +62,9 @@ DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "video", MAP, VIDEO )
 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "looping", BOOLEAN, LOOPING )
 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "muted", BOOLEAN, MUTED )
 DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "volume", MAP, VOLUME )
+DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "underlay", BOOLEAN, UNDERLAY )
+DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "playPosition", INTEGER, PLAY_POSITION )
+DALI_PROPERTY_REGISTRATION( Toolkit, VideoView, "displayMode", INTEGER, DISPLAY_MODE )
 
 DALI_SIGNAL_REGISTRATION( Toolkit, VideoView, "finished", FINISHED_SIGNAL )
 
@@ -66,18 +78,42 @@ DALI_TYPE_REGISTRATION_END()
 
 const char* const VOLUME_LEFT( "volumeLeft" );
 const char* const VOLUME_RIGHT( "volumeRight" );
-const char* const RENDERING_TARGET( "RENDERING_TARGET" );
+
+// 3.0 TC uses RENDERING_TARGET. It should be removed in next release
+const char* const RENDERING_TARGET( "renderingTarget" );
 const char* const WINDOW_SURFACE_TARGET( "windowSurfaceTarget" );
 const char* const NATIVE_IMAGE_TARGET( "nativeImageTarget" );
 
+const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
+  attribute mediump vec2 aPosition;\n
+  uniform mediump mat4 uMvpMatrix;\n
+  uniform mediump vec3 uSize;\n
+  \n
+  void main()\n
+  {\n
+    mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
+    vertexPosition.xyz *= uSize;\n
+    gl_Position = uMvpMatrix * vertexPosition;\n
+  }\n
+);
+
+const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
+  uniform lowp vec4 uColor;\n
+  uniform lowp vec3 mixColor;\n
+  \n
+  void main()\n
+  {\n
+    gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
+  }\n
+);
+
 } // anonymous namepsace
 
 VideoView::VideoView()
-: Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) ),
+: Control( ControlBehaviour( ACTOR_BEHAVIOUR_DEFAULT | DISABLE_STYLE_CHANGE_SIGNALS ) ),
   mCurrentVideoPlayPosition( 0 ),
-  mSetRenderingTarget( false ),
   mIsPlay( false ),
-  mIsPause( false )
+  mIsUnderlay( true )
 {
   mVideoPlayer = Dali::VideoPlayer::New();
 }
@@ -96,23 +132,27 @@ Toolkit::VideoView VideoView::New()
   return handle;
 }
 
+void VideoView::OnInitialize()
+{
+  mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
+  SetWindowSurfaceTarget();
+}
+
 void VideoView::SetUrl( const std::string& url )
 {
   if( mUrl != url || !mPropertyMap.Empty() )
   {
-    mPropertyMap.Clear();
-
     mUrl = url;
+    mPropertyMap.Clear();
   }
 
-  if( mSetRenderingTarget )
+  if( !mIsUnderlay )
   {
-    mVideoPlayer.SetUrl( mUrl );
-  }
-  else
-  {
-    SetNativeImageTarget();
+    Actor self( Self() );
+    Internal::InitializeVisual( self, mVisual, mNativeImage );
   }
+
+  mVideoPlayer.SetUrl( mUrl );
 }
 
 void VideoView::SetPropertyMap( Property::Map map )
@@ -120,7 +160,7 @@ void VideoView::SetPropertyMap( Property::Map map )
   mPropertyMap = map;
 
   Actor self( Self() );
-  InitializeVisual( self, mRenderer, mPropertyMap );
+  Internal::InitializeVisual( self, mVisual, mPropertyMap );
 
   Property::Value* widthValue = mPropertyMap.Find( "width" );
   if( widthValue )
@@ -142,6 +182,20 @@ void VideoView::SetPropertyMap( Property::Map map )
     }
   }
 
+  Property::Value* target = map.Find( RENDERING_TARGET );
+  std::string targetType;
+
+  if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
+  {
+    mIsUnderlay = true;
+    SetWindowSurfaceTarget();
+  }
+  else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
+  {
+    mIsUnderlay = false;
+    SetNativeImageTarget();
+  }
+
   RelayoutRequest();
 }
 
@@ -162,23 +216,25 @@ bool VideoView::IsLooping()
 
 void VideoView::Play()
 {
+  if( mIsUnderlay )
+  {
+    Self().AddRenderer( mRenderer );
+  }
+
   mVideoPlayer.Play();
   mIsPlay = true;
-  mIsPause = false;
 }
 
 void VideoView::Pause()
 {
   mVideoPlayer.Pause();
   mIsPlay = false;
-  mIsPause = true;
 }
 
 void VideoView::Stop()
 {
   mVideoPlayer.Stop();
   mIsPlay = false;
-  mIsPause = false;
 }
 
 void VideoView::Forward( int millisecond )
@@ -227,6 +283,11 @@ Dali::Toolkit::VideoView::VideoViewSignalType& VideoView::FinishedSignal()
 
 void VideoView::EmitSignalFinish()
 {
+  if( mIsUnderlay )
+  {
+    Self().RemoveRenderer( mRenderer );
+  }
+
   if ( !mFinishedSignal.Empty() )
   {
     Dali::Toolkit::VideoView handle( GetOwner() );
@@ -318,25 +379,33 @@ void VideoView::SetProperty( BaseObject* object, Property::Index index, const Pr
       case Toolkit::VideoView::Property::VIDEO:
       {
         std::string videoUrl;
+        Property::Map map;
+
         if( value.Get( videoUrl ) )
         {
           impl.SetUrl( videoUrl );
         }
-
-        Property::Map map;
-        if( value.Get( map ) )
+        else if( value.Get( map ) )
         {
-          impl.SetPropertyMap( map );
+          Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
 
-          Property::Value* target = map.Find( RENDERING_TARGET );
-          std::string targetType;
-          if( target && target->Get( targetType ) && targetType == WINDOW_SURFACE_TARGET )
+          if( map.Count() > 1u || !shaderValue )
           {
-            impl.SetWindowSurfaceTarget();
+            impl.SetPropertyMap( map );
           }
-          else if( target && target->Get( targetType ) && targetType == NATIVE_IMAGE_TARGET )
+          else if( impl.mVisual && map.Count() == 1u && shaderValue )
           {
-            impl.SetNativeImageTarget();
+            Property::Map shaderMap;
+            if( shaderValue->Get( shaderMap ) )
+            {
+              Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
+              visual.SetCustomShader( shaderMap );
+              if( videoView.OnStage() )
+              {
+                visual.SetOffStage( videoView );
+                visual.SetOnStage( videoView );
+              }
+            }
           }
         }
         break;
@@ -374,6 +443,33 @@ void VideoView::SetProperty( BaseObject* object, Property::Index index, const Pr
         }
         break;
       }
+      case Toolkit::VideoView::Property::UNDERLAY:
+      {
+        bool underlay;
+        if( value.Get( underlay ) )
+        {
+          impl.SetUnderlay( underlay );
+        }
+        break;
+      }
+      case Toolkit::VideoView::Property::PLAY_POSITION:
+      {
+        int pos;
+        if( value.Get( pos ) )
+        {
+          impl.SetPlayPosition( pos );
+        }
+        break;
+      }
+      case Toolkit::VideoView::Property::DISPLAY_MODE:
+      {
+        int mode;
+        if( value.Get( mode ) )
+        {
+          impl.SetDisplayMode( mode );
+        }
+        break;
+      }
     }
   }
 }
@@ -422,6 +518,21 @@ Property::Value VideoView::GetProperty( BaseObject* object, Property::Index prop
         value = map;
         break;
       }
+      case Toolkit::VideoView::Property::UNDERLAY:
+      {
+        value = impl.IsUnderlay();
+        break;
+      }
+      case Toolkit::VideoView::Property::PLAY_POSITION:
+      {
+        value = impl.GetPlayPosition();
+        break;
+      }
+      case Toolkit::VideoView::Property::DISPLAY_MODE:
+      {
+        value = impl.GetDisplayMode();
+        break;
+      }
     }
   }
 
@@ -430,29 +541,29 @@ Property::Value VideoView::GetProperty( BaseObject* object, Property::Index prop
 
 void VideoView::SetDepthIndex( int depthIndex )
 {
-  if( mRenderer )
+  if( mVisual )
   {
-    mRenderer.SetDepthIndex( depthIndex );
+    mVisual.SetDepthIndex( depthIndex );
   }
 }
 
 void VideoView::OnStageConnection( int depth )
 {
-  Control::OnStageConnection( depth );
-
-  if( mRenderer )
+  if( mVisual )
   {
     CustomActor self = Self();
-    mRenderer.SetOnStage( self );
+    Toolkit::GetImplementation(mVisual).SetOnStage( self );
   }
+
+  Control::OnStageConnection( depth );
 }
 
 void VideoView::OnStageDisconnection()
 {
-  if( mRenderer )
+  if( mVisual )
   {
     CustomActor self = Self();
-    mRenderer.SetOffStage( self );
+    Toolkit::GetImplementation(mVisual).SetOffStage( self );
   }
 
   Control::OnStageDisconnection();
@@ -504,31 +615,75 @@ void VideoView::SetWindowSurfaceTarget()
   Actor self = Self();
   int curPos = mVideoPlayer.GetPlayPosition();
 
-  mSetRenderingTarget = true;
-  mRenderer.RemoveAndReset( self );
+  if( mVisual )
+  {
+    Toolkit::GetImplementation(mVisual).SetOffStage(self);
+    mVisual.Reset();
+  }
+
+  if( mIsPlay )
+  {
+    mVideoPlayer.Pause();
+  }
+
+  mPositionUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_POSITION, StepCondition( 1.0f, 1.0f ) );
+  mSizeUpdateNotification = self.AddPropertyNotification( Actor::Property::SIZE, StepCondition( 1.0f, 1.0f ) );
+  mScaleUpdateNotification = self.AddPropertyNotification( Actor::Property::WORLD_SCALE, StepCondition( 0.1f, 1.0f ) );
+  mPositionUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
+  mSizeUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
+  mScaleUpdateNotification.NotifySignal().Connect( this, &VideoView::UpdateDisplayArea );
 
   mVideoPlayer.SetRenderingTarget( Dali::Adaptor::Get().GetNativeWindowHandle() );
   mVideoPlayer.SetUrl( mUrl );
-  mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
+
+  if( !mRenderer )
+  {
+    // For underlay rendering mode, video display area have to be transparent.
+    Geometry geometry = VisualFactoryCache::CreateQuadGeometry();
+    Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+    mRenderer = Renderer::New( geometry, shader );
+
+    mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
+    mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE );
+    mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ZERO );
+    mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE );
+    mRenderer.SetProperty( Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ZERO );
+  }
 
   if( mIsPlay )
   {
-    mVideoPlayer.Play();
+    Play();
   }
-  if( mIsPause )
+
+  if( curPos > 0 )
   {
-    mVideoPlayer.Play();
-    mVideoPlayer.Pause();
+    mVideoPlayer.SetPlayPosition( curPos );
   }
-  mVideoPlayer.SetPlayPosition( curPos );
 }
 
 void VideoView::SetNativeImageTarget()
 {
+  if( mVideoPlayer.IsVideoTextureSupported() == false )
+  {
+    DALI_LOG_ERROR( "Platform doesn't support decoded video frame images\n" );
+    mIsUnderlay = true;
+    return;
+  }
+
+  if( mIsPlay )
+  {
+    mVideoPlayer.Pause();
+  }
+
   Actor self( Self() );
-  int curPos = mVideoPlayer.GetPlayPosition();
+  self.RemoveRenderer( mRenderer );
+  Dali::Stage::GetCurrent().KeepRendering( 0.0f );
 
-  mSetRenderingTarget = true;
+  self.RemovePropertyNotification( mPositionUpdateNotification );
+  self.RemovePropertyNotification( mSizeUpdateNotification );
+  self.RemovePropertyNotification( mScaleUpdateNotification );
+
+  int curPos = mVideoPlayer.GetPlayPosition();
 
   Any source;
   Dali::NativeImageSourcePtr nativeImageSourcePtr = Dali::NativeImageSource::New( source );
@@ -536,25 +691,104 @@ void VideoView::SetNativeImageTarget()
 
   mVideoPlayer.SetRenderingTarget( nativeImageSourcePtr );
   mVideoPlayer.SetUrl( mUrl );
-  mVideoPlayer.FinishedSignal().Connect( this, &VideoView::EmitSignalFinish );
 
-  InitializeVisual( self, mRenderer, mNativeImage );
+  Internal::InitializeVisual( self, mVisual, mNativeImage );
+  Self().RemoveRenderer( mRenderer );
 
   if( mIsPlay )
   {
-    mVideoPlayer.Play();
+    Play();
   }
 
-  if( mIsPause )
+  if( curPos > 0 )
   {
-    mVideoPlayer.Play();
-    mVideoPlayer.Pause();
+    mVideoPlayer.SetPlayPosition( curPos );
+  }
+}
+
+void VideoView::UpdateDisplayArea( Dali::PropertyNotification& source )
+{
+  if( !mIsUnderlay )
+  {
+    return;
+  }
+
+  Actor self( Self() );
+
+  bool positionUsesAnchorPoint = self.GetProperty( DevelActor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
+  Vector3 actorSize = self.GetCurrentSize() * self.GetCurrentScale();
+  Vector3 anchorPointOffSet = actorSize * ( positionUsesAnchorPoint ? self.GetCurrentAnchorPoint() : AnchorPoint::TOP_LEFT );
+
+  Vector2 screenPosition = self.GetProperty( DevelActor::Property::SCREEN_POSITION ).Get< Vector2 >();
+
+  mDisplayArea.x = screenPosition.x - anchorPointOffSet.x;
+  mDisplayArea.y = screenPosition.y - anchorPointOffSet.y;
+  mDisplayArea.width = actorSize.x;
+  mDisplayArea.height = actorSize.y;
+
+  mVideoPlayer.SetDisplayArea( mDisplayArea );
+}
+
+void VideoView::SetUnderlay( bool set )
+{
+  if( set != mIsUnderlay )
+  {
+    mIsUnderlay = set;
+
+    if( mIsUnderlay )
+    {
+      SetWindowSurfaceTarget();
+    }
+    else
+    {
+      SetNativeImageTarget();
+    }
+
+    RelayoutRequest();
   }
-  mVideoPlayer.SetPlayPosition( curPos );
+}
+
+bool VideoView::IsUnderlay()
+{
+  return mIsUnderlay;
+}
+
+void VideoView::SetSWCodec( bool on )
+{
+  // If setting SW or HW type is failed , video-view shows video by default codec type.
+  // The default codec type is selected by platform.
+  if( on )
+  {
+    mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::SW );
+  }
+  else
+  {
+    mVideoPlayer.SetCodecType( Dali::VideoPlayerPlugin::CodecType::HW );
+  }
+}
+
+int VideoView::GetPlayPosition()
+{
+  return mVideoPlayer.GetPlayPosition();
+}
+
+void VideoView::SetPlayPosition( int pos )
+{
+  mVideoPlayer.SetPlayPosition( pos );
+}
+
+void VideoView::SetDisplayMode( int mode )
+{
+  mVideoPlayer.SetDisplayMode( static_cast< Dali::VideoPlayerPlugin::DisplayMode::Type >( mode ) );
+}
+
+int VideoView::GetDisplayMode() const
+{
+  return static_cast< int >( mVideoPlayer.GetDisplayMode() );
 }
 
 } // namespace Internal
 
-} // namespace Toolkit
+} // namespace toolkit
 
 } // namespace Dali