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
65 // inlined as its called a lot when sorting transparent renderers
70 * @See Dali::RenderableActor::SetBlendMode().
72 void SetBlendingMode( BlendingMode::Type mode );
75 * @copydoc Dali::RenderableActor::GetBlendMode().
77 BlendingMode::Type GetBlendingMode() const;
80 * Check if the blending mode has changed - if it has, send message to renderer
81 * @param[in] updateBufferIndex The current update buffer index.
82 * @param[in] useBlending True if the renderer should use blending option
84 void ChangeBlending( BufferIndex updateBufferIndex, bool useBlending );
87 * Set the blending options. This should only be called from the update-thread.
88 * @param[in] updateBufferIndex The current update buffer index.
89 * @param[in] options A bitmask of blending options.
91 void SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options );
94 * Set the blend-color. This should only be called from the update-thread.
95 * @param[in] updateBufferIndex The current update buffer index.
96 * @param[in] color The new blend-color.
98 void SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color );
101 * Set the face-culling mode.
102 * @param[in] updateBufferIndex The current update buffer index.
103 * @param[in] mode The face-culling mode.
105 void SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode );
108 * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
109 * @return true if the derived renderable uses geometry scaling
111 bool UsesGeometryScaling() const
113 return mUsesGeometryScaling;
117 * Triggers scale for size update. GetScaleForSize will be called in this frame
119 void SetRecalculateScaleForSize();
122 * Returns the scaling dirty flag, inlined as called from update algorithm often
123 * @return if scale for size is dirty, i.e. scaling has changed
125 bool IsScaleForSizeDirty() const
127 return mScaleForSizeDirty;
131 * Retrieve scale-for-size for given node size
132 * Clears the scale for size flag
133 * @param[in] nodeSize to scale to
134 * @param[out] scaling factors
136 void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
138 public: // For use during in the update algorithm only
141 * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
142 * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
143 * @param[in] updateBufferIndex The current update buffer index.
144 * @return visible tells if this renderer can be potentially seen
146 virtual bool ResolveVisibility( BufferIndex updateBufferIndex )
148 mHasSizeAndColorFlag = false;
149 const Vector4& color = mParent->GetWorldColor( updateBufferIndex );
150 if( color.a > FULLY_TRANSPARENT ) // not fully transparent
152 const Vector3& size = mParent->GetSize( updateBufferIndex );
153 if( ( size.width > Math::MACHINE_EPSILON_1000 )&& // width is greater than a very small number
154 ( size.height > Math::MACHINE_EPSILON_1000 ) ) // height is greater than a very small number
156 mHasSizeAndColorFlag = true;
159 return mHasSizeAndColorFlag;
163 * if this renderable actor has visible size and color
164 * @return true if you can potentially see this actor
166 bool HasVisibleSizeAndColor() const
168 return mHasSizeAndColorFlag;
172 * Check whether the attachment has been marked as ready to render
173 * @param[out] ready TRUE if the attachment has resources to render
174 * @param[out] complete TRUE if the attachment's resources are complete
175 * (e.g. image has finished loading, framebuffer is ready to render, native image
176 * framebuffer has been rendered)
178 void GetReadyAndComplete(bool& ready, bool& complete) const;
181 * Query whether the attachment has blending enabled.
182 * @param[in] updateBufferIndex The current update buffer index.
183 * @return true if blending is enabled, false otherwise.
185 bool IsBlendingOn( BufferIndex updateBufferIndex );
188 * Prepare the object for rendering.
189 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
190 * @param[in] updateBufferIndex The current update buffer index.
192 void PrepareRender( BufferIndex updateBufferIndex );
194 public: // API for derived classes
197 * Retrieve a Renderer used by this attachment; implemented in derived classes.
198 * @note The first Renderer is the main renderer for the attachment, and
199 * should always exist during the lifetime of the RenderableAttachment.
200 * @return A Renderer.
202 virtual Renderer& GetRenderer() = 0;
205 * Retrieve a Renderer used by this attachment.
206 * @note The first Renderer is the main renderer for the attachment, and
207 * should always exist during the lifetime of the RenderableAttachment.
208 * Other renderers are for effects such as shadows and reflections.
209 * @return A Renderer.
211 virtual const Renderer& GetRenderer() const = 0;
214 * Prepare the object resources.
215 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
216 * @param[in] updateBufferIndex The current update buffer index.
217 * @param[in] resourceManager The resource manager.
219 void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
222 * If the resource is being tracked, then follow it. ( Further ready tests will use this
223 * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
224 * @param[in] The resource id
226 void FollowTracker( Integration::ResourceId id );
229 * @copydoc RenderableAttachment::PrepareRender()
231 virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
234 * Query whether the attachment is fully opaque.
235 * @param[in] updateBufferIndex The current update buffer index.
236 * @return True if fully opaque.
238 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
241 * Called to notify that the shader might have been changed
242 * The implementation should recalculate geometry and scale based on the
243 * hints from the new shader
244 * @param[in] updateBufferIndex The current update buffer index.
245 * @return Return true if the geometry changed.
247 virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
250 * Called to notify that the size has been changed
251 * The implementation may tell the renderer to recalculate geometry and scale based on the new size
252 * @param[in] updateBufferIndex The current update buffer index.
254 virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
257 * Chained from NodeAttachment::ConnectToSceneGraph()
259 virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
262 * Chained from NodeAttachment::OnDestroy()
264 virtual void OnDestroy2() = 0;
267 * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
268 * @param[in] nodeSize to scale to
269 * @param[out] scaling factors
271 virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
276 * Protected constructor; only base classes can be instantiated.
277 * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
279 RenderableAttachment( bool usesGeometryScaling );
282 * Virtual destructor, no deletion through this interface
284 virtual ~RenderableAttachment();
286 private: // From NodeAttachment
289 * @copydoc NodeAttachment::ConnectToSceneGraph().
291 virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
294 * @copydoc NodeAttachment::DisconnectFromSceneGraph().
296 virtual void OnDestroy();
299 * @copydoc NodeAttachment::GetRenderable()
301 virtual RenderableAttachment* GetRenderable();
306 * Prepare the object resources.
307 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
308 * @param[in] updateBufferIndex The current buffer index.
309 * @param[in] resourceManager The resource manager.
310 * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
312 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
315 RenderableAttachment( const RenderableAttachment& );
318 RenderableAttachment& operator=( const RenderableAttachment& rhs );
322 SceneController* mSceneController; ///< Used for initializing renderers whilst attached
324 BlendingMode::Type mBlendingMode:2; ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
326 bool mUsesGeometryScaling:1; ///< True if the derived renderer uses scaling.
327 bool mScaleForSizeDirty:1; ///< True if mScaleForSize has changed in the current frame.
328 bool mUseBlend:1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
329 bool mHasSizeAndColorFlag:1; ///< Set during the update algorithm to tell whether this renderer can potentially be seen
330 bool mResourcesReady:1; ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
331 bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
332 bool mHasUntrackedResources:1; ///< Set during PrepareResources, true if have tried to follow untracked resources
333 CullFaceMode mCullFaceMode:3; ///< Cullface mode, 3 bits is enough for 4 values
337 Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
340 // Messages for RenderableAttachment
342 inline void SetSortModifierMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, float modifier )
344 typedef MessageValue1< RenderableAttachment, float > LocalType;
346 // Reserve some memory inside the message queue
347 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
349 // Construct message in the message queue memory; note that delete should not be called on the return value
350 new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
353 inline void SetCullFaceMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, CullFaceMode mode )
355 typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
357 // Reserve some memory inside the message queue
358 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
360 // Construct message in the message queue memory; note that delete should not be called on the return value
361 new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
364 inline void SetBlendingModeMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, BlendingMode::Type mode )
366 typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
368 // Reserve some memory inside the message queue
369 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
371 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
374 inline void SetBlendingOptionsMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, unsigned int options )
376 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
378 // Reserve some memory inside the message queue
379 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
381 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
384 inline void SetBlendColorMessage( EventToUpdate& eventToUpdate, const RenderableAttachment& attachment, const Vector4& color )
386 typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
388 // Reserve some memory inside the message queue
389 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
391 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
394 } // namespace SceneGraph
396 } // namespace Internal
400 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__