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