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/rendering/renderer-impl.h> // Dali::Internal::Renderer
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/rendering/renderer.h> // Dali::Renderer
24 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
25 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
26 #include <dali/internal/event/common/property-input-impl.h>
27 #include <dali/internal/update/rendering/scene-graph-renderer.h>
28 #include <dali/internal/update/manager/update-manager.h>
39 * |name |type |writable|animatable|constraint-input|enum for index-checking|
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "depthIndex", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_INDEX )
43 DALI_PROPERTY( "faceCullingMode", INTEGER, true, false, false, Dali::Renderer::Property::FACE_CULLING_MODE )
44 DALI_PROPERTY( "blendingMode", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_MODE )
45 DALI_PROPERTY( "blendEquationRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_EQUATION_RGB )
46 DALI_PROPERTY( "blendEquationAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_EQUATION_ALPHA )
47 DALI_PROPERTY( "sourceBlendFactorRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB )
48 DALI_PROPERTY( "destinationBlendFactorRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB )
49 DALI_PROPERTY( "sourceBlendFactorAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA )
50 DALI_PROPERTY( "destinationBlendFactorAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA )
51 DALI_PROPERTY( "blendingColor", VECTOR4, true, false, false, Dali::Renderer::Property::BLENDING_COLOR )
52 DALI_PROPERTY( "blendPreMultipliedAlpha", BOOLEAN, true, false, false, Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA )
53 DALI_PROPERTY( "indexRangeFirst", INTEGER, true, false, false, Dali::Renderer::Property::INDEX_RANGE_FIRST )
54 DALI_PROPERTY( "indexRangeCount", INTEGER, true, false, false, Dali::Renderer::Property::INDEX_RANGE_COUNT )
55 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
57 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
61 return Dali::BaseHandle();
64 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
66 } // unnamed namespace
68 RendererPtr Renderer::New()
70 RendererPtr rendererPtr( new Renderer() );
71 rendererPtr->Initialize();
75 void Renderer::SetGeometry( Geometry& geometry )
77 mGeometryConnector.Set( geometry, OnStage() );
78 const SceneGraph::Geometry* geometrySceneObject = geometry.GetGeometrySceneObject();
80 SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
83 Geometry* Renderer::GetGeometry() const
85 return mGeometryConnector.Get().Get();
88 void Renderer::SetTextures( TextureSet& textureSet )
90 mTextureSetConnector.Set( textureSet, OnStage() );
91 const SceneGraph::TextureSet* textureSetSceneObject = textureSet.GetTextureSetSceneObject();
92 SetTexturesMessage( GetEventThreadServices(), *mSceneObject, *textureSetSceneObject );
95 TextureSet* Renderer::GetTextures() const
97 return mTextureSetConnector.Get().Get();
100 void Renderer::SetShader( Shader& shader )
103 SceneGraph::Shader& sceneGraphShader = *shader.GetShaderSceneObject();
104 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
107 Shader* Renderer::GetShader() const
109 return mShader.Get();
112 void Renderer::SetDepthIndex( int depthIndex )
114 if ( mDepthIndex != depthIndex )
116 mDepthIndex = depthIndex;
117 SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
121 int Renderer::GetDepthIndex() const
126 void Renderer::SetFaceCullingMode( Dali::Renderer::FaceCullingMode cullingMode )
128 if( mFaceCullingMode != cullingMode )
130 mFaceCullingMode = cullingMode;
132 SetFaceCullingModeMessage( GetEventThreadServices(), *mSceneObject, mFaceCullingMode );
136 Dali::Renderer::FaceCullingMode Renderer::GetFaceCullingMode()
138 return mFaceCullingMode;
141 void Renderer::SetBlendMode( BlendingMode::Type mode )
143 if( mBlendingMode != mode )
145 mBlendingMode = mode;
147 SetBlendingModeMessage( GetEventThreadServices(), *mSceneObject, mBlendingMode );
151 BlendingMode::Type Renderer::GetBlendMode() const
153 return mBlendingMode;
156 void Renderer::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
158 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
159 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
162 void Renderer::SetBlendFunc( BlendingFactor::Type srcFactorRgb,
163 BlendingFactor::Type destFactorRgb,
164 BlendingFactor::Type srcFactorAlpha,
165 BlendingFactor::Type destFactorAlpha )
167 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
168 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
171 void Renderer::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,
172 BlendingFactor::Type& destFactorRgb,
173 BlendingFactor::Type& srcFactorAlpha,
174 BlendingFactor::Type& destFactorAlpha ) const
176 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
177 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
178 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
179 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
182 void Renderer::SetBlendEquation( BlendingEquation::Type equationRgba )
184 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
185 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
188 void Renderer::SetBlendEquation( BlendingEquation::Type equationRgb,
189 BlendingEquation::Type equationAlpha )
191 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
192 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
195 void Renderer::GetBlendEquation( BlendingEquation::Type& equationRgb,
196 BlendingEquation::Type& equationAlpha ) const
198 // These are not animatable, the cached values are up-to-date.
199 equationRgb = mBlendingOptions.GetBlendEquationRgb();
200 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
203 void Renderer::SetBlendColor( const Vector4& color )
207 mBlendColor = new Vector4();
209 if( *mBlendColor != color )
211 *mBlendColor = color;
212 SetBlendColorMessage( GetEventThreadServices(), *mSceneObject, *mBlendColor );
216 Vector4 Renderer::GetBlendColor() const
222 return Color::TRANSPARENT; // GL default
225 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
227 if( firstElement != mIndexedDrawFirstElement )
229 mIndexedDrawFirstElement = firstElement;
230 SetIndexedDrawFirstElementMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawFirstElement );
234 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
236 if( elementsCount != mIndexedDrawElementCount )
238 mIndexedDrawElementCount = elementsCount;
239 SetIndexedDrawElementsCountMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawElementCount );
244 void Renderer::EnablePreMultipliedAlpha( bool preMultipled )
246 if( mPremultipledAlphaEnabled != preMultipled )
250 SetBlendFunc( BlendingFactor::ONE, BlendingFactor::ONE_MINUS_SRC_ALPHA, BlendingFactor::ONE, BlendingFactor::ONE );
252 mPremultipledAlphaEnabled = preMultipled;
253 SetEnablePreMultipliedAlphaMessage( GetEventThreadServices(), *mSceneObject, mPremultipledAlphaEnabled );
257 bool Renderer::IsPreMultipliedAlphaEnabled() const
259 return mPremultipledAlphaEnabled;
262 SceneGraph::Renderer* Renderer::GetRendererSceneObject()
267 unsigned int Renderer::GetDefaultPropertyCount() const
269 return RENDERER_IMPL.GetDefaultPropertyCount();
272 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
274 RENDERER_IMPL.GetDefaultPropertyIndices( indices );
277 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
279 return RENDERER_IMPL.GetDefaultPropertyName( index );
282 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
284 return RENDERER_IMPL.GetDefaultPropertyIndex( name );
287 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
289 return RENDERER_IMPL.IsDefaultPropertyWritable( index );
292 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
294 return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
297 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
299 return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
302 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
304 return RENDERER_IMPL.GetDefaultPropertyType( index );
307 void Renderer::SetDefaultProperty( Property::Index index,
308 const Property::Value& propertyValue )
312 case Dali::Renderer::Property::DEPTH_INDEX:
314 SetDepthIndex( propertyValue.Get<int>() );
317 case Dali::Renderer::Property::FACE_CULLING_MODE:
320 if( propertyValue.Get( faceCullingMode ) )
322 SetFaceCullingMode( Dali::Renderer::FaceCullingMode( faceCullingMode ) );
326 case Dali::Renderer::Property::BLENDING_MODE:
329 if( propertyValue.Get( blendingMode ) )
331 SetBlendMode( BlendingMode::Type( blendingMode ) );
335 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
337 int blendingEquation;
338 if( propertyValue.Get( blendingEquation ) )
340 BlendingEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
341 mBlendingOptions.SetBlendEquation( static_cast<BlendingEquation::Type>( blendingEquation ), alphaEquation );
342 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
346 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
348 int blendingEquation;
349 if( propertyValue.Get( blendingEquation ) )
351 BlendingEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
352 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendingEquation::Type>( blendingEquation ) );
353 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
357 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB:
360 if( propertyValue.Get( blendingFactor ) )
362 BlendingFactor::Type srcFactorRgb;
363 BlendingFactor::Type destFactorRgb;
364 BlendingFactor::Type srcFactorAlpha;
365 BlendingFactor::Type destFactorAlpha;
366 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
367 SetBlendFunc( static_cast<BlendingFactor::Type>( blendingFactor ),
374 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB:
377 if( propertyValue.Get( blendingFactor ) )
379 BlendingFactor::Type srcFactorRgb;
380 BlendingFactor::Type destFactorRgb;
381 BlendingFactor::Type srcFactorAlpha;
382 BlendingFactor::Type destFactorAlpha;
383 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
384 SetBlendFunc( srcFactorRgb,
385 static_cast<BlendingFactor::Type>( blendingFactor ),
391 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA:
394 if( propertyValue.Get( blendingFactor ) )
396 BlendingFactor::Type srcFactorRgb;
397 BlendingFactor::Type destFactorRgb;
398 BlendingFactor::Type srcFactorAlpha;
399 BlendingFactor::Type destFactorAlpha;
400 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
401 SetBlendFunc( srcFactorRgb,
403 static_cast<BlendingFactor::Type>( blendingFactor ),
408 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA:
411 if( propertyValue.Get( blendingFactor ) )
413 BlendingFactor::Type srcFactorRgb;
414 BlendingFactor::Type destFactorRgb;
415 BlendingFactor::Type srcFactorAlpha;
416 BlendingFactor::Type destFactorAlpha;
417 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
418 SetBlendFunc( srcFactorRgb,
421 static_cast<BlendingFactor::Type>( blendingFactor ) );
425 case Dali::Renderer::Property::BLENDING_COLOR:
428 if( propertyValue.Get( blendColor ) )
430 SetBlendColor( blendColor );
434 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
437 if( propertyValue.Get( preMultipled ) )
439 EnablePreMultipliedAlpha( preMultipled );
443 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
446 if( propertyValue.Get( firstElement ) )
448 SetIndexedDrawFirstElement( firstElement );
452 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
455 if( propertyValue.Get( elementsCount ) )
457 SetIndexedDrawElementsCount( elementsCount );
464 void Renderer::SetSceneGraphProperty( Property::Index index,
465 const PropertyMetadata& entry,
466 const Property::Value& value )
468 RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
469 OnPropertySet(index, value);
472 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
474 Property::Value value;
477 case Dali::Renderer::Property::DEPTH_INDEX:
479 value = GetDepthIndex();
482 case Dali::Renderer::Property::FACE_CULLING_MODE:
484 value = mFaceCullingMode;
487 case Dali::Renderer::Property::BLENDING_MODE:
489 value = mBlendingMode;
492 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
494 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
497 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
499 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
502 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB:
504 BlendingFactor::Type srcFactorRgb;
505 BlendingFactor::Type destFactorRgb;
506 BlendingFactor::Type srcFactorAlpha;
507 BlendingFactor::Type destFactorAlpha;
508 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
509 value = static_cast<int>( srcFactorRgb );
512 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB:
514 BlendingFactor::Type srcFactorRgb;
515 BlendingFactor::Type destFactorRgb;
516 BlendingFactor::Type srcFactorAlpha;
517 BlendingFactor::Type destFactorAlpha;
518 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
519 value = static_cast<int>( destFactorRgb );
522 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA:
524 BlendingFactor::Type srcFactorRgb;
525 BlendingFactor::Type destFactorRgb;
526 BlendingFactor::Type srcFactorAlpha;
527 BlendingFactor::Type destFactorAlpha;
528 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
529 value = static_cast<int>( srcFactorAlpha );
532 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA:
534 BlendingFactor::Type srcFactorRgb;
535 BlendingFactor::Type destFactorRgb;
536 BlendingFactor::Type srcFactorAlpha;
537 BlendingFactor::Type destFactorAlpha;
538 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
539 value = static_cast<int>( destFactorAlpha );
542 case Dali::Renderer::Property::BLENDING_COLOR:
546 value = *mBlendColor;
550 value = Color::TRANSPARENT;
554 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
556 value = IsPreMultipliedAlphaEnabled();
559 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
561 value = static_cast<int>( mIndexedDrawFirstElement );
564 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
566 value = static_cast<int>( mIndexedDrawElementCount );
573 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
578 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
583 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
585 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
586 const SceneGraph::PropertyBase* property = NULL;
590 property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
592 &Renderer::FindAnimatableProperty,
593 &Renderer::FindCustomProperty,
600 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
602 const PropertyInputImpl* property = NULL;
606 const SceneGraph::PropertyBase* baseProperty =
607 RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
608 &Renderer::FindAnimatableProperty,
609 &Renderer::FindCustomProperty,
611 property = static_cast<const PropertyInputImpl*>( baseProperty );
617 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
619 return Property::INVALID_COMPONENT_INDEX;
622 bool Renderer::OnStage() const
624 return mOnStageCount > 0;
627 void Renderer::Connect()
629 if( mOnStageCount == 0 )
631 OnStageConnectMessage( GetEventThreadServices(), *mSceneObject );
632 mGeometryConnector.OnStageConnect();
633 mTextureSetConnector.OnStageConnect();
638 void Renderer::Disconnect()
641 if( mOnStageCount == 0 )
643 OnStageDisconnectMessage( GetEventThreadServices(), *mSceneObject);
644 mGeometryConnector.OnStageDisconnect();
645 mTextureSetConnector.OnStageDisconnect();
650 : mSceneObject (NULL ),
654 mIndexedDrawFirstElement( 0 ),
655 mIndexedDrawElementCount( 0 ),
656 mFaceCullingMode( Dali::Renderer::NONE ),
657 mBlendingMode( Dali::BlendingMode::AUTO ),
659 mPremultipledAlphaEnabled( false )
663 void Renderer::Initialize()
665 EventThreadServices& eventThreadServices = GetEventThreadServices();
666 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
668 mSceneObject = SceneGraph::Renderer::New();
669 AddMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
671 eventThreadServices.RegisterObject( this );
674 Renderer::~Renderer()
676 if( EventThreadServices::IsCoreRunning() )
678 EventThreadServices& eventThreadServices = GetEventThreadServices();
679 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
680 RemoveMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
682 eventThreadServices.UnregisterObject( this );
686 } // namespace Internal