Add GetVisualProperty to Control
[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/rendering/renderer-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/devel-api/visuals/color-visual-actions-devel.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46
47 const char* VERTEX_SHADER =
48   "INPUT mediump vec2 aPosition;\n"
49
50   "uniform highp mat4 uMvpMatrix;\n"
51   "uniform highp vec3 uSize;\n"
52
53   "//Visual size and offset\n"
54   "uniform mediump vec2 offset;\n"
55   "uniform highp vec2 size;\n"
56   "uniform mediump vec4 offsetSizeMode;\n"
57   "uniform mediump vec2 origin;\n"
58   "uniform mediump vec2 anchorPoint;\n"
59   "uniform mediump vec2 extraSize;\n"
60
61   "vec4 ComputeVertexPosition()\n"
62   "{\n"
63   "  vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n"
64   "  vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n"
65   "  return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n"
66   "}\n"
67
68   "void main()\n"
69   "{\n"
70   "  gl_Position = uMvpMatrix * ComputeVertexPosition();\n"
71   "}\n";
72
73
74 const char* FRAGMENT_SHADER =
75   "uniform lowp vec4 uColor;\n"
76   "uniform lowp vec3 mixColor;\n"
77
78   "void main()\n"
79   "{\n"
80   "  OUT_COLOR = vec4(mixColor, 1.0) * uColor;\n"
81   "}\n";
82
83 const char* VERTEX_SHADER_ROUNDED_CORNER =
84   "INPUT mediump vec2 aPosition;\n"
85   "OUTPUT mediump vec2 vPosition;\n"
86   "OUTPUT mediump vec2 vRectSize;\n"
87   "OUTPUT mediump float vCornerRadius;\n"
88
89   "uniform highp mat4 uMvpMatrix;\n"
90   "uniform highp vec3 uSize;\n"
91
92   "//Visual size and offset\n"
93   "uniform mediump vec2 offset;\n"
94   "uniform highp vec2 size;\n"
95   "uniform mediump vec2 extraSize;\n"
96   "uniform mediump vec4 offsetSizeMode;\n"
97   "uniform mediump vec2 origin;\n"
98   "uniform mediump vec2 anchorPoint;\n"
99   "uniform mediump float cornerRadius;\n"
100   "uniform mediump float cornerRadiusPolicy;\n"
101
102   "vec4 ComputeVertexPosition()\n"
103   "{\n"
104   "  vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n"
105   "  vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n"
106   "  mediump float minSize = min( visualSize.x, visualSize.y );\n"
107   "  vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n"
108   "  vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n"
109   "  vRectSize = visualSize / 2.0 - vCornerRadius;\n"
110   "  vPosition = aPosition* visualSize;\n"
111   "  return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n"
112   "}\n"
113
114   "void main()\n"
115   "{\n"
116   "  gl_Position = uMvpMatrix * ComputeVertexPosition();\n"
117   "}\n";
118
119 //float distance = length( max( abs( position - center ), size ) - size ) - radius;
120 const char* FRAGMENT_SHADER_ROUNDED_CORNER =
121   "INPUT mediump vec2 vPosition;\n"
122   "INPUT mediump vec2 vRectSize;\n"
123   "INPUT mediump float vCornerRadius;\n"
124
125   "uniform lowp vec4 uColor;\n"
126   "uniform lowp vec3 mixColor;\n"
127
128   "void main()\n"
129   "{\n"
130   "  mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n"
131   "  OUT_COLOR = vec4(mixColor, 1.0) * uColor;\n"
132   "  OUT_COLOR.a *= 1.0 - smoothstep( -1.0, 1.0, dist );\n"
133   "}\n";
134
135 const char* VERTEX_SHADER_BLUR_EDGE =
136   "INPUT mediump vec2 aPosition;\n"
137   "OUTPUT mediump vec2 vPosition;\n"
138   "OUTPUT mediump vec2 vRectSize;\n"
139
140   "uniform highp   mat4 uMvpMatrix;\n"
141   "uniform highp vec3 uSize;\n"
142
143   "//Visual size and offset\n"
144   "uniform mediump vec2 offset;\n"
145   "uniform highp vec2 size;\n"
146   "uniform mediump vec2 extraSize;\n"
147   "uniform mediump vec4 offsetSizeMode;\n"
148   "uniform mediump vec2 origin;\n"
149   "uniform mediump vec2 anchorPoint;\n"
150   "uniform mediump float blurRadius;\n"
151
152   "vec4 ComputeVertexPosition()\n"
153   "{\n"
154   "  vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize + blurRadius * 2.0;\n"
155   "  vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n"
156   "  vRectSize = visualSize / 2.0;\n"
157   "  vPosition = aPosition* visualSize;\n"
158   "  return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n"
159   "}\n"
160
161   "void main()\n"
162   "{\n"
163   "  gl_Position = uMvpMatrix * ComputeVertexPosition();\n"
164   "}\n";
165
166 const char* FRAGMENT_SHADER_BLUR_EDGE =
167   "INPUT mediump vec2 vPosition;\n"
168   "INPUT mediump vec2 vRectSize;\n"
169
170   "uniform lowp vec4 uColor;\n"
171   "uniform lowp vec3 mixColor;\n"
172   "uniform mediump float blurRadius;\n"
173
174   "void main()\n"
175   "{\n"
176   "  mediump vec2 blur = 1.0 - smoothstep( vRectSize - blurRadius * 2.0, vRectSize, abs( vPosition ) );\n"
177   "  OUT_COLOR = vec4(mixColor, 1.0) * uColor;\n"
178   "  OUT_COLOR.a *= blur.x * blur.y;\n"
179   "}\n";
180
181 }
182
183 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
184 {
185   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
186   colorVisualPtr->SetProperties( properties );
187   return colorVisualPtr;
188 }
189
190 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
191 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
192   mBlurRadius(0.0f),
193   mBlurRadiusIndex(Property::INVALID_INDEX),
194   mRenderIfTransparent(false),
195   mNeedBlurRadius(false)
196 {
197 }
198
199 ColorVisual::~ColorVisual()
200 {
201 }
202
203 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
204 {
205   // By virtue of DoSetProperties being called last, this will override
206   // anything set by Toolkit::Visual::Property::MIX_COLOR
207   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
208   if( colorValue )
209   {
210     Vector4 color;
211     if( colorValue->Get( color ) )
212     {
213       Property::Type type = colorValue->GetType();
214       if( type == Property::VECTOR4 )
215       {
216         SetMixColor( color );
217       }
218       else if( type == Property::VECTOR3 )
219       {
220         Vector3 color3(color);
221         SetMixColor( color3 );
222       }
223     }
224     else
225     {
226       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
227     }
228   }
229
230   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
231   if( renderIfTransparentValue )
232   {
233     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
234     {
235       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
236     }
237   }
238
239   Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
240   if( blurRadiusValue )
241   {
242     if( !blurRadiusValue->Get( mBlurRadius ) )
243     {
244       DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
245     }
246   }
247 }
248
249 void ColorVisual::DoSetOnScene( Actor& actor )
250 {
251   InitializeRenderer();
252
253   // Only add the renderer if it's not fully transparent
254   // We cannot avoid creating a renderer as it's used in the base class
255   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
256   {
257     actor.AddRenderer( mImpl->mRenderer );
258   }
259
260   // Color Visual generated and ready to display
261   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
262 }
263
264 void ColorVisual::DoSetOffScene(Actor& actor)
265 {
266   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
267   {
268     // Update values from Renderer
269     mBlurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
270   }
271
272   actor.RemoveRenderer(mImpl->mRenderer);
273   mImpl->mRenderer.Reset();
274   mBlurRadiusIndex = Property::INVALID_INDEX;
275 }
276
277 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
278 {
279   map.Clear();
280   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
281   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
282   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
283
284   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
285   {
286     // Update values from Renderer
287     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
288     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
289   }
290   else
291   {
292     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
293   }
294 }
295
296 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
297 {
298   // Do nothing
299 }
300
301 void ColorVisual::OnSetTransform()
302 {
303   if( mImpl->mRenderer )
304   {
305     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
306   }
307 }
308
309 void ColorVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
310 {
311   // Check if action is valid for this visual type and perform action if possible
312   switch( actionId )
313   {
314     case DevelColorVisual::Action::UPDATE_PROPERTY:
315     {
316       const Property::Map* map = attributes.GetMap();
317       if( map )
318       {
319         DoSetProperties( *map );
320       }
321       break;
322     }
323   }
324 }
325
326 void ColorVisual::UpdateShader()
327 {
328   if(mImpl->mRenderer)
329   {
330     Shader shader = GetShader();
331     mImpl->mRenderer.SetShader(shader);
332   }
333 }
334
335 void ColorVisual::InitializeRenderer()
336 {
337   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
338
339   Shader shader = GetShader();
340
341   mImpl->mRenderer = Renderer::New(geometry, shader);
342
343   // ColorVisual has it's own index key for mix color - use this instead
344   // of using the new base index to avoid changing existing applications
345   // String keys will get to this property.
346   mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
347
348   if(!EqualsZero(mBlurRadius))
349   {
350     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
351     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
352   }
353
354   // Register transform properties
355   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
356 }
357
358 Shader ColorVisual::GetShader()
359 {
360   Shader shader;
361   if(!EqualsZero(mBlurRadius) || mNeedBlurRadius)
362   {
363     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
364     if( !shader )
365     {
366       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER_BLUR_EDGE, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_BLUR_EDGE );
367       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
368     }
369   }
370   else if( !IsRoundedCornerRequired() )
371   {
372     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
373     if( !shader )
374     {
375       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER );
376       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
377     }
378   }
379   else
380   {
381     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
382     if( !shader )
383     {
384       shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER_ROUNDED_CORNER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_ROUNDED_CORNER );
385       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
386     }
387   }
388
389   return shader;
390 }
391
392 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
393 {
394   if(!mImpl->mRenderer)
395   {
396     Handle handle;
397     return Dali::Property(handle, Property::INVALID_INDEX);
398   }
399
400   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
401   {
402     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
403
404     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
405
406     mNeedBlurRadius = true;
407
408     // Change shader
409     UpdateShader();
410
411     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
412   }
413
414   Handle handle;
415   return Dali::Property(handle, Property::INVALID_INDEX);
416 }
417
418 } // namespace Internal
419
420 } // namespace Toolkit
421
422 } // namespace Dali