2 * Copyright (c) 2018 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/public-api/visuals/visual-properties.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 POSITION_ATTRIBUTE_NAME("aPosition");
45 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
46 const char * const INDEX_NAME("indices");
49 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
50 attribute mediump vec2 aPosition;\n
51 attribute mediump vec2 aDrift;\n
52 uniform highp mat4 uMvpMatrix;\n
53 uniform mediump vec3 uSize;\n
54 uniform mediump float borderSize;\n
57 //Visual size and offset
58 uniform mediump vec2 offset;\n
59 uniform mediump vec2 size;\n
60 uniform mediump vec4 offsetSizeMode;\n
61 uniform mediump vec2 origin;\n
62 uniform mediump vec2 anchorPoint;\n
64 vec2 ComputeVertexPosition()\n
66 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
67 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
68 return (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy;\n
73 vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n
74 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
78 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
79 uniform lowp vec4 uColor;\n
80 uniform lowp vec4 borderColor;\n
81 uniform lowp vec3 mixColor;\n
85 gl_FragColor = vec4(mixColor, 1.0)*borderColor*uColor;\n
89 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
90 attribute mediump vec2 aPosition;\n
91 attribute mediump vec2 aDrift;\n
92 uniform highp mat4 uMvpMatrix;\n
93 uniform mediump vec3 uSize;\n
94 uniform mediump float borderSize;\n
95 varying mediump float vAlpha;\n
99 vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
100 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
101 vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
105 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
106 uniform lowp vec4 uColor;\n
107 uniform lowp vec4 borderColor;\n
108 uniform lowp vec3 mixColor;\n
109 uniform mediump float borderSize;\n
110 varying mediump float vAlpha;\n
114 gl_FragColor = vec4(mixColor, 1.0)*borderColor*uColor;\n
115 gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
120 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
122 BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
123 borderVisualPtr->SetProperties( properties );
124 return borderVisualPtr;
127 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
128 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::BORDER ),
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 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
145 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
146 if( keyValue.first.type == Property::Key::INDEX )
148 DoSetProperty( keyValue.first.indexKey, keyValue.second );
152 if( keyValue.first == COLOR_NAME )
154 DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second );
156 else if( keyValue.first == SIZE_NAME )
158 DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second );
160 else if( keyValue.first == ANTI_ALIASING )
162 DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second );
168 void BorderVisual::DoSetProperty( Dali::Property::Index index,
169 const Dali::Property::Value& value )
173 case Toolkit::BorderVisual::Property::COLOR:
175 if( !value.Get( mBorderColor ) )
177 DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n");
181 case Toolkit::BorderVisual::Property::SIZE:
183 if( !value.Get( mBorderSize ) )
185 DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n");
189 case Toolkit::BorderVisual::Property::ANTI_ALIASING:
191 if( !value.Get( mAntiAliasing ) )
193 DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n");
200 void BorderVisual::DoSetOnScene( Actor& actor )
202 InitializeRenderer();
204 mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
205 if( mBorderColor.a < 1.f || mAntiAliasing)
207 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
209 mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
211 actor.AddRenderer( mImpl->mRenderer );
213 // Border Visual Generated and ready to display
214 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
217 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
220 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER );
221 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
222 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
223 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
226 void BorderVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
231 void BorderVisual::OnSetTransform()
233 if( mImpl->mRenderer )
235 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
239 void BorderVisual::InitializeRenderer()
241 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
244 geometry = CreateBorderGeometry();
245 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
248 Shader shader = GetBorderShader();
249 mImpl->mRenderer = Renderer::New( geometry, shader );
251 //Register transform properties
252 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
255 Shader BorderVisual::GetBorderShader()
260 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
263 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
264 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
269 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
272 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
273 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
281 * Vertices and triangles of the border geometry:
283 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
296 Geometry BorderVisual::CreateBorderGeometry()
298 const float halfWidth = 0.5f;
299 const float halfHeight = 0.5f;
300 struct BorderVertex { Vector2 position; Vector2 drift;};
301 BorderVertex borderVertexData[16] =
303 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
304 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
305 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
306 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
308 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
309 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
310 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
311 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
313 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
314 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
315 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
316 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
318 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
319 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
320 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
321 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
324 Property::Map borderVertexFormat;
325 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
326 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
327 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
328 borderVertices.SetData( borderVertexData, 16 );
331 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};
333 // Create the geometry object
334 Geometry geometry = Geometry::New();
335 geometry.AddVertexBuffer( borderVertices );
336 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
337 geometry.SetType( Geometry::TRIANGLE_STRIP );
342 } // namespace Internal
344 } // namespace Toolkit