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>
45 class SceneController;
48 typedef Dali::Vector< Renderer* > RendererContainer;
49 typedef RendererContainer::Iterator RendererIter;
50 typedef RendererContainer::ConstIterator RendererConstIter;
55 class Renderer : public PropertyOwner,
56 public UniformMapDataProvider,
57 public UniformMap::Observer,
58 public ConnectionChangePropagator::Observer
70 * Construct a new Renderer
72 static Renderer* New();
80 * Overriden delete operator
81 * Deletes the renderer from its global memory pool
83 void operator delete( void* ptr );
86 * Set the texture set for the renderer
87 * @param[in] textureSet The texture set this renderer will use
89 void SetTextures( TextureSet* textureSet );
92 * Returns current texture set object
93 * @return Pointer to the texture set
95 const TextureSet* GetTextures() const
101 * Set the shader for the renderer
102 * @param[in] shader The shader this renderer will use
104 void SetShader( Shader* shader );
107 * Get the shader used by this renderer
108 * @return the shader this renderer uses
110 const Shader& GetShader() const
116 * Set the geometry for the renderer
117 * @param[in] geometry The geometry this renderer will use
119 void SetGeometry( Render::Geometry* geometry );
122 * Get the geometry of this renderer
123 * @return the geometry this renderer uses
125 const Render::Geometry& GetGeometry() const
131 * Set the depth index
132 * @param[in] depthIndex the new depth index to use
134 void SetDepthIndex( int depthIndex );
137 * @brief Get the depth index
138 * @return The depth index
140 int GetDepthIndex() const
146 * Set the face culling mode
147 * @param[in] faceCullingMode to use
149 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
152 * Set the blending mode
153 * @param[in] blendingMode to use
155 void SetBlendMode( BlendMode::Type blendingMode );
158 * Set the blending options. This should only be called from the update thread.
159 * @param[in] options A bitmask of blending options.
161 void SetBlendingOptions( unsigned int options );
164 * Set the blend color for blending operation
165 * @param blendColor to pass to GL
167 void SetBlendColor( const Vector4& blendColor );
170 * Set the index of first element for indexed draw
171 * @param[in] firstElement index of first element to draw
173 void SetIndexedDrawFirstElement( size_t firstElement );
176 * Set the number of elements to draw by indexed draw
177 * @param[in] elementsCount number of elements to draw
179 void SetIndexedDrawElementsCount( size_t elementsCount );
182 * @brief Set whether the Pre-multiplied Alpha Blending is required
183 * @param[in] preMultipled whether alpha is pre-multiplied.
185 void EnablePreMultipliedAlpha( bool preMultipled );
188 * Sets the depth buffer write mode
189 * @param[in] depthWriteMode The depth buffer write mode
191 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
194 * Sets the depth buffer test mode
195 * @param[in] depthTestMode The depth buffer test mode
197 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
200 * Sets the depth function
201 * @param[in] depthFunction The depth function
203 void SetDepthFunction( DepthFunction::Type depthFunction );
206 * Sets the render mode
207 * @param[in] mode The render mode
209 void SetRenderMode( RenderMode::Type mode );
212 * Sets the stencil function
213 * @param[in] stencilFunction The stencil function
215 void SetStencilFunction( StencilFunction::Type stencilFunction );
218 * Sets the stencil function mask
219 * @param[in] stencilFunctionMask The stencil function mask
221 void SetStencilFunctionMask( int stencilFunctionMask );
224 * Sets the stencil function reference
225 * @param[in] stencilFunctionReference The stencil function reference
227 void SetStencilFunctionReference( int stencilFunctionReference );
230 * Sets the stencil mask
231 * @param[in] stencilMask The stencil mask
233 void SetStencilMask( int stencilMask );
236 * Sets the stencil operation for when the stencil test fails
237 * @param[in] stencilOperationOnFail The stencil operation
239 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
242 * Sets the stencil operation for when the depth test fails
243 * @param[in] stencilOperationOnZFail The stencil operation
245 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
248 * Sets the stencil operation for when the depth test passes
249 * @param[in] stencilOperationOnZPass The stencil operation
251 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
254 * Turns on batching feature for the renderer
255 * @param[in] batchingEnabled if true, enables the batching mode for the renderer
257 void SetBatchingEnabled( bool batchingEnabled );
260 * Tests whether batching feature is enabled for this renderer
261 * @return batching state
263 bool IsBatchingEnabled() const
265 return mBatchingEnabled;
269 * Prepare the object for rendering.
270 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
271 * @param[in] updateBufferIndex The current update buffer index.
273 void PrepareRender( BufferIndex updateBufferIndex );
276 * Retrieve the Render thread renderer
277 * @return The associated render thread renderer
279 Render::Renderer& GetRenderer();
282 * Check whether the renderer has been marked as ready to render
283 * ready means that renderer has all resources and should produce correct result
284 * complete means all resources have finished loading
285 * It's possible that renderer is complete but not ready,
286 * for example in case of resource load failed
287 * @param[out] ready TRUE if the renderer has resources to render
288 * @param[out] complete TRUE if the renderer resources are complete
290 void GetReadyAndComplete( bool& ready, bool& complete ) const;
293 * Query whether the renderer is fully opaque, fully transparent or transparent.
294 * @param[in] updateBufferIndex The current update buffer index.
295 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
297 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
300 * Called by the TextureSet to notify to the renderer that it has changed
302 void TextureSetChanged();
304 public: // Implementation of ObjectOwnerContainer template methods
306 * Connect the object to the scene graph
308 * @param[in] sceneController The scene controller - used for sending messages to render thread
309 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
311 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
314 * Disconnect the object from the scene graph
315 * @param[in] sceneController The scene controller - used for sending messages to render thread
316 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
318 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
320 public: // Implementation of ConnectionChangePropagator
322 * @copydoc ConnectionChangePropagator::AddObserver
324 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
327 * @copydoc ConnectionChangePropagator::RemoveObserver
329 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
334 public: // UniformMap::Observer
336 * @copydoc UniformMap::Observer::UniformMappingsChanged
338 virtual void UniformMappingsChanged( const UniformMap& mappings );
340 public: // ConnectionChangePropagator::Observer
343 * @copydoc ConnectionChangePropagator::ConnectionsChanged
345 virtual void ConnectionsChanged( PropertyOwner& owner );
348 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
350 virtual void ConnectedUniformMapChanged( );
353 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
355 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
357 public: // PropertyOwner implementation
359 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
361 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
363 public: // From UniformMapDataProvider
366 * @copydoc UniformMapDataProvider::GetUniformMapChanged
368 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
371 * @copydoc UniformMapDataProvider::GetUniformMap
373 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
378 * Protected constructor; See also Renderer::New()
383 * Helper function to create a new render data provider
384 * @return the new (initialized) data provider
386 RenderDataProvider* NewRenderDataProvider();
390 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
391 SceneController* mSceneController; ///< Used for initializing renderers
392 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
393 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
394 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
395 Shader* mShader; ///< The shader this renderer uses. (Not owned)
396 Vector4* mBlendColor; ///< The blend color for blending operation
398 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
400 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
401 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
402 unsigned int mBlendBitmask; ///< The bitmask of blending options
403 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
404 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
406 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
407 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
408 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
409 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
410 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
412 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
413 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
414 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
415 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
419 bool mBatchingEnabled:1; ///< Flag indicating whether the render supports batching
421 int mDepthIndex; ///< Used only in PrepareRenderInstructions
426 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
428 typedef MessageValue1< Renderer, TextureSet* > LocalType;
430 // Reserve some memory inside the message queue
431 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
433 // Construct message in the message queue memory; note that delete should not be called on the return value
434 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
437 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
439 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
441 // Reserve some memory inside the message queue
442 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
444 // Construct message in the message queue memory; note that delete should not be called on the return value
445 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
448 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
450 typedef MessageValue1< Renderer, Shader* > LocalType;
452 // Reserve some memory inside the message queue
453 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
455 // Construct message in the message queue memory; note that delete should not be called on the return value
456 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
459 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
461 typedef MessageValue1< Renderer, int > LocalType;
463 // Reserve some memory inside the message queue
464 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
466 // Construct message in the message queue memory; note that delete should not be called on the return value
467 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
470 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
472 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
474 // Reserve some memory inside the message queue
475 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
477 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
480 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
482 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
484 // Reserve some memory inside the message queue
485 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
487 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
490 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
492 typedef MessageValue1< Renderer, unsigned int > LocalType;
494 // Reserve some memory inside the message queue
495 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
497 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
500 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
502 typedef MessageValue1< Renderer, Vector4 > LocalType;
504 // Reserve some memory inside the message queue
505 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
507 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
510 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
512 typedef MessageValue1< Renderer, size_t > LocalType;
514 // Reserve some memory inside the message queue
515 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
517 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
520 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
522 typedef MessageValue1< Renderer, size_t > LocalType;
524 // Reserve some memory inside the message queue
525 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
527 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
530 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
532 typedef MessageValue1< Renderer, bool > LocalType;
534 // Reserve some memory inside the message queue
535 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
537 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
540 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
542 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
544 // Reserve some memory inside the message queue
545 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
547 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
550 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
552 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
554 // Reserve some memory inside the message queue
555 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
557 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
560 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
562 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
564 // Reserve some memory inside the message queue
565 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
567 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
570 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
572 typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
574 // Reserve some memory inside the message queue
575 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
577 new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
580 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
582 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
584 // Reserve some memory inside the message queue
585 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
587 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
590 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
592 typedef MessageValue1< Renderer, int > LocalType;
594 // Reserve some memory inside the message queue
595 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
597 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
600 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
602 typedef MessageValue1< Renderer, int > LocalType;
604 // Reserve some memory inside the message queue
605 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
607 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
610 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
612 typedef MessageValue1< Renderer, int > LocalType;
614 // Reserve some memory inside the message queue
615 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
617 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
620 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
622 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
624 // Reserve some memory inside the message queue
625 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
627 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
630 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
632 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
634 // Reserve some memory inside the message queue
635 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
637 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
640 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
642 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
644 // Reserve some memory inside the message queue
645 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
647 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
650 inline void SetBatchingEnabledMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool batchable )
652 typedef MessageValue1< Renderer, bool > LocalType;
654 // Reserve some memory inside the message queue
655 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
657 new (slot) LocalType( &renderer, &Renderer::SetBatchingEnabled, batchable );
660 } // namespace SceneGraph
661 } // namespace Internal
664 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H