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