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_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
60 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
64 return Dali::BaseHandle();
67 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
69 } // unnamed namespace
71 RendererPtr Renderer::New()
73 RendererPtr rendererPtr( new Renderer() );
74 rendererPtr->Initialize();
78 void Renderer::SetGeometry( Geometry& geometry )
80 mGeometry = &geometry;
82 const Render::Geometry* geometrySceneObject = geometry.GetRenderObject();
83 SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
86 Geometry* Renderer::GetGeometry() const
88 return mGeometry.Get();
91 void Renderer::SetTextures( TextureSet& textureSet )
93 mTextureSetConnector.Set( textureSet, OnStage() );
94 const SceneGraph::TextureSet* textureSetSceneObject = textureSet.GetTextureSetSceneObject();
95 SetTexturesMessage( GetEventThreadServices(), *mSceneObject, *textureSetSceneObject );
98 TextureSet* Renderer::GetTextures() const
100 return mTextureSetConnector.Get().Get();
103 void Renderer::SetShader( Shader& shader )
106 SceneGraph::Shader& sceneGraphShader = *shader.GetShaderSceneObject();
107 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
110 Shader* Renderer::GetShader() const
112 return mShader.Get();
115 void Renderer::SetDepthIndex( int depthIndex )
117 if ( mDepthIndex != depthIndex )
119 mDepthIndex = depthIndex;
120 SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
124 int Renderer::GetDepthIndex() const
129 void Renderer::SetFaceCullingMode( FaceCullingMode::Type cullingMode )
131 if( mFaceCullingMode != cullingMode )
133 mFaceCullingMode = cullingMode;
135 SetFaceCullingModeMessage( GetEventThreadServices(), *mSceneObject, mFaceCullingMode );
139 FaceCullingMode::Type Renderer::GetFaceCullingMode()
141 return mFaceCullingMode;
144 void Renderer::SetBlendMode( BlendMode::Type mode )
146 if( mBlendMode != mode )
150 SetBlendModeMessage( GetEventThreadServices(), *mSceneObject, mBlendMode );
154 BlendMode::Type Renderer::GetBlendMode() const
159 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgba, BlendFactor::Type destFactorRgba )
161 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
162 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
165 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgb,
166 BlendFactor::Type destFactorRgb,
167 BlendFactor::Type srcFactorAlpha,
168 BlendFactor::Type destFactorAlpha )
170 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
171 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
174 void Renderer::GetBlendFunc( BlendFactor::Type& srcFactorRgb,
175 BlendFactor::Type& destFactorRgb,
176 BlendFactor::Type& srcFactorAlpha,
177 BlendFactor::Type& destFactorAlpha ) const
179 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
180 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
181 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
182 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
185 void Renderer::SetBlendEquation( BlendEquation::Type equationRgba )
187 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
188 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
191 void Renderer::SetBlendEquation( BlendEquation::Type equationRgb,
192 BlendEquation::Type equationAlpha )
194 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
195 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
198 void Renderer::GetBlendEquation( BlendEquation::Type& equationRgb,
199 BlendEquation::Type& equationAlpha ) const
201 // These are not animatable, the cached values are up-to-date.
202 equationRgb = mBlendingOptions.GetBlendEquationRgb();
203 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
206 void Renderer::SetBlendColor( const Vector4& color )
210 mBlendColor = new Vector4();
212 if( *mBlendColor != color )
214 *mBlendColor = color;
215 SetBlendColorMessage( GetEventThreadServices(), *mSceneObject, *mBlendColor );
219 Vector4 Renderer::GetBlendColor() const
225 return Color::TRANSPARENT; // GL default
228 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
230 if( firstElement != mIndexedDrawFirstElement )
232 mIndexedDrawFirstElement = firstElement;
233 SetIndexedDrawFirstElementMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawFirstElement );
237 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
239 if( elementsCount != mIndexedDrawElementCount )
241 mIndexedDrawElementCount = elementsCount;
242 SetIndexedDrawElementsCountMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawElementCount );
247 void Renderer::EnablePreMultipliedAlpha( bool preMultipled )
249 if( mPremultipledAlphaEnabled != preMultipled )
253 SetBlendFunc( BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA, BlendFactor::ONE, BlendFactor::ONE );
255 mPremultipledAlphaEnabled = preMultipled;
256 SetEnablePreMultipliedAlphaMessage( GetEventThreadServices(), *mSceneObject, mPremultipledAlphaEnabled );
260 bool Renderer::IsPreMultipliedAlphaEnabled() const
262 return mPremultipledAlphaEnabled;
265 SceneGraph::Renderer* Renderer::GetRendererSceneObject()
270 unsigned int Renderer::GetDefaultPropertyCount() const
272 return RENDERER_IMPL.GetDefaultPropertyCount();
275 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
277 RENDERER_IMPL.GetDefaultPropertyIndices( indices );
280 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
282 return RENDERER_IMPL.GetDefaultPropertyName( index );
285 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
287 return RENDERER_IMPL.GetDefaultPropertyIndex( name );
290 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
292 return RENDERER_IMPL.IsDefaultPropertyWritable( index );
295 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
297 return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
300 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
302 return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
305 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
307 return RENDERER_IMPL.GetDefaultPropertyType( index );
310 void Renderer::SetDefaultProperty( Property::Index index,
311 const Property::Value& propertyValue )
315 case Dali::Renderer::Property::DEPTH_INDEX:
317 SetDepthIndex( propertyValue.Get<int>() );
320 case Dali::Renderer::Property::FACE_CULLING_MODE:
323 if( propertyValue.Get( faceCullingMode ) )
325 SetFaceCullingMode( FaceCullingMode::Type( faceCullingMode ) );
329 case Dali::Renderer::Property::BLEND_MODE:
332 if( propertyValue.Get( blendingMode ) )
334 SetBlendMode( BlendMode::Type( blendingMode ) );
338 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
340 int blendingEquation;
341 if( propertyValue.Get( blendingEquation ) )
343 BlendEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
344 mBlendingOptions.SetBlendEquation( static_cast<BlendEquation::Type>( blendingEquation ), alphaEquation );
345 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
349 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
351 int blendingEquation;
352 if( propertyValue.Get( blendingEquation ) )
354 BlendEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
355 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendEquation::Type>( blendingEquation ) );
356 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
360 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
363 if( propertyValue.Get( blendingFactor ) )
365 BlendFactor::Type srcFactorRgb;
366 BlendFactor::Type destFactorRgb;
367 BlendFactor::Type srcFactorAlpha;
368 BlendFactor::Type destFactorAlpha;
369 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
370 SetBlendFunc( static_cast<BlendFactor::Type>( blendingFactor ),
377 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
380 if( propertyValue.Get( blendingFactor ) )
382 BlendFactor::Type srcFactorRgb;
383 BlendFactor::Type destFactorRgb;
384 BlendFactor::Type srcFactorAlpha;
385 BlendFactor::Type destFactorAlpha;
386 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
387 SetBlendFunc( srcFactorRgb,
388 static_cast<BlendFactor::Type>( blendingFactor ),
394 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
397 if( propertyValue.Get( blendingFactor ) )
399 BlendFactor::Type srcFactorRgb;
400 BlendFactor::Type destFactorRgb;
401 BlendFactor::Type srcFactorAlpha;
402 BlendFactor::Type destFactorAlpha;
403 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
404 SetBlendFunc( srcFactorRgb,
406 static_cast<BlendFactor::Type>( blendingFactor ),
411 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
414 if( propertyValue.Get( blendingFactor ) )
416 BlendFactor::Type srcFactorRgb;
417 BlendFactor::Type destFactorRgb;
418 BlendFactor::Type srcFactorAlpha;
419 BlendFactor::Type destFactorAlpha;
420 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
421 SetBlendFunc( srcFactorRgb,
424 static_cast<BlendFactor::Type>( blendingFactor ) );
428 case Dali::Renderer::Property::BLEND_COLOR:
431 if( propertyValue.Get( blendColor ) )
433 SetBlendColor( blendColor );
437 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
440 if( propertyValue.Get( preMultipled ) )
442 EnablePreMultipliedAlpha( preMultipled );
446 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
449 if( propertyValue.Get( firstElement ) )
451 SetIndexedDrawFirstElement( firstElement );
455 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
458 if( propertyValue.Get( elementsCount ) )
460 SetIndexedDrawElementsCount( elementsCount );
464 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
467 propertyValue.Get( value );
468 DepthWriteMode::Type mode = static_cast<DepthWriteMode::Type>(value);
469 if( mode != mDepthWriteMode )
471 mDepthWriteMode = mode;
472 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 );
493 void Renderer::SetSceneGraphProperty( Property::Index index,
494 const PropertyMetadata& entry,
495 const Property::Value& value )
497 RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
498 OnPropertySet(index, value);
501 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
503 Property::Value value;
506 case Dali::Renderer::Property::DEPTH_INDEX:
508 value = GetDepthIndex();
511 case Dali::Renderer::Property::FACE_CULLING_MODE:
513 value = mFaceCullingMode;
516 case Dali::Renderer::Property::BLEND_MODE:
521 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
523 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
526 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
528 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
531 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
533 BlendFactor::Type srcFactorRgb;
534 BlendFactor::Type destFactorRgb;
535 BlendFactor::Type srcFactorAlpha;
536 BlendFactor::Type destFactorAlpha;
537 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
538 value = static_cast<int>( srcFactorRgb );
541 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
543 BlendFactor::Type srcFactorRgb;
544 BlendFactor::Type destFactorRgb;
545 BlendFactor::Type srcFactorAlpha;
546 BlendFactor::Type destFactorAlpha;
547 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
548 value = static_cast<int>( destFactorRgb );
551 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
553 BlendFactor::Type srcFactorRgb;
554 BlendFactor::Type destFactorRgb;
555 BlendFactor::Type srcFactorAlpha;
556 BlendFactor::Type destFactorAlpha;
557 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
558 value = static_cast<int>( srcFactorAlpha );
561 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
563 BlendFactor::Type srcFactorRgb;
564 BlendFactor::Type destFactorRgb;
565 BlendFactor::Type srcFactorAlpha;
566 BlendFactor::Type destFactorAlpha;
567 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
568 value = static_cast<int>( destFactorAlpha );
571 case Dali::Renderer::Property::BLEND_COLOR:
575 value = *mBlendColor;
579 value = Color::TRANSPARENT;
583 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
585 value = IsPreMultipliedAlphaEnabled();
588 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
590 value = static_cast<int>( mIndexedDrawFirstElement );
593 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
595 value = static_cast<int>( mIndexedDrawElementCount );
598 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
600 value = mDepthWriteMode;
603 case Dali::Renderer::Property::DEPTH_FUNCTION:
605 value = mDepthFunction;
612 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
617 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
622 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
624 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
625 const SceneGraph::PropertyBase* property = NULL;
629 property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
631 &Renderer::FindAnimatableProperty,
632 &Renderer::FindCustomProperty,
639 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
641 const PropertyInputImpl* property = NULL;
645 const SceneGraph::PropertyBase* baseProperty =
646 RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
647 &Renderer::FindAnimatableProperty,
648 &Renderer::FindCustomProperty,
650 property = static_cast<const PropertyInputImpl*>( baseProperty );
656 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
658 return Property::INVALID_COMPONENT_INDEX;
661 bool Renderer::OnStage() const
663 return mOnStageCount > 0;
666 void Renderer::Connect()
668 if( mOnStageCount == 0 )
670 OnStageConnectMessage( GetEventThreadServices(), *mSceneObject );
671 mTextureSetConnector.OnStageConnect();
676 void Renderer::Disconnect()
679 if( mOnStageCount == 0 )
681 OnStageDisconnectMessage( GetEventThreadServices(), *mSceneObject);
682 mTextureSetConnector.OnStageDisconnect();
687 : mSceneObject (NULL ),
691 mIndexedDrawFirstElement( 0 ),
692 mIndexedDrawElementCount( 0 ),
693 mFaceCullingMode( FaceCullingMode::NONE ),
694 mBlendMode( BlendMode::AUTO ),
696 mDepthWriteMode( DepthWriteMode::AUTO ),
697 mDepthFunction( DepthFunction::LESS ),
698 mPremultipledAlphaEnabled( false )
702 void Renderer::Initialize()
704 EventThreadServices& eventThreadServices = GetEventThreadServices();
705 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
707 mSceneObject = SceneGraph::Renderer::New();
708 AddMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
710 eventThreadServices.RegisterObject( this );
713 Renderer::~Renderer()
715 if( EventThreadServices::IsCoreRunning() )
717 EventThreadServices& eventThreadServices = GetEventThreadServices();
718 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
719 RemoveMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
721 eventThreadServices.UnregisterObject( this );
725 } // namespace Internal