1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <dali/public-api/rendering/geometry.h>
21 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/common/type-abstraction-enums.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/uniform-map.h>
27 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
28 #include <dali/internal/update/common/animatable-property.h>
29 #include <dali/internal/render/data-providers/render-data-provider.h>
30 #include <dali/internal/render/renderers/render-renderer.h>
46 class SceneController;
49 typedef Dali::Vector< Renderer* > RendererContainer;
50 typedef RendererContainer::Iterator RendererIter;
51 typedef RendererContainer::ConstIterator RendererConstIter;
56 class Renderer : public PropertyOwner,
57 public UniformMapDataProvider,
58 public UniformMap::Observer,
59 public ConnectionChangePropagator::Observer
71 * Construct a new Renderer
73 static Renderer* New();
81 * Overriden delete operator
82 * Deletes the renderer from its global memory pool
84 void operator delete( void* ptr );
87 * Set the texture set for the renderer
88 * @param[in] textureSet The texture set this renderer will use
90 void SetTextures( TextureSet* textureSet );
93 * Returns current texture set object
94 * @return Pointer to the texture set
96 const TextureSet* GetTextures() const
102 * Set the shader for the renderer
103 * @param[in] shader The shader this renderer will use
105 void SetShader( Shader* shader );
108 * Get the shader used by this renderer
109 * @return the shader this renderer uses
111 const Shader& GetShader() const
117 * Set the geometry for the renderer
118 * @param[in] geometry The geometry this renderer will use
120 void SetGeometry( Render::Geometry* geometry );
123 * Set the depth index
124 * @param[in] depthIndex the new depth index to use
126 void SetDepthIndex( int depthIndex );
129 * @brief Get the depth index
130 * @return The depth index
132 int GetDepthIndex() const
138 * Set the face culling mode
139 * @param[in] faceCullingMode to use
141 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
144 * Get face culling mode
145 * @return The face culling mode
147 FaceCullingMode::Type GetFaceCullingMode() const;
150 * Set the blending mode
151 * @param[in] blendingMode to use
153 void SetBlendMode( BlendMode::Type blendingMode );
156 * Get the blending mode
157 * @return The the blending mode
159 BlendMode::Type GetBlendMode() const;
162 * Set the blending options. This should only be called from the update thread.
163 * @param[in] options A bitmask of blending options.
165 void SetBlendingOptions( unsigned int options );
168 * Get the blending options
169 * @return The the blending mode
171 unsigned int GetBlendingOptions() const;
174 * Set the blend color for blending operation
175 * @param blendColor to pass to GL
177 void SetBlendColor( const Vector4& blendColor );
180 * Get the blending color
181 * @return The blend color
183 Vector4 GetBlendColor() const;
186 * Set the index of first element for indexed draw
187 * @param[in] firstElement index of first element to draw
189 void SetIndexedDrawFirstElement( size_t firstElement );
192 * Get the index of first element for indexed draw
193 * @return The index of first element for indexed draw
195 size_t GetIndexedDrawFirstElement() const;
198 * Set the number of elements to draw by indexed draw
199 * @param[in] elementsCount number of elements to draw
201 void SetIndexedDrawElementsCount( size_t elementsCount );
204 * Get the number of elements to draw by indexed draw
205 * @return The number of elements to draw by indexed draw
207 size_t GetIndexedDrawElementsCount() const;
210 * @brief Set whether the Pre-multiplied Alpha Blending is required
211 * @param[in] preMultipled whether alpha is pre-multiplied.
213 void EnablePreMultipliedAlpha( bool preMultipled );
216 * @brief Query whether alpha is pre-multiplied.
217 * @return True is alpha is pre-multiplied, false otherwise.
219 bool IsPreMultipliedAlphaEnabled() const;
222 * Sets the depth buffer write mode
223 * @param[in] depthWriteMode The depth buffer write mode
225 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
228 * Get the depth buffer write mode
229 * @return The depth buffer write mode
231 DepthWriteMode::Type GetDepthWriteMode() const;
234 * Sets the depth buffer test mode
235 * @param[in] depthTestMode The depth buffer test mode
237 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
240 * Get the depth buffer test mode
241 * @return The depth buffer test mode
243 DepthTestMode::Type GetDepthTestMode() const;
246 * Sets the depth function
247 * @param[in] depthFunction The depth function
249 void SetDepthFunction( DepthFunction::Type depthFunction );
252 * Get the depth function
253 * @return The depth function
255 DepthFunction::Type GetDepthFunction() const;
258 * Sets the render mode
259 * @param[in] mode The render mode
261 void SetRenderMode( RenderMode::Type mode );
264 * Sets the stencil function
265 * @param[in] stencilFunction The stencil function
267 void SetStencilFunction( StencilFunction::Type stencilFunction );
270 * Sets the stencil function mask
271 * @param[in] stencilFunctionMask The stencil function mask
273 void SetStencilFunctionMask( int stencilFunctionMask );
276 * Sets the stencil function reference
277 * @param[in] stencilFunctionReference The stencil function reference
279 void SetStencilFunctionReference( int stencilFunctionReference );
282 * Sets the stencil mask
283 * @param[in] stencilMask The stencil mask
285 void SetStencilMask( int stencilMask );
288 * Sets the stencil operation for when the stencil test fails
289 * @param[in] stencilOperationOnFail The stencil operation
291 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
294 * Sets the stencil operation for when the depth test fails
295 * @param[in] stencilOperationOnZFail The stencil operation
297 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
300 * Sets the stencil operation for when the depth test passes
301 * @param[in] stencilOperationOnZPass The stencil operation
303 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
306 * Gets the stencil parameters
307 * @return The stencil parameters
309 const Render::Renderer::StencilParameters& GetStencilParameters() const;
313 * @param[in] updateBufferIndex The current update buffer index.
314 * @param[in] opacity The opacity
316 void BakeOpacity( BufferIndex updateBufferIndex, float opacity );
320 * @param[in] bufferIndex The buffer to read from.
321 * @return The opacity
323 float GetOpacity( BufferIndex updateBufferIndex ) const;
326 * Prepare the object for rendering.
327 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
328 * @param[in] updateBufferIndex The current update buffer index.
330 void PrepareRender( BufferIndex updateBufferIndex );
333 * Retrieve the Render thread renderer
334 * @return The associated render thread renderer
336 Render::Renderer& GetRenderer();
339 * Query whether the renderer is fully opaque, fully transparent or transparent.
340 * @param[in] updateBufferIndex The current update buffer index.
341 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
343 OpacityType GetOpacityType( BufferIndex updateBufferIndex, const Node& node ) const;
346 * Called by the TextureSet to notify to the renderer that it has changed
348 void TextureSetChanged();
351 * Called by the TextureSet to notify to the renderer that it is about to be deleted
353 void TextureSetDeleted();
356 * Connect the object to the scene graph
358 * @param[in] sceneController The scene controller - used for sending messages to render thread
359 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
361 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
364 * Disconnect the object from the scene graph
365 * @param[in] sceneController The scene controller - used for sending messages to render thread
366 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
368 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
370 public: // Implementation of ConnectionChangePropagator
372 * @copydoc ConnectionChangePropagator::AddObserver
374 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
377 * @copydoc ConnectionChangePropagator::RemoveObserver
379 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
381 public: // UniformMap::Observer
383 * @copydoc UniformMap::Observer::UniformMappingsChanged
385 virtual void UniformMappingsChanged( const UniformMap& mappings );
387 public: // ConnectionChangePropagator::Observer
390 * @copydoc ConnectionChangePropagator::ConnectionsChanged
392 virtual void ConnectionsChanged( PropertyOwner& owner );
395 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
397 virtual void ConnectedUniformMapChanged( );
400 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
402 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
404 public: // PropertyOwner implementation
406 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
408 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
410 public: // From UniformMapDataProvider
413 * @copydoc UniformMapDataProvider::GetUniformMapChanged
415 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
418 * @copydoc UniformMapDataProvider::GetUniformMap
420 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
425 * Protected constructor; See also Renderer::New()
430 * Update texture set to the render data provider
432 void UpdateTextureSet();
436 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
438 SceneController* mSceneController; ///< Used for initializing renderers
439 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
440 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
441 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
442 Shader* mShader; ///< The shader this renderer uses. (Not owned)
443 RenderDataProvider* mRenderDataProvider; ///< The render data provider
444 OwnerPointer< Vector4 > mBlendColor; ///< The blend color for blending operation
446 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
448 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
449 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
450 unsigned int mBlendBitmask; ///< The bitmask of blending options
451 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
452 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
454 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
455 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
456 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
457 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
458 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
460 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
461 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
465 AnimatableProperty< float > mOpacity; ///< The opacity value
466 int mDepthIndex; ///< Used only in PrepareRenderInstructions
472 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
474 typedef MessageValue1< Renderer, TextureSet* > LocalType;
476 // Reserve some memory inside the message queue
477 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
479 // Construct message in the message queue memory; note that delete should not be called on the return value
480 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
483 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
485 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
487 // Reserve some memory inside the message queue
488 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
490 // Construct message in the message queue memory; note that delete should not be called on the return value
491 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
494 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
496 typedef MessageValue1< Renderer, Shader* > LocalType;
498 // Reserve some memory inside the message queue
499 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
501 // Construct message in the message queue memory; note that delete should not be called on the return value
502 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
505 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
507 typedef MessageValue1< Renderer, int > LocalType;
509 // Reserve some memory inside the message queue
510 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
512 // Construct message in the message queue memory; note that delete should not be called on the return value
513 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
516 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
518 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
520 // Reserve some memory inside the message queue
521 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
523 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
526 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
528 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
530 // Reserve some memory inside the message queue
531 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
533 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
536 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
538 typedef MessageValue1< Renderer, unsigned int > LocalType;
540 // Reserve some memory inside the message queue
541 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
543 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
546 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
548 typedef MessageValue1< Renderer, Vector4 > LocalType;
550 // Reserve some memory inside the message queue
551 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
553 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
556 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
558 typedef MessageValue1< Renderer, size_t > LocalType;
560 // Reserve some memory inside the message queue
561 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
563 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
566 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
568 typedef MessageValue1< Renderer, size_t > LocalType;
570 // Reserve some memory inside the message queue
571 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
573 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
576 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
578 typedef MessageValue1< Renderer, bool > LocalType;
580 // Reserve some memory inside the message queue
581 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
583 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
586 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
588 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
590 // Reserve some memory inside the message queue
591 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
593 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
596 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
598 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
600 // Reserve some memory inside the message queue
601 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
603 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
606 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
608 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
610 // Reserve some memory inside the message queue
611 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
613 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
616 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
618 typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
620 // Reserve some memory inside the message queue
621 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
623 new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
626 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
628 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
630 // Reserve some memory inside the message queue
631 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
633 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
636 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
638 typedef MessageValue1< Renderer, int > LocalType;
640 // Reserve some memory inside the message queue
641 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
643 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
646 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
648 typedef MessageValue1< Renderer, int > LocalType;
650 // Reserve some memory inside the message queue
651 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
653 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
656 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
658 typedef MessageValue1< Renderer, int > LocalType;
660 // Reserve some memory inside the message queue
661 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
663 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
666 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
668 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
670 // Reserve some memory inside the message queue
671 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
673 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
676 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
678 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
680 // Reserve some memory inside the message queue
681 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
683 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
686 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
688 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
690 // Reserve some memory inside the message queue
691 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
693 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
696 inline void BakeOpacityMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, float opacity )
698 typedef MessageDoubleBuffered1< Renderer, float > LocalType;
700 // Reserve some memory inside the message queue
701 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
703 new (slot) LocalType( &renderer, &Renderer::BakeOpacity, opacity );
706 } // namespace SceneGraph
707 } // namespace Internal
710 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H