03412bf67e75077558d58bec05e73dd21030533e
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / border / border-visual.cpp
1 /*
2  * Copyright (c) 2018 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/public-api/visuals/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 POSITION_ATTRIBUTE_NAME("aPosition");
44 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
45 const char * const INDEX_NAME("indices");
46
47
48 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
49   attribute mediump vec2 aPosition;\n
50   attribute mediump vec2 aDrift;\n
51   uniform highp   mat4 uMvpMatrix;\n
52   uniform highp   vec3 uSize;\n
53   uniform mediump float borderSize;\n
54   \n
55
56   //Visual size and offset
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
62
63   vec2 ComputeVertexPosition()\n
64   {\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
68   }\n
69
70   void main()\n
71   {\n
72     vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n
73     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
74   }\n
75 );
76
77 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
78   uniform lowp vec4 uColor;\n
79   uniform lowp vec4 borderColor;\n
80   uniform lowp vec3 mixColor;\n
81   \n
82   void main()\n
83   {\n
84     gl_FragColor = vec4(mixColor, 1.0)*borderColor*uColor;\n
85   }\n
86 );
87
88 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
89   attribute mediump vec2 aPosition;\n
90   attribute mediump vec2 aDrift;\n
91   uniform highp   mat4 uMvpMatrix;\n
92   uniform highp   vec3 uSize;\n
93   uniform mediump float borderSize;\n
94   varying mediump float vAlpha;\n
95   \n
96   void main()\n
97   {\n
98     vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
99     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
100     vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
101   }\n
102 );
103
104 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
105   uniform lowp vec4 uColor;\n
106   uniform lowp vec4 borderColor;\n
107   uniform lowp vec3 mixColor;\n
108   uniform mediump float borderSize;\n
109   varying mediump float vAlpha;\n
110   \n
111   void main()\n
112   {\n
113     gl_FragColor = vec4(mixColor, 1.0)*borderColor*uColor;\n
114     gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
115   }\n
116 );
117 }
118
119 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
120 {
121   BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
122   borderVisualPtr->SetProperties( properties );
123   return borderVisualPtr;
124 }
125
126 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
127 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::BORDER ),
128   mBorderColor( Color::TRANSPARENT ),
129   mBorderSize( 0.f ),
130   mBorderColorIndex( Property::INVALID_INDEX ),
131   mBorderSizeIndex( Property::INVALID_INDEX ),
132   mAntiAliasing( false )
133 {
134 }
135
136 BorderVisual::~BorderVisual()
137 {
138 }
139
140 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
141 {
142   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
143   {
144     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
145     if( keyValue.first.type == Property::Key::INDEX )
146     {
147       DoSetProperty( keyValue.first.indexKey, keyValue.second );
148     }
149     else
150     {
151       if( keyValue.first == COLOR_NAME )
152       {
153         DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second );
154       }
155       else if( keyValue.first == SIZE_NAME )
156       {
157         DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second );
158       }
159       else if( keyValue.first == ANTI_ALIASING )
160       {
161         DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second );
162       }
163     }
164   }
165 }
166
167 void BorderVisual::DoSetProperty( Dali::Property::Index index,
168                                   const Dali::Property::Value& value )
169 {
170   switch( index )
171   {
172     case Toolkit::BorderVisual::Property::COLOR:
173     {
174       if( !value.Get( mBorderColor ) )
175       {
176         DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n");
177       }
178       break;
179     }
180     case Toolkit::BorderVisual::Property::SIZE:
181     {
182       if( !value.Get( mBorderSize ) )
183       {
184         DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n");
185       }
186       break;
187     }
188     case Toolkit::BorderVisual::Property::ANTI_ALIASING:
189     {
190       if( !value.Get( mAntiAliasing ) )
191       {
192         DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n");
193       }
194       break;
195     }
196   }
197 }
198
199 void BorderVisual::DoSetOnScene( Actor& actor )
200 {
201   InitializeRenderer();
202
203   mBorderColorIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
204   if( mBorderColor.a < 1.f || mAntiAliasing)
205   {
206     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
207   }
208   mBorderSizeIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
209
210   actor.AddRenderer( mImpl->mRenderer );
211
212   // Border Visual Generated and ready to display
213   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
214 }
215
216 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
217 {
218   map.Clear();
219   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER );
220   map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
221   map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
222   map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
223 }
224
225 void BorderVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
226 {
227   // Do nothing
228 }
229
230 void BorderVisual::OnSetTransform()
231 {
232   if( mImpl->mRenderer )
233   {
234     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
235   }
236 }
237
238 void BorderVisual::InitializeRenderer()
239 {
240   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
241   if( !geometry )
242   {
243     geometry =  CreateBorderGeometry();
244     mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
245   }
246
247   Shader shader = GetBorderShader();
248   mImpl->mRenderer = Renderer::New( geometry, shader  );
249
250   //Register transform properties
251   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
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   VertexBuffer borderVertices = VertexBuffer::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