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