Removal of renderable attachment
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / image-actor-impl.cpp
1 /*
2  * Copyright (c) 2014 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 <dali/internal/event/actors/image-actor-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
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>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 // Properties
44
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 )
52
53 BaseHandle Create()
54 {
55   return Dali::ImageActor::New();
56 }
57
58 TypeRegistration mType( typeid( Dali::ImageActor ), typeid( Dali::Actor ), Create );
59
60 struct GridVertex
61 {
62   Vector3 mPosition;
63   Vector2 mTextureCoord;
64 };
65
66 GeometryPtr CreateQuadGeometry( const Vector2& size, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
67 {
68   const float halfWidth = size.width * 0.5f;
69   const float halfHeight = size.height * 0.5f;
70   GridVertex quadVertexData[4] =
71   {
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 ) }
76   };
77
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);
85
86   // Create the geometry object
87   GeometryPtr geometry = Geometry::New();
88   geometry->AddVertexBuffer( *quadVertices );
89   geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
90
91   return geometry;
92 }
93
94 GeometryPtr CreateGridGeometry( const Vector2& size, unsigned int gridWidth, unsigned int gridHeight, unsigned int imageWidth, unsigned int imageHeight, const Dali::ImageActor::PixelArea& pixelArea )
95 {
96   // Create vertices
97   std::vector< GridVertex > vertices;
98   vertices.reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
99
100   for( unsigned int y = 0u; y < gridHeight + 1; ++y )
101   {
102     float yPos = (float)y / gridHeight;
103     for( unsigned int x = 0u; x < gridWidth + 1; ++x )
104     {
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 )
110                           };
111       vertices.push_back( vertex );
112     }
113   }
114
115   // Create indices
116   Vector< unsigned int > indices;
117   indices.Reserve( ( gridWidth + 2 ) * gridHeight * 2 - 2);
118
119   for( unsigned int row = 0u; row < gridHeight; ++row )
120   {
121     unsigned int rowStartIndex = row*(gridWidth+1u);
122     unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
123
124     if( row != 0u ) // degenerate index on non-first row
125     {
126       indices.PushBack( rowStartIndex );
127     }
128
129     for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
130     {
131       indices.PushBack( rowStartIndex + column);
132       indices.PushBack( nextRowStartIndex + column);
133     }
134
135     if( row != gridHeight-1u ) // degenerate index on non-last row
136     {
137       indices.PushBack( nextRowStartIndex + gridWidth );
138     }
139   }
140
141
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 )
149   {
150     vertexPropertyBuffer->SetData( &vertices[ 0 ] );
151   }
152
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 )
159   {
160     indexPropertyBuffer->SetData( &indices[ 0 ] );
161   }
162
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 );
168
169   return geometry;
170
171 }
172
173
174 template< typename T>
175 ConstraintBase* CreateEqualConstraint(Object& target, Property::Index index, Internal::SourceContainer& sources )
176 {
177   typedef typename Dali::Internal::PropertyConstraintPtr< T >::Type PropertyConstraintPtrType;
178
179   CallbackBase* callback = new Dali::Constraint::Function< T >( EqualToConstraint() );
180   PropertyConstraintPtrType funcPtr( new Internal::PropertyConstraint< T >( reinterpret_cast< Dali::Constraint::Function< T >* >( callback ) ) );
181
182   return Internal::Constraint< T >::New( target, index, sources, funcPtr );
183 }
184
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
192   \n
193   void main()\n
194   {\n
195     mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
196     vertexPosition = uMvpMatrix * vertexPosition;\n
197     \n
198     vTexCoord = aTexCoord;\n
199     gl_Position = vertexPosition;\n
200   }\n
201 );
202
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
207   \n
208   void main()\n
209   {\n
210     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
211   }\n
212 );
213
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;
217 }
218
219 ImageActorPtr ImageActor::New()
220 {
221   ImageActorPtr actor( new ImageActor );
222
223   // Second-phase construction of base class
224   actor->Initialize();
225
226   //Create the renderer
227   actor->mRenderer = Renderer::New();
228
229   GeometryPtr quad = CreateQuadGeometry( Vector2::ONE, 1, 1, PixelArea() );
230   actor->mRenderer->SetGeometry( *quad );
231
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 );
236
237   return actor;
238 }
239
240 void ImageActor::OnInitialize()
241 {
242   // TODO: Remove this, at the moment its needed for size negotiation to work
243   SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
244 }
245
246 void ImageActor::SetImage( ImagePtr& image )
247 {
248   if( !image )
249   {
250     if( mRendererIndex != INVALID_RENDERER_ID )
251     {
252       RemoveRenderer( mRendererIndex );
253       mRendererIndex = INVALID_RENDERER_ID;
254     }
255   }
256   else
257   {
258     SamplerPtr sampler = Sampler::New();
259     sampler->SetFilterMode( mMinFilter, mMagFilter );
260
261     mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
262
263     if( mRendererIndex == INVALID_RENDERER_ID )
264     {
265       mRendererIndex = AddRenderer( *mRenderer );
266     }
267
268     if( !mIsPixelAreaSet )
269     {
270       mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
271     }
272
273     RelayoutRequest();
274   }
275 }
276
277 ImagePtr ImageActor::GetImage() const
278 {
279   return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
280 }
281
282 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
283 {
284   mPixelArea = pixelArea;
285   mIsPixelAreaSet = true;
286
287   RelayoutRequest();
288 }
289
290 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
291 {
292   return mPixelArea;
293 }
294
295 bool ImageActor::IsPixelAreaSet() const
296 {
297   return mIsPixelAreaSet;
298 }
299
300 void ImageActor::ClearPixelArea()
301 {
302   mIsPixelAreaSet = false;
303
304   int imageWidth = 0;
305   int imageHeight = 0;
306   ImagePtr image = GetImage();
307   if( image )
308   {
309     imageWidth = image->GetWidth();
310     imageHeight = image->GetHeight();
311   }
312
313   mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
314
315   RelayoutRequest();
316 }
317
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 )
326 {
327 }
328
329 ImageActor::~ImageActor()
330 {
331 }
332
333 Vector3 ImageActor::GetNaturalSize() const
334 {
335   Vector2 naturalSize( CalculateNaturalSize() );
336   return Vector3( naturalSize.width, naturalSize.height, 0.f );
337 }
338
339 Vector2 ImageActor::CalculateNaturalSize() const
340 {
341   // if no image then natural size is 0
342   Vector2 size( 0.0f, 0.0f );
343
344   ImagePtr image = GetImage();
345   if( image )
346   {
347     if( IsPixelAreaSet() )
348     {
349       PixelArea area(GetPixelArea());
350       size.width = area.width;
351       size.height = area.height;
352     }
353     else
354     {
355       size = image->GetNaturalSize();
356     }
357   }
358
359   return size;
360 }
361
362 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
363 {
364   unsigned int gridWidth = 1;
365   unsigned int gridHeight = 1;
366   if( mShaderEffect )
367   {
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 );
372
373     if( ( hints & Dali::ShaderEffect::HINT_GRID_X ) )
374     {
375       gridWidth = size.width / gridDensity;
376     }
377     if( ( hints & Dali::ShaderEffect::HINT_GRID_Y ) )
378     {
379       gridHeight = size.height / gridDensity;
380     }
381
382     //limit the grid size
383     gridWidth = std::min( MAXIMUM_GRID_SIZE, gridWidth );
384     gridHeight = std::min( MAXIMUM_GRID_SIZE, gridHeight );
385   }
386
387   unsigned int imageWidth = 1u;
388   unsigned int imageHeight = 1u;
389   ImagePtr image = GetImage();
390   if( image )
391   {
392     imageWidth = image->GetWidth();
393     imageHeight = image->GetHeight();
394   }
395
396   GeometryPtr quad = gridWidth <= 1 && gridHeight <= 1 ?
397                        CreateQuadGeometry( size, imageWidth, imageHeight, mPixelArea ) :
398                        CreateGridGeometry( size, gridWidth, gridHeight, imageWidth, imageHeight, mPixelArea );
399
400   mRenderer->SetGeometry( *quad );
401
402   Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
403   if( mIsPixelAreaSet && image )
404   {
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);
411
412     // bottom left
413     textureRect.x = x;
414     textureRect.y = y;
415
416     // top right
417     textureRect.z = x + width;
418     textureRect.w = y + height;
419   }
420
421   Material* material = mRenderer->GetMaterial();
422   Property::Index index = material->GetPropertyIndex( "sTextureRect" );
423   if( index != Property::INVALID_INDEX )
424   {
425     material->SetProperty( index, textureRect );
426   }
427   else
428   {
429     material->RegisterProperty( "sTextureRect", textureRect );
430   }
431 }
432
433 unsigned int ImageActor::GetDefaultPropertyCount() const
434 {
435   return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
436 }
437
438 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
439 {
440   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
441
442   indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
443
444   int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
445   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
446   {
447     indices.PushBack( index );
448   }
449 }
450
451 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
452 {
453   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
454   {
455     return Actor::IsDefaultPropertyWritable(index);
456   }
457
458   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
459   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
460   {
461     return DEFAULT_PROPERTY_DETAILS[ index ].writable;
462   }
463
464   return false;
465 }
466
467 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
468 {
469   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
470   {
471     return Actor::IsDefaultPropertyAnimatable( index );
472   }
473
474   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
475   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
476   {
477     return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
478   }
479
480   return false;
481 }
482
483 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
484 {
485   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
486   {
487     return Actor::IsDefaultPropertyAConstraintInput( index );
488   }
489
490   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
491   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
492   {
493     return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
494   }
495
496   return false;
497 }
498
499 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
500 {
501   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
502   {
503     return Actor::GetDefaultPropertyType( index );
504   }
505
506   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
507   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
508   {
509     return DEFAULT_PROPERTY_DETAILS[index].type;
510   }
511
512   // index out-of-bounds
513   return Property::NONE;
514 }
515
516 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
517 {
518   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
519   {
520     return Actor::GetDefaultPropertyName(index);
521   }
522
523   index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
524   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
525   {
526     return DEFAULT_PROPERTY_DETAILS[index].name;
527   }
528
529   // index out-of-bounds
530   return NULL;
531 }
532
533 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
534 {
535   Property::Index index = Property::INVALID_INDEX;
536
537   // Look for name in default properties
538   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
539   {
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
542     {
543       index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
544       break;
545     }
546   }
547
548   // If not found, check in base class
549   if( Property::INVALID_INDEX == index )
550   {
551     index = Actor::GetDefaultPropertyIndex( name );
552   }
553   return index;
554 }
555
556 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
557 {
558   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
559   {
560     Actor::SetDefaultProperty( index, propertyValue );
561   }
562   else
563   {
564     switch(index)
565     {
566       case Dali::ImageActor::Property::PIXEL_AREA:
567       {
568         SetPixelArea(propertyValue.Get<Rect<int> >());
569         break;
570       }
571       case Dali::ImageActor::Property::STYLE:
572       {
573         //not supported
574         break;
575       }
576       case Dali::ImageActor::Property::BORDER:
577       {
578         //not supported
579         break;
580       }
581       case Dali::ImageActor::Property::IMAGE:
582       {
583         Dali::Image img = Scripting::NewImage( propertyValue );
584         if(img)
585         {
586           ImagePtr image( &GetImplementation(img) );
587           SetImage( image );
588         }
589         else
590         {
591           DALI_LOG_WARNING("Cannot create image from property value\n");
592         }
593         break;
594       }
595       default:
596       {
597         DALI_LOG_WARNING("Unknown property (%d)\n", index);
598         break;
599       }
600     } // switch(index)
601
602   } // else
603 }
604
605 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
606 {
607   Property::Value ret;
608   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
609   {
610     ret = Actor::GetDefaultProperty( index );
611   }
612   else
613   {
614     switch( index )
615     {
616       case Dali::ImageActor::Property::PIXEL_AREA:
617       {
618         Rect<int> r = GetPixelArea();
619         ret = r;
620         break;
621       }
622       case Dali::ImageActor::Property::STYLE:
623       {
624         //not supported
625         break;
626       }
627       case Dali::ImageActor::Property::BORDER:
628       {
629         //not supported
630         break;
631       }
632       case Dali::ImageActor::Property::IMAGE:
633       {
634         Property::Map map;
635         Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
636         ret = Property::Value( map );
637         break;
638       }
639       default:
640       {
641         DALI_LOG_WARNING( "Unknown property (%d)\n", index );
642         break;
643       }
644     } // switch(index)
645   }
646
647   return ret;
648 }
649
650 void ImageActor::SetSortModifier(float modifier)
651 {
652   mRenderer->SetDepthIndex( modifier );
653 }
654
655 float ImageActor::GetSortModifier() const
656 {
657   return mRenderer->GetDepthIndex();
658 }
659
660 void ImageActor::SetCullFace(CullFaceMode mode)
661 {
662   mRenderer->GetMaterial()->SetFaceCullingMode( static_cast< Dali::Material::FaceCullingMode >( mode ) );
663 }
664
665 CullFaceMode ImageActor::GetCullFace() const
666 {
667   return static_cast< CullFaceMode >( mRenderer->GetMaterial()->GetFaceCullingMode() );
668 }
669
670 void ImageActor::SetBlendMode( BlendingMode::Type mode )
671 {
672   mRenderer->GetMaterial()->SetBlendMode( mode );
673 }
674
675 BlendingMode::Type ImageActor::GetBlendMode() const
676 {
677   return mRenderer->GetMaterial()->GetBlendMode();
678 }
679
680 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba,   BlendingFactor::Type destFactorRgba )
681 {
682   mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
683 }
684
685 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb,   BlendingFactor::Type destFactorRgb,
686                                BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
687 {
688   mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
689 }
690
691 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,   BlendingFactor::Type& destFactorRgb,
692                                BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
693 {
694   mRenderer->GetMaterial()->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
695 }
696
697 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
698 {
699   mRenderer->GetMaterial()->SetBlendEquation( equationRgba, equationRgba );
700 }
701
702 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
703 {
704   mRenderer->GetMaterial()->SetBlendEquation( equationRgb, equationAlpha );
705 }
706
707 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
708 {
709   mRenderer->GetMaterial()->GetBlendEquation( equationRgb, equationAlpha );
710 }
711
712 void ImageActor::SetBlendColor( const Vector4& color )
713 {
714   mBlendColor = color;
715   mRenderer->GetMaterial()->SetBlendColor( mBlendColor );
716 }
717
718 const Vector4& ImageActor::GetBlendColor() const
719 {
720   return mBlendColor;
721 }
722
723 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
724 {
725   mMinFilter = minFilter;
726   mMagFilter = magFilter;
727
728   if( mTextureIndex != INVALID_TEXTURE_ID )
729   {
730     SamplerPtr sampler = Sampler::New();
731     sampler->SetFilterMode( minFilter, magFilter );
732
733     mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
734   }
735 }
736
737 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
738 {
739   minFilter = mMinFilter;
740   magFilter = mMagFilter;
741 }
742
743 void ImageActor::SetShaderEffect( ShaderEffect& effect )
744 {
745   if( mShaderEffect )
746   {
747     mShaderEffect->Disconnect( this );
748   }
749
750   mShaderEffect = ShaderEffectPtr( &effect );
751   effect.Connect( this );
752
753   Dali::ShaderEffect::GeometryHints hints = effect.GetGeometryHints();
754
755   int shaderHints = Dali::Shader::HINT_NONE;
756
757   if( hints & Dali::ShaderEffect::HINT_DEPTH_BUFFER )
758   {
759     shaderHints |= Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST;
760   }
761   if( hints & Dali::ShaderEffect::HINT_BLENDING )
762   {
763     shaderHints |= Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT;
764   }
765   if( !(hints & Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY) )
766   {
767     shaderHints |= Dali::Shader::HINT_MODIFIES_GEOMETRY;
768   }
769
770   ShaderPtr shader = Shader::New( effect.GetVertexShader(), effect.GetFragmentShader(), static_cast< Dali::Shader::ShaderHints >( shaderHints ) );
771   mRenderer->GetMaterial()->SetShader( *shader );
772
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 )
776   {
777     const ShaderEffect::Uniform& uniform = *it;
778     EffectUniformUpdated( uniform );
779   }
780
781   if( mShaderEffect->GetEffectImage() )
782   {
783     EffectImageUpdated();
784   }
785 }
786
787 void ImageActor::EffectUniformUpdated( const ShaderEffect::Uniform& uniform )
788 {
789   if( !mShaderEffect )
790   {
791     return;
792   }
793
794   Shader* shader = mRenderer->GetMaterial()->GetShader();
795
796   Property::Index index = shader->GetPropertyIndex( uniform.mName );
797   if( index == Property::INVALID_INDEX )
798   {
799     index = shader->RegisterProperty( uniform.mName, uniform.mValue );
800
801     //Constrain the shader's uniform properties to the ShaderEffect properties
802     Internal::ConstraintBase* constraint = NULL;
803     Internal::SourceContainer sources;
804
805     switch( uniform.mValue.GetType() )
806     {
807       case Property::INTEGER:
808       case Property::FLOAT:
809       {
810         constraint = CreateEqualConstraint< float >(*shader, index, sources );
811         break;
812       }
813       case Property::VECTOR2:
814       {
815         constraint = CreateEqualConstraint< Vector2 >(*shader, index, sources );
816         break;
817       }
818       case Property::VECTOR3:
819       {
820         constraint = CreateEqualConstraint< Vector3 >(*shader, index, sources );
821         break;
822       }
823       case Property::VECTOR4:
824       {
825         constraint = CreateEqualConstraint< Vector4 >(*shader, index, sources );
826         break;
827       }
828       case Property::MATRIX3:
829       {
830         constraint = CreateEqualConstraint< Matrix3 >(*shader, index, sources );
831         break;
832       }
833       case Property::MATRIX:
834       {
835         constraint = CreateEqualConstraint< Matrix >(*shader, index, sources );
836         break;
837       }
838       case Property::BOOLEAN:
839       case Property::ARRAY:
840       case Property::ROTATION:
841       case Property::STRING:
842       case Property::RECTANGLE:
843       case Property::MAP:
844       case Property::NONE:
845         //not supported
846         break;
847     }
848
849     //constrain the renderers property to the ShaderEffect
850     if( constraint )
851     {
852       Source source;
853       source.sourceType = OBJECT_PROPERTY;
854       source.propertyIndex = uniform.mIndex;
855       source.object = mShaderEffect.Get();
856
857       constraint->AddSource( source );
858       constraint->Apply();
859     }
860   }
861   else
862   {
863     shader->SetProperty( index, uniform.mValue );
864   }
865 }
866
867 ShaderEffectPtr ImageActor::GetShaderEffect() const
868 {
869   return mShaderEffect;
870 }
871
872 void ImageActor::RemoveShaderEffect()
873 {
874   //if we previously had a subdivided grid then we need to reset the geometry as well
875   if( mShaderEffect )
876   {
877     mShaderEffect->Disconnect( this );
878
879     Dali::ShaderEffect::GeometryHints hints = mShaderEffect->GetGeometryHints();
880     if( ( hints & Dali::ShaderEffect::HINT_GRID_X ) ||
881         ( hints & Dali::ShaderEffect::HINT_GRID_Y ) )
882     {
883       RelayoutRequest();
884     }
885   }
886
887   ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
888   mRenderer->GetMaterial()->SetShader( *shader );
889   mShaderEffect.Reset();
890 }
891
892 void ImageActor::EffectImageUpdated()
893 {
894   if( mShaderEffect )
895   {
896     Dali::Image effectImage = mShaderEffect->GetEffectImage();
897     if( effectImage )
898     {
899       Image& effectImageImpl = GetImplementation( effectImage );
900
901       if( mEffectTextureIndex == INVALID_TEXTURE_ID )
902       {
903         mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
904       }
905       else
906       {
907         mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
908       }
909     }
910     else
911     {
912       if( mEffectTextureIndex != INVALID_TEXTURE_ID )
913       {
914         mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
915       }
916       mEffectTextureIndex = INVALID_TEXTURE_ID;
917     }
918
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 )
923     {
924       shader->RegisterProperty( "sEffectRect", Vector4( 0.f, 0.f, 1.f, 1.f) );
925     }
926   }
927 }
928
929 } // namespace Internal
930
931 } // namespace Dali