Merge "(Vector) Fix memory leak" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-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 "color-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/color-visual-properties.h>
27 #include <dali-toolkit/public-api/visuals/visual-properties.h>
28 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45
46 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
47   attribute mediump vec2 aPosition;\n
48   uniform highp   mat4 uMvpMatrix;\n
49   uniform mediump vec3 uSize;\n
50   \n
51
52   //Visual size and offset
53   uniform mediump vec2 offset;\n
54   uniform mediump vec2 size;\n
55   uniform mediump vec4 offsetSizeMode;\n
56   uniform mediump vec2 origin;\n
57   uniform mediump vec2 anchorPoint;\n
58   uniform mediump vec2 extraSize;\n
59
60   vec4 ComputeVertexPosition()\n
61   {\n
62     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
63     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
64     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
65   }\n
66
67   void main()\n
68   {\n
69     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
70   }\n
71 );
72
73 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
74   uniform lowp vec4 uColor;\n
75   uniform lowp vec3 mixColor;\n
76   \n
77   void main()\n
78   {\n
79     gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
80   }\n
81 );
82
83 const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER(
84   attribute mediump vec2 aPosition;\n
85   uniform highp   mat4 uMvpMatrix;\n
86   uniform mediump vec3 uSize;\n
87   varying mediump vec2 vPosition;\n
88   varying mediump vec2 vRectSize;\n
89   \n
90   //Visual size and offset
91   uniform mediump vec2 offset;\n
92   uniform mediump vec2 size;\n
93   uniform mediump vec2 extraSize;\n
94   uniform mediump vec4 offsetSizeMode;\n
95   uniform mediump vec2 origin;\n
96   uniform mediump vec2 anchorPoint;\n
97   uniform mediump float cornerRadius;\n
98   \n
99   vec4 ComputeVertexPosition()\n
100   {\n
101     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
102     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
103     vRectSize = visualSize / 2.0 - cornerRadius;\n
104     vPosition = aPosition* visualSize;\n
105     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
106   }\n
107   \n
108   void main()\n
109   {\n
110     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
111   }\n
112 );
113
114 //float distance = length( max( abs( position - center ), size ) - size ) - radius;
115 const char* FRAGMENT_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER(
116   varying mediump vec2 vPosition;\n
117   varying mediump vec2 vRectSize;\n
118   uniform lowp vec4 uColor;\n
119   uniform lowp vec3 mixColor;\n
120   uniform mediump float cornerRadius;\n
121   \n
122   void main()\n
123   {\n
124       mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n
125       gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
126       gl_FragColor.a *= 1.0 - smoothstep( -1.0, 1.0, dist );\n
127   }\n
128 );
129
130 const char* VERTEX_SHADER_BLUR_EDGE = DALI_COMPOSE_SHADER(
131   attribute mediump vec2 aPosition;\n
132   uniform highp   mat4 uMvpMatrix;\n
133   uniform mediump vec3 uSize;\n
134   varying mediump vec2 vPosition;\n
135   varying mediump vec2 vRectSize;\n
136   \n
137   //Visual size and offset
138   uniform mediump vec2 offset;\n
139   uniform mediump vec2 size;\n
140   uniform mediump vec2 extraSize;\n
141   uniform mediump vec4 offsetSizeMode;\n
142   uniform mediump vec2 origin;\n
143   uniform mediump vec2 anchorPoint;\n
144   uniform mediump float blurRadius;\n
145   \n
146   vec4 ComputeVertexPosition()\n
147   {\n
148     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize + blurRadius * 2.0;\n
149     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
150     vRectSize = visualSize / 2.0;\n
151     vPosition = aPosition* visualSize;\n
152     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
153   }\n
154   \n
155   void main()\n
156   {\n
157     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
158   }\n
159 );
160
161 const char* FRAGMENT_SHADER_BLUR_EDGE = DALI_COMPOSE_SHADER(
162   varying mediump vec2 vPosition;\n
163   varying mediump vec2 vRectSize;\n
164   uniform lowp vec4 uColor;\n
165   uniform lowp vec3 mixColor;\n
166   uniform mediump float blurRadius;\n
167   \n
168   void main()\n
169   {\n
170       mediump vec2 blur = 1.0 - smoothstep( vRectSize - blurRadius * 2.0, vRectSize, abs( vPosition ) );\n
171       gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
172       gl_FragColor.a *= blur.x * blur.y;\n
173   }\n
174 );
175
176 }
177
178 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
179 {
180   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
181   colorVisualPtr->SetProperties( properties );
182   return colorVisualPtr;
183 }
184
185 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
186 : Visual::Base( factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR ),
187   mBlurRadius( 0.0f ),
188   mRenderIfTransparent( false )
189 {
190 }
191
192 ColorVisual::~ColorVisual()
193 {
194 }
195
196 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
197 {
198   // By virtue of DoSetProperties being called last, this will override
199   // anything set by Toolkit::Visual::Property::MIX_COLOR
200   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
201   if( colorValue )
202   {
203     Vector4 color;
204     if( colorValue->Get( color ) )
205     {
206       Property::Type type = colorValue->GetType();
207       if( type == Property::VECTOR4 )
208       {
209         SetMixColor( color );
210       }
211       else if( type == Property::VECTOR3 )
212       {
213         Vector3 color3(color);
214         SetMixColor( color3 );
215       }
216     }
217     else
218     {
219       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
220     }
221   }
222
223   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
224   if( renderIfTransparentValue )
225   {
226     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
227     {
228       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
229     }
230   }
231
232   Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
233   if( blurRadiusValue )
234   {
235     if( !blurRadiusValue->Get( mBlurRadius ) )
236     {
237       DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
238     }
239   }
240 }
241
242 void ColorVisual::DoSetOnStage( Actor& actor )
243 {
244   InitializeRenderer();
245
246   // Only add the renderer if it's not fully transparent
247   // We cannot avoid creating a renderer as it's used in the base class
248   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
249   {
250     actor.AddRenderer( mImpl->mRenderer );
251   }
252
253   // Color Visual generated and ready to display
254   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
255 }
256
257 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
258 {
259   map.Clear();
260   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
261   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
262   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
263   map.Insert( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius );
264 }
265
266 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
267 {
268   // Do nothing
269 }
270
271
272 void ColorVisual::OnSetTransform()
273 {
274   if( mImpl->mRenderer )
275   {
276     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
277   }
278 }
279
280 void ColorVisual::InitializeRenderer()
281 {
282   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
283
284   Shader shader;
285   if( !EqualsZero( mBlurRadius ) )
286   {
287     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
288     if( !shader )
289     {
290       shader = Shader::New( VERTEX_SHADER_BLUR_EDGE, FRAGMENT_SHADER_BLUR_EDGE );
291       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
292     }
293   }
294   else if( !IsRoundedCornerRequired() )
295   {
296     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
297     if( !shader )
298     {
299       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
300       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
301     }
302   }
303   else
304   {
305     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
306     if( !shader )
307     {
308       shader = Shader::New( VERTEX_SHADER_ROUNDED_CORNER, FRAGMENT_SHADER_ROUNDED_CORNER );
309       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
310     }
311   }
312
313   mImpl->mRenderer = Renderer::New( geometry, shader );
314
315   // ColorVisual has it's own index key for mix color - use this instead
316   // of using the new base index to avoid changing existing applications
317   // String keys will get to this property.
318   mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
319
320   mImpl->mRenderer.RegisterProperty( BLUR_RADIUS_NAME, mBlurRadius );
321
322   if( mImpl->mMixColor.a < 1.f || !EqualsZero( mBlurRadius ) )
323   {
324     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
325   }
326
327   // Register transform properties
328   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
329 }
330
331 } // namespace Internal
332
333 } // namespace Toolkit
334
335 } // namespace Dali