2 * Copyright (c) 2015 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/effects/material-impl.h> // Dali::Internal::Material
22 #include <dali/public-api/shader-effects/material.h> // Dali::Internal::Material
23 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
24 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
25 #include <dali/internal/update/effects/scene-graph-material.h>
26 #include <dali/internal/update/effects/scene-graph-sampler.h>
27 #include <dali/internal/update/manager/update-manager.h>
38 * |name |type |writable|animatable|constraint-input|enum for index-checking|
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "color", VECTOR4, true, true, true, Dali::Material::Property::COLOR )
42 DALI_PROPERTY( "face-culling-mode", STRING, true, false, true, Dali::Material::Property::FACE_CULLING_MODE )
43 DALI_PROPERTY( "blending-mode", STRING, true, false, true, Dali::Material::Property::BLENDING_MODE )
44 DALI_PROPERTY( "blend-equation", STRING, true, false, true, Dali::Material::Property::BLEND_EQUATION )
45 DALI_PROPERTY( "source-blend-factor-rgb", STRING, true, false, true, Dali::Material::Property::BLENDING_SRC_FACTOR_RGB )
46 DALI_PROPERTY( "destination-blend-factor-rgb", STRING, true, false, true, Dali::Material::Property::BLENDING_DEST_FACTOR_RGB )
47 DALI_PROPERTY( "source-blend-factor-alpha", STRING, true, false, true, Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA )
48 DALI_PROPERTY( "destination-blend-factor-alpha", STRING, true, false, true, Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA )
49 DALI_PROPERTY( "blend-color", VECTOR4, true, true, true, Dali::Material::Property::BLEND_COLOR )
50 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
52 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> MATERIAL_IMPL = { DEFAULT_PROPERTY_DETAILS };
54 } // unnamed namespace
56 MaterialPtr Material::New()
58 MaterialPtr material( new Material() );
59 material->Initialize();
63 void Material::SetShader( Shader& shader )
65 DALI_ASSERT_DEBUG( mSceneObject )
66 mShaderConnector.Set( shader, OnStage() );
68 const SceneGraph::Shader& sceneGraphShader = dynamic_cast<const SceneGraph::Shader&>( *shader.GetSceneObject() );
69 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
72 void Material::AddSampler( Sampler& sampler )
74 SamplerConnector connector;
75 connector.Set( sampler, OnStage() );
76 mSamplerConnectors.push_back( connector );
78 const SceneGraph::Sampler& sceneGraphSampler = dynamic_cast<const SceneGraph::Sampler&>( *sampler.GetSceneObject() );
79 SceneGraph::AddSamplerMessage( GetEventThreadServices(), *mSceneObject, sceneGraphSampler );
82 std::size_t Material::GetNumberOfSamplers() const
84 return mSamplerConnectors.size();
87 void Material::RemoveSampler( std::size_t index )
89 mSamplerConnectors.erase( mSamplerConnectors.begin() + index );
92 void Material::SetFaceCullingMode( Dali::Material::FaceCullingMode cullingMode )
94 if( NULL != mSceneObject )
96 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mFaceCullingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, static_cast<int>(cullingMode) );
100 void Material::SetBlendMode( BlendingMode::Type mode )
103 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
106 BlendingMode::Type Material::GetBlendMode() const
109 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
110 return BlendingMode::AUTO;
113 void Material::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
116 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
119 void Material::SetBlendFunc( BlendingFactor::Type srcFactorRgb,
120 BlendingFactor::Type destFactorRgb,
121 BlendingFactor::Type srcFactorAlpha,
122 BlendingFactor::Type destFactorAlpha )
125 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
128 void Material::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,
129 BlendingFactor::Type& destFactorRgb,
130 BlendingFactor::Type& srcFactorAlpha,
131 BlendingFactor::Type& destFactorAlpha ) const
134 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
137 void Material::SetBlendEquation( BlendingEquation::Type equationRgba )
140 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
143 void Material::SetBlendEquation( BlendingEquation::Type equationRgb,
144 BlendingEquation::Type equationAlpha )
147 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
150 void Material::GetBlendEquation( BlendingEquation::Type& equationRgb,
151 BlendingEquation::Type& equationAlpha ) const
154 DALI_ASSERT_ALWAYS( false && "TODO: MESH_REWORK" );
157 void Material::SetBlendColor( const Vector4& color )
161 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, color );
165 const Vector4& Material::GetBlendColor() const
167 return mSceneObject->mBlendColor[ GetEventThreadServices().GetEventBufferIndex() ];
170 const SceneGraph::Material* Material::GetMaterialSceneObject() const
175 unsigned int Material::GetDefaultPropertyCount() const
177 return MATERIAL_IMPL.GetDefaultPropertyCount();
180 void Material::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
182 MATERIAL_IMPL.GetDefaultPropertyIndices( indices );
185 const char* Material::GetDefaultPropertyName(Property::Index index) const
187 return MATERIAL_IMPL.GetDefaultPropertyName( index );
190 Property::Index Material::GetDefaultPropertyIndex( const std::string& name ) const
192 return MATERIAL_IMPL.GetDefaultPropertyIndex( name );
195 bool Material::IsDefaultPropertyWritable( Property::Index index ) const
197 return MATERIAL_IMPL.IsDefaultPropertyWritable( index );
200 bool Material::IsDefaultPropertyAnimatable( Property::Index index ) const
202 return MATERIAL_IMPL.IsDefaultPropertyAnimatable( index );
205 bool Material::IsDefaultPropertyAConstraintInput( Property::Index index ) const
207 return MATERIAL_IMPL.IsDefaultPropertyAConstraintInput( index );
210 Property::Type Material::GetDefaultPropertyType( Property::Index index ) const
212 return MATERIAL_IMPL.GetDefaultPropertyType( index );
215 void Material::SetDefaultProperty( Property::Index index,
216 const Property::Value& propertyValue )
220 case Dali::Material::Property::COLOR:
222 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, propertyValue.Get<Vector4>() );
225 case Dali::Material::Property::FACE_CULLING_MODE:
227 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mFaceCullingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
230 case Dali::Material::Property::BLENDING_MODE:
232 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
235 case Dali::Material::Property::BLEND_EQUATION:
237 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
240 case Dali::Material::Property::BLENDING_SRC_FACTOR_RGB:
242 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
245 case Dali::Material::Property::BLENDING_DEST_FACTOR_RGB:
247 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
250 case Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA:
252 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
255 case Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA:
257 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
260 case Dali::Material::Property::BLEND_COLOR:
262 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, propertyValue.Get<Vector4>() );
268 void Material::SetSceneGraphProperty( Property::Index index,
269 const PropertyMetadata& entry,
270 const Property::Value& value )
272 MATERIAL_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
273 OnPropertySet(index, value);
276 Property::Value Material::GetDefaultProperty( Property::Index index ) const
278 BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
279 Property::Value value;
283 case Dali::Material::Property::COLOR:
287 value = mSceneObject->mColor[bufferIndex];
291 case Dali::Material::Property::FACE_CULLING_MODE:
295 value = mSceneObject->mFaceCullingMode[bufferIndex];
299 case Dali::Material::Property::BLENDING_MODE:
301 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
304 case Dali::Material::Property::BLEND_EQUATION:
306 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
309 case Dali::Material::Property::BLENDING_SRC_FACTOR_RGB:
311 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
314 case Dali::Material::Property::BLENDING_DEST_FACTOR_RGB:
316 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
319 case Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA:
321 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
324 case Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA:
326 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
329 case Dali::Material::Property::BLEND_COLOR:
333 value = mSceneObject->mBlendColor[bufferIndex];
342 const SceneGraph::PropertyOwner* Material::GetPropertyOwner() const
347 const SceneGraph::PropertyOwner* Material::GetSceneObject() const
352 const SceneGraph::PropertyBase* Material::GetSceneObjectAnimatableProperty( Property::Index index ) const
354 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
356 const SceneGraph::PropertyBase* property = NULL;
360 property = MATERIAL_IMPL.GetRegisteredSceneGraphProperty( this,
361 &Material::FindAnimatableProperty,
362 &Material::FindCustomProperty,
365 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
369 case Dali::Material::Property::COLOR:
371 property = &mSceneObject->mColor;
374 case Dali::Material::Property::BLEND_COLOR:
376 property = &mSceneObject->mBlendColor;
381 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable");
391 const PropertyInputImpl* Material::GetSceneObjectInputProperty( Property::Index index ) const
393 const PropertyInputImpl* property = NULL;
397 const SceneGraph::PropertyBase* baseProperty =
398 MATERIAL_IMPL.GetRegisteredSceneGraphProperty( this,
399 &Material::FindAnimatableProperty,
400 &Material::FindCustomProperty,
402 property = static_cast<const PropertyInputImpl*>( baseProperty );
404 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
408 case Dali::Material::Property::COLOR:
410 property = &mSceneObject->mColor;
413 case Dali::Material::Property::FACE_CULLING_MODE:
415 property = &mSceneObject->mFaceCullingMode;
418 case Dali::Material::Property::BLENDING_MODE:
420 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
423 case Dali::Material::Property::BLEND_EQUATION:
425 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
428 case Dali::Material::Property::BLENDING_SRC_FACTOR_RGB:
430 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
433 case Dali::Material::Property::BLENDING_DEST_FACTOR_RGB:
435 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
438 case Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA:
440 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
443 case Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA:
445 DALI_ASSERT_ALWAYS( 0 && "Mesh Rework" );
448 case Dali::Material::Property::BLEND_COLOR:
450 property = &mSceneObject->mBlendColor;
455 DALI_ASSERT_ALWAYS( 0 && "Property cannot be a constraint input");
465 int Material::GetPropertyComponentIndex( Property::Index index ) const
467 return Property::INVALID_COMPONENT_INDEX;
470 bool Material::OnStage() const
475 void Material::Connect()
479 SamplerConnectorContainer::const_iterator end = mSamplerConnectors.end();
480 for( SamplerConnectorContainer::iterator it = mSamplerConnectors.begin();
484 it->OnStageConnect();
488 void Material::Disconnect()
492 SamplerConnectorContainer::const_iterator end = mSamplerConnectors.end();
493 for( SamplerConnectorContainer::iterator it = mSamplerConnectors.begin();
497 it->OnStageDisconnect();
502 : mSceneObject( NULL ),
507 void Material::Initialize()
509 EventThreadServices& eventThreadServices = GetEventThreadServices();
510 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
512 DALI_ASSERT_ALWAYS( EventThreadServices::IsCoreRunning() && "Core is not running" );
514 mSceneObject = new SceneGraph::Material();
515 AddMessage( updateManager, updateManager.GetMaterialOwner(), *mSceneObject );
518 Material::~Material()
520 if( EventThreadServices::IsCoreRunning() )
522 EventThreadServices& eventThreadServices = GetEventThreadServices();
523 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
524 RemoveMessage( updateManager, updateManager.GetMaterialOwner(), *mSceneObject );
528 } // namespace Internal