Purge underscored header file barriers
[platform/core/uifw/dali-core.git] / dali / internal / update / common / double-buffered.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H
2 #define DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/common/owner-pointer.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 // The number of buffers per scene-graph property
31 static const unsigned int NUM_SCENE_GRAPH_BUFFERS = 2;
32
33 // Buffer index used when reading off-stage values
34 static const unsigned int ARBITRARY_OFF_STAGE_BUFFER = 0;
35
36 namespace SceneGraph
37 {
38
39 /**
40  * Templated class for a double-buffered value.
41  */
42 template <typename T>
43 class DoubleBuffered
44 {
45 public:
46
47   DoubleBuffered()
48   : mValue1(),
49     mValue2()
50   {
51   }
52
53   DoubleBuffered(const T& val)
54   : mValue1(val),
55     mValue2(val)
56   {
57   }
58
59   inline T& operator[](const BufferIndex i)
60   {
61     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
62
63     return *(&mValue1+i);
64   }
65
66   inline const T& operator[](const BufferIndex i) const
67   {
68     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
69
70     return *(&mValue1+i);
71   }
72
73 private:
74
75   // Undefined
76   DoubleBuffered<T>(const DoubleBuffered<T>&);
77
78   // Undefined
79   DoubleBuffered<T>& operator=(const DoubleBuffered<T>& rhs);
80
81 private:
82
83   T mValue1;
84   T mValue2;
85 };
86
87 /**
88  * @brief Specialization for owner-pointer
89  *
90  * This class takes ownership of the pointers and releases the memory when the pointer
91  * is no longer used by either buffer
92  */
93 template <typename T>
94 class DoubleBuffered< OwnerPointer< T > >
95 {
96 public:
97
98   /**
99    * Class that deals with setting a value
100    */
101   class Setter
102   {
103   public:
104     /**
105      * @brief Assignment operator to that a value that will later
106      * be set in the correct buffer index of the object referenced by the setter.
107      */
108     Setter& operator=( T* value )
109     {
110       mValue = value;
111       return *this;
112     }
113
114     ~Setter()
115     {
116       mObject.Set( mIndex, mValue );
117     }
118
119   private:
120     Setter( DoubleBuffered& object,
121             BufferIndex i,
122             T* value )
123     : mObject( object ),
124       mIndex( i ),
125       mValue( value )
126     {
127     }
128
129     Setter( const Setter& rhs )
130     : mObject( rhs.mObject ),
131       mIndex( rhs.mIndex ),
132       mValue( rhs.mValue )
133     {
134     }
135
136     DoubleBuffered& mObject;  ///< Double-buffered object that will be changed
137     const BufferIndex mIndex; ///< Buffer index that will be changed
138     T* mValue;                ///< Value of the pointer
139
140     friend class DoubleBuffered;
141   };
142
143   DoubleBuffered()
144   : mValue1( NULL ),
145     mValue2( NULL )
146   {
147   }
148
149   DoubleBuffered(T* val)
150   : mValue1( val ),
151     mValue2( val )
152   {
153   }
154
155   ~DoubleBuffered()
156   {
157     if( mValue2 != mValue1 )
158     {
159       delete mValue2;
160     }
161     delete mValue1;
162   }
163
164   void Set( BufferIndex i, T* value )
165   {
166     T*& current = *(&mValue1 + i);
167     T*& previous = *(&mValue1 + 1u-i);
168
169     if( current != value && current != previous )
170     {
171       delete current;
172     }
173     current = value;
174   }
175
176   Setter operator[]( BufferIndex i )
177   {
178     return Setter( *this, i, *(&mValue1+i) );
179   }
180
181   const T* operator[]( BufferIndex i ) const
182   {
183     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
184
185     return *(&mValue1+i);
186   }
187
188   /**
189    * Auto-age the property: if it was set the previous frame,
190    * then copy the value into the current frame's buffer.
191    */
192   void CopyPrevious( BufferIndex i )
193   {
194     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
195
196     T*& current = *(&mValue1 + i);
197     T*& previous = *(&mValue1 + 1u-i);
198
199     if( current != previous )
200     {
201       delete current;
202     }
203
204     current = previous;
205   }
206
207 private:
208
209   // Undefined
210   DoubleBuffered(const DoubleBuffered&);
211
212   // Undefined
213   DoubleBuffered& operator=(const DoubleBuffered& rhs);
214
215 private:
216
217   T* mValue1;
218   T* mValue2;
219
220 };
221
222
223 } // namespace SceneGraph
224
225 } // namespace Internal
226
227 } // namespace Dali
228
229 #endif // DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H