Friendly janitor cleaning up unnecessary header dependencies
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-renderable-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
3
4 /*
5  * Copyright (c) 2014 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
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/blending-options.h>
23 #include <dali/internal/common/event-to-update.h>
24 #include <dali/internal/common/internal-constants.h>
25 #include <dali/internal/common/type-abstraction-enums.h>
26 #include <dali/internal/update/controllers/scene-controller.h>
27 #include <dali/internal/update/nodes/node.h>
28 #include <dali/internal/update/node-attachments/node-attachment.h>
29 #include <dali/internal/update/resources/resource-manager-declarations.h>
30 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37 class ResourceManager;
38 class ResourceTracker;
39
40 namespace SceneGraph
41 {
42 class Renderer;
43 class Shader;
44
45 /**
46  * RenderableAttachments are responsible for preparing textures, meshes, matrices etc. during the Update.
47  * These resources are then passed to a renderer, for use in the next Render.
48  */
49 class RenderableAttachment : public NodeAttachment
50 {
51 public: // API
52
53   /**
54    * Set the sort-modifier for the attachment.
55    * @param[in] modifier The depth-sort modifier.
56    */
57   void SetSortModifier(float modifier);
58
59   /**
60    * Retrieve the sort-modifier for the attachment.
61    * @return The sort-modifier.
62    */
63   float GetSortModifier() const
64   {
65     // inlined as its called a lot when sorting transparent renderers
66     return mSortModifier;
67   }
68
69   /**
70    * @See Dali::RenderableActor::SetBlendMode().
71    */
72   void SetBlendingMode( BlendingMode::Type mode );
73
74   /**
75    * @copydoc Dali::RenderableActor::GetBlendMode().
76    */
77   BlendingMode::Type GetBlendingMode() const;
78
79   /**
80    * Check if the blending mode has changed - if it has, send message to renderer
81    * @param[in] updateBufferIndex The current update buffer index.
82    * @param[in] useBlending True if the renderer should use blending option
83    */
84   void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
85
86   /**
87    * Set the blending options. This should only be called from the update-thread.
88    * @param[in] updateBufferIndex The current update buffer index.
89    * @param[in] options A bitmask of blending options.
90    */
91   void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
92
93   /**
94    * Set the blend-color. This should only be called from the update-thread.
95    * @param[in] updateBufferIndex The current update buffer index.
96    * @param[in] color The new blend-color.
97    */
98   void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
99
100   /**
101    * Set the face-culling mode.
102    * @param[in] updateBufferIndex The current update buffer index.
103    * @param[in] mode The face-culling mode.
104    */
105   void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
106
107   /**
108    * Set the sampler used to render the texture for this renderable.
109    * @param[in] updateBufferIndex The current update buffer index.
110    * @param[in] samplerBitfield The image sampler packed options to set.
111    */
112   void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
113
114   /**
115    * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
116    * @return true if the derived renderable uses geometry scaling
117    */
118   bool UsesGeometryScaling() const
119   {
120     return mUsesGeometryScaling;
121   }
122
123   /**
124    * Triggers scale for size update. GetScaleForSize will be called in this frame
125    */
126   void SetRecalculateScaleForSize();
127
128   /**
129    * Returns the scaling dirty flag, inlined as called from update algorithm often
130    * @return if scale for size is dirty, i.e. scaling has changed
131    */
132   bool IsScaleForSizeDirty() const
133   {
134     return mScaleForSizeDirty;
135   }
136
137   /**
138    * Retrieve scale-for-size for given node size
139    * Clears the scale for size flag
140    * @param[in] nodeSize to scale to
141    * @param[out] scaling factors
142    */
143   void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
144
145   /**
146    * Apply a shader on the renderable
147    * @param[in] updateBufferIndex The current update buffer index.
148    * @param[in] shader to apply.
149    */
150   void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
151
152   /**
153    * Remove the shader from the renderable
154    * @param[in] updateBufferIndex The current update buffer index.
155    */
156   void RemoveShader( BufferIndex updateBufferIndex );
157
158 public: // For use during in the update algorithm only
159
160   /**
161    * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
162    * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
163    * @param[in] updateBufferIndex The current update buffer index.
164    * @return visible tells if this renderer can be potentially seen
165    */
166   virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
167   {
168     mHasSizeAndColorFlag = false;
169     const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
170     if( color.a > FULLY_TRANSPARENT )               // not fully transparent
171     {
172       const Vector3& size = mParent->GetSize( updateBufferIndex );
173       if( ( size.width > Math::MACHINE_EPSILON_1000 )&&   // width is greater than a very small number
174           ( size.height > Math::MACHINE_EPSILON_1000 ) )  // height is greater than a very small number
175       {
176         mHasSizeAndColorFlag = true;
177       }
178     }
179     return mHasSizeAndColorFlag;
180   }
181
182   /**
183    * if this renderable actor has visible size and color
184    * @return true if you can potentially see this actor
185    */
186   bool HasVisibleSizeAndColor() const
187   {
188     return mHasSizeAndColorFlag;
189   }
190
191   /**
192    * Check whether the attachment has been marked as ready to render
193    * @param[out] ready TRUE if the attachment has resources to render
194    * @param[out] complete TRUE if the attachment's resources are complete
195    * (e.g. image has finished loading, framebuffer is ready to render, native image
196    * framebuffer has been rendered)
197    */
198   void GetReadyAndComplete(bool& ready, bool& complete) const;
199
200   /**
201    * Query whether the attachment has blending enabled.
202    * @param[in] updateBufferIndex The current update buffer index.
203    * @return true if blending is enabled, false otherwise.
204    */
205   bool IsBlendingOn( BufferIndex updateBufferIndex );
206
207   /**
208    * Prepare the object for rendering.
209    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
210    * @param[in] updateBufferIndex The current update buffer index.
211    */
212   void PrepareRender( BufferIndex updateBufferIndex );
213
214 public: // API for derived classes
215
216   /**
217    * Retrieve a Renderer used by this attachment; implemented in derived classes.
218    * @note The first Renderer is the main renderer for the attachment, and
219    * should always exist during the lifetime of the RenderableAttachment.
220    * @return A Renderer.
221    */
222   virtual Renderer& GetRenderer() = 0;
223
224   /**
225    * Retrieve a Renderer used by this attachment.
226    * @note The first Renderer is the main renderer for the attachment, and
227    * should always exist during the lifetime of the RenderableAttachment.
228    * Other renderers are for effects such as shadows and reflections.
229    * @return A Renderer.
230    */
231   virtual const Renderer& GetRenderer() const = 0;
232
233   /**
234    * Prepare the object resources.
235    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
236    * @param[in] updateBufferIndex The current update buffer index.
237    * @param[in] resourceManager The resource manager.
238    */
239   void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
240
241   /**
242    * If the resource is being tracked, then follow it. ( Further ready tests will use this
243    * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
244    * @param[in] The resource id
245    */
246   void FollowTracker( Integration::ResourceId id );
247
248   /**
249    * @copydoc RenderableAttachment::PrepareRender()
250    */
251   virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
252
253   /**
254    * Query whether the attachment is fully opaque.
255    * @param[in] updateBufferIndex The current update buffer index.
256    * @return True if fully opaque.
257    */
258   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
259
260   /**
261    * Called to notify that the shader might have been changed
262    * The implementation should recalculate geometry and scale based on the
263    * hints from the new shader
264    * @param[in] updateBufferIndex The current update buffer index.
265    * @return Return true if the geometry changed.
266    */
267   virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
268
269   /**
270    * Called to notify that the size has been changed
271    * The implementation may tell the renderer to recalculate geometry and scale based on the new size
272    * @param[in] updateBufferIndex The current update buffer index.
273    */
274   virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
275
276   /**
277    * Chained from NodeAttachment::ConnectToSceneGraph()
278    */
279   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
280
281   /**
282    * Chained from NodeAttachment::OnDestroy()
283    */
284   virtual void OnDestroy2() = 0;
285
286   /**
287    * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
288    * @param[in] nodeSize to scale to
289    * @param[out] scaling factors
290    */
291   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
292
293 protected:
294
295   /**
296    * Protected constructor; only base classes can be instantiated.
297    * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
298    */
299   RenderableAttachment( bool usesGeometryScaling );
300
301   /**
302    * Virtual destructor, no deletion through this interface
303    */
304   virtual ~RenderableAttachment();
305
306 private: // From NodeAttachment
307
308   /**
309    * @copydoc NodeAttachment::ConnectToSceneGraph().
310    */
311   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
312
313   /**
314    * @copydoc NodeAttachment::DisconnectFromSceneGraph().
315    */
316   virtual void OnDestroy();
317
318   /**
319    * @copydoc NodeAttachment::GetRenderable()
320    */
321   virtual RenderableAttachment* GetRenderable();
322
323 private:
324
325   /**
326    * Prepare the object resources.
327    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
328    * @param[in] updateBufferIndex The current buffer index.
329    * @param[in] resourceManager The resource manager.
330    * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
331    */
332   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
333
334   /**
335    * Sends the shader to the renderer
336    * @param updateBufferIndex for the message buffer
337    */
338   void SendShaderChangeMessage( BufferIndex updateBufferIndex );
339
340   // Undefined
341   RenderableAttachment( const RenderableAttachment& );
342
343   // Undefined
344   RenderableAttachment& operator=( const RenderableAttachment& rhs );
345
346 protected:
347
348   SceneController* mSceneController;   ///< Used for initializing renderers whilst attached
349   Shader*          mShader;            ///< A pointer to the shader
350
351   Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
352
353   float mSortModifier;
354
355   BlendingMode::Type mBlendingMode:2;  ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
356
357   bool mUsesGeometryScaling:1;         ///< True if the derived renderer uses scaling.
358   bool mScaleForSizeDirty:1;           ///< True if mScaleForSize has changed in the current frame.
359   bool mUseBlend:1;                    ///< True if the attachment & renderer should be considered opaque for sorting and blending.
360   bool mHasSizeAndColorFlag:1;         ///< Set during the update algorithm to tell whether this renderer can potentially be seen
361   bool mResourcesReady:1;              ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
362   bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
363   bool mHasUntrackedResources:1;       ///< Set during PrepareResources, true if have tried to follow untracked resources
364   CullFaceMode mCullFaceMode:3;        ///< Cullface mode, 3 bits is enough for 4 values
365
366 };
367
368 // Messages for RenderableAttachment
369
370 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
371 {
372   typedef MessageValue1< RenderableAttachment, float > LocalType;
373
374   // Reserve some memory inside the message queue
375   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
376
377   // Construct message in the message queue memory; note that delete should not be called on the return value
378   new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
379 }
380
381 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
382 {
383   typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
384
385   // Reserve some memory inside the message queue
386   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
387
388   // Construct message in the message queue memory; note that delete should not be called on the return value
389   new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
390 }
391
392 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
393 {
394   typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
395
396   // Reserve some memory inside the message queue
397   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
398
399   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
400 }
401
402 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
403 {
404   typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
405
406   // Reserve some memory inside the message queue
407   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
408
409   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
410 }
411
412 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
413 {
414   typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
415
416   // Reserve some memory inside the message queue
417   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
418
419   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
420 }
421
422 inline void SetSamplerMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int samplerBitfield )
423 {
424   typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
425
426   // Reserve some memory inside the message queue
427   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
428
429   // Construct message in the message queue memory; note that delete should not be called on the return value
430   new (slot) LocalType( &attachment, &RenderableAttachment::SetSampler, samplerBitfield );
431 }
432
433 inline void ApplyShaderMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Shader& constShader )
434 {
435   // Update thread can edit the object
436   Shader& shader = const_cast< Shader& >( constShader );
437
438   typedef MessageDoubleBuffered1< RenderableAttachment, Shader* > LocalType;
439
440   // Reserve some memory inside the message queue
441   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
442
443   // Construct message in the message queue memory; note that delete should not be called on the return value
444   new (slot) LocalType( &attachment, &RenderableAttachment::ApplyShader, &shader );
445 }
446
447 inline void RemoveShaderMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment )
448 {
449   typedef MessageDoubleBuffered0< RenderableAttachment > LocalType;
450
451   // Reserve some memory inside the message queue
452   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
453
454   // Construct message in the message queue memory; note that delete should not be called on the return value
455   new (slot) LocalType( &attachment, &RenderableAttachment::RemoveShader );
456 }
457
458 } // namespace SceneGraph
459
460 } // namespace Internal
461
462 } // namespace Dali
463
464 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__