1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER2_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER2_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.
21 #include <dali/devel-api/rendering/geometry.h>
22 #include <dali/devel-api/rendering/renderer.h> // Dali::Renderer
23 #include <dali/internal/common/blending-options.h>
24 #include <dali/internal/common/type-abstraction-enums.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/common/uniform-map.h>
28 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
29 #include <dali/internal/render/data-providers/render-data-provider.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
166 * @param[in] preMultipled whether alpha is pre-multiplied.
168 void EnablePreMultipliedAlpha( bool preMultipled );
171 * Sets the depth buffer write mode
172 * @param[in] depthWriteMode The depth buffer write mode
174 void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
177 * Sets the depth function
178 * @param[in] depthFunction The depth function
180 void SetDepthFunction( DepthFunction::Type depthFunction );
183 * Called when an actor with this renderer is added to the stage
185 void OnStageConnect();
188 * Called when an actor with this renderer is removed from the stage
190 void OnStageDisconnect();
193 * Prepare the object for rendering.
194 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
195 * @param[in] updateBufferIndex The current update buffer index.
197 void PrepareRender( BufferIndex updateBufferIndex );
200 * Retrieve the Render thread renderer
201 * @return The associated render thread renderer
203 Render::Renderer& GetRenderer();
206 * Check whether the renderer has been marked as ready to render
207 * ready means that renderer has all resources and should produce correct result
208 * complete means all resources have finished loading
209 * It's possible that renderer is complete but not ready,
210 * for example in case of resource load failed
211 * @param[out] ready TRUE if the renderer has resources to render
212 * @param[out] complete TRUE if the renderer resources are complete
214 void GetReadyAndComplete( bool& ready, bool& complete ) const;
217 * Query whether the renderer is fully opaque, fully transparent or transparent.
218 * @param[in] updateBufferIndex The current update buffer index.
219 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
221 Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
224 * Query whether the renderer is currently in use by an actor on the stage
226 bool IsReferenced() const
228 return mReferenceCount > 0;
232 * Called by the TextureSet to notify to the renderer that it has changed
234 void TextureSetChanged();
236 public: // Implementation of ObjectOwnerContainer template methods
238 * Connect the object to the scene graph
240 * @param[in] sceneController The scene controller - used for sending messages to render thread
241 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
243 void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
246 * Disconnect the object from the scene graph
247 * @param[in] sceneController The scene controller - used for sending messages to render thread
248 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
250 void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
252 public: // Implementation of ConnectionChangePropagator
254 * @copydoc ConnectionChangePropagator::AddObserver
256 void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
259 * @copydoc ConnectionChangePropagator::RemoveObserver
261 void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
266 public: // UniformMap::Observer
268 * @copydoc UniformMap::Observer::UniformMappingsChanged
270 virtual void UniformMappingsChanged( const UniformMap& mappings );
272 public: // ConnectionChangePropagator::Observer
275 * @copydoc ConnectionChangePropagator::ConnectionsChanged
277 virtual void ConnectionsChanged( PropertyOwner& owner );
280 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
282 virtual void ConnectedUniformMapChanged( );
285 * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
287 virtual void ObservedObjectDestroyed(PropertyOwner& owner);
289 public: // PropertyOwner implementation
291 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
293 virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
295 public: // From UniformMapDataProvider
298 * @copydoc UniformMapDataProvider::GetUniformMapChanged
300 virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
303 * @copydoc UniformMapDataProvider::GetUniformMap
305 virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
310 * Protected constructor; See also Renderer::New()
315 * Helper function to create a new render data provider
316 * @return the new (initialized) data provider
318 RenderDataProvider* NewRenderDataProvider();
322 SceneController* mSceneController; ///< Used for initializing renderers
323 Render::Renderer* mRenderer; ///< Raw pointer to the renderer (that's owned by RenderManager)
324 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
325 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
328 Vector4* mBlendColor; ///< The blend color for blending operation
329 unsigned int mBlendBitmask; ///< The bitmask of blending options
330 FaceCullingMode::Type mFaceCullingMode; ///< The mode of face culling
331 BlendMode::Type mBlendMode; ///< The mode of blending
332 DepthWriteMode::Type mDepthWriteMode; ///< The depth write mode
333 DepthFunction::Type mDepthFunction; ///< The depth function
335 CollectedUniformMap mCollectedUniformMap[2]; ///< Uniform maps collected by the renderer
337 size_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
338 size_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
339 unsigned int mReferenceCount; ///< Number of nodes currently using this renderer
340 unsigned int mRegenerateUniformMap; ///< 2 if the map should be regenerated, 1 if it should be copied.
341 unsigned short mResendFlag; ///< Indicate whether data should be resent to the renderer
342 bool mUniformMapChanged[2]; ///< Records if the uniform map has been altered this frame
343 bool mResourcesReady; ///< Set during the Update algorithm; true if the renderer has resources ready for the current frame.
344 bool mFinishedResourceAcquisition; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
345 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
349 int mDepthIndex; ///< Used only in PrepareRenderInstructions
354 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
356 typedef MessageValue1< Renderer, TextureSet* > LocalType;
358 // Reserve some memory inside the message queue
359 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
361 // Construct message in the message queue memory; note that delete should not be called on the return value
362 new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
365 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
367 typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
369 // Reserve some memory inside the message queue
370 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
372 // Construct message in the message queue memory; note that delete should not be called on the return value
373 new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
376 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
378 typedef MessageValue1< Renderer, Shader* > LocalType;
380 // Reserve some memory inside the message queue
381 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
383 // Construct message in the message queue memory; note that delete should not be called on the return value
384 new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
387 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
389 typedef MessageValue1< Renderer, int > LocalType;
391 // Reserve some memory inside the message queue
392 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
394 // Construct message in the message queue memory; note that delete should not be called on the return value
395 new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
398 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
400 typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
402 // Reserve some memory inside the message queue
403 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
405 new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
408 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
410 typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
412 // Reserve some memory inside the message queue
413 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
415 new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
418 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
420 typedef MessageValue1< Renderer, unsigned int > LocalType;
422 // Reserve some memory inside the message queue
423 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
425 new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
428 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
430 typedef MessageValue1< Renderer, Vector4 > LocalType;
432 // Reserve some memory inside the message queue
433 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
435 new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
438 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
440 typedef MessageValue1< Renderer, size_t > LocalType;
442 // Reserve some memory inside the message queue
443 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
448 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
450 typedef MessageValue1< Renderer, size_t > LocalType;
452 // Reserve some memory inside the message queue
453 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
455 new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
458 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
460 typedef MessageValue1< Renderer, bool > LocalType;
462 // Reserve some memory inside the message queue
463 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
465 new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
468 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
470 typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
472 // Reserve some memory inside the message queue
473 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
475 new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
478 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
480 typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
482 // Reserve some memory inside the message queue
483 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
485 new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
488 inline void OnStageConnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
490 typedef Message< Renderer > LocalType;
492 // Reserve some memory inside the message queue
493 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
495 // Construct message in the message queue memory; note that delete should not be called on the return value
496 new (slot) LocalType( &renderer, &Renderer::OnStageConnect );
499 inline void OnStageDisconnectMessage( EventThreadServices& eventThreadServices, const Renderer& renderer )
501 typedef Message< Renderer > LocalType;
503 // Reserve some memory inside the message queue
504 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
506 // Construct message in the message queue memory; note that delete should not be called on the return value
507 new (slot) LocalType( &renderer, &Renderer::OnStageDisconnect );
510 } // namespace SceneGraph
511 } // namespace Internal
514 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H