Decrease Render::Renderer index map if detached
[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();
395
396   /**
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
400    */
401   OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
402
403   /**
404    * Connect the object to the scene graph
405    *
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
408    */
409   void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
410
411   /**
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
415    */
416   void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
417
418   /**
419    * Detached from the scene graph object.
420    * @param[in] node node who detach this renderer.
421    */
422   void DetachFromNodeDataProvider(const NodeDataProvider& node);
423
424   /**
425    * @copydoc RenderDataProvider::GetUniformMapDataProvider()
426    */
427   const UniformMapDataProvider& GetUniformMapDataProvider() const override
428   {
429     return *this;
430   };
431
432   /**
433    * @copydoc RenderDataProvider::IsUpdated()
434    */
435   bool IsUpdated() const override;
436
437   /**
438    * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
439    */
440   Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
441
442   /**
443    * Sets RenderCallback object
444    *
445    * @param[in] callback Valid pointer to RenderCallback object
446    */
447   void SetRenderCallback(RenderCallback* callback);
448
449   /**
450    * Returns currently set RenderCallback pointer
451    *
452    * @return RenderCallback pointer or nullptr
453    */
454   RenderCallback* GetRenderCallback()
455   {
456     return mRenderCallback;
457   }
458
459   /**
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
462    * instructions.
463    * @param[in] updateBufferIndex The current update buffer index.
464    */
465   void UpdateUniformMap(BufferIndex updateBufferIndex);
466
467   /**
468    * Set the given external draw commands on this renderer.
469    */
470   void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
471
472   /**
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.
476    */
477   bool IsDirty() const;
478
479   /**
480    * Reset both dirty flag and updated flag.
481    * @note This is called after rendering has completed.
482    */
483   void ResetDirtyFlag();
484
485   /**
486    * @brief Reset to base values of all animatable properties.
487    *
488    * @param[in] updateBufferIndex the current buffer index
489    */
490   void ResetToBaseValues(BufferIndex updateBufferIndex);
491
492   /**
493    * @brief Mark all animatable properties as dirty.
494    */
495   void MarkAsDirty();
496
497   /**
498    * Get the capacity of the memory pools
499    * @return the capacity of the memory pools
500    */
501   static uint32_t GetMemoryPoolCapacity();
502
503 public: // PropertyOwner::MappingChanged
504   /**
505    * @copydoc PropertyOwner::OnMappingChanged
506    */
507   void OnMappingChanged() override;
508
509 public: // PropertyOwner implementation
510   /**
511    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
512    */
513   virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
514
515   /**
516    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::AddInitializeResetter
517    */
518   void AddInitializeResetter(ResetterManager& manager) const override;
519
520 public: // From UniformMapDataProvider
521   /**
522    * @copydoc UniformMapDataProvider::GetCollectedUniformMap
523    */
524   const CollectedUniformMap& GetCollectedUniformMap() const override;
525
526 public: // For VisualProperties
527   /**
528    * To be used only for 1st stage initialization in event thread.
529    */
530   void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
531   {
532     mVisualProperties = visualProperties;
533   }
534
535   /**
536    * May be accessed from event thread
537    */
538   const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
539   {
540     return mVisualProperties.Get();
541   }
542
543 private:
544   /**
545    * Protected constructor; See also Renderer::New()
546    */
547   Renderer();
548
549 private:
550   enum Decay
551   {
552     DONE    = 0,
553     LAST    = 1,
554     INITIAL = 2
555   };
556
557 private:
558   CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
559
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)
565
566   OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
567   OwnerPointer<Vector4>                                    mBlendColor;                ///< The blend color for blending operation
568
569   Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
570
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
577
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)
585
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
589
590   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
591   Dali::RenderCallback*                         mRenderCallback{nullptr};
592
593 public:
594   AnimatableProperty<float> mOpacity;    ///< The opacity value
595   int32_t                   mDepthIndex; ///< Used only in PrepareRenderInstructions
596 };
597
598 } // namespace SceneGraph
599 } // namespace Internal
600 } // namespace Dali
601
602 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H