Merge "fix property buffer gl initialization" into devel/master
[platform/core/uifw/dali-core.git] / dali / public-api / math / uint-16-pair.h
1 #ifndef __DALI_UINT_16_PAIR_H__
2 #define __DALI_UINT_16_PAIR_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 <stdint.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/type-traits.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_math
32  * @{
33  */
34
35 /**
36  * @brief Simple class for passing around pairs of small unsigned integers.
37  *
38  * Use this for integer dimensions and points with limited range such as image
39  * sizes and pixel coordinates where a pair of floating point numbers is
40  * inefficient and illogical (i.e. the data is inherently integer).
41  * These are immutable. If you want to change a value, make a whole new object.
42  * One of these can be passed in a single 32 bit integer register on
43  * common architectures.
44  */
45 class Uint16Pair
46 {
47 public:
48   /**
49    * @brief Default constructor for the (0, 0) vector.
50    */
51   Uint16Pair() : mData(0) {}
52
53   /**
54    * @brief Constructor taking separate x and y (width and height) parameters.
55    * @param[in] width The width or X dimension of the vector. Make sure it is less than 65536,
56    * @param[in] height The height or Y dimension of the vector. Make sure it is less than 65536,
57    */
58   Uint16Pair( uint32_t width, uint32_t height )
59   {
60     DALI_ASSERT_DEBUG( width < ( 1u << 16 ) && "Width parameter not representable." );
61     DALI_ASSERT_DEBUG( height < ( 1u << 16 ) && "Height parameter not representable." );
62
63     /* Do equivalent of the code below with one aligned memory access:
64      * mComponents[0] = width;
65      * mComponents[1] = height;
66      * Unit tests make sure this is equivalent.
67      **/
68     mData = (height << 16u) + width;
69   }
70
71   /**
72    * @brief Copy constructor.
73    */
74   Uint16Pair( const Uint16Pair& rhs )
75   {
76     mData = rhs.mData;
77   }
78
79   /**
80    * @returns the x dimension stored in this 2-tuple.
81    */
82   uint16_t GetWidth() const
83   {
84     return mComponents[0];
85   }
86
87   /**
88    * @returns the y dimension stored in this 2-tuple.
89    */
90   uint16_t GetHeight() const
91   {
92     return mComponents[1];
93   }
94
95   /**
96    * @returns the x dimension stored in this 2-tuple.
97    */
98   uint16_t GetX()  const
99   {
100     return mComponents[0];
101   }
102
103   /**
104    * @returns the y dimension stored in this 2-tuple.
105    */
106   uint16_t GetY() const
107   {
108     return mComponents[1];
109   }
110
111   /**
112    * Assignment operator.
113    */
114   Uint16Pair& operator=( const Uint16Pair& rhs )
115   {
116     if( rhs != *this )
117     {
118       mData = rhs.mData;
119     }
120     return *this;
121   }
122
123   /**
124    * Equality operator.
125    */
126   bool operator==( const Uint16Pair& rhs ) const
127   {
128     return mData == rhs.mData;
129   }
130
131   /**
132    * Inequality operator.
133    */
134   bool operator!=( const Uint16Pair& rhs ) const
135   {
136     return mData != rhs.mData;
137   }
138
139   /**
140    * Less than comparison operator for storing in collections (not geometrically
141    * meaningful).
142    */
143   bool operator<( const Uint16Pair& rhs ) const
144   {
145     return mData < rhs.mData;
146   }
147
148   /**
149    * Greater than comparison operator for storing in collections (not
150    * geometrically meaningful).
151    */
152   bool operator>( const Uint16Pair& rhs ) const
153   {
154     return mData > rhs.mData;
155   }
156
157   /**
158    * @brief Create an instance by rounding a floating point vector to closest
159    * integers.
160    *
161    * Uses a template for loose coupling, to save a header include, and allow any
162    * vector type with .x and .y members to be converted.
163    */
164   template<typename FLOAT_VECTOR_N_TYPE>
165   static Uint16Pair FromFloatVec2( const FLOAT_VECTOR_N_TYPE& from )
166   {
167     DALI_ASSERT_DEBUG( from.x + 0.5f < 65536.0f );
168     DALI_ASSERT_DEBUG( from.y + 0.5f < 65536.0f );
169     return Uint16Pair( from.x + 0.5f, from.y + 0.5f );
170   }
171
172   /**
173    * @brief Create an instance by rounding a floating point array to closest
174    * integers.
175    *
176    * Uses a template to allow any vector type with operator [] to be converted
177    * in addition to plain arrays.
178    */
179   template<typename FLOAT_ARRAY>
180   static Uint16Pair FromFloatArray( const FLOAT_ARRAY& from )
181   {
182     DALI_ASSERT_DEBUG( from[0] + 0.5f < 65536.0f );
183     DALI_ASSERT_DEBUG( from[1] + 0.5f < 65536.0f );
184     return Uint16Pair( from[0] + 0.5f, from[1] + 0.5f );
185   }
186
187 private:
188   union
189   {
190     // Addressable view of X and Y:
191     uint16_t mComponents[2];
192     // Packed view of X and Y to force alignment and allow a faster copy:
193     uint32_t mData;
194   };
195 };
196
197 // Allow Uint16Pair to be treated as a POD type
198 template <> struct TypeTraits< Uint16Pair > : public BasicTypes< Uint16Pair > { enum { IS_TRIVIAL_TYPE = true }; };
199
200 /**
201  * @}
202  */
203 } // namespace Dali
204
205 #endif // __DALI_UINT_16_PAIR_H__