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/public-api/rendering/geometry.h>
30 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
44 class SceneController;
50 using RendererKey = MemoryPoolKey<SceneGraph::Renderer>;
52 } // namespace SceneGraph
53 } // namespace Internal
55 // Ensure RendererKey can be used in Dali::Vector
57 struct TypeTraits<Internal::SceneGraph::RendererKey> : public BasicTypes<Internal::SceneGraph::RendererKey>
61 IS_TRIVIAL_TYPE = true
69 using RendererContainer = Dali::Vector<RendererKey>;
70 using RendererIter = RendererContainer::Iterator;
71 using RendererConstIter = RendererContainer::ConstIterator;
73 namespace VisualRenderer
75 struct AnimatableVisualProperties
77 AnimatableVisualProperties()
78 : mTransformOffset(Vector2::ZERO),
79 mTransformSize(Vector2::ONE),
80 mTransformOrigin(Vector2::ZERO),
81 mTransformAnchorPoint(Vector2::ZERO),
82 mTransformOffsetSizeMode(Vector4::ZERO),
83 mExtraSize(Vector2::ZERO),
84 mMixColor(Vector3::ONE),
85 mPreMultipliedAlpha(0.0f),
86 mExtendedPropertiesDeleteFunction(nullptr)
90 ~AnimatableVisualProperties()
92 if(mExtendedProperties && mExtendedPropertiesDeleteFunction)
94 mExtendedPropertiesDeleteFunction(mExtendedProperties);
99 * @brief Cached coefficient value when we calculate visual transformed update size.
100 * It can reduce complexity of calculate the vertex position.
102 * Vector2 vertexPosition = (XA * aPosition + XB) * originalSize + (CA * aPosition + CB) + Vector2(D, D) * aPosition
104 struct VisualTransformedUpdateSizeCoefficientCache
106 Vector2 coefXA{Vector2::ZERO};
107 Vector2 coefXB{Vector2::ZERO};
108 Vector2 coefCA{Vector2::ZERO};
109 Vector2 coefCB{Vector2::ZERO};
113 uint64_t decoratedHash{0u};
115 VisualTransformedUpdateSizeCoefficientCache mCoefficient; ///< Coefficient value to calculate visual transformed update size by VisualProperties more faster.
117 AnimatableProperty<Vector2> mTransformOffset;
118 AnimatableProperty<Vector2> mTransformSize;
119 AnimatableProperty<Vector2> mTransformOrigin;
120 AnimatableProperty<Vector2> mTransformAnchorPoint;
121 AnimatableProperty<Vector4> mTransformOffsetSizeMode;
122 AnimatableProperty<Vector2> mExtraSize;
123 AnimatableProperty<Vector3> mMixColor;
124 AnimatableProperty<float> mPreMultipliedAlpha;
126 void* mExtendedProperties{nullptr}; // Enable derived class to extend properties further
127 void (*mExtendedPropertiesDeleteFunction)(void*){nullptr}; // Derived class's custom delete functor
130 struct AnimatableDecoratedVisualProperties
132 AnimatableDecoratedVisualProperties()
133 : mCornerRadius(Vector4::ZERO),
134 mCornerRadiusPolicy(1.0f),
135 mBorderlineWidth(0.0f),
136 mBorderlineColor(Color::BLACK),
137 mBorderlineOffset(0.0f),
141 ~AnimatableDecoratedVisualProperties()
145 // Delete function of AnimatableDecoratedVisualProperties* converted as void*
146 static void DeleteFunction(void* data)
148 delete static_cast<AnimatableDecoratedVisualProperties*>(data);
151 AnimatableProperty<Vector4> mCornerRadius;
152 AnimatableProperty<float> mCornerRadiusPolicy;
153 AnimatableProperty<float> mBorderlineWidth;
154 AnimatableProperty<Vector4> mBorderlineColor;
155 AnimatableProperty<float> mBorderlineOffset;
156 AnimatableProperty<float> mBlurRadius;
158 } // namespace VisualRenderer
160 class Renderer : public PropertyOwner,
161 public UniformMapDataProvider,
162 public RenderDataProvider
173 * Construct a new Renderer
175 static RendererKey NewKey();
180 ~Renderer() override;
183 * Overriden delete operator
184 * Deletes the renderer from its global memory pool
186 void operator delete(void* ptr);
189 * Get a pointer to the object from the given key.
190 * Used by MemoryPoolKey to provide pointer semantics.
192 static Renderer* Get(RendererKey::KeyType);
195 * Get the key of the given renderer in the associated memory pool.
196 * @param[in] renderer the given renderer
197 * @return The key in the associated memory pool.
199 static RendererKey GetKey(const SceneGraph::Renderer& renderer);
202 * Get the key of the given renderer in the associated memory pool.
203 * @param[in] renderer the given renderer
204 * @return The key in the associated memory pool, or -1 if not
207 static RendererKey GetKey(SceneGraph::Renderer* renderer);
210 * Set the texture set for the renderer
211 * @param[in] textureSet The texture set this renderer will use
213 void SetTextures(TextureSet* textureSet);
216 * Get the associated texture set
217 * @return the texture set.
219 const SceneGraph::TextureSet* GetTextureSet() const
225 * @copydoc RenderDataProvider::GetTextures()
227 const Vector<Render::TextureKey>* GetTextures() const override;
230 * @copydoc RenderDataProvider::GetSamplers()
232 const Vector<Render::Sampler*>* GetSamplers() const override;
235 * Set the shader for the renderer
236 * @param[in] shader The shader this renderer will use
238 void SetShader(Shader* shader);
241 * @copydoc RenderDataProvider::GetShader()
243 const Shader& GetShader() const override
249 * Set the geometry for the renderer
250 * @param[in] geometry The geometry this renderer will use
252 void SetGeometry(Render::Geometry* geometry);
255 * Set the depth index
256 * @param[in] depthIndex the new depth index to use
258 void SetDepthIndex(int depthIndex);
261 * @brief Get the depth index
262 * @return The depth index
264 int GetDepthIndex() const
270 * Set the face culling mode
271 * @param[in] faceCullingMode to use
273 void SetFaceCullingMode(FaceCullingMode::Type faceCullingMode);
276 * Get face culling mode
277 * @return The face culling mode
279 FaceCullingMode::Type GetFaceCullingMode() const;
282 * Set the blending mode
283 * @param[in] blendingMode to use
285 void SetBlendMode(BlendMode::Type blendingMode);
288 * Get the blending mode
289 * @return The the blending mode
291 BlendMode::Type GetBlendMode() const;
294 * Set the blending options. This should only be called from the update thread.
295 * @param[in] options A bitmask of blending options.
297 void SetBlendingOptions(uint32_t options);
300 * Get the blending options
301 * @return The the blending mode
303 uint32_t GetBlendingOptions() const;
306 * Set the blend color for blending operation
307 * @param blendColor to pass to GL
309 void SetBlendColor(const Vector4& blendColor);
312 * Get the blending color
313 * @return The blend color
315 Vector4 GetBlendColor() const;
318 * Set the index of first element for indexed draw
319 * @param[in] firstElement index of first element to draw
321 void SetIndexedDrawFirstElement(uint32_t firstElement);
324 * Get the index of first element for indexed draw
325 * @return The index of first element for indexed draw
327 uint32_t GetIndexedDrawFirstElement() const;
330 * Set the number of elements to draw by indexed draw
331 * @param[in] elementsCount number of elements to draw
333 void SetIndexedDrawElementsCount(uint32_t elementsCount);
336 * Get the number of elements to draw by indexed draw
337 * @return The number of elements to draw by indexed draw
339 uint32_t GetIndexedDrawElementsCount() const;
342 * @brief Set whether the Pre-multiplied Alpha Blending is required
343 * @param[in] preMultipled whether alpha is pre-multiplied.
345 void EnablePreMultipliedAlpha(bool preMultipled);
348 * @brief Query whether alpha is pre-multiplied.
349 * @return True is alpha is pre-multiplied, false otherwise.
351 bool IsPreMultipliedAlphaEnabled() const;
354 * Sets the depth buffer write mode
355 * @param[in] depthWriteMode The depth buffer write mode
357 void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
360 * Get the depth buffer write mode
361 * @return The depth buffer write mode
363 DepthWriteMode::Type GetDepthWriteMode() const;
366 * Sets the depth buffer test mode
367 * @param[in] depthTestMode The depth buffer test mode
369 void SetDepthTestMode(DepthTestMode::Type depthTestMode);
372 * Get the depth buffer test mode
373 * @return The depth buffer test mode
375 DepthTestMode::Type GetDepthTestMode() const;
378 * Sets the depth function
379 * @param[in] depthFunction The depth function
381 void SetDepthFunction(DepthFunction::Type depthFunction);
384 * Get the depth function
385 * @return The depth function
387 DepthFunction::Type GetDepthFunction() const;
390 * Sets the render mode
391 * @param[in] mode The render mode
393 void SetRenderMode(RenderMode::Type mode);
396 * Sets the stencil function
397 * @param[in] stencilFunction The stencil function
399 void SetStencilFunction(StencilFunction::Type stencilFunction);
402 * Sets the stencil function mask
403 * @param[in] stencilFunctionMask The stencil function mask
405 void SetStencilFunctionMask(int stencilFunctionMask);
408 * Sets the stencil function reference
409 * @param[in] stencilFunctionReference The stencil function reference
411 void SetStencilFunctionReference(int stencilFunctionReference);
414 * Sets the stencil mask
415 * @param[in] stencilMask The stencil mask
417 void SetStencilMask(int stencilMask);
420 * Sets the stencil operation for when the stencil test fails
421 * @param[in] stencilOperationOnFail The stencil operation
423 void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
426 * Sets the stencil operation for when the depth test fails
427 * @param[in] stencilOperationOnZFail The stencil operation
429 void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
432 * Sets the stencil operation for when the depth test passes
433 * @param[in] stencilOperationOnZPass The stencil operation
435 void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
438 * Gets the stencil parameters
439 * @return The stencil parameters
441 const Render::Renderer::StencilParameters& GetStencilParameters() const;
445 * @param[in] updateBufferIndex The current update buffer index.
446 * @param[in] opacity The opacity
448 void BakeOpacity(BufferIndex updateBufferIndex, float opacity);
451 * @copydoc RenderDataProvider::GetOpacity()
453 float GetOpacity(BufferIndex updateBufferIndex) const override;
456 * Sets the rendering behavior
457 * @param[in] renderingBehavior The rendering behavior required.
459 void SetRenderingBehavior(DevelRenderer::Rendering::Type renderingBehavior);
462 * Gets the rendering behavior
463 * @return The rendering behavior
465 DevelRenderer::Rendering::Type GetRenderingBehavior() const;
468 * Prepare the object for rendering.
469 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
470 * @param[in] updateBufferIndex The current update buffer index.
471 * @return Whether this renderer has been updated in the current frame
473 bool PrepareRender(BufferIndex updateBufferIndex);
476 * Retrieve the Render thread renderer
477 * @return The associated render thread renderer
479 Render::RendererKey GetRenderer();
482 * Query whether the renderer is fully opaque, fully transparent or transparent.
483 * @param[in] updateBufferIndex The current update buffer index.
484 * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
486 OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
489 * Connect the object to the scene graph
491 * @param[in] sceneController The scene controller - used for sending messages to render thread
492 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
494 void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
497 * Disconnect the object from the scene graph
498 * @param[in] sceneController The scene controller - used for sending messages to render thread
499 * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
501 void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
504 * @copydoc RenderDataProvider::GetUniformMapDataProvider()
506 const UniformMapDataProvider& GetUniformMapDataProvider() const override
512 * @copydoc RenderDataProvider::IsUpdated()
514 bool IsUpdated() const override;
517 * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
519 Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
522 * Sets RenderCallback object
524 * @param[in] callback Valid pointer to RenderCallback object
526 void SetRenderCallback(RenderCallback* callback);
529 * Returns currently set RenderCallback pointer
531 * @return RenderCallback pointer or nullptr
533 RenderCallback* GetRenderCallback()
535 return mRenderCallback;
539 * Merge shader uniform map into renderer uniform map if any of the
540 * maps have changed. Only update uniform map if added to render
542 * @param[in] updateBufferIndex The current update buffer index.
544 void UpdateUniformMap(BufferIndex updateBufferIndex);
547 * Set the given external draw commands on this renderer.
549 void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
552 * Query whether a renderer is dirty.
553 * @return true if the renderer is dirty.
554 * @note It is used to decide whether to reuse the RenderList. We can't reuse the RenderList if this is dirty.
556 bool IsDirty() const;
559 * Reset both dirty flag and updated flag.
560 * @note This is called after rendering has completed.
562 void ResetDirtyFlag();
565 * Get the capacity of the memory pools
566 * @return the capacity of the memory pools
568 static uint32_t GetMemoryPoolCapacity();
570 public: // PropertyOwner::MappingChanged
572 * @copydoc PropertyOwner::OnMappingChanged
574 void OnMappingChanged() override;
576 public: // PropertyOwner implementation
578 * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
580 virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
582 public: // From UniformMapDataProvider
584 * @copydoc UniformMapDataProvider::GetCollectedUniformMap
586 const CollectedUniformMap& GetCollectedUniformMap() const override;
588 public: // For VisualProperties
590 * To be used only for 1st stage initialization in event thread.
592 void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
594 mVisualProperties = visualProperties;
598 * May be accessed from event thread
600 const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
602 return mVisualProperties.Get();
607 * Protected constructor; See also Renderer::New()
620 CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
622 SceneController* mSceneController; ///< Used for initializing renderers
623 Render::RendererKey mRenderer; ///< Key to the renderer (that's owned by RenderManager)
624 TextureSet* mTextureSet; ///< The texture set this renderer uses. (Not owned)
625 Render::Geometry* mGeometry; ///< The geometry this renderer uses. (Not owned)
626 Shader* mShader; ///< The shader this renderer uses. (Not owned)
628 OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
629 OwnerPointer<Vector4> mBlendColor; ///< The blend color for blending operation
631 Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
633 uint32_t mIndexedDrawFirstElement; ///< first element index to be drawn using indexed draw
634 uint32_t mIndexedDrawElementsCount; ///< number of elements to be drawn using indexed draw
635 uint32_t mBlendBitmask; ///< The bitmask of blending options
636 uint32_t mResendFlag; ///< Indicate whether data should be resent to the renderer
637 UniformMap::SizeType mUniformMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
638 UniformMap::SizeType mShaderMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
640 DepthFunction::Type mDepthFunction : 4; ///< Local copy of the depth function
641 FaceCullingMode::Type mFaceCullingMode : 3; ///< Local copy of the mode of face culling
642 BlendMode::Type mBlendMode : 3; ///< Local copy of the mode of blending
643 DepthWriteMode::Type mDepthWriteMode : 3; ///< Local copy of the depth write mode
644 DepthTestMode::Type mDepthTestMode : 3; ///< Local copy of the depth test mode
645 DevelRenderer::Rendering::Type mRenderingBehavior : 2; ///< The rendering behavior
646 Decay mUpdateDecay : 2; ///< Update decay (aging)
648 bool mRegenerateUniformMap : 1; ///< true if the map should be regenerated
649 bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
650 bool mDirtyFlag : 1; ///< Flag indicating whether the properties are changed
652 std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
653 Dali::RenderCallback* mRenderCallback{nullptr};
656 AnimatableProperty<float> mOpacity; ///< The opacity value
657 int32_t mDepthIndex; ///< Used only in PrepareRenderInstructions
660 } // namespace SceneGraph
661 } // namespace Internal
664 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H