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, const Property::Map& properties )
124 BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
125 borderVisualPtr->SetProperties( properties );
126 return borderVisualPtr;
129 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
130 : Visual::Base( factoryCache ),
131 mBorderColor( Color::TRANSPARENT ),
133 mBorderColorIndex( Property::INVALID_INDEX ),
134 mBorderSizeIndex( Property::INVALID_INDEX ),
135 mAntiAliasing( false )
139 BorderVisual::~BorderVisual()
143 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
145 Property::Value* color = propertyMap.Find( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME );
146 if( !( color && color->Get(mBorderColor) ) )
148 DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object\n" );
151 Property::Value* size = propertyMap.Find( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME );
152 if( !( size && size->Get(mBorderSize) ) )
154 DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object\n" );
157 Property::Value* antiAliasing = propertyMap.Find( Toolkit::BorderVisual::Property::ANTI_ALIASING, ANTI_ALIASING );
160 antiAliasing->Get( mAntiAliasing );
164 void BorderVisual::DoSetOnStage( Actor& actor )
166 InitializeRenderer();
168 mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
169 if( mBorderColor.a < 1.f || mAntiAliasing)
171 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
173 mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
175 actor.AddRenderer( mImpl->mRenderer );
178 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
181 map.Insert( DevelVisual::Property::TYPE, Toolkit::Visual::BORDER );
182 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
183 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
184 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
187 void BorderVisual::OnSetTransform()
189 if( mImpl->mRenderer )
191 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
195 void BorderVisual::InitializeRenderer()
197 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
200 geometry = CreateBorderGeometry();
201 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
205 Shader shader = GetBorderShader();
206 mImpl->mRenderer = Renderer::New( geometry, shader );
208 //Register transform properties
209 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
213 void BorderVisual::SetBorderColor(const Vector4& color)
215 mBorderColor = color;
217 if( mImpl->mRenderer )
219 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
222 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
227 void BorderVisual::SetBorderSize( float size )
231 if( mImpl->mRenderer )
233 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
237 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
239 if( mAntiAliasing != antiAliasing )
241 mAntiAliasing = antiAliasing;
242 if( mImpl->mRenderer )
244 Shader borderShader( GetBorderShader() );
245 mImpl->mRenderer.SetShader( borderShader );
248 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
254 Shader BorderVisual::GetBorderShader()
259 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
262 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
263 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
268 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
271 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
272 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
280 * Vertices and triangles of the border geometry:
282 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
295 Geometry BorderVisual::CreateBorderGeometry()
297 const float halfWidth = 0.5f;
298 const float halfHeight = 0.5f;
299 struct BorderVertex { Vector2 position; Vector2 drift;};
300 BorderVertex borderVertexData[16] =
302 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
303 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
304 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
305 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
307 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
308 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
309 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
310 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
312 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
313 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
314 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
315 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
317 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
318 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
319 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
320 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
323 Property::Map borderVertexFormat;
324 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
325 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
326 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
327 borderVertices.SetData( borderVertexData, 16 );
330 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};
332 // Create the geometry object
333 Geometry geometry = Geometry::New();
334 geometry.AddVertexBuffer( borderVertices );
335 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
336 geometry.SetType( Geometry::TRIANGLE_STRIP );
341 } // namespace Internal
343 } // namespace Toolkit