6686af678369283b9a844bfd50dd81c15a76a6db
[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/internal-constants.h>
24 #include <dali/internal/common/type-abstraction-enums.h>
25 #include <dali/internal/event/common/event-thread-services.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    * @See Dali::RenderableActor::SetBlendMode().
55    */
56   void SetBlendingMode( BlendingMode::Type mode );
57
58   /**
59    * @copydoc Dali::RenderableActor::GetBlendMode().
60    */
61   BlendingMode::Type GetBlendingMode() const;
62
63   /**
64    * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
65    * @return true if the derived renderable uses geometry scaling
66    */
67   bool UsesGeometryScaling() const
68   {
69     return mUsesGeometryScaling;
70   }
71
72   /**
73    * Triggers scale for size update. GetScaleForSize will be called in this frame
74    */
75   void SetRecalculateScaleForSize();
76
77   /**
78    * Returns the scaling dirty flag, inlined as called from update algorithm often
79    * @return if scale for size is dirty, i.e. scaling has changed
80    */
81   bool IsScaleForSizeDirty() const
82   {
83     return mScaleForSizeDirty;
84   }
85
86   /**
87    * Retrieve scale-for-size for given node size
88    * Clears the scale for size flag
89    * @param[in] nodeSize to scale to
90    * @param[out] scaling factors
91    */
92   void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
93
94
95 public: // For use during in the update algorithm only
96
97   /**
98    * @param[in] updateBufferIndex The current update buffer index.
99    * @return visible tells if this renderer can be potentially seen
100    */
101   bool ResolveVisibility( BufferIndex updateBufferIndex );
102
103   /**
104    * if this renderable actor has visible size and color
105    * @return true if you can potentially see this actor
106    */
107   bool HasVisibleSizeAndColor() const
108   {
109     return mHasSizeAndColorFlag;
110   }
111
112   /**
113    * Check whether the attachment has been marked as ready to render
114    * @param[out] ready TRUE if the attachment has resources to render
115    * @param[out] complete TRUE if the attachment's resources are complete
116    * (e.g. image has finished loading, framebuffer is ready to render, native image
117    * framebuffer has been rendered)
118    */
119   void GetReadyAndComplete(bool& ready, bool& complete) const;
120
121   /**
122    * Query whether the attachment has blending enabled.
123    * @param[in] updateBufferIndex The current update buffer index.
124    * @return true if blending is enabled, false otherwise.
125    */
126   bool IsBlendingOn( BufferIndex updateBufferIndex );
127
128   /**
129    * Check if the blending mode has changed - if it has, send message to renderer
130    * @param[in] updateBufferIndex The current update buffer index.
131    * @param[in] useBlending True if the renderer should use blending option
132    */
133   void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
134
135   /**
136    * Prepare the object for rendering.
137    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
138    * @param[in] updateBufferIndex The current update buffer index.
139    */
140   void PrepareRender( BufferIndex updateBufferIndex );
141
142 public: // API for derived classes
143
144   /**
145    * Retrieve a Renderer used by this attachment; implemented in derived classes.
146    * @note The first Renderer is the main renderer for the attachment, and
147    * should always exist during the lifetime of the RenderableAttachment.
148    * @return A Renderer.
149    */
150   virtual Renderer& GetRenderer() = 0;
151
152   /**
153    * Retrieve a Renderer used by this attachment.
154    * @note The first Renderer is the main renderer for the attachment, and
155    * should always exist during the lifetime of the RenderableAttachment.
156    * Other renderers are for effects such as shadows and reflections.
157    * @return A Renderer.
158    */
159   virtual const Renderer& GetRenderer() const = 0;
160
161   /**
162    * Prepare the object resources.
163    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
164    * @param[in] updateBufferIndex The current update buffer index.
165    * @param[in] resourceManager The resource manager.
166    */
167   void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
168
169   /**
170    * If the resource is being tracked, then follow it. ( Further ready tests will use this
171    * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
172    * @param[in] The resource id
173    */
174   void FollowTracker( Integration::ResourceId id );
175
176   /**
177    * @copydoc RenderableAttachment::PrepareRender()
178    */
179   virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
180
181   /**
182    * Query whether the attachment is fully opaque.
183    * @param[in] updateBufferIndex The current update buffer index.
184    * @return True if fully opaque.
185    */
186   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
187
188   /**
189    * Called to notify that the size has been changed
190    * The implementation may tell the renderer to recalculate scale
191    * based on the new size
192    * @param[in] updateBufferIndex The current update buffer index.
193    */
194   virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
195
196   /**
197    * Chained from NodeAttachment::ConnectToSceneGraph()
198    */
199   virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
200
201   /**
202    * Chained from NodeAttachment::OnDestroy()
203    */
204   virtual void OnDestroy2() = 0;
205
206   /**
207    * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
208    * @param[in] nodeSize to scale to
209    * @param[out] scaling factors
210    */
211   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
212
213
214   /**
215    * Set the sort-modifier for the attachment.
216    * @param[in] modifier The depth-sort modifier.
217    */
218   void SetSortModifier(float modifier);
219
220   /**
221    * Retrieve the sort-modifier for the attachment.
222    * @return The sort-modifier.
223    */
224   float GetSortModifier() const
225   {
226     // inlined as its called a lot when sorting transparent renderers
227     return mSortModifier;
228   }
229
230 protected:
231
232   /**
233    * Protected constructor; only base classes can be instantiated.
234    * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
235    */
236   RenderableAttachment( bool usesGeometryScaling );
237
238   /**
239    * Virtual destructor, no deletion through this interface
240    */
241   virtual ~RenderableAttachment();
242
243 private: // From NodeAttachment
244
245   /**
246    * @copydoc NodeAttachment::ConnectToSceneGraph().
247    */
248   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
249
250   /**
251    * @copydoc NodeAttachment::DisconnectFromSceneGraph().
252    */
253   virtual void OnDestroy();
254
255   /**
256    * @copydoc NodeAttachment::GetRenderable()
257    */
258   virtual RenderableAttachment* GetRenderable();
259
260 private:
261
262   /**
263    * Prepare the object resources.
264    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
265    * @param[in] updateBufferIndex The current buffer index.
266    * @param[in] resourceManager The resource manager.
267    * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
268    */
269   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
270
271   // Undefined
272   RenderableAttachment( const RenderableAttachment& );
273
274   // Undefined
275   RenderableAttachment& operator=( const RenderableAttachment& rhs );
276
277 protected:
278   SceneController* mSceneController;   ///< Used for initializing renderers whilst attached
279   Shader*          mShader;            ///< A pointer to the shader
280
281   Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
282
283   float mSortModifier;
284
285   BlendingMode::Type mBlendingMode:2;  ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
286
287   bool mUsesGeometryScaling:1;         ///< True if the derived renderer uses scaling.
288   bool mScaleForSizeDirty:1;           ///< True if mScaleForSize has changed in the current frame.
289   bool mUseBlend:1;                    ///< True if the attachment & renderer should be considered opaque for sorting and blending.
290   bool mHasSizeAndColorFlag:1;         ///< Set during the update algorithm to tell whether this renderer can potentially be seen
291   bool mResourcesReady:1;              ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
292   bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
293   bool mHasUntrackedResources:1;       ///< Set during PrepareResources, true if have tried to follow untracked resources
294 };
295
296 // Messages for RenderableAttachment
297
298 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, BlendingMode::Type mode )
299 {
300   typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
301
302   // Reserve some memory inside the message queue
303   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
304
305   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
306 }
307
308 } // namespace SceneGraph
309
310 } // namespace Internal
311
312 } // namespace Dali
313
314 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__