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