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
191 complete = true; // If attachment doesn't have size or color, this should be marked as complete.
193 CompleteStatusManager& completeStatusManager = mSceneController->GetCompleteStatusManager();
195 if( mHasSizeAndColorFlag )
197 std::size_t numTrackedResources = mTrackedResources.Count();
198 if( mHasUntrackedResources || numTrackedResources == 0 )
200 ready = mResourcesReady;
201 complete = mFinishedResourceAcquisition;
205 // If there are tracked resources and no untracked resources, test the trackers
207 for( size_t i=0; i < numTrackedResources; ++i )
209 ResourceTracker* tracker = completeStatusManager.FindResourceTracker(mTrackedResources[i]);
210 if( tracker && ! tracker->IsComplete() )
217 complete = mFinishedResourceAcquisition;
226 bool RenderableAttachment::IsBlendingOn( BufferIndex updateBufferIndex )
228 // Check whether blending needs to be disabled / enabled
230 switch( mBlendingMode )
232 case BlendingMode::OFF:
238 case BlendingMode::AUTO:
240 // Blending if the node is not fully opaque only.
241 blend = !IsFullyOpaque( updateBufferIndex );
244 case BlendingMode::ON:
252 DALI_ASSERT_ALWAYS( !"RenderableAttachment::PrepareRender. Wrong blending mode" );
258 void RenderableAttachment::PrepareRender( BufferIndex updateBufferIndex )
260 // call the derived class first as it might change its state regarding blending
261 DoPrepareRender( updateBufferIndex );
263 bool blend = IsBlendingOn( updateBufferIndex );
264 ChangeBlending( updateBufferIndex, blend );
267 RenderableAttachment::RenderableAttachment( bool usesGeometryScaling )
268 : mSceneController(NULL),
269 mBlendingMode( Dali::RenderableActor::DEFAULT_BLENDING_MODE ),
270 mUsesGeometryScaling( usesGeometryScaling ),
271 mScaleForSizeDirty( true ),
273 mHasSizeAndColorFlag( false ),
274 mResourcesReady( false ),
275 mFinishedResourceAcquisition( false ),
276 mHasUntrackedResources( false ),
277 mCullFaceMode( CullNone ),
278 mSortModifier( 0.0f )
282 RenderableAttachment::~RenderableAttachment()
286 void RenderableAttachment::ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex )
288 mSceneController = &sceneController;
290 // Chain to derived attachments
291 ConnectToSceneGraph2( updateBufferIndex );
293 // After derived classes have (potentially) created their renderer
294 GetRenderer().SetCullFace( mCullFaceMode );
297 void RenderableAttachment::OnDestroy()
299 // Chain to derived attachments
302 // SceneController is no longer valid
303 mSceneController = NULL;
306 RenderableAttachment* RenderableAttachment::GetRenderable()
312 } // namespace SceneGraph
314 } // namespace Internal