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 );
92 * Set the shader for the renderer
93 * @param[in] shader The shader this renderer will use
95 void SetShader( Shader* shader );
98 * Get the shader used by this renderer
99 * @return the shader this renderer uses
107 * Set the geometry for the renderer
108 * @param[in] geometry The geometry this renderer will use
110 void SetGeometry( Render::Geometry* geometry );
113 * Set the depth index
114 * @param[in] depthIndex the new depth index to use
116 void SetDepthIndex( int depthIndex );
119 * @brief Get the depth index
120 * @return The depth index
122 int GetDepthIndex() const
128 * Set the face culling mode
129 * @param[in] faceCullingMode to use
131 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
134 * Set the blending mode
135 * @param[in] blendingMode to use
137 void SetBlendMode( BlendMode::Type blendingMode );
140 * Set the blending options. This should only be called from the update thread.
141 * @param[in] options A bitmask of blending options.
143 void SetBlendingOptions( unsigned int options );
146 * Set the blend color for blending operation
147 * @param blendColor to pass to GL
149 void SetBlendColor( const Vector4& blendColor );
152 * Set the index of first element for indexed draw
153 * @param[in] firstElement index of first element to draw
155 void SetIndexedDrawFirstElement( size_t firstElement );
158 * Set the number of elements to draw by indexed draw
159 * @param[in] elementsCount number of elements to draw
161 void SetIndexedDrawElementsCount( size_t elementsCount );
164 * @brief Set whether the Pre-multiplied Alpha Blending is required
165 * @param[in] preMultipled whether alpha is pre-multiplied.
167 void EnablePreMultipliedAlpha( bool preMultipled );
170 * Sets the depth buffer write mode
171 * @param[in] depthWriteMode The depth buffer write mode
173 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
176 * Sets the depth buffer test mode
177 * @param[in] depthTestMode The depth buffer test mode
179 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
182 * Sets the depth function
183 * @param[in] depthFunction The depth function
185 void SetDepthFunction( DepthFunction::Type depthFunction );
188 * Sets the stencil mode
189 * @param[in] mode The stencil function
191 void SetStencilMode( StencilMode::Type mode );
194 * Sets the stencil function
195 * @param[in] stencilFunction The stencil function
197 void SetStencilFunction( StencilFunction::Type stencilFunction );
200 * Sets the stencil function mask
201 * @param[in] stencilFunctionMask The stencil function mask
203 void SetStencilFunctionMask( int stencilFunctionMask );
206 * Sets the stencil function reference
207 * @param[in] stencilFunctionReference The stencil function reference
209 void SetStencilFunctionReference( int stencilFunctionReference );
212 * Sets the stencil mask
213 * @param[in] stencilMask The stencil mask
215 void SetStencilMask( int stencilMask );
218 * Sets the stencil operation for when the stencil test fails
219 * @param[in] stencilOperationOnFail The stencil operation
221 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
224 * Sets the stencil operation for when the depth test fails
225 * @param[in] stencilOperationOnZFail The stencil operation
227 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
230 * Sets the stencil operation for when the depth test passes
231 * @param[in] stencilOperationOnZPass The stencil operation
233 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
236 * Sets whether or not to write to the color buffer
237 * @param[in] writeToColorBuffer True to write to the color buffer
239 void SetWriteToColorBuffer( bool writeToColorBuffer );
242 * Prepare the object for rendering.
243 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
244 * @param[in] updateBufferIndex The current update buffer index.
246 void PrepareRender( BufferIndex updateBufferIndex );
249 * Retrieve the Render thread renderer
250 * @return The associated render thread renderer
252 Render::Renderer& GetRenderer();
255 * Check whether the renderer has been marked as ready to render
256 * ready means that renderer has all resources and should produce correct result
257 * complete means all resources have finished loading
258 * It's possible that renderer is complete but not ready,
259 * for example in case of resource load failed
260 * @param[out] ready TRUE if the renderer has resources to render
261 * @param[out] complete TRUE if the renderer resources are complete
263 void GetReadyAndComplete( bool& ready, bool& complete ) const;
266 * Query whether the renderer is fully opaque, fully transparent or transparent.
267 * @param[in] updateBufferIndex The current update buffer index.
268 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
270 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
273 * Called by the TextureSet to notify to the renderer that it has changed
275 void TextureSetChanged();
277 public: // Implementation of ObjectOwnerContainer template methods
279 * Connect the object to the scene graph
281 * @param[in] sceneController The scene controller - used for sending messages to render thread
282 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
284 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
287 * Disconnect the object from the scene graph
288 * @param[in] sceneController The scene controller - used for sending messages to render thread
289 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
291 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
293 public: // Implementation of ConnectionChangePropagator
295 * @copydoc ConnectionChangePropagator::AddObserver
297 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
300 * @copydoc ConnectionChangePropagator::RemoveObserver
302 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
307 public: // UniformMap::Observer
309 * @copydoc UniformMap::Observer::UniformMappingsChanged
311 virtual void UniformMappingsChanged( const UniformMap& mappings );
313 public: // ConnectionChangePropagator::Observer
316 * @copydoc ConnectionChangePropagator::ConnectionsChanged
318 virtual void ConnectionsChanged( PropertyOwner& owner );
321 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
323 virtual void ConnectedUniformMapChanged( );
326 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
328 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
330 public: // PropertyOwner implementation
332 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
334 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
336 public: // From UniformMapDataProvider
339 * @copydoc UniformMapDataProvider::GetUniformMapChanged
341 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
344 * @copydoc UniformMapDataProvider::GetUniformMap
346 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
351 * Protected constructor; See also Renderer::New()
356 * Helper function to create a new render data provider
357 * @return the new (initialized) data provider
359 RenderDataProvider* NewRenderDataProvider();
363 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
364 SceneController* mSceneController; ///< Used for initializing renderers
365 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
366 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
367 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
368 Shader* mShader; ///< The shader this renderer uses. (Not owned)
369 Vector4* mBlendColor; ///< The blend color for blending operation
371 Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
373 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
374 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
375 unsigned int mBlendBitmask; ///< The bitmask of blending options
376 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
377 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
379 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
380 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
381 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
382 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
383 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
384 bool mWriteToColorBuffer:1; ///< Local copy of the write to color buffer flag
386 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
387 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
388 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
389 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
393 int mDepthIndex; ///< Used only in PrepareRenderInstructions
398 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
400 typedef MessageValue1< Renderer, TextureSet* > LocalType;
402 // Reserve some memory inside the message queue
403 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
405 // Construct message in the message queue memory; note that delete should not be called on the return value
406 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
409 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
411 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
413 // Reserve some memory inside the message queue
414 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
416 // Construct message in the message queue memory; note that delete should not be called on the return value
417 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
420 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
422 typedef MessageValue1< Renderer, Shader* > LocalType;
424 // Reserve some memory inside the message queue
425 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
427 // Construct message in the message queue memory; note that delete should not be called on the return value
428 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
431 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
433 typedef MessageValue1< Renderer, int > LocalType;
435 // Reserve some memory inside the message queue
436 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
438 // Construct message in the message queue memory; note that delete should not be called on the return value
439 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
442 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
444 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
446 // Reserve some memory inside the message queue
447 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
449 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
452 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
454 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
456 // Reserve some memory inside the message queue
457 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
459 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
462 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
464 typedef MessageValue1< Renderer, unsigned int > LocalType;
466 // Reserve some memory inside the message queue
467 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
469 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
472 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
474 typedef MessageValue1< Renderer, Vector4 > LocalType;
476 // Reserve some memory inside the message queue
477 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
479 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
482 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
484 typedef MessageValue1< Renderer, size_t > LocalType;
486 // Reserve some memory inside the message queue
487 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
489 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
492 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
494 typedef MessageValue1< Renderer, size_t > LocalType;
496 // Reserve some memory inside the message queue
497 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
499 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
502 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
504 typedef MessageValue1< Renderer, bool > LocalType;
506 // Reserve some memory inside the message queue
507 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
509 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
512 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
514 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
516 // Reserve some memory inside the message queue
517 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
519 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
522 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
524 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
526 // Reserve some memory inside the message queue
527 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
529 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
532 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
534 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
536 // Reserve some memory inside the message queue
537 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
539 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
542 inline void SetStencilModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilMode::Type mode )
544 typedef MessageValue1< Renderer, StencilMode::Type > LocalType;
546 // Reserve some memory inside the message queue
547 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
549 new (slot) LocalType( &renderer, &Renderer::SetStencilMode, mode );
552 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
554 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
556 // Reserve some memory inside the message queue
557 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
559 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
562 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
564 typedef MessageValue1< Renderer, int > LocalType;
566 // Reserve some memory inside the message queue
567 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
569 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
572 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
574 typedef MessageValue1< Renderer, int > LocalType;
576 // Reserve some memory inside the message queue
577 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
579 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
582 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
584 typedef MessageValue1< Renderer, int > LocalType;
586 // Reserve some memory inside the message queue
587 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
589 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
592 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
594 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
596 // Reserve some memory inside the message queue
597 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
599 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
602 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
604 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
606 // Reserve some memory inside the message queue
607 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
609 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
612 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
614 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
616 // Reserve some memory inside the message queue
617 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
619 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
622 inline void SetWriteToColorBufferMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool writeToColorBuffer )
624 typedef MessageValue1< Renderer, bool > LocalType;
626 // Reserve some memory inside the message queue
627 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
629 new (slot) LocalType( &renderer, &Renderer::SetWriteToColorBuffer, writeToColorBuffer );
632 } // namespace SceneGraph
633 } // namespace Internal
636 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H