2 * Copyright (c) 2016 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-visual.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
26 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
30 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
44 const char * const COLOR_NAME("borderColor");
45 const char * const SIZE_NAME("borderSize");
46 const char * const ANTI_ALIASING("antiAliasing");
48 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
49 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
50 const char * const INDEX_NAME("indices");
53 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
54 attribute mediump vec2 aPosition;\n
55 attribute mediump vec2 aDrift;\n
56 uniform mediump mat4 uMvpMatrix;\n
57 uniform mediump vec3 uSize;\n
58 uniform mediump float borderSize;\n
61 //Visual size and offset
62 uniform mediump vec2 offset;\n
63 uniform mediump vec2 size;\n
64 uniform mediump vec4 offsetSizeMode;\n
65 uniform mediump vec2 origin;\n
66 uniform mediump vec2 anchorPoint;\n
68 vec2 ComputeVertexPosition()\n
70 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
71 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
72 return (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy;\n
77 vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n
78 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
82 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
83 uniform lowp vec4 uColor;\n
84 uniform lowp vec4 borderColor;\n
88 gl_FragColor = borderColor*uColor;\n
92 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
93 attribute mediump vec2 aPosition;\n
94 attribute mediump vec2 aDrift;\n
95 uniform mediump mat4 uMvpMatrix;\n
96 uniform mediump vec3 uSize;\n
97 uniform mediump float borderSize;\n
98 varying mediump float vAlpha;\n
102 vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
103 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
104 vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
108 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
109 uniform lowp vec4 uColor;\n
110 uniform lowp vec4 borderColor;\n
111 uniform mediump float borderSize;\n
112 varying mediump float vAlpha;\n
116 gl_FragColor = borderColor*uColor;\n
117 gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
122 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache )
124 return new BorderVisual( factoryCache );
127 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
128 : Visual::Base( factoryCache ),
129 mBorderColor( Color::TRANSPARENT ),
131 mBorderColorIndex( Property::INVALID_INDEX ),
132 mBorderSizeIndex( Property::INVALID_INDEX ),
133 mAntiAliasing( false )
137 BorderVisual::~BorderVisual()
141 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
143 Property::Value* color = propertyMap.Find( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME );
144 if( !( color && color->Get(mBorderColor) ) )
146 DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object\n" );
149 Property::Value* size = propertyMap.Find( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME );
150 if( !( size && size->Get(mBorderSize) ) )
152 DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object\n" );
155 Property::Value* antiAliasing = propertyMap.Find( Toolkit::BorderVisual::Property::ANTI_ALIASING, ANTI_ALIASING );
158 antiAliasing->Get( mAntiAliasing );
162 void BorderVisual::DoSetOnStage( Actor& actor )
164 InitializeRenderer();
166 mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
167 if( mBorderColor.a < 1.f || mAntiAliasing)
169 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
171 mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
173 actor.AddRenderer( mImpl->mRenderer );
176 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
179 map.Insert( DevelVisual::Property::TYPE, Toolkit::Visual::BORDER );
180 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
181 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
182 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
185 void BorderVisual::OnSetTransform()
187 if( mImpl->mRenderer )
189 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
193 void BorderVisual::InitializeRenderer()
195 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
198 geometry = CreateBorderGeometry();
199 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
203 Shader shader = GetBorderShader();
204 mImpl->mRenderer = Renderer::New( geometry, shader );
206 //Register transform properties
207 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
211 void BorderVisual::SetBorderColor(const Vector4& color)
213 mBorderColor = color;
215 if( mImpl->mRenderer )
217 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
220 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
225 void BorderVisual::SetBorderSize( float size )
229 if( mImpl->mRenderer )
231 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
235 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
237 if( mAntiAliasing != antiAliasing )
239 mAntiAliasing = antiAliasing;
240 if( mImpl->mRenderer )
242 Shader borderShader( GetBorderShader() );
243 mImpl->mRenderer.SetShader( borderShader );
246 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
252 Shader BorderVisual::GetBorderShader()
257 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
260 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
261 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
266 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
269 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
270 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
278 * Vertices and triangles of the border geometry:
280 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
293 Geometry BorderVisual::CreateBorderGeometry()
295 const float halfWidth = 0.5f;
296 const float halfHeight = 0.5f;
297 struct BorderVertex { Vector2 position; Vector2 drift;};
298 BorderVertex borderVertexData[16] =
300 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
301 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
302 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
303 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
305 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
306 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
307 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
308 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
310 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
311 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
312 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
313 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
315 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
316 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
317 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
318 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
321 Property::Map borderVertexFormat;
322 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
323 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
324 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
325 borderVertices.SetData( borderVertexData, 16 );
328 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};
330 // Create the geometry object
331 Geometry geometry = Geometry::New();
332 geometry.AddVertexBuffer( borderVertices );
333 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
334 geometry.SetType( Geometry::TRIANGLE_STRIP );
339 } // namespace Internal
341 } // namespace Toolkit