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 * if this renderable actor has visible size and color
160 * @return true if you can potentially see this actor
162 bool HasVisibleSizeAndColor() const
164 return mHasSizeAndColorFlag;
168 * Check whether the attachment has been marked as ready to render
169 * @param[out] ready TRUE if the attachment has resources to render
170 * @param[out] complete TRUE if the attachment's resources are complete
171 * (e.g. image has finished loading, framebuffer is ready to render, native image
172 * framebuffer has been rendered)
174 void GetReadyAndComplete(bool& ready, bool& complete) const;
177 * Query whether the attachment has blending enabled.
178 * @param[in] updateBufferIndex The current update buffer index.
179 * @return true if blending is enabled, false otherwise.
181 bool IsBlendingOn( BufferIndex updateBufferIndex );
184 * Prepare the object for rendering.
185 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
186 * @param[in] updateBufferIndex The current update buffer index.
188 void PrepareRender( BufferIndex updateBufferIndex );
190 public: // API for derived classes
193 * Retrieve a Renderer used by this attachment; implemented in derived classes.
194 * @note The first Renderer is the main renderer for the attachment, and
195 * should always exist during the lifetime of the RenderableAttachment.
196 * @return A Renderer.
198 virtual Renderer& GetRenderer() = 0;
201 * Retrieve a Renderer used by this attachment.
202 * @note The first Renderer is the main renderer for the attachment, and
203 * should always exist during the lifetime of the RenderableAttachment.
204 * Other renderers are for effects such as shadows and reflections.
205 * @return A Renderer.
207 virtual const Renderer& GetRenderer() const = 0;
210 * Prepare the object resources.
211 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
212 * @param[in] updateBufferIndex The current update buffer index.
213 * @param[in] resourceManager The resource manager.
215 void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
218 * If the resource is being tracked, then follow it. ( Further ready tests will use this
219 * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
220 * @param[in] The resource id
222 void FollowTracker( Integration::ResourceId id );
225 * @copydoc RenderableAttachment::PrepareRender()
227 virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
230 * Query whether the attachment is fully opaque.
231 * @param[in] updateBufferIndex The current update buffer index.
232 * @return True if fully opaque.
234 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
237 * Called to notify that the shader might have been changed
238 * The implementation should recalculate geometry and scale based on the
239 * hints from the new shader
240 * @param[in] updateBufferIndex The current update buffer index.
241 * @return Return true if the geometry changed.
243 virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
246 * Called to notify that the size has been changed
247 * The implementation may tell the renderer to recalculate geometry and scale based on the new size
248 * @param[in] updateBufferIndex The current update buffer index.
250 virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
253 * Chained from NodeAttachment::ConnectToSceneGraph()
255 virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
258 * Chained from NodeAttachment::OnDestroy()
260 virtual void OnDestroy2() = 0;
263 * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
264 * @param[in] nodeSize to scale to
265 * @param[out] scaling factors
267 virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
272 * Protected constructor; only base classes can be instantiated.
273 * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
275 RenderableAttachment( bool usesGeometryScaling );
278 * Virtual destructor, no deletion through this interface
280 virtual ~RenderableAttachment();
282 private: // From NodeAttachment
285 * @copydoc NodeAttachment::ConnectToSceneGraph().
287 virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
290 * @copydoc NodeAttachment::DisconnectFromSceneGraph().
292 virtual void OnDestroy();
295 * @copydoc NodeAttachment::GetRenderable()
297 virtual RenderableAttachment* GetRenderable();
302 * Prepare the object resources.
303 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
304 * @param[in] updateBufferIndex The current buffer index.
305 * @param[in] resourceManager The resource manager.
306 * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
308 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
311 RenderableAttachment( const RenderableAttachment& );
314 RenderableAttachment& operator=( const RenderableAttachment& rhs );
318 SceneController* mSceneController; ///< Used for initializing renderers whilst attached
320 BlendingMode::Type mBlendingMode:2; ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
322 bool mUsesGeometryScaling:1; ///< True if the derived renderer uses scaling.
323 bool mScaleForSizeDirty:1; ///< True if mScaleForSize has changed in the current frame.
324 bool mUseBlend:1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
325 bool mHasSizeAndColorFlag:1; ///< Set during the update algorithm to tell whether this renderer can potentially be seen
326 bool mResourcesReady:1; ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
327 bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
328 bool mHasUntrackedResources:1; ///< Set during PrepareResources, true if have tried to follow untracked resources
329 CullFaceMode mCullFaceMode:3; ///< Cullface mode, 3 bits is enough for 4 values
333 Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
336 // Messages for RenderableAttachment
338 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
340 typedef MessageValue1< RenderableAttachment, float > LocalType;
342 // Reserve some memory inside the message queue
343 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
345 // Construct message in the message queue memory; note that delete should not be called on the return value
346 new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
349 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
351 typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
353 // Reserve some memory inside the message queue
354 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
356 // Construct message in the message queue memory; note that delete should not be called on the return value
357 new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
360 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
362 typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
364 // Reserve some memory inside the message queue
365 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
367 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
370 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
372 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
374 // Reserve some memory inside the message queue
375 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
377 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
380 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
382 typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
384 // Reserve some memory inside the message queue
385 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
387 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
390 } // namespace SceneGraph
392 } // namespace Internal
396 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__