1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
5 * Copyright (c) 2017 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 * Set the depth index
123 * @param[in] depthIndex the new depth index to use
125 void SetDepthIndex( int depthIndex );
128 * @brief Get the depth index
129 * @return The depth index
131 int GetDepthIndex() const
137 * Set the face culling mode
138 * @param[in] faceCullingMode to use
140 void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
143 * Set the blending mode
144 * @param[in] blendingMode to use
146 void SetBlendMode( BlendMode::Type blendingMode );
149 * Set the blending options. This should only be called from the update thread.
150 * @param[in] options A bitmask of blending options.
152 void SetBlendingOptions( unsigned int options );
155 * Set the blend color for blending operation
156 * @param blendColor to pass to GL
158 void SetBlendColor( const Vector4& blendColor );
161 * Set the index of first element for indexed draw
162 * @param[in] firstElement index of first element to draw
164 void SetIndexedDrawFirstElement( size_t firstElement );
167 * Set the number of elements to draw by indexed draw
168 * @param[in] elementsCount number of elements to draw
170 void SetIndexedDrawElementsCount( size_t elementsCount );
173 * @brief Set whether the Pre-multiplied Alpha Blending is required
174 * @param[in] preMultipled whether alpha is pre-multiplied.
176 void EnablePreMultipliedAlpha( bool preMultipled );
179 * Sets the depth buffer write mode
180 * @param[in] depthWriteMode The depth buffer write mode
182 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
185 * Sets the depth buffer test mode
186 * @param[in] depthTestMode The depth buffer test mode
188 void SetDepthTestMode( DepthTestMode::Type depthTestMode );
191 * Sets the depth function
192 * @param[in] depthFunction The depth function
194 void SetDepthFunction( DepthFunction::Type depthFunction );
197 * Sets the render mode
198 * @param[in] mode The render mode
200 void SetRenderMode( RenderMode::Type mode );
203 * Sets the stencil function
204 * @param[in] stencilFunction The stencil function
206 void SetStencilFunction( StencilFunction::Type stencilFunction );
209 * Sets the stencil function mask
210 * @param[in] stencilFunctionMask The stencil function mask
212 void SetStencilFunctionMask( int stencilFunctionMask );
215 * Sets the stencil function reference
216 * @param[in] stencilFunctionReference The stencil function reference
218 void SetStencilFunctionReference( int stencilFunctionReference );
221 * Sets the stencil mask
222 * @param[in] stencilMask The stencil mask
224 void SetStencilMask( int stencilMask );
227 * Sets the stencil operation for when the stencil test fails
228 * @param[in] stencilOperationOnFail The stencil operation
230 void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
233 * Sets the stencil operation for when the depth test fails
234 * @param[in] stencilOperationOnZFail The stencil operation
236 void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
239 * Sets the stencil operation for when the depth test passes
240 * @param[in] stencilOperationOnZPass The stencil operation
242 void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
245 * Prepare the object for rendering.
246 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
247 * @param[in] updateBufferIndex The current update buffer index.
249 void PrepareRender( BufferIndex updateBufferIndex );
252 * Retrieve the Render thread renderer
253 * @return The associated render thread renderer
255 Render::Renderer& GetRenderer();
258 * Check whether the renderer has been marked as ready to render
259 * ready means that renderer has all resources and should produce correct result
260 * complete means all resources have finished loading
261 * It's possible that renderer is complete but not ready,
262 * for example in case of resource load failed
263 * @param[out] ready TRUE if the renderer has resources to render
264 * @param[out] complete TRUE if the renderer resources are complete
266 void GetReadyAndComplete( bool& ready, bool& complete ) const;
269 * Query whether the renderer is fully opaque, fully transparent or transparent.
270 * @param[in] updateBufferIndex The current update buffer index.
271 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
273 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
276 * Called by the TextureSet to notify to the renderer that it has changed
278 void TextureSetChanged();
281 * Called by the TextureSet to notify to the renderer that it is about to be deleted
283 void TextureSetDeleted();
285 public: // Implementation of ObjectOwnerContainer template methods
287 * Connect the object to the scene graph
289 * @param[in] sceneController The scene controller - used for sending messages to render thread
290 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
292 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
295 * Disconnect the object from the scene graph
296 * @param[in] sceneController The scene controller - used for sending messages to render thread
297 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
299 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
301 public: // Implementation of ConnectionChangePropagator
303 * @copydoc ConnectionChangePropagator::AddObserver
305 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
308 * @copydoc ConnectionChangePropagator::RemoveObserver
310 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
315 public: // UniformMap::Observer
317 * @copydoc UniformMap::Observer::UniformMappingsChanged
319 virtual void UniformMappingsChanged( const UniformMap& mappings );
321 public: // ConnectionChangePropagator::Observer
324 * @copydoc ConnectionChangePropagator::ConnectionsChanged
326 virtual void ConnectionsChanged( PropertyOwner& owner );
329 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
331 virtual void ConnectedUniformMapChanged( );
334 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
336 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
338 public: // PropertyOwner implementation
340 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
342 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
344 public: // From UniformMapDataProvider
347 * @copydoc UniformMapDataProvider::GetUniformMapChanged
349 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
352 * @copydoc UniformMapDataProvider::GetUniformMap
354 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
359 * Protected constructor; See also Renderer::New()
364 * Helper function to create a new render data provider
365 * @return the new (initialized) data provider
367 RenderDataProvider* NewRenderDataProvider();
370 * Helper function to retrieve the blend color.
371 * @return The blend color.
373 const Vector4& GetBlendColor() const;
377 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
378 SceneController* mSceneController; ///< Used for initializing renderers
379 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
380 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
381 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
382 Shader* mShader; ///< The shader this renderer uses. (Not owned)
383 OwnerPointer< Vector4 > mBlendColor; ///< The blend color for blending operation
385 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
387 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
388 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
389 unsigned int mBlendBitmask; ///< The bitmask of blending options
390 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
391 unsigned int mResendFlag; ///< Indicate whether data should be resent to the renderer
393 DepthFunction::Type mDepthFunction:3; ///< Local copy of the depth function
394 FaceCullingMode::Type mFaceCullingMode:2; ///< Local copy of the mode of face culling
395 BlendMode::Type mBlendMode:2; ///< Local copy of the mode of blending
396 DepthWriteMode::Type mDepthWriteMode:2; ///< Local copy of the depth write mode
397 DepthTestMode::Type mDepthTestMode:2; ///< Local copy of the depth test mode
399 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
400 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
401 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
402 bool mPremultipledAlphaEnabled:1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
406 int mDepthIndex; ///< Used only in PrepareRenderInstructions
411 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
413 typedef MessageValue1< Renderer, TextureSet* > LocalType;
415 // Reserve some memory inside the message queue
416 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
418 // Construct message in the message queue memory; note that delete should not be called on the return value
419 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
422 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
424 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
426 // Reserve some memory inside the message queue
427 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
433 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
435 typedef MessageValue1< Renderer, Shader* > LocalType;
437 // Reserve some memory inside the message queue
438 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
440 // Construct message in the message queue memory; note that delete should not be called on the return value
441 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
444 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
446 typedef MessageValue1< Renderer, int > LocalType;
448 // Reserve some memory inside the message queue
449 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
451 // Construct message in the message queue memory; note that delete should not be called on the return value
452 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
455 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
457 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
459 // Reserve some memory inside the message queue
460 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
462 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
465 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
467 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
469 // Reserve some memory inside the message queue
470 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
472 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
475 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
477 typedef MessageValue1< Renderer, unsigned int > LocalType;
479 // Reserve some memory inside the message queue
480 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
482 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
485 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
487 typedef MessageValue1< Renderer, Vector4 > LocalType;
489 // Reserve some memory inside the message queue
490 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
492 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
495 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
497 typedef MessageValue1< Renderer, size_t > LocalType;
499 // Reserve some memory inside the message queue
500 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
502 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
505 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
507 typedef MessageValue1< Renderer, size_t > LocalType;
509 // Reserve some memory inside the message queue
510 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
512 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
515 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
517 typedef MessageValue1< Renderer, bool > LocalType;
519 // Reserve some memory inside the message queue
520 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
522 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
525 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
527 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
529 // Reserve some memory inside the message queue
530 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
532 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
535 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
537 typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
539 // Reserve some memory inside the message queue
540 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
542 new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
545 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
547 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
549 // Reserve some memory inside the message queue
550 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
552 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
555 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
557 typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
559 // Reserve some memory inside the message queue
560 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
562 new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
565 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
567 typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
569 // Reserve some memory inside the message queue
570 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
572 new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
575 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
577 typedef MessageValue1< Renderer, int > LocalType;
579 // Reserve some memory inside the message queue
580 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
582 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
585 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
587 typedef MessageValue1< Renderer, int > LocalType;
589 // Reserve some memory inside the message queue
590 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
592 new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
595 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
597 typedef MessageValue1< Renderer, int > LocalType;
599 // Reserve some memory inside the message queue
600 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
602 new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
605 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
607 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
609 // Reserve some memory inside the message queue
610 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
612 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
615 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
617 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
619 // Reserve some memory inside the message queue
620 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
622 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
625 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
627 typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
629 // Reserve some memory inside the message queue
630 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
632 new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
635 } // namespace SceneGraph
636 } // namespace Internal
639 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H