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 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
37 class ResourceManager;
38 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 * Set the sampler used to render the texture for this renderable.
109 * @param[in] updateBufferIndex The current update buffer index.
110 * @param[in] samplerBitfield The image sampler packed options to set.
112 void SetSampler( BufferIndex updateBufferIndex, unsigned int samplerBitfield );
115 * Flag to check if any geometry scaling is needed, inlined as called from update algorithm often
116 * @return true if the derived renderable uses geometry scaling
118 bool UsesGeometryScaling() const
120 return mUsesGeometryScaling;
124 * Triggers scale for size update. GetScaleForSize will be called in this frame
126 void SetRecalculateScaleForSize();
129 * Returns the scaling dirty flag, inlined as called from update algorithm often
130 * @return if scale for size is dirty, i.e. scaling has changed
132 bool IsScaleForSizeDirty() const
134 return mScaleForSizeDirty;
138 * Retrieve scale-for-size for given node size
139 * Clears the scale for size flag
140 * @param[in] nodeSize to scale to
141 * @param[out] scaling factors
143 void GetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
146 * Apply a shader on the renderable
147 * @param[in] updateBufferIndex The current update buffer index.
148 * @param[in] shader to apply.
150 void ApplyShader( BufferIndex updateBufferIndex, Shader* shader );
153 * Remove the shader from the renderable
154 * @param[in] updateBufferIndex The current update buffer index.
156 void RemoveShader( BufferIndex updateBufferIndex );
158 public: // For use during in the update algorithm only
161 * TODO this method should not be virtual but because mesh attachment is a mess, it needs to be
162 * considered visible regardless of its size... need to remove geometry scaling to fix this!!!
163 * @param[in] updateBufferIndex The current update buffer index.
164 * @return visible tells if this renderer can be potentially seen
166 virtual bool ResolveVisibility( BufferIndex updateBufferIndex );
169 * if this renderable actor has visible size and color
170 * @return true if you can potentially see this actor
172 bool HasVisibleSizeAndColor() const
174 return mHasSizeAndColorFlag;
178 * Check whether the attachment has been marked as ready to render
179 * @param[out] ready TRUE if the attachment has resources to render
180 * @param[out] complete TRUE if the attachment's resources are complete
181 * (e.g. image has finished loading, framebuffer is ready to render, native image
182 * framebuffer has been rendered)
184 void GetReadyAndComplete(bool& ready, bool& complete) const;
187 * Query whether the attachment has blending enabled.
188 * @param[in] updateBufferIndex The current update buffer index.
189 * @return true if blending is enabled, false otherwise.
191 bool IsBlendingOn( BufferIndex updateBufferIndex );
194 * Prepare the object for rendering.
195 * This is called by the UpdateManager when an object is due to be rendered in the current frame.
196 * @param[in] updateBufferIndex The current update buffer index.
198 void PrepareRender( BufferIndex updateBufferIndex );
200 public: // API for derived classes
203 * Retrieve a Renderer used by this attachment; implemented in derived classes.
204 * @note The first Renderer is the main renderer for the attachment, and
205 * should always exist during the lifetime of the RenderableAttachment.
206 * @return A Renderer.
208 virtual Renderer& GetRenderer() = 0;
211 * Retrieve a Renderer used by this attachment.
212 * @note The first Renderer is the main renderer for the attachment, and
213 * should always exist during the lifetime of the RenderableAttachment.
214 * Other renderers are for effects such as shadows and reflections.
215 * @return A Renderer.
217 virtual const Renderer& GetRenderer() const = 0;
220 * Prepare the object resources.
221 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
222 * @param[in] updateBufferIndex The current update buffer index.
223 * @param[in] resourceManager The resource manager.
225 void PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager );
228 * If the resource is being tracked, then follow it. ( Further ready tests will use this
229 * list ) Otherwise, if it's not complete, set mHasUntrackedResources.
230 * @param[in] The resource id
232 void FollowTracker( Integration::ResourceId id );
235 * @copydoc RenderableAttachment::PrepareRender()
237 virtual void DoPrepareRender( BufferIndex updateBufferIndex ) = 0;
240 * Query whether the attachment is fully opaque.
241 * @param[in] updateBufferIndex The current update buffer index.
242 * @return True if fully opaque.
244 virtual bool IsFullyOpaque( BufferIndex updateBufferIndex ) = 0;
247 * Called to notify that the shader might have been changed
248 * The implementation should recalculate geometry and scale based on the
249 * hints from the new shader
250 * @param[in] updateBufferIndex The current update buffer index.
251 * @return Return true if the geometry changed.
253 virtual void ShaderChanged( BufferIndex updateBufferIndex ) = 0;
256 * Called to notify that the size has been changed
257 * The implementation may tell the renderer to recalculate geometry and scale based on the new size
258 * @param[in] updateBufferIndex The current update buffer index.
260 virtual void SizeChanged( BufferIndex updateBufferIndex ) = 0;
263 * Chained from NodeAttachment::ConnectToSceneGraph()
265 virtual void ConnectToSceneGraph2( BufferIndex updateBufferIndex ) = 0;
268 * Chained from NodeAttachment::OnDestroy()
270 virtual void OnDestroy2() = 0;
273 * Retrieve the scale-for-size for given node size. Default implementation returns Vector3::ZERO
274 * @param[in] nodeSize to scale to
275 * @param[out] scaling factors
277 virtual void DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling );
282 * Protected constructor; only base classes can be instantiated.
283 * @param usesGeometryScaling should be false if the derived class does not need geometry scaling
285 RenderableAttachment( bool usesGeometryScaling );
288 * Virtual destructor, no deletion through this interface
290 virtual ~RenderableAttachment();
292 private: // From NodeAttachment
295 * @copydoc NodeAttachment::ConnectToSceneGraph().
297 virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
300 * @copydoc NodeAttachment::DisconnectFromSceneGraph().
302 virtual void OnDestroy();
305 * @copydoc NodeAttachment::GetRenderable()
307 virtual RenderableAttachment* GetRenderable();
312 * Prepare the object resources.
313 * This must be called by the UpdateManager before calling PrepareRender, for each frame.
314 * @param[in] updateBufferIndex The current buffer index.
315 * @param[in] resourceManager The resource manager.
316 * @return True if resources are ready, false will prevent PrepareRender being called for this attachment.
318 virtual bool DoPrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager ) = 0;
321 * Sends the shader to the renderer
322 * @param updateBufferIndex for the message buffer
324 void SendShaderChangeMessage( BufferIndex updateBufferIndex );
327 RenderableAttachment( const RenderableAttachment& );
330 RenderableAttachment& operator=( const RenderableAttachment& rhs );
334 SceneController* mSceneController; ///< Used for initializing renderers whilst attached
335 Shader* mShader; ///< A pointer to the shader
337 Dali::Vector< Integration::ResourceId > mTrackedResources; ///< Filled during PrepareResources if there are uncomplete, tracked resources.
341 BlendingMode::Type mBlendingMode:2; ///< Whether blending is used to render the renderable attachment. 2 bits is enough for 3 values
343 bool mUsesGeometryScaling:1; ///< True if the derived renderer uses scaling.
344 bool mScaleForSizeDirty:1; ///< True if mScaleForSize has changed in the current frame.
345 bool mUseBlend:1; ///< True if the attachment & renderer should be considered opaque for sorting and blending.
346 bool mHasSizeAndColorFlag:1; ///< Set during the update algorithm to tell whether this renderer can potentially be seen
347 bool mResourcesReady:1; ///< Set during the Update algorithm; true if the attachment has resources ready for the current frame.
348 bool mFinishedResourceAcquisition:1; ///< Set during DoPrepareResources; true if ready & all resource acquisition has finished (successfully or otherwise)
349 bool mHasUntrackedResources:1; ///< Set during PrepareResources, true if have tried to follow untracked resources
350 CullFaceMode mCullFaceMode:3; ///< Cullface mode, 3 bits is enough for 4 values
354 // Messages for RenderableAttachment
356 inline void SetSortModifierMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, float modifier )
358 typedef MessageValue1< RenderableAttachment, float > LocalType;
360 // Reserve some memory inside the message queue
361 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
363 // Construct message in the message queue memory; note that delete should not be called on the return value
364 new (slot) LocalType( &attachment, &RenderableAttachment::SetSortModifier, modifier );
367 inline void SetCullFaceMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, CullFaceMode mode )
369 typedef MessageDoubleBuffered1< RenderableAttachment, CullFaceMode > LocalType;
371 // Reserve some memory inside the message queue
372 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
374 // Construct message in the message queue memory; note that delete should not be called on the return value
375 new (slot) LocalType( &attachment, &RenderableAttachment::SetCullFace, mode );
378 inline void SetBlendingModeMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, BlendingMode::Type mode )
380 typedef MessageValue1< RenderableAttachment, BlendingMode::Type > LocalType;
382 // Reserve some memory inside the message queue
383 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
385 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingMode, mode );
388 inline void SetBlendingOptionsMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, unsigned int options )
390 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
392 // Reserve some memory inside the message queue
393 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
395 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendingOptions, options );
398 inline void SetBlendColorMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, const Vector4& color )
400 typedef MessageDoubleBuffered1< RenderableAttachment, Vector4 > LocalType;
402 // Reserve some memory inside the message queue
403 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
405 new (slot) LocalType( &attachment, &RenderableAttachment::SetBlendColor, color );
408 inline void SetSamplerMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, unsigned int samplerBitfield )
410 typedef MessageDoubleBuffered1< RenderableAttachment, unsigned int > LocalType;
412 // Reserve some memory inside the message queue
413 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
415 // Construct message in the message queue memory; note that delete should not be called on the return value
416 new (slot) LocalType( &attachment, &RenderableAttachment::SetSampler, samplerBitfield );
419 inline void ApplyShaderMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment, const Shader& constShader )
421 // Update thread can edit the object
422 Shader& shader = const_cast< Shader& >( constShader );
424 typedef MessageDoubleBuffered1< RenderableAttachment, Shader* > LocalType;
426 // Reserve some memory inside the message queue
427 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &attachment, &RenderableAttachment::ApplyShader, &shader );
433 inline void RemoveShaderMessage( EventThreadServices& eventThreadServices, const RenderableAttachment& attachment )
435 typedef MessageDoubleBuffered0< RenderableAttachment > LocalType;
437 // Reserve some memory inside the message queue
438 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
440 // Construct message in the message queue memory; note that delete should not be called on the return value
441 new (slot) LocalType( &attachment, &RenderableAttachment::RemoveShader );
444 } // namespace SceneGraph
446 } // namespace Internal
450 #endif // __DALI_INTERNAL_SCENE_GRAPH_RENDERABLE_ATTACHMENT_H__