74ad4d2427d87e9057bff0aa5afeb35a47b52108
[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   varying mediump float vCornerRadius;\n
90   \n
91   //Visual size and offset
92   uniform mediump vec2 offset;\n
93   uniform mediump vec2 size;\n
94   uniform mediump vec2 extraSize;\n
95   uniform mediump vec4 offsetSizeMode;\n
96   uniform mediump vec2 origin;\n
97   uniform mediump vec2 anchorPoint;\n
98   uniform mediump float cornerRadius;\n
99   uniform mediump float cornerRadiusPolicy;\n
100   \n
101   vec4 ComputeVertexPosition()\n
102   {\n
103     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n
104     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
105     mediump float minSize = min( visualSize.x, visualSize.y );\n
106     vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n
107     vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n
108     vRectSize = visualSize / 2.0 - vCornerRadius;\n
109     vPosition = aPosition* visualSize;\n
110     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
111   }\n
112   \n
113   void main()\n
114   {\n
115     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
116   }\n
117 );
118
119 //float distance = length( max( abs( position - center ), size ) - size ) - radius;
120 const char* FRAGMENT_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER(
121   varying mediump vec2 vPosition;\n
122   varying mediump vec2 vRectSize;\n
123   varying mediump float vCornerRadius;\n
124   uniform lowp vec4 uColor;\n
125   uniform lowp vec3 mixColor;\n
126   \n
127   void main()\n
128   {\n
129       mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n
130       gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
131       gl_FragColor.a *= 1.0 - smoothstep( -1.0, 1.0, dist );\n
132   }\n
133 );
134
135 const char* VERTEX_SHADER_BLUR_EDGE = DALI_COMPOSE_SHADER(
136   attribute mediump vec2 aPosition;\n
137   uniform highp   mat4 uMvpMatrix;\n
138   uniform mediump vec3 uSize;\n
139   varying mediump vec2 vPosition;\n
140   varying mediump vec2 vRectSize;\n
141   \n
142   //Visual size and offset
143   uniform mediump vec2 offset;\n
144   uniform mediump vec2 size;\n
145   uniform mediump vec2 extraSize;\n
146   uniform mediump vec4 offsetSizeMode;\n
147   uniform mediump vec2 origin;\n
148   uniform mediump vec2 anchorPoint;\n
149   uniform mediump float blurRadius;\n
150   \n
151   vec4 ComputeVertexPosition()\n
152   {\n
153     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize + blurRadius * 2.0;\n
154     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
155     vRectSize = visualSize / 2.0;\n
156     vPosition = aPosition* visualSize;\n
157     return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
158   }\n
159   \n
160   void main()\n
161   {\n
162     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
163   }\n
164 );
165
166 const char* FRAGMENT_SHADER_BLUR_EDGE = DALI_COMPOSE_SHADER(
167   varying mediump vec2 vPosition;\n
168   varying mediump vec2 vRectSize;\n
169   uniform lowp vec4 uColor;\n
170   uniform lowp vec3 mixColor;\n
171   uniform mediump float blurRadius;\n
172   \n
173   void main()\n
174   {\n
175       mediump vec2 blur = 1.0 - smoothstep( vRectSize - blurRadius * 2.0, vRectSize, abs( vPosition ) );\n
176       gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
177       gl_FragColor.a *= blur.x * blur.y;\n
178   }\n
179 );
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   mRenderIfTransparent( false )
194 {
195 }
196
197 ColorVisual::~ColorVisual()
198 {
199 }
200
201 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
202 {
203   // By virtue of DoSetProperties being called last, this will override
204   // anything set by Toolkit::Visual::Property::MIX_COLOR
205   Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
206   if( colorValue )
207   {
208     Vector4 color;
209     if( colorValue->Get( color ) )
210     {
211       Property::Type type = colorValue->GetType();
212       if( type == Property::VECTOR4 )
213       {
214         SetMixColor( color );
215       }
216       else if( type == Property::VECTOR3 )
217       {
218         Vector3 color3(color);
219         SetMixColor( color3 );
220       }
221     }
222     else
223     {
224       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
225     }
226   }
227
228   Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
229   if( renderIfTransparentValue )
230   {
231     if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
232     {
233       DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
234     }
235   }
236
237   Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
238   if( blurRadiusValue )
239   {
240     if( !blurRadiusValue->Get( mBlurRadius ) )
241     {
242       DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
243     }
244   }
245 }
246
247 void ColorVisual::DoSetOnStage( Actor& actor )
248 {
249   InitializeRenderer();
250
251   // Only add the renderer if it's not fully transparent
252   // We cannot avoid creating a renderer as it's used in the base class
253   if( mRenderIfTransparent || mImpl->mMixColor.a > 0.0f )
254   {
255     actor.AddRenderer( mImpl->mRenderer );
256   }
257
258   // Color Visual generated and ready to display
259   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
260 }
261
262 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
263 {
264   map.Clear();
265   map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
266   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
267   map.Insert( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, mRenderIfTransparent );
268   map.Insert( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius );
269 }
270
271 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
272 {
273   // Do nothing
274 }
275
276
277 void ColorVisual::OnSetTransform()
278 {
279   if( mImpl->mRenderer )
280   {
281     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
282   }
283 }
284
285 void ColorVisual::InitializeRenderer()
286 {
287   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
288
289   Shader shader;
290   if( !EqualsZero( mBlurRadius ) )
291   {
292     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
293     if( !shader )
294     {
295       shader = Shader::New( VERTEX_SHADER_BLUR_EDGE, FRAGMENT_SHADER_BLUR_EDGE );
296       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
297     }
298   }
299   else if( !IsRoundedCornerRequired() )
300   {
301     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
302     if( !shader )
303     {
304       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
305       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
306     }
307   }
308   else
309   {
310     shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
311     if( !shader )
312     {
313       shader = Shader::New( VERTEX_SHADER_ROUNDED_CORNER, FRAGMENT_SHADER_ROUNDED_CORNER );
314       mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
315     }
316   }
317
318   mImpl->mRenderer = Renderer::New( geometry, shader );
319
320   // ColorVisual has it's own index key for mix color - use this instead
321   // of using the new base index to avoid changing existing applications
322   // String keys will get to this property.
323   mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor) );
324
325   mImpl->mRenderer.RegisterProperty( BLUR_RADIUS_NAME, mBlurRadius );
326
327   if( mImpl->mMixColor.a < 1.f || !EqualsZero( mBlurRadius ) )
328   {
329     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
330   }
331
332   // Register transform properties
333   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
334 }
335
336 } // namespace Internal
337
338 } // namespace Toolkit
339
340 } // namespace Dali