Merge "Add BuildPickingRay to devel api" into devel/master
[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 using RendererContainer = Dali::Vector<RendererKey>;
71 using RendererIter      = RendererContainer::Iterator;
72 using RendererConstIter = RendererContainer::ConstIterator;
73
74 class Renderer : public PropertyOwner,
75                  public UniformMapDataProvider,
76                  public RenderDataProvider
77 {
78 public:
79   enum OpacityType
80   {
81     OPAQUE,
82     TRANSPARENT,
83     TRANSLUCENT
84   };
85
86   /**
87    * Construct a new Renderer
88    */
89   static RendererKey NewKey();
90
91   /**
92    * Destructor
93    */
94   ~Renderer() override;
95
96   /**
97    * Overriden delete operator
98    * Deletes the renderer from its global memory pool
99    */
100   void operator delete(void* ptr);
101
102   /**
103    * Get a pointer to the object from the given key.
104    * Used by MemoryPoolKey to provide pointer semantics.
105    */
106   static Renderer* Get(RendererKey::KeyType);
107
108   /**
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.
112    */
113   static RendererKey GetKey(const SceneGraph::Renderer& renderer);
114
115   /**
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
119    * found.
120    */
121   static RendererKey GetKey(SceneGraph::Renderer* renderer);
122
123   /**
124    * Set the texture set for the renderer
125    * @param[in] textureSet The texture set this renderer will use
126    */
127   void SetTextures(TextureSet* textureSet);
128
129   /**
130    * Get the associated texture set
131    * @return the texture set.
132    */
133   const SceneGraph::TextureSet* GetTextureSet() const
134   {
135     return mTextureSet;
136   }
137
138   /**
139    * @copydoc RenderDataProvider::GetTextures()
140    */
141   const Vector<Render::TextureKey>* GetTextures() const override;
142
143   /**
144    * @copydoc RenderDataProvider::GetSamplers()
145    */
146   const Vector<Render::Sampler*>* GetSamplers() const override;
147
148   /**
149    * Set the shader for the renderer
150    * @param[in] shader The shader this renderer will use
151    */
152   void SetShader(Shader* shader);
153
154   /**
155    * @copydoc RenderDataProvider::GetShader()
156    */
157   const Shader& GetShader() const override
158   {
159     return *mShader;
160   }
161
162   /**
163    * Set the geometry for the renderer
164    * @param[in] geometry The geometry this renderer will use
165    */
166   void SetGeometry(Render::Geometry* geometry);
167
168   /**
169    * Set the depth index
170    * @param[in] depthIndex the new depth index to use
171    */
172   void SetDepthIndex(int depthIndex);
173
174   /**
175    * @brief Get the depth index
176    * @return The depth index
177    */
178   int GetDepthIndex() const
179   {
180     return mDepthIndex;
181   }
182
183   /**
184    * Set the face culling mode
185    * @param[in] faceCullingMode to use
186    */
187   void SetFaceCullingMode(FaceCullingMode::Type faceCullingMode);
188
189   /**
190    * Get face culling mode
191    * @return The face culling mode
192    */
193   FaceCullingMode::Type GetFaceCullingMode() const;
194
195   /**
196    * Set the blending mode
197    * @param[in] blendingMode to use
198    */
199   void SetBlendMode(BlendMode::Type blendingMode);
200
201   /**
202    * Get the blending mode
203    * @return The the blending mode
204    */
205   BlendMode::Type GetBlendMode() const;
206
207   /**
208    * Set the blending options. This should only be called from the update thread.
209    * @param[in] options A bitmask of blending options.
210    */
211   void SetBlendingOptions(uint32_t options);
212
213   /**
214    * Get the blending options
215    * @return The the blending mode
216    */
217   uint32_t GetBlendingOptions() const;
218
219   /**
220    * Set the blend color for blending operation
221    * @param blendColor to pass to GL
222    */
223   void SetBlendColor(const Vector4& blendColor);
224
225   /**
226    * Get the blending color
227    * @return The blend color
228    */
229   Vector4 GetBlendColor() const;
230
231   /**
232    * Set the index of first element for indexed draw
233    * @param[in] firstElement index of first element to draw
234    */
235   void SetIndexedDrawFirstElement(uint32_t firstElement);
236
237   /**
238    * Get the index of first element for indexed draw
239    * @return The index of first element for indexed draw
240    */
241   uint32_t GetIndexedDrawFirstElement() const;
242
243   /**
244    * Set the number of elements to draw by indexed draw
245    * @param[in] elementsCount number of elements to draw
246    */
247   void SetIndexedDrawElementsCount(uint32_t elementsCount);
248
249   /**
250    * Get the number of elements to draw by indexed draw
251    * @return The number of elements to draw by indexed draw
252    */
253   uint32_t GetIndexedDrawElementsCount() const;
254
255   /**
256    * @brief Set whether the Pre-multiplied Alpha Blending is required
257    * @param[in] preMultipled whether alpha is pre-multiplied.
258    */
259   void EnablePreMultipliedAlpha(bool preMultipled);
260
261   /**
262    * @brief Query whether alpha is pre-multiplied.
263    * @return True is alpha is pre-multiplied, false otherwise.
264    */
265   bool IsPreMultipliedAlphaEnabled() const;
266
267   /**
268    * Sets the depth buffer write mode
269    * @param[in] depthWriteMode The depth buffer write mode
270    */
271   void SetDepthWriteMode(DepthWriteMode::Type depthWriteMode);
272
273   /**
274    * Get the depth buffer write mode
275    * @return The depth buffer write mode
276    */
277   DepthWriteMode::Type GetDepthWriteMode() const;
278
279   /**
280    * Sets the depth buffer test mode
281    * @param[in] depthTestMode The depth buffer test mode
282    */
283   void SetDepthTestMode(DepthTestMode::Type depthTestMode);
284
285   /**
286    * Get the depth buffer test mode
287    * @return The depth buffer test mode
288    */
289   DepthTestMode::Type GetDepthTestMode() const;
290
291   /**
292    * Sets the depth function
293    * @param[in] depthFunction The depth function
294    */
295   void SetDepthFunction(DepthFunction::Type depthFunction);
296
297   /**
298    * Get the depth function
299    * @return The depth function
300    */
301   DepthFunction::Type GetDepthFunction() const;
302
303   /**
304    * Sets the render mode
305    * @param[in] mode The render mode
306    */
307   void SetRenderMode(RenderMode::Type mode);
308
309   /**
310    * Sets the stencil function
311    * @param[in] stencilFunction The stencil function
312    */
313   void SetStencilFunction(StencilFunction::Type stencilFunction);
314
315   /**
316    * Sets the stencil function mask
317    * @param[in] stencilFunctionMask The stencil function mask
318    */
319   void SetStencilFunctionMask(int stencilFunctionMask);
320
321   /**
322    * Sets the stencil function reference
323    * @param[in] stencilFunctionReference The stencil function reference
324    */
325   void SetStencilFunctionReference(int stencilFunctionReference);
326
327   /**
328    * Sets the stencil mask
329    * @param[in] stencilMask The stencil mask
330    */
331   void SetStencilMask(int stencilMask);
332
333   /**
334    * Sets the stencil operation for when the stencil test fails
335    * @param[in] stencilOperationOnFail The stencil operation
336    */
337   void SetStencilOperationOnFail(StencilOperation::Type stencilOperationOnFail);
338
339   /**
340    * Sets the stencil operation for when the depth test fails
341    * @param[in] stencilOperationOnZFail The stencil operation
342    */
343   void SetStencilOperationOnZFail(StencilOperation::Type stencilOperationOnZFail);
344
345   /**
346    * Sets the stencil operation for when the depth test passes
347    * @param[in] stencilOperationOnZPass The stencil operation
348    */
349   void SetStencilOperationOnZPass(StencilOperation::Type stencilOperationOnZPass);
350
351   /**
352    * Gets the stencil parameters
353    * @return The stencil parameters
354    */
355   const Render::Renderer::StencilParameters& GetStencilParameters() const;
356
357   /**
358    * Bakes the opacity
359    * @param[in] updateBufferIndex The current update buffer index.
360    * @param[in] opacity The opacity
361    */
362   void BakeOpacity(BufferIndex updateBufferIndex, float opacity);
363
364   /**
365    * @copydoc RenderDataProvider::GetOpacity()
366    */
367   float GetOpacity(BufferIndex updateBufferIndex) const override;
368
369   /**
370    * Sets the rendering behavior
371    * @param[in] renderingBehavior The rendering behavior required.
372    */
373   void SetRenderingBehavior(DevelRenderer::Rendering::Type renderingBehavior);
374
375   /**
376    * Gets the rendering behavior
377    * @return The rendering behavior
378    */
379   DevelRenderer::Rendering::Type GetRenderingBehavior() const;
380
381   /**
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
386    */
387   bool PrepareRender(BufferIndex updateBufferIndex);
388
389   /**
390    * Retrieve the Render thread renderer
391    * @return The associated render thread renderer
392    */
393   Render::RendererKey GetRenderer();
394
395   /**
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
399    */
400   OpacityType GetOpacityType(BufferIndex updateBufferIndex, const Node& node) const;
401
402   /**
403    * Connect the object to the scene graph
404    *
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
407    */
408   void ConnectToSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
409
410   /**
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
414    */
415   void DisconnectFromSceneGraph(SceneController& sceneController, BufferIndex bufferIndex);
416
417   /**
418    * @copydoc RenderDataProvider::GetUniformMapDataProvider()
419    */
420   const UniformMapDataProvider& GetUniformMapDataProvider() const override
421   {
422     return *this;
423   };
424
425   /**
426    * @copydoc RenderDataProvider::IsUpdated()
427    */
428   bool IsUpdated() const override;
429
430   /**
431    * @copydoc RenderDataProvider::GetVisualTransformedUpdateArea()
432    */
433   Vector4 GetVisualTransformedUpdateArea(BufferIndex updateBufferIndex, const Vector4& originalUpdateArea) noexcept override;
434
435   /**
436    * Sets RenderCallback object
437    *
438    * @param[in] callback Valid pointer to RenderCallback object
439    */
440   void SetRenderCallback(RenderCallback* callback);
441
442   /**
443    * Returns currently set RenderCallback pointer
444    *
445    * @return RenderCallback pointer or nullptr
446    */
447   RenderCallback* GetRenderCallback()
448   {
449     return mRenderCallback;
450   }
451
452   /**
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
455    * instructions.
456    * @param[in] updateBufferIndex The current update buffer index.
457    */
458   void UpdateUniformMap(BufferIndex updateBufferIndex);
459
460   /**
461    * Set the given external draw commands on this renderer.
462    */
463   void SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands, uint32_t size);
464
465   /**
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.
469    */
470   bool IsDirty() const;
471
472   /**
473    * Reset both dirty flag and updated flag.
474    * @note This is called after rendering has completed.
475    */
476   void ResetDirtyFlag();
477
478   /**
479    * Get the capacity of the memory pools
480    * @return the capacity of the memory pools
481    */
482   static uint32_t GetMemoryPoolCapacity();
483
484 public: // PropertyOwner::MappingChanged
485   /**
486    * @copydoc PropertyOwner::OnMappingChanged
487    */
488   void OnMappingChanged() override;
489
490 public: // PropertyOwner implementation
491   /**
492    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
493    */
494   virtual void ResetDefaultProperties(BufferIndex updateBufferIndex){};
495
496 public: // From UniformMapDataProvider
497   /**
498    * @copydoc UniformMapDataProvider::GetCollectedUniformMap
499    */
500   const CollectedUniformMap& GetCollectedUniformMap() const override;
501
502 public: // For VisualProperties
503   /**
504    * To be used only for 1st stage initialization in event thread.
505    */
506   void SetVisualProperties(VisualRenderer::AnimatableVisualProperties* visualProperties)
507   {
508     mVisualProperties = visualProperties;
509   }
510
511   /**
512    * May be accessed from event thread
513    */
514   const VisualRenderer::AnimatableVisualProperties* GetVisualProperties() const
515   {
516     return mVisualProperties.Get();
517   }
518
519 private:
520   /**
521    * Protected constructor; See also Renderer::New()
522    */
523   Renderer();
524
525 private:
526   enum Decay
527   {
528     DONE    = 0,
529     LAST    = 1,
530     INITIAL = 2
531   };
532
533 private:
534   CollectedUniformMap mCollectedUniformMap; ///< Uniform maps collected by the renderer
535
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)
541
542   OwnerPointer<VisualRenderer::AnimatableVisualProperties> mVisualProperties{nullptr}; ///< VisualProperties (optional/owned)
543   OwnerPointer<Vector4>                                    mBlendColor;                ///< The blend color for blending operation
544
545   Dali::Internal::Render::Renderer::StencilParameters mStencilParameters; ///< Struct containing all stencil related options
546
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
553
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)
561
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
565
566   std::vector<Dali::DevelRenderer::DrawCommand> mDrawCommands;
567   Dali::RenderCallback*                         mRenderCallback{nullptr};
568
569 public:
570   AnimatableProperty<float> mOpacity;    ///< The opacity value
571   int32_t                   mDepthIndex; ///< Used only in PrepareRenderInstructions
572 };
573
574 } // namespace SceneGraph
575 } // namespace Internal
576 } // namespace Dali
577
578 #endif // DALI_INTERNAL_SCENE_GRAPH_RENDERER_H