Anti-Aliasing of BorderRenderer 04/56504/5
authorXiangyin Ma <x1.ma@samsung.com>
Fri, 8 Jan 2016 15:16:52 +0000 (15:16 +0000)
committerXiangyin Ma <x1.ma@samsung.com>
Mon, 11 Jan 2016 15:03:29 +0000 (15:03 +0000)
Change-Id: I81fd631ec6e0ff394082081065c23d3673366a2c

automated-tests/src/dali-toolkit/utc-Dali-RendererFactory.cpp
dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.cpp
dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h
dali-toolkit/internal/controls/renderers/border/border-renderer.cpp
dali-toolkit/internal/controls/renderers/border/border-renderer.h
dali-toolkit/internal/controls/renderers/renderer-factory-cache.h
dali-toolkit/internal/controls/renderers/renderer-factory-impl.cpp
dali-toolkit/internal/controls/renderers/renderer-factory-impl.h

index 31ac725..9a66d4b 100644 (file)
@@ -354,6 +354,7 @@ int UtcDaliRendererFactoryGetBorderRenderer1(void)
   controlRenderer.SetOnStage( actor );
 
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
   controlRenderer.SetOnStage( actor );
 
   DALI_TEST_CHECK( actor.GetRendererCount() == 1u );
+  DALI_TEST_EQUALS( actor.GetRendererAt(0).GetMaterial().GetBlendMode(), BlendingMode::ON, TEST_LOCATION );
 
   TestGlAbstraction& gl = application.GetGlAbstraction();
 
 
   TestGlAbstraction& gl = application.GetGlAbstraction();
 
@@ -382,10 +383,10 @@ int UtcDaliRendererFactoryGetBorderRenderer2(void)
   RendererFactory factory = RendererFactory::Get();
   DALI_TEST_CHECK( factory );
 
   RendererFactory factory = RendererFactory::Get();
   DALI_TEST_CHECK( factory );
 
-  Vector4 testColor( 1.f, 0.5f, 0.3f, 0.2f );
+  Vector4 testColor( 1.f, 0.5f, 0.3f, 1.f );
   float testSize = 5.f;
 
   float testSize = 5.f;
 
-  ControlRenderer controlRenderer = factory.GetControlRenderer(testSize, testColor);
+  ControlRenderer controlRenderer = factory.GetControlRenderer(testSize, testColor );
   DALI_TEST_CHECK( controlRenderer );
 
   Actor actor = Actor::New();
   DALI_TEST_CHECK( controlRenderer );
 
   Actor actor = Actor::New();
@@ -401,6 +402,8 @@ int UtcDaliRendererFactoryGetBorderRenderer2(void)
   application.SendNotification();
   application.Render(0);
 
   application.SendNotification();
   application.Render(0);
 
+  DALI_TEST_EQUALS( actor.GetRendererAt(0).GetMaterial().GetBlendMode(), BlendingMode::AUTO, TEST_LOCATION );
+
   Vector4 actualColor(Vector4::ZERO);
   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "borderColor", actualColor ) );
   DALI_TEST_EQUALS( actualColor, testColor, TEST_LOCATION );
   Vector4 actualColor(Vector4::ZERO);
   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "borderColor", actualColor ) );
   DALI_TEST_EQUALS( actualColor, testColor, TEST_LOCATION );
@@ -409,10 +412,20 @@ int UtcDaliRendererFactoryGetBorderRenderer2(void)
   DALI_TEST_CHECK( gl.GetUniformValue<float>( "borderSize", actualSize ) );
   DALI_TEST_EQUALS( actualSize, testSize, TEST_LOCATION );
 
   DALI_TEST_CHECK( gl.GetUniformValue<float>( "borderSize", actualSize ) );
   DALI_TEST_EQUALS( actualSize, testSize, TEST_LOCATION );
 
+  controlRenderer.SetOffStage( actor );
+
+  // enable the anti-aliasing
+  controlRenderer = factory.GetControlRenderer(testSize, testColor, true );
+  controlRenderer.SetOnStage( actor );
+
+  application.SendNotification();
+  application.Render(0);
+  DALI_TEST_EQUALS( actor.GetRendererAt(0).GetMaterial().GetBlendMode(), BlendingMode::ON, TEST_LOCATION );
+
+
   END_TEST;
 }
 
   END_TEST;
 }
 
