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>
25 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
26 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
27 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
28 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
29 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
42 const char * const COLOR_NAME("borderColor");
43 const char * const SIZE_NAME("borderSize");
44 const char * const ANTI_ALIASING("antiAliasing");
46 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
47 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
48 const char * const INDEX_NAME("indices");
51 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
52 attribute mediump vec2 aPosition;\n
53 attribute mediump vec2 aDrift;\n
54 uniform mediump mat4 uMvpMatrix;\n
55 uniform mediump vec3 uSize;\n
56 uniform mediump float borderSize;\n
60 vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
61 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
65 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
66 uniform lowp vec4 uColor;\n
67 uniform lowp vec4 borderColor;\n
71 gl_FragColor = borderColor*uColor;\n
75 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
76 attribute mediump vec2 aPosition;\n
77 attribute mediump vec2 aDrift;\n
78 uniform mediump mat4 uMvpMatrix;\n
79 uniform mediump vec3 uSize;\n
80 uniform mediump float borderSize;\n
81 varying mediump float vAlpha;\n
85 vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
86 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
87 vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
91 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
92 uniform lowp vec4 uColor;\n
93 uniform lowp vec4 borderColor;\n
94 uniform mediump float borderSize;\n
95 varying mediump float vAlpha;\n
99 gl_FragColor = borderColor*uColor;\n
100 gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
105 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
106 : Visual::Base( factoryCache ),
107 mBorderColor( Color::TRANSPARENT ),
109 mBorderColorIndex( Property::INVALID_INDEX ),
110 mBorderSizeIndex( Property::INVALID_INDEX ),
111 mAntiAliasing( false )
115 BorderVisual::~BorderVisual()
119 void BorderVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
121 Property::Value* color = propertyMap.Find( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME );
122 if( !( color && color->Get(mBorderColor) ) )
124 DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object\n" );
127 Property::Value* size = propertyMap.Find( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME );
128 if( !( size && size->Get(mBorderSize) ) )
130 DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object\n" );
133 Property::Value* antiAliasing = propertyMap.Find( Toolkit::BorderVisual::Property::ANTI_ALIASING, ANTI_ALIASING );
136 antiAliasing->Get( mAntiAliasing );
140 void BorderVisual::SetClipRect( const Rect<int>& clipRect )
142 Visual::Base::SetClipRect( clipRect );
144 //ToDo: renderer responds to the clipRect change
147 void BorderVisual::DoSetOnStage( Actor& actor )
149 InitializeRenderer();
151 mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
152 if( mBorderColor.a < 1.f || mAntiAliasing)
154 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
156 mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
159 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
162 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER );
163 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
164 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
165 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
168 void BorderVisual::InitializeRenderer()
170 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
173 geometry = CreateBorderGeometry();
174 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
178 Shader shader = GetBorderShader();
179 mImpl->mRenderer = Renderer::New( geometry, shader );
183 void BorderVisual::SetBorderColor(const Vector4& color)
185 mBorderColor = color;
187 if( mImpl->mRenderer )
189 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
192 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
197 void BorderVisual::SetBorderSize( float size )
201 if( mImpl->mRenderer )
203 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
207 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
209 if( mAntiAliasing != antiAliasing )
211 mAntiAliasing = antiAliasing;
212 if( mImpl->mRenderer )
214 Shader borderShader( GetBorderShader() );
215 mImpl->mRenderer.SetShader( borderShader );
218 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
224 Shader BorderVisual::GetBorderShader()
229 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
232 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
233 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
238 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
241 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
242 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
250 * Vertices and triangles of the border geometry:
252 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
265 Geometry BorderVisual::CreateBorderGeometry()
267 const float halfWidth = 0.5f;
268 const float halfHeight = 0.5f;
269 struct BorderVertex { Vector2 position; Vector2 drift;};
270 BorderVertex borderVertexData[16] =
272 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
273 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
274 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
275 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
277 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
278 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
279 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
280 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
282 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
283 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
284 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
285 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
287 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
288 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
289 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
290 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
293 Property::Map borderVertexFormat;
294 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
295 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
296 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
297 borderVertices.SetData( borderVertexData, 16 );
300 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};
302 // Create the geometry object
303 Geometry geometry = Geometry::New();
304 geometry.AddVertexBuffer( borderVertices );
305 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
306 geometry.SetType( Geometry::TRIANGLE_STRIP );
311 } // namespace Internal
313 } // namespace Toolkit