1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
5 * Copyright (c) 2016 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/render/data-providers/render-data-provider.h>
29 #include <dali/internal/render/renderers/render-renderer.h>
44 class SceneController;
47 typedef Dali::Vector< Renderer* > RendererContainer;
48 typedef RendererContainer::Iterator RendererIter;
49 typedef RendererContainer::ConstIterator RendererConstIter;
54 class Renderer : public PropertyOwner,
55 public UniformMapDataProvider,
56 public UniformMap::Observer,
57 public ConnectionChangePropagator::Observer
69 * Construct a new Renderer
71 static Renderer* New();
79 * Overriden delete operator
80 * Deletes the renderer from its global memory pool
82 void operator delete( void* ptr );
85 * Set the texture set for the renderer
86 * @param[in] textureSet The texture set this renderer will use
88 void SetTextures( TextureSet* textureSet );
91 * Returns current texture set object
92 * @return Pointer to the texture set
94 const TextureSet* GetTextures() const
100 * Set the shader for the renderer
101 * @param[in] shader The shader this renderer will use
103 void SetShader( Shader* shader );
106 * Get the shader used by this renderer
107 * @return the shader this renderer uses
109 const Shader& GetShader() const
115 * Set the geometry for the renderer
116 * @param[in] geometry The geometry this renderer will use
118 void SetGeometry( Render::Geometry* geometry );
121 * Get the geometry of this renderer
122 * @return the geometry this renderer uses
124 const Render::Geometry& GetGeometry() const
130 * Set the depth index
131 * @param[in] depthIndex the new depth index to use
133 void SetDepthIndex( int depthIndex );
136 * @brief Get the depth index
137 * @return The depth index
139 int GetDepthIndex() const
145 * Set the face culling mode
146 * @param[in] faceCullingMode to use
148 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
151 * Set the blending mode
152 * @param[in] blendingMode to use
154 void SetBlendMode( BlendMode::Type blendingMode );
157 * Set the blending options. This should only be called from the update thread.
158 * @param[in] options A bitmask of blending options.
160 void SetBlendingOptions( unsigned int options );
163 * Set the blend color for blending operation
164 * @param blendColor to pass to GL
166 void SetBlendColor( const Vector4& blendColor );
169 * Set the index of first element for indexed draw
170 * @param[in] firstElement index of first element to draw
172 void SetIndexedDrawFirstElement( size_t firstElement );
175 * Set the number of elements to draw by indexed draw
176 * @param[in] elementsCount number of elements to draw
178 void SetIndexedDrawElementsCount( size_t elementsCount );
181 * @brief Set whether the Pre-multiplied Alpha Blending is required
182 * @param[in] preMultipled whether alpha is pre-multiplied.
184 void EnablePreMultipliedAlpha( bool preMultipled );
187 * Sets the depth buffer write mode
188 * @param[in] depthWriteMode The depth buffer write mode
190 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
193 * Sets the depth buffer test mode
194 * @param[in] depthTestMode The depth buffer test mode
196 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
199 * Sets the depth function
200 * @param[in] depthFunction The depth function
202 void SetDepthFunction( DepthFunction::Type depthFunction );
205 * Sets the render mode
206 * @param[in] mode The render mode
208 void SetRenderMode( RenderMode::Type mode );
211 * Sets the stencil function
212 * @param[in] stencilFunction The stencil function
214 void SetStencilFunction( StencilFunction::Type stencilFunction );
217 * Sets the stencil function mask
218 * @param[in] stencilFunctionMask The stencil function mask
220 void SetStencilFunctionMask( int stencilFunctionMask );
223 * Sets the stencil function reference
224 * @param[in] stencilFunctionReference The stencil function reference
226 void SetStencilFunctionReference( int stencilFunctionReference );
229 * Sets the stencil mask
230 * @param[in] stencilMask The stencil mask
232 void SetStencilMask( int stencilMask );
235 * Sets the stencil operation for when the stencil test fails
236 * @param[in] stencilOperationOnFail The stencil operation
238 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
241 * Sets the stencil operation for when the depth test fails
242 * @param[in] stencilOperationOnZFail The stencil operation
244 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
247 * Sets the stencil operation for when the depth test passes
248 * @param[in] stencilOperationOnZPass The stencil operation
250 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
253 * Turns on batching feature for the renderer
254 * @param[in] batchingEnabled if true, enables the batching mode for the renderer
256 void SetBatchingEnabled( bool batchingEnabled );
259 * Tests whether batching feature is enabled for this renderer
260 * @return batching state
262 bool IsBatchingEnabled() const
264 return mBatchingEnabled;
268 * Prepare the object for rendering.
269 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
270 * @param[in] updateBufferIndex The current update buffer index.
272 void PrepareRender( BufferIndex updateBufferIndex );
275 * Retrieve the Render thread renderer
276 * @return The associated render thread renderer
278 Render::Renderer& GetRenderer();
281 * Check whether the renderer has been marked as ready to render
282 * ready means that renderer has all resources and should produce correct result
283 * complete means all resources have finished loading
284 * It's possible that renderer is complete but not ready,
285 * for example in case of resource load failed
286 * @param[out] ready TRUE if the renderer has resources to render
287 * @param[out] complete TRUE if the renderer resources are complete
289 void GetReadyAndComplete( bool& ready, bool& complete ) const;
292 * Query whether the renderer is fully opaque, fully transparent or transparent.
293 * @param[in] updateBufferIndex The current update buffer index.
294 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
296 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
299 * Called by the TextureSet to notify to the renderer that it has changed
301 void TextureSetChanged();
303 public: // Implementation of ObjectOwnerContainer template methods
305 * Connect the object to the scene graph
307 * @param[in] sceneController The scene controller - used for sending messages to render thread
308 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
310 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
313 * Disconnect the object from the scene graph
314 * @param[in] sceneController The scene controller - used for sending messages to render thread
315 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
317 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
319 public: // Implementation of ConnectionChangePropagator
321 * @copydoc ConnectionChangePropagator::AddObserver
323 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
326 * @copydoc ConnectionChangePropagator::RemoveObserver
328 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
333 public: // UniformMap::Observer
335 * @copydoc UniformMap::Observer::UniformMappingsChanged
337 virtual void UniformMappingsChanged( const UniformMap& mappings );
339 public: // ConnectionChangePropagator::Observer
342 * @copydoc ConnectionChangePropagator::ConnectionsChanged
344 virtual void ConnectionsChanged( PropertyOwner& owner );
347 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
349 virtual void ConnectedUniformMapChanged( );
352 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
354 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
356 public: // PropertyOwner implementation
358 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
360 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
362 public: // From UniformMapDataProvider
365 * @copydoc UniformMapDataProvider::GetUniformMapChanged
367 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
370 * @copydoc UniformMapDataProvider::GetUniformMap
372 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
377 * Protected constructor; See also Renderer::New()
382 * Helper function to create a new render data provider
383 * @return the new (initialized) data provider
385 RenderDataProvider* NewRenderDataProvider();
389 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
390 SceneController* mSceneController; ///< Used for initializing renderers
391 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
392 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
393 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
394 Shader* mShader; ///< The shader this renderer uses. (Not owned)
395 Vector4* mBlendColor; ///< The blend color for blending operation
397 Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
399 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
400 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
401 unsigned int mBlendBitmask; ///< The bitmask of blending options
402 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
403 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
405 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
406 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
407 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
408 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
409 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
411 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
412 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
413 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
414 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
418 bool mBatchingEnabled : 1; ///< Flag indicating whether the render supports batching
419 int mDepthIndex; ///< Used only in PrepareRenderInstructions
424 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
426 typedef MessageValue1< Renderer, TextureSet* > LocalType;
428 // Reserve some memory inside the message queue
429 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
431 // Construct message in the message queue memory; note that delete should not be called on the return value
432 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
435 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
437 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
439 // Reserve some memory inside the message queue
440 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
442 // Construct message in the message queue memory; note that delete should not be called on the return value
443 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
446 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
448 typedef MessageValue1< Renderer, Shader* > LocalType;
450 // Reserve some memory inside the message queue
451 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
453 // Construct message in the message queue memory; note that delete should not be called on the return value
454 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
457 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
459 typedef MessageValue1< Renderer, int > LocalType;
461 // Reserve some memory inside the message queue
462 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
464 // Construct message in the message queue memory; note that delete should not be called on the return value
465 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
468 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
470 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
472 // Reserve some memory inside the message queue
473 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
475 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
478 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
480 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
482 // Reserve some memory inside the message queue
483 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
485 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
488 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
490 typedef MessageValue1< Renderer, unsigned int > LocalType;
492 // Reserve some memory inside the message queue
493 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
495 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
498 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
500 typedef MessageValue1< Renderer, Vector4 > LocalType;
502 // Reserve some memory inside the message queue
503 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
505 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
508 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
510 typedef MessageValue1< Renderer, size_t > LocalType;
512 // Reserve some memory inside the message queue
513 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
515 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
518 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
520 typedef MessageValue1< Renderer, size_t > LocalType;
522 // Reserve some memory inside the message queue
523 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
525 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
528 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
530 typedef MessageValue1< Renderer, bool > LocalType;
532 // Reserve some memory inside the message queue
533 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
535 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
538 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
540 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
542 // Reserve some memory inside the message queue
543 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
545 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
548 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
550 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
552 // Reserve some memory inside the message queue
553 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
555 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
558 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
560 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
562 // Reserve some memory inside the message queue
563 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
565 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
568 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
570 typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
572 // Reserve some memory inside the message queue
573 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
575 new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
578 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
580 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
582 // Reserve some memory inside the message queue
583 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
585 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
588 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
590 typedef MessageValue1< Renderer, int > LocalType;
592 // Reserve some memory inside the message queue
593 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
595 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
598 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
600 typedef MessageValue1< Renderer, int > LocalType;
602 // Reserve some memory inside the message queue
603 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
605 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
608 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
610 typedef MessageValue1< Renderer, int > LocalType;
612 // Reserve some memory inside the message queue
613 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
615 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
618 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
620 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
622 // Reserve some memory inside the message queue
623 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
625 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
628 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
630 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
632 // Reserve some memory inside the message queue
633 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
635 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
638 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
640 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
642 // Reserve some memory inside the message queue
643 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
645 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
648 inline void SetBatchingEnabledMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool batchable )
650 typedef MessageValue1< Renderer, bool > LocalType;
652 // Reserve some memory inside the message queue
653 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
655 new (slot) LocalType( &renderer, &Renderer::SetBatchingEnabled, batchable );
658 } // namespace SceneGraph
659 } // namespace Internal
662 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H