2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
21 #include <dali/integration-api/resource-declarations.h>
22 #include <dali/public-api/actors/renderable-actor.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/internal/update/resources/resource-manager.h>
25 #include <dali/internal/update/resources/complete-status-manager.h>
26 #include <dali/internal/update/resources/resource-tracker.h>
27 #include <dali/internal/render/queue/render-queue.h>
28 #include <dali/internal/render/renderers/scene-graph-renderer.h>
29 #include <dali/internal/render/shaders/shader.h>
42 void RenderableAttachment::SetSortModifier(float modifier)
44 // Setting sort modifier makes the node dirty, i.e. we cannot reuse previous frames render items
47 // only do this if we are on-stage
48 mParent->SetDirtyFlag( SortModifierFlag );
50 mSortModifier = modifier;
53 void RenderableAttachment::SetBlendingMode( BlendingMode::Type mode )
58 BlendingMode::Type RenderableAttachment::GetBlendingMode() const
63 void RenderableAttachment::ChangeBlending( BufferIndex updateBufferIndex, bool useBlend )
65 if ( mUseBlend != useBlend )
69 // Enable/disable blending in the next render
70 typedef MessageValue1< Renderer, bool > DerivedType;
72 // Reserve some memory inside the render queue
73 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
75 // Construct message in the render queue memory; note that delete should not be called on the return value
76 new (slot) DerivedType( &GetRenderer(), &Renderer::SetUseBlend, useBlend );
80 void RenderableAttachment::SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options )
82 // Blending options are forwarded to renderer in render-thread
83 typedef MessageValue1< Renderer, unsigned int > DerivedType;
85 // Reserve some memory inside the render queue
86 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
88 // Construct message in the render queue memory; note that delete should not be called on the return value
89 new (slot) DerivedType( &GetRenderer(), &Renderer::SetBlendingOptions, options );
92 void RenderableAttachment::SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color )
94 // Blend color is forwarded to renderer in render-thread
95 typedef MessageValue1< Renderer, Vector4 > DerivedType;
97 // Reserve some memory inside the render queue
98 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
100 // Construct message in the render queue memory; note that delete should not be called on the return value
101 new (slot) DerivedType( &GetRenderer(), &Renderer::SetBlendColor, color );
104 void RenderableAttachment::PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager )
106 mHasUntrackedResources = false; // Only need to know this if the resources are not yet complete
107 mTrackedResources.Clear(); // Resource trackers are only needed if not yet completea
109 if( Shader* shader = mParent->GetAppliedShader() )
111 Integration::ResourceId id = shader->GetEffectTextureResourceId();
115 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
117 if(CompleteStatusManager::COMPLETE != completeStatusManager.GetStatus( id ))
120 mFinishedResourceAcquisition = false;
121 mResourcesReady = false;
123 // If shader has effect texture and it's not complete and tracked, ensure
124 // we call DoPrepareResources, as the effect texture may become ready in
125 // the ProcessRenderTasks step. Otherwise, may early out.
126 if( mHasUntrackedResources )
134 mResourcesReady = DoPrepareResources( updateBufferIndex, resourceManager );
137 void RenderableAttachment::FollowTracker( Integration::ResourceId id )
139 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
141 if( completeStatusManager.FindResourceTracker(id) != NULL )
144 std::size_t numTrackedResources = mTrackedResources.Count();
145 for( size_t i=0; i < numTrackedResources; ++i )
147 if(mTrackedResources[i] == id)
155 mTrackedResources.PushBack( id );
160 mHasUntrackedResources = true;
164 void RenderableAttachment::SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode )
166 DALI_ASSERT_DEBUG(mSceneController);
167 DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
169 mCullFaceMode = mode;
171 typedef MessageValue1< Renderer, CullFaceMode > DerivedType;
173 // Reserve some memory inside the render queue
174 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
176 // Construct message in the render queue memory; note that delete should not be called on the return value
177 new (slot) DerivedType( &GetRenderer(), &Renderer::SetCullFace, mode );
180 void RenderableAttachment::SetRecalculateScaleForSize()
182 mScaleForSizeDirty = true;
185 void RenderableAttachment::GetScaleForSize( const Vector3& nodeSize, Vector3& scaling )
187 DoGetScaleForSize( nodeSize, scaling );
188 mScaleForSizeDirty = false;
191 void RenderableAttachment::DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling )
193 scaling = Vector3::ONE;
196 void RenderableAttachment::GetReadyAndComplete(bool& ready, bool& complete) const
198 ready = mResourcesReady;
201 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
203 std::size_t numTrackedResources = mTrackedResources.Count();
204 if( mHasUntrackedResources || numTrackedResources == 0 )
206 complete = mFinishedResourceAcquisition;
210 // If there are tracked resources and no untracked resources, test the trackers
211 bool trackersComplete = true;
212 for( size_t i=0; i < numTrackedResources; ++i )
214 ResourceId id = mTrackedResources[i];
215 ResourceTracker* tracker = completeStatusManager.FindResourceTracker(id);
216 if( tracker && ! tracker->IsComplete() )
218 trackersComplete = false;
223 complete = mFinishedResourceAcquisition || trackersComplete;
227 bool RenderableAttachment::IsBlendingOn( BufferIndex updateBufferIndex )
229 // Check whether blending needs to be disabled / enabled
231 switch( mBlendingMode )
233 case BlendingMode::OFF:
239 case BlendingMode::AUTO:
241 // Blending if the node is not fully opaque only.
242 blend = !IsFullyOpaque( updateBufferIndex );
245 case BlendingMode::ON:
253 DALI_ASSERT_ALWAYS( !"RenderableAttachment::PrepareRender. Wrong blending mode" );
259 void RenderableAttachment::PrepareRender( BufferIndex updateBufferIndex )
261 // call the derived class first as it might change its state regarding blending
262 DoPrepareRender( updateBufferIndex );
264 bool blend = IsBlendingOn( updateBufferIndex );
265 ChangeBlending( updateBufferIndex, blend );
268 RenderableAttachment::RenderableAttachment( bool usesGeometryScaling )
269 : mSceneController(NULL),
270 mBlendingMode( Dali::RenderableActor::DEFAULT_BLENDING_MODE ),
271 mUsesGeometryScaling( usesGeometryScaling ),
272 mScaleForSizeDirty( true ),
274 mHasSizeAndColorFlag( false ),
275 mResourcesReady( false ),
276 mFinishedResourceAcquisition( false ),
277 mHasUntrackedResources( false ),
278 mCullFaceMode( CullNone ),
279 mSortModifier( 0.0f )
283 RenderableAttachment::~RenderableAttachment()
287 void RenderableAttachment::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
289 mSceneController = &sceneController;
291 // Chain to derived attachments
292 ConnectToSceneGraph2( updateBufferIndex );
294 // After derived classes have (potentially) created their renderer
295 GetRenderer().SetCullFace( mCullFaceMode );
298 void RenderableAttachment::OnDestroy()
300 // Chain to derived attachments
303 // SceneController is no longer valid
304 mSceneController = NULL;
307 RenderableAttachment* RenderableAttachment::GetRenderable()
313 } // namespace SceneGraph
315 } // namespace Internal