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 size_t INVALID_TEXTURE_ID = (size_t)-1;
169 const int INVALID_RENDERER_ID = -1;
170 const uint16_t MAXIMUM_GRID_SIZE = 2048;
173 ImageActorPtr ImageActor::New()
175 ImageActorPtr actor( new ImageActor );
177 // Second-phase construction of base class
180 //Create the renderer
181 actor->mRenderer = Renderer::New();
183 GeometryPtr quad = CreateGeometry( 1u, 1u, Vector2::ONE );
184 actor->mRenderer->SetGeometry( *quad );
186 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
187 MaterialPtr material = Material::New();
188 material->SetShader( *shader );
189 actor->mRenderer->SetMaterial( *material );
194 void ImageActor::OnInitialize()
196 // TODO: Remove this, at the moment its needed for size negotiation to work
197 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
200 void ImageActor::SetImage( ImagePtr& image )
204 if( mRendererIndex != INVALID_RENDERER_ID )
206 RemoveRenderer( mRendererIndex );
207 mRendererIndex = INVALID_RENDERER_ID;
212 SamplerPtr sampler = Sampler::New();
213 sampler->SetFilterMode( mMinFilter, mMagFilter );
215 if( mTextureIndex != INVALID_TEXTURE_ID )
217 mRenderer->GetMaterial()->SetTextureImage( mTextureIndex, image.Get() );
221 mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
224 if( mRendererIndex == INVALID_RENDERER_ID )
226 mRendererIndex = AddRenderer( *mRenderer );
229 if( !mIsPixelAreaSet )
231 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
239 ImagePtr ImageActor::GetImage() const
241 return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
244 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
246 mPixelArea = pixelArea;
247 mIsPixelAreaSet = true;
253 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
258 bool ImageActor::IsPixelAreaSet() const
260 return mIsPixelAreaSet;
263 void ImageActor::ClearPixelArea()
265 mIsPixelAreaSet = false;
269 ImagePtr image = GetImage();
272 imageWidth = image->GetWidth();
273 imageHeight = image->GetHeight();
276 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
282 void ImageActor::SetStyle( Dali::ImageActor::Style style )
284 DALI_LOG_WARNING( "SetStyle Deprecated. Only STYLE_QUAD supported." );
288 Dali::ImageActor::Style ImageActor::GetStyle() const
290 DALI_LOG_WARNING( "GetStyle Deprecated. Only STYLE_QUAD supported." );
294 void ImageActor::SetNinePatchBorder( const Vector4& border )
296 DALI_LOG_WARNING( "SetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
297 mNinePatchBorder = border;
300 Vector4 ImageActor::GetNinePatchBorder() const
302 DALI_LOG_WARNING( "GetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
303 return mNinePatchBorder;
306 ImageActor::ImageActor()
307 : Actor( Actor::BASIC ),
308 mActorSize( Vector2::ZERO ),
310 mRendererIndex( INVALID_RENDERER_ID ),
311 mTextureIndex( INVALID_TEXTURE_ID ),
312 mEffectTextureIndex( INVALID_TEXTURE_ID ),
313 mMinFilter( FilterMode::DEFAULT ),
314 mMagFilter( FilterMode::DEFAULT ),
315 mStyle( Dali::ImageActor::STYLE_QUAD ),
316 mIsPixelAreaSet( false )
320 ImageActor::~ImageActor()
324 Vector3 ImageActor::GetNaturalSize() const
326 Vector2 naturalSize( CalculateNaturalSize() );
327 return Vector3( naturalSize.width, naturalSize.height, 0.f );
330 Vector2 ImageActor::CalculateNaturalSize() const
332 // if no image then natural size is 0
333 Vector2 size( 0.0f, 0.0f );
335 ImagePtr image = GetImage();
338 if( IsPixelAreaSet() )
340 PixelArea area(GetPixelArea());
341 size.width = area.width;
342 size.height = area.height;
346 size = image->GetNaturalSize();
353 void ImageActor::UpdateGeometry()
355 uint16_t gridWidth = 1u;
356 uint16_t gridHeight = 1u;
360 Vector2 gridSize = mShaderEffect->GetGridSize( Vector2(mPixelArea.width, mPixelArea.height) );
362 //limit the grid size
363 gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.width) );
364 gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.height) );
367 mGridSize.SetWidth( gridWidth );
368 mGridSize.SetHeight( gridHeight );
370 GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight, mActorSize );
371 mRenderer->SetGeometry( *geometry );
373 void ImageActor::UpdateTexureRect()
375 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
377 ImagePtr image = GetImage();
378 if( mIsPixelAreaSet && image )
380 const float uScale = 1.0f / float(image->GetWidth());
381 const float vScale = 1.0f / float(image->GetHeight());
383 textureRect.x = uScale * float(mPixelArea.x);
384 textureRect.y = vScale * float(mPixelArea.y);
386 textureRect.z = uScale * float(mPixelArea.x + mPixelArea.width);
387 textureRect.w = vScale * float(mPixelArea.y + mPixelArea.height);
390 Material* material = mRenderer->GetMaterial();
391 material->RegisterProperty( TEXTURE_RECT_UNIFORM_NAME, textureRect );
394 unsigned int ImageActor::GetDefaultPropertyCount() const
396 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
399 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
401 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
403 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
405 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
406 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
408 indices.PushBack( index );
412 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
414 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
416 return Actor::IsDefaultPropertyWritable(index);
419 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
420 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
422 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
428 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
430 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
432 return Actor::IsDefaultPropertyAnimatable( index );
435 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
436 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
438 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
444 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
446 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
448 return Actor::IsDefaultPropertyAConstraintInput( index );
451 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
452 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
454 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
460 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
462 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
464 return Actor::GetDefaultPropertyType( index );
467 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
468 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
470 return DEFAULT_PROPERTY_DETAILS[index].type;
473 // index out-of-bounds
474 return Property::NONE;
477 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
479 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
481 return Actor::GetDefaultPropertyName(index);
484 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
485 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
487 return DEFAULT_PROPERTY_DETAILS[index].name;
490 // index out-of-bounds
494 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
496 Property::Index index = Property::INVALID_INDEX;
498 // Look for name in default properties
499 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
501 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
502 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
504 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
509 // If not found, check in base class
510 if( Property::INVALID_INDEX == index )
512 index = Actor::GetDefaultPropertyIndex( name );
517 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
519 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
521 Actor::SetDefaultProperty( index, propertyValue );
527 case Dali::ImageActor::Property::PIXEL_AREA:
529 SetPixelArea(propertyValue.Get<Rect<int> >());
532 case Dali::ImageActor::Property::STYLE:
537 case Dali::ImageActor::Property::BORDER:
542 case Dali::ImageActor::Property::IMAGE:
544 Dali::Image img = Scripting::NewImage( propertyValue );
547 ImagePtr image( &GetImplementation(img) );
552 DALI_LOG_WARNING("Cannot create image from property value\n");
558 DALI_LOG_WARNING("Unknown property (%d)\n", index);
566 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
569 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
571 ret = Actor::GetDefaultProperty( index );
577 case Dali::ImageActor::Property::PIXEL_AREA:
579 Rect<int> r = GetPixelArea();
583 case Dali::ImageActor::Property::STYLE:
588 case Dali::ImageActor::Property::BORDER:
593 case Dali::ImageActor::Property::IMAGE:
596 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
597 ret = Property::Value( map );
602 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
611 void ImageActor::SetSortModifier(float modifier)
613 mRenderer->SetDepthIndex( modifier );
616 float ImageActor::GetSortModifier() const
618 return mRenderer->GetDepthIndex();
621 void ImageActor::SetBlendMode( BlendingMode::Type mode )
623 mRenderer->SetBlendMode( mode );
626 BlendingMode::Type ImageActor::GetBlendMode() const
628 return mRenderer->GetBlendMode();
631 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
633 mRenderer->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
636 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
637 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
639 mRenderer->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
642 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
643 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
645 mRenderer->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
648 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
650 mRenderer->SetBlendEquation( equationRgba, equationRgba );
653 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
655 mRenderer->SetBlendEquation( equationRgb, equationAlpha );
658 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
660 mRenderer->GetBlendEquation( equationRgb, equationAlpha );
663 void ImageActor::SetBlendColor( const Vector4& color )
666 mRenderer->SetBlendColor( mBlendColor );
669 const Vector4& ImageActor::GetBlendColor() const
674 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
676 mMinFilter = minFilter;
677 mMagFilter = magFilter;
679 if( mTextureIndex != INVALID_TEXTURE_ID )
681 SamplerPtr sampler = Sampler::New();
682 sampler->SetFilterMode( minFilter, magFilter );
684 mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
688 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
690 minFilter = mMinFilter;
691 magFilter = mMagFilter;
694 void ImageActor::SetShaderEffect( ShaderEffect& effect )
698 mShaderEffect->Disconnect( this );
701 mShaderEffect = ShaderEffectPtr( &effect );
702 effect.Connect( this );
704 ShaderPtr shader = mShaderEffect->GetShader();
705 mRenderer->GetMaterial()->SetShader( *shader );
707 EffectImageUpdated();
712 ShaderEffectPtr ImageActor::GetShaderEffect() const
714 return mShaderEffect;
717 void ImageActor::RemoveShaderEffect()
721 mShaderEffect->Disconnect( this );
722 // change to the standard shader and quad geometry
723 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
724 mRenderer->GetMaterial()->SetShader( *shader );
725 mShaderEffect.Reset();
731 void ImageActor::EffectImageUpdated()
735 Dali::Image effectImage = mShaderEffect->GetEffectImage();
738 Image& effectImageImpl = GetImplementation( effectImage );
740 if( mEffectTextureIndex == INVALID_TEXTURE_ID )
742 mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
746 mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
751 if( mEffectTextureIndex != INVALID_TEXTURE_ID )
753 mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
755 mEffectTextureIndex = INVALID_TEXTURE_ID;
761 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
763 if( mActorSize != size )
770 void ImageActor::OnSizeSet( const Vector3& targetSize )
772 Vector2 size( targetSize.x, targetSize.y );
773 if( mActorSize != size )
780 } // namespace Internal