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