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