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 using RendererContainer = Dali::Vector<RendererKey>;
71 using RendererIter = RendererContainer::Iterator;
72 using RendererConstIter = RendererContainer::ConstIterator;
74 class Renderer : public PropertyOwner,
75 public UniformMapDataProvider,
76 public RenderDataProvider
87 * Construct a new Renderer
89 static RendererKey NewKey();
97 * Overriden delete operator
98 * Deletes the renderer from its global memory pool
100 void operator delete(void* ptr);
103 * Get a pointer to the object from the given key.
104 * Used by MemoryPoolKey to provide pointer semantics.
106 static Renderer* Get(RendererKey::KeyType);
109 * Get the key of the given renderer in the associated memory pool.
110 * @param[in] renderer the given renderer
111 * @return The key in the associated memory pool.
113 static RendererKey GetKey(const SceneGraph::Renderer& renderer);
116 * Get the key of the given renderer in the associated memory pool.
117 * @param[in] renderer the given renderer
118 * @return The key in the associated memory pool, or -1 if not
121 static RendererKey GetKey(SceneGraph::Renderer* renderer);
124 * Set the texture set for the renderer
125 * @param[in] textureSet The texture set this renderer will use
127 void SetTextures(TextureSet* textureSet);
130 * Get the associated texture set
131 * @return the texture set.
133 const SceneGraph::TextureSet* GetTextureSet() const
139 * @copydoc RenderDataProvider::GetTextures()
141 const Vector<Render::TextureKey>* GetTextures() const override;
144 * @copydoc RenderDataProvider::GetSamplers()
146 const Vector<Render::Sampler*>* GetSamplers() const override;
149 * Set the shader for the renderer
150 * @param[in] shader The shader this renderer will use
152 void SetShader(Shader* shader);
155 * @copydoc RenderDataProvider::GetShader()
157 const Shader& GetShader() const override
163 * Set the geometry for the renderer
164 * @param[in] geometry The geometry this renderer will use
166 void SetGeometry(Render::Geometry* geometry);
169 * Set the depth index
170 * @param[in] depthIndex the new depth index to use
172 void SetDepthIndex(int depthIndex);
175 * @brief Get the depth index
176 * @return The depth index
178 int GetDepthIndex() const
184 * Set the face culling mode
185 * @param[in] faceCullingMode to use
187 void SetFaceCullingMode(FaceCullingMode::Type faceCullingMode);
190 * Get face culling mode
191 * @return The face culling mode
193 FaceCullingMode::Type GetFaceCullingMode() const;
196 * Set the blending mode
197 * @param[in] blendingMode to use
199 void SetBlendMode(BlendMode::Type blendingMode);
202 * Get the blending mode
203 * @return The the blending mode
205 BlendMode::Type GetBlendMode() const;
208 * Set the blending options. This should only be called from the update thread.
209 * @param[in] options A bitmask of blending options.
211 void SetBlendingOptions(uint32_t options);
214 * Get the blending options
215 * @return The the blending mode
217 uint32_t GetBlendingOptions() const;
220 * Set the blend color for blending operation
221 * @param blendColor to pass to GL
223 void SetBlendColor(const Vector4& blendColor);
226 * Get the blending color
227 * @return The blend color
229 Vector4 GetBlendColor() const;
232 * Set the index of first element for indexed draw
233 * @param[in] firstElement index of first element to draw
235 void SetIndexedDrawFirstElement(uint32_t firstElement);
238 * Get the index of first element for indexed draw
239 * @return The index of first element for indexed draw
241 uint32_t GetIndexedDrawFirstElement() const;
244 * Set the number of elements to draw by indexed draw
245 * @param[in] elementsCount number of elements to draw
247 void SetIndexedDrawElementsCount(uint32_t elementsCount);
250 * Get the number of elements to draw by indexed draw
251 * @return The number of elements to draw by indexed draw
253 uint32_t GetIndexedDrawElementsCount() const;
256 * @brief Set whether the Pre-multiplied Alpha Blending is required
257 * @param[in] preMultipled whether alpha is pre-multiplied.
259 void EnablePreMultipliedAlpha(bool preMultipled);
262 * @brief Query whether alpha is pre-multiplied.
263 * @return True is alpha is pre-multiplied, false otherwise.
265 bool IsPreMultipliedAlphaEnabled() const;
268 * Sets the depth buffer write mode
269 * @param[in] depthWriteMode The depth buffer write mode
271 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
274 * Get the depth buffer write mode
275 * @return The depth buffer write mode
277 DepthWriteMode::Type GetDepthWriteMode() const;
280 * Sets the depth buffer test mode
281 * @param[in] depthTestMode The depth buffer test mode
283 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
286 * Get the depth buffer test mode
287 * @return The depth buffer test mode
289 DepthTestMode::Type GetDepthTestMode() const;
292 * Sets the depth function
293 * @param[in] depthFunction The depth function
295 void SetDepthFunction(DepthFunction::Type depthFunction);
298 * Get the depth function
299 * @return The depth function
301 DepthFunction::Type GetDepthFunction() const;
304 * Sets the render mode
305 * @param[in] mode The render mode
307 void SetRenderMode(RenderMode::Type mode);
310 * Sets the stencil function
311 * @param[in] stencilFunction The stencil function
313 void SetStencilFunction(StencilFunction::Type stencilFunction);
316 * Sets the stencil function mask
317 * @param[in] stencilFunctionMask The stencil function mask
319 void SetStencilFunctionMask(int stencilFunctionMask);
322 * Sets the stencil function reference
323 * @param[in] stencilFunctionReference The stencil function reference
325 void SetStencilFunctionReference(int stencilFunctionReference);
328 * Sets the stencil mask
329 * @param[in] stencilMask The stencil mask
331 void SetStencilMask(int stencilMask);
334 * Sets the stencil operation for when the stencil test fails
335 * @param[in] stencilOperationOnFail The stencil operation
337 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
340 * Sets the stencil operation for when the depth test fails
341 * @param[in] stencilOperationOnZFail The stencil operation
343 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
346 * Sets the stencil operation for when the depth test passes
347 * @param[in] stencilOperationOnZPass The stencil operation
349 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
352 * Gets the stencil parameters
353 * @return The stencil parameters
355 const Render::Renderer::StencilParameters& GetStencilParameters() const;
359 * @param[in] updateBufferIndex The current update buffer index.
360 * @param[in] opacity The opacity
362 void BakeOpacity(BufferIndex updateBufferIndex, float opacity);
365 * @copydoc RenderDataProvider::GetOpacity()
367 float GetOpacity(BufferIndex updateBufferIndex) const override;
370 * Sets the rendering behavior
371 * @param[in] renderingBehavior The rendering behavior required.
373 void SetRenderingBehavior(DevelRenderer::Rendering::Type renderingBehavior);
376 * Gets the rendering behavior
377 * @return The rendering behavior
379 DevelRenderer::Rendering::Type GetRenderingBehavior() const;
382 * Prepare the object for rendering.
383 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
384 * @param[in] updateBufferIndex The current update buffer index.
385 * @return Whether this renderer has been updated in the current frame
387 bool PrepareRender(BufferIndex updateBufferIndex);
390 * Retrieve the Render thread renderer
391 * @return The associated render thread renderer
393 Render::RendererKey GetRenderer();
396 * Query whether the renderer is fully opaque, fully transparent or transparent.
397 * @param[in] updateBufferIndex The current update buffer index.
398 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
400 OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
403 * Connect the object to the scene graph
405 * @param[in] sceneController The scene controller - used for sending messages to render thread
406 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
408 void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
411 * Disconnect the object from the scene graph
412 * @param[in] sceneController The scene controller - used for sending messages to render thread
413 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
415 void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
418 * @copydoc RenderDataProvider::GetUniformMapDataProvider()
420 const UniformMapDataProvider& GetUniformMapDataProvider() const override
426 * @copydoc RenderDataProvider::IsUpdated()
428 bool IsUpdated() const override;
431 * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
433 Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
436 * Sets RenderCallback object
438 * @param[in] callback Valid pointer to RenderCallback object
440 void SetRenderCallback(RenderCallback* callback);
443 * Returns currently set RenderCallback pointer
445 * @return RenderCallback pointer or nullptr
447 RenderCallback* GetRenderCallback()
449 return mRenderCallback;
453 * Merge shader uniform map into renderer uniform map if any of the
454 * maps have changed. Only update uniform map if added to render
456 * @param[in] updateBufferIndex The current update buffer index.
458 void UpdateUniformMap(BufferIndex updateBufferIndex);
461 * Set the given external draw commands on this renderer.
463 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
466 * Query whether a renderer is dirty.
467 * @return true if the renderer is dirty.
468 * @note It is used to decide whether to reuse the RenderList. We can't reuse the RenderList if this is dirty.
470 bool IsDirty() const;
473 * Reset both dirty flag and updated flag.
474 * @note This is called after rendering has completed.
476 void ResetDirtyFlag();
479 * Get the capacity of the memory pools
480 * @return the capacity of the memory pools
482 static uint32_t GetMemoryPoolCapacity();
484 public: // PropertyOwner::MappingChanged
486 * @copydoc PropertyOwner::OnMappingChanged
488 void OnMappingChanged() override;
490 public: // PropertyOwner implementation
492 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
494 virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
496 public: // From UniformMapDataProvider
498 * @copydoc UniformMapDataProvider::GetCollectedUniformMap
500 const CollectedUniformMap& GetCollectedUniformMap() const override;
502 public: // For VisualProperties
504 * To be used only for 1st stage initialization in event thread.
506 void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
508 mVisualProperties = visualProperties;
512 * May be accessed from event thread
514 const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
516 return mVisualProperties.Get();
521 * Protected constructor; See also Renderer::New()
534 CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
536 SceneController* mSceneController; ///< Used for initializing renderers
537 Render::RendererKey mRenderer; ///< Key to the renderer (that's owned by RenderManager)
538 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
539 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
540 Shader* mShader; ///< The shader this renderer uses. (Not owned)
542 OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
543 OwnerPointer<Vector4> mBlendColor; ///< The blend color for blending operation
545 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
547 uint32_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
548 uint32_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
549 uint32_t mBlendBitmask; ///< The bitmask of blending options
550 uint32_t mResendFlag; ///< Indicate whether data should be resent to the renderer
551 UniformMap::SizeType mUniformMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
552 UniformMap::SizeType mShaderMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
554 DepthFunction::Type mDepthFunction : 4; ///< Local copy of the depth function
555 FaceCullingMode::Type mFaceCullingMode : 3; ///< Local copy of the mode of face culling
556 BlendMode::Type mBlendMode : 3; ///< Local copy of the mode of blending
557 DepthWriteMode::Type mDepthWriteMode : 3; ///< Local copy of the depth write mode
558 DepthTestMode::Type mDepthTestMode : 3; ///< Local copy of the depth test mode
559 DevelRenderer::Rendering::Type mRenderingBehavior : 2; ///< The rendering behavior
560 Decay mUpdateDecay : 2; ///< Update decay (aging)
562 bool mRegenerateUniformMap : 1; ///< true if the map should be regenerated
563 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
564 bool mDirtyFlag : 1; ///< Flag indicating whether the properties are changed
566 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
567 Dali::RenderCallback* mRenderCallback{nullptr};
570 AnimatableProperty<float> mOpacity; ///< The opacity value
571 int32_t mDepthIndex; ///< Used only in PrepareRenderInstructions
574 } // namespace SceneGraph
575 } // namespace Internal
578 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H