License conversion from Flora to Apache 2.0
[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) 2014 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/public-api/math/matrix.h>
23 #include <dali/public-api/math/quaternion.h>
24 #include <dali/public-api/math/vector3.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 // The number of buffers per scene-graph property
33 static const unsigned int NUM_SCENE_GRAPH_BUFFERS = 2;
34
35 // Buffer index used when reading off-stage values
36 static const unsigned int ARBITRARY_OFF_STAGE_BUFFER = 0;
37
38 namespace SceneGraph
39 {
40
41 /**
42  * Templated class for a double-buffered value.
43  * This simplifies init code, and forces explicit initialization.
44  */
45 template <typename T>
46 class DoubleBuffered
47 {
48 public:
49
50   DoubleBuffered(const T& val)
51   : mValue1(val),
52     mValue2(val)
53   {
54   }
55
56   inline T& operator[](const size_t i)
57   {
58     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
59
60     return *(&mValue1+i);
61   }
62
63   inline const T& operator[](const size_t i) const
64   {
65     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
66
67     return *(&mValue1+i);
68   }
69
70 private:
71
72   // Undefined
73   DoubleBuffered<T>(const DoubleBuffered<T>&);
74
75   // Undefined
76   DoubleBuffered<T>& operator=(const DoubleBuffered<T>& rhs);
77
78 private:
79
80   T mValue1;
81   T mValue2;
82 };
83
84 /**
85  * Templated class for a double-buffered value, initialized with 3 value parameters.
86  * This simplifies init code, and forces explicit initialization.
87  */
88 template <typename T, typename P>
89 class DoubleBuffered3
90 {
91 public:
92
93   DoubleBuffered3(const T& val)
94   : mValue1(val),
95     mValue2(val)
96   {
97   }
98
99   DoubleBuffered3(P val1, P val2, P val3)
100   : mValue1(val1, val2, val3),
101     mValue2(val1, val2, val3)
102   {
103   }
104
105   inline T& operator[](const size_t i)
106   {
107     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
108
109     return *(&mValue1+i);
110   }
111
112   inline const T& operator[](const size_t i) const
113   {
114     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
115
116     return *(&mValue1+i);
117   }
118
119 private:
120
121   // Undefined
122   DoubleBuffered3<T,P>(const DoubleBuffered3<T,P>&);
123
124   // Undefined
125   DoubleBuffered3<T,P>& operator=(const DoubleBuffered3<T,P>& rhs);
126
127 private:
128
129   T mValue1;
130   T mValue2;
131 };
132
133 /**
134  * Templated class for a double-buffered value, initialized with 4 value parameters.
135  * This simplifies init code, and forces explicit initialization.
136  */
137 template <typename T, typename P>
138 class DoubleBuffered4
139 {
140 public:
141
142   DoubleBuffered4(const T& val)
143   : mValue1(val),
144     mValue2(val)
145   {
146   }
147
148   DoubleBuffered4(P val1, P val2, P val3, P val4)
149   : mValue1(val1, val2, val3, val4),
150     mValue2(val1, val2, val3, val4)
151   {
152   }
153
154   inline T& operator[](const size_t i)
155   {
156     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
157
158     return *(&mValue1+i);
159   }
160
161   inline const T& operator[](const size_t i) const
162   {
163     DALI_ASSERT_DEBUG(i < NUM_SCENE_GRAPH_BUFFERS);
164
165     return *(&mValue1+i);
166   }
167
168 private:
169
170   // Undefined
171   DoubleBuffered4<T,P>(const DoubleBuffered4<T,P>&);
172
173   // Undefined
174   DoubleBuffered4<T,P>& operator=(const DoubleBuffered4<T,P>& rhs);
175
176 private:
177
178   T mValue1;
179   T mValue2;
180 };
181
182 typedef DoubleBuffered<int>   DoubleBufferedInt;
183 typedef DoubleBuffered<float> DoubleBufferedFloat;
184 typedef DoubleBuffered<bool>  DoubleBufferedBool;
185
186 typedef DoubleBuffered3<Vector3,float>      DoubleBufferedVector3;
187 typedef DoubleBuffered4<Vector4,float>      DoubleBufferedVector4;
188
189 typedef DoubleBuffered4<Quaternion, float>  DoubleBufferedQuaternion;
190
191 typedef DoubleBuffered<Matrix> DoubleBufferedMatrix;
192
193 } // namespace SceneGraph
194
195 } // namespace Internal
196
197 } // namespace Dali
198
199 #endif // __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_H__