• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkTypes_DEFINED
9 #define SkTypes_DEFINED
10 
11 // IWYU pragma: begin_exports
12 
13 // In at least two known scenarios when using GCC with libc++:
14 //  * GCC 4.8 targeting ARMv7 with NEON
15 //  * GCC 4.9 targeting ARMv8 64 bit
16 // we need to typedef float float32_t (or include <arm_neon.h> which does that)
17 // before #including <memory>. This makes no sense.  I'm not very interested in
18 // understanding why... these are old, bizarre platform configuration that we
19 // should just let die.
20 // See https://llvm.org/bugs/show_bug.cgi?id=25608 .
21 #include <ciso646>  // Include something innocuous to define _LIBCPP_VERISON if it's libc++.
22 #if defined(__GNUC__) && __GNUC__ == 4 \
23  && ((defined(__arm__) && (defined(__ARM_NEON__) || defined(__ARM_NEON))) || defined(__aarch64__)) \
24  && defined(_LIBCPP_VERSION)
25     typedef float float32_t;
26     #include <memory>
27 #endif
28 
29 #include "SkPreConfig.h"
30 #include "SkUserConfig.h"
31 #include "SkPostConfig.h"
32 #include <stddef.h>
33 #include <stdint.h>
34 // IWYU pragma: end_exports
35 
36 #include <string.h>
37 // TODO(herb): remove after chromuim skia/ext/SkMemory_new_handler.cpp
38 // has been updated to point to private/SkMalloc.h
39 #include "../private/SkMalloc.h"
40 
41 // enable to test new device-base clipping
42 //#define SK_USE_DEVICE_CLIPPING
43 
44 /** \file SkTypes.h
45 */
46 
47 /** See SkGraphics::GetVersion() to retrieve these at runtime
48  */
49 #define SKIA_VERSION_MAJOR  1
50 #define SKIA_VERSION_MINOR  0
51 #define SKIA_VERSION_PATCH  0
52 
53 
54 /** Called internally if we hit an unrecoverable error.
55     The platform implementation must not return, but should either throw
56     an exception or otherwise exit.
57 */
58 SK_API extern void sk_abort_no_print(void);
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 
62 #ifdef override_GLOBAL_NEW
63 #include <new>
64 
new(size_t size)65 inline void* operator new(size_t size) {
66     return sk_malloc_throw(size);
67 }
68 
delete(void * p)69 inline void operator delete(void* p) {
70     sk_free(p);
71 }
72 #endif
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 
76 #define SK_INIT_TO_AVOID_WARNING    = 0
77 
78 #ifndef SkDebugf
79     SK_API void SkDebugf(const char format[], ...);
80 #endif
81 
82 #define SkREQUIRE_SEMICOLON_AFTER(code) do { code } while (false)
83 
84 #define SkASSERT_RELEASE(cond) \
85     SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT(#cond); } )
86 
87 #ifdef SK_DEBUG
88     #define SkASSERT(cond) \
89         SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { SK_ABORT("assert(" #cond ")"); })
90     #define SkASSERTF(cond, fmt, ...) \
91         SkREQUIRE_SEMICOLON_AFTER(if (!(cond)) { \
92                                       SkDebugf(fmt"\n", __VA_ARGS__); \
93                                       SK_ABORT("assert(" #cond ")"); \
94                                   })
95     #define SkDEBUGFAIL(message)        SK_ABORT(message)
96     #define SkDEBUGFAILF(fmt, ...)      SkASSERTF(false, fmt, ##__VA_ARGS__)
97     #define SkDEBUGCODE(...)            __VA_ARGS__
98     #define SkDECLAREPARAM(type, var)   , type var
99     #define SkPARAM(var)                , var
100     #define SkDEBUGF(args       )       SkDebugf args
101     #define SkAssertResult(cond)        SkASSERT(cond)
102 #else
103     #define SkASSERT(cond)
104     #define SkASSERTF(cond, fmt, ...)
105     #define SkDEBUGFAIL(message)
106     #define SkDEBUGFAILF(fmt, ...)
107     #define SkDEBUGCODE(...)
108     #define SkDEBUGF(args)
109     #define SkDECLAREPARAM(type, var)
110     #define SkPARAM(var)
111 
112     // unlike SkASSERT, this guy executes its condition in the non-debug build.
113     // The if is present so that this can be used with functions marked SK_WARN_UNUSED_RESULT.
114     #define SkAssertResult(cond)         if (cond) {} do {} while(false)
115 #endif
116 
117 // Legacy macro names for SK_ABORT
118 #define SkFAIL(message)                 SK_ABORT(message)
119 #define sk_throw()                      SK_ABORT("sk_throw")
120 
121 #ifdef SK_IGNORE_TO_STRING
122     #define SK_TO_STRING_NONVIRT()
123     #define SK_TO_STRING_VIRT()
124     #define SK_TO_STRING_PUREVIRT()
125     #define SK_TO_STRING_OVERRIDE()
126 #else
127     class SkString;
128     // the 'toString' helper functions convert Sk* objects to human-readable
129     // form in developer mode
130     #define SK_TO_STRING_NONVIRT() void toString(SkString* str) const;
131     #define SK_TO_STRING_VIRT() virtual void toString(SkString* str) const;
132     #define SK_TO_STRING_PUREVIRT() virtual void toString(SkString* str) const = 0;
133     #define SK_TO_STRING_OVERRIDE() void toString(SkString* str) const override;
134 #endif
135 
136 /*
137  *  Usage:  SK_MACRO_CONCAT(a, b)   to construct the symbol ab
138  *
139  *  SK_MACRO_CONCAT_IMPL_PRIV just exists to make this work. Do not use directly
140  *
141  */
142 #define SK_MACRO_CONCAT(X, Y)           SK_MACRO_CONCAT_IMPL_PRIV(X, Y)
143 #define SK_MACRO_CONCAT_IMPL_PRIV(X, Y)  X ## Y
144 
145 /*
146  *  Usage: SK_MACRO_APPEND_LINE(foo)    to make foo123, where 123 is the current
147  *                                      line number. Easy way to construct
148  *                                      unique names for local functions or
149  *                                      variables.
150  */
151 #define SK_MACRO_APPEND_LINE(name)  SK_MACRO_CONCAT(name, __LINE__)
152 
153 /**
154  * For some classes, it's almost always an error to instantiate one without a name, e.g.
155  *   {
156  *       SkAutoMutexAcquire(&mutex);
157  *       <some code>
158  *   }
159  * In this case, the writer meant to hold mutex while the rest of the code in the block runs,
160  * but instead the mutex is acquired and then immediately released.  The correct usage is
161  *   {
162  *       SkAutoMutexAcquire lock(&mutex);
163  *       <some code>
164  *   }
165  *
166  * To prevent callers from instantiating your class without a name, use SK_REQUIRE_LOCAL_VAR
167  * like this:
168  *   class classname {
169  *       <your class>
170  *   };
171  *   #define classname(...) SK_REQUIRE_LOCAL_VAR(classname)
172  *
173  * This won't work with templates, and you must inline the class' constructors and destructors.
174  * Take a look at SkAutoFree and SkAutoMalloc in this file for examples.
175  */
176 #define SK_REQUIRE_LOCAL_VAR(classname) \
177     static_assert(false, "missing name for " #classname)
178 
179 ///////////////////////////////////////////////////////////////////////
180 
181 /**
182  *  Fast type for signed 8 bits. Use for parameter passing and local variables,
183  *  not for storage.
184  */
185 typedef int S8CPU;
186 
187 /**
188  *  Fast type for unsigned 8 bits. Use for parameter passing and local
189  *  variables, not for storage
190  */
191 typedef unsigned U8CPU;
192 
193 /**
194  *  Fast type for signed 16 bits. Use for parameter passing and local variables,
195  *  not for storage
196  */
197 typedef int S16CPU;
198 
199 /**
200  *  Fast type for unsigned 16 bits. Use for parameter passing and local
201  *  variables, not for storage
202  */
203 typedef unsigned U16CPU;
204 
205 /**
206  *  Meant to be a small version of bool, for storage purposes. Will be 0 or 1
207  */
208 typedef uint8_t SkBool8;
209 
210 #include "../private/SkTFitsIn.h"
SkTo(S s)211 template <typename D, typename S> D SkTo(S s) {
212     SkASSERT(SkTFitsIn<D>(s));
213     return static_cast<D>(s);
214 }
215 #define SkToS8(x)    SkTo<int8_t>(x)
216 #define SkToU8(x)    SkTo<uint8_t>(x)
217 #define SkToS16(x)   SkTo<int16_t>(x)
218 #define SkToU16(x)   SkTo<uint16_t>(x)
219 #define SkToS32(x)   SkTo<int32_t>(x)
220 #define SkToU32(x)   SkTo<uint32_t>(x)
221 #define SkToInt(x)   SkTo<int>(x)
222 #define SkToUInt(x)  SkTo<unsigned>(x)
223 #define SkToSizeT(x) SkTo<size_t>(x)
224 
225 /** Returns 0 or 1 based on the condition
226 */
227 #define SkToBool(cond)  ((cond) != 0)
228 
229 #define SK_MaxS16   32767
230 #define SK_MinS16   -32767
231 #define SK_MaxU16   0xFFFF
232 #define SK_MinU16   0
233 #define SK_MaxS32   0x7FFFFFFF
234 #define SK_MinS32   -SK_MaxS32
235 #define SK_MaxU32   0xFFFFFFFF
236 #define SK_MinU32   0
237 #define SK_NaN32    ((int) (1U << 31))
238 #define SK_MaxSizeT SIZE_MAX
239 
240 /** Returns true if the value can be represented with signed 16bits
241  */
SkIsS16(long x)242 static inline bool SkIsS16(long x) {
243     return (int16_t)x == x;
244 }
245 
246 /** Returns true if the value can be represented with unsigned 16bits
247  */
SkIsU16(long x)248 static inline bool SkIsU16(long x) {
249     return (uint16_t)x == x;
250 }
251 
SkLeftShift(int32_t value,int32_t shift)252 static inline int32_t SkLeftShift(int32_t value, int32_t shift) {
253     return (int32_t) ((uint32_t) value << shift);
254 }
255 
SkLeftShift(int64_t value,int32_t shift)256 static inline int64_t SkLeftShift(int64_t value, int32_t shift) {
257     return (int64_t) ((uint64_t) value << shift);
258 }
259 
260 //////////////////////////////////////////////////////////////////////////////
261 
262 /** Returns the number of entries in an array (not a pointer) */
263 template <typename T, size_t N> char (&SkArrayCountHelper(T (&array)[N]))[N];
264 #define SK_ARRAY_COUNT(array) (sizeof(SkArrayCountHelper(array)))
265 
266 // Can be used to bracket data types that must be dense, e.g. hash keys.
267 #if defined(__clang__)  // This should work on GCC too, but GCC diagnostic pop didn't seem to work!
268     #define SK_BEGIN_REQUIRE_DENSE _Pragma("GCC diagnostic push") \
269                                    _Pragma("GCC diagnostic error \"-Wpadded\"")
270     #define SK_END_REQUIRE_DENSE   _Pragma("GCC diagnostic pop")
271 #else
272     #define SK_BEGIN_REQUIRE_DENSE
273     #define SK_END_REQUIRE_DENSE
274 #endif
275 
276 #define SkAlign2(x)     (((x) + 1) >> 1 << 1)
277 #define SkIsAlign2(x)   (0 == ((x) & 1))
278 
279 #define SkAlign4(x)     (((x) + 3) >> 2 << 2)
280 #define SkIsAlign4(x)   (0 == ((x) & 3))
281 
282 #define SkAlign8(x)     (((x) + 7) >> 3 << 3)
283 #define SkIsAlign8(x)   (0 == ((x) & 7))
284 
285 #define SkAlign16(x)     (((x) + 15) >> 4 << 4)
286 #define SkIsAlign16(x)   (0 == ((x) & 15))
287 
288 #define SkAlignPtr(x)   (sizeof(void*) == 8 ?   SkAlign8(x) :   SkAlign4(x))
289 #define SkIsAlignPtr(x) (sizeof(void*) == 8 ? SkIsAlign8(x) : SkIsAlign4(x))
290 
291 typedef uint32_t SkFourByteTag;
292 #define SkSetFourByteTag(a, b, c, d)    (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
293 
294 /** 32 bit integer to hold a unicode value
295 */
296 typedef int32_t SkUnichar;
297 
298 /** 16 bit unsigned integer to hold a glyph index
299 */
300 typedef uint16_t SkGlyphID;
301 
302 /** 32 bit value to hold a millisecond duration
303  *  Note that SK_MSecMax is about 25 days.
304  */
305 typedef uint32_t SkMSec;
306 /** 1 second measured in milliseconds
307 */
308 #define SK_MSec1 1000
309 /** maximum representable milliseconds; 24d 20h 31m 23.647s.
310 */
311 #define SK_MSecMax 0x7FFFFFFF
312 /** Returns a < b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
313 */
314 #define SkMSec_LT(a, b)     ((int32_t)(a) - (int32_t)(b) < 0)
315 /** Returns a <= b for milliseconds, correctly handling wrap-around from 0xFFFFFFFF to 0
316 */
317 #define SkMSec_LE(a, b)     ((int32_t)(a) - (int32_t)(b) <= 0)
318 
319 /** The generation IDs in Skia reserve 0 has an invalid marker.
320  */
321 #define SK_InvalidGenID     0
322 /** The unique IDs in Skia reserve 0 has an invalid marker.
323  */
324 #define SK_InvalidUniqueID  0
325 
326 /****************************************************************************
327     The rest of these only build with C++
328 */
329 #ifdef __cplusplus
330 
331 /** Faster than SkToBool for integral conditions. Returns 0 or 1
332 */
Sk32ToBool(uint32_t n)333 static inline constexpr int Sk32ToBool(uint32_t n) {
334     return (n | (0-n)) >> 31;
335 }
336 
337 /** Generic swap function. Classes with efficient swaps should specialize this function to take
338     their fast path. This function is used by SkTSort. */
SkTSwap(T & a,T & b)339 template <typename T> static inline void SkTSwap(T& a, T& b) {
340     T c(std::move(a));
341     a = std::move(b);
342     b = std::move(c);
343 }
344 
SkAbs32(int32_t value)345 static inline int32_t SkAbs32(int32_t value) {
346     SkASSERT(value != SK_NaN32);  // The most negative int32_t can't be negated.
347     if (value < 0) {
348         value = -value;
349     }
350     return value;
351 }
352 
SkTAbs(T value)353 template <typename T> static inline T SkTAbs(T value) {
354     if (value < 0) {
355         value = -value;
356     }
357     return value;
358 }
359 
SkMax32(int32_t a,int32_t b)360 static inline int32_t SkMax32(int32_t a, int32_t b) {
361     if (a < b)
362         a = b;
363     return a;
364 }
365 
SkMin32(int32_t a,int32_t b)366 static inline int32_t SkMin32(int32_t a, int32_t b) {
367     if (a > b)
368         a = b;
369     return a;
370 }
371 
SkTMin(const T & a,const T & b)372 template <typename T> constexpr const T& SkTMin(const T& a, const T& b) {
373     return (a < b) ? a : b;
374 }
375 
SkTMax(const T & a,const T & b)376 template <typename T> constexpr const T& SkTMax(const T& a, const T& b) {
377     return (b < a) ? a : b;
378 }
379 
SkSign32(int32_t a)380 static inline int32_t SkSign32(int32_t a) {
381     return (a >> 31) | ((unsigned) -a >> 31);
382 }
383 
SkFastMin32(int32_t value,int32_t max)384 static inline int32_t SkFastMin32(int32_t value, int32_t max) {
385     if (value > max) {
386         value = max;
387     }
388     return value;
389 }
390 
391 /** Returns value pinned between min and max, inclusively. */
SkTPin(const T & value,const T & min,const T & max)392 template <typename T> static constexpr const T& SkTPin(const T& value, const T& min, const T& max) {
393     return SkTMax(SkTMin(value, max), min);
394 }
395 
396 
397 ///////////////////////////////////////////////////////////////////////////////
398 
399 /**
400  *  Indicates whether an allocation should count against a cache budget.
401  */
402 enum class SkBudgeted : bool {
403     kNo  = false,
404     kYes = true
405 };
406 
407 /**
408  * Indicates whether a backing store needs to be an exact match or can be larger
409  * than is strictly necessary
410  */
411 enum class SkBackingFit {
412     kApprox,
413     kExact
414 };
415 
416 ///////////////////////////////////////////////////////////////////////////////
417 
418 /** Use to combine multiple bits in a bitmask in a type safe way.
419  */
420 template <typename T>
SkTBitOr(T a,T b)421 T SkTBitOr(T a, T b) {
422     return (T)(a | b);
423 }
424 
425 /**
426  *  Use to cast a pointer to a different type, and maintaining strict-aliasing
427  */
SkTCast(const void * ptr)428 template <typename Dst> Dst SkTCast(const void* ptr) {
429     union {
430         const void* src;
431         Dst dst;
432     } data;
433     data.src = ptr;
434     return data.dst;
435 }
436 
437 //////////////////////////////////////////////////////////////////////////////
438 
439 /** \class SkNoncopyable
440 
441 SkNoncopyable is the base class for objects that do not want to
442 be copied. It hides its copy-constructor and its assignment-operator.
443 */
444 class SK_API SkNoncopyable {
445 public:
446     SkNoncopyable() = default;
447 
448     SkNoncopyable(SkNoncopyable&&) = default;
449     SkNoncopyable& operator =(SkNoncopyable&&) = default;
450 
451     SkNoncopyable(const SkNoncopyable&) = delete;
452     SkNoncopyable& operator=(const SkNoncopyable&) = delete;
453 };
454 
455 #endif /* C++ */
456 
457 #endif
458