3cb168850bd436b8412353afca9c295238782b03
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / border / border-visual.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "border-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
26 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
27 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
28 #include <dali-toolkit/internal/visuals/visual-data-impl.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 namespace
40 {
41 const char * const COLOR_NAME("borderColor");
42 const char * const SIZE_NAME("borderSize");
43 const char * const ANTI_ALIASING("antiAliasing");
44
45 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
46 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
47 const char * const INDEX_NAME("indices");
48
49
50 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
51   attribute mediump vec2 aPosition;\n
52   attribute mediump vec2 aDrift;\n
53   uniform mediump mat4 uMvpMatrix;\n
54   uniform mediump vec3 uSize;\n
55   uniform mediump float borderSize;\n
56   \n
57   void main()\n
58   {\n
59     vec2 position = aPosition*uSize.xy + aDrift*borderSize;\n
60     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
61   }\n
62 );
63
64 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
65   uniform lowp vec4 uColor;\n
66   uniform lowp vec4 borderColor;\n
67   \n
68   void main()\n
69   {\n
70     gl_FragColor = borderColor*uColor;\n
71   }\n
72 );
73
74 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
75   attribute mediump vec2 aPosition;\n
76   attribute mediump vec2 aDrift;\n
77   uniform mediump mat4 uMvpMatrix;\n
78   uniform mediump vec3 uSize;\n
79   uniform mediump float borderSize;\n
80   varying mediump float vAlpha;\n
81   \n
82   void main()\n
83   {\n
84     vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
85     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
86     vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
87   }\n
88 );
89
90 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
91   uniform lowp vec4 uColor;\n
92   uniform lowp vec4 borderColor;\n
93   uniform mediump float borderSize;\n
94   varying mediump float vAlpha;\n
95   \n
96   void main()\n
97   {\n
98     gl_FragColor = borderColor*uColor;\n
99     gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
100   }\n
101 );
102 }
103
104 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
105 : Visual( factoryCache ),
106   mBorderColor( Color::TRANSPARENT ),
107   mBorderSize( 0.f ),
108   mBorderColorIndex( Property::INVALID_INDEX ),
109   mBorderSizeIndex( Property::INVALID_INDEX ),
110   mAntiAliasing( false )
111 {
112 }
113
114 BorderVisual::~BorderVisual()
115 {
116 }
117
118 void BorderVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
119 {
120   Property::Value* color = propertyMap.Find( COLOR_NAME );
121   if( !( color && color->Get(mBorderColor) ) )
122   {
123     DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object" );
124   }
125
126   Property::Value* size = propertyMap.Find( SIZE_NAME );
127   if( !( size && size->Get(mBorderSize) ) )
128   {
129     DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object" );
130   }
131
132   Property::Value* antiAliasing = propertyMap.Find( ANTI_ALIASING );
133   if( antiAliasing )
134   {
135     antiAliasing->Get( mAntiAliasing );
136   }
137 }
138
139 void BorderVisual::SetClipRect( const Rect<int>& clipRect )
140 {
141   Visual::SetClipRect( clipRect );
142
143   //ToDo: renderer responds to the clipRect change
144 }
145
146 void BorderVisual::DoSetOnStage( Actor& actor )
147 {
148   InitializeRenderer();
149
150   mBorderColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_NAME, mBorderColor );
151   if( mBorderColor.a < 1.f || mAntiAliasing)
152   {
153     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
154   }
155   mBorderSizeIndex = (mImpl->mRenderer).RegisterProperty( SIZE_NAME, mBorderSize );
156 }
157
158 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
159 {
160   map.Clear();
161   map.Insert( RENDERER_TYPE, BORDER_RENDERER );
162   map.Insert( COLOR_NAME, mBorderColor );
163   map.Insert( SIZE_NAME, mBorderSize );
164 }
165
166 void BorderVisual::InitializeRenderer()
167 {
168   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
169   if( !geometry )
170   {
171     geometry =  CreateBorderGeometry();
172     mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
173   }
174
175
176   Shader shader = GetBorderShader();
177   mImpl->mRenderer = Renderer::New( geometry, shader  );
178
179 }
180
181 void BorderVisual::SetBorderColor(const Vector4& color)
182 {
183   mBorderColor = color;
184
185   if( mImpl->mRenderer )
186   {
187     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
188     if( color.a < 1.f )
189     {
190       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
191     }
192   }
193 }
194
195 void BorderVisual::SetBorderSize( float size )
196 {
197   mBorderSize = size;
198
199   if( mImpl->mRenderer )
200   {
201     (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
202   }
203 }
204
205 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
206 {
207   if( mAntiAliasing != antiAliasing )
208   {
209     mAntiAliasing = antiAliasing;
210     if( mImpl->mRenderer )
211     {
212       Shader borderShader( GetBorderShader() );
213       mImpl->mRenderer.SetShader( borderShader );
214       if( mAntiAliasing )
215       {
216         mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
217       }
218     }
219   }
220 }
221
222 Shader BorderVisual::GetBorderShader()
223 {
224   Shader shader;
225   if( mAntiAliasing )
226   {
227     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
228     if( !shader )
229     {
230       shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
231       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
232     }
233   }
234   else
235   {
236     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
237     if( !shader )
238     {
239       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
240       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
241     }
242   }
243
244   return shader;
245 }
246
247 /**
248  * Vertices and triangles of the border geometry:
249  *
250  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
251  *
252  * 0--1--2--3
253  * |\ | /| /|
254  * | \|/ |/ |
255  * 4--5--6--7
256  * |\ |  |\ |
257  * | \|  | \|
258  * 8--9--10-11
259  * | /| /|\ |
260  * |/ |/ | \|
261  * 12-13-14-15
262  */
263 Geometry BorderVisual::CreateBorderGeometry()
264 {
265   const float halfWidth = 0.5f;
266   const float halfHeight = 0.5f;
267   struct BorderVertex { Vector2 position; Vector2 drift;};
268   BorderVertex borderVertexData[16] =
269   {
270       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
271       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
272       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
273       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
274
275       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
276       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
277       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
278       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
279
280       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
281       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
282       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
283       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
284
285       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
286       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
287       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
288       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
289   };
290
291   Property::Map borderVertexFormat;
292   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
293   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
294   PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
295   borderVertices.SetData( borderVertexData, 16 );
296
297   // Create indices
298   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};
299
300   // Create the geometry object
301   Geometry geometry = Geometry::New();
302   geometry.AddVertexBuffer( borderVertices );
303   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
304   geometry.SetGeometryType( Geometry::TRIANGLE_STRIP );
305
306   return geometry;
307 }
308
309 } // namespace Internal
310
311 } // namespace Toolkit
312
313 } // namespace Dali