2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/common/discard-queue.h>
22 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/update/nodes/node.h>
25 #include <dali/internal/render/queue/render-queue.h>
26 #include <dali/internal/update/node-attachments/scene-graph-renderable-attachment.h>
27 #include <dali/internal/render/renderers/scene-graph-renderer.h>
28 #include <dali/internal/render/shaders/shader.h>
29 #include <dali/internal/update/modeling/scene-graph-mesh.h>
40 namespace // unnamed namespace
43 static void DoGlCleanup( BufferIndex updateBufferIndex, GlResourceOwner& owner, RenderQueue& renderQueue )
45 typedef Message< GlResourceOwner > DerivedType;
47 // Reserve some memory inside the render queue
48 unsigned int* slot = renderQueue.ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
50 // Construct message in the render queue memory; note that delete should not be called on the return value
51 new (slot) DerivedType( &owner, &GlResourceOwner::GlCleanup );
54 } // unnamed namespace
56 DiscardQueue::DiscardQueue( RenderQueue& renderQueue )
57 : mRenderQueue( renderQueue )
61 DiscardQueue::~DiscardQueue()
65 void DiscardQueue::Add( BufferIndex updateBufferIndex, Node* node )
67 DALI_ASSERT_DEBUG( NULL != node );
69 // The GL resources will now be freed in frame N
70 // The Update for frame N+1 may occur in parallel with the rendering of frame N
71 // Queue the node for destruction in frame N+2
72 if ( 0u == updateBufferIndex )
74 mNodeQueue0.PushBack( node );
78 mNodeQueue1.PushBack( node );
82 void DiscardQueue::Add( BufferIndex updateBufferIndex, NodeAttachment* attachment )
84 DALI_ASSERT_DEBUG( NULL != attachment );
86 // The GL resources will now be freed in Render frame N
87 // The Update for frame N+1 may occur in parallel with the rendering of frame N
88 // Queue the attachment for destruction in Update frame N+2
89 if ( 0u == updateBufferIndex )
91 mAttachmentQueue0.PushBack( attachment );
95 mAttachmentQueue1.PushBack( attachment );
99 void DiscardQueue::Add( BufferIndex updateBufferIndex, Mesh* mesh )
101 DALI_ASSERT_DEBUG( mesh );
103 // Send message to clean-up GL resources in the next Render
104 DoGlCleanup( updateBufferIndex, *mesh, mRenderQueue );
106 // The GL resources will now be freed in frame N
107 // The Update for frame N+1 may occur in parallel with the rendering of frame N
108 // Queue the node for destruction in frame N+2
109 if ( 0u == updateBufferIndex )
111 mMeshQueue0.PushBack( mesh );
115 mMeshQueue1.PushBack( mesh );
119 void DiscardQueue::Add( BufferIndex updateBufferIndex, Shader* shader )
121 DALI_ASSERT_DEBUG( NULL != shader );
123 // Programs are cached for the lifetime of DALi so no need for GL cleanup for shader for now.
125 // The GL resources will now be freed in frame N
126 // The Update for frame N+1 may occur in parallel with the rendering of frame N
127 // Queue the node for destruction in frame N+2
128 if ( 0u == updateBufferIndex )
130 mShaderQueue0.PushBack( shader );
134 mShaderQueue1.PushBack( shader );
138 void DiscardQueue::Clear( BufferIndex updateBufferIndex )
140 // Destroy some discarded objects; these should no longer own any GL resources
142 if ( 0u == updateBufferIndex )
145 mAttachmentQueue0.Clear();
147 mShaderQueue0.Clear();
152 mAttachmentQueue1.Clear();
154 mShaderQueue1.Clear();
158 } // namespace SceneGraph
160 } // namespace Internal