Use RegisterUniqueProperty for some more renderers
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / arc / arc-visual.cpp
index 618b75d..bf07a40 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 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.
 
 //INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
-#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
+#include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
+#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
+#include <dali-toolkit/internal/visuals/visual-factory-impl.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
-#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
 
 namespace Dali
 {
-
 namespace Toolkit
 {
-
 namespace Internal
 {
-
 namespace
 {
+const int CUSTOM_PROPERTY_COUNT(9); // 5 transform properties + thickness,start,sweep,radius
 
 // cap
-DALI_ENUM_TO_STRING_TABLE_BEGIN( CAP )
-DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, BUTT )
-DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, ROUND )
-DALI_ENUM_TO_STRING_TABLE_END( CAP )
-
-const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
-  attribute mediump vec2 aPosition;\n
-  uniform highp   mat4 uMvpMatrix;\n
-  uniform mediump vec3 uSize;\n
-  \n
-  varying mediump vec2 vPosition;\n
-  \n
-  //Visual size and offset
-  uniform mediump vec2 offset;\n
-  uniform mediump vec2 size;\n
-  uniform mediump vec4 offsetSizeMode;\n
-  uniform mediump vec2 origin;\n
-  uniform mediump vec2 anchorPoint;\n
-
-  vec4 ComputeVertexPosition()\n
-  {\n
-    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
-    vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
-    vPosition = aPosition* visualSize;\n
-    return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
-  }\n
-
-  void main()\n
-  {\n
-    gl_Position = uMvpMatrix * ComputeVertexPosition();\n
-  }\n
-);
-
-const char* FRAGMENT_SHADER_BUTT_CAP = DALI_COMPOSE_SHADER(
-  varying mediump vec2 vPosition;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform mediump float thickness;\n
-  uniform mediump float radius;\n
-  uniform mediump float startAngle;\n
-  uniform mediump float sweepAngle;\n
-  \n
-  const mediump float M_PI_OVER_2 = 1.57079632679;\n
-  const mediump float M_PI = 3.14159265359;\n
-  const mediump float M_PI_2 = 6.28318530718;\n
-  \n
-  mediump float GetOpacity()\n
-  {\n
-      mediump float start = radians( mod( startAngle, 360.0 ) );\n
-      mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
-      mediump float dist = length( vPosition );\n
-      if( angle <= radians( sweepAngle ) )\n
-      {\n
-        return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
-      }\n
-      mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
-      mediump vec2 q0 = vec2( dist * cos( start - M_PI_OVER_2 ), dist * sin( start - M_PI_OVER_2 ) );\n
-      mediump vec2 q1 = vec2( dist * cos( end - M_PI_OVER_2 ), dist * sin( end - M_PI_OVER_2 ) );\n
-      mediump float opacity = 1.0 - smoothstep( 0.0, 2.0, min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
-      opacity *= step( 0.0, thickness / 2.0 - abs( dist - radius ) );\n
-      return opacity;\n
-  }\n
-  void main()\n
-  {\n
-    gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
-    gl_FragColor.a *= GetOpacity();\n
-  }\n
-);
-
-const char* FRAGMENT_SHADER_ROUND_CAP = DALI_COMPOSE_SHADER(
-  varying mediump vec2 vPosition;\n
-  uniform lowp vec4 uColor;\n
-  uniform lowp vec3 mixColor;\n
-  uniform mediump float thickness;\n
-  uniform mediump float radius;\n
-  uniform mediump float startAngle;\n
-  uniform mediump float sweepAngle;\n
-  \n
-  const mediump float M_PI_OVER_2 = 1.57079632679;\n
-  const mediump float M_PI_2 = 6.28318530718;\n
-  \n
-  mediump float GetOpacity()\n
-  {\n
-      mediump float start = radians( mod( startAngle, 360.0 ) );\n
-      mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
-      mediump float dist = length( vPosition );\n
-      if( angle <= radians( sweepAngle ) )\n
-      {\n
-        return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
-      }\n
-      mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
-      mediump vec2 q0 = vec2( radius * cos( start - M_PI_OVER_2 ), radius * sin( start - M_PI_OVER_2 ) );\n
-      mediump vec2 q1 = vec2( radius * cos( end - M_PI_OVER_2 ), radius * sin( end - M_PI_OVER_2 ) );\n
-      return smoothstep( -1.0, 1.0, thickness / 2.0 - min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
-  }\n
-  void main()\n
-  {\n
-    gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
-    gl_FragColor.a *= GetOpacity();\n
-  }\n
-);
+DALI_ENUM_TO_STRING_TABLE_BEGIN(CAP)
+  DALI_ENUM_TO_STRING_WITH_SCOPE(DevelArcVisual::Cap, BUTT)
+  DALI_ENUM_TO_STRING_WITH_SCOPE(DevelArcVisual::Cap, ROUND)
+DALI_ENUM_TO_STRING_TABLE_END(CAP)
 
-}
+} // namespace
 
-ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
+ArcVisualPtr ArcVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
 {
-  ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) );
-  arcVisualPtr->SetProperties( properties );
+  ArcVisualPtr arcVisualPtr(new ArcVisual(factoryCache));
+  arcVisualPtr->SetProperties(properties);
+  arcVisualPtr->Initialize();
   return arcVisualPtr;
 }
 
