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 );
115 Property::Map vertexFormat;
116 vertexFormat[ "aPosition" ] = Property::VECTOR3;
117 vertexFormat[ "aTexCoord" ] = Property::VECTOR2;
118 PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New();
119 vertexPropertyBuffer->SetFormat( vertexFormat );
120 vertexPropertyBuffer->SetSize( vertices.size() );
121 if( vertices.size() > 0 )
123 vertexPropertyBuffer->SetData( &vertices[ 0 ] );
126 Property::Map indexFormat;
127 indexFormat[ "indices" ] = Property::INTEGER;
128 PropertyBufferPtr indexPropertyBuffer = PropertyBuffer::New();
129 indexPropertyBuffer->SetFormat( indexFormat );
130 indexPropertyBuffer->SetSize( indices.Size() );
131 if( indices.Size() > 0 )
133 indexPropertyBuffer->SetData( &indices[ 0 ] );
136 // Create the geometry object
137 GeometryPtr geometry = Geometry::New();
138 geometry->AddVertexBuffer( *vertexPropertyBuffer );
139 geometry->SetIndexBuffer( *indexPropertyBuffer );
140 geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
146 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
147 attribute mediump vec3 aPosition;\n
148 attribute mediump vec2 aTexCoord;\n
149 varying mediump vec2 vTexCoord;\n
150 uniform mediump mat4 uMvpMatrix;\n
151 uniform mediump vec3 uSize;\n
152 uniform mediump vec4 sTextureRect;\n
156 gl_Position = uMvpMatrix * vec4(aPosition, 1.0);\n
157 vTexCoord = aTexCoord;\n
161 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
162 varying mediump vec2 vTexCoord;\n
163 uniform sampler2D sTexture;\n
164 uniform lowp vec4 uColor;\n
168 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
172 const char * const TEXTURE_RECT_UNIFORM_NAME( "sTextureRect" );
174 const size_t INVALID_TEXTURE_ID = (size_t)-1;
175 const int INVALID_RENDERER_ID = -1;
176 const uint16_t MAXIMUM_GRID_SIZE = 2048;
179 ImageActorPtr ImageActor::New()
181 ImageActorPtr actor( new ImageActor );
183 // Second-phase construction of base class
186 //Create the renderer
187 actor->mRenderer = Renderer::New();
189 GeometryPtr quad = CreateGeometry( 1u, 1u, Vector2::ONE );
190 actor->mRenderer->SetGeometry( *quad );
192 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
193 MaterialPtr material = Material::New();
194 material->SetShader( *shader );
195 actor->mRenderer->SetMaterial( *material );
200 void ImageActor::OnInitialize()
202 // TODO: Remove this, at the moment its needed for size negotiation to work
203 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
206 void ImageActor::SetImage( ImagePtr& image )
210 if( mRendererIndex != INVALID_RENDERER_ID )
212 RemoveRenderer( mRendererIndex );
213 mRendererIndex = INVALID_RENDERER_ID;
218 SamplerPtr sampler = Sampler::New();
219 sampler->SetFilterMode( mMinFilter, mMagFilter );
221 mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
223 if( mRendererIndex == INVALID_RENDERER_ID )
225 mRendererIndex = AddRenderer( *mRenderer );
228 if( !mIsPixelAreaSet )
230 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
238 ImagePtr ImageActor::GetImage() const
240 return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
243 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
245 mPixelArea = pixelArea;
246 mIsPixelAreaSet = true;
252 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
257 bool ImageActor::IsPixelAreaSet() const
259 return mIsPixelAreaSet;
262 void ImageActor::ClearPixelArea()
264 mIsPixelAreaSet = false;
268 ImagePtr image = GetImage();
271 imageWidth = image->GetWidth();
272 imageHeight = image->GetHeight();
275 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
281 void ImageActor::SetStyle( Dali::ImageActor::Style style )
283 DALI_LOG_WARNING( "SetStyle Deprecated. Only STYLE_QUAD supported." );
287 Dali::ImageActor::Style ImageActor::GetStyle() const
289 DALI_LOG_WARNING( "GetStyle Deprecated. Only STYLE_QUAD supported." );
293 void ImageActor::SetNinePatchBorder( const Vector4& border )
295 DALI_LOG_WARNING( "SetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
296 mNinePatchBorder = border;
299 Vector4 ImageActor::GetNinePatchBorder() const
301 DALI_LOG_WARNING( "GetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
302 return mNinePatchBorder;
305 ImageActor::ImageActor()
306 : Actor( Actor::BASIC ),
307 mActorSize( Vector2::ZERO ),
309 mRendererIndex( INVALID_RENDERER_ID ),
310 mTextureIndex( INVALID_TEXTURE_ID ),
311 mEffectTextureIndex( INVALID_TEXTURE_ID ),
312 mMinFilter( FilterMode::DEFAULT ),
313 mMagFilter( FilterMode::DEFAULT ),
314 mStyle( Dali::ImageActor::STYLE_QUAD ),
315 mIsPixelAreaSet( false )
319 ImageActor::~ImageActor()
323 Vector3 ImageActor::GetNaturalSize() const
325 Vector2 naturalSize( CalculateNaturalSize() );
326 return Vector3( naturalSize.width, naturalSize.height, 0.f );
329 Vector2 ImageActor::CalculateNaturalSize() const
331 // if no image then natural size is 0
332 Vector2 size( 0.0f, 0.0f );
334 ImagePtr image = GetImage();
337 if( IsPixelAreaSet() )
339 PixelArea area(GetPixelArea());
340 size.width = area.width;
341 size.height = area.height;
345 size = image->GetNaturalSize();
352 void ImageActor::UpdateGeometry()
354 uint16_t gridWidth = 1u;
355 uint16_t gridHeight = 1u;
359 Vector2 gridSize = mShaderEffect->GetGridSize( Vector2(mPixelArea.width, mPixelArea.height) );
361 //limit the grid size
362 gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.width) );
363 gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.height) );
366 mGridSize.SetWidth( gridWidth );
367 mGridSize.SetHeight( gridHeight );
369 GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight, mActorSize );
370 mRenderer->SetGeometry( *geometry );
372 void ImageActor::UpdateTexureRect()
374 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
376 ImagePtr image = GetImage();
377 if( mIsPixelAreaSet && image )
379 const float uScale = 1.0f / float(image->GetWidth());
380 const float vScale = 1.0f / float(image->GetHeight());
382 textureRect.x = uScale * float(mPixelArea.x);
383 textureRect.y = vScale * float(mPixelArea.y);
385 textureRect.z = uScale * float(mPixelArea.x + mPixelArea.width);
386 textureRect.w = vScale * float(mPixelArea.y + mPixelArea.height);
389 Material* material = mRenderer->GetMaterial();
390 material->RegisterProperty( TEXTURE_RECT_UNIFORM_NAME, textureRect );
393 unsigned int ImageActor::GetDefaultPropertyCount() const
395 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
398 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
400 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
402 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
404 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
405 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
407 indices.PushBack( index );
411 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
413 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
415 return Actor::IsDefaultPropertyWritable(index);
418 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
419 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
421 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
427 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
429 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
431 return Actor::IsDefaultPropertyAnimatable( index );
434 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
435 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
437 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
443 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
445 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
447 return Actor::IsDefaultPropertyAConstraintInput( index );
450 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
451 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
453 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
459 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
461 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
463 return Actor::GetDefaultPropertyType( index );
466 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
467 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
469 return DEFAULT_PROPERTY_DETAILS[index].type;
472 // index out-of-bounds
473 return Property::NONE;
476 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
478 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
480 return Actor::GetDefaultPropertyName(index);
483 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
484 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
486 return DEFAULT_PROPERTY_DETAILS[index].name;
489 // index out-of-bounds
493 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
495 Property::Index index = Property::INVALID_INDEX;
497 // Look for name in default properties
498 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
500 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
501 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
503 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
508 // If not found, check in base class
509 if( Property::INVALID_INDEX == index )
511 index = Actor::GetDefaultPropertyIndex( name );
516 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
518 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
520 Actor::SetDefaultProperty( index, propertyValue );
526 case Dali::ImageActor::Property::PIXEL_AREA:
528 SetPixelArea(propertyValue.Get<Rect<int> >());
531 case Dali::ImageActor::Property::STYLE:
536 case Dali::ImageActor::Property::BORDER:
541 case Dali::ImageActor::Property::IMAGE:
543 Dali::Image img = Scripting::NewImage( propertyValue );
546 ImagePtr image( &GetImplementation(img) );
551 DALI_LOG_WARNING("Cannot create image from property value\n");
557 DALI_LOG_WARNING("Unknown property (%d)\n", index);
565 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
568 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
570 ret = Actor::GetDefaultProperty( index );
576 case Dali::ImageActor::Property::PIXEL_AREA:
578 Rect<int> r = GetPixelArea();
582 case Dali::ImageActor::Property::STYLE:
587 case Dali::ImageActor::Property::BORDER:
592 case Dali::ImageActor::Property::IMAGE:
595 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
596 ret = Property::Value( map );
601 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
610 void ImageActor::SetSortModifier(float modifier)
612 mRenderer->SetDepthIndex( modifier );
615 float ImageActor::GetSortModifier() const
617 return mRenderer->GetDepthIndex();
620 void ImageActor::SetCullFace(CullFaceMode mode)
622 mRenderer->SetFaceCullingMode( static_cast< Dali::Renderer::FaceCullingMode >( mode ) );
625 CullFaceMode ImageActor::GetCullFace() const
627 return static_cast< CullFaceMode >( mRenderer->GetFaceCullingMode() );
630 void ImageActor::SetBlendMode( BlendingMode::Type mode )
632 mRenderer->SetBlendMode( mode );
635 BlendingMode::Type ImageActor::GetBlendMode() const
637 return mRenderer->GetBlendMode();
640 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
642 mRenderer->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
645 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
646 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
648 mRenderer->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
651 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
652 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
654 mRenderer->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
657 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
659 mRenderer->SetBlendEquation( equationRgba, equationRgba );
662 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
664 mRenderer->SetBlendEquation( equationRgb, equationAlpha );
667 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
669 mRenderer->GetBlendEquation( equationRgb, equationAlpha );
672 void ImageActor::SetBlendColor( const Vector4& color )
675 mRenderer->SetBlendColor( mBlendColor );
678 const Vector4& ImageActor::GetBlendColor() const
683 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
685 mMinFilter = minFilter;
686 mMagFilter = magFilter;
688 if( mTextureIndex != INVALID_TEXTURE_ID )
690 SamplerPtr sampler = Sampler::New();
691 sampler->SetFilterMode( minFilter, magFilter );
693 mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
697 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
699 minFilter = mMinFilter;
700 magFilter = mMagFilter;
703 void ImageActor::SetShaderEffect( ShaderEffect& effect )
707 mShaderEffect->Disconnect( this );
710 mShaderEffect = ShaderEffectPtr( &effect );
711 effect.Connect( this );
713 ShaderPtr shader = mShaderEffect->GetShader();
714 mRenderer->GetMaterial()->SetShader( *shader );
716 EffectImageUpdated();
721 ShaderEffectPtr ImageActor::GetShaderEffect() const
723 return mShaderEffect;
726 void ImageActor::RemoveShaderEffect()
730 mShaderEffect->Disconnect( this );
731 // change to the standard shader and quad geometry
732 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
733 mRenderer->GetMaterial()->SetShader( *shader );
734 mShaderEffect.Reset();
740 void ImageActor::EffectImageUpdated()
744 Dali::Image effectImage = mShaderEffect->GetEffectImage();
747 Image& effectImageImpl = GetImplementation( effectImage );
749 if( mEffectTextureIndex == INVALID_TEXTURE_ID )
751 mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
755 mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
760 if( mEffectTextureIndex != INVALID_TEXTURE_ID )
762 mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
764 mEffectTextureIndex = INVALID_TEXTURE_ID;
770 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
772 if( mActorSize != size )
779 void ImageActor::OnSizeSet( const Vector3& targetSize )
781 Vector2 size( targetSize.x, targetSize.y );
782 if( mActorSize != size )
789 } // namespace Internal