Change Epsilon 0 and 1 to use the compiler provided values
[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   /**
180    * Prepare the object for rendering.
181    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
182    * @param[in] updateBufferIndex The current update buffer index.
183    */
184   void PrepareRender( BufferIndex updateBufferIndex );
185
186 public: // API for derived classes
187
188   /**
189    * Retrieve a Renderer used by this attachment; implemented in derived classes.
190    * @note The first Renderer is the main renderer for the attachment, and
191    * should always exist during the lifetime of the RenderableAttachment.
192    * @return A Renderer.
193    */
194   virtual Renderer& GetRenderer() = 0;
195
196   /**
197    * Retrieve a Renderer used by this attachment.
198    * @note The first Renderer is the main renderer for the attachment, and
199    * should always exist during the lifetime of the RenderableAttachment.
200    * Other renderers are for effects such as shadows and reflections.
201    * @return A Renderer.
202    */
203   virtual const Renderer& GetRenderer() const = 0;
204
205   /**
206    * Prepare the object resources.
207    * This must be called by the UpdateManager before calling PrepareRender, for each frame.
208    * @param[in] updateBufferIndex The current buffer index.
209    * @param[in] resourceManager The resource manager.
210    * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
211    */
212   virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
213
214   /**
215    * @copydoc RenderableAttachment::PrepareRender()
216    */
217   virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
218
219   /**
220    * Query whether the attachment is fully opaque.
221    * @param[in] updateBufferIndex The current update buffer index.
222    * @return True if fully opaque.
223    */
224   virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
225
226   /**
227    * Called to notify that the size has been changed
228    * The implementation may tell the renderer to recalculate scale
229    * based on the new size
230    * @param[in] updateBufferIndex The current update buffer index.
231    */
232   virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
233
234   /**
235    * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
236    * @param[in] nodeSize to scale to
237    * @param[out] scaling factors
238    */
239   virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
240
241   /**
242    * Set the sort-modifier for the attachment.
243    * @param[in] modifier The depth-sort modifier.
244    */
245   void SetSortModifier(float modifier);
246
247   /**
248    * Get the depth index for the attachment
249    * @param[in] bufferIndex The current update buffer index.
250    */
251   virtual int GetDepthIndex()
252   {
253     return static_cast<int>( mSortModifier );
254   }
255
256   /**
257    * Write the attachment's sort attributes to the passed in reference
258    * @todo MESH_REWORK Consider removing this after merge with scene-graph-renderer-attachment,
259    * and allowing PrepareRenderInstruction to read directly from this object
260    *
261    * @param[in] bufferIndex The current update buffer index.
262    * @param[out] sortAttributes
263    */
264   virtual void SetSortAttributes( BufferIndex bufferIndex, RendererWithSortAttributes& sortAttributes );
265
266 private:
267
268   // Undefined
269   RenderableAttachment( const RenderableAttachment& );
270
271   // Undefined
272   RenderableAttachment& operator=( const RenderableAttachment& rhs );
273
274 protected:
275   SceneController* mSceneController;   ///< Used for initializing renderers whilst attached
276   Shader*          mShader;            ///< A pointer to the shader
277
278   Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
279
280   float mSortModifier;
281
282   BlendingMode::Type mBlendingMode:2;  ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
283
284   bool mUsesGeometryScaling:1;         ///< True if the derived renderer uses scaling.
285   bool mScaleForSizeDirty:1;           ///< True if mScaleForSize has changed in the current frame.
286   bool mUseBlend:1;                    ///< True if the attachment & renderer should be considered opaque for sorting and blending.
287   bool mHasSizeAndColorFlag:1;         ///< Set during the update algorithm to tell whether this renderer can potentially be seen
288   bool mResourcesReady:1;              ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
289   bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
290   bool mHasUntrackedResources:1;       ///< Set during PrepareResources, true if have tried to follow untracked resources
291 };
292
293 // Messages for RenderableAttachment
294
295 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, BlendingMode::Type mode )
296 {
297   typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
298
299   // Reserve some memory inside the message queue
300   unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
301
302   new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
303 }
304
305 } // namespace SceneGraph
306
307 } // namespace Internal
308
309 } // namespace Dali
310
311 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__