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