Conversion to Apache 2.0 license
[platform/core/uifw/dali-core.git] / dali / internal / update / common / discard-queue.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/common/discard-queue.h>
20
21 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace SceneGraph
38 {
39
40 namespace // unnamed namespace
41 {
42
43 static void DoGlCleanup( BufferIndex updateBufferIndex, GlResourceOwner& owner, RenderQueue& renderQueue )
44 {
45   typedef Message< GlResourceOwner > DerivedType;
46
47   // Reserve some memory inside the render queue
48   unsigned int* slot = renderQueue.ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
49
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 );
52 }
53
54 } // unnamed namespace
55
56 DiscardQueue::DiscardQueue( RenderQueue& renderQueue )
57 : mRenderQueue( renderQueue )
58 {
59 }
60
61 DiscardQueue::~DiscardQueue()
62 {
63 }
64
65 void DiscardQueue::Add( BufferIndex updateBufferIndex, Node* node )
66 {
67   DALI_ASSERT_DEBUG( NULL != node );
68
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 )
73   {
74     mNodeQueue0.PushBack( node );
75   }
76   else
77   {
78     mNodeQueue1.PushBack( node );
79   }
80 }
81
82 void DiscardQueue::Add( BufferIndex updateBufferIndex, NodeAttachment* attachment )
83 {
84   DALI_ASSERT_DEBUG( NULL != attachment );
85
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 )
90   {
91     mAttachmentQueue0.PushBack( attachment );
92   }
93   else
94   {
95     mAttachmentQueue1.PushBack( attachment );
96   }
97 }
98
99 void DiscardQueue::Add( BufferIndex updateBufferIndex, RefObject& resource )
100 {
101   // Check whether resource has GL data
102   GlResourceOwner* glResource = dynamic_cast<GlResourceOwner*>( &resource );
103   if ( glResource )
104   {
105     // Send message to clean-up GL resources in the next Render
106     DoGlCleanup( updateBufferIndex, *glResource, mRenderQueue );
107   }
108
109   // The GL resources will now be freed in frame N
110   // The Update for frame N+1 may occur in parallel with the rendering of frame N
111   // Queue the node for destruction in frame N+2
112   if ( 0u == updateBufferIndex )
113   {
114     mResourceQueue0.push_back( DiscardQueue::ResourcePointer(&resource) );
115   }
116   else
117   {
118     mResourceQueue1.push_back( DiscardQueue::ResourcePointer(&resource) );
119   }
120 }
121
122 void DiscardQueue::Add( BufferIndex updateBufferIndex, Mesh* mesh )
123 {
124   DALI_ASSERT_DEBUG( mesh );
125
126   // Send message to clean-up GL resources in the next Render
127   DoGlCleanup( updateBufferIndex, *mesh, mRenderQueue );
128
129   // The GL resources will now be freed in frame N
130   // The Update for frame N+1 may occur in parallel with the rendering of frame N
131   // Queue the node for destruction in frame N+2
132   if ( 0u == updateBufferIndex )
133   {
134     mMeshQueue0.PushBack( mesh );
135   }
136   else
137   {
138     mMeshQueue1.PushBack( mesh );
139   }
140 }
141
142 void DiscardQueue::Add( BufferIndex updateBufferIndex, Shader* shader )
143 {
144   DALI_ASSERT_DEBUG( NULL != shader );
145
146   // Programs are cached for the lifetime of DALi so no need for GL cleanup for shader for now.
147
148   // The GL resources will now be freed in frame N
149   // The Update for frame N+1 may occur in parallel with the rendering of frame N
150   // Queue the node for destruction in frame N+2
151   if ( 0u == updateBufferIndex )
152   {
153     mShaderQueue0.PushBack( shader );
154   }
155   else
156   {
157     mShaderQueue1.PushBack( shader );
158   }
159 }
160
161 void DiscardQueue::Clear( BufferIndex updateBufferIndex )
162 {
163   // Destroy some discarded objects; these should no longer own any GL resources
164
165   if ( 0u == updateBufferIndex )
166   {
167     mNodeQueue0.Clear();
168     mAttachmentQueue0.Clear();
169     mResourceQueue0.clear();
170     mMeshQueue0.Clear();
171     mShaderQueue0.Clear();
172   }
173   else
174   {
175     mNodeQueue1.Clear();
176     mAttachmentQueue1.Clear();
177     mResourceQueue1.clear();
178     mMeshQueue1.Clear();
179     mShaderQueue1.Clear();
180   }
181 }
182
183 } // namespace SceneGraph
184
185 } // namespace Internal
186
187 } // namespace Dali