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 CreateGeometry( unsigned int gridWidth, unsigned int gridHeight )
69 std::vector< Vector2 > vertices;
70 vertices.reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
72 for( unsigned int y = 0u; y < gridHeight + 1; ++y )
74 float yPos = (float)y / gridHeight;
75 for( unsigned int x = 0u; x < gridWidth + 1; ++x )
77 float xPos = (float)x / gridWidth;
78 vertices.push_back( Vector2( xPos - 0.5f, yPos - 0.5f ) );
83 Vector< unsigned int > indices;
84 indices.Reserve( ( gridWidth + 2 ) * gridHeight * 2 - 2);
86 for( unsigned int row = 0u; row < gridHeight; ++row )
88 unsigned int rowStartIndex = row*(gridWidth+1u);
89 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
91 if( row != 0u ) // degenerate index on non-first row
93 indices.PushBack( rowStartIndex );
96 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
98 indices.PushBack( rowStartIndex + column);
99 indices.PushBack( nextRowStartIndex + column);
102 if( row != gridHeight-1u ) // degenerate index on non-last row
104 indices.PushBack( nextRowStartIndex + gridWidth );
109 Property::Map vertexFormat;
110 vertexFormat[ "aPosition" ] = Property::VECTOR2;
111 PropertyBufferPtr vertexPropertyBuffer = PropertyBuffer::New();
112 vertexPropertyBuffer->SetFormat( vertexFormat );
113 vertexPropertyBuffer->SetSize( vertices.size() );
114 if( vertices.size() > 0 )
116 vertexPropertyBuffer->SetData( &vertices[ 0 ] );
119 Property::Map indexFormat;
120 indexFormat[ "indices" ] = Property::INTEGER;
121 PropertyBufferPtr indexPropertyBuffer = PropertyBuffer::New();
122 indexPropertyBuffer->SetFormat( indexFormat );
123 indexPropertyBuffer->SetSize( indices.Size() );
124 if( indices.Size() > 0 )
126 indexPropertyBuffer->SetData( &indices[ 0 ] );
129 // Create the geometry object
130 GeometryPtr geometry = Geometry::New();
131 geometry->AddVertexBuffer( *vertexPropertyBuffer );
132 geometry->SetIndexBuffer( *indexPropertyBuffer );
133 geometry->SetGeometryType( Dali::Geometry::TRIANGLE_STRIP );
139 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
140 attribute mediump vec2 aPosition;\n
141 varying mediump vec2 vTexCoord;\n
142 uniform mediump mat4 uMvpMatrix;\n
143 uniform mediump vec3 uSize;\n
144 uniform mediump vec4 uTextureRect;\n
148 gl_Position = uMvpMatrix * vec4(aPosition*uSize.xy, 0.0, 1.0);\n
149 vTexCoord = mix( uTextureRect.xy, uTextureRect.zw, aPosition + vec2(0.5));\n
153 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
154 varying mediump vec2 vTexCoord;\n
155 uniform sampler2D sTexture;\n
156 uniform lowp vec4 uColor;\n
160 gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
164 const size_t INVALID_TEXTURE_ID = (size_t)-1;
165 const int INVALID_RENDERER_ID = -1;
166 const uint16_t MAXIMUM_GRID_SIZE = 2048;
169 ImageActorPtr ImageActor::New()
171 ImageActorPtr actor( new ImageActor );
173 // Second-phase construction of base class
176 //Create the renderer
177 actor->mRenderer = Renderer::New();
179 GeometryPtr quad = CreateGeometry( 1u, 1u );
180 actor->mRenderer->SetGeometry( *quad );
182 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
183 MaterialPtr material = Material::New();
184 material->SetShader( *shader );
185 actor->mRenderer->SetMaterial( *material );
190 void ImageActor::OnInitialize()
192 // TODO: Remove this, at the moment its needed for size negotiation to work
193 SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
196 void ImageActor::SetImage( ImagePtr& image )
200 if( mRendererIndex != INVALID_RENDERER_ID )
202 RemoveRenderer( mRendererIndex );
203 mRendererIndex = INVALID_RENDERER_ID;
208 SamplerPtr sampler = Sampler::New();
209 sampler->SetFilterMode( mMinFilter, mMagFilter );
211 mTextureIndex = mRenderer->GetMaterial()->AddTexture( image, "sTexture", sampler );
213 if( mRendererIndex == INVALID_RENDERER_ID )
215 mRendererIndex = AddRenderer( *mRenderer );
218 if( !mIsPixelAreaSet )
220 mPixelArea = PixelArea( 0, 0, image->GetWidth(), image->GetHeight() );
228 ImagePtr ImageActor::GetImage() const
230 return mRenderer->GetMaterial()->GetTexture( mTextureIndex );
233 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
235 mPixelArea = pixelArea;
236 mIsPixelAreaSet = true;
242 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
247 bool ImageActor::IsPixelAreaSet() const
249 return mIsPixelAreaSet;
252 void ImageActor::ClearPixelArea()
254 mIsPixelAreaSet = false;
258 ImagePtr image = GetImage();
261 imageWidth = image->GetWidth();
262 imageHeight = image->GetHeight();
265 mPixelArea = PixelArea( 0, 0, imageWidth, imageHeight );
271 void ImageActor::SetStyle( Dali::ImageActor::Style style )
273 DALI_LOG_WARNING( "SetStyle Deprecated. Only STYLE_QUAD supported." );
277 Dali::ImageActor::Style ImageActor::GetStyle() const
279 DALI_LOG_WARNING( "GetStyle Deprecated. Only STYLE_QUAD supported." );
283 void ImageActor::SetNinePatchBorder( const Vector4& border )
285 DALI_LOG_WARNING( "SetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
286 mNinePatchBorder = border;
289 Vector4 ImageActor::GetNinePatchBorder() const
291 DALI_LOG_WARNING( "GetNinePatchBorder Deprecated. Only STYLE_QUAD supported." );
292 return mNinePatchBorder;
295 ImageActor::ImageActor()
296 : Actor( Actor::BASIC ),
298 mRendererIndex( INVALID_RENDERER_ID ),
299 mTextureIndex( INVALID_TEXTURE_ID ),
300 mEffectTextureIndex( INVALID_TEXTURE_ID ),
301 mMinFilter( FilterMode::DEFAULT ),
302 mMagFilter( FilterMode::DEFAULT ),
303 mStyle( Dali::ImageActor::STYLE_QUAD ),
304 mIsPixelAreaSet( false )
308 ImageActor::~ImageActor()
312 Vector3 ImageActor::GetNaturalSize() const
314 Vector2 naturalSize( CalculateNaturalSize() );
315 return Vector3( naturalSize.width, naturalSize.height, 0.f );
318 Vector2 ImageActor::CalculateNaturalSize() const
320 // if no image then natural size is 0
321 Vector2 size( 0.0f, 0.0f );
323 ImagePtr image = GetImage();
326 if( IsPixelAreaSet() )
328 PixelArea area(GetPixelArea());
329 size.width = area.width;
330 size.height = area.height;
334 size = image->GetNaturalSize();
341 void ImageActor::UpdateGeometry()
343 uint16_t gridWidth = 1u;
344 uint16_t gridHeight = 1u;
348 Vector2 gridSize = mShaderEffect->GetGridSize( Vector2(mPixelArea.width, mPixelArea.height) );
350 //limit the grid size
351 gridWidth = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.width) );
352 gridHeight = std::min( MAXIMUM_GRID_SIZE, static_cast<uint16_t>(gridSize.height) );
355 if( gridWidth != mGridSize.GetWidth() || gridHeight != mGridSize.GetHeight() )
357 mGridSize.SetWidth( gridWidth );
358 mGridSize.SetHeight( gridHeight );
360 GeometryPtr geometry = CreateGeometry( gridWidth, gridHeight );
361 mRenderer->SetGeometry( *geometry );
364 void ImageActor::UpdateTexureRect()
366 Vector4 textureRect( 0.f, 0.f, 1.f, 1.f );
368 ImagePtr image = GetImage();
369 if( mIsPixelAreaSet && image )
371 const float uScale = 1.0f / float(image->GetWidth());
372 const float vScale = 1.0f / float(image->GetHeight());
374 textureRect.x = uScale * float(mPixelArea.x);
375 textureRect.y = vScale * float(mPixelArea.y);
377 textureRect.z = uScale * float(mPixelArea.x + mPixelArea.width);
378 textureRect.w = vScale * float(mPixelArea.y + mPixelArea.height);
381 Material* material = mRenderer->GetMaterial();
382 material->RegisterProperty( "uTextureRect", textureRect );
385 unsigned int ImageActor::GetDefaultPropertyCount() const
387 return Actor::GetDefaultPropertyCount() + DEFAULT_PROPERTY_COUNT;
390 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
392 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
394 indices.Reserve( indices.Size() + DEFAULT_PROPERTY_COUNT );
396 int index = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
397 for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i, ++index )
399 indices.PushBack( index );
403 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
405 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
407 return Actor::IsDefaultPropertyWritable(index);
410 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
411 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
413 return DEFAULT_PROPERTY_DETAILS[ index ].writable;
419 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
421 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
423 return Actor::IsDefaultPropertyAnimatable( index );
426 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
427 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
429 return DEFAULT_PROPERTY_DETAILS[ index ].animatable;
435 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
437 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
439 return Actor::IsDefaultPropertyAConstraintInput( index );
442 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
443 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
445 return DEFAULT_PROPERTY_DETAILS[ index ].constraintInput;
451 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
453 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
455 return Actor::GetDefaultPropertyType( index );
458 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
459 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
461 return DEFAULT_PROPERTY_DETAILS[index].type;
464 // index out-of-bounds
465 return Property::NONE;
468 const char* ImageActor::GetDefaultPropertyName( Property::Index index ) const
470 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
472 return Actor::GetDefaultPropertyName(index);
475 index -= DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
476 if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
478 return DEFAULT_PROPERTY_DETAILS[index].name;
481 // index out-of-bounds
485 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
487 Property::Index index = Property::INVALID_INDEX;
489 // Look for name in default properties
490 for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
492 const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
493 if( 0 == strcmp( name.c_str(), property->name ) ) // Don't want to convert rhs to string
495 index = i + DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX;
500 // If not found, check in base class
501 if( Property::INVALID_INDEX == index )
503 index = Actor::GetDefaultPropertyIndex( name );
508 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
510 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
512 Actor::SetDefaultProperty( index, propertyValue );
518 case Dali::ImageActor::Property::PIXEL_AREA:
520 SetPixelArea(propertyValue.Get<Rect<int> >());
523 case Dali::ImageActor::Property::STYLE:
528 case Dali::ImageActor::Property::BORDER:
533 case Dali::ImageActor::Property::IMAGE:
535 Dali::Image img = Scripting::NewImage( propertyValue );
538 ImagePtr image( &GetImplementation(img) );
543 DALI_LOG_WARNING("Cannot create image from property value\n");
549 DALI_LOG_WARNING("Unknown property (%d)\n", index);
557 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
560 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
562 ret = Actor::GetDefaultProperty( index );
568 case Dali::ImageActor::Property::PIXEL_AREA:
570 Rect<int> r = GetPixelArea();
574 case Dali::ImageActor::Property::STYLE:
579 case Dali::ImageActor::Property::BORDER:
584 case Dali::ImageActor::Property::IMAGE:
587 Scripting::CreatePropertyMap( Dali::Image( GetImage().Get() ), map );
588 ret = Property::Value( map );
593 DALI_LOG_WARNING( "Unknown property (%d)\n", index );
602 void ImageActor::SetSortModifier(float modifier)
604 mRenderer->SetDepthIndex( modifier );
607 float ImageActor::GetSortModifier() const
609 return mRenderer->GetDepthIndex();
612 void ImageActor::SetCullFace(CullFaceMode mode)
614 mRenderer->SetFaceCullingMode( static_cast< Dali::Renderer::FaceCullingMode >( mode ) );
617 CullFaceMode ImageActor::GetCullFace() const
619 return static_cast< CullFaceMode >( mRenderer->GetFaceCullingMode() );
622 void ImageActor::SetBlendMode( BlendingMode::Type mode )
624 mRenderer->SetBlendMode( mode );
627 BlendingMode::Type ImageActor::GetBlendMode() const
629 return mRenderer->GetBlendMode();
632 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
634 mRenderer->SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
637 void ImageActor::SetBlendFunc( BlendingFactor::Type srcFactorRgb, BlendingFactor::Type destFactorRgb,
638 BlendingFactor::Type srcFactorAlpha, BlendingFactor::Type destFactorAlpha )
640 mRenderer->SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
643 void ImageActor::GetBlendFunc( BlendingFactor::Type& srcFactorRgb, BlendingFactor::Type& destFactorRgb,
644 BlendingFactor::Type& srcFactorAlpha, BlendingFactor::Type& destFactorAlpha ) const
646 mRenderer->GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
649 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgba )
651 mRenderer->SetBlendEquation( equationRgba, equationRgba );
654 void ImageActor::SetBlendEquation( BlendingEquation::Type equationRgb, BlendingEquation::Type equationAlpha )
656 mRenderer->SetBlendEquation( equationRgb, equationAlpha );
659 void ImageActor::GetBlendEquation( BlendingEquation::Type& equationRgb, BlendingEquation::Type& equationAlpha ) const
661 mRenderer->GetBlendEquation( equationRgb, equationAlpha );
664 void ImageActor::SetBlendColor( const Vector4& color )
667 mRenderer->SetBlendColor( mBlendColor );
670 const Vector4& ImageActor::GetBlendColor() const
675 void ImageActor::SetFilterMode( FilterMode::Type minFilter, FilterMode::Type magFilter )
677 mMinFilter = minFilter;
678 mMagFilter = magFilter;
680 if( mTextureIndex != INVALID_TEXTURE_ID )
682 SamplerPtr sampler = Sampler::New();
683 sampler->SetFilterMode( minFilter, magFilter );
685 mRenderer->GetMaterial()->SetTextureSampler( mTextureIndex, sampler.Get() );
689 void ImageActor::GetFilterMode( FilterMode::Type& minFilter, FilterMode::Type& magFilter ) const
691 minFilter = mMinFilter;
692 magFilter = mMagFilter;
695 void ImageActor::SetShaderEffect( ShaderEffect& effect )
699 mShaderEffect->Disconnect( this );
702 mShaderEffect = ShaderEffectPtr( &effect );
703 effect.Connect( this );
705 ShaderPtr shader = mShaderEffect->GetShader();
706 mRenderer->GetMaterial()->SetShader( *shader );
708 EffectImageUpdated();
713 ShaderEffectPtr ImageActor::GetShaderEffect() const
715 return mShaderEffect;
718 void ImageActor::RemoveShaderEffect()
722 mShaderEffect->Disconnect( this );
723 // change to the standard shader and quad geometry
724 ShaderPtr shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, Dali::Shader::HINT_NONE );
725 mRenderer->GetMaterial()->SetShader( *shader );
726 mShaderEffect.Reset();
732 void ImageActor::EffectImageUpdated()
736 Dali::Image effectImage = mShaderEffect->GetEffectImage();
739 Image& effectImageImpl = GetImplementation( effectImage );
741 if( mEffectTextureIndex == INVALID_TEXTURE_ID )
743 mEffectTextureIndex = mRenderer->GetMaterial()->AddTexture( &effectImageImpl, "sEffect", NULL );
747 mRenderer->GetMaterial()->SetTextureImage( mEffectTextureIndex, &effectImageImpl );
752 if( mEffectTextureIndex != INVALID_TEXTURE_ID )
754 mRenderer->GetMaterial()->RemoveTexture( mEffectTextureIndex );
756 mEffectTextureIndex = INVALID_TEXTURE_ID;
762 } // namespace Internal