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/common/discard-queue.h>
21 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/update/nodes/node.h>
24 #include <dali/internal/render/queue/render-queue.h>
25 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
26 #include <dali/internal/render/renderers/scene-graph-renderer.h>
27 #include <dali/internal/render/shaders/shader.h>
28 #include <dali/internal/update/modeling/scene-graph-mesh.h>
39 namespace // unnamed namespace
42 static void DoGlCleanup( BufferIndex updateBufferIndex, GlResourceOwner& owner, RenderQueue& renderQueue )
44 typedef Message< GlResourceOwner > DerivedType;
46 // Reserve some memory inside the render queue
47 unsigned int* slot = renderQueue.ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
49 // Construct message in the render queue memory; note that delete should not be called on the return value
50 new (slot) DerivedType( &owner, &GlResourceOwner::GlCleanup );
53 } // unnamed namespace
55 DiscardQueue::DiscardQueue( RenderQueue& renderQueue )
56 : mRenderQueue( renderQueue )
60 DiscardQueue::~DiscardQueue()
64 void DiscardQueue::Add( BufferIndex updateBufferIndex, Node* node )
66 DALI_ASSERT_DEBUG( NULL != node );
68 // The GL resources will now be freed in frame N
69 // The Update for frame N+1 may occur in parallel with the rendering of frame N
70 // Queue the node for destruction in frame N+2
71 if ( 0u == updateBufferIndex )
73 mNodeQueue0.PushBack( node );
77 mNodeQueue1.PushBack( node );
81 void DiscardQueue::Add( BufferIndex updateBufferIndex, NodeAttachment* attachment )
83 DALI_ASSERT_DEBUG( NULL != attachment );
85 // The GL resources will now be freed in Render frame N
86 // The Update for frame N+1 may occur in parallel with the rendering of frame N
87 // Queue the attachment for destruction in Update frame N+2
88 if ( 0u == updateBufferIndex )
90 mAttachmentQueue0.PushBack( attachment );
94 mAttachmentQueue1.PushBack( attachment );
98 void DiscardQueue::Add( BufferIndex updateBufferIndex, RefObject& resource )
100 // Check whether resource has GL data
101 GlResourceOwner* glResource = dynamic_cast<GlResourceOwner*>( &resource );
104 // Send message to clean-up GL resources in the next Render
105 DoGlCleanup( updateBufferIndex, *glResource, mRenderQueue );
108 // The GL resources will now be freed in frame N
109 // The Update for frame N+1 may occur in parallel with the rendering of frame N
110 // Queue the node for destruction in frame N+2
111 if ( 0u == updateBufferIndex )
113 mResourceQueue0.push_back( DiscardQueue::ResourcePointer(&resource) );
117 mResourceQueue1.push_back( DiscardQueue::ResourcePointer(&resource) );
121 void DiscardQueue::Add( BufferIndex updateBufferIndex, Mesh* mesh )
123 DALI_ASSERT_DEBUG( mesh );
125 // Send message to clean-up GL resources in the next Render
126 DoGlCleanup( updateBufferIndex, *mesh, mRenderQueue );
128 // The GL resources will now be freed in frame N
129 // The Update for frame N+1 may occur in parallel with the rendering of frame N
130 // Queue the node for destruction in frame N+2
131 if ( 0u == updateBufferIndex )
133 mMeshQueue0.PushBack( mesh );
137 mMeshQueue1.PushBack( mesh );
141 void DiscardQueue::Add( BufferIndex updateBufferIndex, Shader* shader )
143 DALI_ASSERT_DEBUG( NULL != shader );
145 // Programs are cached for the lifetime of DALi so no need for GL cleanup for shader for now.
147 // The GL resources will now be freed in frame N
148 // The Update for frame N+1 may occur in parallel with the rendering of frame N
149 // Queue the node for destruction in frame N+2
150 if ( 0u == updateBufferIndex )
152 mShaderQueue0.PushBack( shader );
156 mShaderQueue1.PushBack( shader );
160 void DiscardQueue::Clear( BufferIndex updateBufferIndex )
162 // Destroy some discarded objects; these should no longer own any GL resources
164 if ( 0u == updateBufferIndex )
167 mAttachmentQueue0.Clear();
168 mResourceQueue0.clear();
170 mShaderQueue0.Clear();
175 mAttachmentQueue1.Clear();
176 mResourceQueue1.clear();
178 mShaderQueue1.Clear();
182 } // namespace SceneGraph
184 } // namespace Internal