-ArcVisual::ArcVisual( VisualFactoryCache& factoryCache )
-: Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>( Toolkit::DevelVisual::ARC ) ),
-  mThickness( 0.0f ),
-  mRadius( 0.0f ),
-  mStartAngle( 0.0f ),
-  mSweepAngle( 360.0f ),
-  mRadiusIndex( Property::INVALID_INDEX ),
-  mCapType( DevelArcVisual::Cap::BUTT )
+ArcVisual::ArcVisual(VisualFactoryCache& factoryCache)
+: Visual::Base(factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>(Toolkit::DevelVisual::ARC)),
+  mThickness(0.0f),
+  mRadius(0.0f),
+  mStartAngle(0.0f),
+  mSweepAngle(360.0f),
+  mRadiusIndex(Property::INVALID_INDEX),
+  mThicknessIndex(Property::INVALID_INDEX),
+  mStartAngleIndex(Property::INVALID_INDEX),
+  mSweepAngleIndex(Property::INVALID_INDEX),
+  mCapType(DevelArcVisual::Cap::BUTT)
 {
 }
 
@@ -167,117 +73,159 @@ ArcVisual::~ArcVisual()
 {
 }
 
-void ArcVisual::DoSetProperties( const Property::Map& propertyMap )
+void ArcVisual::DoSetProperties(const Property::Map& propertyMap)
 {
-  Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME );
-  if( thicknessValue )
+  Property::Value* thicknessValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME);
+  if(thicknessValue)
   {
-    if( !thicknessValue->Get( mThickness ) )
+    if(!thicknessValue->Get(mThickness))
+    {
+      DALI_LOG_ERROR("ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType());
+    }
+    else
     {
-      DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() );
+      if(mImpl->mRenderer)
+      {
+        mImpl->mRenderer.SetProperty(mThicknessIndex, mThickness);
+
+        // Need to calculate radius again
+        OnSetTransform();
+      }
     }
   }
 
-  Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME );
-  if( startAngleValue )
+  Property::Value* startAngleValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME);
+  if(startAngleValue)
   {
-    if( !startAngleValue->Get( mStartAngle ) )
+    if(!startAngleValue->Get(mStartAngle))
+    {
+      DALI_LOG_ERROR("ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType());
+    }
+    else
     {
-      DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() );
+      if(mImpl->mRenderer)
+      {
+        mImpl->mRenderer.SetProperty(mStartAngleIndex, mStartAngle);
+      }
     }
   }
 
-  Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME );
-  if( sweepAngleValue )
+  Property::Value* sweepAngleValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME);
+  if(sweepAngleValue)
   {
-    if( !sweepAngleValue->Get( mSweepAngle ) )
+    if(!sweepAngleValue->Get(mSweepAngle))
     {
-      DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() );
+      DALI_LOG_ERROR("ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType());
+    }
+    else
+    {
+      if(mImpl->mRenderer)
+      {
+        mImpl->mRenderer.SetProperty(mSweepAngleIndex, mSweepAngle);
+      }
     }
   }
 
-  Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME );
-  if( capValue )
+  Property::Value* capValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::CAP, CAP_NAME);
+  if(capValue)
   {
     int capType = 0;
-    Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType );
-    mCapType = Toolkit::DevelArcVisual::Cap::Type( capType );
+    Scripting::GetEnumerationProperty(*capValue, CAP_TABLE, CAP_TABLE_COUNT, capType);
+    mCapType = Toolkit::DevelArcVisual::Cap::Type(capType);
   }
 }
 
