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( "blendingMode", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_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( "sourceBlendFactorRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB )
49 DALI_PROPERTY( "destinationBlendFactorRgb", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB )
50 DALI_PROPERTY( "sourceBlendFactorAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA )
51 DALI_PROPERTY( "destinationBlendFactorAlpha", INTEGER, true, false, false, Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA )
52 DALI_PROPERTY( "blendingColor", VECTOR4, true, false, false, Dali::Renderer::Property::BLENDING_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_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
58 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> RENDERER_IMPL = { DEFAULT_PROPERTY_DETAILS };
62 return Dali::BaseHandle();
65 TypeRegistration mType( typeid( Dali::Renderer ), typeid( Dali::Handle ), Create );
67 } // unnamed namespace
69 RendererPtr Renderer::New()
71 RendererPtr rendererPtr( new Renderer() );
72 rendererPtr->Initialize();
76 void Renderer::SetGeometry( Geometry& geometry )
78 mGeometry = &geometry;
80 const Render::Geometry* geometrySceneObject = geometry.GetRenderObject();
81 SetGeometryMessage( GetEventThreadServices(), *mSceneObject, *geometrySceneObject );
84 Geometry* Renderer::GetGeometry() const
86 return mGeometry.Get();
89 void Renderer::SetTextures( TextureSet& textureSet )
91 mTextureSetConnector.Set( textureSet, OnStage() );
92 const SceneGraph::TextureSet* textureSetSceneObject = textureSet.GetTextureSetSceneObject();
93 SetTexturesMessage( GetEventThreadServices(), *mSceneObject, *textureSetSceneObject );
96 TextureSet* Renderer::GetTextures() const
98 return mTextureSetConnector.Get().Get();
101 void Renderer::SetShader( Shader& shader )
104 SceneGraph::Shader& sceneGraphShader = *shader.GetShaderSceneObject();
105 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
108 Shader* Renderer::GetShader() const
110 return mShader.Get();
113 void Renderer::SetDepthIndex( int depthIndex )
115 if ( mDepthIndex != depthIndex )
117 mDepthIndex = depthIndex;
118 SetDepthIndexMessage( GetEventThreadServices(), *mSceneObject, depthIndex );
122 int Renderer::GetDepthIndex() const
127 void Renderer::SetFaceCullingMode( Dali::Renderer::FaceCullingMode cullingMode )
129 if( mFaceCullingMode != cullingMode )
131 mFaceCullingMode = cullingMode;
133 SetFaceCullingModeMessage( GetEventThreadServices(), *mSceneObject, mFaceCullingMode );
137 Dali::Renderer::FaceCullingMode Renderer::GetFaceCullingMode()
139 return mFaceCullingMode;
142 void Renderer::SetBlendMode( BlendingMode::Type mode )
144 if( mBlendingMode != mode )
146 mBlendingMode = mode;
148 SetBlendingModeMessage( GetEventThreadServices(), *mSceneObject, mBlendingMode );
152 BlendingMode::Type Renderer::GetBlendMode() const
154 return mBlendingMode;
157 void Renderer::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
159 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
160 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
163 void Renderer::SetBlendFunc( BlendingFactor::Type srcFactorRgb,
164 BlendingFactor::Type destFactorRgb,
165 BlendingFactor::Type srcFactorAlpha,
166 BlendingFactor::Type destFactorAlpha )
168 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
169 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
172 void Renderer::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,
173 BlendingFactor::Type& destFactorRgb,
174 BlendingFactor::Type& srcFactorAlpha,
175 BlendingFactor::Type& destFactorAlpha ) const
177 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
178 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
179 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
180 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
183 void Renderer::SetBlendEquation( BlendingEquation::Type equationRgba )
185 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
186 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
189 void Renderer::SetBlendEquation( BlendingEquation::Type equationRgb,
190 BlendingEquation::Type equationAlpha )
192 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
193 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
196 void Renderer::GetBlendEquation( BlendingEquation::Type& equationRgb,
197 BlendingEquation::Type& equationAlpha ) const
199 // These are not animatable, the cached values are up-to-date.
200 equationRgb = mBlendingOptions.GetBlendEquationRgb();
201 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
204 void Renderer::SetBlendColor( const Vector4& color )
208 mBlendColor = new Vector4();
210 if( *mBlendColor != color )
212 *mBlendColor = color;
213 SetBlendColorMessage( GetEventThreadServices(), *mSceneObject, *mBlendColor );
217 Vector4 Renderer::GetBlendColor() const
223 return Color::TRANSPARENT; // GL default
226 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
228 if( firstElement != mIndexedDrawFirstElement )
230 mIndexedDrawFirstElement = firstElement;
231 SetIndexedDrawFirstElementMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawFirstElement );
235 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
237 if( elementsCount != mIndexedDrawElementCount )
239 mIndexedDrawElementCount = elementsCount;
240 SetIndexedDrawElementsCountMessage( GetEventThreadServices(), *mSceneObject, mIndexedDrawElementCount );
245 void Renderer::EnablePreMultipliedAlpha( bool preMultipled )
247 if( mPremultipledAlphaEnabled != preMultipled )
251 SetBlendFunc( BlendingFactor::ONE, BlendingFactor::ONE_MINUS_SRC_ALPHA, BlendingFactor::ONE, BlendingFactor::ONE );
253 mPremultipledAlphaEnabled = preMultipled;
254 SetEnablePreMultipliedAlphaMessage( GetEventThreadServices(), *mSceneObject, mPremultipledAlphaEnabled );
258 bool Renderer::IsPreMultipliedAlphaEnabled() const
260 return mPremultipledAlphaEnabled;
263 SceneGraph::Renderer* Renderer::GetRendererSceneObject()
268 unsigned int Renderer::GetDefaultPropertyCount() const
270 return RENDERER_IMPL.GetDefaultPropertyCount();
273 void Renderer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
275 RENDERER_IMPL.GetDefaultPropertyIndices( indices );
278 const char* Renderer::GetDefaultPropertyName(Property::Index index) const
280 return RENDERER_IMPL.GetDefaultPropertyName( index );
283 Property::Index Renderer::GetDefaultPropertyIndex( const std::string& name ) const
285 return RENDERER_IMPL.GetDefaultPropertyIndex( name );
288 bool Renderer::IsDefaultPropertyWritable( Property::Index index ) const
290 return RENDERER_IMPL.IsDefaultPropertyWritable( index );
293 bool Renderer::IsDefaultPropertyAnimatable( Property::Index index ) const
295 return RENDERER_IMPL.IsDefaultPropertyAnimatable( index );
298 bool Renderer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
300 return RENDERER_IMPL.IsDefaultPropertyAConstraintInput( index );
303 Property::Type Renderer::GetDefaultPropertyType( Property::Index index ) const
305 return RENDERER_IMPL.GetDefaultPropertyType( index );
308 void Renderer::SetDefaultProperty( Property::Index index,
309 const Property::Value& propertyValue )
313 case Dali::Renderer::Property::DEPTH_INDEX:
315 SetDepthIndex( propertyValue.Get<int>() );
318 case Dali::Renderer::Property::FACE_CULLING_MODE:
321 if( propertyValue.Get( faceCullingMode ) )
323 SetFaceCullingMode( Dali::Renderer::FaceCullingMode( faceCullingMode ) );
327 case Dali::Renderer::Property::BLENDING_MODE:
330 if( propertyValue.Get( blendingMode ) )
332 SetBlendMode( BlendingMode::Type( blendingMode ) );
336 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
338 int blendingEquation;
339 if( propertyValue.Get( blendingEquation ) )
341 BlendingEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
342 mBlendingOptions.SetBlendEquation( static_cast<BlendingEquation::Type>( blendingEquation ), alphaEquation );
343 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
347 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
349 int blendingEquation;
350 if( propertyValue.Get( blendingEquation ) )
352 BlendingEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
353 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendingEquation::Type>( blendingEquation ) );
354 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
358 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB:
361 if( propertyValue.Get( blendingFactor ) )
363 BlendingFactor::Type srcFactorRgb;
364 BlendingFactor::Type destFactorRgb;
365 BlendingFactor::Type srcFactorAlpha;
366 BlendingFactor::Type destFactorAlpha;
367 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
368 SetBlendFunc( static_cast<BlendingFactor::Type>( blendingFactor ),
375 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB:
378 if( propertyValue.Get( blendingFactor ) )
380 BlendingFactor::Type srcFactorRgb;
381 BlendingFactor::Type destFactorRgb;
382 BlendingFactor::Type srcFactorAlpha;
383 BlendingFactor::Type destFactorAlpha;
384 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
385 SetBlendFunc( srcFactorRgb,
386 static_cast<BlendingFactor::Type>( blendingFactor ),
392 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA:
395 if( propertyValue.Get( blendingFactor ) )
397 BlendingFactor::Type srcFactorRgb;
398 BlendingFactor::Type destFactorRgb;
399 BlendingFactor::Type srcFactorAlpha;
400 BlendingFactor::Type destFactorAlpha;
401 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
402 SetBlendFunc( srcFactorRgb,
404 static_cast<BlendingFactor::Type>( blendingFactor ),
409 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA:
412 if( propertyValue.Get( blendingFactor ) )
414 BlendingFactor::Type srcFactorRgb;
415 BlendingFactor::Type destFactorRgb;
416 BlendingFactor::Type srcFactorAlpha;
417 BlendingFactor::Type destFactorAlpha;
418 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
419 SetBlendFunc( srcFactorRgb,
422 static_cast<BlendingFactor::Type>( blendingFactor ) );
426 case Dali::Renderer::Property::BLENDING_COLOR:
429 if( propertyValue.Get( blendColor ) )
431 SetBlendColor( blendColor );
435 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
438 if( propertyValue.Get( preMultipled ) )
440 EnablePreMultipliedAlpha( preMultipled );
444 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
447 if( propertyValue.Get( firstElement ) )
449 SetIndexedDrawFirstElement( firstElement );
453 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
456 if( propertyValue.Get( elementsCount ) )
458 SetIndexedDrawElementsCount( elementsCount );
465 void Renderer::SetSceneGraphProperty( Property::Index index,
466 const PropertyMetadata& entry,
467 const Property::Value& value )
469 RENDERER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
470 OnPropertySet(index, value);
473 Property::Value Renderer::GetDefaultProperty( Property::Index index ) const
475 Property::Value value;
478 case Dali::Renderer::Property::DEPTH_INDEX:
480 value = GetDepthIndex();
483 case Dali::Renderer::Property::FACE_CULLING_MODE:
485 value = mFaceCullingMode;
488 case Dali::Renderer::Property::BLENDING_MODE:
490 value = mBlendingMode;
493 case Dali::Renderer::Property::BLEND_EQUATION_RGB:
495 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
498 case Dali::Renderer::Property::BLEND_EQUATION_ALPHA:
500 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
503 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_RGB:
505 BlendingFactor::Type srcFactorRgb;
506 BlendingFactor::Type destFactorRgb;
507 BlendingFactor::Type srcFactorAlpha;
508 BlendingFactor::Type destFactorAlpha;
509 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
510 value = static_cast<int>( srcFactorRgb );
513 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_RGB:
515 BlendingFactor::Type srcFactorRgb;
516 BlendingFactor::Type destFactorRgb;
517 BlendingFactor::Type srcFactorAlpha;
518 BlendingFactor::Type destFactorAlpha;
519 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
520 value = static_cast<int>( destFactorRgb );
523 case Dali::Renderer::Property::BLENDING_SRC_FACTOR_ALPHA:
525 BlendingFactor::Type srcFactorRgb;
526 BlendingFactor::Type destFactorRgb;
527 BlendingFactor::Type srcFactorAlpha;
528 BlendingFactor::Type destFactorAlpha;
529 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
530 value = static_cast<int>( srcFactorAlpha );
533 case Dali::Renderer::Property::BLENDING_DEST_FACTOR_ALPHA:
535 BlendingFactor::Type srcFactorRgb;
536 BlendingFactor::Type destFactorRgb;
537 BlendingFactor::Type srcFactorAlpha;
538 BlendingFactor::Type destFactorAlpha;
539 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
540 value = static_cast<int>( destFactorAlpha );
543 case Dali::Renderer::Property::BLENDING_COLOR:
547 value = *mBlendColor;
551 value = Color::TRANSPARENT;
555 case Dali::Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA:
557 value = IsPreMultipliedAlphaEnabled();
560 case Dali::Renderer::Property::INDEX_RANGE_FIRST:
562 value = static_cast<int>( mIndexedDrawFirstElement );
565 case Dali::Renderer::Property::INDEX_RANGE_COUNT:
567 value = static_cast<int>( mIndexedDrawElementCount );
574 const SceneGraph::PropertyOwner* Renderer::GetPropertyOwner() const
579 const SceneGraph::PropertyOwner* Renderer::GetSceneObject() const
584 const SceneGraph::PropertyBase* Renderer::GetSceneObjectAnimatableProperty( Property::Index index ) const
586 DALI_ASSERT_ALWAYS( IsPropertyAnimatable(index) && "Property is not animatable" );
587 const SceneGraph::PropertyBase* property = NULL;
591 property = RENDERER_IMPL.GetRegisteredSceneGraphProperty(
593 &Renderer::FindAnimatableProperty,
594 &Renderer::FindCustomProperty,
601 const PropertyInputImpl* Renderer::GetSceneObjectInputProperty( Property::Index index ) const
603 const PropertyInputImpl* property = NULL;
607 const SceneGraph::PropertyBase* baseProperty =
608 RENDERER_IMPL.GetRegisteredSceneGraphProperty( this,
609 &Renderer::FindAnimatableProperty,
610 &Renderer::FindCustomProperty,
612 property = static_cast<const PropertyInputImpl*>( baseProperty );
618 int Renderer::GetPropertyComponentIndex( Property::Index index ) const
620 return Property::INVALID_COMPONENT_INDEX;
623 bool Renderer::OnStage() const
625 return mOnStageCount > 0;
628 void Renderer::Connect()
630 if( mOnStageCount == 0 )
632 OnStageConnectMessage( GetEventThreadServices(), *mSceneObject );
633 mTextureSetConnector.OnStageConnect();
638 void Renderer::Disconnect()
641 if( mOnStageCount == 0 )
643 OnStageDisconnectMessage( GetEventThreadServices(), *mSceneObject);
644 mTextureSetConnector.OnStageDisconnect();
649 : mSceneObject (NULL ),
653 mIndexedDrawFirstElement( 0 ),
654 mIndexedDrawElementCount( 0 ),
655 mFaceCullingMode( Dali::Renderer::NONE ),
656 mBlendingMode( Dali::BlendingMode::AUTO ),
658 mPremultipledAlphaEnabled( false )
662 void Renderer::Initialize()
664 EventThreadServices& eventThreadServices = GetEventThreadServices();
665 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
667 mSceneObject = SceneGraph::Renderer::New();
668 AddMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
670 eventThreadServices.RegisterObject( this );
673 Renderer::~Renderer()
675 if( EventThreadServices::IsCoreRunning() )
677 EventThreadServices& eventThreadServices = GetEventThreadServices();
678 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
679 RemoveMessage( updateManager, updateManager.GetRendererOwner(), *mSceneObject );
681 eventThreadServices.UnregisterObject( this );
685 } // namespace Internal