4af77753f822660d264545506647cf7e9ab3ad6e
[platform/core/uifw/lottie-player.git] / src / vector / vglobal.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (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 #ifndef VGLOBAL_H
18 #define VGLOBAL_H
19
20 #include <cmath>
21 #include <cstdint>
22 #include <iostream>
23 #include <type_traits>
24 #include <utility>
25
26 typedef uint32_t uint;
27 typedef uint16_t ushort;
28 typedef uint8_t  uchar;
29
30 #if !defined(V_NAMESPACE)
31
32 #define V_USE_NAMESPACE
33 #define V_BEGIN_NAMESPACE
34 #define V_END_NAMESPACE
35
36 #else /* user namespace */
37
38 #define V_USE_NAMESPACE using namespace ::V_NAMESPACE;
39 #define V_BEGIN_NAMESPACE namespace V_NAMESPACE {
40 #define V_END_NAMESPACE }
41
42 #endif
43
44 #define V_UNUSED __attribute__((__unused__))
45 #define V_REQUIRED_RESULT __attribute__((__warn_unused_result__))
46
47 #define V_CONSTEXPR constexpr
48 #define V_NOTHROW noexcept
49
50 #include "vdebug.h"
51
52 #if __GNUC__ >= 7
53 #define VECTOR_FALLTHROUGH __attribute__ ((fallthrough));
54 #else
55 #define VECTOR_FALLTHROUGH
56 #endif
57
58 #include <atomic>
59 class RefCount {
60 public:
61     inline RefCount(int i) : atomic(i) {}
62     inline bool ref()
63     {
64         int count = atomic.load();
65         if (count == 0)  // !isSharable
66             return false;
67         if (count != -1)  // !isStatic
68             atomic.fetch_add(1);
69         return true;
70     }
71     inline bool deref()
72     {
73         int count = atomic.load();
74         if (count == 0)  // !isSharable
75             return false;
76         if (count == -1)  // isStatic
77             return true;
78         atomic.fetch_sub(1);
79         return --count;
80     }
81     bool isShared() const
82     {
83         int count = atomic.load();
84         return (count != 1) && (count != 0);
85     }
86     bool isStatic() const
87     {
88         // Persistent object, never deleted
89         int count = atomic.load();
90         return count == -1;
91     }
92     inline int count() const { return atomic; }
93     void       setOwned() { atomic.store(1); }
94
95 private:
96     std::atomic<int> atomic;
97 };
98
99 template <typename T>
100 V_CONSTEXPR inline const T &vMin(const T &a, const T &b)
101 {
102     return (a < b) ? a : b;
103 }
104 template <typename T>
105 V_CONSTEXPR inline const T &vMax(const T &a, const T &b)
106 {
107     return (a < b) ? b : a;
108 }
109
110 static const double EPSILON_DOUBLE = 0.000000000001f;
111 static const float  EPSILON_FLOAT = 0.000001f;
112
113 static inline bool vCompare(double p1, double p2)
114 {
115     return (std::abs(p1 - p2) < EPSILON_DOUBLE);
116 }
117
118 static inline bool vCompare(float p1, float p2)
119 {
120     return (std::abs(p1 - p2) < EPSILON_FLOAT);
121 }
122
123 static inline bool vIsZero(float f)
124 {
125     return (std::abs(f) <= EPSILON_FLOAT);
126 }
127
128 static inline bool vIsZero(double f)
129 {
130     return (std::abs(f) <= EPSILON_DOUBLE);
131 }
132
133 class vFlagHelper {
134     int i;
135
136 public:
137     constexpr inline vFlagHelper(int ai) noexcept : i(ai) {}
138     constexpr inline operator int() const noexcept { return i; }
139
140     constexpr inline vFlagHelper(uint ai) noexcept : i(int(ai)) {}
141     constexpr inline vFlagHelper(short ai) noexcept : i(int(ai)) {}
142     constexpr inline vFlagHelper(ushort ai) noexcept : i(int(uint(ai))) {}
143     constexpr inline operator uint() const noexcept { return uint(i); }
144 };
145
146 template <typename Enum>
147 class vFlag {
148 public:
149     static_assert(
150         (sizeof(Enum) <= sizeof(int)),
151         "vFlag only supports int as storage so bigger type will overflow");
152     static_assert((std::is_enum<Enum>::value),
153                   "vFlag is only usable on enumeration types.");
154
155     typedef typename std::conditional<
156         std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
157         unsigned int, signed int>::type Int;
158
159     typedef Enum enum_type;
160     // compiler-generated copy/move ctor/assignment operators are fine!
161
162     constexpr inline vFlag(Enum f) noexcept : i(Int(f)) {}
163     constexpr inline vFlag() noexcept : i(0) {}
164     constexpr inline vFlag(vFlagHelper f) noexcept : i(f) {}
165
166     inline vFlag &operator&=(int mask) noexcept
167     {
168         i &= mask;
169         return *this;
170     }
171     inline vFlag &operator&=(uint mask) noexcept
172     {
173         i &= mask;
174         return *this;
175     }
176     inline vFlag &operator&=(Enum mask) noexcept
177     {
178         i &= Int(mask);
179         return *this;
180     }
181     inline vFlag &operator|=(vFlag f) noexcept
182     {
183         i |= f.i;
184         return *this;
185     }
186     inline vFlag &operator|=(Enum f) noexcept
187     {
188         i |= Int(f);
189         return *this;
190     }
191     inline vFlag &operator^=(vFlag f) noexcept
192     {
193         i ^= f.i;
194         return *this;
195     }
196     inline vFlag &operator^=(Enum f) noexcept
197     {
198         i ^= Int(f);
199         return *this;
200     }
201
202     constexpr inline operator Int() const noexcept { return i; }
203
204     constexpr inline vFlag operator|(vFlag f) const
205     {
206         return vFlag(vFlagHelper(i | f.i));
207     }
208     constexpr inline vFlag operator|(Enum f) const noexcept
209     {
210         return vFlag(vFlagHelper(i | Int(f)));
211     }
212     constexpr inline vFlag operator^(vFlag f) const noexcept
213     {
214         return vFlag(vFlagHelper(i ^ f.i));
215     }
216     constexpr inline vFlag operator^(Enum f) const noexcept
217     {
218         return vFlag(vFlagHelper(i ^ Int(f)));
219     }
220     constexpr inline vFlag operator&(int mask) const noexcept
221     {
222         return vFlag(vFlagHelper(i & mask));
223     }
224     constexpr inline vFlag operator&(uint mask) const noexcept
225     {
226         return vFlag(vFlagHelper(i & mask));
227     }
228     constexpr inline vFlag operator&(Enum f) const noexcept
229     {
230         return vFlag(vFlagHelper(i & Int(f)));
231     }
232     constexpr inline vFlag operator~() const noexcept
233     {
234         return vFlag(vFlagHelper(~i));
235     }
236
237     constexpr inline bool operator!() const noexcept { return !i; }
238
239     constexpr inline bool testFlag(Enum f) const noexcept
240     {
241         return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f));
242     }
243     inline vFlag &setFlag(Enum f, bool on = true) noexcept
244     {
245         return on ? (*this |= f) : (*this &= ~f);
246     }
247
248     Int i;
249 };
250
251 class VColor {
252 public:
253     inline VColor() noexcept { a = r = g = b = 0; }
254     inline VColor(int red, int green, int blue, int alpha = 255) noexcept
255     {
256         r = red;
257         g = green;
258         b = blue;
259         a = alpha;
260     }
261     inline int  red() const noexcept { return r; }
262     inline int  green() const noexcept { return g; }
263     inline int  blue() const noexcept { return b; }
264     inline int  alpha() const noexcept { return a; }
265     inline void setRed(int red) noexcept { r = red; }
266     inline void setGreen(int green) noexcept { g = green; }
267     inline void setBlue(int blue) noexcept { b = blue; }
268     inline void setAlpha(int alpha) noexcept { a = alpha; }
269     inline bool isOpaque() const { return a == 255; }
270     inline bool operator==(const VColor &o) const
271     {
272         return ((a == o.a) && (r == o.r) && (g == o.g) && (b == o.b));
273     }
274     uint premulARGB() const
275     {
276         int pr = (r * a) / 255;
277         int pg = (g * a) / 255;
278         int pb = (b * a) / 255;
279         return uint((a << 24) | (pr << 16) | (pg << 8) | (pb));
280     }
281
282     uint premulARGB(float opacity) const
283     {
284         int alpha = a * opacity;
285         int pr = (r * alpha) / 255;
286         int pg = (g * alpha) / 255;
287         int pb = (b * alpha) / 255;
288         return uint((alpha << 24) | (pr << 16) | (pg << 8) | (pb));
289     }
290
291 public:
292     uchar a;
293     uchar r;
294     uchar g;
295     uchar b;
296 };
297
298 enum class FillRule: unsigned char { EvenOdd, Winding };
299 enum class JoinStyle: unsigned char { Miter, Bevel, Round };
300 enum class CapStyle: unsigned char { Flat, Square, Round };
301
302 #ifndef V_CONSTRUCTOR_FUNCTION
303 #define V_CONSTRUCTOR_FUNCTION0(AFUNC)            \
304     namespace {                                   \
305     static const struct AFUNC##_ctor_class_ {     \
306         inline AFUNC##_ctor_class_() { AFUNC(); } \
307     } AFUNC##_ctor_instance_;                     \
308     }
309
310 #define V_CONSTRUCTOR_FUNCTION(AFUNC) V_CONSTRUCTOR_FUNCTION0(AFUNC)
311 #endif
312
313 #endif  // VGLOBAL_H