//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
namespace
{
-const char * const RENDERER_TYPE("renderer-type");
-const char * const RENDERER_TYPE_VALUE("border-renderer");
-
-const char * const COLOR_NAME("border-color");
-const char * const COLOR_UNIFORM_NAME("uBorderColor");
-const char * const SIZE_NAME("border-size");
-const char * const SIZE_UNIFORM_NAME("uBorderSize");
+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");
attribute mediump vec2 aDrift;\n
uniform mediump mat4 uMvpMatrix;\n
uniform mediump vec3 uSize;\n
- uniform mediump float uBorderSize;\n
+ uniform mediump float borderSize;\n
\n
void main()\n
{\n
- vec2 position = aPosition*uSize.xy + aDrift*uBorderSize;\n
+ vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
}\n
);
const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
uniform lowp vec4 uColor;\n
- uniform lowp vec4 uBorderColor;\n
+ uniform lowp vec4 borderColor;\n
+ \n
+ void main()\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 = uBorderColor*uColor;\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()
-: ControlRenderer(),
+BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
+: ControlRenderer( factoryCache ),
mBorderColor( Color::TRANSPARENT ),
mBorderSize( 0.f ),
mBorderColorIndex( Property::INVALID_INDEX ),
- mBorderSizeIndex( Property::INVALID_INDEX )
+ mBorderSizeIndex( Property::INVALID_INDEX ),
+ mAntiAliasing( false )
{
}
{
}
-void BorderRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
+void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
{
- Initialize( factoryCache );
-
Property::Value* color = propertyMap.Find( COLOR_NAME );
if( !( color && color->Get(mBorderColor) ) )
{
{
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::DoSetOnStage( Actor& actor )
{
- mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_UNIFORM_NAME, mBorderColor );
- if( mBorderColor.a < 1.f )
+ InitializeRenderer();
+
+ mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
+ if( mBorderColor.a < 1.f || mAntiAliasing)
{
- (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
+ mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
}
- mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_UNIFORM_NAME, mBorderSize );
+ mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
}
-void BorderRenderer::CreatePropertyMap( Property::Map& map ) const
+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 );
}
-void BorderRenderer::Initialize( RendererFactoryCache& factoryCache)
+void BorderRenderer::InitializeRenderer()
{
- mImpl->mGeometry = factoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
- if( !(mImpl->mGeometry) )
+ Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
+ if( !geometry )
{
- mImpl->mGeometry = CreateBorderGeometry();
- factoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, mImpl->mGeometry );
+ geometry = CreateBorderGeometry();
+ mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
}
- mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
- if( !(mImpl->mShader) )
- {
- mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
- factoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, mImpl->mShader );
- }
+
+ Shader shader = GetBorderShader();
+ mImpl->mRenderer = Renderer::New( geometry, shader );
+
}
void BorderRenderer::SetBorderColor(const Vector4& color)
{
mBorderColor = color;
- if( mImpl->mIsOnStage )
+ 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::BLENDING_MODE, BlendingMode::ON );
}
}
}
{
mBorderSize = size;
- if( mImpl->mIsOnStage )
+ if( mImpl->mRenderer )
{
(mImpl->mRenderer).SetProperty( mBorderSizeIndex, 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::BLENDING_MODE, 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
- * | /| /| /|
- * |/ |/ |/ |
+ * |\ | /| /|
+ * | \|/ |/ |
* 4--5--6--7
- * | /| | /|
- * |/ | |/ |
+ * |\ | |\ |
+ * | \| | \|
* 8--9--10-11
- * | /| /| /|
- * |/ |/ |/ |
+ * | /| /|\ |
+ * |/ |/ | \|
* 12-13-14-15
*/
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[48] = { 0, 4, 1, 1, 4, 5, 1, 5, 2, 2, 5, 6, 2, 6,3, 3, 6, 7,
- 4, 8, 5, 5, 8, 9, 6, 10, 7, 7, 10, 11,
- 8, 12, 9, 9, 12, 13, 9, 13, 10, 10, 13, 14, 10, 11, 14, 11, 14, 15};
-
- Property::Map indexFormat;
- indexFormat[INDEX_NAME] = Property::INTEGER;
- PropertyBuffer indices = PropertyBuffer::New( indexFormat, 48 );
- 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;
}