Merge "Added TextController Relayouter class to handle relayouting" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / border / border-visual.cpp
1 /*
2  * Copyright (c) 2020 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/rendering/renderer-devel.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
27 #include <dali-toolkit/public-api/visuals/visual-properties.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 POSITION_ATTRIBUTE_NAME("aPosition");
45 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
46 const char * const INDEX_NAME("indices");
47
48 const char* VERTEX_SHADER =
49   "INPUT mediump vec2 aPosition;\n"
50   "INPUT mediump vec2 aDrift;\n"
51
52   "uniform highp mat4 uMvpMatrix;\n"
53   "uniform highp vec3 uSize;\n"
54   "uniform mediump float borderSize;\n"
55
56   "//Visual size and offset\n"
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 const char* FRAGMENT_SHADER =
77   "uniform lowp vec4 uColor;\n"
78   "uniform lowp vec4 borderColor;\n"
79   "uniform lowp vec3 mixColor;\n"
80
81   "void main()\n"
82   "{\n"
83   "  OUT_COLOR = vec4(mixColor, 1.0) * borderColor * uColor;\n"
84   "}\n";
85
86 const char* VERTEX_SHADER_ANTI_ALIASING =
87   "INPUT mediump vec2 aPosition;\n"
88   "INPUT mediump vec2 aDrift;\n"
89   "OUTPUT mediump float vAlpha;\n"
90
91   "uniform highp mat4 uMvpMatrix;\n"
92   "uniform highp vec3 uSize;\n"
93   "uniform mediump float borderSize;\n"
94
95   "void main()\n"
96   "{\n"
97   "  vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n"
98   "  gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n"
99   "  vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);"
100   "}\n";
101
102 const char* FRAGMENT_SHADER_ANTI_ALIASING =
103   "INPUT mediump float vAlpha;\n"
104
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
110   "void main()\n"
111   "{\n"
112   "  OUT_COLOR = vec4(mixColor, 1.0) * borderColor * uColor;\n"
113   "  OUT_COLOR.a *= smoothstep(0.0, 1.5, vAlpha) * smoothstep( borderSize + 1.5, borderSize, vAlpha );\n"
114   "}\n";
115 }
116
117 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
118 {
119   BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
120   borderVisualPtr->SetProperties( properties );
121   return borderVisualPtr;
122 }
123
124 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
125 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::BORDER ),
126   mBorderColor( Color::TRANSPARENT ),
127   mBorderSize( 0.f ),
128   mBorderColorIndex( Property::INVALID_INDEX ),
129   mBorderSizeIndex( Property::INVALID_INDEX ),
130   mAntiAliasing( false )
131 {
132 }
133
134 BorderVisual::~BorderVisual()
135 {
136 }
137
138 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
139 {
140   for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
141   {
142     KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
143     if( keyValue.first.type == Property::Key::INDEX )
144     {
145       DoSetProperty( keyValue.first.indexKey, keyValue.second );
146     }
147     else
148     {
149       if( keyValue.first == COLOR_NAME )
150       {
151         DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second );
152       }
153       else if( keyValue.first == SIZE_NAME )
154       {
155         DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second );
156       }
157       else if( keyValue.first == ANTI_ALIASING )
158       {
159         DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second );
160       }
161     }
162   }
163 }
164
165 void BorderVisual::DoSetProperty( Dali::Property::Index index,
166                                   const Dali::Property::Value& value )
167 {
168   switch( index )
169   {
170     case Toolkit::BorderVisual::Property::COLOR:
171     {
172       if( !value.Get( mBorderColor ) )
173       {
174         DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n");
175       }
176       break;
177     }
178     case Toolkit::BorderVisual::Property::SIZE:
179     {
180       if( !value.Get( mBorderSize ) )
181       {
182         DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n");
183       }
184       break;
185     }
186     case Toolkit::BorderVisual::Property::ANTI_ALIASING:
187     {
188       if( !value.Get( mAntiAliasing ) )
189       {
190         DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n");
191       }
192       break;
193     }
194   }
195 }
196
197 void BorderVisual::DoSetOnScene( Actor& actor )
198 {
199   InitializeRenderer();
200
201   mBorderColorIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
202   if( mBorderColor.a < 1.f || mAntiAliasing )
203   {
204     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
205   }
206   mBorderSizeIndex = mImpl->mRenderer.RegisterProperty( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
207
208   actor.AddRenderer( mImpl->mRenderer );
209
210   // Border Visual Generated and ready to display
211   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
212 }
213
214 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
215 {
216   map.Clear();
217   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::BORDER );
218   map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
219   map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
220   map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
221 }
222
223 void BorderVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
224 {
225   // Do nothing
226 }
227
228 void BorderVisual::OnSetTransform()
229 {
230   if( mImpl->mRenderer )
231   {
232     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
233   }
234 }
235
236 void BorderVisual::InitializeRenderer()
237 {
238   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
239   if( !geometry )
240   {
241     geometry =  CreateBorderGeometry();
242     mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
243   }
244
245   Shader shader = GetBorderShader();
246   mImpl->mRenderer = Renderer::New( geometry, shader  );
247
248   //Register transform properties
249   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
250 }
251
252 Shader BorderVisual::GetBorderShader()
253 {
254   Shader shader;
255   if( mAntiAliasing )
256   {
257     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
258     if( !shader )
259     {
260       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER_ANTI_ALIASING, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_ANTI_ALIASING );
261       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
262     }
263   }
264   else
265   {
266     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
267     if( !shader )
268     {
269       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER );
270       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
271     }
272   }
273
274   return shader;
275 }
276
277 /**
278  * Vertices and triangles of the border geometry:
279  *
280  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
281  *
282  * 0--1--2--3
283  * |\ | /| /|
284  * | \|/ |/ |
285  * 4--5--6--7
286  * |\ |  |\ |
287  * | \|  | \|
288  * 8--9--10-11
289  * | /| /|\ |
290  * |/ |/ | \|
291  * 12-13-14-15
292  */
293 Geometry BorderVisual::CreateBorderGeometry()
294 {
295   const float halfWidth = 0.5f;
296   const float halfHeight = 0.5f;
297   struct BorderVertex { Vector2 position; Vector2 drift;};
298   BorderVertex borderVertexData[16] =
299   {
300       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
301       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
302       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
303       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
304
305       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
306       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
307       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
308       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
309
310       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
311       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
312       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
313       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
314
315       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
316       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
317       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
318       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
319   };
320
321   Property::Map borderVertexFormat;
322   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
323   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
324   VertexBuffer borderVertices = VertexBuffer::New( borderVertexFormat );
325   borderVertices.SetData( borderVertexData, 16 );
326
327   // Create indices
328   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};
329
330   // Create the geometry object
331   Geometry geometry = Geometry::New();
332   geometry.AddVertexBuffer( borderVertices );
333   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
334   geometry.SetType( Geometry::TRIANGLE_STRIP );
335
336   return geometry;
337 }
338
339 } // namespace Internal
340
341 } // namespace Toolkit
342
343 } // namespace Dali