2 * Copyright (c) 2020 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/rendering/renderer-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");
48 const char* VERTEX_SHADER =
49 "INPUT mediump vec2 aPosition;\n"
50 "INPUT mediump vec2 aDrift;\n"
52 "uniform highp mat4 uMvpMatrix;\n"
53 "uniform highp vec3 uSize;\n"
54 "uniform mediump float borderSize;\n"
56 "//Visual size and offset\n"
57 "uniform mediump vec2 offset;\n"
58 "uniform highp vec2 size;\n"
59 "uniform mediump vec4 offsetSizeMode;\n"
60 "uniform mediump vec2 origin;\n"
61 "uniform mediump vec2 anchorPoint;\n"
63 "vec2 ComputeVertexPosition()\n"
65 " vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n"
66 " vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n"
67 " return (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy;\n"
72 " vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n"
73 " gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n"
76 const char* FRAGMENT_SHADER =
77 "uniform lowp vec4 uColor;\n"
78 "uniform lowp vec4 borderColor;\n"
79 "uniform lowp vec3 mixColor;\n"
83 " OUT_COLOR = vec4(mixColor, 1.0) * borderColor * uColor;\n"
86 const char* VERTEX_SHADER_ANTI_ALIASING =
87 "INPUT mediump vec2 aPosition;\n"
88 "INPUT mediump vec2 aDrift;\n"
89 "OUTPUT mediump float vAlpha;\n"
91 "uniform highp mat4 uMvpMatrix;\n"
92 "uniform highp vec3 uSize;\n"
93 "uniform mediump float borderSize;\n"
97 " vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n"
98 " gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n"
99 " vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);"
102 const char* FRAGMENT_SHADER_ANTI_ALIASING =
103 "INPUT mediump float vAlpha;\n"
105 "uniform lowp vec4 uColor;\n"
106 "uniform lowp vec4 borderColor;\n"
107 "uniform lowp vec3 mixColor;\n"
108 "uniform mediump float borderSize;\n"
112 " OUT_COLOR = vec4(mixColor, 1.0) * borderColor * uColor;\n"
113 " OUT_COLOR.a *= smoothstep(0.0, 1.5, vAlpha) * smoothstep( borderSize + 1.5, borderSize, vAlpha );\n"
117 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
119 BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
120 borderVisualPtr->SetProperties( properties );
121 return borderVisualPtr;
124 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
125 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::BORDER ),
126 mBorderColor( Color::TRANSPARENT ),
128 mBorderColorIndex( Property::INVALID_INDEX ),
129 mBorderSizeIndex( Property::INVALID_INDEX ),
130 mAntiAliasing( false )
134 BorderVisual::~BorderVisual()
138 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
140 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
142 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
143 if( keyValue.first.type == Property::Key::INDEX )
145 DoSetProperty( keyValue.first.indexKey, keyValue.second );
149 if( keyValue.first == COLOR_NAME )
151 DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second );
153 else if( keyValue.first == SIZE_NAME )
155 DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second );
157 else if( keyValue.first == ANTI_ALIASING )
159 DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second );
165 void BorderVisual::DoSetProperty( Dali::Property::Index index,
166 const Dali::Property::Value& value )
170 case Toolkit::BorderVisual::Property::COLOR:
172 if( !value.Get( mBorderColor ) )
174 DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n");
178 case Toolkit::BorderVisual::Property::SIZE:
180 if( !value.Get( mBorderSize ) )
182 DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n");
186 case Toolkit::BorderVisual::Property::ANTI_ALIASING:
188 if( !value.Get( mAntiAliasing ) )
190 DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n");
197 void BorderVisual::DoSetOnScene( Actor& actor )
199 InitializeRenderer();
201 mBorderColorIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
202 if( ( mBorderColor.a < 1.f || mAntiAliasing ) || IsAdvancedBlendEquationApplied() )
204 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
206 mBorderSizeIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
208 actor.AddRenderer( mImpl->mRenderer );
210 // Border Visual Generated and ready to display
211 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
214 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
217 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER );
218 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
219 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
220 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
223 void BorderVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
228 void BorderVisual::OnSetTransform()
230 if( mImpl->mRenderer )
232 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
236 void BorderVisual::InitializeRenderer()
238 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
241 geometry = CreateBorderGeometry();
242 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
245 Shader shader = GetBorderShader();
246 mImpl->mRenderer = Renderer::New( geometry, shader );
248 //Register transform properties
249 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
252 Shader BorderVisual::GetBorderShader()
257 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
260 shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER_ANTI_ALIASING, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_ANTI_ALIASING );
261 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
266 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
269 shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + 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 VertexBuffer borderVertices = VertexBuffer::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