-
 int UtcDaliRendererFactoryGetLinearGradientRenderer(void)
 {
   ToolkitTestApplication application;
 int UtcDaliRendererFactoryGetLinearGradientRenderer(void)
 {
   ToolkitTestApplication application;
index 084ddcb..b1de7f0 100644 (file)
@@ -111,7 +111,13 @@ void RendererFactory::ResetRenderer( ControlRenderer& renderer, Actor& actor, co
 
 ControlRenderer RendererFactory::GetControlRenderer( float borderSize, const Vector4& borderColor )
 {
 
 ControlRenderer RendererFactory::GetControlRenderer( float borderSize, const Vector4& borderColor )
 {
-  return GetImplementation( *this ).GetControlRenderer( borderSize, borderColor );
+  return GetImplementation( *this ).GetControlRenderer( borderSize, borderColor, false );
+}
+
+
+ControlRenderer RendererFactory::GetControlRenderer( float borderSize, const Vector4& borderColor, bool antiAliasing )
+{
+  return GetImplementation( *this ).GetControlRenderer( borderSize, borderColor, antiAliasing );
 }
 
 ControlRenderer RendererFactory::GetControlRenderer( const Image& image )
 }
 
 ControlRenderer RendererFactory::GetControlRenderer( const Image& image )
index 6dffc9a..c6fc451 100644 (file)
@@ -127,6 +127,16 @@ public:
   ControlRenderer GetControlRenderer( float borderSize, const Vector4& borderColor );
 
   /**
   ControlRenderer GetControlRenderer( float borderSize, const Vector4& borderColor );
 
   /**
+   * @brief Request the control renderer to renderer the border with the given size and color, and specify whether anti-aliasing is needed.
+   *
+   * @param[in] borderSize The size of the border. Border size is the same along all edges.
+   * @param[in] borderColor The color of the border.
+   * @param[in] antiAliasing Whether anti-aliasing is required for border rendering.
+   * @return The pointer pointing to the control renderer
+   */
+  ControlRenderer GetControlRenderer( float borderSize, const Vector4& borderColor, bool antiAliasing );
+
+  /**
    * @brief Request the control renderer to render the image.
    *
    * @param[in] image The image to be rendered.
    * @brief Request the control renderer to render the image.
    *
    * @param[in] image The image to be rendered.
index 19872bd..396d02c 100644 (file)
@@ -42,6 +42,7 @@ const char * const RENDERER_TYPE_VALUE("borderRenderer");
 
 const char * const COLOR_NAME("borderColor");
 const char * const SIZE_NAME("borderSize");
 
 const char * const COLOR_NAME("borderColor");
 const char * const SIZE_NAME("borderSize");
+const char * const ANTI_ALIASING("antiAliasing");
 
 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
 
 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
@@ -71,6 +72,35 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
     gl_FragColor = borderColor*uColor;\n
   }\n
 );
     gl_FragColor = borderColor*uColor;\n
   }\n
 );
+
+const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
+  attribute mediump vec2 aPosition;\n
+  attribute mediump vec2 aDrift;\n
+  uniform mediump mat4 uMvpMatrix;\n
+  uniform mediump vec3 uSize;\n
+  uniform mediump float borderSize;\n
+  varying mediump float vAlpha;\n
+  \n
+  void main()\n
+  {\n
+    vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
+    gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
+    vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
+  }\n
+);
+
+const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
+  uniform lowp vec4 uColor;\n
+  uniform lowp vec4 borderColor;\n
+  uniform mediump float borderSize;\n
+  varying mediump float vAlpha;\n
+  \n
+  void main()\n
+  {\n
+    gl_FragColor = borderColor*uColor;\n
+    gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
+  }\n
+);
 }
 
 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
 }
 
 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
@@ -78,7 +108,8 @@ BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
   mBorderColor( Color::TRANSPARENT ),
   mBorderSize( 0.f ),
   mBorderColorIndex( Property::INVALID_INDEX ),
   mBorderColor( Color::TRANSPARENT ),
   mBorderSize( 0.f ),
   mBorderColorIndex( Property::INVALID_INDEX ),
-  mBorderSizeIndex( Property::INVALID_INDEX )
+  mBorderSizeIndex( Property::INVALID_INDEX ),
+  mAntiAliasing( false )
 {
 }
 
 {
 }
 
@@ -99,6 +130,12 @@ void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMa
   {
     DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
   }
   {
     DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
   }
+
+  Property::Value* antiAliasing = propertyMap.Find( ANTI_ALIASING );
+  if( antiAliasing )
+  {
+    antiAliasing->Get( mAntiAliasing );
+  }
 }
 
 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
 }
 
 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
@@ -113,7 +150,7 @@ void BorderRenderer::DoSetOnStage( Actor& actor )
   InitializeRenderer();
 
   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
   InitializeRenderer();
 
   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
-  if( mBorderColor.a < 1.f )
+  if( mBorderColor.a < 1.f || mAntiAliasing)
   {
     (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
   }
   {
     (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
   }
@@ -137,14 +174,7 @@ void BorderRenderer::InitializeRenderer()
     mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
   }
 
     mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
   }
 
-  Shader shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
-  if( !shader )
-  {
-    shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
-    mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER, shader );
-  }
-
-  Material material = Material::New( shader );
+  Material material = Material::New( GetBorderShader() );
   mImpl->mRenderer = Renderer::New( geometry, material );
 }
 
   mImpl->mRenderer = Renderer::New( geometry, material );
 }
 
@@ -155,7 +185,7 @@ void BorderRenderer::SetBorderColor(const Vector4& color)
   if( mImpl->mRenderer )
   {
     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
   if( mImpl->mRenderer )
   {
     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
-    if( color.a < 1.f &&  (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
+    if( color.a < 1.f )
     {
       (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
     }
     {
       (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
     }
@@ -172,14 +202,57 @@ void BorderRenderer::SetBorderSize( float size )
   }
 }
 
   }
 }
 
+void BorderRenderer::RequireAntiAliasing( bool antiAliasing )
+{
+  if( mAntiAliasing != antiAliasing )
+  {
+    mAntiAliasing = antiAliasing;
+    if( mImpl->mRenderer )
+    {
+      Material material =  mImpl->mRenderer.GetMaterial();
+      Shader shader = GetBorderShader();
+      material.SetShader( shader );
+      if( mAntiAliasing )
+      {
+        material.SetBlendMode( BlendingMode::ON );
+      }
+    }
+  }
+}
+
+Shader BorderRenderer::GetBorderShader()
+{
+  Shader shader;
+  if( mAntiAliasing )
+  {
+    shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING );
+    if( !shader )
+    {
+      shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
+      mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
+    }
+  }
+  else
+  {
+    shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
+    if( !shader )
+    {
+      shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
+      mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER, shader );
+    }
+  }
+
+  return shader;
+}
+
 /**
  * Vertices and triangles of the border geometry:
  *
  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
  *
  * 0--1--2--3
 /**
  * Vertices and triangles of the border geometry:
  *
  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
  *
  * 0--1--2--3
- * | /| /| /|
- * ||/ |/ |
+ * || /| /|
+ * | \|/ |/ |
  * 4--5--6--7
  * |\ |  |\ |
  * | \|  | \|
  * 4--5--6--7
  * |\ |  |\ |
  * | \|  | \|
@@ -223,7 +296,7 @@ Geometry BorderRenderer::CreateBorderGeometry()
   borderVertices.SetData(borderVertexData);
 
   // Create indices
   borderVertices.SetData(borderVertexData);
 
   // Create indices
-  unsigned int indexData[24] = { 0,4,1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5 };
+  unsigned int indexData[24] = { 1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5,0,1};
   Property::Map indexFormat;
   indexFormat[INDEX_NAME] = Property::INTEGER;
   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
   Property::Map indexFormat;
   indexFormat[INDEX_NAME] = Property::INTEGER;
   PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
index 63160fb..56ff901 100644 (file)
@@ -41,6 +41,7 @@ namespace Internal
  * |-----------------|-------------|
  * | borderColor     | VECTOR4     |
  * | borderSize      | FLOAT       |
  * |-----------------|-------------|
  * | borderColor     | VECTOR4     |
  * | borderSize      | FLOAT       |
+ * | antiAliasing    | BOOLEAN     |
  */
 
 class BorderRenderer : public ControlRenderer
  */
 
 class BorderRenderer : public ControlRenderer
@@ -97,6 +98,12 @@ public:
    */
   void SetBorderSize( float size );
 
    */
   void SetBorderSize( float size );
 
+  /**
+   * Enable/Disable the anti-aliasing.
+   * @param[in] enable Whether the anti-aliasing be enabled or not.
+   */
+  void RequireAntiAliasing( bool antiAliasing );
+
 private:
 
   /**
 private:
 
   /**
@@ -105,6 +112,12 @@ private:
   void InitializeRenderer();
 
   /**
   void InitializeRenderer();
 
   /**
+   * Request the border shader from the factory cache. If fail, create tha shader and add it to cache.
+   * @return The border shader.
+   */
+  Shader GetBorderShader();
+
+  /**
    * Create the geometry which presents the border.
    * @return The border geometry
    */
    * Create the geometry which presents the border.
    * @return The border geometry
    */
@@ -123,6 +136,8 @@ private:
 
   Property::Index mBorderColorIndex;
   Property::Index mBorderSizeIndex;
 
   Property::Index mBorderColorIndex;
   Property::Index mBorderSizeIndex;
+
+  bool mAntiAliasing;
 };
 
 } // namespace Internal
 };
 
 } // namespace Internal
