ContextObserver removal, part II: Its gone
[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 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
7 //
8 //     http://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/update/common/discard-queue.h>
19
20 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace SceneGraph
37 {
38
39 namespace // unnamed namespace
40 {
41
42 static void DoGlCleanup( BufferIndex updateBufferIndex, GlResourceOwner& owner, RenderQueue& renderQueue )
43 {
44   typedef Message< GlResourceOwner > DerivedType;
45
46   // Reserve some memory inside the render queue
47   unsigned int* slot = renderQueue.ReserveMessageSlot( updateBufferIndex, sizeof( DerivedType ) );
48
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 );
51 }
52
53 } // unnamed namespace
54
55 DiscardQueue::DiscardQueue( RenderQueue& renderQueue )
56 : mRenderQueue( renderQueue )
57 {
58 }
59
60 DiscardQueue::~DiscardQueue()
61 {
62 }
63
64 void DiscardQueue::Add( BufferIndex updateBufferIndex, Node* node )
65 {
66   DALI_ASSERT_DEBUG( NULL != node );
67
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 )
72   {
73     mNodeQueue0.PushBack( node );
74   }
75   else
76   {
77     mNodeQueue1.PushBack( node );
78   }
79 }
80
81 void DiscardQueue::Add( BufferIndex updateBufferIndex, NodeAttachment* attachment )
82 {
83   DALI_ASSERT_DEBUG( NULL != attachment );
84
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 )
89   {
90     mAttachmentQueue0.PushBack( attachment );
91   }
92   else
93   {
94     mAttachmentQueue1.PushBack( attachment );
95   }
96 }
97
98 void DiscardQueue::Add( BufferIndex updateBufferIndex, RefObject& resource )
99 {
100   // Check whether resource has GL data
101   GlResourceOwner* glResource = dynamic_cast<GlResourceOwner*>( &resource );
102   if ( glResource )
103   {
104     // Send message to clean-up GL resources in the next Render
105     DoGlCleanup( updateBufferIndex, *glResource, mRenderQueue );
106   }
107
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 )
112   {
113     mResourceQueue0.push_back( DiscardQueue::ResourcePointer(&resource) );
114   }
115   else
116   {
117     mResourceQueue1.push_back( DiscardQueue::ResourcePointer(&resource) );
118   }
119 }
120
121 void DiscardQueue::Add( BufferIndex updateBufferIndex, Mesh* mesh )
122 {
123   DALI_ASSERT_DEBUG( mesh );
124
125   // Send message to clean-up GL resources in the next Render
126   DoGlCleanup( updateBufferIndex, *mesh, mRenderQueue );
127
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 )
132   {
133     mMeshQueue0.PushBack( mesh );
134   }
135   else
136   {
137     mMeshQueue1.PushBack( mesh );
138   }
139 }
140
141 void DiscardQueue::Add( BufferIndex updateBufferIndex, Shader* shader )
142 {
143   DALI_ASSERT_DEBUG( NULL != shader );
144
145   // Programs are cached for the lifetime of DALi so no need for GL cleanup for shader for now.
146
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 )
151   {
152     mShaderQueue0.PushBack( shader );
153   }
154   else
155   {
156     mShaderQueue1.PushBack( shader );
157   }
158 }
159
160 void DiscardQueue::Clear( BufferIndex updateBufferIndex )
161 {
162   // Destroy some discarded objects; these should no longer own any GL resources
163
164   if ( 0u == updateBufferIndex )
165   {
166     mNodeQueue0.Clear();
167     mAttachmentQueue0.Clear();
168     mResourceQueue0.clear();
169     mMeshQueue0.Clear();
170     mShaderQueue0.Clear();
171   }
172   else
173   {
174     mNodeQueue1.Clear();
175     mAttachmentQueue1.Clear();
176     mResourceQueue1.clear();
177     mMeshQueue1.Clear();
178     mShaderQueue1.Clear();
179   }
180 }
181
182 } // namespace SceneGraph
183
184 } // namespace Internal
185
186 } // namespace Dali