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