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 );
173 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
174 attribute mediump vec3 aPosition;\n
175 attribute mediump vec2 aTexCoord;\n
176 varying mediump vec2 vTexCoord;\n
177 uniform mediump mat4 uMvpMatrix;\n
178 uniform mediump vec3 uSize;\n
179 uniform mediump vec4 uTextureRect;\n
183 mediump vec4 vertexPosition = vec4(aPosition, 1.0);\n
184 vertexPosition = uMvpMatrix * vertexPosition;\n
186 vTexCoord = aTexCoord;\n
187 gl_Position = vertexPosition;\n
191 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
192 varying mediump vec2 vTexCoord;\n
193 uniform sampler2D sTexture;\n
194 uniform lowp vec4 uColor;\n
198 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
202 const size_t INVALID_TEXTURE_ID = (size_t)-1;
203 const int INVALID_RENDERER_ID = -1;
204 const unsigned int MAXIMUM_GRID_SIZE = 2048;
207 ImageActorPtr ImageActor::New()
209 ImageActorPtr actor( new ImageActor );
211 // Second-phase construction of base class
214 //Create the renderer
215 actor->mRenderer = Renderer::New();
217 GeometryPtr quad = CreateQuadGeometry( Vector2::ONE, 1, 1, PixelArea() );
218 actor->mRenderer->SetGeometry( *quad );
220 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
221 MaterialPtr material = Material::New();
222 material->SetShader( *shader );
223 actor->mRenderer->SetMaterial( *material );
228 void ImageActor::OnInitialize()
230 // TODO: Remove this, at the moment its needed for size negotiation to work
231 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
234 void ImageActor::SetImage( ImagePtr& image )
238 if( mRendererIndex != INVALID_RENDERER_ID )
240 RemoveRenderer( mRendererIndex );
241 mRendererIndex = INVALID_RENDERER_ID;
246 SamplerPtr sampler = Sampler::New();
247 sampler->SetFilterMode( mMinFilter, mMagFilter );
249 mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
251 if( mRendererIndex == INVALID_RENDERER_ID )
253 mRendererIndex = AddRenderer( *mRenderer );
256 if( !mIsPixelAreaSet )
258 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
265 ImagePtr ImageActor::GetImage() const
267 return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
270 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
272 mPixelArea = pixelArea;
273 mIsPixelAreaSet = true;
278 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
283 bool ImageActor::IsPixelAreaSet() const
285 return mIsPixelAreaSet;
288 void ImageActor::ClearPixelArea()
290 mIsPixelAreaSet = false;
294 ImagePtr image = GetImage();
297 imageWidth = image->GetWidth();
298 imageHeight = image->GetHeight();
301 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
306 ImageActor::ImageActor()
307 : Actor( Actor::BASIC ),
308 mRendererIndex( INVALID_RENDERER_ID ),
309 mTextureIndex( INVALID_TEXTURE_ID ),
310 mEffectTextureIndex( INVALID_TEXTURE_ID ),
311 mMinFilter( FilterMode::DEFAULT ),
312 mMagFilter( FilterMode::DEFAULT ),
313 mIsPixelAreaSet( false )
317 ImageActor::~ImageActor()
321 Vector3 ImageActor::GetNaturalSize() const
323 Vector2 naturalSize( CalculateNaturalSize() );
324 return Vector3( naturalSize.width, naturalSize.height, 0.f );
327 Vector2 ImageActor::CalculateNaturalSize() const
329 // if no image then natural size is 0
330 Vector2 size( 0.0f, 0.0f );
332 ImagePtr image = GetImage();
335 if( IsPixelAreaSet() )
337 PixelArea area(GetPixelArea());
338 size.width = area.width;
339 size.height = area.height;
343 size = image->GetNaturalSize();
350 void ImageActor::OnRelayout( const Vector2& size, RelayoutContainer& container )
352 unsigned int gridWidth = 1;
353 unsigned int gridHeight = 1;
356 Vector2 gridSize = mShaderEffect->GetGridSize( size );
358 //limit the grid size
359 gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<unsigned int>(gridSize.width) );
360 gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<unsigned int>(gridSize.height) );
363 unsigned int imageWidth = 1u;
364 unsigned int imageHeight = 1u;
365 ImagePtr image = GetImage();
368 imageWidth = image->GetWidth();
369 imageHeight = image->GetHeight();
372 GeometryPtr geometry = gridWidth <= 1 && gridHeight <= 1 ?
373 CreateQuadGeometry( size, imageWidth, imageHeight, mPixelArea ) :
374 CreateGridGeometry( size, gridWidth, gridHeight, imageWidth, imageHeight, mPixelArea );
376 mRenderer->SetGeometry( *geometry );
378 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
379 if( mIsPixelAreaSet && image )
381 const float uScale = 1.0f / float(imageWidth);
382 const float vScale = 1.0f / float(imageHeight);
383 const float x = uScale * float(mPixelArea.x);
384 const float y = vScale * float(mPixelArea.y);
385 const float width = uScale * float(mPixelArea.width);
386 const float height = vScale * float(mPixelArea.height);
393 textureRect.z = x + width;
394 textureRect.w = y + height;
397 Material* material = mRenderer->GetMaterial();
398 material->RegisterProperty( "sTextureRect", textureRect );
401 unsigned int ImageActor::GetDefaultPropertyCount() const
403 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
406 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
408 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
410 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
412 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
413 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
415 indices.PushBack( index );
419 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
421 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
423 return Actor::IsDefaultPropertyWritable(index);
426 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
427 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
429 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
435 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
437 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
439 return Actor::IsDefaultPropertyAnimatable( index );
442 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
443 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
445 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
451 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
453 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
455 return Actor::IsDefaultPropertyAConstraintInput( index );
458 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
459 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
461 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
467 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
469 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
471 return Actor::GetDefaultPropertyType( index );
474 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
475 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
477 return DEFAULT_PROPERTY_DETAILS[index].type;
480 // index out-of-bounds
481 return Property::NONE;
484 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
486 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
488 return Actor::GetDefaultPropertyName(index);
491 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
492 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
494 return DEFAULT_PROPERTY_DETAILS[index].name;
497 // index out-of-bounds
501 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
503 Property::Index index = Property::INVALID_INDEX;
505 // Look for name in default properties
506 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
508 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
509 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
511 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
516 // If not found, check in base class
517 if( Property::INVALID_INDEX == index )
519 index = Actor::GetDefaultPropertyIndex( name );
524 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
526 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
528 Actor::SetDefaultProperty( index, propertyValue );
534 case Dali::ImageActor::Property::PIXEL_AREA:
536 SetPixelArea(propertyValue.Get<Rect<int> >());
539 case Dali::ImageActor::Property::STYLE:
544 case Dali::ImageActor::Property::BORDER:
549 case Dali::ImageActor::Property::IMAGE:
551 Dali::Image img = Scripting::NewImage( propertyValue );
554 ImagePtr image( &GetImplementation(img) );
559 DALI_LOG_WARNING("Cannot create image from property value\n");
565 DALI_LOG_WARNING("Unknown property (%d)\n", index);
573 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
576 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
578 ret = Actor::GetDefaultProperty( index );
584 case Dali::ImageActor::Property::PIXEL_AREA:
586 Rect<int> r = GetPixelArea();
590 case Dali::ImageActor::Property::STYLE:
595 case Dali::ImageActor::Property::BORDER:
600 case Dali::ImageActor::Property::IMAGE:
603 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
604 ret = Property::Value( map );
609 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
618 void ImageActor::SetSortModifier(float modifier)
620 mRenderer->SetDepthIndex( modifier );
623 float ImageActor::GetSortModifier() const
625 return mRenderer->GetDepthIndex();
628 void ImageActor::SetCullFace(CullFaceMode mode)
630 mRenderer->GetMaterial()->SetFaceCullingMode( static_cast< Dali::Material::FaceCullingMode >( mode ) );
633 CullFaceMode ImageActor::GetCullFace() const
635 return static_cast< CullFaceMode >( mRenderer->GetMaterial()->GetFaceCullingMode() );
638 void ImageActor::SetBlendMode( BlendingMode::Type mode )
640 mRenderer->GetMaterial()->SetBlendMode( mode );
643 BlendingMode::Type ImageActor::GetBlendMode() const
645 return mRenderer->GetMaterial()->GetBlendMode();
648 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
650 mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
653 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
654 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
656 mRenderer->GetMaterial()->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
659 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
660 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
662 mRenderer->GetMaterial()->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
665 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
667 mRenderer->GetMaterial()->SetBlendEquation( equationRgba, equationRgba );
670 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
672 mRenderer->GetMaterial()->SetBlendEquation( equationRgb, equationAlpha );
675 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
677 mRenderer->GetMaterial()->GetBlendEquation( equationRgb, equationAlpha );
680 void ImageActor::SetBlendColor( const Vector4& color )
683 mRenderer->GetMaterial()->SetBlendColor( mBlendColor );
686 const Vector4& ImageActor::GetBlendColor() const
691 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
693 mMinFilter = minFilter;
694 mMagFilter = magFilter;
696 if( mTextureIndex != INVALID_TEXTURE_ID )
698 SamplerPtr sampler = Sampler::New();
699 sampler->SetFilterMode( minFilter, magFilter );
701 mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
705 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
707 minFilter = mMinFilter;
708 magFilter = mMagFilter;
711 void ImageActor::SetShaderEffect( ShaderEffect& effect )
715 mShaderEffect->Disconnect( this );
718 mShaderEffect = ShaderEffectPtr( &effect );
719 effect.Connect( this );
721 ShaderPtr shader = mShaderEffect->GetShader();
722 mRenderer->GetMaterial()->SetShader( *shader );
724 EffectImageUpdated();
727 ShaderEffectPtr ImageActor::GetShaderEffect() const
729 return mShaderEffect;
732 void ImageActor::RemoveShaderEffect()
734 //if we previously had a subdivided grid then we need to reset the geometry as well
737 mShaderEffect->Disconnect( this );
739 if( mShaderEffect->GetGridSize( Vector2(INFINITY, INFINITY) ) != Vector2::ONE )
745 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
746 mRenderer->GetMaterial()->SetShader( *shader );
747 mShaderEffect.Reset();
750 void ImageActor::EffectImageUpdated()
754 Dali::Image effectImage = mShaderEffect->GetEffectImage();
757 Image& effectImageImpl = GetImplementation( effectImage );
759 if( mEffectTextureIndex == INVALID_TEXTURE_ID )
761 mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
765 mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
770 if( mEffectTextureIndex != INVALID_TEXTURE_ID )
772 mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
774 mEffectTextureIndex = INVALID_TEXTURE_ID;
777 //ensure that the sEffectRect uniform is set
778 Shader* shader = mRenderer->GetMaterial()->GetShader();
779 Property::Index index = shader->GetPropertyIndex( "sEffectRect" );
780 if( index == Property::INVALID_INDEX )
782 shader->RegisterProperty( "sEffectRect", Vector4( 0.f, 0.f, 1.f, 1.f) );
787 } // namespace Internal