1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
5 * Copyright (c) 2023 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/devel-api/rendering/renderer-devel.h>
21 #include <dali/internal/common/blending-options.h>
22 #include <dali/internal/common/memory-pool-key.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/render/data-providers/render-data-provider.h>
25 #include <dali/internal/render/renderers/render-renderer.h>
26 #include <dali/internal/update/common/animatable-property.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/common/uniform-map.h>
29 #include <dali/internal/update/rendering/scene-graph-visual-renderer.h>
30 #include <dali/public-api/rendering/geometry.h>
31 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
45 class SceneController;
51 using RendererKey = MemoryPoolKey<SceneGraph::Renderer>;
53 } // namespace SceneGraph
54 } // namespace Internal
56 // Ensure RendererKey can be used in Dali::Vector
58 struct TypeTraits<Internal::SceneGraph::RendererKey> : public BasicTypes<Internal::SceneGraph::RendererKey>
62 IS_TRIVIAL_TYPE = true
70 class NodeDataProvider;
71 using RendererContainer = Dali::Vector<RendererKey>;
72 using RendererIter = RendererContainer::Iterator;
73 using RendererConstIter = RendererContainer::ConstIterator;
75 class Renderer : public PropertyOwner,
76 public UniformMapDataProvider,
77 public RenderDataProvider
88 * Construct a new Renderer
90 static RendererKey NewKey();
98 * Overriden delete operator
99 * Deletes the renderer from its global memory pool
101 void operator delete(void* ptr);
104 * Get a pointer to the object from the given key.
105 * Used by MemoryPoolKey to provide pointer semantics.
107 static Renderer* Get(RendererKey::KeyType);
110 * Get the key of the given renderer in the associated memory pool.
111 * @param[in] renderer the given renderer
112 * @return The key in the associated memory pool.
114 static RendererKey GetKey(const SceneGraph::Renderer& renderer);
117 * Get the key of the given renderer in the associated memory pool.
118 * @param[in] renderer the given renderer
119 * @return The key in the associated memory pool, or -1 if not
122 static RendererKey GetKey(SceneGraph::Renderer* renderer);
125 * Set the texture set for the renderer
126 * @param[in] textureSet The texture set this renderer will use
128 void SetTextures(TextureSet* textureSet);
131 * Get the associated texture set
132 * @return the texture set.
134 const SceneGraph::TextureSet* GetTextureSet() const
140 * @copydoc RenderDataProvider::GetTextures()
142 const Vector<Render::TextureKey>* GetTextures() const override;
145 * @copydoc RenderDataProvider::GetSamplers()
147 const Vector<Render::Sampler*>* GetSamplers() const override;
150 * Set the shader for the renderer
151 * @param[in] shader The shader this renderer will use
153 void SetShader(Shader* shader);
156 * @copydoc RenderDataProvider::GetShader()
158 const Shader& GetShader() const override
164 * Set the geometry for the renderer
165 * @param[in] geometry The geometry this renderer will use
167 void SetGeometry(Render::Geometry* geometry);
170 * Set the depth index
171 * @param[in] depthIndex the new depth index to use
173 void SetDepthIndex(int depthIndex);
176 * @brief Get the depth index
177 * @return The depth index
179 int GetDepthIndex() const
185 * Set the face culling mode
186 * @param[in] faceCullingMode to use
188 void SetFaceCullingMode(FaceCullingMode::Type faceCullingMode);
191 * Get face culling mode
192 * @return The face culling mode
194 FaceCullingMode::Type GetFaceCullingMode() const;
197 * Set the blending mode
198 * @param[in] blendingMode to use
200 void SetBlendMode(BlendMode::Type blendingMode);
203 * Get the blending mode
204 * @return The the blending mode
206 BlendMode::Type GetBlendMode() const;
209 * Set the blending options. This should only be called from the update thread.
210 * @param[in] options A bitmask of blending options.
212 void SetBlendingOptions(uint32_t options);
215 * Get the blending options
216 * @return The the blending mode
218 uint32_t GetBlendingOptions() const;
221 * Set the blend color for blending operation
222 * @param blendColor to pass to GL
224 void SetBlendColor(const Vector4& blendColor);
227 * Get the blending color
228 * @return The blend color
230 Vector4 GetBlendColor() const;
233 * Set the index of first element for indexed draw
234 * @param[in] firstElement index of first element to draw
236 void SetIndexedDrawFirstElement(uint32_t firstElement);
239 * Get the index of first element for indexed draw
240 * @return The index of first element for indexed draw
242 uint32_t GetIndexedDrawFirstElement() const;
245 * Set the number of elements to draw by indexed draw
246 * @param[in] elementsCount number of elements to draw
248 void SetIndexedDrawElementsCount(uint32_t elementsCount);
251 * Get the number of elements to draw by indexed draw
252 * @return The number of elements to draw by indexed draw
254 uint32_t GetIndexedDrawElementsCount() const;
257 * @brief Set whether the Pre-multiplied Alpha Blending is required
258 * @param[in] preMultipled whether alpha is pre-multiplied.
260 void EnablePreMultipliedAlpha(bool preMultipled);
263 * @brief Query whether alpha is pre-multiplied.
264 * @return True is alpha is pre-multiplied, false otherwise.
266 bool IsPreMultipliedAlphaEnabled() const;
269 * Sets the depth buffer write mode
270 * @param[in] depthWriteMode The depth buffer write mode
272 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
275 * Get the depth buffer write mode
276 * @return The depth buffer write mode
278 DepthWriteMode::Type GetDepthWriteMode() const;
281 * Sets the depth buffer test mode
282 * @param[in] depthTestMode The depth buffer test mode
284 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
287 * Get the depth buffer test mode
288 * @return The depth buffer test mode
290 DepthTestMode::Type GetDepthTestMode() const;
293 * Sets the depth function
294 * @param[in] depthFunction The depth function
296 void SetDepthFunction(DepthFunction::Type depthFunction);
299 * Get the depth function
300 * @return The depth function
302 DepthFunction::Type GetDepthFunction() const;
305 * Sets the render mode
306 * @param[in] mode The render mode
308 void SetRenderMode(RenderMode::Type mode);
311 * Sets the stencil function
312 * @param[in] stencilFunction The stencil function
314 void SetStencilFunction(StencilFunction::Type stencilFunction);
317 * Sets the stencil function mask
318 * @param[in] stencilFunctionMask The stencil function mask
320 void SetStencilFunctionMask(int stencilFunctionMask);
323 * Sets the stencil function reference
324 * @param[in] stencilFunctionReference The stencil function reference
326 void SetStencilFunctionReference(int stencilFunctionReference);
329 * Sets the stencil mask
330 * @param[in] stencilMask The stencil mask
332 void SetStencilMask(int stencilMask);
335 * Sets the stencil operation for when the stencil test fails
336 * @param[in] stencilOperationOnFail The stencil operation
338 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
341 * Sets the stencil operation for when the depth test fails
342 * @param[in] stencilOperationOnZFail The stencil operation
344 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
347 * Sets the stencil operation for when the depth test passes
348 * @param[in] stencilOperationOnZPass The stencil operation
350 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
353 * Gets the stencil parameters
354 * @return The stencil parameters
356 const Render::Renderer::StencilParameters& GetStencilParameters() const;
360 * @param[in] updateBufferIndex The current update buffer index.
361 * @param[in] opacity The opacity
363 void BakeOpacity(BufferIndex updateBufferIndex, float opacity);
366 * @copydoc RenderDataProvider::GetOpacity()
368 float GetOpacity(BufferIndex updateBufferIndex) const override;
371 * Sets the rendering behavior
372 * @param[in] renderingBehavior The rendering behavior required.
374 void SetRenderingBehavior(DevelRenderer::Rendering::Type renderingBehavior);
377 * Gets the rendering behavior
378 * @return The rendering behavior
380 DevelRenderer::Rendering::Type GetRenderingBehavior() const;
383 * Prepare the object for rendering.
384 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
385 * @param[in] updateBufferIndex The current update buffer index.
386 * @return Whether this renderer has been updated in the current frame
388 bool PrepareRender(BufferIndex updateBufferIndex);
391 * Retrieve the Render thread renderer
392 * @return The associated render thread renderer
394 Render::RendererKey GetRenderer();
397 * Query whether the renderer is fully opaque, fully transparent or transparent.
398 * @param[in] updateBufferIndex The current update buffer index.
399 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
401 OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
404 * Connect the object to the scene graph
406 * @param[in] sceneController The scene controller - used for sending messages to render thread
407 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
409 void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
412 * Disconnect the object from the scene graph
413 * @param[in] sceneController The scene controller - used for sending messages to render thread
414 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
416 void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
419 * Detached from the scene graph object.
420 * @param[in] node node who detach this renderer.
422 void DetachFromNodeDataProvider(const NodeDataProvider& node);
425 * @copydoc RenderDataProvider::GetUniformMapDataProvider()
427 const UniformMapDataProvider& GetUniformMapDataProvider() const override
433 * @copydoc RenderDataProvider::IsUpdated()
435 bool IsUpdated() const override;
438 * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
440 Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
443 * Sets RenderCallback object
445 * @param[in] callback Valid pointer to RenderCallback object
447 void SetRenderCallback(RenderCallback* callback);
450 * Returns currently set RenderCallback pointer
452 * @return RenderCallback pointer or nullptr
454 RenderCallback* GetRenderCallback()
456 return mRenderCallback;
460 * Merge shader uniform map into renderer uniform map if any of the
461 * maps have changed. Only update uniform map if added to render
463 * @param[in] updateBufferIndex The current update buffer index.
465 void UpdateUniformMap(BufferIndex updateBufferIndex);
468 * Set the given external draw commands on this renderer.
470 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
473 * Query whether a renderer is dirty.
474 * @return true if the renderer is dirty.
475 * @note It is used to decide whether to reuse the RenderList. We can't reuse the RenderList if this is dirty.
477 bool IsDirty() const;
480 * Reset both dirty flag and updated flag.
481 * @note This is called after rendering has completed.
483 void ResetDirtyFlag();
486 * @brief Reset to base values of all animatable properties.
488 * @param[in] updateBufferIndex the current buffer index
490 void ResetToBaseValues(BufferIndex updateBufferIndex);
493 * @brief Mark all animatable properties as dirty.
498 * Get the capacity of the memory pools
499 * @return the capacity of the memory pools
501 static uint32_t GetMemoryPoolCapacity();
503 public: // PropertyOwner::MappingChanged
505 * @copydoc PropertyOwner::OnMappingChanged
507 void OnMappingChanged() override;
509 public: // PropertyOwner implementation
511 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
513 virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
516 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::AddInitializeResetter
518 void AddInitializeResetter(ResetterManager& manager) const override;
520 public: // From UniformMapDataProvider
522 * @copydoc UniformMapDataProvider::GetCollectedUniformMap
524 const CollectedUniformMap& GetCollectedUniformMap() const override;
526 public: // For VisualProperties
528 * To be used only for 1st stage initialization in event thread.
530 void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
532 mVisualProperties = visualProperties;
536 * May be accessed from event thread
538 const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
540 return mVisualProperties.Get();
545 * Protected constructor; See also Renderer::New()
558 CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
560 SceneController* mSceneController; ///< Used for initializing renderers
561 Render::RendererKey mRenderer; ///< Key to the renderer (that's owned by RenderManager)
562 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
563 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
564 Shader* mShader; ///< The shader this renderer uses. (Not owned)
566 OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
567 OwnerPointer<Vector4> mBlendColor; ///< The blend color for blending operation
569 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
571 uint32_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
572 uint32_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
573 uint32_t mBlendBitmask; ///< The bitmask of blending options
574 uint32_t mResendFlag; ///< Indicate whether data should be resent to the renderer
575 UniformMap::SizeType mUniformMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
576 UniformMap::SizeType mShaderMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
578 DepthFunction::Type mDepthFunction : 4; ///< Local copy of the depth function
579 FaceCullingMode::Type mFaceCullingMode : 3; ///< Local copy of the mode of face culling
580 BlendMode::Type mBlendMode : 3; ///< Local copy of the mode of blending
581 DepthWriteMode::Type mDepthWriteMode : 3; ///< Local copy of the depth write mode
582 DepthTestMode::Type mDepthTestMode : 3; ///< Local copy of the depth test mode
583 DevelRenderer::Rendering::Type mRenderingBehavior : 2; ///< The rendering behavior
584 Decay mUpdateDecay : 2; ///< Update decay (aging)
586 bool mRegenerateUniformMap : 1; ///< true if the map should be regenerated
587 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
588 bool mDirtyFlag : 1; ///< Flag indicating whether the properties are changed
590 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
591 Dali::RenderCallback* mRenderCallback{nullptr};
594 AnimatableProperty<float> mOpacity; ///< The opacity value
595 int32_t mDepthIndex; ///< Used only in PrepareRenderInstructions
598 } // namespace SceneGraph
599 } // namespace Internal
602 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H