[SRUK] Initial copy from Tizen 2.2 version
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/actors/layer.h>
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/common/double-buffered.h>
30 #include <dali/internal/update/resources/resource-manager-declarations.h>
31 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
32
33 namespace Dali
34 {
35
36 namespace Internal
37 {
38 class ResourceManager;
39 class ResourceTracker;
40
41 namespace SceneGraph
42 {
43 class Renderer;
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   /**
66    * @See Dali::RenderableActor::SetBlendMode().
67    */
68   void SetBlendingMode( BlendingMode::Type mode );
69
70   /**
71    * @copydoc Dali::RenderableActor::GetBlendMode().
72    */
73   BlendingMode::Type GetBlendingMode() const;
74
75   /**
76    * Check if the blending mode has changed - if it has, send message to renderer
77    * @param[in] updateBufferIndex The current update buffer index.
78    * @param[in] useBlending True if the renderer should use blending option
79    */
80   void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
81
82   /**
83    * Set the blending options. This should only be called from the update-thread.
84    * @param[in] updateBufferIndex The current update buffer index.
85    * @param[in] options A bitmask of blending options.
86    */
87   void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
88
89   /**
90    * Set the blend-color. This should only be called from the update-thread.
91    * @param[in] updateBufferIndex The current update buffer index.
92    * @param[in] color The new blend-color.
93    */
94   void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
95
96   /**
97    * Set the face-culling mode.
98    * @param[in] updateBufferIndex The current update buffer index.
99    * @param[in] mode The face-culling mode.
100    */
101   void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
102
103   /**
104    * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
105    * @return true if the derived renderable uses geometry scaling
106    */
107   bool UsesGeometryScaling() const
108   {
109     return mUsesGeometryScaling;
110   }
111
112   /**
113    * Triggers scale for size update. GetScaleForSize will be called in this frame
114    */
115   void SetRecalculateScaleForSize();
116
117   /**
118    * Returns the scaling dirty flag, inlined as called from update algorithm often
119    * @return if scale for size is dirty, i.e. scaling has changed
120    */
121   bool IsScaleForSizeDirty() const
122   {
123     return mScaleForSizeDirty;
124   }
125
126   /**
127    * Retrieve scale-for-size for given node size
128    * Clears the scale for size flag
129    * @param[in] nodeSize to scale to
130    * @param[out] scaling factors
131    */
132   void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
133
134 public: // For use during in the update algorithm only
135
136   /**
137    * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
138    * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
139    * @param[in] updateBufferIndex The current update buffer index.
140    * @return visible tells if this renderer can be potentially seen
141    */
142   virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
143   {
144     mHasSizeAndColorFlag = false;
145     const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
146     if( color.a > FULLY_TRANSPARENT )               // not fully transparent
147     {
148       const Vector3& size = mParent->GetSize( updateBufferIndex );
149       if( ( size.width > Math::MACHINE_EPSILON_1000 )&&   // width is greater than a very small number
150           ( size.height > Math::MACHINE_EPSILON_1000 ) )  // height is greater than a very small number
151       {
152         mHasSizeAndColorFlag = true;
153       }
154     }
155     return mHasSizeAndColorFlag;
156   }
157
158   /**
159    * Check whether the attachment has been marked as ready to render
160    * @param[out] ready TRUE if the attachment has resources to render
161    * @param[out] complete TRUE if the attachment's resources are complete
162    * (e.g. image has finished loading, framebuffer is ready to render, native image
163    * framebuffer has been rendered)
164    */
165   void GetReadyAndComplete(bool& ready, bool& complete) const;
166
167   /**
168    * Query whether the attachment has blending enabled.
169    * @param[in] updateBufferIndex The current update buffer index.
170    * @return true if blending is enabled, false otherwise.
171    */
172   bool IsBlendingOn( BufferIndex updateBufferIndex );
173
174   /**
175    * Prepare the object for rendering.
176    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
177    * @param[in] updateBufferIndex The current update buffer index.
178    */
179   void PrepareRender( BufferIndex updateBufferIndex );
180
181 public: // API for derived classes
182
183   /**
184    * Retrieve a Renderer used by this attachment; implemented in derived classes.
185    * @note The first Renderer is the main renderer for the attachment, and
186    * should always exist during the lifetime of the RenderableAttachment.
187    * @return A Renderer.
188    */
189   virtual Renderer& GetRenderer() = 0;
190
191   /**
192    * Retrieve a Renderer used by this attachment.
193    * @note The first Renderer is the main renderer for the attachment, and
194    * should always exist during the lifetime of the RenderableAttachment.
195    * Other renderers are for effects such as shadows and reflections.
196    * @return A Renderer.
197    */
198   virtual const Renderer& GetRenderer() const = 0;
199
200   /**
201    * Prepare the object resources.
202    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
203    * @param[in] updateBufferIndex The current update buffer index.
204    * @param[in] resourceManager The resource manager.
205    */
206   void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
207
208   /**
209    * If the resource is being tracked, then follow it. ( Further ready tests will use this
210    * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
211    * @param[in] The resource id
212    */
213   void FollowTracker( Integration::ResourceId id );
214
215   /**
216    * @copydoc RenderableAttachment::PrepareRender()
217    */
218   virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
219
220   /**
221    * Query whether the attachment is fully opaque.
222    * @param[in] updateBufferIndex The current update buffer index.
223    * @return True if fully opaque.
224    */
225   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
226
227   /**
228    * Called to notify that the shader might have been changed
229    * The implementation should recalculate geometry and scale based on the
230    * hints from the new shader
231    * @param[in] updateBufferIndex The current update buffer index.
232    * @return Return true if the geometry changed.
233    */
234   virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
235
236   /**
237    * Called to notify that the size has been changed
238    * The implementation may tell the renderer to recalculate geometry and scale based on the new size
239    * @param[in] updateBufferIndex The current update buffer index.
240    */
241   virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
242
243   /**
244    * Chained from NodeAttachment::ConnectToSceneGraph()
245    */
246   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
247
248   /**
249    * Chained from NodeAttachment::OnDestroy()
250    */
251   virtual void OnDestroy2() = 0;
252
253   /**
254    * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
255    * @param[in] nodeSize to scale to
256    * @param[out] scaling factors
257    */
258   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
259
260 protected:
261
262   /**
263    * Protected constructor; only base classes can be instantiated.
264    * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
265    */
266   RenderableAttachment( bool usesGeometryScaling );
267
268   /**
269    * Virtual destructor, no deletion through this interface
270    */
271   virtual ~RenderableAttachment();
272
273 private: // From NodeAttachment
274
275   /**
276    * @copydoc NodeAttachment::ConnectToSceneGraph().
277    */
278   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
279
280   /**
281    * @copydoc NodeAttachment::DisconnectFromSceneGraph().
282    */
283   virtual void OnDestroy();
284
285   /**
286    * @copydoc NodeAttachment::GetRenderable()
287    */
288   virtual RenderableAttachment* GetRenderable();
289
290 private:
291
292   /**
293    * Prepare the object resources.
294    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
295    * @param[in] updateBufferIndex The current buffer index.
296    * @param[in] resourceManager The resource manager.
297    * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
298    */
299   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
300
301   // Undefined
302   RenderableAttachment( const RenderableAttachment& );
303
304   // Undefined
305   RenderableAttachment& operator=( const RenderableAttachment& rhs );
306
307 protected:
308
309   SceneController* mSceneController;   ///< Used for initializing renderers whilst attached
310
311   BlendingMode::Type mBlendingMode:2;    ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
312
313   bool mUsesGeometryScaling:1;         ///< True if the derived renderer uses scaling.
314   bool mScaleForSizeDirty:1;           ///< True if mScaleForSize has changed in the current frame.
315   bool mUseBlend:1;                    ///< True if the attachment & renderer should be considered opaque for sorting and blending.
316   bool mHasSizeAndColorFlag:1;         ///< Set during the update algorithm to tell whether this renderer can potentially be seen
317   bool mResourcesReady:1;              ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
318   bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
319   bool mHasUntrackedResources:1;       ///< Set during PrepareResources, true if have tried to follow untracked resources
320   CullFaceMode mCullFaceMode:3;        ///< Cullface mode, 3 bits is enough for 4 values
321
322   float mSortModifier;
323
324   Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
325 };
326
327 // Messages for RenderableAttachment
328
329 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
330 {
331   typedef MessageValue1< RenderableAttachment, float > LocalType;
332
333   // Reserve some memory inside the message queue
334   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
335
336   // Construct message in the message queue memory; note that delete should not be called on the return value
337   new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
338 }
339
340 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
341 {
342   typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
343
344   // Reserve some memory inside the message queue
345   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
346
347   // Construct message in the message queue memory; note that delete should not be called on the return value
348   new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
349 }
350
351 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
352 {
353   typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
354
355   // Reserve some memory inside the message queue
356   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
357
358   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
359 }
360
361 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
362 {
363   typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
364
365   // Reserve some memory inside the message queue
366   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
367
368   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
369 }
370
371 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
372 {
373   typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
374
375   // Reserve some memory inside the message queue
376   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
377
378   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
379 }
380
381 } // namespace SceneGraph
382
383 } // namespace Internal
384
385 } // namespace Dali
386
387 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__