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 );
62 GridVertex( float positionX, float positionY, const Vector2& size )
63 : mPosition( positionX*size.x, positionY*size.y, 0.f ),
64 mTextureCoord( positionX+0.5f, positionY+0.5f )
69 Vector2 mTextureCoord;
72 GeometryPtr CreateGeometry( unsigned int gridWidth, unsigned int gridHeight, const Vector2& size )
75 std::vector< GridVertex > vertices;
76 vertices.reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
78 for( unsigned int y = 0u; y < gridHeight + 1; ++y )
80 float yPos = (float)y / gridHeight;
81 for( unsigned int x = 0u; x < gridWidth + 1; ++x )
83 float xPos = (float)x / gridWidth;
84 vertices.push_back( GridVertex( xPos - 0.5f, yPos - 0.5f, size ) );
89 Vector< unsigned int > indices;
90 indices.Reserve( ( gridWidth + 2 ) * gridHeight * 2 - 2);
92 for( unsigned int row = 0u; row < gridHeight; ++row )
94 unsigned int rowStartIndex = row*(gridWidth+1u);
95 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
97 if( row != 0u ) // degenerate index on non-first row
99 indices.PushBack( rowStartIndex );
102 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
104 indices.PushBack( rowStartIndex + column);
105 indices.PushBack( nextRowStartIndex + column);
108 if( row != gridHeight-1u ) // degenerate index on non-last row
110 indices.PushBack( nextRowStartIndex + gridWidth );
114 Property::Map vertexFormat;
115 vertexFormat[ "aPosition" ] = Property::VECTOR3;
116 vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
117 PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New( vertexFormat );
118 if( vertices.size() > 0 )
120 vertexPropertyBuffer->SetData( &vertices[ 0 ], vertices.size() );
123 Property::Map indexFormat;
124 indexFormat[ "indices" ] = Property::INTEGER;
125 PropertyBufferPtr indexPropertyBuffer = PropertyBuffer::New( indexFormat );
126 if( indices.Size() > 0 )
128 indexPropertyBuffer->SetData( &indices[ 0 ], indices.Size() );
131 // Create the geometry object
132 GeometryPtr geometry = Geometry::New();
133 geometry->AddVertexBuffer( *vertexPropertyBuffer );
134 geometry->SetIndexBuffer( *indexPropertyBuffer );
135 geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
140 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
141 attribute mediump vec3 aPosition;\n
142 attribute mediump vec2 aTexCoord;\n
143 varying mediump vec2 vTexCoord;\n
144 uniform mediump mat4 uMvpMatrix;\n
145 uniform mediump vec3 uSize;\n
146 uniform mediump vec4 sTextureRect;\n
150 gl_Position = uMvpMatrix * vec4(aPosition, 1.0);\n
151 vTexCoord = aTexCoord;\n
155 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
156 varying mediump vec2 vTexCoord;\n
157 uniform sampler2D sTexture;\n
158 uniform lowp vec4 uColor;\n
162 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
166 const char * const TEXTURE_RECT_UNIFORM_NAME( "sTextureRect" );
168 const int INVALID_RENDERER_ID = -1;
169 const uint16_t MAXIMUM_GRID_SIZE = 2048;
172 ImageActorPtr ImageActor::New()
174 ImageActorPtr actor( new ImageActor );
176 // Second-phase construction of base class
179 //Create the renderer
180 actor->mRenderer = Renderer::New();
182 GeometryPtr quad = CreateGeometry( 1u, 1u, Vector2::ONE );
183 actor->mRenderer->SetGeometry( *quad );
185 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
186 actor->mRenderer->SetShader( *shader );
187 TextureSetPtr textureSet = TextureSet::New();
188 actor->mRenderer->SetTextures( *textureSet );
193 void ImageActor::OnInitialize()
195 // TODO: Remove this, at the moment its needed for size negotiation to work
196 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
199 void ImageActor::SetImage( ImagePtr& image )
203 if( mRendererIndex != INVALID_RENDERER_ID )
205 RemoveRenderer( mRendererIndex );
206 mRendererIndex = INVALID_RENDERER_ID;
211 SamplerPtr sampler = Sampler::New();
212 sampler->SetFilterMode( mMinFilter, mMagFilter );
214 TextureSet* textureSet( mRenderer->GetTextures() );
215 textureSet->SetImage( 0u, image.Get() );
216 textureSet->SetSampler( 0u, sampler );
218 if( mRendererIndex == INVALID_RENDERER_ID )
220 mRendererIndex = AddRenderer( *mRenderer );
223 if( !mIsPixelAreaSet )
225 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
233 ImagePtr ImageActor::GetImage() const
235 return mRenderer->GetTextures()->GetImage( 0u );
238 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
240 mPixelArea = pixelArea;
241 mIsPixelAreaSet = true;
247 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
252 bool ImageActor::IsPixelAreaSet() const
254 return mIsPixelAreaSet;
257 void ImageActor::ClearPixelArea()
259 mIsPixelAreaSet = false;
263 ImagePtr image = GetImage();
266 imageWidth = image->GetWidth();
267 imageHeight = image->GetHeight();
270 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
276 void ImageActor::SetStyle( Dali::ImageActor::Style style )
278 DALI_LOG_WARNING( "SetStyle Deprecated. Only STYLE_QUAD supported." );
282 Dali::ImageActor::Style ImageActor::GetStyle() const
284 DALI_LOG_WARNING( "GetStyle Deprecated. Only STYLE_QUAD supported." );
288 void ImageActor::SetNinePatchBorder( const Vector4& border )
290 DALI_LOG_WARNING( "SetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
291 mNinePatchBorder = border;
294 Vector4 ImageActor::GetNinePatchBorder() const
296 DALI_LOG_WARNING( "GetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
297 return mNinePatchBorder;
300 ImageActor::ImageActor()
301 : Actor( Actor::BASIC ),
302 mActorSize( Vector2::ZERO ),
304 mRendererIndex( INVALID_RENDERER_ID ),
305 mMinFilter( FilterMode::DEFAULT ),
306 mMagFilter( FilterMode::DEFAULT ),
307 mStyle( Dali::ImageActor::STYLE_QUAD ),
308 mIsPixelAreaSet( false )
312 ImageActor::~ImageActor()
316 Vector3 ImageActor::GetNaturalSize() const
318 Vector2 naturalSize( CalculateNaturalSize() );
319 return Vector3( naturalSize.width, naturalSize.height, 0.f );
322 Vector2 ImageActor::CalculateNaturalSize() const
324 // if no image then natural size is 0
325 Vector2 size( 0.0f, 0.0f );
327 ImagePtr image = GetImage();
330 if( IsPixelAreaSet() )
332 PixelArea area(GetPixelArea());
333 size.width = area.width;
334 size.height = area.height;
338 size = image->GetNaturalSize();
345 void ImageActor::UpdateGeometry()
347 uint16_t gridWidth = 1u;
348 uint16_t gridHeight = 1u;
352 Vector2 gridSize = mShaderEffect->GetGridSize( Vector2(mPixelArea.width, mPixelArea.height) );
354 //limit the grid size
355 gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.width) );
356 gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.height) );
359 mGridSize.SetWidth( gridWidth );
360 mGridSize.SetHeight( gridHeight );
362 GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight, mActorSize );
363 mRenderer->SetGeometry( *geometry );
365 void ImageActor::UpdateTexureRect()
367 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
369 ImagePtr image = GetImage();
370 if( mIsPixelAreaSet && image )
372 const float uScale = 1.0f / float(image->GetWidth());
373 const float vScale = 1.0f / float(image->GetHeight());
375 textureRect.x = uScale * float(mPixelArea.x);
376 textureRect.y = vScale * float(mPixelArea.y);
378 textureRect.z = uScale * float(mPixelArea.x + mPixelArea.width);
379 textureRect.w = vScale * float(mPixelArea.y + mPixelArea.height);
382 TextureSet* textureSet = mRenderer->GetTextures();
383 textureSet->RegisterProperty( TEXTURE_RECT_UNIFORM_NAME, textureRect );
386 unsigned int ImageActor::GetDefaultPropertyCount() const
388 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
391 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
393 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
395 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
397 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
398 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
400 indices.PushBack( index );
404 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
406 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
408 return Actor::IsDefaultPropertyWritable(index);
411 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
412 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
414 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
420 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
422 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
424 return Actor::IsDefaultPropertyAnimatable( index );
427 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
428 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
430 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
436 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
438 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
440 return Actor::IsDefaultPropertyAConstraintInput( index );
443 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
444 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
446 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
452 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
454 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
456 return Actor::GetDefaultPropertyType( index );
459 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
460 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
462 return DEFAULT_PROPERTY_DETAILS[index].type;
465 // index out-of-bounds
466 return Property::NONE;
469 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
471 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
473 return Actor::GetDefaultPropertyName(index);
476 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
477 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
479 return DEFAULT_PROPERTY_DETAILS[index].name;
482 // index out-of-bounds
486 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
488 Property::Index index = Property::INVALID_INDEX;
490 // Look for name in default properties
491 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
493 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
494 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
496 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
501 // If not found, check in base class
502 if( Property::INVALID_INDEX == index )
504 index = Actor::GetDefaultPropertyIndex( name );
509 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
511 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
513 Actor::SetDefaultProperty( index, propertyValue );
519 case Dali::ImageActor::Property::PIXEL_AREA:
521 SetPixelArea(propertyValue.Get<Rect<int> >());
524 case Dali::ImageActor::Property::STYLE:
529 case Dali::ImageActor::Property::BORDER:
534 case Dali::ImageActor::Property::IMAGE:
536 Dali::Image img = Scripting::NewImage( propertyValue );
539 ImagePtr image( &GetImplementation(img) );
544 DALI_LOG_WARNING("Cannot create image from property value\n");
550 DALI_LOG_WARNING("Unknown property (%d)\n", index);
558 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
561 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
563 ret = Actor::GetDefaultProperty( index );
569 case Dali::ImageActor::Property::PIXEL_AREA:
571 Rect<int> r = GetPixelArea();
575 case Dali::ImageActor::Property::STYLE:
580 case Dali::ImageActor::Property::BORDER:
585 case Dali::ImageActor::Property::IMAGE:
588 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
589 ret = Property::Value( map );
594 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
603 void ImageActor::SetSortModifier(float modifier)
605 mRenderer->SetDepthIndex( modifier );
608 float ImageActor::GetSortModifier() const
610 return mRenderer->GetDepthIndex();
613 void ImageActor::SetBlendMode( BlendingMode::Type mode )
615 mRenderer->SetBlendMode( mode );
618 BlendingMode::Type ImageActor::GetBlendMode() const
620 return mRenderer->GetBlendMode();
623 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
625 mRenderer->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
628 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
629 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
631 mRenderer->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
634 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
635 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
637 mRenderer->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
640 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
642 mRenderer->SetBlendEquation( equationRgba, equationRgba );
645 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
647 mRenderer->SetBlendEquation( equationRgb, equationAlpha );
650 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
652 mRenderer->GetBlendEquation( equationRgb, equationAlpha );
655 void ImageActor::SetBlendColor( const Vector4& color )
658 mRenderer->SetBlendColor( mBlendColor );
661 const Vector4& ImageActor::GetBlendColor() const
666 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
668 mMinFilter = minFilter;
669 mMagFilter = magFilter;
671 SamplerPtr sampler = Sampler::New();
672 sampler->SetFilterMode( minFilter, magFilter );
673 mRenderer->GetTextures()->SetSampler( 0u, sampler.Get() );
676 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
678 minFilter = mMinFilter;
679 magFilter = mMagFilter;
682 void ImageActor::SetShaderEffect( ShaderEffect& effect )
686 mShaderEffect->Disconnect( this );
689 mShaderEffect = ShaderEffectPtr( &effect );
690 effect.Connect( this );
692 ShaderPtr shader = mShaderEffect->GetShader();
693 mRenderer->SetShader( *shader );
695 EffectImageUpdated();
700 ShaderEffectPtr ImageActor::GetShaderEffect() const
702 return mShaderEffect;
705 void ImageActor::RemoveShaderEffect()
709 mShaderEffect->Disconnect( this );
710 // change to the standard shader and quad geometry
711 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
712 mRenderer->SetShader( *shader );
713 mShaderEffect.Reset();
719 void ImageActor::EffectImageUpdated()
723 Dali::Image effectImage = mShaderEffect->GetEffectImage();
726 Image& effectImageImpl = GetImplementation( effectImage );
727 mRenderer->GetTextures()->SetImage( 1u, &effectImageImpl );
731 mRenderer->GetTextures()->SetImage( 1u, 0 );
736 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
738 if( mActorSize != size )
745 void ImageActor::OnSizeSet( const Vector3& targetSize )
747 Vector2 size( targetSize.x, targetSize.y );
748 if( mActorSize != size )
755 } // namespace Internal