[dali_1.0.49] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / update / common / double-buffered-property.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_PROPERTY_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_PROPERTY_H__
3
4 /*
5  * Copyright (c) 2015 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 // EXTERNAL INCLUDES
22 #include <limits>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/property.h>
27 #include <dali/public-api/object/property-input.h>
28 #include <dali/public-api/object/property-types.h>
29 #include <dali/internal/common/message.h>
30 #include <dali/internal/common/type-abstraction.h>
31 #include <dali/internal/event/common/event-thread-services.h>
32 #include <dali/internal/event/common/property-input-impl.h>
33 #include <dali/internal/update/common/double-buffered.h>
34 #include <dali/internal/update/common/scene-graph-buffers.h>
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 namespace SceneGraph
43 {
44
45 namespace DoubleBufferedPropertyFlags
46 {
47 /**
48  * Dirty flags record whether a doubleBuffered property has changed.
49  * In the frame following a change, the property is copied from the most recent
50  * buffer to the old buffer.
51  */
52 static const unsigned int CLEAN_FLAG  = 0x00; ///< Indicates that the value did not change in this, or the previous frame
53 static const unsigned int COPIED_FLAG = 0x01; ///< Indicates that the value was copied during the previous frame
54 static const unsigned int SET_FLAG    = 0x02; ///< Indicates that the value was Set during the previous frame
55 }
56
57 /**
58  * Base class to reduce code size from the templates.
59  */
60 class DoubleBufferedPropertyBase : public PropertyInputImpl
61 {
62 public:
63
64   /**
65    * Constructor, initialize the dirty flag
66    */
67   DoubleBufferedPropertyBase()
68   : PropertyInputImpl(),
69     mDirtyFlags( DoubleBufferedPropertyFlags::COPIED_FLAG )
70   {}
71
72   /**
73    * Virtual destructor.
74    */
75   virtual ~DoubleBufferedPropertyBase()
76   {}
77
78   /**
79    * Auto-age the property: if it was set the previous frame,
80    * then copy the value into the current frame's buffer.
81    */
82   virtual void CopyPrevious( BufferIndex updateBufferIndex ) = 0;
83
84
85 protected: // for derived classes
86   /**
87    * Flag that the property has been Set during the current frame.
88    */
89   void OnSet()
90   {
91     mDirtyFlags = DoubleBufferedPropertyFlags::SET_FLAG;
92   }
93
94 public: // From PropertyBase
95
96   /**
97    * @copydoc Dali::Internal::PropertyInputImpl::InputChanged()
98    */
99   virtual bool InputChanged() const
100   {
101     return ( DoubleBufferedPropertyFlags::CLEAN_FLAG != mDirtyFlags );
102   }
103
104   /**
105    * @copydoc Dali::Internal::PropertyInputImpl::InputInitialized()
106    */
107   virtual bool InputInitialized() const
108   {
109     return true; // DoubleBuffered properties are always valid
110   }
111
112 protected: // so that ResetToBaseValue can set it directly
113
114   unsigned int mDirtyFlags; ///< Flag whether value changed during previous 2 frames
115
116 };
117
118 /**
119  * A doubleBuffered property of a scene-graph object.
120  */
121 template <class T>
122 class DoubleBufferedPropertyImpl : public DoubleBufferedPropertyBase
123 {
124 public:
125   /**
126    * Create an doubleBuffered property.
127    * @param [in] initialValue The initial value of the property.
128    */
129   DoubleBufferedPropertyImpl( typename ParameterType<T>::PassingType initialValue )
130   : mValue( initialValue )
131   {
132   }
133
134   /**
135    * Virtual destructor.
136    */
137   virtual ~DoubleBufferedPropertyImpl()
138   {
139   }
140
141   /**
142    * @copydoc Dali::Internal::SceneGraph::PropertyBase::GetType()
143    */
144   virtual Dali::Property::Type GetType() const
145   {
146     return Dali::PropertyTypes::Get<T>();
147   }
148
149   /**
150    * @copydoc Dali::Internal::SceneGraph::DoubleBufferedPropertyBase::CopyPrevious()
151    */
152   virtual void CopyPrevious( BufferIndex updateBufferIndex )
153   {
154     if( DoubleBufferedPropertyFlags::SET_FLAG == mDirtyFlags)
155     {
156       mValue[ updateBufferIndex ] = mValue[ 1-updateBufferIndex ];
157       mDirtyFlags = ( mDirtyFlags >> 1 );
158     }
159   }
160
161   /**
162    * Set the property value. This will persist for the current frame, and will
163    * be copied to the other buffer next frame (unless it is set again)
164    * @param[in] bufferIndex The buffer to write.
165    * @param[in] value The new property value.
166    */
167   void Set(BufferIndex bufferIndex, typename ParameterType<T>::PassingType value )
168   {
169     // check if the value actually changed to avoid dirtying nodes unnecessarily
170     if( mValue[bufferIndex] != value )
171     {
172       mValue[bufferIndex] = value;
173
174       OnSet();
175     }
176   }
177
178   /**
179    * @copydoc Dali::SceneGraph::DoubleBufferedProperty::Get()
180    */
181   const T& Get(size_t bufferIndex) const
182   {
183     return mValue[bufferIndex];
184   }
185
186   /**
187    * Retrieve the property value.
188    * @param[in] bufferIndex The buffer to read.
189    * @return The property value.
190    */
191   const T& operator[](size_t bufferIndex) const
192   {
193     return mValue[bufferIndex];
194   }
195
196 protected:
197   DoubleBuffered<T> mValue; ///< The double-buffered property value
198
199 private:  // Undefined
200   DoubleBufferedPropertyImpl(const DoubleBufferedPropertyImpl& property);
201   DoubleBufferedPropertyImpl& operator=(const DoubleBufferedPropertyImpl& rhs);
202
203 };
204
205 template<typename T>
206 class DoubleBufferedProperty;
207
208 template<>
209 class DoubleBufferedProperty<bool> : public DoubleBufferedPropertyImpl<bool>
210 {
211 public:
212   /**
213    * Constructor
214    */
215   DoubleBufferedProperty( bool value ) : DoubleBufferedPropertyImpl( value ) {};
216
217   /**
218    * copydoc PropertyInputImpl::GetBoolean
219    */
220   virtual const bool& GetBoolean( BufferIndex bufferIndex ) const
221   {
222     return mValue[bufferIndex];
223   }
224 };
225
226 template<>
227 class DoubleBufferedProperty<int> : public DoubleBufferedPropertyImpl<int>
228 {
229 public:
230   /**
231    * Constructor
232    */
233   DoubleBufferedProperty( int value ) : DoubleBufferedPropertyImpl( value ) {};
234
235   /**
236    * copydoc PropertyInputImpl::GetInteger
237    */
238   virtual const int& GetInteger( BufferIndex bufferIndex ) const
239   {
240     return mValue[bufferIndex];
241   }
242 };
243
244 template<>
245 class DoubleBufferedProperty<float> : public DoubleBufferedPropertyImpl<float>
246 {
247 public:
248   /**
249    * Constructor
250    */
251   DoubleBufferedProperty( float value ) : DoubleBufferedPropertyImpl( value ) {};
252
253   /**
254    * copydoc PropertyInputImpl::GetFloat
255    */
256   virtual const float& GetFloat( BufferIndex bufferIndex ) const
257   {
258     return mValue[bufferIndex];
259   }
260 };
261
262 template<>
263 class DoubleBufferedProperty<Vector2> : public DoubleBufferedPropertyImpl<Vector2>
264 {
265 public:
266   /**
267    * Constructor
268    */
269   DoubleBufferedProperty( const Vector2& value ) : DoubleBufferedPropertyImpl( value ) {};
270
271   /**
272    * copydoc PropertyInputImpl::GetVector2
273    */
274   virtual const Vector2& GetVector2( BufferIndex bufferIndex ) const
275   {
276     return mValue[bufferIndex];
277   }
278 };
279
280 template<>
281 class DoubleBufferedProperty<Vector3> : public DoubleBufferedPropertyImpl<Vector3>
282 {
283 public:
284   /**
285    * Constructor
286    */
287   DoubleBufferedProperty( const Vector3& value ) : DoubleBufferedPropertyImpl( value ) {};
288
289   /**
290    * copydoc PropertyInputImpl::GetVector3
291    */
292   virtual const Vector3& GetVector3( BufferIndex bufferIndex ) const
293   {
294     return mValue[bufferIndex];
295   }
296 };
297
298 template<>
299 class DoubleBufferedProperty<Vector4> : public DoubleBufferedPropertyImpl<Vector4>
300 {
301 public:
302   /**
303    * Constructor
304    */
305   DoubleBufferedProperty( const Vector4& value ) : DoubleBufferedPropertyImpl( value ) {};
306
307   /**
308    * copydoc PropertyInputImpl::GetVector4
309    */
310   virtual const Vector4& GetVector4( BufferIndex bufferIndex ) const
311   {
312     return mValue[bufferIndex];
313   }
314 };
315
316 template<>
317 class DoubleBufferedProperty<Quaternion> : public DoubleBufferedPropertyImpl<Quaternion>
318 {
319 public:
320   /**
321    * Constructor
322    */
323   DoubleBufferedProperty( const Quaternion& value ) : DoubleBufferedPropertyImpl( value ) {};
324
325   /**
326    * copydoc PropertyInputImpl::GetQuaternion
327    */
328   virtual const Quaternion& GetQuaternion( BufferIndex bufferIndex ) const
329   {
330     return mValue[bufferIndex];
331   }
332 };
333
334 template<>
335 class DoubleBufferedProperty<Matrix3> : public DoubleBufferedPropertyImpl<Matrix3>
336 {
337 public:
338   /**
339    * Constructor
340    */
341   DoubleBufferedProperty( const Matrix3& value ) : DoubleBufferedPropertyImpl( value ) {};
342
343   /**
344    * copydoc PropertyInputImpl::GetMatrix3
345    */
346   virtual const Matrix3& GetMatrix3( BufferIndex bufferIndex ) const
347   {
348     return mValue[bufferIndex];
349   }
350 };
351
352 template<>
353 class DoubleBufferedProperty<Matrix> : public DoubleBufferedPropertyImpl<Matrix>
354 {
355 public:
356   /**
357    * Constructor
358    */
359   DoubleBufferedProperty( const Matrix& value ) : DoubleBufferedPropertyImpl( value ) {};
360
361   /**
362    * copydoc PropertyInputImpl::GetMatrix
363    */
364   virtual const Matrix& GetMatrix( BufferIndex bufferIndex ) const
365   {
366     return mValue[bufferIndex];
367   }
368 };
369
370 } // namespace SceneGraph
371
372 } // namespace Internal
373
374 } // namespace Dali
375
376 #endif // __DALI_INTERNAL_SCENE_GRAPH_DOUBLE_BUFFERED_PROPERTY_H__