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_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
59 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
63 return Dali::BaseHandle();
66 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
68 } // unnamed namespace
70 RendererPtr Renderer::New()
72 RendererPtr rendererPtr( new Renderer() );
73 rendererPtr->Initialize();
77 void Renderer::SetGeometry( Geometry& geometry )
79 mGeometry = &geometry;
81 const Render::Geometry* geometrySceneObject = geometry.GetRenderObject();
82 SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
85 Geometry* Renderer::GetGeometry() const
87 return mGeometry.Get();
90 void Renderer::SetTextures( TextureSet& textureSet )
92 mTextureSetConnector.Set( textureSet, OnStage() );
93 const SceneGraph::TextureSet* textureSetSceneObject = textureSet.GetTextureSetSceneObject();
94 SetTexturesMessage( GetEventThreadServices(), *mSceneObject, *textureSetSceneObject );
97 TextureSet* Renderer::GetTextures() const
99 return mTextureSetConnector.Get().Get();
102 void Renderer::SetShader( Shader& shader )
105 SceneGraph::Shader& sceneGraphShader = *shader.GetShaderSceneObject();
106 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
109 Shader* Renderer::GetShader() const
111 return mShader.Get();
114 void Renderer::SetDepthIndex( int depthIndex )
116 if ( mDepthIndex != depthIndex )
118 mDepthIndex = depthIndex;
119 SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
123 int Renderer::GetDepthIndex() const
128 void Renderer::SetFaceCullingMode( FaceCullingMode::Type cullingMode )
130 if( mFaceCullingMode != cullingMode )
132 mFaceCullingMode = cullingMode;
134 SetFaceCullingModeMessage( GetEventThreadServices(), *mSceneObject, mFaceCullingMode );
138 FaceCullingMode::Type Renderer::GetFaceCullingMode()
140 return mFaceCullingMode;
143 void Renderer::SetBlendMode( BlendMode::Type mode )
145 if( mBlendMode != mode )
149 SetBlendModeMessage( GetEventThreadServices(), *mSceneObject, mBlendMode );
153 BlendMode::Type Renderer::GetBlendMode() const
158 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgba, BlendFactor::Type destFactorRgba )
160 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
161 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
164 void Renderer::SetBlendFunc( BlendFactor::Type srcFactorRgb,
165 BlendFactor::Type destFactorRgb,
166 BlendFactor::Type srcFactorAlpha,
167 BlendFactor::Type destFactorAlpha )
169 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
170 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
173 void Renderer::GetBlendFunc( BlendFactor::Type& srcFactorRgb,
174 BlendFactor::Type& destFactorRgb,
175 BlendFactor::Type& srcFactorAlpha,
176 BlendFactor::Type& destFactorAlpha ) const
178 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
179 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
180 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
181 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
184 void Renderer::SetBlendEquation( BlendEquation::Type equationRgba )
186 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
187 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
190 void Renderer::SetBlendEquation( BlendEquation::Type equationRgb,
191 BlendEquation::Type equationAlpha )
193 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
194 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
197 void Renderer::GetBlendEquation( BlendEquation::Type& equationRgb,
198 BlendEquation::Type& equationAlpha ) const
200 // These are not animatable, the cached values are up-to-date.
201 equationRgb = mBlendingOptions.GetBlendEquationRgb();
202 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
205 void Renderer::SetBlendColor( const Vector4& color )
209 mBlendColor = new Vector4();
211 if( *mBlendColor != color )
213 *mBlendColor = color;
214 SetBlendColorMessage( GetEventThreadServices(), *mSceneObject, *mBlendColor );
218 Vector4 Renderer::GetBlendColor() const
224 return Color::TRANSPARENT; // GL default
227 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
229 if( firstElement != mIndexedDrawFirstElement )
231 mIndexedDrawFirstElement = firstElement;
232 SetIndexedDrawFirstElementMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawFirstElement );
236 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
238 if( elementsCount != mIndexedDrawElementCount )
240 mIndexedDrawElementCount = elementsCount;
241 SetIndexedDrawElementsCountMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawElementCount );
246 void Renderer::EnablePreMultipliedAlpha( bool preMultipled )
248 if( mPremultipledAlphaEnabled != preMultipled )
252 SetBlendFunc( BlendFactor::ONE, BlendFactor::ONE_MINUS_SRC_ALPHA, BlendFactor::ONE, BlendFactor::ONE );
254 mPremultipledAlphaEnabled = preMultipled;
255 SetEnablePreMultipliedAlphaMessage( GetEventThreadServices(), *mSceneObject, mPremultipledAlphaEnabled );
259 bool Renderer::IsPreMultipliedAlphaEnabled() const
261 return mPremultipledAlphaEnabled;
264 SceneGraph::Renderer* Renderer::GetRendererSceneObject()
269 unsigned int Renderer::GetDefaultPropertyCount() const
271 return RENDERER_IMPL.GetDefaultPropertyCount();
274 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
276 RENDERER_IMPL.GetDefaultPropertyIndices( indices );
279 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
281 return RENDERER_IMPL.GetDefaultPropertyName( index );
284 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
286 return RENDERER_IMPL.GetDefaultPropertyIndex( name );
289 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
291 return RENDERER_IMPL.IsDefaultPropertyWritable( index );
294 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
296 return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
299 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
301 return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
304 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
306 return RENDERER_IMPL.GetDefaultPropertyType( index );
309 void Renderer::SetDefaultProperty( Property::Index index,
310 const Property::Value& propertyValue )
314 case Dali::Renderer::Property::DEPTH_INDEX:
316 SetDepthIndex( propertyValue.Get<int>() );
319 case Dali::Renderer::Property::FACE_CULLING_MODE:
322 if( propertyValue.Get( faceCullingMode ) )
324 SetFaceCullingMode( FaceCullingMode::Type( faceCullingMode ) );
328 case Dali::Renderer::Property::BLEND_MODE:
331 if( propertyValue.Get( blendingMode ) )
333 SetBlendMode( BlendMode::Type( blendingMode ) );
337 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
339 int blendingEquation;
340 if( propertyValue.Get( blendingEquation ) )
342 BlendEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
343 mBlendingOptions.SetBlendEquation( static_cast<BlendEquation::Type>( blendingEquation ), alphaEquation );
344 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
348 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
350 int blendingEquation;
351 if( propertyValue.Get( blendingEquation ) )
353 BlendEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
354 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendEquation::Type>( blendingEquation ) );
355 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
359 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
362 if( propertyValue.Get( blendingFactor ) )
364 BlendFactor::Type srcFactorRgb;
365 BlendFactor::Type destFactorRgb;
366 BlendFactor::Type srcFactorAlpha;
367 BlendFactor::Type destFactorAlpha;
368 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
369 SetBlendFunc( static_cast<BlendFactor::Type>( blendingFactor ),
376 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
379 if( propertyValue.Get( blendingFactor ) )
381 BlendFactor::Type srcFactorRgb;
382 BlendFactor::Type destFactorRgb;
383 BlendFactor::Type srcFactorAlpha;
384 BlendFactor::Type destFactorAlpha;
385 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
386 SetBlendFunc( srcFactorRgb,
387 static_cast<BlendFactor::Type>( blendingFactor ),
393 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
396 if( propertyValue.Get( blendingFactor ) )
398 BlendFactor::Type srcFactorRgb;
399 BlendFactor::Type destFactorRgb;
400 BlendFactor::Type srcFactorAlpha;
401 BlendFactor::Type destFactorAlpha;
402 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
403 SetBlendFunc( srcFactorRgb,
405 static_cast<BlendFactor::Type>( blendingFactor ),
410 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
413 if( propertyValue.Get( blendingFactor ) )
415 BlendFactor::Type srcFactorRgb;
416 BlendFactor::Type destFactorRgb;
417 BlendFactor::Type srcFactorAlpha;
418 BlendFactor::Type destFactorAlpha;
419 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
420 SetBlendFunc( srcFactorRgb,
423 static_cast<BlendFactor::Type>( blendingFactor ) );
427 case Dali::Renderer::Property::BLEND_COLOR:
430 if( propertyValue.Get( blendColor ) )
432 SetBlendColor( blendColor );
436 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
439 if( propertyValue.Get( preMultipled ) )
441 EnablePreMultipliedAlpha( preMultipled );
445 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
448 if( propertyValue.Get( firstElement ) )
450 SetIndexedDrawFirstElement( firstElement );
454 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
457 if( propertyValue.Get( elementsCount ) )
459 SetIndexedDrawElementsCount( elementsCount );
463 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
466 propertyValue.Get( value );
467 DepthWriteMode::Type mode = static_cast<DepthWriteMode::Type>(value);
468 if( mode != mDepthWriteMode )
470 mDepthWriteMode = mode;
471 SetDepthWriteModeMessage( GetEventThreadServices(), *mSceneObject, mode );
479 void Renderer::SetSceneGraphProperty( Property::Index index,
480 const PropertyMetadata& entry,
481 const Property::Value& value )
483 RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
484 OnPropertySet(index, value);
487 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
489 Property::Value value;
492 case Dali::Renderer::Property::DEPTH_INDEX:
494 value = GetDepthIndex();
497 case Dali::Renderer::Property::FACE_CULLING_MODE:
499 value = mFaceCullingMode;
502 case Dali::Renderer::Property::BLEND_MODE:
507 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
509 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
512 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
514 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
517 case Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB:
519 BlendFactor::Type srcFactorRgb;
520 BlendFactor::Type destFactorRgb;
521 BlendFactor::Type srcFactorAlpha;
522 BlendFactor::Type destFactorAlpha;
523 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
524 value = static_cast<int>( srcFactorRgb );
527 case Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB:
529 BlendFactor::Type srcFactorRgb;
530 BlendFactor::Type destFactorRgb;
531 BlendFactor::Type srcFactorAlpha;
532 BlendFactor::Type destFactorAlpha;
533 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
534 value = static_cast<int>( destFactorRgb );
537 case Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA:
539 BlendFactor::Type srcFactorRgb;
540 BlendFactor::Type destFactorRgb;
541 BlendFactor::Type srcFactorAlpha;
542 BlendFactor::Type destFactorAlpha;
543 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
544 value = static_cast<int>( srcFactorAlpha );
547 case Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA:
549 BlendFactor::Type srcFactorRgb;
550 BlendFactor::Type destFactorRgb;
551 BlendFactor::Type srcFactorAlpha;
552 BlendFactor::Type destFactorAlpha;
553 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
554 value = static_cast<int>( destFactorAlpha );
557 case Dali::Renderer::Property::BLEND_COLOR:
561 value = *mBlendColor;
565 value = Color::TRANSPARENT;
569 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
571 value = IsPreMultipliedAlphaEnabled();
574 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
576 value = static_cast<int>( mIndexedDrawFirstElement );
579 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
581 value = static_cast<int>( mIndexedDrawElementCount );
584 case Dali::Renderer::Property::DEPTH_WRITE_MODE:
586 value = mDepthWriteMode;
593 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
598 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
603 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
605 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
606 const SceneGraph::PropertyBase* property = NULL;
610 property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
612 &Renderer::FindAnimatableProperty,
613 &Renderer::FindCustomProperty,
620 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
622 const PropertyInputImpl* property = NULL;
626 const SceneGraph::PropertyBase* baseProperty =
627 RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
628 &Renderer::FindAnimatableProperty,
629 &Renderer::FindCustomProperty,
631 property = static_cast<const PropertyInputImpl*>( baseProperty );
637 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
639 return Property::INVALID_COMPONENT_INDEX;
642 bool Renderer::OnStage() const
644 return mOnStageCount > 0;
647 void Renderer::Connect()
649 if( mOnStageCount == 0 )
651 OnStageConnectMessage( GetEventThreadServices(), *mSceneObject );
652 mTextureSetConnector.OnStageConnect();
657 void Renderer::Disconnect()
660 if( mOnStageCount == 0 )
662 OnStageDisconnectMessage( GetEventThreadServices(), *mSceneObject);
663 mTextureSetConnector.OnStageDisconnect();
668 : mSceneObject (NULL ),
672 mIndexedDrawFirstElement( 0 ),
673 mIndexedDrawElementCount( 0 ),
674 mFaceCullingMode( FaceCullingMode::NONE ),
675 mBlendMode( BlendMode::AUTO ),
677 mDepthWriteMode( DepthWriteMode::AUTO ),
678 mPremultipledAlphaEnabled( false )
682 void Renderer::Initialize()
684 EventThreadServices& eventThreadServices = GetEventThreadServices();
685 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
687 mSceneObject = SceneGraph::Renderer::New();
688 AddMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
690 eventThreadServices.RegisterObject( this );
693 Renderer::~Renderer()
695 if( EventThreadServices::IsCoreRunning() )
697 EventThreadServices& eventThreadServices = GetEventThreadServices();
698 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
699 RemoveMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
701 eventThreadServices.UnregisterObject( this );
705 } // namespace Internal