Merge "(Vector) Fix memory leak" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / arc / arc-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 <dali-toolkit/internal/visuals/arc/arc-visual.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.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
43 // cap
44 DALI_ENUM_TO_STRING_TABLE_BEGIN( CAP )
45 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, BUTT )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, ROUND )
47 DALI_ENUM_TO_STRING_TABLE_END( CAP )
48
49 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
50   attribute mediump vec2 aPosition;\n
51   uniform highp   mat4 uMvpMatrix;\n
52   uniform mediump vec3 uSize;\n
53   \n
54   varying mediump vec2 vPosition;\n
55   \n
56   //Visual size and offset
57   uniform mediump vec2 offset;\n
58   uniform mediump vec2 size;\n
59   uniform mediump vec4 offsetSizeMode;\n
60   uniform mediump vec2 origin;\n
61   uniform mediump vec2 anchorPoint;\n
62
63   vec4 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     vPosition = aPosition* visualSize;\n
68     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
69   }\n
70
71   void main()\n
72   {\n
73     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
74   }\n
75 );
76
77 const char* FRAGMENT_SHADER_BUTT_CAP = DALI_COMPOSE_SHADER(
78   varying mediump vec2 vPosition;\n
79   uniform lowp vec4 uColor;\n
80   uniform lowp vec3 mixColor;\n
81   uniform mediump float thickness;\n
82   uniform mediump float radius;\n
83   uniform mediump float startAngle;\n
84   uniform mediump float sweepAngle;\n
85   \n
86   const mediump float M_PI_OVER_2 = 1.57079632679;\n
87   const mediump float M_PI = 3.14159265359;\n
88   const mediump float M_PI_2 = 6.28318530718;\n
89   \n
90   mediump float GetOpacity()\n
91   {\n
92       mediump float start = radians( mod( startAngle, 360.0 ) );\n
93       mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
94       mediump float dist = length( vPosition );\n
95       if( angle <= radians( sweepAngle ) )\n
96       {\n
97         return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
98       }\n
99       mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
100       mediump vec2 q0 = vec2( dist * cos( start - M_PI_OVER_2 ), dist * sin( start - M_PI_OVER_2 ) );\n
101       mediump vec2 q1 = vec2( dist * cos( end - M_PI_OVER_2 ), dist * sin( end - M_PI_OVER_2 ) );\n
102       mediump float opacity = 1.0 - smoothstep( 0.0, 2.0, min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
103       opacity *= step( 0.0, thickness / 2.0 - abs( dist - radius ) );\n
104       return opacity;\n
105   }\n
106   void main()\n
107   {\n
108     gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
109     gl_FragColor.a *= GetOpacity();\n
110   }\n
111 );
112
113 const char* FRAGMENT_SHADER_ROUND_CAP = DALI_COMPOSE_SHADER(
114   varying mediump vec2 vPosition;\n
115   uniform lowp vec4 uColor;\n
116   uniform lowp vec3 mixColor;\n
117   uniform mediump float thickness;\n
118   uniform mediump float radius;\n
119   uniform mediump float startAngle;\n
120   uniform mediump float sweepAngle;\n
121   \n
122   const mediump float M_PI_OVER_2 = 1.57079632679;\n
123   const mediump float M_PI_2 = 6.28318530718;\n
124   \n
125   mediump float GetOpacity()\n
126   {\n
127       mediump float start = radians( mod( startAngle, 360.0 ) );\n
128       mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
129       mediump float dist = length( vPosition );\n
130       if( angle <= radians( sweepAngle ) )\n
131       {\n
132         return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
133       }\n
134       mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
135       mediump vec2 q0 = vec2( radius * cos( start - M_PI_OVER_2 ), radius * sin( start - M_PI_OVER_2 ) );\n
136       mediump vec2 q1 = vec2( radius * cos( end - M_PI_OVER_2 ), radius * sin( end - M_PI_OVER_2 ) );\n
137       return smoothstep( -1.0, 1.0, thickness / 2.0 - min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
138   }\n
139   void main()\n
140   {\n
141     gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
142     gl_FragColor.a *= GetOpacity();\n
143   }\n
144 );
145
146 }
147
148 ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
149 {
150   ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) );
151   arcVisualPtr->SetProperties( properties );
152   return arcVisualPtr;
153 }
154
155 ArcVisual::ArcVisual( VisualFactoryCache& factoryCache )
156 : Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>( Toolkit::DevelVisual::ARC ) ),
157   mThickness( 0.0f ),
158   mRadius( 0.0f ),
159   mStartAngle( 0.0f ),
160   mSweepAngle( 360.0f ),
161   mRadiusIndex( Property::INVALID_INDEX ),
162   mCapType( DevelArcVisual::Cap::BUTT )
163 {
164 }
165
166 ArcVisual::~ArcVisual()
167 {
168 }
169
170 void ArcVisual::DoSetProperties( const Property::Map& propertyMap )
171 {
172   Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME );
173   if( thicknessValue )
174   {
175     if( !thicknessValue->Get( mThickness ) )
176     {
177       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() );
178     }
179   }
180
181   Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME );
182   if( startAngleValue )
183   {
184     if( !startAngleValue->Get( mStartAngle ) )
185     {
186       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() );
187     }
188   }
189
190   Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME );
191   if( sweepAngleValue )
192   {
193     if( !sweepAngleValue->Get( mSweepAngle ) )
194     {
195       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() );
196     }
197   }
198
199   Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME );
200   if( capValue )
201   {
202     int capType = 0;
203     Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType );
204     mCapType = Toolkit::DevelArcVisual::Cap::Type( capType );
205   }
206 }
207
208 void ArcVisual::DoSetOnStage( Actor& actor )
209 {
210   InitializeRenderer();
211
212   actor.AddRenderer( mImpl->mRenderer );
213
214   // Arc Visual generated and ready to display
215   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
216 }
217
218 void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const
219 {
220   map.Clear();
221   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC );
222   map.Insert( Toolkit::DevelArcVisual::Property::THICKNESS, mThickness );
223   map.Insert( Toolkit::DevelArcVisual::Property::START_ANGLE, mStartAngle );
224   map.Insert( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, mSweepAngle );
225   map.Insert( Toolkit::DevelArcVisual::Property::CAP, mCapType );
226 }
227
228 void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
229 {
230   // Do nothing
231 }
232
233 void ArcVisual::OnSetTransform()
234 {
235   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
236   mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f;
237
238   if( mImpl->mRenderer )
239   {
240     mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius );
241   }
242 }
243
244 void ArcVisual::InitializeRenderer()
245 {
246   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
247
248   Shader shader;
249   if( mCapType == DevelArcVisual::Cap::BUTT )
250   {
251     shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER );
252     if( !shader )
253     {
254       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_BUTT_CAP );
255       mFactoryCache.SaveShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader );
256     }
257   }
258   else
259   {
260     shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER );
261     if( !shader )
262     {
263       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ROUND_CAP );
264       mFactoryCache.SaveShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader );
265     }
266   }
267
268   mImpl->mRenderer = Renderer::New( geometry, shader );
269
270   mImpl->mRenderer.RegisterProperty( THICKNESS_NAME, mThickness );
271   mImpl->mRenderer.RegisterProperty( START_ANGLE_NAME, mStartAngle );
272   mImpl->mRenderer.RegisterProperty( SWEEP_ANGLE_NAME, mSweepAngle );
273   mImpl->mRenderer.RegisterProperty( CAP_NAME, 0.0f );
274
275   mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius );
276
277   mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
278
279   // Register transform properties
280   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
281 }
282
283 } // namespace Internal
284
285 } // namespace Toolkit
286
287 } // namespace Dali