2 * Copyright (c) 2014 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 <dali/internal/event/actors/image-actor-impl.h>
22 #include <cstring> // for strcmp
25 #include <dali/public-api/animation/constraints.h> // for EqualToConstraint
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/devel-api/scripting/scripting.h>
28 #include <dali/internal/event/animation/constraint-impl.h>
29 #include <dali/internal/event/common/property-helper.h>
30 #include <dali/internal/event/effects/shader-effect-impl.h>
31 #include <dali/internal/event/images/image-connector.h>
32 #include <dali/internal/event/images/nine-patch-image-impl.h>
45 // Name Type writable animatable constraint-input enum for index-checking
46 DALI_PROPERTY_TABLE_BEGIN
47 DALI_PROPERTY( "pixelArea", RECTANGLE, true, false, true, Dali::ImageActor::Property::PIXEL_AREA )
48 DALI_PROPERTY( "style", STRING, true, false, true, Dali::ImageActor::Property::STYLE )
49 DALI_PROPERTY( "border", VECTOR4, true, false, true, Dali::ImageActor::Property::BORDER )
50 DALI_PROPERTY( "image", MAP, true, false, false, Dali::ImageActor::Property::IMAGE )
51 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
55 return Dali::ImageActor::New();
58 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::Actor ), Create );
63 Vector2 mTextureCoord;
66 GeometryPtr CreateQuadGeometry( const Vector2& size, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
68 const float halfWidth = size.width * 0.5f;
69 const float halfHeight = size.height * 0.5f;
70 GridVertex quadVertexData[4] =
72 { Vector3( -halfWidth, -halfHeight, 0.f ), Vector2( ( pixelArea.x ) / (float)imageWidth, ( pixelArea.y ) / (float)imageHeight ) },
73 { Vector3( -halfWidth, halfHeight, 0.f ), Vector2( ( pixelArea.x ) / (float)imageWidth, ( pixelArea.y + pixelArea.height ) / (float)imageHeight ) },
74 { Vector3( halfWidth, -halfHeight, 0.f ), Vector2( ( pixelArea.x + pixelArea.width ) / (float)imageWidth, ( pixelArea.y ) / (float)imageHeight ) },
75 { Vector3( halfWidth, halfHeight, 0.f ), Vector2( ( pixelArea.x + pixelArea.width ) / (float)imageWidth, ( pixelArea.y + pixelArea.height ) / (float)imageHeight ) }
78 Property::Map quadVertexFormat;
79 quadVertexFormat["aPosition"] = Property::VECTOR3;
80 quadVertexFormat["aTexCoord"] = Property::VECTOR2;
81 PropertyBufferPtr quadVertices = PropertyBuffer::New();
82 quadVertices->SetFormat( quadVertexFormat );
83 quadVertices->SetSize( 4 );
84 quadVertices->SetData(quadVertexData);
86 // Create the geometry object
87 GeometryPtr geometry = Geometry::New();
88 geometry->AddVertexBuffer( *quadVertices );
89 geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
94 GeometryPtr CreateGridGeometry( const Vector2& size, unsigned int gridWidth, unsigned int gridHeight, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
97 std::vector< GridVertex > vertices;
98 vertices.reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
100 for( unsigned int y = 0u; y < gridHeight + 1; ++y )
102 float yPos = (float)y / gridHeight;
103 for( unsigned int x = 0u; x < gridWidth + 1; ++x )
105 float xPos = (float)x / gridWidth;
106 GridVertex vertex = {
107 Vector3( size.width * ( xPos - 0.5f ), size.height * ( yPos - 0.5f ), 0.0f ),
108 Vector2( ( pixelArea.x + pixelArea.width * xPos ) / (float)imageWidth,
109 ( pixelArea.y + pixelArea.height * yPos ) / (float)imageHeight )
111 vertices.push_back( vertex );
116 Vector< unsigned int > indices;
117 indices.Reserve( ( gridWidth + 2 ) * gridHeight * 2 - 2);
119 for( unsigned int row = 0u; row < gridHeight; ++row )
121 unsigned int rowStartIndex = row*(gridWidth+1u);
122 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
124 if( row != 0u ) // degenerate index on non-first row
126 indices.PushBack( rowStartIndex );
129 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
131 indices.PushBack( rowStartIndex + column);
132 indices.PushBack( nextRowStartIndex + column);
135 if( row != gridHeight-1u ) // degenerate index on non-last row
137 indices.PushBack( nextRowStartIndex + gridWidth );
142 Property::Map vertexFormat;
143 vertexFormat[ "aPosition" ] = Property::VECTOR3;
144 vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
145 PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New();
146 vertexPropertyBuffer->SetFormat( vertexFormat );
147 vertexPropertyBuffer->SetSize( vertices.size() );
148 if( vertices.size() > 0 )
150 vertexPropertyBuffer->SetData( &vertices[ 0 ] );
153 Property::Map indexFormat;
154 indexFormat[ "indices" ] = Property::INTEGER;
155 PropertyBufferPtr indexPropertyBuffer = PropertyBuffer::New();
156 indexPropertyBuffer->SetFormat( indexFormat );
157 indexPropertyBuffer->SetSize( indices.Size() );
158 if( indices.Size() > 0 )
160 indexPropertyBuffer->SetData( &indices[ 0 ] );
163 // Create the geometry object
164 GeometryPtr geometry = Geometry::New();
165 geometry->AddVertexBuffer( *vertexPropertyBuffer );
166 geometry->SetIndexBuffer( *indexPropertyBuffer );
167 geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
174 template< typename T>
175 ConstraintBase* CreateEqualConstraint(Object& target, Property::Index index, Internal::SourceContainer& sources )
177 typedef typename Dali::Internal::PropertyConstraintPtr< T >::Type PropertyConstraintPtrType;
179 CallbackBase* callback = new Dali::Constraint::Function< T >( EqualToConstraint() );
180 PropertyConstraintPtrType funcPtr( new Internal::PropertyConstraint< T >( reinterpret_cast< Dali::Constraint::Function< T >* >( callback ) ) );
182 return Internal::Constraint< T >::New( target, index, sources, funcPtr );
185 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
186 attribute mediump vec3 aPosition;\n
187 attribute mediump vec2 aTexCoord;\n
188 varying mediump vec2 vTexCoord;\n
189 uniform mediump mat4 uMvpMatrix;\n
190 uniform mediump vec3 uSize;\n
191 uniform mediump vec4 uTextureRect;\n
195 mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
196 vertexPosition = uMvpMatrix * vertexPosition;\n
198 vTexCoord = aTexCoord;\n
199 gl_Position = vertexPosition;\n
203 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
204 varying mediump vec2 vTexCoord;\n
205 uniform sampler2D sTexture;\n
206 uniform lowp vec4 uColor;\n
210 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
214 const size_t INVALID_TEXTURE_ID = (size_t)-1;
215 const int INVALID_RENDERER_ID = -1;
216 const unsigned int MAXIMUM_GRID_SIZE = 2048;
219 ImageActorPtr ImageActor::New()
221 ImageActorPtr actor( new ImageActor );
223 // Second-phase construction of base class
226 //Create the renderer
227 actor->mRenderer = Renderer::New();
229 GeometryPtr quad = CreateQuadGeometry( Vector2::ONE, 1, 1, PixelArea() );
230 actor->mRenderer->SetGeometry( *quad );
232 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
233 MaterialPtr material = Material::New();
234 material->SetShader( *shader );
235 actor->mRenderer->SetMaterial( *material );
240 void ImageActor::OnInitialize()
242 // TODO: Remove this, at the moment its needed for size negotiation to work
243 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
246 void ImageActor::SetImage( ImagePtr& image )
250 if( mRendererIndex != INVALID_RENDERER_ID )
252 RemoveRenderer( mRendererIndex );
253 mRendererIndex = INVALID_RENDERER_ID;
258 SamplerPtr sampler = Sampler::New();
259 sampler->SetFilterMode( mMinFilter, mMagFilter );
261 mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
263 if( mRendererIndex == INVALID_RENDERER_ID )
265 mRendererIndex = AddRenderer( *mRenderer );
268 if( !mIsPixelAreaSet )
270 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
277 ImagePtr ImageActor::GetImage() const
279 return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
282 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
284 mPixelArea = pixelArea;
285 mIsPixelAreaSet = true;
290 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
295 bool ImageActor::IsPixelAreaSet() const
297 return mIsPixelAreaSet;
300 void ImageActor::ClearPixelArea()
302 mIsPixelAreaSet = false;
306 ImagePtr image = GetImage();
309 imageWidth = image->GetWidth();
310 imageHeight = image->GetHeight();
313 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
318 ImageActor::ImageActor()
319 : Actor( Actor::BASIC ),
320 mRendererIndex( INVALID_RENDERER_ID ),
321 mTextureIndex( INVALID_TEXTURE_ID ),
322 mEffectTextureIndex( INVALID_TEXTURE_ID ),
323 mMinFilter( FilterMode::DEFAULT ),
324 mMagFilter( FilterMode::DEFAULT ),
325 mIsPixelAreaSet( false )
329 ImageActor::~ImageActor()
333 Vector3 ImageActor::GetNaturalSize() const
335 Vector2 naturalSize( CalculateNaturalSize() );
336 return Vector3( naturalSize.width, naturalSize.height, 0.f );
339 Vector2 ImageActor::CalculateNaturalSize() const
341 // if no image then natural size is 0
342 Vector2 size( 0.0f, 0.0f );
344 ImagePtr image = GetImage();
347 if( IsPixelAreaSet() )
349 PixelArea area(GetPixelArea());
350 size.width = area.width;
351 size.height = area.height;
355 size = image->GetNaturalSize();
362 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
364 unsigned int gridWidth = 1;
365 unsigned int gridHeight = 1;
368 Dali::ShaderEffect::GeometryHints hints = mShaderEffect->GetGeometryHints();
369 Property::Value gridDensityValue = mShaderEffect->GetDefaultProperty( Dali::ShaderEffect::Property::GRID_DENSITY );
370 int gridDensity = Dali::ShaderEffect::DEFAULT_GRID_DENSITY;
371 gridDensityValue.Get( gridDensity );
373 if( ( hints & Dali::ShaderEffect::HINT_GRID_X ) )
375 gridWidth = size.width / gridDensity;
377 if( ( hints & Dali::ShaderEffect::HINT_GRID_Y ) )
379 gridHeight = size.height / gridDensity;
382 //limit the grid size
383 gridWidth = std::min( MAXIMUM_GRID_SIZE, gridWidth );
384 gridHeight = std::min( MAXIMUM_GRID_SIZE, gridHeight );
387 unsigned int imageWidth = 1u;
388 unsigned int imageHeight = 1u;
389 ImagePtr image = GetImage();
392 imageWidth = image->GetWidth();
393 imageHeight = image->GetHeight();
396 GeometryPtr quad = gridWidth <= 1 && gridHeight <= 1 ?
397 CreateQuadGeometry( size, imageWidth, imageHeight, mPixelArea ) :
398 CreateGridGeometry( size, gridWidth, gridHeight, imageWidth, imageHeight, mPixelArea );
400 mRenderer->SetGeometry( *quad );
402 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
403 if( mIsPixelAreaSet && image )
405 const float uScale = 1.0f / float(imageWidth);
406 const float vScale = 1.0f / float(imageHeight);
407 const float x = uScale * float(mPixelArea.x);
408 const float y = vScale * float(mPixelArea.y);
409 const float width = uScale * float(mPixelArea.width);
410 const float height = vScale * float(mPixelArea.height);
417 textureRect.z = x + width;
418 textureRect.w = y + height;
421 Material* material = mRenderer->GetMaterial();
422 Property::Index index = material->GetPropertyIndex( "sTextureRect" );
423 if( index != Property::INVALID_INDEX )
425 material->SetProperty( index, textureRect );
429 material->RegisterProperty( "sTextureRect", textureRect );
433 unsigned int ImageActor::GetDefaultPropertyCount() const
435 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
438 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
440 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
442 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
444 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
445 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
447 indices.PushBack( index );
451 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
453 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
455 return Actor::IsDefaultPropertyWritable(index);
458 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
459 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
461 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
467 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
469 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
471 return Actor::IsDefaultPropertyAnimatable( index );
474 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
475 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
477 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
483 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
485 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
487 return Actor::IsDefaultPropertyAConstraintInput( index );
490 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
491 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
493 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
499 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
501 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
503 return Actor::GetDefaultPropertyType( index );
506 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
507 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
509 return DEFAULT_PROPERTY_DETAILS[index].type;
512 // index out-of-bounds
513 return Property::NONE;
516 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
518 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
520 return Actor::GetDefaultPropertyName(index);
523 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
524 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
526 return DEFAULT_PROPERTY_DETAILS[index].name;
529 // index out-of-bounds
533 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
535 Property::Index index = Property::INVALID_INDEX;
537 // Look for name in default properties
538 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
540 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
541 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
543 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
548 // If not found, check in base class
549 if( Property::INVALID_INDEX == index )
551 index = Actor::GetDefaultPropertyIndex( name );
556 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
558 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
560 Actor::SetDefaultProperty( index, propertyValue );
566 case Dali::ImageActor::Property::PIXEL_AREA:
568 SetPixelArea(propertyValue.Get<Rect<int> >());
571 case Dali::ImageActor::Property::STYLE:
576 case Dali::ImageActor::Property::BORDER:
581 case Dali::ImageActor::Property::IMAGE:
583 Dali::Image img = Scripting::NewImage( propertyValue );
586 ImagePtr image( &GetImplementation(img) );
591 DALI_LOG_WARNING("Cannot create image from property value\n");
597 DALI_LOG_WARNING("Unknown property (%d)\n", index);
605 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
608 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
610 ret = Actor::GetDefaultProperty( index );
616 case Dali::ImageActor::Property::PIXEL_AREA:
618 Rect<int> r = GetPixelArea();
622 case Dali::ImageActor::Property::STYLE:
627 case Dali::ImageActor::Property::BORDER:
632 case Dali::ImageActor::Property::IMAGE:
635 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
636 ret = Property::Value( map );
641 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
650 void ImageActor::SetSortModifier(float modifier)
652 mRenderer->SetDepthIndex( modifier );
655 float ImageActor::GetSortModifier() const
657 return mRenderer->GetDepthIndex();
660 void ImageActor::SetCullFace(CullFaceMode mode)
662 mRenderer->GetMaterial()->SetFaceCullingMode( static_cast< Dali::Material::FaceCullingMode >( mode ) );
665 CullFaceMode ImageActor::GetCullFace() const
667 return static_cast< CullFaceMode >( mRenderer->GetMaterial()->GetFaceCullingMode() );
670 void ImageActor::SetBlendMode( BlendingMode::Type mode )
672 mRenderer->GetMaterial()->SetBlendMode( mode );
675 BlendingMode::Type ImageActor::GetBlendMode() const
677 return mRenderer->GetMaterial()->GetBlendMode();
680 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
682 mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
685 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
686 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
688 mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
691 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
692 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
694 mRenderer->GetMaterial()->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
697 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
699 mRenderer->GetMaterial()->SetBlendEquation( equationRgba, equationRgba );
702 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
704 mRenderer->GetMaterial()->SetBlendEquation( equationRgb, equationAlpha );
707 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
709 mRenderer->GetMaterial()->GetBlendEquation( equationRgb, equationAlpha );
712 void ImageActor::SetBlendColor( const Vector4& color )
715 mRenderer->GetMaterial()->SetBlendColor( mBlendColor );
718 const Vector4& ImageActor::GetBlendColor() const
723 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
725 mMinFilter = minFilter;
726 mMagFilter = magFilter;
728 if( mTextureIndex != INVALID_TEXTURE_ID )
730 SamplerPtr sampler = Sampler::New();
731 sampler->SetFilterMode( minFilter, magFilter );
733 mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
737 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
739 minFilter = mMinFilter;
740 magFilter = mMagFilter;
743 void ImageActor::SetShaderEffect( ShaderEffect& effect )
747 mShaderEffect->Disconnect( this );
750 mShaderEffect = ShaderEffectPtr( &effect );
751 effect.Connect( this );
753 Dali::ShaderEffect::GeometryHints hints = effect.GetGeometryHints();
755 int shaderHints = Dali::Shader::HINT_NONE;
757 if( hints & Dali::ShaderEffect::HINT_DEPTH_BUFFER )
759 shaderHints |= Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST;
761 if( hints & Dali::ShaderEffect::HINT_BLENDING )
763 shaderHints |= Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT;
765 if( !(hints & Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY) )
767 shaderHints |= Dali::Shader::HINT_MODIFIES_GEOMETRY;
770 ShaderPtr shader = Shader::New( effect.GetVertexShader(), effect.GetFragmentShader(), static_cast< Dali::Shader::ShaderHints >( shaderHints ) );
771 mRenderer->GetMaterial()->SetShader( *shader );
773 //get the uniforms and apply them to the renderer
774 const ShaderEffect::UniformArray& uniforms = mShaderEffect->GetUniforms();
775 for( ShaderEffect::UniformArray::const_iterator it = uniforms.begin(); it != uniforms.end(); ++it )
777 const ShaderEffect::Uniform& uniform = *it;
778 EffectUniformUpdated( uniform );
781 if( mShaderEffect->GetEffectImage() )
783 EffectImageUpdated();
787 void ImageActor::EffectUniformUpdated( const ShaderEffect::Uniform& uniform )
794 Shader* shader = mRenderer->GetMaterial()->GetShader();
796 Property::Index index = shader->GetPropertyIndex( uniform.mName );
797 if( index == Property::INVALID_INDEX )
799 index = shader->RegisterProperty( uniform.mName, uniform.mValue );
801 //Constrain the shader's uniform properties to the ShaderEffect properties
802 Internal::ConstraintBase* constraint = NULL;
803 Internal::SourceContainer sources;
805 switch( uniform.mValue.GetType() )
807 case Property::INTEGER:
808 case Property::FLOAT:
810 constraint = CreateEqualConstraint< float >(*shader, index, sources );
813 case Property::VECTOR2:
815 constraint = CreateEqualConstraint< Vector2 >(*shader, index, sources );
818 case Property::VECTOR3:
820 constraint = CreateEqualConstraint< Vector3 >(*shader, index, sources );
823 case Property::VECTOR4:
825 constraint = CreateEqualConstraint< Vector4 >(*shader, index, sources );
828 case Property::MATRIX3:
830 constraint = CreateEqualConstraint< Matrix3 >(*shader, index, sources );
833 case Property::MATRIX:
835 constraint = CreateEqualConstraint< Matrix >(*shader, index, sources );
838 case Property::BOOLEAN:
839 case Property::ARRAY:
840 case Property::ROTATION:
841 case Property::STRING:
842 case Property::RECTANGLE:
849 //constrain the renderers property to the ShaderEffect
853 source.sourceType = OBJECT_PROPERTY;
854 source.propertyIndex = uniform.mIndex;
855 source.object = mShaderEffect.Get();
857 constraint->AddSource( source );
863 shader->SetProperty( index, uniform.mValue );
867 ShaderEffectPtr ImageActor::GetShaderEffect() const
869 return mShaderEffect;
872 void ImageActor::RemoveShaderEffect()
874 //if we previously had a subdivided grid then we need to reset the geometry as well
877 mShaderEffect->Disconnect( this );
879 Dali::ShaderEffect::GeometryHints hints = mShaderEffect->GetGeometryHints();
880 if( ( hints & Dali::ShaderEffect::HINT_GRID_X ) ||
881 ( hints & Dali::ShaderEffect::HINT_GRID_Y ) )
887 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
888 mRenderer->GetMaterial()->SetShader( *shader );
889 mShaderEffect.Reset();
892 void ImageActor::EffectImageUpdated()
896 Dali::Image effectImage = mShaderEffect->GetEffectImage();
899 Image& effectImageImpl = GetImplementation( effectImage );
901 if( mEffectTextureIndex == INVALID_TEXTURE_ID )
903 mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
907 mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
912 if( mEffectTextureIndex != INVALID_TEXTURE_ID )
914 mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
916 mEffectTextureIndex = INVALID_TEXTURE_ID;
919 //ensure that the sEffectRect uniform is set
920 Shader* shader = mRenderer->GetMaterial()->GetShader();
921 Property::Index index = shader->GetPropertyIndex( "sEffectRect" );
922 if( index == Property::INVALID_INDEX )
924 shader->RegisterProperty( "sEffectRect", Vector4( 0.f, 0.f, 1.f, 1.f) );
929 } // namespace Internal