2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "border-renderer.h"
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
26 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
27 #include <dali-toolkit/internal/controls/renderers/renderer-string-constants.h>
28 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
41 const char * const COLOR_NAME("borderColor");
42 const char * const SIZE_NAME("borderSize");
43 const char * const ANTI_ALIASING("antiAliasing");
45 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
46 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
47 const char * const INDEX_NAME("indices");
50 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
51 attribute mediump vec2 aPosition;\n
52 attribute mediump vec2 aDrift;\n
53 uniform mediump mat4 uMvpMatrix;\n
54 uniform mediump vec3 uSize;\n
55 uniform mediump float borderSize;\n
59 vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
60 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
64 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
65 uniform lowp vec4 uColor;\n
66 uniform lowp vec4 borderColor;\n
70 gl_FragColor = borderColor*uColor;\n
74 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
75 attribute mediump vec2 aPosition;\n
76 attribute mediump vec2 aDrift;\n
77 uniform mediump mat4 uMvpMatrix;\n
78 uniform mediump vec3 uSize;\n
79 uniform mediump float borderSize;\n
80 varying mediump float vAlpha;\n
84 vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
85 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
86 vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
90 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
91 uniform lowp vec4 uColor;\n
92 uniform lowp vec4 borderColor;\n
93 uniform mediump float borderSize;\n
94 varying mediump float vAlpha;\n
98 gl_FragColor = borderColor*uColor;\n
99 gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
104 BorderRenderer::BorderRenderer( RendererFactoryCache& factoryCache )
105 : ControlRenderer( factoryCache ),
106 mBorderColor( Color::TRANSPARENT ),
108 mBorderColorIndex( Property::INVALID_INDEX ),
109 mBorderSizeIndex( Property::INVALID_INDEX ),
110 mAntiAliasing( false )
114 BorderRenderer::~BorderRenderer()
118 void BorderRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
120 Property::Value* color = propertyMap.Find( COLOR_NAME );
121 if( !( color && color->Get(mBorderColor) ) )
123 DALI_LOG_ERROR( "Fail to provide a border color to the BorderRenderer object" );
126 Property::Value* size = propertyMap.Find( SIZE_NAME );
127 if( !( size && size->Get(mBorderSize) ) )
129 DALI_LOG_ERROR( "Fail to provide a border size to the BorderRenderer object" );
132 Property::Value* antiAliasing = propertyMap.Find( ANTI_ALIASING );
135 antiAliasing->Get( mAntiAliasing );
139 void BorderRenderer::SetClipRect( const Rect<int>& clipRect )
141 ControlRenderer::SetClipRect( clipRect );
143 //ToDo: renderer responds to the clipRect change
146 void BorderRenderer::DoSetOnStage( Actor& actor )
148 InitializeRenderer();
150 mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
151 if( mBorderColor.a < 1.f || mAntiAliasing)
153 mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
155 mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
158 void BorderRenderer::DoCreatePropertyMap( Property::Map& map ) const
161 map.Insert( RENDERER_TYPE, BORDER_RENDERER );
162 map.Insert( COLOR_NAME, mBorderColor );
163 map.Insert( SIZE_NAME, mBorderSize );
166 void BorderRenderer::InitializeRenderer()
168 Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::BORDER_GEOMETRY );
171 geometry = CreateBorderGeometry();
172 mFactoryCache.SaveGeometry( RendererFactoryCache::BORDER_GEOMETRY, geometry );
176 Shader shader = GetBorderShader();
177 mImpl->mRenderer = Renderer::New( geometry, shader );
181 void BorderRenderer::SetBorderColor(const Vector4& color)
183 mBorderColor = color;
185 if( mImpl->mRenderer )
187 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
190 mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
195 void BorderRenderer::SetBorderSize( float size )
199 if( mImpl->mRenderer )
201 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
205 void BorderRenderer::RequireAntiAliasing( bool antiAliasing )
207 if( mAntiAliasing != antiAliasing )
209 mAntiAliasing = antiAliasing;
210 if( mImpl->mRenderer )
212 Shader borderShader( GetBorderShader() );
213 mImpl->mRenderer.SetShader( borderShader );
216 mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
222 Shader BorderRenderer::GetBorderShader()
227 shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING );
230 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
231 mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
236 shader = mFactoryCache.GetShader( RendererFactoryCache::BORDER_SHADER );
239 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
240 mFactoryCache.SaveShader( RendererFactoryCache::BORDER_SHADER, shader );
248 * Vertices and triangles of the border geometry:
250 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
263 Geometry BorderRenderer::CreateBorderGeometry()
265 const float halfWidth = 0.5f;
266 const float halfHeight = 0.5f;
267 struct BorderVertex { Vector2 position; Vector2 drift;};
268 BorderVertex borderVertexData[16] =
270 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
271 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
272 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
273 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
275 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
276 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
277 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
278 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
280 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
281 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
282 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
283 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
285 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
286 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
287 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
288 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
291 Property::Map borderVertexFormat;
292 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
293 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
294 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
295 borderVertices.SetData( borderVertexData, 16 );
298 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};
300 // Create the geometry object
301 Geometry geometry = Geometry::New();
302 geometry.AddVertexBuffer( borderVertices );
303 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
304 geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
309 } // namespace Internal
311 } // namespace Toolkit