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 float RenderableAttachment::GetSortModifier() const
58 void RenderableAttachment::SetBlendingMode( BlendingMode::Type mode )
63 BlendingMode::Type RenderableAttachment::GetBlendingMode() const
68 void RenderableAttachment::ChangeBlending( BufferIndex updateBufferIndex, bool useBlend )
70 if ( mUseBlend != useBlend )
74 // Enable/disable blending in the next render
75 typedef MessageValue1< Renderer, bool > DerivedType;
77 // Reserve some memory inside the render queue
78 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
80 // Construct message in the render queue memory; note that delete should not be called on the return value
81 new (slot) DerivedType( &GetRenderer(), &Renderer::SetUseBlend, useBlend );
85 void RenderableAttachment::SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options )
87 // Blending options are forwarded to renderer in render-thread
88 typedef MessageValue1< Renderer, unsigned int > DerivedType;
90 // Reserve some memory inside the render queue
91 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
93 // Construct message in the render queue memory; note that delete should not be called on the return value
94 new (slot) DerivedType( &GetRenderer(), &Renderer::SetBlendingOptions, options );
97 void RenderableAttachment::SetBlendColor( BufferIndex updateBufferIndex, const Vector4& color )
99 // Blend color is forwarded to renderer in render-thread
100 typedef MessageValue1< Renderer, Vector4 > DerivedType;
102 // Reserve some memory inside the render queue
103 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
105 // Construct message in the render queue memory; note that delete should not be called on the return value
106 new (slot) DerivedType( &GetRenderer(), &Renderer::SetBlendColor, color );
109 void RenderableAttachment::PrepareResources( BufferIndex updateBufferIndex, ResourceManager& resourceManager )
111 mHasUntrackedResources = false; // Only need to know this if the resources are not yet complete
112 mTrackedResources.Clear(); // Resource trackers are only needed if not yet completea
114 if( Shader* shader = mParent->GetAppliedShader() )
116 Integration::ResourceId id = shader->GetEffectTextureResourceId();
120 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
122 if(CompleteStatusManager::COMPLETE != completeStatusManager.GetStatus( id ))
125 mFinishedResourceAcquisition = false;
126 mResourcesReady = false;
128 // If shader has effect texture and it's not complete and tracked, ensure
129 // we call DoPrepareResources, as the effect texture may become ready in
130 // the ProcessRenderTasks step. Otherwise, may early out.
131 if( mHasUntrackedResources )
139 mResourcesReady = DoPrepareResources( updateBufferIndex, resourceManager );
142 void RenderableAttachment::FollowTracker( Integration::ResourceId id )
144 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
146 if( completeStatusManager.FindResourceTracker(id) != NULL )
148 mTrackedResources.PushBack( id );
152 mHasUntrackedResources = true;
156 void RenderableAttachment::SetCullFace( BufferIndex updateBufferIndex, CullFaceMode mode )
158 DALI_ASSERT_DEBUG(mSceneController);
159 DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
161 mCullFaceMode = mode;
163 typedef MessageValue1< Renderer, CullFaceMode > DerivedType;
165 // Reserve some memory inside the render queue
166 unsigned int* slot = mSceneController->GetRenderQueue().ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
168 // Construct message in the render queue memory; note that delete should not be called on the return value
169 new (slot) DerivedType( &GetRenderer(), &Renderer::SetCullFace, mode );
172 void RenderableAttachment::SetRecalculateScaleForSize()
174 mScaleForSizeDirty = true;
177 void RenderableAttachment::GetScaleForSize( const Vector3& nodeSize, Vector3& scaling )
179 DoGetScaleForSize( nodeSize, scaling );
180 mScaleForSizeDirty = false;
183 void RenderableAttachment::DoGetScaleForSize( const Vector3& nodeSize, Vector3& scaling )
185 scaling = Vector3::ONE;
188 void RenderableAttachment::GetReadyAndComplete(bool& ready, bool& complete) const
193 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
195 std::size_t numTrackedResources = mTrackedResources.Count();
196 if( mHasUntrackedResources || numTrackedResources == 0 )
198 ready = mResourcesReady;
199 complete = mFinishedResourceAcquisition;
203 // If there are tracked resources and no untracked resources, test the trackers
205 for( size_t i=0; i < numTrackedResources; ++i )
207 ResourceTracker* tracker = completeStatusManager.FindResourceTracker(mTrackedResources[i]);
208 if( tracker && ! tracker->IsComplete() )
215 complete = mFinishedResourceAcquisition;
223 bool RenderableAttachment::IsBlendingOn( BufferIndex updateBufferIndex )
225 // Check whether blending needs to be disabled / enabled
227 switch( mBlendingMode )
229 case BlendingMode::OFF:
235 case BlendingMode::AUTO:
237 // Blending if the node is not fully opaque only.
238 blend = !IsFullyOpaque( updateBufferIndex );
241 case BlendingMode::ON:
249 DALI_ASSERT_ALWAYS( !"RenderableAttachment::PrepareRender. Wrong blending mode" );
255 void RenderableAttachment::PrepareRender( BufferIndex updateBufferIndex )
257 // call the derived class first as it might change its state regarding blending
258 DoPrepareRender( updateBufferIndex );
260 bool blend = IsBlendingOn( updateBufferIndex );
261 ChangeBlending( updateBufferIndex, blend );
264 RenderableAttachment::RenderableAttachment( bool usesGeometryScaling )
265 : mSceneController(NULL),
266 mBlendingMode( Dali::RenderableActor::DEFAULT_BLENDING_MODE ),
267 mUsesGeometryScaling( usesGeometryScaling ),
268 mScaleForSizeDirty( true ),
270 mHasSizeAndColorFlag( false ),
271 mResourcesReady( false ),
272 mFinishedResourceAcquisition( false ),
273 mHasUntrackedResources( false ),
274 mCullFaceMode( CullNone ),
275 mSortModifier( 0.0f )
279 RenderableAttachment::~RenderableAttachment()
283 void RenderableAttachment::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
285 mSceneController = &sceneController;
287 // Chain to derived attachments
288 ConnectToSceneGraph2( updateBufferIndex );
290 // After derived classes have (potentially) created their renderer
291 GetRenderer().SetCullFace( mCullFaceMode );
294 void RenderableAttachment::OnDestroy()
296 // Chain to derived attachments
299 // SceneController is no longer valid
300 mSceneController = NULL;
303 RenderableAttachment* RenderableAttachment::GetRenderable()
309 } // namespace SceneGraph
311 } // namespace Internal