-void ArcVisual::DoSetOnStage( Actor& actor )
+void ArcVisual::DoSetOnScene(Actor& actor)
 {
-  InitializeRenderer();
-
-  actor.AddRenderer( mImpl->mRenderer );
+  actor.AddRenderer(mImpl->mRenderer);
 
   // Arc Visual generated and ready to display
-  ResourceReady( Toolkit::Visual::ResourceStatus::READY );
+  ResourceReady(Toolkit::Visual::ResourceStatus::READY);
+}
+
+void ArcVisual::DoSetOffScene(Actor& actor)
+{
+  actor.RemoveRenderer(mImpl->mRenderer);
 }
 
-void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const
+void ArcVisual::DoCreatePropertyMap(Property::Map& map) const
 {
+  float thickness, startAngle, sweepAngle;
+  if(mImpl->mRenderer)
+  {
+    // Update values from Renderer
+    thickness  = mImpl->mRenderer.GetProperty<float>(mThicknessIndex);
+    startAngle = mImpl->mRenderer.GetProperty<float>(mStartAngleIndex);
+    sweepAngle = mImpl->mRenderer.GetProperty<float>(mSweepAngleIndex);
+  }
+  else
+  {
+    thickness  = mThickness;
+    startAngle = mStartAngle;
+    sweepAngle = mSweepAngle;
+  }
+
   map.Clear();
-  map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC );
-  map.Insert( Toolkit::DevelArcVisual::Property::THICKNESS, mThickness );
-  map.Insert( Toolkit::DevelArcVisual::Property::START_ANGLE, mStartAngle );
-  map.Insert( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, mSweepAngle );
-  map.Insert( Toolkit::DevelArcVisual::Property::CAP, mCapType );
+  map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC);
+  map.Insert(Toolkit::DevelArcVisual::Property::THICKNESS, thickness);
+  map.Insert(Toolkit::DevelArcVisual::Property::START_ANGLE, startAngle);
+  map.Insert(Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, sweepAngle);
+  map.Insert(Toolkit::DevelArcVisual::Property::CAP, mCapType);
 }
 
-void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
+void ArcVisual::DoCreateInstancePropertyMap(Property::Map& map) const
 {
   // Do nothing
 }
 
 void ArcVisual::OnSetTransform()
 {
-  Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
-  mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f;
+  Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize);
+  mRadius            = (std::min(visualSize.width, visualSize.height) - mThickness) / 2.0f;
 
-  if( mImpl->mRenderer )
+  if(mImpl->mRenderer)
   {
-    mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius );
+    mImpl->mRenderer.SetProperty(mRadiusIndex, mRadius);
   }
 }
 
-void ArcVisual::InitializeRenderer()
+void ArcVisual::OnInitialize()
 {
-  Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
+  Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
 
   Shader shader;
-  if( mCapType == DevelArcVisual::Cap::BUTT )
+  if(mCapType == DevelArcVisual::Cap::BUTT)
   {
-    shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER );
-    if( !shader )
+    shader = mFactoryCache.GetShader(VisualFactoryCache::ARC_BUTT_CAP_SHADER);
+    if(!shader)
     {
-      shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_BUTT_CAP );
-      mFactoryCache.SaveShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader );
+      shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_ARC_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_ARC_VISUAL_BUTT_CAP_SHADER_FRAG.data());
+      mFactoryCache.SaveShader(VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader);
     }
   }
   else
   {
-    shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER );
-    if( !shader )
+    shader = mFactoryCache.GetShader(VisualFactoryCache::ARC_ROUND_CAP_SHADER);
+    if(!shader)
     {
-      shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ROUND_CAP );
-      mFactoryCache.SaveShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader );
+      shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_ARC_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_ARC_VISUAL_ROUND_CAP_SHADER_FRAG.data());
+      mFactoryCache.SaveShader(VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader);
     }
   }
 
-  mImpl->mRenderer = Renderer::New( geometry, shader );
+  mImpl->mRenderer = Renderer::New(geometry, shader);
+  mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
 
-  mImpl->mRenderer.RegisterProperty( THICKNESS_NAME, mThickness );
-  mImpl->mRenderer.RegisterProperty( START_ANGLE_NAME, mStartAngle );
-  mImpl->mRenderer.RegisterProperty( SWEEP_ANGLE_NAME, mSweepAngle );
-  mImpl->mRenderer.RegisterProperty( CAP_NAME, 0.0f );
+  mThicknessIndex  = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::THICKNESS, THICKNESS_NAME, mThickness);
+  mStartAngleIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME, mStartAngle);
+  mSweepAngleIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME, mSweepAngle);
 
-  mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius );
+  mRadiusIndex = mImpl->mRenderer.RegisterProperty(RADIUS_NAME, mRadius);
 
-  mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
+  mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
 
   // Register transform properties
-  mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
+  mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
 }
 
 } // namespace Internal