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>
29 #include <dali/internal/render/renderers/render-geometry.h>
40 * |name |type |writable|animatable|constraint-input|enum for index-checking|
42 DALI_PROPERTY_TABLE_BEGIN
43 DALI_PROPERTY( "depthIndex", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_INDEX )
44 DALI_PROPERTY( "faceCullingMode", INTEGER, true, false, false, Dali::Renderer::Property::FACE_CULLING_MODE )
45 DALI_PROPERTY( "blendMode", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_MODE )
46 DALI_PROPERTY( "blendEquationRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_EQUATION_RGB )
47 DALI_PROPERTY( "blendEquationAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_EQUATION_ALPHA )
48 DALI_PROPERTY( "blendFactorSrcRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB )
49 DALI_PROPERTY( "blendFactorDestRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB )
50 DALI_PROPERTY( "blendFactorSrcAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA )
51 DALI_PROPERTY( "blendFactorDestAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA )
52 DALI_PROPERTY( "blendColor", VECTOR4, true, false, false, Dali::Renderer::Property::BLEND_COLOR )
53 DALI_PROPERTY( "blendPreMultipliedAlpha", BOOLEAN, true, false, false, Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA )
54 DALI_PROPERTY( "indexRangeFirst", INTEGER, true, false, false, Dali::Renderer::Property::INDEX_RANGE_FIRST )
55 DALI_PROPERTY( "indexRangeCount", INTEGER, true, false, false, Dali::Renderer::Property::INDEX_RANGE_COUNT )
56 DALI_PROPERTY( "depthWriteMode", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_WRITE_MODE )
57 DALI_PROPERTY( "depthFunction", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_FUNCTION )
58 DALI_PROPERTY( "depthTestMode", INTEGER, true, false, false, Dali::Renderer::Property::DEPTH_TEST_MODE )
59 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
61 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
65 return Dali::BaseHandle();
68 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
70 } // unnamed namespace
72 RendererPtr Renderer::New()
74 RendererPtr rendererPtr( new Renderer() );
75 rendererPtr->Initialize();
79 void Renderer::SetGeometry( Geometry& geometry )
81 mGeometry = &geometry;
83 const Render::Geometry* geometrySceneObject = geometry.GetRenderObject();
84 SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
87 Geometry* Renderer::GetGeometry() const
89 return mGeometry.Get();
92 void Renderer::SetTextures( TextureSet& textureSet )
94 mTextureSetConnector.Set( textureSet, OnStage() );
95 const SceneGraph::TextureSet* textureSetSceneObject = textureSet.GetTextureSetSceneObject();
96 SetTexturesMessage( GetEventThreadServices(), *mSceneObject, *textureSetSceneObject );
99 TextureSet* Renderer::GetTextures() const
101 return mTextureSetConnector.Get().Get();
104 void Renderer::SetShader( Shader& shader )
107 SceneGraph::Shader& sceneGraphShader = *shader.GetShaderSceneObject();
108 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
111 Shader* Renderer::GetShader() const
113 return mShader.Get();
116 void Renderer::SetDepthIndex( int depthIndex )
118 if ( mDepthIndex != depthIndex )
120 mDepthIndex = depthIndex;
121 SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
125 int Renderer::GetDepthIndex() const
130 void Renderer::SetFaceCullingMode( FaceCullingMode::Type cullingMode )
132 if( mFaceCullingMode != cullingMode )
134 mFaceCullingMode = cullingMode;
136 SetFaceCullingModeMessage( GetEventThreadServices(), *mSceneObject, mFaceCullingMode );
140 FaceCullingMode::Type Renderer::GetFaceCullingMode()
142 return mFaceCullingMode;
145 void Renderer::SetBlendMode( BlendMode::Type mode )
147 if( mBlendMode != mode )
151 SetBlendModeMessage( GetEventThreadServices(), *mSceneObject, mBlendMode );
155 BlendMode::Type Renderer::GetBlendMode() const
160 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgba, BlendFactor::Type destFactorRgba )
162 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
163 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
166 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgb,
167 BlendFactor::Type destFactorRgb,
168 BlendFactor::Type srcFactorAlpha,
169 BlendFactor::Type destFactorAlpha )
171 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
172 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
175 void Renderer::GetBlendFunc( BlendFactor::Type& srcFactorRgb,
176 BlendFactor::Type& destFactorRgb,
177 BlendFactor::Type& srcFactorAlpha,
178 BlendFactor::Type& destFactorAlpha ) const
180 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
181 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
182 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
183 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
186 void Renderer::SetBlendEquation( BlendEquation::Type equationRgba )
188 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
189 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
192 void Renderer::SetBlendEquation( BlendEquation::Type equationRgb,
193 BlendEquation::Type equationAlpha )
195 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
196 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
199 void Renderer::GetBlendEquation( BlendEquation::Type& equationRgb,
200 BlendEquation::Type& equationAlpha ) const
202 // These are not animatable, the cached values are up-to-date.
203 equationRgb = mBlendingOptions.GetBlendEquationRgb();
204 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
207 void Renderer::SetBlendColor( const Vector4& color )
211 mBlendColor = new Vector4();
213 if( *mBlendColor != color )
215 *mBlendColor = color;
216 SetBlendColorMessage( GetEventThreadServices(), *mSceneObject, *mBlendColor );
220 Vector4 Renderer::GetBlendColor() const
226 return Color::TRANSPARENT; // GL default
229 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
231 if( firstElement != mIndexedDrawFirstElement )
233 mIndexedDrawFirstElement = firstElement;
234 SetIndexedDrawFirstElementMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawFirstElement );
238 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
240 if( elementsCount != mIndexedDrawElementCount )
242 mIndexedDrawElementCount = elementsCount;
243 SetIndexedDrawElementsCountMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawElementCount );
248 void Renderer::EnablePreMultipliedAlpha( bool preMultipled )
250 if( mPremultipledAlphaEnabled != preMultipled )
254 SetBlendFunc( BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA, BlendFactor::ONE, BlendFactor::ONE );
256 mPremultipledAlphaEnabled = preMultipled;
257 SetEnablePreMultipliedAlphaMessage( GetEventThreadServices(), *mSceneObject, mPremultipledAlphaEnabled );
261 bool Renderer::IsPreMultipliedAlphaEnabled() const
263 return mPremultipledAlphaEnabled;
266 SceneGraph::Renderer* Renderer::GetRendererSceneObject()
271 unsigned int Renderer::GetDefaultPropertyCount() const
273 return RENDERER_IMPL.GetDefaultPropertyCount();
276 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
278 RENDERER_IMPL.GetDefaultPropertyIndices( indices );
281 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
283 return RENDERER_IMPL.GetDefaultPropertyName( index );
286 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
288 return RENDERER_IMPL.GetDefaultPropertyIndex( name );
291 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
293 return RENDERER_IMPL.IsDefaultPropertyWritable( index );
296 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
298 return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
301 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
303 return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
306 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
308 return RENDERER_IMPL.GetDefaultPropertyType( index );
311 void Renderer::SetDefaultProperty( Property::Index index,
312 const Property::Value& propertyValue )
316 case Dali::Renderer::Property::DEPTH_INDEX:
318 SetDepthIndex( propertyValue.Get<int>() );
321 case Dali::Renderer::Property::FACE_CULLING_MODE:
324 if( propertyValue.Get( faceCullingMode ) )
326 SetFaceCullingMode( FaceCullingMode::Type( faceCullingMode ) );
330 case Dali::Renderer::Property::BLEND_MODE:
333 if( propertyValue.Get( blendingMode ) )
335 SetBlendMode( BlendMode::Type( blendingMode ) );
339 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
341 int blendingEquation;
342 if( propertyValue.Get( blendingEquation ) )
344 BlendEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
345 mBlendingOptions.SetBlendEquation( static_cast<BlendEquation::Type>( blendingEquation ), alphaEquation );
346 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
350 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
352 int blendingEquation;
353 if( propertyValue.Get( blendingEquation ) )
355 BlendEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
356 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendEquation::Type>( blendingEquation ) );
357 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
361 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
364 if( propertyValue.Get( blendingFactor ) )
366 BlendFactor::Type srcFactorRgb;
367 BlendFactor::Type destFactorRgb;
368 BlendFactor::Type srcFactorAlpha;
369 BlendFactor::Type destFactorAlpha;
370 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
371 SetBlendFunc( static_cast<BlendFactor::Type>( blendingFactor ),
378 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
381 if( propertyValue.Get( blendingFactor ) )
383 BlendFactor::Type srcFactorRgb;
384 BlendFactor::Type destFactorRgb;
385 BlendFactor::Type srcFactorAlpha;
386 BlendFactor::Type destFactorAlpha;
387 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
388 SetBlendFunc( srcFactorRgb,
389 static_cast<BlendFactor::Type>( blendingFactor ),
395 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
398 if( propertyValue.Get( blendingFactor ) )
400 BlendFactor::Type srcFactorRgb;
401 BlendFactor::Type destFactorRgb;
402 BlendFactor::Type srcFactorAlpha;
403 BlendFactor::Type destFactorAlpha;
404 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
405 SetBlendFunc( srcFactorRgb,
407 static_cast<BlendFactor::Type>( blendingFactor ),
412 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
415 if( propertyValue.Get( blendingFactor ) )
417 BlendFactor::Type srcFactorRgb;
418 BlendFactor::Type destFactorRgb;
419 BlendFactor::Type srcFactorAlpha;
420 BlendFactor::Type destFactorAlpha;
421 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
422 SetBlendFunc( srcFactorRgb,
425 static_cast<BlendFactor::Type>( blendingFactor ) );
429 case Dali::Renderer::Property::BLEND_COLOR:
432 if( propertyValue.Get( blendColor ) )
434 SetBlendColor( blendColor );
438 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
441 if( propertyValue.Get( preMultipled ) )
443 EnablePreMultipliedAlpha( preMultipled );
447 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
450 if( propertyValue.Get( firstElement ) )
452 SetIndexedDrawFirstElement( firstElement );
456 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
459 if( propertyValue.Get( elementsCount ) )
461 SetIndexedDrawElementsCount( elementsCount );
465 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
468 propertyValue.Get( value );
469 DepthWriteMode::Type mode = static_cast<DepthWriteMode::Type>(value);
470 if( mode != mDepthWriteMode )
472 mDepthWriteMode = mode;
473 SetDepthWriteModeMessage( GetEventThreadServices(), *mSceneObject, mode );
477 case Dali::Renderer::Property::DEPTH_FUNCTION:
480 propertyValue.Get( value );
481 DepthFunction::Type depthFunction = static_cast<DepthFunction::Type>(value);
482 if( depthFunction != mDepthFunction )
484 mDepthFunction = depthFunction;
485 SetDepthFunctionMessage( GetEventThreadServices(), *mSceneObject, depthFunction );
489 case Dali::Renderer::Property::DEPTH_TEST_MODE:
492 propertyValue.Get( value );
493 DepthTestMode::Type mode = static_cast<DepthTestMode::Type>(value);
494 if( mode != mDepthTestMode )
496 mDepthTestMode = mode;
497 SetDepthTestModeMessage( GetEventThreadServices(), *mSceneObject, mode );
504 void Renderer::SetSceneGraphProperty( Property::Index index,
505 const PropertyMetadata& entry,
506 const Property::Value& value )
508 RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
509 OnPropertySet(index, value);
512 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
514 Property::Value value;
517 case Dali::Renderer::Property::DEPTH_INDEX:
519 value = GetDepthIndex();
522 case Dali::Renderer::Property::FACE_CULLING_MODE:
524 value = mFaceCullingMode;
527 case Dali::Renderer::Property::BLEND_MODE:
532 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
534 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
537 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
539 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
542 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
544 BlendFactor::Type srcFactorRgb;
545 BlendFactor::Type destFactorRgb;
546 BlendFactor::Type srcFactorAlpha;
547 BlendFactor::Type destFactorAlpha;
548 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
549 value = static_cast<int>( srcFactorRgb );
552 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
554 BlendFactor::Type srcFactorRgb;
555 BlendFactor::Type destFactorRgb;
556 BlendFactor::Type srcFactorAlpha;
557 BlendFactor::Type destFactorAlpha;
558 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
559 value = static_cast<int>( destFactorRgb );
562 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
564 BlendFactor::Type srcFactorRgb;
565 BlendFactor::Type destFactorRgb;
566 BlendFactor::Type srcFactorAlpha;
567 BlendFactor::Type destFactorAlpha;
568 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
569 value = static_cast<int>( srcFactorAlpha );
572 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
574 BlendFactor::Type srcFactorRgb;
575 BlendFactor::Type destFactorRgb;
576 BlendFactor::Type srcFactorAlpha;
577 BlendFactor::Type destFactorAlpha;
578 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
579 value = static_cast<int>( destFactorAlpha );
582 case Dali::Renderer::Property::BLEND_COLOR:
586 value = *mBlendColor;
590 value = Color::TRANSPARENT;
594 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
596 value = IsPreMultipliedAlphaEnabled();
599 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
601 value = static_cast<int>( mIndexedDrawFirstElement );
604 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
606 value = static_cast<int>( mIndexedDrawElementCount );
609 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
611 value = mDepthWriteMode;
614 case Dali::Renderer::Property::DEPTH_FUNCTION:
616 value = mDepthFunction;
619 case Dali::Renderer::Property::DEPTH_TEST_MODE:
621 value = mDepthTestMode;
628 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
633 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
638 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
640 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
641 const SceneGraph::PropertyBase* property = NULL;
645 property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
647 &Renderer::FindAnimatableProperty,
648 &Renderer::FindCustomProperty,
655 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
657 const PropertyInputImpl* property = NULL;
661 const SceneGraph::PropertyBase* baseProperty =
662 RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
663 &Renderer::FindAnimatableProperty,
664 &Renderer::FindCustomProperty,
666 property = static_cast<const PropertyInputImpl*>( baseProperty );
672 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
674 return Property::INVALID_COMPONENT_INDEX;
677 bool Renderer::OnStage() const
679 return mOnStageCount > 0;
682 void Renderer::Connect()
684 if( mOnStageCount == 0 )
686 OnStageConnectMessage( GetEventThreadServices(), *mSceneObject );
687 mTextureSetConnector.OnStageConnect();
692 void Renderer::Disconnect()
695 if( mOnStageCount == 0 )
697 OnStageDisconnectMessage( GetEventThreadServices(), *mSceneObject);
698 mTextureSetConnector.OnStageDisconnect();
703 : mSceneObject (NULL ),
707 mIndexedDrawFirstElement( 0 ),
708 mIndexedDrawElementCount( 0 ),
709 mFaceCullingMode( FaceCullingMode::NONE ),
710 mBlendMode( BlendMode::AUTO ),
712 mDepthWriteMode( DepthWriteMode::AUTO ),
713 mDepthFunction( DepthFunction::LESS ),
714 mDepthTestMode( DepthTestMode::AUTO ),
715 mPremultipledAlphaEnabled( false )
719 void Renderer::Initialize()
721 EventThreadServices& eventThreadServices = GetEventThreadServices();
722 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
724 mSceneObject = SceneGraph::Renderer::New();
725 AddMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
727 eventThreadServices.RegisterObject( this );
730 Renderer::~Renderer()
732 if( EventThreadServices::IsCoreRunning() )
734 EventThreadServices& eventThreadServices = GetEventThreadServices();
735 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
736 RemoveMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
738 eventThreadServices.UnregisterObject( this );
742 } // namespace Internal