2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "color-visual.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
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>
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
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
59 vec4 ComputeVertexPosition()\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
68 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
72 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
73 uniform lowp vec4 uColor;\n
74 uniform lowp vec3 mixColor;\n
78 gl_FragColor = vec4(mixColor, 1.0)*uColor;\n
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
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
97 vec4 ComputeVertexPosition()\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
108 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
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
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
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
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
143 vec4 ComputeVertexPosition()\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
154 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
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
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
175 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
177 ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
178 colorVisualPtr->SetProperties( properties );
179 return colorVisualPtr;
182 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
183 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
185 mRenderIfTransparent( false )
189 ColorVisual::~ColorVisual()
193 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
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 );
201 if( colorValue->Get( color ) )
203 Property::Type type = colorValue->GetType();
204 if( type == Property::VECTOR4 )
206 SetMixColor( color );
208 else if( type == Property::VECTOR3 )
210 Vector3 color3(color);
211 SetMixColor( color3 );
216 DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
220 Property::Value* renderIfTransparentValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::RENDER_IF_TRANSPARENT, RENDER_IF_TRANSPARENT_NAME );
221 if( renderIfTransparentValue )
223 if( ! renderIfTransparentValue->Get( mRenderIfTransparent ) )
225 DALI_LOG_ERROR( "ColorVisual: renderIfTransparent property has incorrect type: %d\n", renderIfTransparentValue->GetType() );
229 Property::Value* blurRadiusValue = propertyMap.Find( Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME );
230 if( blurRadiusValue )
232 if( !blurRadiusValue->Get( mBlurRadius ) )
234 DALI_LOG_ERROR( "ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType() );
239 void ColorVisual::DoSetOnStage( Actor& actor )
241 InitializeRenderer();
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 )
247 actor.AddRenderer( mImpl->mRenderer );
250 // Color Visual generated and ready to display
251 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
254 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
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 );
263 void ColorVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
269 void ColorVisual::OnSetTransform()
271 if( mImpl->mRenderer )
273 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
277 void ColorVisual::InitializeRenderer()
279 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
282 if( !EqualsZero( mBlurRadius ) )
284 shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE );
287 shader = Shader::New( VERTEX_SHADER_BLUR_EDGE, FRAGMENT_SHADER_BLUR_EDGE );
288 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader );
291 else if( !IsRoundedCornerRequired() )
293 shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
296 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
297 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
302 shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER );
305 shader = Shader::New( VERTEX_SHADER_ROUNDED_CORNER, FRAGMENT_SHADER_ROUNDED_CORNER );
306 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader );
310 mImpl->mRenderer = Renderer::New( geometry, shader );
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) );
317 mImpl->mRenderer.RegisterProperty( BLUR_RADIUS_NAME, mBlurRadius );
319 if( mImpl->mMixColor.a < 1.f || !EqualsZero( mBlurRadius ) )
321 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
324 // Register transform properties
325 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
328 } // namespace Internal
330 } // namespace Toolkit