Blending enum clean-up
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / border / border-renderer.cpp
index 19872bd..d765ac7 100644 (file)
@@ -24,6 +24,7 @@
 //INTERNAL INCLUDES
 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
+#include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
 
 namespace Dali
@@ -37,11 +38,9 @@ namespace Internal
 
 namespace
 {
-const char * const RENDERER_TYPE("rendererType");
-const char * const RENDERER_TYPE_VALUE("borderRenderer");
-
 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");
@@ -71,6 +70,35 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
     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 )
@@ -78,7 +106,8 @@ BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
   mBorderColor( Color::TRANSPARENT ),
   mBorderSize( 0.f ),
   mBorderColorIndex( Property::INVALID_INDEX ),
-  mBorderSizeIndex( Property::INVALID_INDEX )
+  mBorderSizeIndex( Property::INVALID_INDEX ),
+  mAntiAliasing( false )
 {
 }
 
@@ -99,6 +128,12 @@ void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMa
   {
     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 )
@@ -113,9 +148,9 @@ void BorderRenderer::DoSetOnStage( Actor& actor )
   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.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
   }
   mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
 }
@@ -123,7 +158,7 @@ void BorderRenderer::DoSetOnStage( Actor& actor )
 void BorderRenderer::DoCreatePropertyMap( Property::Map& map ) const
 {
   map.Clear();
-  map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
+  map.Insert( RENDERER_TYPE, BORDER_RENDERER );
   map.Insert( COLOR_NAME, mBorderColor );
   map.Insert( SIZE_NAME, mBorderSize );
 }
@@ -137,15 +172,10 @@ void BorderRenderer::InitializeRenderer()
     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 );
-  mImpl->mRenderer = Renderer::New( geometry, material );
+  Shader shader = GetBorderShader();
+  mImpl->mRenderer = Renderer::New( geometry, shader  );
+
 }
 
 void BorderRenderer::SetBorderColor(const Vector4& color)
@@ -155,9 +185,9 @@ void BorderRenderer::SetBorderColor(const Vector4& 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.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
     }
   }
 }
@@ -172,14 +202,56 @@ void BorderRenderer::SetBorderSize( float size )
   }
 }
 
+void BorderRenderer::RequireAntiAliasing( bool antiAliasing )
+{
+  if( mAntiAliasing != antiAliasing )
+  {
+    mAntiAliasing = antiAliasing;
+    if( mImpl->mRenderer )
+    {
+      Shader borderShader( GetBorderShader() );
+      mImpl->mRenderer.SetShader( borderShader );
+      if( mAntiAliasing )
+      {
+        mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::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
- * | /| /| /|
- * ||/ |/ |
+ * || /| /|
+ * | \|/ |/ |
  * 4--5--6--7
  * |\ |  |\ |
  * | \|  | \|
@@ -219,20 +291,16 @@ Geometry BorderRenderer::CreateBorderGeometry()
   Property::Map borderVertexFormat;
   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
-  PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat, 16 );
-  borderVertices.SetData(borderVertexData);
+  PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
+  borderVertices.SetData( borderVertexData, 16 );
 
   // 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 };
-  Property::Map indexFormat;
-  indexFormat[INDEX_NAME] = Property::INTEGER;
-  PropertyBuffer indices = PropertyBuffer::New( indexFormat, 24 );
-  indices.SetData(indexData);
+  unsigned short 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};
 
   // Create the geometry object
   Geometry geometry = Geometry::New();
   geometry.AddVertexBuffer( borderVertices );
-  geometry.SetIndexBuffer( indices );
+  geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
 
   return geometry;