2 * Copyright (c) 2015 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 "gradient-renderer.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/images/buffer-image.h>
25 #include <dali/public-api/object/property-array.h>
28 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
29 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
30 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
31 #include <dali-toolkit/internal/controls/renderers/gradient/linear-gradient.h>
32 #include <dali-toolkit/internal/controls/renderers/gradient/radial-gradient.h>
45 // properties: linear gradient
46 const char * const GRADIENT_START_POSITION_NAME("gradient-start-position"); // Property::VECTOR2
47 const char * const GRADIENT_END_POSITION_NAME("gradient-end-position"); // Property::VECTOR2
49 // properties: radial gradient
50 const char * const GRADIENT_CENTER_NAME("gradient-center"); // Property::VECTOR2
51 const char * const GRADIENT_RADIUS_NAME("gradient-radius"); // Property::FLOAT
53 // properties: linear&radial gradient
54 const char * const GRADIENT_STOP_OFFSET_NAME("gradient-stop-offset"); // Property::Array FLOAT
55 const char * const GRADIENT_STOP_COLOR_NAME("gradient-stop-color"); // Property::Array VECTOR4
56 const char * const GRADIENT_UNITS_NAME("gradient-units"); // Property::String "userSpaceOnUse | objectBoundingBox"
57 const char * const GRADIENT_SPREAD_METHOD_NAME("gradient-spread-method"); // Property::String "pad | reflect | repeat"
60 const char * const UNIT_USER_SPACE("user-space");
61 const char * const SPREAD_REFLECT("reflect");
62 const char * const SPREAD_REPEAT("repeat");
65 const char * const UNIFORM_ALIGNMENT_MATRIX_NAME( "uAlignmentMatrix" );
66 const char * const UNIFORM_TEXTULRE_NAME("sTexture");
69 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
70 attribute mediump vec2 aPosition;\n
71 uniform mediump mat4 uMvpMatrix;\n
72 uniform mediump vec3 uSize;\n
73 uniform mediump mat3 uAlignmentMatrix;\n
74 varying mediump vec2 vTexCoord;\n
78 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
79 vertexPosition.xyz *= uSize;\n
80 gl_Position = uMvpMatrix * vertexPosition;\n
82 vTexCoord = (uAlignmentMatrix*vertexPosition.xyw).xy;\n
86 const char* FRAGMENT_SHADER_LINEAR = DALI_COMPOSE_SHADER(
87 uniform sampler2D sTexture;\n // sampler1D?
88 uniform lowp vec4 uColor;\n
89 varying mediump vec2 vTexCoord;\n
93 gl_FragColor = texture2D( sTexture, vec2( vTexCoord.y, 0.5 ) ) * uColor;\n
97 const char* FRAGMENT_SHADER_RADIAL = DALI_COMPOSE_SHADER(
98 uniform sampler2D sTexture;\n // sampler1D?
99 uniform lowp vec4 uColor;\n
100 varying mediump vec2 vTexCoord;\n
104 gl_FragColor = texture2D( sTexture, vec2( length(vTexCoord), 0.5 ) ) * uColor;\n
108 Sampler::WrapMode GetWrapMode( Gradient::SpreadMethod spread )
112 case Gradient::REPEAT:
114 return Sampler::REPEAT;
116 case Gradient::REFLECT:
118 return Sampler::MIRRORED_REPEAT;
123 return Sampler::CLAMP_TO_EDGE;
131 GradientRenderer::GradientRenderer()
132 :mGradientTransformIndex( Property::INVALID_INDEX )
136 GradientRenderer::~GradientRenderer()
140 void GradientRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
142 mImpl->mGeometry = factoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
143 if( !(mImpl->mGeometry) )
145 mImpl->mGeometry = RendererFactoryCache::CreateQuadGeometry();
146 factoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, mImpl->mGeometry );
150 if( propertyMap.Find( GRADIENT_RADIUS_NAME ))
152 mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::GRADIENT_SHADER_RADIAL );
153 if( !(mImpl->mShader) )
155 mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_RADIAL );
156 factoryCache.SaveShader( RendererFactoryCache::GRADIENT_SHADER_RADIAL, mImpl->mShader );
158 gradientType = RADIAL;
162 mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::GRADIENT_SHADER_LINEAR );
163 if( !(mImpl->mShader) )
165 mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_LINEAR );
166 factoryCache.SaveShader( RendererFactoryCache::GRADIENT_SHADER_LINEAR, mImpl->mShader );
168 gradientType = LINEAR;
171 if( NewGradient(gradientType, propertyMap) )
173 mGradientTransform = mGradient->GetAlignmentTransform();
177 DALI_LOG_ERROR( "Fail to provide valid properties to create a GradientRenderer object" );
181 void GradientRenderer::SetSize( const Vector2& size )
183 ControlRenderer::SetSize( size );
185 if( mGradient->GetGradientUnits() == Gradient::OBJECT_BOUNDING_BOX )
188 Matrix3 scaling( 1.f/(size.x+Math::MACHINE_EPSILON_100), 0.f, 0.f,
189 0.f, 1.f/(size.y+Math::MACHINE_EPSILON_100), 0.f, 0.5f, 0.5f, 1.f );
190 Matrix3::Multiply( mGradientTransform, scaling, mGradient->GetAlignmentTransform() );
192 if( mImpl->mRenderer )
194 (mImpl->mRenderer).SetProperty( mGradientTransformIndex, mGradientTransform );
199 void GradientRenderer::SetClipRect( const Rect<int>& clipRect )
201 ControlRenderer::SetClipRect( clipRect );
203 //ToDo: renderer responds to the clipRect change
206 void GradientRenderer::SetOffset( const Vector2& offset )
208 //ToDo: renderer applies the offset
211 void GradientRenderer::DoSetOnStage( Actor& actor )
213 mGradientTransformIndex = (mImpl->mRenderer).RegisterProperty( UNIFORM_ALIGNMENT_MATRIX_NAME, mGradientTransform );
215 Dali::BufferImage lookupTexture = mGradient->GenerateLookupTexture();
216 Sampler sampler = Sampler::New( lookupTexture, UNIFORM_TEXTULRE_NAME );
217 Sampler::WrapMode wrap = GetWrapMode( mGradient->GetSpreadMethod() );
218 sampler.SetWrapMode( wrap, wrap );
220 Material material = (mImpl->mRenderer).GetMaterial();
223 material.AddSampler( sampler );
227 bool GradientRenderer::NewGradient(Type gradientType, const Property::Map& propertyMap)
229 if( gradientType==LINEAR )
231 Property::Value* startPositionValue = propertyMap.Find( GRADIENT_START_POSITION_NAME );
232 Property::Value* endPositionValue = propertyMap.Find( GRADIENT_END_POSITION_NAME );
233 Vector2 startPosition;
236 if( startPositionValue && startPositionValue->Get(startPosition)
237 && endPositionValue && endPositionValue->Get( endPosition ) )
239 mGradient = new LinearGradient( startPosition, endPosition );
248 Property::Value* centerValue = propertyMap.Find( GRADIENT_CENTER_NAME );
249 Property::Value* radiusValue = propertyMap.Find( GRADIENT_RADIUS_NAME );
252 if( centerValue && centerValue->Get(center)
253 && radiusValue && radiusValue->Get(radius) )
255 mGradient = new RadialGradient( center, radius );
263 unsigned int numValidStop = 0u;
264 Property::Value* stopOffsetValue = propertyMap.Find( GRADIENT_STOP_OFFSET_NAME );
265 Property::Value* stopColorValue = propertyMap.Find( GRADIENT_STOP_COLOR_NAME );
266 if( stopOffsetValue && stopColorValue )
268 Property::Array* offsetArray = stopOffsetValue->GetArray();
269 Property::Array* colorArray = stopColorValue->GetArray();
270 if( offsetArray && colorArray )
272 unsigned int numStop = offsetArray->Count() < colorArray->Count() ?
273 offsetArray->Count() : colorArray->Count();
276 for( unsigned int i=0; i<numStop; i++ )
278 if( (offsetArray->GetElementAt(i)).Get(offset)
279 && (colorArray->GetElementAt(i)).Get(color) )
281 mGradient->AddStop( offset, color);
287 if( numValidStop < 1u ) // no valid stop
292 Property::Value* unitsValue = propertyMap.Find( GRADIENT_UNITS_NAME );
294 // The default unit is OBJECT_BOUNDING_BOX.
295 // Only need to set new units if 'user-space'
296 if( unitsValue && unitsValue->Get( units ) && units == UNIT_USER_SPACE )
298 mGradient->SetGradientUnits( Gradient::USER_SPACE_ON_USE );
301 Property::Value* spread = propertyMap.Find( GRADIENT_SPREAD_METHOD_NAME );
302 std::string stringValue ;
303 // The default spread method is PAD.
304 // Only need to set new spread if 'reflect' or 'repeat"
305 if( spread && spread->Get( stringValue ))
307 if( stringValue == SPREAD_REFLECT )
309 mGradient->SetSpreadMethod( Gradient::REFLECT );
311 else if( stringValue == SPREAD_REPEAT )
313 mGradient->SetSpreadMethod( Gradient::REPEAT );
320 } // namespace Internal
322 } // namespace Toolkit