Remove dead logic handling renderer ready and completion
[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) 2017 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/public-api/rendering/geometry.h>
21 #include <dali/public-api/rendering/renderer.h> // Dali::Renderer
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/common/type-abstraction-enums.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/common/uniform-map.h>
27 #include <dali/internal/update/common/scene-graph-connection-change-propagator.h>
28 #include <dali/internal/render/data-providers/render-data-provider.h>
29 #include <dali/internal/render/renderers/render-renderer.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Render
38 {
39 class Renderer;
40 class Geometry;
41 }
42
43 namespace SceneGraph
44 {
45 class SceneController;
46
47 class Renderer;
48 typedef Dali::Vector< Renderer* > RendererContainer;
49 typedef RendererContainer::Iterator RendererIter;
50 typedef RendererContainer::ConstIterator RendererConstIter;
51
52 class TextureSet;
53 class Geometry;
54
55 class Renderer :  public PropertyOwner,
56                   public UniformMapDataProvider,
57                   public UniformMap::Observer,
58                   public ConnectionChangePropagator::Observer
59 {
60 public:
61
62   enum Opacity
63   {
64     OPAQUE,
65     TRANSPARENT,
66     TRANSLUCENT
67   };
68
69   /**
70    * Construct a new Renderer
71    */
72   static Renderer* New();
73
74   /**
75    * Destructor
76    */
77   virtual ~Renderer();
78
79   /**
80    * Overriden delete operator
81    * Deletes the renderer from its global memory pool
82    */
83   void operator delete( void* ptr );
84
85   /**
86    * Set the texture set for the renderer
87    * @param[in] textureSet The texture set this renderer will use
88    */
89   void SetTextures( TextureSet* textureSet );
90
91   /**
92    * Returns current texture set object
93    * @return Pointer to the texture set
94    */
95   const TextureSet* GetTextures() const
96   {
97     return mTextureSet;
98   }
99
100   /**
101    * Set the shader for the renderer
102    * @param[in] shader The shader this renderer will use
103    */
104   void SetShader( Shader* shader );
105
106   /**
107    * Get the shader used by this renderer
108    * @return the shader this renderer uses
109    */
110   const Shader& GetShader() const
111   {
112     return *mShader;
113   }
114
115   /**
116    * Set the geometry for the renderer
117    * @param[in] geometry The geometry this renderer will use
118    */
119   void SetGeometry( Render::Geometry* geometry );
120
121   /**
122    * Set the depth index
123    * @param[in] depthIndex the new depth index to use
124    */
125   void SetDepthIndex( int depthIndex );
126
127   /**
128    * @brief Get the depth index
129    * @return The depth index
130    */
131   int GetDepthIndex() const
132   {
133     return mDepthIndex;
134   }
135
136   /**
137    * Set the face culling mode
138    * @param[in] faceCullingMode to use
139    */
140   void SetFaceCullingMode( FaceCullingMode::Type faceCullingMode );
141
142   /**
143    * Set the blending mode
144    * @param[in] blendingMode to use
145    */
146   void SetBlendMode( BlendMode::Type blendingMode );
147
148   /**
149    * Set the blending options. This should only be called from the update thread.
150    * @param[in] options A bitmask of blending options.
151    */
152   void SetBlendingOptions( unsigned int options );
153
154   /**
155    * Set the blend color for blending operation
156    * @param blendColor to pass to GL
157    */
158   void SetBlendColor( const Vector4& blendColor );
159
160   /**
161    * Set the index of first element for indexed draw
162    * @param[in] firstElement index of first element to draw
163    */
164   void SetIndexedDrawFirstElement( size_t firstElement );
165
166   /**
167    * Set the number of elements to draw by indexed draw
168    * @param[in] elementsCount number of elements to draw
169    */
170   void SetIndexedDrawElementsCount( size_t elementsCount );
171
172   /**
173    * @brief Set whether the Pre-multiplied Alpha Blending is required
174    * @param[in] preMultipled whether alpha is pre-multiplied.
175    */
176   void EnablePreMultipliedAlpha( bool preMultipled );
177
178   /**
179    * Sets the depth buffer write mode
180    * @param[in] depthWriteMode The depth buffer write mode
181    */
182   void SetDepthWriteMode( DepthWriteMode::Type depthWriteMode );
183
184   /**
185    * Sets the depth buffer test mode
186    * @param[in] depthTestMode The depth buffer test mode
187    */
188   void SetDepthTestMode( DepthTestMode::Type depthTestMode );
189
190   /**
191    * Sets the depth function
192    * @param[in] depthFunction The depth function
193    */
194   void SetDepthFunction( DepthFunction::Type depthFunction );
195
196   /**
197    * Sets the render mode
198    * @param[in] mode The render mode
199    */
200   void SetRenderMode( RenderMode::Type mode );
201
202   /**
203    * Sets the stencil function
204    * @param[in] stencilFunction The stencil function
205    */
206   void SetStencilFunction( StencilFunction::Type stencilFunction );
207
208   /**
209    * Sets the stencil function mask
210    * @param[in] stencilFunctionMask The stencil function mask
211    */
212   void SetStencilFunctionMask( int stencilFunctionMask );
213
214   /**
215    * Sets the stencil function reference
216    * @param[in] stencilFunctionReference The stencil function reference
217    */
218   void SetStencilFunctionReference( int stencilFunctionReference );
219
220   /**
221    * Sets the stencil mask
222    * @param[in] stencilMask The stencil mask
223    */
224   void SetStencilMask( int stencilMask );
225
226   /**
227    * Sets the stencil operation for when the stencil test fails
228    * @param[in] stencilOperationOnFail The stencil operation
229    */
230   void SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail );
231
232   /**
233    * Sets the stencil operation for when the depth test fails
234    * @param[in] stencilOperationOnZFail The stencil operation
235    */
236   void SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail );
237
238   /**
239    * Sets the stencil operation for when the depth test passes
240    * @param[in] stencilOperationOnZPass The stencil operation
241    */
242   void SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass );
243
244   /**
245    * Prepare the object for rendering.
246    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
247    * @param[in] updateBufferIndex The current update buffer index.
248    */
249   void PrepareRender( BufferIndex updateBufferIndex );
250
251   /*
252    * Retrieve the Render thread renderer
253    * @return The associated render thread renderer
254    */
255   Render::Renderer& GetRenderer();
256
257   /**
258    * Query whether the renderer is fully opaque, fully transparent or transparent.
259    * @param[in] updateBufferIndex The current update buffer index.
260    * @return OPAQUE if fully opaque, TRANSPARENT if fully transparent and TRANSLUCENT if in between
261    */
262   Opacity GetOpacity( BufferIndex updateBufferIndex, const Node& node ) const;
263
264   /**
265    * Called by the TextureSet to notify to the renderer that it has changed
266    */
267   void TextureSetChanged();
268
269   /**
270    * Called by the TextureSet to notify to the renderer that it is about to be deleted
271    */
272   void TextureSetDeleted();
273
274   /**
275    * Connect the object to the scene graph
276    *
277    * @param[in] sceneController The scene controller - used for sending messages to render thread
278    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
279    */
280   void ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
281
282   /**
283    * Disconnect the object from the scene graph
284    * @param[in] sceneController The scene controller - used for sending messages to render thread
285    * @param[in] bufferIndex The current buffer index - used for sending messages to render thread
286    */
287   void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex );
288
289 public: // Implementation of ConnectionChangePropagator
290   /**
291    * @copydoc ConnectionChangePropagator::AddObserver
292    */
293   void AddConnectionObserver(ConnectionChangePropagator::Observer& observer){};
294
295   /**
296    * @copydoc ConnectionChangePropagator::RemoveObserver
297    */
298   void RemoveConnectionObserver(ConnectionChangePropagator::Observer& observer){};
299
300 public: // UniformMap::Observer
301   /**
302    * @copydoc UniformMap::Observer::UniformMappingsChanged
303    */
304   virtual void UniformMappingsChanged( const UniformMap& mappings );
305
306 public: // ConnectionChangePropagator::Observer
307
308   /**
309    * @copydoc ConnectionChangePropagator::ConnectionsChanged
310    */
311   virtual void ConnectionsChanged( PropertyOwner& owner );
312
313   /**
314    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
315    */
316   virtual void ConnectedUniformMapChanged( );
317
318   /**
319    * @copydoc ConnectionChangePropagator::ConnectedUniformMapChanged
320    */
321   virtual void ObservedObjectDestroyed(PropertyOwner& owner);
322
323 public: // PropertyOwner implementation
324   /**
325    * @copydoc Dali::Internal::SceneGraph::PropertyOwner::ResetDefaultProperties()
326    */
327   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
328
329 public: // From UniformMapDataProvider
330
331   /**
332    * @copydoc UniformMapDataProvider::GetUniformMapChanged
333    */
334   virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
335
336   /**
337    * @copydoc UniformMapDataProvider::GetUniformMap
338    */
339   virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
340
341 private:
342
343   /**
344    * Protected constructor; See also Renderer::New()
345    */
346   Renderer();
347
348   /**
349    * Helper function to create a new render data provider
350    * @return the new (initialized) data provider
351    */
352   RenderDataProvider* NewRenderDataProvider();
353
354   /**
355    * Helper function to retrieve the blend color.
356    * @return The blend color.
357    */
358   const Vector4& GetBlendColor() const;
359
360 private:
361
362   CollectedUniformMap          mCollectedUniformMap[2];           ///< Uniform maps collected by the renderer
363   SceneController*             mSceneController;                  ///< Used for initializing renderers
364   Render::Renderer*            mRenderer;                         ///< Raw pointer to the renderer (that's owned by RenderManager)
365   TextureSet*                  mTextureSet;                       ///< The texture set this renderer uses. (Not owned)
366   Render::Geometry*            mGeometry;                         ///< The geometry this renderer uses. (Not owned)
367   Shader*                      mShader;                           ///< The shader this renderer uses. (Not owned)
368   OwnerPointer< Vector4 >      mBlendColor;                       ///< The blend color for blending operation
369
370   Dali::Internal::Render::Renderer::StencilParameters mStencilParameters;         ///< Struct containing all stencil related options
371
372   size_t                       mIndexedDrawFirstElement;          ///< first element index to be drawn using indexed draw
373   size_t                       mIndexedDrawElementsCount;         ///< number of elements to be drawn using indexed draw
374   unsigned int                 mBlendBitmask;                     ///< The bitmask of blending options
375   unsigned int                 mRegenerateUniformMap;             ///< 2 if the map should be regenerated, 1 if it should be copied.
376   unsigned int                 mResendFlag;                       ///< Indicate whether data should be resent to the renderer
377
378   DepthFunction::Type          mDepthFunction:3;                  ///< Local copy of the depth function
379   FaceCullingMode::Type        mFaceCullingMode:2;                ///< Local copy of the mode of face culling
380   BlendMode::Type              mBlendMode:2;                      ///< Local copy of the mode of blending
381   DepthWriteMode::Type         mDepthWriteMode:2;                 ///< Local copy of the depth write mode
382   DepthTestMode::Type          mDepthTestMode:2;                  ///< Local copy of the depth test mode
383
384   bool                         mUniformMapChanged[2];             ///< Records if the uniform map has been altered this frame
385   bool                         mPremultipledAlphaEnabled:1;       ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
386
387 public:
388
389   int                          mDepthIndex;                       ///< Used only in PrepareRenderInstructions
390
391 };
392
393
394 /// Messages
395 inline void SetTexturesMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const TextureSet& textureSet )
396 {
397   typedef MessageValue1< Renderer, TextureSet* > LocalType;
398
399   // Reserve some memory inside the message queue
400   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
401
402   // Construct message in the message queue memory; note that delete should not be called on the return value
403   new (slot) LocalType( &renderer, &Renderer::SetTextures, const_cast<TextureSet*>(&textureSet) );
404 }
405
406 inline void SetGeometryMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Render::Geometry& geometry )
407 {
408   typedef MessageValue1< Renderer, Render::Geometry* > LocalType;
409
410   // Reserve some memory inside the message queue
411   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
412
413   // Construct message in the message queue memory; note that delete should not be called on the return value
414   new (slot) LocalType( &renderer, &Renderer::SetGeometry, const_cast<Render::Geometry*>(&geometry) );
415 }
416
417 inline void SetShaderMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, Shader& shader )
418 {
419   typedef MessageValue1< Renderer, Shader* > LocalType;
420
421   // Reserve some memory inside the message queue
422   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
423
424   // Construct message in the message queue memory; note that delete should not be called on the return value
425   new (slot) LocalType( &renderer, &Renderer::SetShader, &shader );
426 }
427
428 inline void SetDepthIndexMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int depthIndex )
429 {
430   typedef MessageValue1< Renderer, int > LocalType;
431
432   // Reserve some memory inside the message queue
433   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
434
435   // Construct message in the message queue memory; note that delete should not be called on the return value
436   new (slot) LocalType( &renderer, &Renderer::SetDepthIndex, depthIndex );
437 }
438
439 inline void SetFaceCullingModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, FaceCullingMode::Type faceCullingMode )
440 {
441   typedef MessageValue1< Renderer, FaceCullingMode::Type > LocalType;
442
443   // Reserve some memory inside the message queue
444   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
445
446   new (slot) LocalType( &renderer, &Renderer::SetFaceCullingMode, faceCullingMode );
447 }
448
449 inline void SetBlendModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, BlendMode::Type blendingMode )
450 {
451   typedef MessageValue1< Renderer, BlendMode::Type > LocalType;
452
453   // Reserve some memory inside the message queue
454   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
455
456   new (slot) LocalType( &renderer, &Renderer::SetBlendMode, blendingMode );
457 }
458
459 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, unsigned int options )
460 {
461   typedef MessageValue1< Renderer, unsigned int > LocalType;
462
463   // Reserve some memory inside the message queue
464   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
465
466   new (slot) LocalType( &renderer, &Renderer::SetBlendingOptions, options );
467 }
468
469 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, const Vector4& blendColor )
470 {
471   typedef MessageValue1< Renderer, Vector4 > LocalType;
472
473   // Reserve some memory inside the message queue
474   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
475
476   new (slot) LocalType( &renderer, &Renderer::SetBlendColor, blendColor );
477 }
478
479 inline void SetIndexedDrawFirstElementMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t firstElement )
480 {
481   typedef MessageValue1< Renderer, size_t > LocalType;
482
483   // Reserve some memory inside the message queue
484   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
485
486   new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawFirstElement, firstElement );
487 }
488
489 inline void SetIndexedDrawElementsCountMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, size_t elementsCount )
490 {
491   typedef MessageValue1< Renderer, size_t > LocalType;
492
493   // Reserve some memory inside the message queue
494   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
495
496   new (slot) LocalType( &renderer, &Renderer::SetIndexedDrawElementsCount, elementsCount );
497 }
498
499 inline void SetEnablePreMultipliedAlphaMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, bool preMultiplied )
500 {
501   typedef MessageValue1< Renderer, bool > LocalType;
502
503   // Reserve some memory inside the message queue
504   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
505
506   new (slot) LocalType( &renderer, &Renderer::EnablePreMultipliedAlpha, preMultiplied );
507 }
508
509 inline void SetDepthWriteModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthWriteMode::Type depthWriteMode )
510 {
511   typedef MessageValue1< Renderer, DepthWriteMode::Type > LocalType;
512
513   // Reserve some memory inside the message queue
514   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
515
516   new (slot) LocalType( &renderer, &Renderer::SetDepthWriteMode, depthWriteMode );
517 }
518
519 inline void SetDepthTestModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthTestMode::Type depthTestMode )
520 {
521   typedef MessageValue1< Renderer, DepthTestMode::Type > LocalType;
522
523   // Reserve some memory inside the message queue
524   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
525
526   new (slot) LocalType( &renderer, &Renderer::SetDepthTestMode, depthTestMode );
527 }
528
529 inline void SetDepthFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, DepthFunction::Type depthFunction )
530 {
531   typedef MessageValue1< Renderer, DepthFunction::Type > LocalType;
532
533   // Reserve some memory inside the message queue
534   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
535
536   new (slot) LocalType( &renderer, &Renderer::SetDepthFunction, depthFunction );
537 }
538
539 inline void SetRenderModeMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, RenderMode::Type mode )
540 {
541   typedef MessageValue1< Renderer, RenderMode::Type > LocalType;
542
543   // Reserve some memory inside the message queue
544   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
545
546   new (slot) LocalType( &renderer, &Renderer::SetRenderMode, mode );
547 }
548
549 inline void SetStencilFunctionMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilFunction::Type stencilFunction )
550 {
551   typedef MessageValue1< Renderer, StencilFunction::Type > LocalType;
552
553   // Reserve some memory inside the message queue
554   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
555
556   new (slot) LocalType( &renderer, &Renderer::SetStencilFunction, stencilFunction );
557 }
558
559 inline void SetStencilFunctionMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int mask )
560 {
561   typedef MessageValue1< Renderer, int > LocalType;
562
563   // Reserve some memory inside the message queue
564   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
565
566   new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionMask, mask );
567 }
568
569 inline void SetStencilFunctionReferenceMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilFunctionReference )
570 {
571   typedef MessageValue1< Renderer, int > LocalType;
572
573   // Reserve some memory inside the message queue
574   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
575
576   new (slot) LocalType( &renderer, &Renderer::SetStencilFunctionReference, stencilFunctionReference );
577 }
578
579 inline void SetStencilMaskMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, int stencilMask )
580 {
581   typedef MessageValue1< Renderer, int > LocalType;
582
583   // Reserve some memory inside the message queue
584   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
585
586   new (slot) LocalType( &renderer, &Renderer::SetStencilMask, stencilMask );
587 }
588
589 inline void SetStencilOperationOnFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
590 {
591   typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
592
593   // Reserve some memory inside the message queue
594   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
595
596   new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnFail, stencilOperation );
597 }
598
599 inline void SetStencilOperationOnZFailMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
600 {
601   typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
602
603   // Reserve some memory inside the message queue
604   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
605
606   new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZFail, stencilOperation );
607 }
608
609 inline void SetStencilOperationOnZPassMessage( EventThreadServices& eventThreadServices, const Renderer& renderer, StencilOperation::Type stencilOperation )
610 {
611   typedef MessageValue1< Renderer, StencilOperation::Type > LocalType;
612
613   // Reserve some memory inside the message queue
614   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
615
616   new (slot) LocalType( &renderer, &Renderer::SetStencilOperationOnZPass, stencilOperation );
617 }
618
619 } // namespace SceneGraph
620 } // namespace Internal
621 } // namespace Dali
622
623 #endif //  DALI_INTERNAL_SCENE_GRAPH_RENDERER_H