index 14ae4a5..2dbcd7a 100644 (file)
@@ -51,6 +51,7 @@ public:
   {
     COLOR_SHADER,
     BORDER_SHADER,
   {
     COLOR_SHADER,
     BORDER_SHADER,
+    BORDER_SHADER_ANTI_ALIASING,
     GRADIENT_SHADER_LINEAR_USER_SPACE,
     GRADIENT_SHADER_LINEAR_BOUNDING_BOX,
     GRADIENT_SHADER_RADIAL_USER_SPACE,
     GRADIENT_SHADER_LINEAR_USER_SPACE,
     GRADIENT_SHADER_LINEAR_BOUNDING_BOX,
     GRADIENT_SHADER_RADIAL_USER_SPACE,
index 41b3060..c931513 100644 (file)
@@ -180,7 +180,7 @@ void RendererFactory::ResetRenderer( Toolkit::ControlRenderer& renderer, Actor&
   }
 }
 
   }
 }
 
-Toolkit::ControlRenderer RendererFactory::GetControlRenderer( float borderSize, const Vector4& borderColor )
+Toolkit::ControlRenderer RendererFactory::GetControlRenderer( float borderSize, const Vector4& borderColor, bool antiAliasing )
 {
   if( !mFactoryCache )
   {
 {
   if( !mFactoryCache )
   {
@@ -196,6 +196,7 @@ Toolkit::ControlRenderer RendererFactory::GetControlRenderer( float borderSize,
 
   rendererPtr->SetBorderSize( borderSize );
   rendererPtr->SetBorderColor( borderColor );
 
   rendererPtr->SetBorderSize( borderSize );
   rendererPtr->SetBorderColor( borderColor );
+  rendererPtr->RequireAntiAliasing( antiAliasing );
 
   return Toolkit::ControlRenderer( rendererPtr );
 }
 
   return Toolkit::ControlRenderer( rendererPtr );
 }
@@ -381,7 +382,7 @@ void RendererFactory::ResetRenderer( Toolkit::ControlRenderer& renderer, Actor&
   }
 
   renderer = GetControlRenderer( propertyMap );
   }
 
   renderer = GetControlRenderer( propertyMap );
-  if( actor && actor.OnStage() )
+  if( renderer && actor && actor.OnStage() )
   {
     renderer.SetOnStage( actor );
   }
   {
     renderer.SetOnStage( actor );
   }
index 033b904..e129aef 100644 (file)
@@ -75,9 +75,9 @@ public:
   void ResetRenderer( Toolkit::ControlRenderer& renderer, Actor& actor, const Vector4& color );
 
   /**
   void ResetRenderer( Toolkit::ControlRenderer& renderer, Actor& actor, const Vector4& color );
 
   /**
-   * @copydoc Toolkit::RenderFactory::GetControlRenderer( float, const Vector4& )
+   * @copydoc Toolkit::RenderFactory::GetControlRenderer( float, const Vector4&, bool )
    */
    */
-  Toolkit::ControlRenderer GetControlRenderer( float borderSize, const Vector4& borderColor );
+  Toolkit::ControlRenderer GetControlRenderer( float borderSize, const Vector4& borderColor, bool antiAliasing );
 
   /**
    * @copydoc Toolkit::RenderFactory::GetControlRenderer( const Image& )
 
   /**
    * @copydoc Toolkit::RenderFactory::GetControlRenderer( const Image& )