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