889a791b0e2ab5f8353649a604ff17737343d5b5
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-renderer.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
2 #define DALI_INTERNAL_SCENE_GRAPH_RENDERER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Render
37 {
38 class Renderer;
39 class Geometry;
40 } // namespace Render
41
42 namespace SceneGraph
43 {
44 class SceneController;
45
46 class Renderer;
47 class TextureSet;
48 class Geometry;
49
50 using RendererKey = MemoryPoolKey<SceneGraph::Renderer>;
51
52 } // namespace SceneGraph
53 } // namespace Internal
54
55 // Ensure RendererKey can be used in Dali::Vector
56 template<>
57 struct TypeTraits<Internal::SceneGraph::RendererKey> : public BasicTypes<Internal::SceneGraph::RendererKey>
58 {
59   enum
60   {
61     IS_TRIVIAL_TYPE = true
62   };
63 };
64
65 namespace Internal
66 {
67 namespace SceneGraph
68 {
69 using RendererContainer = Dali::Vector<RendererKey>;
70 using RendererIter      = RendererContainer::Iterator;
71 using RendererConstIter = RendererContainer::ConstIterator;
72
73 namespace VisualRenderer
74 {
75 struct AnimatableVisualProperties
76 {
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)
87   {
88   }
89
90   ~AnimatableVisualProperties()
91   {
92     if(mExtendedProperties && mExtendedPropertiesDeleteFunction)
93     {
94       mExtendedPropertiesDeleteFunction(mExtendedProperties);
95     }
96   }
97
98   /**
99    * @brief Cached coefficient value when we calculate visual transformed update size.
100    * It can reduce complexity of calculate the vertex position.
101    *
102    * Vector2 vertexPosition = (XA * aPosition + XB) * originalSize + (CA * aPosition + CB) + Vector2(D, D) * aPosition
103    */
104   struct VisualTransformedUpdateSizeCoefficientCache
105   {
106     Vector2 coefXA{Vector2::ZERO};
107     Vector2 coefXB{Vector2::ZERO};
108     Vector2 coefCA{Vector2::ZERO};
109     Vector2 coefCB{Vector2::ZERO};
110     float   coefD{0.0f};
111
112     uint64_t hash{0u};
113     uint64_t decoratedHash{0u};
114   };
115   VisualTransformedUpdateSizeCoefficientCache mCoefficient; ///< Coefficient value to calculate visual transformed update size by VisualProperties more faster.
116
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;
125
126   void* mExtendedProperties{nullptr};                        // Enable derived class to extend properties further
127   void (*mExtendedPropertiesDeleteFunction)(void*){nullptr}; // Derived class's custom delete functor
128 };
129
130 struct AnimatableDecoratedVisualProperties
131 {
132   AnimatableDecoratedVisualProperties()
133   : mCornerRadius(Vector4::ZERO),
134     mCornerRadiusPolicy(1.0f),
135     mBorderlineWidth(0.0f),
136     mBorderlineColor(Color::BLACK),
137     mBorderlineOffset(0.0f),
138     mBlurRadius(0.0f)
139   {
140   }
141   ~AnimatableDecoratedVisualProperties()
142   {
143   }
144
145   // Delete function of AnimatableDecoratedVisualProperties* converted as void*
146   static void DeleteFunction(void* data)
147   {
148     delete static_cast<AnimatableDecoratedVisualProperties*>(data);
149   }
150
151   AnimatableProperty<Vector4> mCornerRadius;
152   AnimatableProperty<float>   mCornerRadiusPolicy;
153   AnimatableProperty<float>   mBorderlineWidth;
154   AnimatableProperty<Vector4> mBorderlineColor;
155   AnimatableProperty<float>   mBorderlineOffset;
156   AnimatableProperty<float>   mBlurRadius;
157 };
158 } // namespace VisualRenderer
159
160 class Renderer : public PropertyOwner,
161                  public UniformMapDataProvider,
162                  public RenderDataProvider
163 {
164 public:
165   enum OpacityType
166   {
167     OPAQUE,
168     TRANSPARENT,
169     TRANSLUCENT
170   };
171
172   /**
173    * Construct a new Renderer
174    */
175   static RendererKey NewKey();
176
177   /**
178    * Destructor
179    */
180   ~Renderer() override;
181
182   /**
183    * Overriden delete operator
184    * Deletes the renderer from its global memory pool
185    */
186   void operator delete(void* ptr);
187
188   /**
189    * Get a pointer to the object from the given key.
190    * Used by MemoryPoolKey to provide pointer semantics.
191    */
192   static Renderer* Get(RendererKey::KeyType);
193
194   /**
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.
198    */
199   static RendererKey GetKey(const SceneGraph::Renderer& renderer);
200
201   /**
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
205    * found.
206    */
207   static RendererKey GetKey(SceneGraph::Renderer* renderer);
208
209   /**
210    * Set the texture set for the renderer
211    * @param[in] textureSet The texture set this renderer will use
212    */
213   void SetTextures(TextureSet* textureSet);
214
215   /**
216    * Get the associated texture set
217    * @return the texture set.
218    */
219   const SceneGraph::TextureSet* GetTextureSet() const
220   {
221     return mTextureSet;
222   }
223
224   /**
225    * @copydoc RenderDataProvider::GetTextures()
226    */
227   const Vector<Render::TextureKey>* GetTextures() const override;
228
229   /**
230    * @copydoc RenderDataProvider::GetSamplers()
231    */
232   const Vector<Render::Sampler*>* GetSamplers() const override;
233
234   /**
235    * Set the shader for the renderer
236    * @param[in] shader The shader this renderer will use
237    */
238   void SetShader(Shader* shader);
239
240   /**
241    * @copydoc RenderDataProvider::GetShader()
242    */
243   const Shader& GetShader() const override
244   {
245     return *mShader;
246   }
247
248   /**
249    * Set the geometry for the renderer
250    * @param[in] geometry The geometry this renderer will use
251    */
252   void SetGeometry(Render::Geometry* geometry);
253
254   /**
255    * Set the depth index
256    * @param[in] depthIndex the new depth index to use
257    */
258   void SetDepthIndex(int depthIndex);
259
260   /**
261    * @brief Get the depth index
262    * @return The depth index
263    */
264   int GetDepthIndex() const
265   {
266     return mDepthIndex;
267   }
268
269   /**
270    * Set the face culling mode
271    * @param[in] faceCullingMode to use
272    */
273   void SetFaceCullingMode(FaceCullingMode::Type faceCullingMode);
274
275   /**
276    * Get face culling mode
277    * @return The face culling mode
278    */
279   FaceCullingMode::Type GetFaceCullingMode() const;
280
281   /**
282    * Set the blending mode
283    * @param[in] blendingMode to use
284    */
285   void SetBlendMode(BlendMode::Type blendingMode);
286
287   /**
288    * Get the blending mode
289    * @return The the blending mode
290    */
291   BlendMode::Type GetBlendMode() const;
292
293   /**
294    * Set the blending options. This should only be called from the update thread.
295    * @param[in] options A bitmask of blending options.
296    */
297   void SetBlendingOptions(uint32_t options);
298
299   /**
300    * Get the blending options
301    * @return The the blending mode
302    */
303   uint32_t GetBlendingOptions() const;
304
305   /**
306    * Set the blend color for blending operation
307    * @param blendColor to pass to GL
308    */
309   void SetBlendColor(const Vector4& blendColor);
310
311   /**
312    * Get the blending color
313    * @return The blend color
314    */
315   Vector4 GetBlendColor() const;
316
317   /**
318    * Set the index of first element for indexed draw
319    * @param[in] firstElement index of first element to draw
320    */
321   void SetIndexedDrawFirstElement(uint32_t firstElement);
322
323   /**
324    * Get the index of first element for indexed draw
325    * @return The index of first element for indexed draw
326    */
327   uint32_t GetIndexedDrawFirstElement() const;
328
329   /**
330    * Set the number of elements to draw by indexed draw
331    * @param[in] elementsCount number of elements to draw
332    */
333   void SetIndexedDrawElementsCount(uint32_t elementsCount);
334
335   /**
336    * Get the number of elements to draw by indexed draw
337    * @return The number of elements to draw by indexed draw
338    */
339   uint32_t GetIndexedDrawElementsCount() const;
340
341   /**
342    * @brief Set whether the Pre-multiplied Alpha Blending is required
343    * @param[in] preMultipled whether alpha is pre-multiplied.
344    */
345   void EnablePreMultipliedAlpha(bool preMultipled);
346
347   /**
348    * @brief Query whether alpha is pre-multiplied.
349    * @return True is alpha is pre-multiplied, false otherwise.
350    */
351   bool IsPreMultipliedAlphaEnabled() const;
352
353   /**
354    * Sets the depth buffer write mode
355    * @param[in] depthWriteMode The depth buffer write mode
356    */
357   void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
358
359   /**
360    * Get the depth buffer write mode
361    * @return The depth buffer write mode
362    */
363   DepthWriteMode::Type GetDepthWriteMode() const;
364
365   /**
366    * Sets the depth buffer test mode
367    * @param[in] depthTestMode The depth buffer test mode
368    */
369   void SetDepthTestMode(DepthTestMode::Type depthTestMode);
370
371   /**
372    * Get the depth buffer test mode
373    * @return The depth buffer test mode
374    */
375   DepthTestMode::Type GetDepthTestMode() const;
376
377   /**
378    * Sets the depth function
379    * @param[in] depthFunction The depth function
380    */
381   void SetDepthFunction(DepthFunction::Type depthFunction);
382
383   /**
384    * Get the depth function
385    * @return The depth function
386    */
387   DepthFunction::Type GetDepthFunction() const;
388
389   /**
390    * Sets the render mode
391    * @param[in] mode The render mode
392    */
393   void SetRenderMode(RenderMode::Type mode);
394
395   /**
396    * Sets the stencil function
397    * @param[in] stencilFunction The stencil function
398    */
399   void SetStencilFunction(StencilFunction::Type stencilFunction);
400
401   /**
402    * Sets the stencil function mask
403    * @param[in] stencilFunctionMask The stencil function mask
404    */
405   void SetStencilFunctionMask(int stencilFunctionMask);
406
407   /**
408    * Sets the stencil function reference
409    * @param[in] stencilFunctionReference The stencil function reference
410    */
411   void SetStencilFunctionReference(int stencilFunctionReference);
412
413   /**
414    * Sets the stencil mask
415    * @param[in] stencilMask The stencil mask
416    */
417   void SetStencilMask(int stencilMask);
418
419   /**
420    * Sets the stencil operation for when the stencil test fails
421    * @param[in] stencilOperationOnFail The stencil operation
422    */
423   void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
424
425   /**
426    * Sets the stencil operation for when the depth test fails
427    * @param[in] stencilOperationOnZFail The stencil operation
428    */
429   void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
430
431   /**
432    * Sets the stencil operation for when the depth test passes
433    * @param[in] stencilOperationOnZPass The stencil operation
434    */
435   void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
436
437   /**
438    * Gets the stencil parameters
439    * @return The stencil parameters
440    */
441   const Render::Renderer::StencilParameters& GetStencilParameters() const;
442
443   /**
444    * Bakes the opacity
445    * @param[in] updateBufferIndex The current update buffer index.
446    * @param[in] opacity The opacity
447    */
448   void BakeOpacity(BufferIndex updateBufferIndex, float opacity);
449
450   /**
451    * @copydoc RenderDataProvider::GetOpacity()
452    */
453   float GetOpacity(BufferIndex updateBufferIndex) const override;
454
455   /**
456    * Sets the rendering behavior
457    * @param[in] renderingBehavior The rendering behavior required.
458    */
459   void SetRenderingBehavior(DevelRenderer::Rendering::Type renderingBehavior);
460
461   /**
462    * Gets the rendering behavior
463    * @return The rendering behavior
464    */
465   DevelRenderer::Rendering::Type GetRenderingBehavior() const;
466
467   /**
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
472    */
473   bool PrepareRender(BufferIndex updateBufferIndex);
474
475   /**
476    * Retrieve the Render thread renderer
477    * @return The associated render thread renderer
478    */
479   Render::RendererKey GetRenderer();
480
481   /**
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
485    */
486   OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
487
488   /**
489    * Connect the object to the scene graph
490    *
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
493    */
494   void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
495
496   /**
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
500    */
501   void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
502
503   /**
504    * @copydoc RenderDataProvider::GetUniformMapDataProvider()
505    */
506   const UniformMapDataProvider& GetUniformMapDataProvider() const override
507   {
508     return *this;
509   };
510
511   /**
512    * @copydoc RenderDataProvider::IsUpdated()
513    */
514   bool IsUpdated() const override
515   {
516     return Updated();
517   }
518
519   /**
520    * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
521    */
522   Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
523
524   /**
525    * Sets RenderCallback object
526    *
527    * @param[in] callback Valid pointer to RenderCallback object
528    */
529   void SetRenderCallback(RenderCallback* callback);
530
531   /**
532    * Returns currently set RenderCallback pointer
533    *
534    * @return RenderCallback pointer or nullptr
535    */
536   RenderCallback* GetRenderCallback()
537   {
538     return mRenderCallback;
539   }
540
541   /**
542    * Merge shader uniform map into renderer uniform map if any of the
543    * maps have changed.  Only update uniform map if added to render
544    * instructions.
545    * @param[in] updateBufferIndex The current update buffer index.
546    */
547   void UpdateUniformMap(BufferIndex updateBufferIndex);
548
549   /**
550    * Set the given external draw commands on this renderer.
551    */
552   void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
553
554   /**
555    * Query whether a renderer is dirty.
556    * @return true if the renderer is dirty.
557    * @note It is used to decide whether to reuse the RenderList. We can't reuse the RenderList if this is dirty.
558    */
559   bool IsDirty() const;
560
561   /**
562    * Reset both dirty flag and updated flag.
563    * @note This is called after rendering has completed.
564    */
565   void ResetDirtyFlag();
566
567   /**
568    * Get the capacity of the memory pools
569    * @return the capacity of the memory pools
570    */
571   static uint32_t GetMemoryPoolCapacity();
572
573 public: // PropertyOwner::MappingChanged
574   /**
575    * @copydoc PropertyOwner::OnMappingChanged
576    */
577   void OnMappingChanged() override;
578
579 public: // PropertyOwner implementation
580   /**
581    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
582    */
583   virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
584
585 public: // From UniformMapDataProvider
586   /**
587    * @copydoc UniformMapDataProvider::GetCollectedUniformMap
588    */
589   const CollectedUniformMap& GetCollectedUniformMap() const override;
590
591 public: // For VisualProperties
592   /**
593    * To be used only for 1st stage initialization in event thread.
594    */
595   void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
596   {
597     mVisualProperties = visualProperties;
598   }
599
600   /**
601    * May be accessed from event thread
602    */
603   const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
604   {
605     return mVisualProperties.Get();
606   }
607
608 private:
609   /**
610    * Protected constructor; See also Renderer::New()
611    */
612   Renderer();
613
614 private:
615   enum Decay
616   {
617     DONE    = 0,
618     LAST    = 1,
619     INITIAL = 2
620   };
621
622 private:
623   CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
624
625   SceneController*    mSceneController; ///< Used for initializing renderers
626   Render::RendererKey mRenderer;        ///< Key to the renderer (that's owned by RenderManager)
627   TextureSet*         mTextureSet;      ///< The texture set this renderer uses. (Not owned)
628   Render::Geometry*   mGeometry;        ///< The geometry this renderer uses. (Not owned)
629   Shader*             mShader;          ///< The shader this renderer uses. (Not owned)
630
631   OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
632   OwnerPointer<Vector4>                                    mBlendColor;                ///< The blend color for blending operation
633
634   Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
635
636   uint64_t             mUniformsHash{0};             ///< Hash of uniform map property values
637   uint32_t             mIndexedDrawFirstElement;     ///< first element index to be drawn using indexed draw
638   uint32_t             mIndexedDrawElementsCount;    ///< number of elements to be drawn using indexed draw
639   uint32_t             mBlendBitmask;                ///< The bitmask of blending options
640   uint32_t             mResendFlag;                  ///< Indicate whether data should be resent to the renderer
641   UniformMap::SizeType mUniformMapChangeCounter{0u}; ///< Value to check if uniform data should be updated
642   UniformMap::SizeType mShaderMapChangeCounter{0u};  ///< Value to check if uniform data should be updated
643
644   DepthFunction::Type            mDepthFunction : 4;     ///< Local copy of the depth function
645   FaceCullingMode::Type          mFaceCullingMode : 3;   ///< Local copy of the mode of face culling
646   BlendMode::Type                mBlendMode : 3;         ///< Local copy of the mode of blending
647   DepthWriteMode::Type           mDepthWriteMode : 3;    ///< Local copy of the depth write mode
648   DepthTestMode::Type            mDepthTestMode : 3;     ///< Local copy of the depth test mode
649   DevelRenderer::Rendering::Type mRenderingBehavior : 2; ///< The rendering behavior
650   Decay                          mUpdateDecay : 2;       ///< Update decay (aging)
651
652   bool mRegenerateUniformMap : 1;     ///< true if the map should be regenerated
653   bool mPremultipledAlphaEnabled : 1; ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
654   bool mDirtyFlag : 1;                ///< Flag indicating whether the properties are changed
655
656   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
657   Dali::RenderCallback*                         mRenderCallback{nullptr};
658
659 public:
660   AnimatableProperty<float> mOpacity;    ///< The opacity value
661   int32_t                   mDepthIndex; ///< Used only in PrepareRenderInstructions
662 };
663
664 } // namespace SceneGraph
665 } // namespace Internal
666 } // namespace Dali
667
668 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H