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