1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
38 class ResourceManager;
39 class ResourceTracker;
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.
49 class RenderableAttachment : public NodeAttachment
54 * Set the sort-modifier for the attachment.
55 * @param[in] modifier The depth-sort modifier.
57 void SetSortModifier(float modifier);
60 * Retrieve the sort-modifier for the attachment.
61 * @return The sort-modifier.
63 float GetSortModifier() const;
66 * @See Dali::RenderableActor::SetBlendMode().
68 void SetBlendingMode( BlendingMode::Type mode );
71 * @copydoc Dali::RenderableActor::GetBlendMode().
73 BlendingMode::Type GetBlendingMode() const;
76 * Check if the blending mode has changed - if it has, send message to renderer
77 * @param[in] updateBufferIndex The current update buffer index.
78 * @param[in] useBlending True if the renderer should use blending option
80 void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
83 * Set the blending options. This should only be called from the update-thread.
84 * @param[in] updateBufferIndex The current update buffer index.
85 * @param[in] options A bitmask of blending options.
87 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
90 * Set the blend-color. This should only be called from the update-thread.
91 * @param[in] updateBufferIndex The current update buffer index.
92 * @param[in] color The new blend-color.
94 void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
97 * Set the face-culling mode.
98 * @param[in] updateBufferIndex The current update buffer index.
99 * @param[in] mode The face-culling mode.
101 void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
104 * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
105 * @return true if the derived renderable uses geometry scaling
107 bool UsesGeometryScaling() const
109 return mUsesGeometryScaling;
113 * Triggers scale for size update. GetScaleForSize will be called in this frame
115 void SetRecalculateScaleForSize();
118 * Returns the scaling dirty flag, inlined as called from update algorithm often
119 * @return if scale for size is dirty, i.e. scaling has changed
121 bool IsScaleForSizeDirty() const
123 return mScaleForSizeDirty;
127 * Retrieve scale-for-size for given node size
128 * Clears the scale for size flag
129 * @param[in] nodeSize to scale to
130 * @param[out] scaling factors
132 void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
134 public: // For use during in the update algorithm only
137 * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
138 * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
139 * @param[in] updateBufferIndex The current update buffer index.
140 * @return visible tells if this renderer can be potentially seen
142 virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
144 mHasSizeAndColorFlag = false;
145 const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
146 if( color.a > FULLY_TRANSPARENT ) // not fully transparent
148 const Vector3& size = mParent->GetSize( updateBufferIndex );
149 if( ( size.width > Math::MACHINE_EPSILON_1000 )&& // width is greater than a very small number
150 ( size.height > Math::MACHINE_EPSILON_1000 ) ) // height is greater than a very small number
152 mHasSizeAndColorFlag = true;
155 return mHasSizeAndColorFlag;
159 * Check whether the attachment has been marked as ready to render
160 * @param[out] ready TRUE if the attachment has resources to render
161 * @param[out] complete TRUE if the attachment's resources are complete
162 * (e.g. image has finished loading, framebuffer is ready to render, native image
163 * framebuffer has been rendered)
165 void GetReadyAndComplete(bool& ready, bool& complete) const;
168 * Query whether the attachment has blending enabled.
169 * @param[in] updateBufferIndex The current update buffer index.
170 * @return true if blending is enabled, false otherwise.
172 bool IsBlendingOn( BufferIndex updateBufferIndex );
175 * Prepare the object for rendering.
176 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
177 * @param[in] updateBufferIndex The current update buffer index.
179 void PrepareRender( BufferIndex updateBufferIndex );
181 public: // API for derived classes
184 * Retrieve a Renderer used by this attachment; implemented in derived classes.
185 * @note The first Renderer is the main renderer for the attachment, and
186 * should always exist during the lifetime of the RenderableAttachment.
187 * @return A Renderer.
189 virtual Renderer& GetRenderer() = 0;
192 * Retrieve a Renderer used by this attachment.
193 * @note The first Renderer is the main renderer for the attachment, and
194 * should always exist during the lifetime of the RenderableAttachment.
195 * Other renderers are for effects such as shadows and reflections.
196 * @return A Renderer.
198 virtual const Renderer& GetRenderer() const = 0;
201 * Prepare the object resources.
202 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
203 * @param[in] updateBufferIndex The current update buffer index.
204 * @param[in] resourceManager The resource manager.
206 void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
209 * If the resource is being tracked, then follow it. ( Further ready tests will use this
210 * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
211 * @param[in] The resource id
213 void FollowTracker( Integration::ResourceId id );
216 * @copydoc RenderableAttachment::PrepareRender()
218 virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
221 * Query whether the attachment is fully opaque.
222 * @param[in] updateBufferIndex The current update buffer index.
223 * @return True if fully opaque.
225 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
228 * Called to notify that the shader might have been changed
229 * The implementation should recalculate geometry and scale based on the
230 * hints from the new shader
231 * @param[in] updateBufferIndex The current update buffer index.
232 * @return Return true if the geometry changed.
234 virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
237 * Called to notify that the size has been changed
238 * The implementation may tell the renderer to recalculate geometry and scale based on the new size
239 * @param[in] updateBufferIndex The current update buffer index.
241 virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
244 * Chained from NodeAttachment::ConnectToSceneGraph()
246 virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
249 * Chained from NodeAttachment::OnDestroy()
251 virtual void OnDestroy2() = 0;
254 * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
255 * @param[in] nodeSize to scale to
256 * @param[out] scaling factors
258 virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
263 * Protected constructor; only base classes can be instantiated.
264 * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
266 RenderableAttachment( bool usesGeometryScaling );
269 * Virtual destructor, no deletion through this interface
271 virtual ~RenderableAttachment();
273 private: // From NodeAttachment
276 * @copydoc NodeAttachment::ConnectToSceneGraph().
278 virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
281 * @copydoc NodeAttachment::DisconnectFromSceneGraph().
283 virtual void OnDestroy();
286 * @copydoc NodeAttachment::GetRenderable()
288 virtual RenderableAttachment* GetRenderable();
293 * Prepare the object resources.
294 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
295 * @param[in] updateBufferIndex The current buffer index.
296 * @param[in] resourceManager The resource manager.
297 * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
299 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
302 RenderableAttachment( const RenderableAttachment& );
305 RenderableAttachment& operator=( const RenderableAttachment& rhs );
309 SceneController* mSceneController; ///< Used for initializing renderers whilst attached
311 BlendingMode::Type mBlendingMode:2; ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
313 bool mUsesGeometryScaling:1; ///< True if the derived renderer uses scaling.
314 bool mScaleForSizeDirty:1; ///< True if mScaleForSize has changed in the current frame.
315 bool mUseBlend:1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
316 bool mHasSizeAndColorFlag:1; ///< Set during the update algorithm to tell whether this renderer can potentially be seen
317 bool mResourcesReady:1; ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
318 bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
319 bool mHasUntrackedResources:1; ///< Set during PrepareResources, true if have tried to follow untracked resources
320 CullFaceMode mCullFaceMode:3; ///< Cullface mode, 3 bits is enough for 4 values
324 Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
327 // Messages for RenderableAttachment
329 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
331 typedef MessageValue1< RenderableAttachment, float > LocalType;
333 // Reserve some memory inside the message queue
334 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
336 // Construct message in the message queue memory; note that delete should not be called on the return value
337 new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
340 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
342 typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
344 // Reserve some memory inside the message queue
345 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
347 // Construct message in the message queue memory; note that delete should not be called on the return value
348 new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
351 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
353 typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
355 // Reserve some memory inside the message queue
356 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
358 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
361 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
363 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
365 // Reserve some memory inside the message queue
366 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
368 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
371 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
373 typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
375 // Reserve some memory inside the message queue
376 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
378 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
381 } // namespace SceneGraph
383 } // namespace Internal
387 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__